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( long nX
, long nY
, long nWidth
, long nHeight
)
208 mnClipWidth
+= mnClipX
- nX
;
211 if( nX
+ nWidth
> mnClipX
+ mnClipWidth
)
212 mnClipWidth
= nX
+ nWidth
- mnClipX
;
215 mnClipHeight
+= mnClipY
- nY
;
218 if( nY
+ nHeight
> mnClipY
+ mnClipHeight
)
219 mnClipHeight
= nY
+ nHeight
- mnClipY
;
225 mnClipWidth
= nWidth
;
226 mnClipHeight
= nHeight
;
231 void AquaSalObject::EndSetClipRegion()
236 void AquaSalObject::SetPosSize( long nX
, long nY
, long nWidth
, long nHeight
)
245 void AquaSalObject::setClippedPosSize()
247 OSX_SALDATA_RUNINMAIN( setClippedPosSize() )
249 NSRect aViewRect
= { NSZeroPoint
, NSMakeSize( mnWidth
, mnHeight
) };
250 if( maSysData
.mpNSView
)
252 NSView
* pNSView
= maSysData
.mpNSView
;
253 [pNSView setFrame
: aViewRect
];
256 NSRect aClipViewRect
= NSMakeRect( mnX
, mnY
, mnWidth
, mnHeight
);
257 NSPoint aClipPt
= NSZeroPoint
;
260 aClipViewRect
.origin
.x
+= mnClipX
;
261 aClipViewRect
.origin
.y
+= mnClipY
;
262 aClipViewRect
.size
.width
= mnClipWidth
;
263 aClipViewRect
.size
.height
= mnClipHeight
;
266 aClipPt
.y
= mnHeight
- mnClipHeight
;
269 mpFrame
->VCLToCocoa( aClipViewRect
, false );
270 [mpClipView setFrame
: aClipViewRect
];
272 [mpClipView scrollToPoint
: aClipPt
];
275 void AquaSalObject::Show( bool bVisible
)
280 OSX_SALDATA_RUNINMAIN( Show( bVisible
) )
282 [mpClipView setHidden
: (bVisible
? NO
: YES
)];
285 const SystemEnvData
* AquaSalObject::GetSystemData() const
290 class AquaOpenGLContext
: public OpenGLContext
293 virtual void initWindow() override
;
298 virtual const GLWindow
& getOpenGLWindow() const override
{ return m_aGLWin
; }
299 virtual GLWindow
& getModifiableOpenGLWindow() override
{ return m_aGLWin
; }
300 SAL_WNODEPRECATED_DECLARATIONS_PUSH
301 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
303 NSOpenGLView
* getOpenGLView();
304 SAL_WNODEPRECATED_DECLARATIONS_POP
305 virtual bool ImplInit() override
;
306 virtual SystemWindowData
generateWinData(vcl::Window
* pParent
, bool bRequestLegacyContext
) override
;
307 virtual void makeCurrent() override
;
308 virtual void destroyCurrentContext() override
;
309 virtual void resetCurrent() override
;
310 virtual void swapBuffers() override
;
313 void AquaOpenGLContext::resetCurrent()
315 OSX_SALDATA_RUNINMAIN( resetCurrent() )
321 (void) this; // loplugin:staticmethods
322 SAL_WNODEPRECATED_DECLARATIONS_PUSH
323 // "'NSOpenGLContext' is deprecated: first deprecated in macOS 10.14 - Please use Metal or
325 [NSOpenGLContext clearCurrentContext
];
326 SAL_WNODEPRECATED_DECLARATIONS_POP
329 void AquaOpenGLContext::makeCurrent()
331 OSX_SALDATA_RUNINMAIN( makeCurrent() )
340 SAL_WNODEPRECATED_DECLARATIONS_PUSH
341 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
343 NSOpenGLView
* pView
= getOpenGLView();
344 SAL_WNODEPRECATED_DECLARATIONS_POP
345 [[pView openGLContext
] makeCurrentContext
];
350 void AquaOpenGLContext::swapBuffers()
352 OSX_SALDATA_RUNINMAIN( swapBuffers() )
356 SAL_WNODEPRECATED_DECLARATIONS_PUSH
357 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
359 NSOpenGLView
* pView
= getOpenGLView();
360 SAL_WNODEPRECATED_DECLARATIONS_POP
361 [[pView openGLContext
] flushBuffer
];
366 SystemWindowData
AquaOpenGLContext::generateWinData(vcl::Window
* /*pParent*/, bool bRequestLegacyContext
)
368 SystemWindowData aWinData
;
369 aWinData
.bOpenGL
= true;
370 aWinData
.bLegacy
= bRequestLegacyContext
;
374 void AquaOpenGLContext::destroyCurrentContext()
376 OSX_SALDATA_RUNINMAIN( destroyCurrentContext() )
377 SAL_WNODEPRECATED_DECLARATIONS_PUSH
378 // "'NSOpenGLContext' is deprecated: first deprecated in macOS 10.14 - Please use Metal or
380 [NSOpenGLContext clearCurrentContext
];
381 SAL_WNODEPRECATED_DECLARATIONS_POP
384 void AquaOpenGLContext::initWindow()
386 OSX_SALDATA_RUNINMAIN( initWindow() )
388 if( !m_pChildWindow
)
390 SystemWindowData winData
= generateWinData(mpWindow
, mbRequestLegacyContext
);
391 m_pChildWindow
= VclPtr
<SystemChildWindow
>::Create(mpWindow
, 0, &winData
, false);
396 InitChildWindow(m_pChildWindow
.get());
400 bool AquaOpenGLContext::ImplInit()
402 OSX_SALDATA_RUNINMAIN_UNION( ImplInit(), boolean
)
406 VCL_GL_INFO("OpenGLContext::ImplInit----start");
407 SAL_WNODEPRECATED_DECLARATIONS_PUSH
408 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
410 NSOpenGLView
* pView
= getOpenGLView();
411 SAL_WNODEPRECATED_DECLARATIONS_POP
412 [[pView openGLContext
] makeCurrentContext
];
414 bool bRet
= InitGL();
419 SAL_WNODEPRECATED_DECLARATIONS_PUSH
420 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
422 NSOpenGLView
* AquaOpenGLContext::getOpenGLView()
423 SAL_WNODEPRECATED_DECLARATIONS_POP
425 SAL_WNODEPRECATED_DECLARATIONS_PUSH
426 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
428 return reinterpret_cast<NSOpenGLView
*>(m_pChildWindow
->GetSystemData()->mpNSView
);
429 SAL_WNODEPRECATED_DECLARATIONS_POP
432 OpenGLContext
* AquaSalInstance::CreateOpenGLContext()
434 OSX_SALDATA_RUNINMAIN_POINTER( CreateOpenGLContext(), OpenGLContext
* )
435 return new AquaOpenGLContext
;
438 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */