1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <tools/debug.hxx>
22 #include <vcl/opengl/OpenGLContext.hxx>
23 #include <vcl/opengl/OpenGLHelper.hxx>
24 #include <opengl/zone.hxx>
26 #include <osx/saldata.hxx>
27 #include <osx/salframe.h>
28 #include <osx/salinst.h>
29 #include <osx/salobj.h>
30 #include <osx/runinmain.hxx>
32 #include <AppKit/NSOpenGLView.h>
34 AquaSalObject::AquaSalObject( AquaSalFrame
* pFrame
, SystemWindowData
const * pWindowData
) :
46 maSysData
.nSize
= sizeof( maSysData
);
47 maSysData
.mpNSView
= nullptr;
48 maSysData
.mbOpenGL
= false;
50 NSRect aInitFrame
= { NSZeroPoint
, { 20, 20 } };
51 mpClipView
= [[NSClipView alloc
] initWithFrame
: aInitFrame
];
54 [mpFrame
->getNSView() addSubview
: mpClipView
];
55 [mpClipView setHidden
: YES
];
57 if (pWindowData
&& pWindowData
->bOpenGL
)
59 maSysData
.mbOpenGL
= true;
60 SAL_WNODEPRECATED_DECLARATIONS_PUSH
61 // "'NSOpenGLPixelFormat' is deprecated: first deprecated in macOS 10.14 - Please use
62 // Metal or MetalKit."
63 NSOpenGLPixelFormat
* pixFormat
= nullptr;
64 SAL_WNODEPRECATED_DECLARATIONS_POP
66 if (pWindowData
->bLegacy
)
68 SAL_WNODEPRECATED_DECLARATIONS_PUSH
69 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS 10.14"
70 NSOpenGLPixelFormatAttribute
const aAttributes
[] =
71 SAL_WNODEPRECATED_DECLARATIONS_POP
73 SAL_WNODEPRECATED_DECLARATIONS_PUSH
74 // "'NSOpenGLPFADoubleBuffer' is deprecated: first deprecated in macOS 10.14",
75 // "'NSOpenGLPFAAlphaSize' is deprecated: first deprecated in macOS 10.14",
76 // "'NSOpenGLPFAColorSize' is deprecated: first deprecated in macOS 10.14",
77 // "'NSOpenGLPFADepthSize' is deprecated: first deprecated in macOS 10.14",
78 // "'NSOpenGLPFAMultisample' is deprecated: first deprecated in macOS 10.14",
79 // "'NSOpenGLPFASampleBuffers' is deprecated: first deprecated in macOS 10.14",
80 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS
82 // "'NSOpenGLPFASamples' is deprecated: first deprecated in macOS 10.14"
83 NSOpenGLPFADoubleBuffer
,
84 NSOpenGLPFAAlphaSize
, 8,
85 NSOpenGLPFAColorSize
, 24,
86 NSOpenGLPFADepthSize
, 24,
87 NSOpenGLPFAMultisample
,
88 NSOpenGLPFASampleBuffers
, NSOpenGLPixelFormatAttribute(1),
89 NSOpenGLPFASamples
, NSOpenGLPixelFormatAttribute(4),
90 SAL_WNODEPRECATED_DECLARATIONS_POP
93 SAL_WNODEPRECATED_DECLARATIONS_PUSH
94 // "'NSOpenGLPixelFormat' is deprecated: first deprecated in macOS 10.14 - Please
95 // use Metal or MetalKit."
96 pixFormat
= [[NSOpenGLPixelFormat alloc
] initWithAttributes
:aAttributes
];
97 SAL_WNODEPRECATED_DECLARATIONS_POP
101 SAL_WNODEPRECATED_DECLARATIONS_PUSH
102 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS 10.14"
103 NSOpenGLPixelFormatAttribute
const aAttributes
[] =
104 SAL_WNODEPRECATED_DECLARATIONS_POP
106 SAL_WNODEPRECATED_DECLARATIONS_PUSH
107 // "'NSOpenGLPFAOpenGLProfile' is deprecated: first deprecated in macOS 10.14",
108 // "'NSOpenGLProfileVersion3_2Core' is deprecated: first deprecated in macOS
110 // "'NSOpenGLPFADoubleBuffer' is deprecated: first deprecated in macOS 10.14",
111 // "'NSOpenGLPFAAlphaSize' is deprecated: first deprecated in macOS 10.14",
112 // "'NSOpenGLPFAColorSize' is deprecated: first deprecated in macOS 10.14",
113 // "'NSOpenGLPFADepthSize' is deprecated: first deprecated in macOS 10.14",
114 // "'NSOpenGLPFAMultisample' is deprecated: first deprecated in macOS 10.14",
115 // "'NSOpenGLPFASampleBuffers' is deprecated: first deprecated in macOS 10.14",
116 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS
118 // "'NSOpenGLPFASamples' is deprecated: first deprecated in macOS 10.14"
119 NSOpenGLPFAOpenGLProfile
, NSOpenGLProfileVersion3_2Core
,
120 NSOpenGLPFADoubleBuffer
,
121 NSOpenGLPFAAlphaSize
, 8,
122 NSOpenGLPFAColorSize
, 24,
123 NSOpenGLPFADepthSize
, 24,
124 NSOpenGLPFAMultisample
,
125 NSOpenGLPFASampleBuffers
, NSOpenGLPixelFormatAttribute(1),
126 NSOpenGLPFASamples
, NSOpenGLPixelFormatAttribute(4),
127 SAL_WNODEPRECATED_DECLARATIONS_POP
130 SAL_WNODEPRECATED_DECLARATIONS_PUSH
131 // "'NSOpenGLPixelFormat' is deprecated: first deprecated in macOS 10.14 - Please
132 // use Metal or MetalKit."
133 pixFormat
= [[NSOpenGLPixelFormat alloc
] initWithAttributes
:aAttributes
];
134 SAL_WNODEPRECATED_DECLARATIONS_POP
137 SAL_WNODEPRECATED_DECLARATIONS_PUSH
138 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
140 maSysData
.mpNSView
= [[NSOpenGLView alloc
] initWithFrame
: aInitFrame pixelFormat
:pixFormat
];
141 SAL_WNODEPRECATED_DECLARATIONS_POP
145 maSysData
.mpNSView
= [[NSView alloc
] initWithFrame
: aInitFrame
];
148 if( maSysData
.mpNSView
)
151 [mpClipView setDocumentView
: maSysData
.mpNSView
];
155 AquaSalObject::~AquaSalObject()
157 assert( GetSalData()->mpInstance
->IsMainThread() );
159 if( maSysData
.mpNSView
)
161 NSView
*pView
= maSysData
.mpNSView
;
162 [pView removeFromSuperview
];
167 [mpClipView removeFromSuperview
];
168 [mpClipView release
];
172 // Please note that the talk about QTMovieView below presumably refers
173 // to stuff in the QuickTime avmedia thingie, and that QuickTime is
174 // deprecated, not available for 64-bit code, and won't thus be used
175 // in a "modern" build of LO anyway. So the relevance of the comment
179 sadly there seems to be no way to impose clipping on a child view,
180 especially a QTMovieView which seems to ignore the current context
181 completely. Also there is no real way to shape a window; on Aqua a
182 similar effect to non-rectangular windows is achieved by using a
183 non-opaque window and not painting where one wants the background
186 With respect to SalObject this leaves us to having an NSClipView
187 containing the child view. Even a QTMovieView respects the boundaries of
188 that, which gives us a clip "region" consisting of one rectangle.
189 This is gives us an 80% solution only, though.
192 void AquaSalObject::ResetClipRegion()
198 void AquaSalObject::BeginSetClipRegion( sal_uInt32
)
203 void AquaSalObject::UnionClipRegion( long nX
, long nY
, long nWidth
, long nHeight
)
209 mnClipWidth
+= mnClipX
- nX
;
212 if( nX
+ nWidth
> mnClipX
+ mnClipWidth
)
213 mnClipWidth
= nX
+ nWidth
- mnClipX
;
216 mnClipHeight
+= mnClipY
- nY
;
219 if( nY
+ nHeight
> mnClipY
+ mnClipHeight
)
220 mnClipHeight
= nY
+ nHeight
- mnClipY
;
226 mnClipWidth
= nWidth
;
227 mnClipHeight
= nHeight
;
232 void AquaSalObject::EndSetClipRegion()
237 void AquaSalObject::SetPosSize( long nX
, long nY
, long nWidth
, long nHeight
)
246 void AquaSalObject::setClippedPosSize()
248 OSX_SALDATA_RUNINMAIN( setClippedPosSize() )
250 NSRect aViewRect
= { NSZeroPoint
, NSMakeSize( mnWidth
, mnHeight
) };
251 if( maSysData
.mpNSView
)
253 NSView
* pNSView
= maSysData
.mpNSView
;
254 [pNSView setFrame
: aViewRect
];
257 NSRect aClipViewRect
= NSMakeRect( mnX
, mnY
, mnWidth
, mnHeight
);
258 NSPoint aClipPt
= NSZeroPoint
;
261 aClipViewRect
.origin
.x
+= mnClipX
;
262 aClipViewRect
.origin
.y
+= mnClipY
;
263 aClipViewRect
.size
.width
= mnClipWidth
;
264 aClipViewRect
.size
.height
= mnClipHeight
;
267 aClipPt
.y
= mnHeight
- mnClipHeight
;
270 mpFrame
->VCLToCocoa( aClipViewRect
, false );
271 [mpClipView setFrame
: aClipViewRect
];
273 [mpClipView scrollToPoint
: aClipPt
];
276 void AquaSalObject::Show( bool bVisible
)
281 OSX_SALDATA_RUNINMAIN( Show( bVisible
) )
283 [mpClipView setHidden
: (bVisible
? NO
: YES
)];
286 const SystemEnvData
* AquaSalObject::GetSystemData() const
291 class AquaOpenGLContext
: public OpenGLContext
294 virtual void initWindow() override
;
299 virtual const GLWindow
& getOpenGLWindow() const override
{ return m_aGLWin
; }
300 virtual GLWindow
& getModifiableOpenGLWindow() override
{ return m_aGLWin
; }
301 SAL_WNODEPRECATED_DECLARATIONS_PUSH
302 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
304 NSOpenGLView
* getOpenGLView();
305 SAL_WNODEPRECATED_DECLARATIONS_POP
306 virtual bool ImplInit() override
;
307 virtual SystemWindowData
generateWinData(vcl::Window
* pParent
, bool bRequestLegacyContext
) override
;
308 virtual void makeCurrent() override
;
309 virtual void destroyCurrentContext() override
;
310 virtual void resetCurrent() override
;
311 virtual void swapBuffers() override
;
314 void AquaOpenGLContext::resetCurrent()
316 OSX_SALDATA_RUNINMAIN( resetCurrent() )
322 (void) this; // loplugin:staticmethods
323 SAL_WNODEPRECATED_DECLARATIONS_PUSH
324 // "'NSOpenGLContext' is deprecated: first deprecated in macOS 10.14 - Please use Metal or
326 [NSOpenGLContext clearCurrentContext
];
327 SAL_WNODEPRECATED_DECLARATIONS_POP
330 void AquaOpenGLContext::makeCurrent()
332 OSX_SALDATA_RUNINMAIN( makeCurrent() )
341 SAL_WNODEPRECATED_DECLARATIONS_PUSH
342 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
344 NSOpenGLView
* pView
= getOpenGLView();
345 SAL_WNODEPRECATED_DECLARATIONS_POP
346 [[pView openGLContext
] makeCurrentContext
];
351 void AquaOpenGLContext::swapBuffers()
353 OSX_SALDATA_RUNINMAIN( swapBuffers() )
357 SAL_WNODEPRECATED_DECLARATIONS_PUSH
358 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
360 NSOpenGLView
* pView
= getOpenGLView();
361 SAL_WNODEPRECATED_DECLARATIONS_POP
362 [[pView openGLContext
] flushBuffer
];
367 SystemWindowData
AquaOpenGLContext::generateWinData(vcl::Window
* /*pParent*/, bool bRequestLegacyContext
)
369 SystemWindowData aWinData
;
370 aWinData
.bOpenGL
= true;
371 aWinData
.bLegacy
= bRequestLegacyContext
;
375 void AquaOpenGLContext::destroyCurrentContext()
377 OSX_SALDATA_RUNINMAIN( destroyCurrentContext() )
378 SAL_WNODEPRECATED_DECLARATIONS_PUSH
379 // "'NSOpenGLContext' is deprecated: first deprecated in macOS 10.14 - Please use Metal or
381 [NSOpenGLContext clearCurrentContext
];
382 SAL_WNODEPRECATED_DECLARATIONS_POP
385 void AquaOpenGLContext::initWindow()
387 OSX_SALDATA_RUNINMAIN( initWindow() )
389 if( !m_pChildWindow
)
391 SystemWindowData winData
= generateWinData(mpWindow
, mbRequestLegacyContext
);
392 m_pChildWindow
= VclPtr
<SystemChildWindow
>::Create(mpWindow
, 0, &winData
, false);
397 InitChildWindow(m_pChildWindow
.get());
401 bool AquaOpenGLContext::ImplInit()
403 OSX_SALDATA_RUNINMAIN_UNION( ImplInit(), boolean
)
407 VCL_GL_INFO("OpenGLContext::ImplInit----start");
408 SAL_WNODEPRECATED_DECLARATIONS_PUSH
409 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
411 NSOpenGLView
* pView
= getOpenGLView();
412 SAL_WNODEPRECATED_DECLARATIONS_POP
413 [[pView openGLContext
] makeCurrentContext
];
415 bool bRet
= InitGL();
420 SAL_WNODEPRECATED_DECLARATIONS_PUSH
421 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
423 NSOpenGLView
* AquaOpenGLContext::getOpenGLView()
424 SAL_WNODEPRECATED_DECLARATIONS_POP
426 SAL_WNODEPRECATED_DECLARATIONS_PUSH
427 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
429 return reinterpret_cast<NSOpenGLView
*>(m_pChildWindow
->GetSystemData()->mpNSView
);
430 SAL_WNODEPRECATED_DECLARATIONS_POP
433 OpenGLContext
* AquaSalInstance::CreateOpenGLContext()
435 OSX_SALDATA_RUNINMAIN_POINTER( CreateOpenGLContext(), OpenGLContext
* )
436 return new AquaOpenGLContext
;
439 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */