0026348: Visualization, TKOpenGl - eliminate invalid NULL checks for transformation...
[occt.git] / src / OpenGl / OpenGl_Window_1.mm
1 // Created on: 2012-11-12
2 // Created by: Kirill Gavrilov
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
17
18 #define GL_GLEXT_LEGACY // To prevent inclusion of system glext.h on Mac OS X 10.6.8
19
20 #import <TargetConditionals.h>
21
22 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
23   #import <UIKit/UIKit.h>
24 #else
25   #import <Cocoa/Cocoa.h>
26
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;
30 @end
31 #endif
32
33 #endif
34
35 #include <InterfaceGraphic.hxx>
36
37 #include <OpenGl_Window.hxx>
38 #include <OpenGl_FrameBuffer.hxx>
39
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>
45
46 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
47   //
48 #else
49   #include <OpenGL/CGLRenderers.h>
50 #endif
51
52 // =======================================================================
53 // function : OpenGl_Window
54 // purpose  :
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
65   myUIView (NULL),
66 #endif
67   mySwapInterval (theCaps->swapInterval)
68 {
69   myPlatformWindow->Size (myWidth, myHeight);
70
71 #if defined(__APPLE__)
72   myWidthPt  = myWidth;
73   myHeightPt = myHeight;
74 #endif
75
76 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
77   EAGLContext* aGLContext = theGContext;
78   if (aGLContext == NULL)
79   {
80     void* aViewPtr = (void* )myPlatformWindow->NativeHandle();
81
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,
88                                         NULL];
89
90     aGLContext = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
91     if (aGLContext == NULL
92     || ![EAGLContext setCurrentContext: aGLContext])
93     {
94       TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: EAGLContext creation failed");
95       Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
96       return;
97     }
98
99     myGlContext->Init (aGLContext, Standard_False);
100   }
101   else
102   {
103     if (![EAGLContext setCurrentContext: aGLContext])
104     {
105       TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: EAGLContext can not be assigned");
106       Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
107       return;
108     }
109
110     myGlContext->Init (aGLContext, Standard_False);
111   }
112 #else
113
114   Cocoa_LocalPool aLocalPool;
115
116   // all GL context within one OpenGl_GraphicDriver should be shared!
117   NSOpenGLContext* aGLCtxShare = theShareCtx.IsNull() ? NULL : theShareCtx->myGContext;
118   NSOpenGLContext* aGLContext  = theGContext;
119   bool isCore = false;
120   if (aGLContext == NULL)
121   {
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)
129     {
130       anAttribs[aLastAttrib++] = NSOpenGLPFARendererID;
131       anAttribs[aLastAttrib++] = (NSOpenGLPixelFormatAttribute )kCGLRendererGenericFloatID;
132     }
133     else
134     {
135       anAttribs[aLastAttrib++] = NSOpenGLPFAAccelerated;
136     }
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)
142     {
143       aLastAttrib = aLastMainAttrib;
144       if (aTryCore == 1)
145       {
146         if (theCaps->contextCompatible)
147         {
148           continue;
149         }
150
151         // supported since OS X 10.7+
152         anAttribs[aLastAttrib++] = 99;     // NSOpenGLPFAOpenGLProfile
153         anAttribs[aLastAttrib++] = 0x3200; // NSOpenGLProfileVersion3_2Core
154       }
155
156       for (aTryStereo = 1; aTryStereo >= 0; --aTryStereo)
157       {
158         if (aTryStereo == 1)
159         {
160           if (!theCaps->contextStereo)
161           {
162             continue;
163           }
164           anAttribs[aLastAttrib++] = NSOpenGLPFAStereo;
165         }
166
167         anAttribs[aLastAttrib] = 0;
168
169         NSOpenGLPixelFormat* aGLFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes: anAttribs] autorelease];
170         aGLContext = [[NSOpenGLContext alloc] initWithFormat: aGLFormat
171                                                 shareContext: aGLCtxShare];
172         if (aGLContext != NULL)
173         {
174           break;
175         }
176       }
177
178       if (aGLContext != NULL)
179       {
180         break;
181       }
182     }
183
184     if (aGLContext == NULL)
185     {
186       TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: NSOpenGLContext creation failed");
187       Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
188       return;
189     }
190
191     if (aTryStereo == 0
192      && theCaps->contextStereo)
193     {
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);
196     }
197     if (aTryCore == 0
198     && !theCaps->contextCompatible)
199     {
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);
202     }
203
204     NSView* aView = (NSView* )myPlatformWindow->NativeHandle();
205     [aGLContext setView: aView];
206     isCore = (aTryCore == 1);
207   }
208
209   myGlContext->Init (aGLContext, isCore);
210 #endif
211
212   myGlContext->Share (theShareCtx);
213   myGlContext->SetSwapInterval (mySwapInterval);
214   Init();
215 }
216
217 // =======================================================================
218 // function : ~OpenGl_Window
219 // purpose  :
220 // =======================================================================
221 OpenGl_Window::~OpenGl_Window()
222 {
223   if (!myOwnGContext
224    ||  myGlContext.IsNull())
225   {
226     myGlContext.Nullify();
227     return;
228   }
229
230 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
231   myGlContext.Nullify();
232   [EAGLContext setCurrentContext: NULL];
233   myUIView = NULL;
234 #else
235   NSOpenGLContext* aGLCtx = myGlContext->myGContext;
236   myGlContext.Nullify();
237
238   [NSOpenGLContext clearCurrentContext];
239   [aGLCtx clearDrawable];
240   [aGLCtx release];
241 #endif
242 }
243
244 // =======================================================================
245 // function : Resize
246 // purpose  : call_subr_resize
247 // =======================================================================
248 void OpenGl_Window::Resize()
249 {
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)
256   {
257     return;
258   }
259
260   myWidthPt  = aWidthPt;
261   myHeightPt = aHeightPt;
262
263   Init();
264 }
265
266 // =======================================================================
267 // function : Init
268 // purpose  :
269 // =======================================================================
270 void OpenGl_Window::Init()
271 {
272   if (!Activate())
273   {
274     return;
275   }
276
277 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
278   Handle(OpenGl_FrameBuffer) aDefFbo = myGlContext->SetDefaultFrameBuffer (NULL);
279   if (!aDefFbo.IsNull())
280   {
281     aDefFbo->Release (myGlContext.operator->());
282   }
283   else
284   {
285     aDefFbo = new OpenGl_FrameBuffer();
286   }
287
288   if (myOwnGContext)
289   {
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);
299
300     if (!aDefFbo->InitWithRB (myGlContext, myWidth, myHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, aWinRBColor))
301     {
302       TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO creation failed");
303       Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
304       return;
305     }
306   }
307   else
308   {
309     if (!aDefFbo->InitWrapper (myGlContext))
310     {
311       TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO wrapper creation failed");
312       Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
313       return;
314     }
315
316     myWidth  = aDefFbo->GetVPSizeX();
317     myHeight = aDefFbo->GetVPSizeY();
318   }
319   myGlContext->SetDefaultFrameBuffer (aDefFbo);
320   aDefFbo->BindBuffer (myGlContext);
321   aDefFbo.Nullify();
322 #else
323   NSOpenGLContext* aGLCtx  = myGlContext->myGContext;
324   NSView*          aView   = [aGLCtx view];
325   NSRect           aBounds = [aView bounds];
326
327   // we should call this method each time when window is resized
328   [aGLCtx update];
329
330   if ([aView respondsToSelector: @selector(convertSizeToBacking:)])
331   {
332     NSSize aRes = [aView convertSizeToBacking: aBounds.size];
333     myWidth  = Standard_Integer(aRes.width);
334     myHeight = Standard_Integer(aRes.height);
335   }
336   else
337   {
338     myWidth  = Standard_Integer(aBounds.size.width);
339     myHeight = Standard_Integer(aBounds.size.height);
340   }
341   myWidthPt  = Standard_Integer(aBounds.size.width);
342   myHeightPt = Standard_Integer(aBounds.size.height);
343 #endif
344
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)
351   {
352     ::glMatrixMode (GL_MODELVIEW);
353   }
354 #endif
355 }
356
357 // =======================================================================
358 // function : SetSwapInterval
359 // purpose  :
360 // =======================================================================
361 void OpenGl_Window::SetSwapInterval()
362 {
363   if (mySwapInterval != myGlContext->caps->swapInterval)
364   {
365     mySwapInterval = myGlContext->caps->swapInterval;
366     myGlContext->SetSwapInterval (mySwapInterval);
367   }
368 }
369
370 #endif // __APPLE__