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
.mpNSView
= nullptr;
47 maSysData
.mbOpenGL
= false;
49 NSRect aInitFrame
= { NSZeroPoint
, { 20, 20 } };
50 mpClipView
= [[NSClipView alloc
] initWithFrame
: aInitFrame
];
53 [mpFrame
->getNSView() addSubview
: mpClipView
];
54 [mpClipView setHidden
: YES
];
56 if (pWindowData
&& pWindowData
->bOpenGL
)
58 maSysData
.mbOpenGL
= true;
59 SAL_WNODEPRECATED_DECLARATIONS_PUSH
60 // "'NSOpenGLPixelFormat' is deprecated: first deprecated in macOS 10.14 - Please use
61 // Metal or MetalKit."
62 NSOpenGLPixelFormat
* pixFormat
= nullptr;
63 SAL_WNODEPRECATED_DECLARATIONS_POP
65 if (pWindowData
->bLegacy
)
67 SAL_WNODEPRECATED_DECLARATIONS_PUSH
68 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS 10.14"
69 NSOpenGLPixelFormatAttribute
const aAttributes
[] =
70 SAL_WNODEPRECATED_DECLARATIONS_POP
72 SAL_WNODEPRECATED_DECLARATIONS_PUSH
73 // "'NSOpenGLPFADoubleBuffer' is deprecated: first deprecated in macOS 10.14",
74 // "'NSOpenGLPFAAlphaSize' is deprecated: first deprecated in macOS 10.14",
75 // "'NSOpenGLPFAColorSize' is deprecated: first deprecated in macOS 10.14",
76 // "'NSOpenGLPFADepthSize' is deprecated: first deprecated in macOS 10.14",
77 // "'NSOpenGLPFAMultisample' is deprecated: first deprecated in macOS 10.14",
78 // "'NSOpenGLPFASampleBuffers' is deprecated: first deprecated in macOS 10.14",
79 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS
81 // "'NSOpenGLPFASamples' is deprecated: first deprecated in macOS 10.14"
82 NSOpenGLPFADoubleBuffer
,
83 NSOpenGLPFAAlphaSize
, 8,
84 NSOpenGLPFAColorSize
, 24,
85 NSOpenGLPFADepthSize
, 24,
86 NSOpenGLPFAMultisample
,
87 NSOpenGLPFASampleBuffers
, NSOpenGLPixelFormatAttribute(1),
88 NSOpenGLPFASamples
, NSOpenGLPixelFormatAttribute(4),
89 SAL_WNODEPRECATED_DECLARATIONS_POP
92 SAL_WNODEPRECATED_DECLARATIONS_PUSH
93 // "'NSOpenGLPixelFormat' is deprecated: first deprecated in macOS 10.14 - Please
94 // use Metal or MetalKit."
95 pixFormat
= [[NSOpenGLPixelFormat alloc
] initWithAttributes
:aAttributes
];
96 SAL_WNODEPRECATED_DECLARATIONS_POP
100 SAL_WNODEPRECATED_DECLARATIONS_PUSH
101 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS 10.14"
102 NSOpenGLPixelFormatAttribute
const aAttributes
[] =
103 SAL_WNODEPRECATED_DECLARATIONS_POP
105 SAL_WNODEPRECATED_DECLARATIONS_PUSH
106 // "'NSOpenGLPFAOpenGLProfile' is deprecated: first deprecated in macOS 10.14",
107 // "'NSOpenGLProfileVersion3_2Core' is deprecated: first deprecated in macOS
109 // "'NSOpenGLPFADoubleBuffer' is deprecated: first deprecated in macOS 10.14",
110 // "'NSOpenGLPFAAlphaSize' is deprecated: first deprecated in macOS 10.14",
111 // "'NSOpenGLPFAColorSize' is deprecated: first deprecated in macOS 10.14",
112 // "'NSOpenGLPFADepthSize' is deprecated: first deprecated in macOS 10.14",
113 // "'NSOpenGLPFAMultisample' is deprecated: first deprecated in macOS 10.14",
114 // "'NSOpenGLPFASampleBuffers' is deprecated: first deprecated in macOS 10.14",
115 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS
117 // "'NSOpenGLPFASamples' is deprecated: first deprecated in macOS 10.14"
118 NSOpenGLPFAOpenGLProfile
, NSOpenGLProfileVersion3_2Core
,
119 NSOpenGLPFADoubleBuffer
,
120 NSOpenGLPFAAlphaSize
, 8,
121 NSOpenGLPFAColorSize
, 24,
122 NSOpenGLPFADepthSize
, 24,
123 NSOpenGLPFAMultisample
,
124 NSOpenGLPFASampleBuffers
, NSOpenGLPixelFormatAttribute(1),
125 NSOpenGLPFASamples
, NSOpenGLPixelFormatAttribute(4),
126 SAL_WNODEPRECATED_DECLARATIONS_POP
129 SAL_WNODEPRECATED_DECLARATIONS_PUSH
130 // "'NSOpenGLPixelFormat' is deprecated: first deprecated in macOS 10.14 - Please
131 // use Metal or MetalKit."
132 pixFormat
= [[NSOpenGLPixelFormat alloc
] initWithAttributes
:aAttributes
];
133 SAL_WNODEPRECATED_DECLARATIONS_POP
136 SAL_WNODEPRECATED_DECLARATIONS_PUSH
137 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
139 maSysData
.mpNSView
= [[NSOpenGLView alloc
] initWithFrame
: aInitFrame pixelFormat
:pixFormat
];
140 SAL_WNODEPRECATED_DECLARATIONS_POP
144 maSysData
.mpNSView
= [[NSView alloc
] initWithFrame
: aInitFrame
];
147 if( maSysData
.mpNSView
)
150 [mpClipView setDocumentView
: maSysData
.mpNSView
];
154 AquaSalObject::~AquaSalObject()
156 assert( GetSalData()->mpInstance
->IsMainThread() );
158 if( maSysData
.mpNSView
)
160 NSView
*pView
= maSysData
.mpNSView
;
161 [pView removeFromSuperview
];
166 [mpClipView removeFromSuperview
];
167 [mpClipView release
];
171 // Please note that the talk about QTMovieView below presumably refers
172 // to stuff in the QuickTime avmedia thingie, and that QuickTime is
173 // deprecated, not available for 64-bit code, and won't thus be used
174 // in a "modern" build of LO anyway. So the relevance of the comment
178 sadly there seems to be no way to impose clipping on a child view,
179 especially a QTMovieView which seems to ignore the current context
180 completely. Also there is no real way to shape a window; on Aqua a
181 similar effect to non-rectangular windows is achieved by using a
182 non-opaque window and not painting where one wants the background
185 With respect to SalObject this leaves us to having an NSClipView
186 containing the child view. Even a QTMovieView respects the boundaries of
187 that, which gives us a clip "region" consisting of one rectangle.
188 This is gives us an 80% solution only, though.
191 void AquaSalObject::ResetClipRegion()
197 void AquaSalObject::BeginSetClipRegion( sal_uInt32
)
202 void AquaSalObject::UnionClipRegion(
203 tools::Long nX
, tools::Long nY
, tools::Long nWidth
, tools::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(
238 tools::Long nX
, tools::Long nY
, tools::Long nWidth
, tools::Long nHeight
)
247 void AquaSalObject::setClippedPosSize()
249 OSX_SALDATA_RUNINMAIN( setClippedPosSize() )
251 NSRect aViewRect
= { NSZeroPoint
, NSMakeSize( mnWidth
, mnHeight
) };
252 if( maSysData
.mpNSView
)
254 NSView
* pNSView
= maSysData
.mpNSView
;
255 [pNSView setFrame
: aViewRect
];
258 NSRect aClipViewRect
= NSMakeRect( mnX
, mnY
, mnWidth
, mnHeight
);
259 NSPoint aClipPt
= NSZeroPoint
;
262 aClipViewRect
.origin
.x
+= mnClipX
;
263 aClipViewRect
.origin
.y
+= mnClipY
;
264 aClipViewRect
.size
.width
= mnClipWidth
;
265 aClipViewRect
.size
.height
= mnClipHeight
;
268 aClipPt
.y
= mnHeight
- mnClipHeight
;
271 mpFrame
->VCLToCocoa( aClipViewRect
, false );
272 [mpClipView setFrame
: aClipViewRect
];
274 [mpClipView scrollToPoint
: aClipPt
];
277 void AquaSalObject::Show( bool bVisible
)
282 OSX_SALDATA_RUNINMAIN( Show( bVisible
) )
284 [mpClipView setHidden
: (bVisible
? NO
: YES
)];
287 const SystemEnvData
& AquaSalObject::GetSystemData() const
294 class AquaOpenGLContext
: public OpenGLContext
297 virtual void initWindow() override
;
302 virtual const GLWindow
& getOpenGLWindow() const override
{ return m_aGLWin
; }
303 virtual GLWindow
& getModifiableOpenGLWindow() override
{ return m_aGLWin
; }
304 SAL_WNODEPRECATED_DECLARATIONS_PUSH
305 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
307 NSOpenGLView
* getOpenGLView();
308 SAL_WNODEPRECATED_DECLARATIONS_POP
309 virtual bool ImplInit() override
;
310 virtual SystemWindowData
generateWinData(vcl::Window
* pParent
, bool bRequestLegacyContext
) override
;
311 virtual void makeCurrent() override
;
312 virtual void destroyCurrentContext() override
;
313 virtual void resetCurrent() override
;
314 virtual void swapBuffers() override
;
319 void AquaOpenGLContext::resetCurrent()
321 OSX_SALDATA_RUNINMAIN( resetCurrent() )
327 (void) this; // loplugin:staticmethods
328 SAL_WNODEPRECATED_DECLARATIONS_PUSH
329 // "'NSOpenGLContext' is deprecated: first deprecated in macOS 10.14 - Please use Metal or
331 [NSOpenGLContext clearCurrentContext
];
332 SAL_WNODEPRECATED_DECLARATIONS_POP
335 void AquaOpenGLContext::makeCurrent()
337 OSX_SALDATA_RUNINMAIN( makeCurrent() )
346 SAL_WNODEPRECATED_DECLARATIONS_PUSH
347 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
349 NSOpenGLView
* pView
= getOpenGLView();
350 SAL_WNODEPRECATED_DECLARATIONS_POP
351 [[pView openGLContext
] makeCurrentContext
];
356 void AquaOpenGLContext::swapBuffers()
358 OSX_SALDATA_RUNINMAIN( swapBuffers() )
362 SAL_WNODEPRECATED_DECLARATIONS_PUSH
363 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
365 NSOpenGLView
* pView
= getOpenGLView();
366 SAL_WNODEPRECATED_DECLARATIONS_POP
367 [[pView openGLContext
] flushBuffer
];
372 SystemWindowData
AquaOpenGLContext::generateWinData(vcl::Window
* /*pParent*/, bool bRequestLegacyContext
)
374 SystemWindowData aWinData
;
375 aWinData
.bOpenGL
= true;
376 aWinData
.bLegacy
= bRequestLegacyContext
;
380 void AquaOpenGLContext::destroyCurrentContext()
382 OSX_SALDATA_RUNINMAIN( destroyCurrentContext() )
383 SAL_WNODEPRECATED_DECLARATIONS_PUSH
384 // "'NSOpenGLContext' is deprecated: first deprecated in macOS 10.14 - Please use Metal or
386 [NSOpenGLContext clearCurrentContext
];
387 SAL_WNODEPRECATED_DECLARATIONS_POP
390 void AquaOpenGLContext::initWindow()
392 OSX_SALDATA_RUNINMAIN( initWindow() )
394 if( !m_pChildWindow
)
396 SystemWindowData winData
= generateWinData(mpWindow
, mbRequestLegacyContext
);
397 m_pChildWindow
= VclPtr
<SystemChildWindow
>::Create(mpWindow
, 0, &winData
, false);
402 InitChildWindow(m_pChildWindow
.get());
406 bool AquaOpenGLContext::ImplInit()
408 OSX_SALDATA_RUNINMAIN_UNION( ImplInit(), boolean
)
412 VCL_GL_INFO("OpenGLContext::ImplInit----start");
413 SAL_WNODEPRECATED_DECLARATIONS_PUSH
414 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
416 NSOpenGLView
* pView
= getOpenGLView();
417 SAL_WNODEPRECATED_DECLARATIONS_POP
418 [[pView openGLContext
] makeCurrentContext
];
420 bool bRet
= InitGL();
425 SAL_WNODEPRECATED_DECLARATIONS_PUSH
426 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
428 NSOpenGLView
* AquaOpenGLContext::getOpenGLView()
429 SAL_WNODEPRECATED_DECLARATIONS_POP
431 SAL_WNODEPRECATED_DECLARATIONS_PUSH
432 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
434 return reinterpret_cast<NSOpenGLView
*>(m_pChildWindow
->GetSystemData()->mpNSView
);
435 SAL_WNODEPRECATED_DECLARATIONS_POP
438 OpenGLContext
* AquaSalInstance::CreateOpenGLContext()
440 OSX_SALDATA_RUNINMAIN_POINTER( CreateOpenGLContext(), OpenGLContext
* )
441 return new AquaOpenGLContext
;
444 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */