1 // Created on: 2012-11-12
2 // Created by: Kirill Gavrilov
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
18 #ifndef GL_GLEXT_LEGACY
19 #define GL_GLEXT_LEGACY // To prevent inclusion of system glext.h on Mac OS X 10.6.8
22 #import <TargetConditionals.h>
24 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
25 #import <UIKit/UIKit.h>
27 #import <Cocoa/Cocoa.h>
29 #if !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7)
30 @interface NSView (LionAPI)
31 - (NSSize )convertSizeToBacking: (NSSize )theSize;
37 #include <OpenGl_Window.hxx>
38 #include <OpenGl_FrameBuffer.hxx>
40 #include <OpenGl_Context.hxx>
41 #include <Aspect_GraphicDeviceDefinitionError.hxx>
42 #include <Cocoa_LocalPool.hxx>
43 #include <TCollection_AsciiString.hxx>
44 #include <TCollection_ExtendedString.hxx>
46 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
49 #include <OpenGL/CGLRenderers.h>
52 // =======================================================================
53 // function : OpenGl_Window
55 // =======================================================================
56 OpenGl_Window::OpenGl_Window (const Handle(OpenGl_GraphicDriver)& theDriver,
57 const Handle(Aspect_Window)& thePlatformWindow,
58 Aspect_RenderingContext theGContext,
59 const Handle(OpenGl_Caps)& theCaps,
60 const Handle(OpenGl_Context)& theShareCtx)
61 : myGlContext (new OpenGl_Context (theCaps)),
62 myOwnGContext (theGContext == 0),
63 myPlatformWindow (thePlatformWindow),
64 #if defined(__APPLE__) && defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
67 mySwapInterval (theCaps->swapInterval)
70 myPlatformWindow->Size (myWidth, myHeight);
72 #if defined(__APPLE__)
74 myHeightPt = myHeight;
77 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
78 EAGLContext* aGLContext = theGContext;
79 if (aGLContext == NULL)
81 void* aViewPtr = (void* )myPlatformWindow->NativeHandle();
83 myUIView = (__bridge UIView* )aViewPtr;
84 CAEAGLLayer* anEaglLayer = (CAEAGLLayer* )myUIView.layer;
85 anEaglLayer.opaque = TRUE;
86 anEaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
87 [NSNumber numberWithBool: FALSE], kEAGLDrawablePropertyRetainedBacking,
88 kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
91 aGLContext = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
92 if (aGLContext == NULL
93 || ![EAGLContext setCurrentContext: aGLContext])
95 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: EAGLContext creation failed");
96 throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString());
100 myGlContext->Init (aGLContext, Standard_False);
104 if (![EAGLContext setCurrentContext: aGLContext])
106 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: EAGLContext can not be assigned");
107 throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString());
111 myGlContext->Init (aGLContext, Standard_False);
115 Cocoa_LocalPool aLocalPool;
117 // all GL context within one OpenGl_GraphicDriver should be shared!
118 NSOpenGLContext* aGLCtxShare = theShareCtx.IsNull() ? NULL : theShareCtx->myGContext;
119 NSOpenGLContext* aGLContext = theGContext;
121 if (aGLContext == NULL)
123 NSOpenGLPixelFormatAttribute anAttribs[32] = {};
124 Standard_Integer aLastAttrib = 0;
125 //anAttribs[aLastAttrib++] = NSOpenGLPFAColorSize; anAttribs[aLastAttrib++] = 32,
126 anAttribs[aLastAttrib++] = NSOpenGLPFADepthSize; anAttribs[aLastAttrib++] = 24;
127 anAttribs[aLastAttrib++] = NSOpenGLPFAStencilSize; anAttribs[aLastAttrib++] = 8;
128 anAttribs[aLastAttrib++] = NSOpenGLPFADoubleBuffer;
129 if (theCaps->contextNoAccel)
131 anAttribs[aLastAttrib++] = NSOpenGLPFARendererID;
132 anAttribs[aLastAttrib++] = (NSOpenGLPixelFormatAttribute )kCGLRendererGenericFloatID;
136 anAttribs[aLastAttrib++] = NSOpenGLPFAAccelerated;
138 anAttribs[aLastAttrib] = 0;
139 const Standard_Integer aLastMainAttrib = aLastAttrib;
140 Standard_Integer aTryCore = 0;
141 Standard_Integer aTryStereo = 0;
142 for (aTryCore = 1; aTryCore >= 0; --aTryCore)
144 aLastAttrib = aLastMainAttrib;
147 if (theCaps->contextCompatible)
152 // supported since OS X 10.7+
153 anAttribs[aLastAttrib++] = 99; // NSOpenGLPFAOpenGLProfile
154 anAttribs[aLastAttrib++] = 0x3200; // NSOpenGLProfileVersion3_2Core
157 for (aTryStereo = 1; aTryStereo >= 0; --aTryStereo)
161 if (!theCaps->contextStereo)
166 // deprecated since macOS 10.12 without replacement
167 Standard_DISABLE_DEPRECATION_WARNINGS
168 anAttribs[aLastAttrib++] = NSOpenGLPFAStereo;
169 Standard_ENABLE_DEPRECATION_WARNINGS
172 anAttribs[aLastAttrib] = 0;
174 NSOpenGLPixelFormat* aGLFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes: anAttribs] autorelease];
175 aGLContext = [[NSOpenGLContext alloc] initWithFormat: aGLFormat
176 shareContext: aGLCtxShare];
177 if (aGLContext != NULL)
183 if (aGLContext != NULL)
189 if (aGLContext == NULL)
191 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: NSOpenGLContext creation failed");
192 throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString());
197 && theCaps->contextStereo)
199 TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: QuadBuffer is unavailable!");
200 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_OTHER, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
203 && !theCaps->contextCompatible)
205 TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: core profile creation failed.");
206 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
209 NSView* aView = (NSView* )myPlatformWindow->NativeHandle();
210 [aGLContext setView: aView];
211 isCore = (aTryCore == 1);
214 myGlContext->Init (aGLContext, isCore);
217 myGlContext->Share (theShareCtx);
218 myGlContext->SetSwapInterval (mySwapInterval);
222 // =======================================================================
223 // function : ~OpenGl_Window
225 // =======================================================================
226 OpenGl_Window::~OpenGl_Window()
229 || myGlContext.IsNull())
231 myGlContext.Nullify();
235 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
236 myGlContext.Nullify();
237 [EAGLContext setCurrentContext: NULL];
240 NSOpenGLContext* aGLCtx = myGlContext->myGContext;
241 myGlContext.Nullify();
243 [NSOpenGLContext clearCurrentContext];
244 [aGLCtx clearDrawable];
249 // =======================================================================
251 // purpose : call_subr_resize
252 // =======================================================================
253 void OpenGl_Window::Resize()
255 // If the size is not changed - do nothing
256 Standard_Integer aWidthPt = 0;
257 Standard_Integer aHeightPt = 0;
258 myPlatformWindow->Size (aWidthPt, aHeightPt);
259 if (myWidthPt == aWidthPt
260 && myHeightPt == aHeightPt)
262 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
265 // check backing store change (moving to another screen)
266 NSOpenGLContext* aGLCtx = myGlContext->myGContext;
267 NSView* aView = [aGLCtx view];
268 if (![aView respondsToSelector: @selector(convertSizeToBacking:)])
273 NSRect aBounds = [aView bounds];
274 NSSize aRes = [aView convertSizeToBacking: aBounds.size];
275 if (myWidth == Standard_Integer(aRes.width)
276 && myHeight == Standard_Integer(aRes.height))
283 myWidthPt = aWidthPt;
284 myHeightPt = aHeightPt;
289 // =======================================================================
292 // =======================================================================
293 void OpenGl_Window::Init()
300 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
301 Handle(OpenGl_FrameBuffer) aDefFbo = myGlContext->SetDefaultFrameBuffer (NULL);
302 if (!aDefFbo.IsNull())
304 aDefFbo->Release (myGlContext.operator->());
308 aDefFbo = new OpenGl_FrameBuffer();
313 EAGLContext* aGLCtx = myGlContext->myGContext;
314 CAEAGLLayer* anEaglLayer = (CAEAGLLayer* )myUIView.layer;
315 GLuint aWinRBColor = 0;
316 ::glGenRenderbuffers (1, &aWinRBColor);
317 ::glBindRenderbuffer (GL_RENDERBUFFER, aWinRBColor);
318 [aGLCtx renderbufferStorage: GL_RENDERBUFFER fromDrawable: anEaglLayer];
319 ::glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &myWidth);
320 ::glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &myHeight);
321 ::glBindRenderbuffer (GL_RENDERBUFFER, 0);
323 if (!aDefFbo->InitWithRB (myGlContext, myWidth, myHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, aWinRBColor))
325 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO creation failed");
326 throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString());
332 if (!aDefFbo->InitWrapper (myGlContext))
334 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO wrapper creation failed");
335 throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString());
339 myWidth = aDefFbo->GetVPSizeX();
340 myHeight = aDefFbo->GetVPSizeY();
342 myGlContext->SetDefaultFrameBuffer (aDefFbo);
343 aDefFbo->BindBuffer (myGlContext);
346 NSOpenGLContext* aGLCtx = myGlContext->myGContext;
347 NSView* aView = [aGLCtx view];
348 NSRect aBounds = [aView bounds];
350 // we should call this method each time when window is resized
353 if ([aView respondsToSelector: @selector(convertSizeToBacking:)])
355 NSSize aRes = [aView convertSizeToBacking: aBounds.size];
356 myWidth = Standard_Integer(aRes.width);
357 myHeight = Standard_Integer(aRes.height);
361 myWidth = Standard_Integer(aBounds.size.width);
362 myHeight = Standard_Integer(aBounds.size.height);
364 myWidthPt = Standard_Integer(aBounds.size.width);
365 myHeightPt = Standard_Integer(aBounds.size.height);
368 ::glDisable (GL_DITHER);
369 ::glDisable (GL_SCISSOR_TEST);
370 ::glViewport (0, 0, myWidth, myHeight);
371 #if !defined(GL_ES_VERSION_2_0)
372 ::glDrawBuffer (GL_BACK);
373 if (myGlContext->core11 != NULL)
375 ::glMatrixMode (GL_MODELVIEW);
380 // =======================================================================
381 // function : SetSwapInterval
383 // =======================================================================
384 void OpenGl_Window::SetSwapInterval()
386 if (mySwapInterval != myGlContext->caps->swapInterval)
388 mySwapInterval = myGlContext->caps->swapInterval;
389 myGlContext->SetSwapInterval (mySwapInterval);