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 #define GL_GLEXT_LEGACY // To prevent inclusion of system glext.h on Mac OS X 10.6.8
20 #import <TargetConditionals.h>
22 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
23 #import <UIKit/UIKit.h>
25 #import <Cocoa/Cocoa.h>
27 #if !defined(MAC_OS_X_VERSION_10_7) || (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7)
28 @interface NSView (LionAPI)
29 - (NSSize )convertSizeToBacking: (NSSize )theSize;
35 #include <InterfaceGraphic.hxx>
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)
69 myPlatformWindow->Size (myWidth, myHeight);
71 #if defined(__APPLE__)
73 myHeightPt = myHeight;
76 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
77 EAGLContext* aGLContext = theGContext;
78 if (aGLContext == NULL)
80 void* aViewPtr = (void* )myPlatformWindow->NativeHandle();
82 myUIView = (__bridge UIView* )aViewPtr;
83 CAEAGLLayer* anEaglLayer = (CAEAGLLayer* )myUIView.layer;
84 anEaglLayer.opaque = TRUE;
85 anEaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
86 [NSNumber numberWithBool: FALSE], kEAGLDrawablePropertyRetainedBacking,
87 kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
90 aGLContext = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
91 if (aGLContext == NULL
92 || ![EAGLContext setCurrentContext: aGLContext])
94 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: EAGLContext creation failed");
95 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
99 myGlContext->Init (aGLContext, Standard_False);
103 if (![EAGLContext setCurrentContext: aGLContext])
105 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: EAGLContext can not be assigned");
106 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
110 myGlContext->Init (aGLContext, Standard_False);
114 Cocoa_LocalPool aLocalPool;
116 // all GL context within one OpenGl_GraphicDriver should be shared!
117 NSOpenGLContext* aGLCtxShare = theShareCtx.IsNull() ? NULL : theShareCtx->myGContext;
118 NSOpenGLContext* aGLContext = theGContext;
120 if (aGLContext == NULL)
122 NSOpenGLPixelFormatAttribute anAttribs[32] = {};
123 Standard_Integer aLastAttrib = 0;
124 //anAttribs[aLastAttrib++] = NSOpenGLPFAColorSize; anAttribs[aLastAttrib++] = 32,
125 anAttribs[aLastAttrib++] = NSOpenGLPFADepthSize; anAttribs[aLastAttrib++] = 24;
126 anAttribs[aLastAttrib++] = NSOpenGLPFAStencilSize; anAttribs[aLastAttrib++] = 8;
127 anAttribs[aLastAttrib++] = NSOpenGLPFADoubleBuffer;
128 if (theCaps->contextNoAccel)
130 anAttribs[aLastAttrib++] = NSOpenGLPFARendererID;
131 anAttribs[aLastAttrib++] = (NSOpenGLPixelFormatAttribute )kCGLRendererGenericFloatID;
135 anAttribs[aLastAttrib++] = NSOpenGLPFAAccelerated;
137 anAttribs[aLastAttrib] = 0;
138 const Standard_Integer aLastMainAttrib = aLastAttrib;
139 Standard_Integer aTryCore = 0;
140 Standard_Integer aTryStereo = 0;
141 for (aTryCore = 1; aTryCore >= 0; --aTryCore)
143 aLastAttrib = aLastMainAttrib;
146 if (theCaps->contextCompatible)
151 // supported since OS X 10.7+
152 anAttribs[aLastAttrib++] = 99; // NSOpenGLPFAOpenGLProfile
153 anAttribs[aLastAttrib++] = 0x3200; // NSOpenGLProfileVersion3_2Core
156 for (aTryStereo = 1; aTryStereo >= 0; --aTryStereo)
160 if (!theCaps->contextStereo)
164 anAttribs[aLastAttrib++] = NSOpenGLPFAStereo;
167 anAttribs[aLastAttrib] = 0;
169 NSOpenGLPixelFormat* aGLFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes: anAttribs] autorelease];
170 aGLContext = [[NSOpenGLContext alloc] initWithFormat: aGLFormat
171 shareContext: aGLCtxShare];
172 if (aGLContext != NULL)
178 if (aGLContext != NULL)
184 if (aGLContext == NULL)
186 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: NSOpenGLContext creation failed");
187 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
192 && theCaps->contextStereo)
194 TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: QuadBuffer is unavailable!");
195 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_OTHER, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
198 && !theCaps->contextCompatible)
200 TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: core profile creation failed.");
201 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
204 NSView* aView = (NSView* )myPlatformWindow->NativeHandle();
205 [aGLContext setView: aView];
206 isCore = (aTryCore == 1);
209 myGlContext->Init (aGLContext, isCore);
212 myGlContext->Share (theShareCtx);
213 myGlContext->SetSwapInterval (mySwapInterval);
217 // =======================================================================
218 // function : ~OpenGl_Window
220 // =======================================================================
221 OpenGl_Window::~OpenGl_Window()
224 || myGlContext.IsNull())
226 myGlContext.Nullify();
230 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
231 myGlContext.Nullify();
232 [EAGLContext setCurrentContext: NULL];
235 NSOpenGLContext* aGLCtx = myGlContext->myGContext;
236 myGlContext.Nullify();
238 [NSOpenGLContext clearCurrentContext];
239 [aGLCtx clearDrawable];
244 // =======================================================================
246 // purpose : call_subr_resize
247 // =======================================================================
248 void OpenGl_Window::Resize()
250 // If the size is not changed - do nothing
251 Standard_Integer aWidthPt = 0;
252 Standard_Integer aHeightPt = 0;
253 myPlatformWindow->Size (aWidthPt, aHeightPt);
254 if (myWidthPt == aWidthPt
255 && myHeightPt == aHeightPt)
260 myWidthPt = aWidthPt;
261 myHeightPt = aHeightPt;
266 // =======================================================================
269 // =======================================================================
270 void OpenGl_Window::Init()
277 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
278 Handle(OpenGl_FrameBuffer) aDefFbo = myGlContext->SetDefaultFrameBuffer (NULL);
279 if (!aDefFbo.IsNull())
281 aDefFbo->Release (myGlContext.operator->());
285 aDefFbo = new OpenGl_FrameBuffer();
290 EAGLContext* aGLCtx = myGlContext->myGContext;
291 CAEAGLLayer* anEaglLayer = (CAEAGLLayer* )myUIView.layer;
292 GLuint aWinRBColor = 0;
293 ::glGenRenderbuffers (1, &aWinRBColor);
294 ::glBindRenderbuffer (GL_RENDERBUFFER, aWinRBColor);
295 [aGLCtx renderbufferStorage: GL_RENDERBUFFER fromDrawable: anEaglLayer];
296 ::glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &myWidth);
297 ::glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &myHeight);
298 ::glBindRenderbuffer (GL_RENDERBUFFER, 0);
300 if (!aDefFbo->InitWithRB (myGlContext, myWidth, myHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, aWinRBColor))
302 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO creation failed");
303 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
309 if (!aDefFbo->InitWrapper (myGlContext))
311 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO wrapper creation failed");
312 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
316 myWidth = aDefFbo->GetVPSizeX();
317 myHeight = aDefFbo->GetVPSizeY();
319 myGlContext->SetDefaultFrameBuffer (aDefFbo);
320 aDefFbo->BindBuffer (myGlContext);
323 NSOpenGLContext* aGLCtx = myGlContext->myGContext;
324 NSView* aView = [aGLCtx view];
325 NSRect aBounds = [aView bounds];
327 // we should call this method each time when window is resized
330 if ([aView respondsToSelector: @selector(convertSizeToBacking:)])
332 NSSize aRes = [aView convertSizeToBacking: aBounds.size];
333 myWidth = Standard_Integer(aRes.width);
334 myHeight = Standard_Integer(aRes.height);
338 myWidth = Standard_Integer(aBounds.size.width);
339 myHeight = Standard_Integer(aBounds.size.height);
341 myWidthPt = Standard_Integer(aBounds.size.width);
342 myHeightPt = Standard_Integer(aBounds.size.height);
345 ::glDisable (GL_DITHER);
346 ::glDisable (GL_SCISSOR_TEST);
347 ::glViewport (0, 0, myWidth, myHeight);
348 #if !defined(GL_ES_VERSION_2_0)
349 ::glDrawBuffer (GL_BACK);
350 if (myGlContext->core11 != NULL)
352 ::glMatrixMode (GL_MODELVIEW);
357 // =======================================================================
358 // function : SetSwapInterval
360 // =======================================================================
361 void OpenGl_Window::SetSwapInterval()
363 if (mySwapInterval != myGlContext->caps->swapInterval)
365 mySwapInterval = myGlContext->caps->swapInterval;
366 myGlContext->SetSwapInterval (mySwapInterval);