6aca4d39 |
1 | // Created on: 2012-11-12 |
4fe56619 |
2 | // Created by: Kirill Gavrilov |
6aca4d39 |
3 | // Copyright (c) 2012-2014 OPEN CASCADE SAS |
4fe56619 |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
4fe56619 |
6 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
4fe56619 |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
4fe56619 |
15 | |
16 | #if defined(__APPLE__) && !defined(MACOSX_USE_GLX) |
17 | |
896faa72 |
18 | #ifndef GL_GLEXT_LEGACY |
0e9d3b83 |
19 | #define GL_GLEXT_LEGACY // To prevent inclusion of system glext.h on Mac OS X 10.6.8 |
896faa72 |
20 | #endif |
0e9d3b83 |
21 | |
a2e4f780 |
22 | #import <TargetConditionals.h> |
23 | |
24 | #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE |
25 | #import <UIKit/UIKit.h> |
26 | #else |
27 | #import <Cocoa/Cocoa.h> |
28 | |
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; |
32 | @end |
33 | #endif |
34 | |
35 | #endif |
4fe56619 |
36 | |
4fe56619 |
37 | #include <OpenGl_Window.hxx> |
a2e4f780 |
38 | #include <OpenGl_FrameBuffer.hxx> |
4fe56619 |
39 | |
40 | #include <OpenGl_Context.hxx> |
4fe56619 |
41 | #include <Aspect_GraphicDeviceDefinitionError.hxx> |
42 | #include <Cocoa_LocalPool.hxx> |
43 | #include <TCollection_AsciiString.hxx> |
4e1523ef |
44 | #include <TCollection_ExtendedString.hxx> |
4fe56619 |
45 | |
a2e4f780 |
46 | #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE |
47 | // |
48 | #else |
49 | #include <OpenGL/CGLRenderers.h> |
50 | #endif |
abe46077 |
51 | |
4fe56619 |
52 | // ======================================================================= |
53 | // function : OpenGl_Window |
54 | // purpose : |
55 | // ======================================================================= |
c7ccbb77 |
56 | OpenGl_Window::OpenGl_Window (const Handle(OpenGl_GraphicDriver)& theDriver, |
c357e426 |
57 | const Handle(Aspect_Window)& thePlatformWindow, |
4fe56619 |
58 | Aspect_RenderingContext theGContext, |
58655684 |
59 | const Handle(OpenGl_Caps)& theCaps, |
4fe56619 |
60 | const Handle(OpenGl_Context)& theShareCtx) |
73192b37 |
61 | : myGlContext (new OpenGl_Context (theCaps)), |
4fe56619 |
62 | myOwnGContext (theGContext == 0), |
c357e426 |
63 | myPlatformWindow (thePlatformWindow), |
64 | #if defined(__APPLE__) && defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE |
a2e4f780 |
65 | myUIView (NULL), |
66 | #endif |
f978241f |
67 | mySwapInterval (theCaps->swapInterval) |
4fe56619 |
68 | { |
5ef127d0 |
69 | (void )theDriver; |
c357e426 |
70 | myPlatformWindow->Size (myWidth, myHeight); |
71 | |
72 | #if defined(__APPLE__) |
73 | myWidthPt = myWidth; |
74 | myHeightPt = myHeight; |
75 | #endif |
4fe56619 |
76 | |
a2e4f780 |
77 | #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE |
78 | EAGLContext* aGLContext = theGContext; |
79 | if (aGLContext == NULL) |
4e1523ef |
80 | { |
c357e426 |
81 | void* aViewPtr = (void* )myPlatformWindow->NativeHandle(); |
a2e4f780 |
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"); |
9775fa61 |
96 | throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString()); |
a2e4f780 |
97 | return; |
98 | } |
99 | |
100 | myGlContext->Init (aGLContext, Standard_False); |
4e1523ef |
101 | } |
102 | else |
abe46077 |
103 | { |
a2e4f780 |
104 | if (![EAGLContext setCurrentContext: aGLContext]) |
105 | { |
106 | TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: EAGLContext can not be assigned"); |
9775fa61 |
107 | throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString()); |
a2e4f780 |
108 | return; |
109 | } |
110 | |
111 | myGlContext->Init (aGLContext, Standard_False); |
4e1523ef |
112 | } |
a2e4f780 |
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) |
4e1523ef |
122 | { |
a2e4f780 |
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) |
4e1523ef |
130 | { |
a2e4f780 |
131 | anAttribs[aLastAttrib++] = NSOpenGLPFARendererID; |
132 | anAttribs[aLastAttrib++] = (NSOpenGLPixelFormatAttribute )kCGLRendererGenericFloatID; |
4e1523ef |
133 | } |
a2e4f780 |
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) |
4e1523ef |
143 | { |
a2e4f780 |
144 | aLastAttrib = aLastMainAttrib; |
145 | if (aTryCore == 1) |
4e1523ef |
146 | { |
a2e4f780 |
147 | if (theCaps->contextCompatible) |
4e1523ef |
148 | { |
149 | continue; |
150 | } |
a2e4f780 |
151 | |
152 | // supported since OS X 10.7+ |
153 | anAttribs[aLastAttrib++] = 99; // NSOpenGLPFAOpenGLProfile |
154 | anAttribs[aLastAttrib++] = 0x3200; // NSOpenGLProfileVersion3_2Core |
4e1523ef |
155 | } |
156 | |
a2e4f780 |
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 | } |
4e1523ef |
178 | |
4e1523ef |
179 | if (aGLContext != NULL) |
180 | { |
181 | break; |
182 | } |
183 | } |
184 | |
a2e4f780 |
185 | if (aGLContext == NULL) |
4e1523ef |
186 | { |
a2e4f780 |
187 | TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: NSOpenGLContext creation failed"); |
9775fa61 |
188 | throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString()); |
a2e4f780 |
189 | return; |
4e1523ef |
190 | } |
4e1523ef |
191 | |
a2e4f780 |
192 | if (aTryStereo == 0 |
193 | && theCaps->contextStereo) |
194 | { |
195 | TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: QuadBuffer is unavailable!"); |
3b523c4c |
196 | myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_OTHER, 0, GL_DEBUG_SEVERITY_LOW, aMsg); |
a2e4f780 |
197 | } |
198 | if (aTryCore == 0 |
199 | && !theCaps->contextCompatible) |
200 | { |
201 | TCollection_ExtendedString aMsg("OpenGl_Window::CreateWindow: core profile creation failed."); |
3b523c4c |
202 | myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_LOW, aMsg); |
a2e4f780 |
203 | } |
4fe56619 |
204 | |
c357e426 |
205 | NSView* aView = (NSView* )myPlatformWindow->NativeHandle(); |
a2e4f780 |
206 | [aGLContext setView: aView]; |
207 | isCore = (aTryCore == 1); |
4e1523ef |
208 | } |
209 | |
a2e4f780 |
210 | myGlContext->Init (aGLContext, isCore); |
211 | #endif |
4fe56619 |
212 | |
4fe56619 |
213 | myGlContext->Share (theShareCtx); |
f978241f |
214 | myGlContext->SetSwapInterval (mySwapInterval); |
4fe56619 |
215 | Init(); |
216 | } |
217 | |
218 | // ======================================================================= |
219 | // function : ~OpenGl_Window |
220 | // purpose : |
221 | // ======================================================================= |
222 | OpenGl_Window::~OpenGl_Window() |
223 | { |
a2e4f780 |
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; |
4fe56619 |
237 | myGlContext.Nullify(); |
238 | |
239 | [NSOpenGLContext clearCurrentContext]; |
a2e4f780 |
240 | [aGLCtx clearDrawable]; |
241 | [aGLCtx release]; |
242 | #endif |
4fe56619 |
243 | } |
244 | |
245 | // ======================================================================= |
246 | // function : Resize |
247 | // purpose : call_subr_resize |
248 | // ======================================================================= |
c357e426 |
249 | void OpenGl_Window::Resize() |
4fe56619 |
250 | { |
4fe56619 |
251 | // If the size is not changed - do nothing |
c357e426 |
252 | Standard_Integer aWidthPt = 0; |
253 | Standard_Integer aHeightPt = 0; |
254 | myPlatformWindow->Size (aWidthPt, aHeightPt); |
255 | if (myWidthPt == aWidthPt |
256 | && myHeightPt == aHeightPt) |
4fe56619 |
257 | { |
5501f9a9 |
258 | #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE |
4fe56619 |
259 | return; |
5501f9a9 |
260 | #else |
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:)]) |
265 | { |
266 | return; |
267 | } |
268 | |
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)) |
273 | { |
274 | return; |
275 | } |
276 | #endif |
4fe56619 |
277 | } |
278 | |
c357e426 |
279 | myWidthPt = aWidthPt; |
280 | myHeightPt = aHeightPt; |
4fe56619 |
281 | |
282 | Init(); |
283 | } |
284 | |
285 | // ======================================================================= |
286 | // function : Init |
287 | // purpose : |
288 | // ======================================================================= |
289 | void OpenGl_Window::Init() |
290 | { |
291 | if (!Activate()) |
292 | { |
293 | return; |
294 | } |
295 | |
a2e4f780 |
296 | #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE |
297 | Handle(OpenGl_FrameBuffer) aDefFbo = myGlContext->SetDefaultFrameBuffer (NULL); |
298 | if (!aDefFbo.IsNull()) |
299 | { |
300 | aDefFbo->Release (myGlContext.operator->()); |
301 | } |
302 | else |
303 | { |
304 | aDefFbo = new OpenGl_FrameBuffer(); |
305 | } |
4fe56619 |
306 | |
a2e4f780 |
307 | if (myOwnGContext) |
308 | { |
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); |
318 | |
3c4b62a4 |
319 | if (!aDefFbo->InitWithRB (myGlContext, myWidth, myHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, aWinRBColor)) |
a2e4f780 |
320 | { |
321 | TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO creation failed"); |
9775fa61 |
322 | throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString()); |
a2e4f780 |
323 | return; |
324 | } |
325 | } |
326 | else |
327 | { |
328 | if (!aDefFbo->InitWrapper (myGlContext)) |
329 | { |
330 | TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: default FBO wrapper creation failed"); |
9775fa61 |
331 | throw Aspect_GraphicDeviceDefinitionError(aMsg.ToCString()); |
a2e4f780 |
332 | return; |
333 | } |
4fe56619 |
334 | |
a2e4f780 |
335 | myWidth = aDefFbo->GetVPSizeX(); |
336 | myHeight = aDefFbo->GetVPSizeY(); |
337 | } |
338 | myGlContext->SetDefaultFrameBuffer (aDefFbo); |
339 | aDefFbo->BindBuffer (myGlContext); |
340 | aDefFbo.Nullify(); |
341 | #else |
342 | NSOpenGLContext* aGLCtx = myGlContext->myGContext; |
343 | NSView* aView = [aGLCtx view]; |
344 | NSRect aBounds = [aView bounds]; |
4fe56619 |
345 | |
a2e4f780 |
346 | // we should call this method each time when window is resized |
347 | [aGLCtx update]; |
4fe56619 |
348 | |
a2e4f780 |
349 | if ([aView respondsToSelector: @selector(convertSizeToBacking:)]) |
350 | { |
351 | NSSize aRes = [aView convertSizeToBacking: aBounds.size]; |
352 | myWidth = Standard_Integer(aRes.width); |
353 | myHeight = Standard_Integer(aRes.height); |
354 | } |
355 | else |
356 | { |
357 | myWidth = Standard_Integer(aBounds.size.width); |
358 | myHeight = Standard_Integer(aBounds.size.height); |
359 | } |
360 | myWidthPt = Standard_Integer(aBounds.size.width); |
361 | myHeightPt = Standard_Integer(aBounds.size.height); |
362 | #endif |
363 | |
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) |
370 | { |
371 | ::glMatrixMode (GL_MODELVIEW); |
372 | } |
373 | #endif |
4fe56619 |
374 | } |
375 | |
c357e426 |
376 | // ======================================================================= |
377 | // function : SetSwapInterval |
378 | // purpose : |
379 | // ======================================================================= |
380 | void OpenGl_Window::SetSwapInterval() |
381 | { |
382 | if (mySwapInterval != myGlContext->caps->swapInterval) |
383 | { |
384 | mySwapInterval = myGlContext->caps->swapInterval; |
385 | myGlContext->SetSwapInterval (mySwapInterval); |
386 | } |
387 | } |
388 | |
4fe56619 |
389 | #endif // __APPLE__ |