0030748: Visualization - Marker displayed in immediate layer ruins QT Quick view...
[occt.git] / src / OpenGl / OpenGl_Window_1.mm
CommitLineData
6aca4d39 1// Created on: 2012-11-12
4fe56619 2// Created by: Kirill Gavrilov
6aca4d39 3// Copyright (c) 2012-2014 OPEN CASCADE SAS
4fe56619 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
4fe56619 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
4fe56619 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
4fe56619 15
16#if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
17
896faa72 18#ifndef GL_GLEXT_LEGACY
0e9d3b83 19#define GL_GLEXT_LEGACY // To prevent inclusion of system glext.h on Mac OS X 10.6.8
896faa72 20#endif
0e9d3b83 21
42451ec0 22// macOS 10.4 deprecated OpenGL framework - suppress useless warnings
23#define GL_SILENCE_DEPRECATION
24
a2e4f780 25#import <TargetConditionals.h>
26
27#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
28 #import <UIKit/UIKit.h>
29#else
30 #import <Cocoa/Cocoa.h>
31
32#if !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7)
33@interface NSView (LionAPI)
34- (NSSize )convertSizeToBacking: (NSSize )theSize;
35@end
36#endif
37
38#endif
4fe56619 39
4fe56619 40#include <OpenGl_Window.hxx>
a2e4f780 41#include <OpenGl_FrameBuffer.hxx>
4fe56619 42
43#include <OpenGl_Context.hxx>
4fe56619 44#include <Aspect_GraphicDeviceDefinitionError.hxx>
45#include <Cocoa_LocalPool.hxx>
46#include <TCollection_AsciiString.hxx>
4e1523ef 47#include <TCollection_ExtendedString.hxx>
4fe56619 48
a2e4f780 49#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
50 //
51#else
52 #include <OpenGL/CGLRenderers.h>
53#endif
abe46077 54
4fe56619 55// =======================================================================
56// function : OpenGl_Window
57// purpose :
58// =======================================================================
c7ccbb77 59OpenGl_Window::OpenGl_Window (const Handle(OpenGl_GraphicDriver)& theDriver,
c357e426 60 const Handle(Aspect_Window)& thePlatformWindow,
4fe56619 61 Aspect_RenderingContext theGContext,
58655684 62 const Handle(OpenGl_Caps)& theCaps,
4fe56619 63 const Handle(OpenGl_Context)& theShareCtx)
73192b37 64: myGlContext (new OpenGl_Context (theCaps)),
4fe56619 65 myOwnGContext (theGContext == 0),
c357e426 66 myPlatformWindow (thePlatformWindow),
67#if defined(__APPLE__) && defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
a2e4f780 68 myUIView (NULL),
69#endif
f978241f 70 mySwapInterval (theCaps->swapInterval)
4fe56619 71{
5ef127d0 72 (void )theDriver;
c357e426 73 myPlatformWindow->Size (myWidth, myHeight);
74
75#if defined(__APPLE__)
76 myWidthPt = myWidth;
77 myHeightPt = myHeight;
78#endif
4fe56619 79
a2e4f780 80#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
81 EAGLContext* aGLContext = theGContext;
82 if (aGLContext == NULL)
4e1523ef 83 {
c357e426 84 void* aViewPtr = (void* )myPlatformWindow->NativeHandle();
a2e4f780 85
86 myUIView = (__bridge UIView* )aViewPtr;
87 CAEAGLLayer* anEaglLayer = (CAEAGLLayer* )myUIView.layer;
88 anEaglLayer.opaque = TRUE;
89 anEaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
90 [NSNumber numberWithBool: FALSE], kEAGLDrawablePropertyRetainedBacking,
91 kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
92 NULL];
93
94 aGLContext = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
95 if (aGLContext == NULL
96 || ![EAGLContext setCurrentContext: aGLContext])
97 {
98 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: EAGLContext creation failed");
9775fa61 99 throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString());
a2e4f780 100 return;
101 }
102
103 myGlContext->Init (aGLContext, Standard_False);
4e1523ef 104 }
105 else
abe46077 106 {
a2e4f780 107 if (![EAGLContext setCurrentContext: aGLContext])
108 {
109 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: EAGLContext can not be assigned");
9775fa61 110 throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString());
a2e4f780 111 return;
112 }
113
114 myGlContext->Init (aGLContext, Standard_False);
4e1523ef 115 }
a2e4f780 116#else
117
118 Cocoa_LocalPool aLocalPool;
119
120 // all GL context within one OpenGl_GraphicDriver should be shared!
121 NSOpenGLContext* aGLCtxShare = theShareCtx.IsNull() ? NULL : theShareCtx->myGContext;
122 NSOpenGLContext* aGLContext = theGContext;
123 bool isCore = false;
124 if (aGLContext == NULL)
4e1523ef 125 {
a2e4f780 126 NSOpenGLPixelFormatAttribute anAttribs[32] = {};
127 Standard_Integer aLastAttrib = 0;
128 //anAttribs[aLastAttrib++] = NSOpenGLPFAColorSize; anAttribs[aLastAttrib++] = 32,
129 anAttribs[aLastAttrib++] = NSOpenGLPFADepthSize; anAttribs[aLastAttrib++] = 24;
130 anAttribs[aLastAttrib++] = NSOpenGLPFAStencilSize; anAttribs[aLastAttrib++] = 8;
131 anAttribs[aLastAttrib++] = NSOpenGLPFADoubleBuffer;
132 if (theCaps->contextNoAccel)
4e1523ef 133 {
a2e4f780 134 anAttribs[aLastAttrib++] = NSOpenGLPFARendererID;
135 anAttribs[aLastAttrib++] = (NSOpenGLPixelFormatAttribute )kCGLRendererGenericFloatID;
4e1523ef 136 }
a2e4f780 137 else
138 {
139 anAttribs[aLastAttrib++] = NSOpenGLPFAAccelerated;
140 }
141 anAttribs[aLastAttrib] = 0;
142 const Standard_Integer aLastMainAttrib = aLastAttrib;
143 Standard_Integer aTryCore = 0;
144 Standard_Integer aTryStereo = 0;
145 for (aTryCore = 1; aTryCore >= 0; --aTryCore)
4e1523ef 146 {
a2e4f780 147 aLastAttrib = aLastMainAttrib;
148 if (aTryCore == 1)
4e1523ef 149 {
a2e4f780 150 if (theCaps->contextCompatible)
4e1523ef 151 {
152 continue;
153 }
a2e4f780 154
155 // supported since OS X 10.7+
156 anAttribs[aLastAttrib++] = 99; // NSOpenGLPFAOpenGLProfile
157 anAttribs[aLastAttrib++] = 0x3200; // NSOpenGLProfileVersion3_2Core
4e1523ef 158 }
159
a2e4f780 160 for (aTryStereo = 1; aTryStereo >= 0; --aTryStereo)
161 {
162 if (aTryStereo == 1)
163 {
164 if (!theCaps->contextStereo)
165 {
166 continue;
167 }
e2b4dea2 168
169 // deprecated since macOS 10.12 without replacement
170 Standard_DISABLE_DEPRECATION_WARNINGS
a2e4f780 171 anAttribs[aLastAttrib++] = NSOpenGLPFAStereo;
e2b4dea2 172 Standard_ENABLE_DEPRECATION_WARNINGS
a2e4f780 173 }
174
175 anAttribs[aLastAttrib] = 0;
176
177 NSOpenGLPixelFormat* aGLFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes: anAttribs] autorelease];
178 aGLContext = [[NSOpenGLContext alloc] initWithFormat: aGLFormat
179 shareContext: aGLCtxShare];
180 if (aGLContext != NULL)
181 {
182 break;
183 }
184 }
4e1523ef 185
4e1523ef 186 if (aGLContext != NULL)
187 {
188 break;
189 }
190 }
191
a2e4f780 192 if (aGLContext == NULL)
4e1523ef 193 {
a2e4f780 194 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: NSOpenGLContext creation failed");
9775fa61 195 throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString());
a2e4f780 196 return;
4e1523ef 197 }
4e1523ef 198
a2e4f780 199 if (aTryStereo == 0
200 && theCaps->contextStereo)
201 {
202 TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: QuadBuffer is unavailable!");
3b523c4c 203 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_OTHER, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
a2e4f780 204 }
205 if (aTryCore == 0
206 && !theCaps->contextCompatible)
207 {
208 TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: core profile creation failed.");
3b523c4c 209 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
a2e4f780 210 }
4fe56619 211
c357e426 212 NSView* aView = (NSView* )myPlatformWindow->NativeHandle();
a2e4f780 213 [aGLContext setView: aView];
214 isCore = (aTryCore == 1);
4e1523ef 215 }
216
a2e4f780 217 myGlContext->Init (aGLContext, isCore);
218#endif
4fe56619 219
4fe56619 220 myGlContext->Share (theShareCtx);
f978241f 221 myGlContext->SetSwapInterval (mySwapInterval);
4fe56619 222 Init();
223}
224
225// =======================================================================
226// function : ~OpenGl_Window
227// purpose :
228// =======================================================================
229OpenGl_Window::~OpenGl_Window()
230{
a2e4f780 231 if (!myOwnGContext
232 || myGlContext.IsNull())
233 {
234 myGlContext.Nullify();
235 return;
236 }
237
238#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
239 myGlContext.Nullify();
240 [EAGLContext setCurrentContext: NULL];
241 myUIView = NULL;
242#else
243 NSOpenGLContext* aGLCtx = myGlContext->myGContext;
4fe56619 244 myGlContext.Nullify();
245
246 [NSOpenGLContext clearCurrentContext];
a2e4f780 247 [aGLCtx clearDrawable];
248 [aGLCtx release];
249#endif
4fe56619 250}
251
252// =======================================================================
253// function : Resize
254// purpose : call_subr_resize
255// =======================================================================
c357e426 256void OpenGl_Window::Resize()
4fe56619 257{
4fe56619 258 // If the size is not changed - do nothing
c357e426 259 Standard_Integer aWidthPt = 0;
260 Standard_Integer aHeightPt = 0;
261 myPlatformWindow->Size (aWidthPt, aHeightPt);
262 if (myWidthPt == aWidthPt
263 && myHeightPt == aHeightPt)
4fe56619 264 {
5501f9a9 265 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
4fe56619 266 return;
5501f9a9 267 #else
268 // check backing store change (moving to another screen)
269 NSOpenGLContext* aGLCtx = myGlContext->myGContext;
270 NSView* aView = [aGLCtx view];
271 if (![aView respondsToSelector: @selector(convertSizeToBacking:)])
272 {
273 return;
274 }
275
276 NSRect aBounds = [aView bounds];
277 NSSize aRes = [aView convertSizeToBacking: aBounds.size];
278 if (myWidth == Standard_Integer(aRes.width)
279 && myHeight == Standard_Integer(aRes.height))
280 {
281 return;
282 }
283 #endif
4fe56619 284 }
285
c357e426 286 myWidthPt = aWidthPt;
287 myHeightPt = aHeightPt;
4fe56619 288
289 Init();
290}
291
292// =======================================================================
293// function : Init
294// purpose :
295// =======================================================================
296void OpenGl_Window::Init()
297{
298 if (!Activate())
299 {
300 return;
301 }
302
a2e4f780 303#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
304 Handle(OpenGl_FrameBuffer) aDefFbo = myGlContext->SetDefaultFrameBuffer (NULL);
305 if (!aDefFbo.IsNull())
306 {
307 aDefFbo->Release (myGlContext.operator->());
308 }
309 else
310 {
311 aDefFbo = new OpenGl_FrameBuffer();
312 }
4fe56619 313
a2e4f780 314 if (myOwnGContext)
315 {
316 EAGLContext* aGLCtx = myGlContext->myGContext;
317 CAEAGLLayer* anEaglLayer = (CAEAGLLayer* )myUIView.layer;
318 GLuint aWinRBColor = 0;
319 ::glGenRenderbuffers (1, &aWinRBColor);
320 ::glBindRenderbuffer (GL_RENDERBUFFER, aWinRBColor);
321 [aGLCtx renderbufferStorage: GL_RENDERBUFFER fromDrawable: anEaglLayer];
322 ::glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &myWidth);
323 ::glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &myHeight);
324 ::glBindRenderbuffer (GL_RENDERBUFFER, 0);
325
3c4b62a4 326 if (!aDefFbo->InitWithRB (myGlContext, myWidth, myHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, aWinRBColor))
a2e4f780 327 {
328 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO creation failed");
9775fa61 329 throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString());
a2e4f780 330 return;
331 }
332 }
333 else
334 {
335 if (!aDefFbo->InitWrapper (myGlContext))
336 {
337 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO wrapper creation failed");
9775fa61 338 throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString());
a2e4f780 339 return;
340 }
4fe56619 341
a2e4f780 342 myWidth = aDefFbo->GetVPSizeX();
343 myHeight = aDefFbo->GetVPSizeY();
344 }
345 myGlContext->SetDefaultFrameBuffer (aDefFbo);
346 aDefFbo->BindBuffer (myGlContext);
347 aDefFbo.Nullify();
348#else
349 NSOpenGLContext* aGLCtx = myGlContext->myGContext;
350 NSView* aView = [aGLCtx view];
351 NSRect aBounds = [aView bounds];
4fe56619 352
a2e4f780 353 // we should call this method each time when window is resized
354 [aGLCtx update];
4fe56619 355
a2e4f780 356 if ([aView respondsToSelector: @selector(convertSizeToBacking:)])
357 {
358 NSSize aRes = [aView convertSizeToBacking: aBounds.size];
359 myWidth = Standard_Integer(aRes.width);
360 myHeight = Standard_Integer(aRes.height);
361 }
362 else
363 {
364 myWidth = Standard_Integer(aBounds.size.width);
365 myHeight = Standard_Integer(aBounds.size.height);
366 }
367 myWidthPt = Standard_Integer(aBounds.size.width);
368 myHeightPt = Standard_Integer(aBounds.size.height);
369#endif
370
371 ::glDisable (GL_DITHER);
372 ::glDisable (GL_SCISSOR_TEST);
373 ::glViewport (0, 0, myWidth, myHeight);
374#if !defined(GL_ES_VERSION_2_0)
375 ::glDrawBuffer (GL_BACK);
376 if (myGlContext->core11 != NULL)
377 {
378 ::glMatrixMode (GL_MODELVIEW);
379 }
380#endif
4fe56619 381}
382
c357e426 383// =======================================================================
384// function : SetSwapInterval
385// purpose :
386// =======================================================================
387void OpenGl_Window::SetSwapInterval()
388{
389 if (mySwapInterval != myGlContext->caps->swapInterval)
390 {
391 mySwapInterval = myGlContext->caps->swapInterval;
392 myGlContext->SetSwapInterval (mySwapInterval);
393 }
394}
395
4fe56619 396#endif // __APPLE__