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)
165 anAttribs[aLastAttrib++] = NSOpenGLPFAStereo;
168 anAttribs[aLastAttrib] = 0;
170 NSOpenGLPixelFormat* aGLFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes: anAttribs] autorelease];
171 aGLContext = [[NSOpenGLContext alloc] initWithFormat: aGLFormat
172 shareContext: aGLCtxShare];
173 if (aGLContext != NULL)
179 if (aGLContext != NULL)
185 if (aGLContext == NULL)
187 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: NSOpenGLContext creation failed");
188 throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString());
193 && theCaps->contextStereo)
195 TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: QuadBuffer is unavailable!");
196 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_OTHER, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
199 && !theCaps->contextCompatible)
201 TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: core profile creation failed.");
202 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
205 NSView* aView = (NSView* )myPlatformWindow->NativeHandle();
206 [aGLContext setView: aView];
207 isCore = (aTryCore == 1);
210 myGlContext->Init (aGLContext, isCore);
213 myGlContext->Share (theShareCtx);
214 myGlContext->SetSwapInterval (mySwapInterval);
218 // =======================================================================
219 // function : ~OpenGl_Window
221 // =======================================================================
222 OpenGl_Window::~OpenGl_Window()
225 || myGlContext.IsNull())
227 myGlContext.Nullify();
231 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
232 myGlContext.Nullify();
233 [EAGLContext setCurrentContext: NULL];
236 NSOpenGLContext* aGLCtx = myGlContext->myGContext;
237 myGlContext.Nullify();
239 [NSOpenGLContext clearCurrentContext];
240 [aGLCtx clearDrawable];
245 // =======================================================================
247 // purpose : call_subr_resize
248 // =======================================================================
249 void OpenGl_Window::Resize()
251 // If the size is not changed - do nothing
252 Standard_Integer aWidthPt = 0;
253 Standard_Integer aHeightPt = 0;
254 myPlatformWindow->Size (aWidthPt, aHeightPt);
255 if (myWidthPt == aWidthPt
256 && myHeightPt == aHeightPt)
258 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
261 // check backing store change (moving to another screen)
262 NSOpenGLContext* aGLCtx = myGlContext->myGContext;
263 NSView* aView = [aGLCtx view];
264 if (![aView respondsToSelector: @selector(convertSizeToBacking:)])
269 NSRect aBounds = [aView bounds];
270 NSSize aRes = [aView convertSizeToBacking: aBounds.size];
271 if (myWidth == Standard_Integer(aRes.width)
272 && myHeight == Standard_Integer(aRes.height))
279 myWidthPt = aWidthPt;
280 myHeightPt = aHeightPt;
285 // =======================================================================
288 // =======================================================================
289 void OpenGl_Window::Init()
296 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
297 Handle(OpenGl_FrameBuffer) aDefFbo = myGlContext->SetDefaultFrameBuffer (NULL);
298 if (!aDefFbo.IsNull())
300 aDefFbo->Release (myGlContext.operator->());
304 aDefFbo = new OpenGl_FrameBuffer();
309 EAGLContext* aGLCtx = myGlContext->myGContext;
310 CAEAGLLayer* anEaglLayer = (CAEAGLLayer* )myUIView.layer;
311 GLuint aWinRBColor = 0;
312 ::glGenRenderbuffers (1, &aWinRBColor);
313 ::glBindRenderbuffer (GL_RENDERBUFFER, aWinRBColor);
314 [aGLCtx renderbufferStorage: GL_RENDERBUFFER fromDrawable: anEaglLayer];
315 ::glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &myWidth);
316 ::glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &myHeight);
317 ::glBindRenderbuffer (GL_RENDERBUFFER, 0);
319 if (!aDefFbo->InitWithRB (myGlContext, myWidth, myHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, aWinRBColor))
321 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO creation failed");
322 throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString());
328 if (!aDefFbo->InitWrapper (myGlContext))
330 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO wrapper creation failed");
331 throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString());
335 myWidth = aDefFbo->GetVPSizeX();
336 myHeight = aDefFbo->GetVPSizeY();
338 myGlContext->SetDefaultFrameBuffer (aDefFbo);
339 aDefFbo->BindBuffer (myGlContext);
342 NSOpenGLContext* aGLCtx = myGlContext->myGContext;
343 NSView* aView = [aGLCtx view];
344 NSRect aBounds = [aView bounds];
346 // we should call this method each time when window is resized
349 if ([aView respondsToSelector: @selector(convertSizeToBacking:)])
351 NSSize aRes = [aView convertSizeToBacking: aBounds.size];
352 myWidth = Standard_Integer(aRes.width);
353 myHeight = Standard_Integer(aRes.height);
357 myWidth = Standard_Integer(aBounds.size.width);
358 myHeight = Standard_Integer(aBounds.size.height);
360 myWidthPt = Standard_Integer(aBounds.size.width);
361 myHeightPt = Standard_Integer(aBounds.size.height);
364 ::glDisable (GL_DITHER);
365 ::glDisable (GL_SCISSOR_TEST);
366 ::glViewport (0, 0, myWidth, myHeight);
367 #if !defined(GL_ES_VERSION_2_0)
368 ::glDrawBuffer (GL_BACK);
369 if (myGlContext->core11 != NULL)
371 ::glMatrixMode (GL_MODELVIEW);
376 // =======================================================================
377 // function : SetSwapInterval
379 // =======================================================================
380 void OpenGl_Window::SetSwapInterval()
382 if (mySwapInterval != myGlContext->caps->swapInterval)
384 mySwapInterval = myGlContext->caps->swapInterval;
385 myGlContext->SetSwapInterval (mySwapInterval);