0027266: Coding, TKOpenGl - drop unused files OpenGl_telem_util.hxx and OpenGl_tgl_fu...
[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   (void )theDriver;
70   myPlatformWindow->Size (myWidth, myHeight);
71
72 #if defined(__APPLE__)
73   myWidthPt  = myWidth;
74   myHeightPt = myHeight;
75 #endif
76
77 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
78   EAGLContext* aGLContext = theGContext;
79   if (aGLContext == NULL)
80   {
81     void* aViewPtr = (void* )myPlatformWindow->NativeHandle();
82
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,
89                                         NULL];
90
91     aGLContext = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
92     if (aGLContext == NULL
93     || ![EAGLContext setCurrentContext: aGLContext])
94     {
95       TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: EAGLContext creation failed");
96       Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
97       return;
98     }
99
100     myGlContext->Init (aGLContext, Standard_False);
101   }
102   else
103   {
104     if (![EAGLContext setCurrentContext: aGLContext])
105     {
106       TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: EAGLContext can not be assigned");
107       Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
108       return;
109     }
110
111     myGlContext->Init (aGLContext, Standard_False);
112   }
113 #else
114
115   Cocoa_LocalPool aLocalPool;
116
117   // all GL context within one OpenGl_GraphicDriver should be shared!
118   NSOpenGLContext* aGLCtxShare = theShareCtx.IsNull() ? NULL : theShareCtx->myGContext;
119   NSOpenGLContext* aGLContext  = theGContext;
120   bool isCore = false;
121   if (aGLContext == NULL)
122   {
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)
130     {
131       anAttribs[aLastAttrib++] = NSOpenGLPFARendererID;
132       anAttribs[aLastAttrib++] = (NSOpenGLPixelFormatAttribute )kCGLRendererGenericFloatID;
133     }
134     else
135     {
136       anAttribs[aLastAttrib++] = NSOpenGLPFAAccelerated;
137     }
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)
143     {
144       aLastAttrib = aLastMainAttrib;
145       if (aTryCore == 1)
146       {
147         if (theCaps->contextCompatible)
148         {
149           continue;
150         }
151
152         // supported since OS X 10.7+
153         anAttribs[aLastAttrib++] = 99;     // NSOpenGLPFAOpenGLProfile
154         anAttribs[aLastAttrib++] = 0x3200; // NSOpenGLProfileVersion3_2Core
155       }
156
157       for (aTryStereo = 1; aTryStereo >= 0; --aTryStereo)
158       {
159         if (aTryStereo == 1)
160         {
161           if (!theCaps->contextStereo)
162           {
163             continue;
164           }
165           anAttribs[aLastAttrib++] = NSOpenGLPFAStereo;
166         }
167
168         anAttribs[aLastAttrib] = 0;
169
170         NSOpenGLPixelFormat* aGLFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes: anAttribs] autorelease];
171         aGLContext = [[NSOpenGLContext alloc] initWithFormat: aGLFormat
172                                                 shareContext: aGLCtxShare];
173         if (aGLContext != NULL)
174         {
175           break;
176         }
177       }
178
179       if (aGLContext != NULL)
180       {
181         break;
182       }
183     }
184
185     if (aGLContext == NULL)
186     {
187       TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: NSOpenGLContext creation failed");
188       Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
189       return;
190     }
191
192     if (aTryStereo == 0
193      && theCaps->contextStereo)
194     {
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);
197     }
198     if (aTryCore == 0
199     && !theCaps->contextCompatible)
200     {
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);
203     }
204
205     NSView* aView = (NSView* )myPlatformWindow->NativeHandle();
206     [aGLContext setView: aView];
207     isCore = (aTryCore == 1);
208   }
209
210   myGlContext->Init (aGLContext, isCore);
211 #endif
212
213   myGlContext->Share (theShareCtx);
214   myGlContext->SetSwapInterval (mySwapInterval);
215   Init();
216 }
217
218 // =======================================================================
219 // function : ~OpenGl_Window
220 // purpose  :
221 // =======================================================================
222 OpenGl_Window::~OpenGl_Window()
223 {
224   if (!myOwnGContext
225    ||  myGlContext.IsNull())
226   {
227     myGlContext.Nullify();
228     return;
229   }
230
231 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
232   myGlContext.Nullify();
233   [EAGLContext setCurrentContext: NULL];
234   myUIView = NULL;
235 #else
236   NSOpenGLContext* aGLCtx = myGlContext->myGContext;
237   myGlContext.Nullify();
238
239   [NSOpenGLContext clearCurrentContext];
240   [aGLCtx clearDrawable];
241   [aGLCtx release];
242 #endif
243 }
244
245 // =======================================================================
246 // function : Resize
247 // purpose  : call_subr_resize
248 // =======================================================================
249 void OpenGl_Window::Resize()
250 {
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)
257   {
258     return;
259   }
260
261   myWidthPt  = aWidthPt;
262   myHeightPt = aHeightPt;
263
264   Init();
265 }
266
267 // =======================================================================
268 // function : Init
269 // purpose  :
270 // =======================================================================
271 void OpenGl_Window::Init()
272 {
273   if (!Activate())
274   {
275     return;
276   }
277
278 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
279   Handle(OpenGl_FrameBuffer) aDefFbo = myGlContext->SetDefaultFrameBuffer (NULL);
280   if (!aDefFbo.IsNull())
281   {
282     aDefFbo->Release (myGlContext.operator->());
283   }
284   else
285   {
286     aDefFbo = new OpenGl_FrameBuffer();
287   }
288
289   if (myOwnGContext)
290   {
291     EAGLContext* aGLCtx      = myGlContext->myGContext;
292     CAEAGLLayer* anEaglLayer = (CAEAGLLayer* )myUIView.layer;
293     GLuint aWinRBColor = 0;
294     ::glGenRenderbuffers (1, &aWinRBColor);
295     ::glBindRenderbuffer (GL_RENDERBUFFER, aWinRBColor);
296     [aGLCtx renderbufferStorage: GL_RENDERBUFFER fromDrawable: anEaglLayer];
297     ::glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH,  &myWidth);
298     ::glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &myHeight);
299     ::glBindRenderbuffer (GL_RENDERBUFFER, 0);
300
301     if (!aDefFbo->InitWithRB (myGlContext, myWidth, myHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, aWinRBColor))
302     {
303       TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO creation failed");
304       Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
305       return;
306     }
307   }
308   else
309   {
310     if (!aDefFbo->InitWrapper (myGlContext))
311     {
312       TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO wrapper creation failed");
313       Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
314       return;
315     }
316
317     myWidth  = aDefFbo->GetVPSizeX();
318     myHeight = aDefFbo->GetVPSizeY();
319   }
320   myGlContext->SetDefaultFrameBuffer (aDefFbo);
321   aDefFbo->BindBuffer (myGlContext);
322   aDefFbo.Nullify();
323 #else
324   NSOpenGLContext* aGLCtx  = myGlContext->myGContext;
325   NSView*          aView   = [aGLCtx view];
326   NSRect           aBounds = [aView bounds];
327
328   // we should call this method each time when window is resized
329   [aGLCtx update];
330
331   if ([aView respondsToSelector: @selector(convertSizeToBacking:)])
332   {
333     NSSize aRes = [aView convertSizeToBacking: aBounds.size];
334     myWidth  = Standard_Integer(aRes.width);
335     myHeight = Standard_Integer(aRes.height);
336   }
337   else
338   {
339     myWidth  = Standard_Integer(aBounds.size.width);
340     myHeight = Standard_Integer(aBounds.size.height);
341   }
342   myWidthPt  = Standard_Integer(aBounds.size.width);
343   myHeightPt = Standard_Integer(aBounds.size.height);
344 #endif
345
346   ::glDisable (GL_DITHER);
347   ::glDisable (GL_SCISSOR_TEST);
348   ::glViewport (0, 0, myWidth, myHeight);
349 #if !defined(GL_ES_VERSION_2_0)
350   ::glDrawBuffer (GL_BACK);
351   if (myGlContext->core11 != NULL)
352   {
353     ::glMatrixMode (GL_MODELVIEW);
354   }
355 #endif
356 }
357
358 // =======================================================================
359 // function : SetSwapInterval
360 // purpose  :
361 // =======================================================================
362 void OpenGl_Window::SetSwapInterval()
363 {
364   if (mySwapInterval != myGlContext->caps->swapInterval)
365   {
366     mySwapInterval = myGlContext->caps->swapInterval;
367     myGlContext->SetSwapInterval (mySwapInterval);
368   }
369 }
370
371 #endif // __APPLE__