Commit | Line | Data |
---|---|---|
b311480e | 1 | // Created on: 2011-10-20 |
2 | // Created by: Sergey ZERCHANINOV | |
1981cb22 | 3 | // Copyright (c) 2011-2013 OPEN CASCADE SAS |
b311480e | 4 | // |
973c2be1 | 5 | // This file is part of Open CASCADE Technology software library. |
b311480e | 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. | |
b311480e | 12 | // |
973c2be1 | 13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. | |
b311480e | 15 | |
2166f0fa | 16 | #include <OpenGl_GraphicDriver.hxx> |
f0430952 | 17 | #include <OpenGl_Context.hxx> |
938d4544 | 18 | #include <OpenGl_Flipper.hxx> |
a174a3c5 | 19 | #include <OpenGl_GraduatedTrihedron.hxx> |
20 | #include <OpenGl_Group.hxx> | |
2166f0fa | 21 | #include <OpenGl_View.hxx> |
a6eb515f | 22 | #include <OpenGl_StencilTest.hxx> |
a174a3c5 | 23 | #include <OpenGl_Text.hxx> |
24 | #include <OpenGl_Trihedron.hxx> | |
2166f0fa | 25 | #include <OpenGl_Workspace.hxx> |
7fd59977 | 26 | |
25b97fac | 27 | #include <Aspect_GraphicDeviceDefinitionError.hxx> |
c357e426 | 28 | #include <Aspect_IdentDefinitionError.hxx> |
25b97fac | 29 | #include <Message_Messenger.hxx> |
73192b37 | 30 | #include <OSD_Environment.hxx> |
a174a3c5 | 31 | #include <Standard_NotImplemented.hxx> |
32 | ||
92efcf78 | 33 | IMPLEMENT_STANDARD_RTTIEXT(OpenGl_GraphicDriver,Graphic3d_GraphicDriver) |
34 | ||
c357e426 | 35 | #if defined(_WIN32) |
36 | #include <WNT_Window.hxx> | |
37 | #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX) | |
38 | #include <Cocoa_Window.hxx> | |
39 | #else | |
40 | #include <Xw_Window.hxx> | |
41 | #endif | |
42 | ||
d8d01f6e | 43 | #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)) |
73192b37 | 44 | #include <X11/Xlib.h> // XOpenDisplay() |
45 | #endif | |
46 | ||
d8d01f6e | 47 | #if defined(HAVE_EGL) || defined(__ANDROID__) || defined(__QNX__) |
25b97fac | 48 | #include <EGL/egl.h> |
49 | #endif | |
50 | ||
2166f0fa SK |
51 | namespace |
52 | { | |
5e27df78 | 53 | static const Handle(OpenGl_Context) TheNullGlCtx; |
7fd59977 | 54 | } |
55 | ||
65993a95 | 56 | // ======================================================================= |
57 | // function : OpenGl_GraphicDriver | |
58 | // purpose : | |
59 | // ======================================================================= | |
25b97fac | 60 | OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp, |
05e2200b | 61 | const Standard_Boolean theToInitialize) |
fe9fc669 | 62 | : Graphic3d_GraphicDriver (theDisp), |
05e2200b | 63 | myIsOwnContext (Standard_False), |
d8d01f6e | 64 | #if defined(HAVE_EGL) || defined(__ANDROID__) || defined(__QNX__) |
25b97fac | 65 | myEglDisplay ((Aspect_Display )EGL_NO_DISPLAY), |
66 | myEglContext ((Aspect_RenderingContext )EGL_NO_CONTEXT), | |
67 | myEglConfig (NULL), | |
68 | #endif | |
65993a95 | 69 | myCaps (new OpenGl_Caps()), |
70 | myMapOfView (1, NCollection_BaseAllocator::CommonBaseAllocator()), | |
a521d90d | 71 | myMapOfStructure (1, NCollection_BaseAllocator::CommonBaseAllocator()) |
65993a95 | 72 | { |
d8d01f6e | 73 | #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)) |
73192b37 | 74 | if (myDisplayConnection.IsNull()) |
75 | { | |
73192b37 | 76 | //Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_GraphicDriver: cannot connect to X server!"); |
fe9fc669 | 77 | return; |
73192b37 | 78 | } |
79 | ||
73192b37 | 80 | Display* aDisplay = myDisplayConnection->GetDisplay(); |
81 | Bool toSync = ::getenv ("CSF_GraphicSync") != NULL | |
82 | || ::getenv ("CALL_SYNCHRO_X") != NULL; | |
83 | XSynchronize (aDisplay, toSync); | |
84 | ||
25b97fac | 85 | #if !defined(HAVE_EGL) |
73192b37 | 86 | // does the server know about OpenGL & GLX? |
87 | int aDummy; | |
88 | if (!XQueryExtension (aDisplay, "GLX", &aDummy, &aDummy, &aDummy)) | |
89 | { | |
05e2200b | 90 | ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, this system doesn't appear to support OpenGL!", Message_Warning); |
73192b37 | 91 | } |
92 | #endif | |
25b97fac | 93 | #endif |
05e2200b | 94 | if (theToInitialize |
95 | && !InitContext()) | |
96 | { | |
97 | Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_GraphicDriver: default context can not be initialized!"); | |
98 | } | |
c357e426 | 99 | |
100 | // default layers are always presented in display layer sequence it can not be removed | |
101 | Graphic3d_ZLayerSettings anUnderlaySettings; | |
102 | anUnderlaySettings.Flags = 0; | |
103 | anUnderlaySettings.IsImmediate = false; | |
104 | myLayerIds.Add (Graphic3d_ZLayerId_BotOSD); | |
105 | myLayerSeq.Append (Graphic3d_ZLayerId_BotOSD); | |
106 | myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_BotOSD, anUnderlaySettings); | |
107 | ||
108 | Graphic3d_ZLayerSettings aDefSettings; | |
109 | aDefSettings.Flags = Graphic3d_ZLayerDepthTest | |
110 | | Graphic3d_ZLayerDepthWrite; | |
111 | aDefSettings.IsImmediate = false; | |
112 | myLayerIds.Add (Graphic3d_ZLayerId_Default); | |
113 | myLayerSeq.Append (Graphic3d_ZLayerId_Default); | |
114 | myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Default, aDefSettings); | |
115 | ||
116 | Graphic3d_ZLayerSettings aTopSettings; | |
117 | aTopSettings.Flags = Graphic3d_ZLayerDepthTest | |
118 | | Graphic3d_ZLayerDepthWrite; | |
119 | aTopSettings.IsImmediate = true; | |
120 | myLayerIds.Add (Graphic3d_ZLayerId_Top); | |
121 | myLayerSeq.Append (Graphic3d_ZLayerId_Top); | |
122 | myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Top, aTopSettings); | |
123 | ||
124 | Graphic3d_ZLayerSettings aTopmostSettings; | |
125 | aTopmostSettings.Flags = Graphic3d_ZLayerDepthTest | |
126 | | Graphic3d_ZLayerDepthWrite | |
127 | | Graphic3d_ZLayerDepthClear; | |
128 | aTopmostSettings.IsImmediate = true; | |
129 | myLayerIds.Add (Graphic3d_ZLayerId_Topmost); | |
130 | myLayerSeq.Append (Graphic3d_ZLayerId_Topmost); | |
131 | myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Topmost, aTopmostSettings); | |
132 | ||
133 | Graphic3d_ZLayerSettings anOsdSettings; | |
134 | anOsdSettings.Flags = 0; | |
135 | anOsdSettings.IsImmediate = true; | |
136 | myLayerIds.Add (Graphic3d_ZLayerId_TopOSD); | |
137 | myLayerSeq.Append (Graphic3d_ZLayerId_TopOSD); | |
138 | myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_TopOSD, anOsdSettings); | |
05e2200b | 139 | } |
25b97fac | 140 | |
05e2200b | 141 | // ======================================================================= |
142 | // function : ~OpenGl_GraphicDriver | |
143 | // purpose : | |
144 | // ======================================================================= | |
145 | OpenGl_GraphicDriver::~OpenGl_GraphicDriver() | |
146 | { | |
147 | ReleaseContext(); | |
148 | } | |
149 | ||
150 | // ======================================================================= | |
151 | // function : ReleaseContext | |
152 | // purpose : | |
153 | // ======================================================================= | |
154 | void OpenGl_GraphicDriver::ReleaseContext() | |
155 | { | |
156 | Handle(OpenGl_Context) aCtxShared; | |
c357e426 | 157 | for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); |
158 | aViewIter.More(); aViewIter.Next()) | |
25b97fac | 159 | { |
c357e426 | 160 | const Handle(OpenGl_View)& aView = aViewIter.ChangeValue(); |
161 | const Handle(OpenGl_Window)& aWindow = aView->GlWindow(); | |
162 | if (aWindow.IsNull()) | |
163 | { | |
164 | continue; | |
165 | } | |
166 | ||
167 | const Handle(OpenGl_Context)& aCtx = aWindow->GetGlContext(); | |
05e2200b | 168 | if (aCtx->MakeCurrent() |
169 | && aCtxShared.IsNull()) | |
25b97fac | 170 | { |
05e2200b | 171 | aCtxShared = aCtx; |
25b97fac | 172 | } |
05e2200b | 173 | } |
174 | ||
175 | if (!aCtxShared.IsNull()) | |
176 | { | |
177 | aCtxShared->MakeCurrent(); | |
178 | } | |
a272ed94 | 179 | for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); |
05e2200b | 180 | aViewIter.More(); aViewIter.Next()) |
181 | { | |
182 | const Handle(OpenGl_View)& aView = aViewIter.ChangeValue(); | |
183 | aView->ReleaseGlResources (aCtxShared); | |
184 | } | |
185 | ||
186 | for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure); | |
187 | aStructIt.More (); aStructIt.Next()) | |
188 | { | |
189 | OpenGl_Structure* aStruct = aStructIt.ChangeValue(); | |
190 | aStruct->ReleaseGlResources (aCtxShared); | |
191 | } | |
05e2200b | 192 | myDeviceLostFlag = myDeviceLostFlag || !myMapOfStructure.IsEmpty(); |
193 | ||
c357e426 | 194 | for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); |
195 | aViewIter.More(); aViewIter.Next()) | |
05e2200b | 196 | { |
c357e426 | 197 | const Handle(OpenGl_View)& aView = aViewIter.ChangeValue(); |
198 | const Handle(OpenGl_Window)& aWindow = aView->GlWindow(); | |
199 | if (aWindow.IsNull()) | |
200 | { | |
201 | continue; | |
202 | } | |
203 | ||
204 | aWindow->GetGlContext()->forcedRelease(); | |
05e2200b | 205 | } |
25b97fac | 206 | |
d8d01f6e | 207 | #if defined(HAVE_EGL) || defined(__ANDROID__) || defined(__QNX__) |
05e2200b | 208 | if (myIsOwnContext) |
209 | { | |
210 | if (myEglContext != (Aspect_RenderingContext )EGL_NO_CONTEXT) | |
25b97fac | 211 | { |
05e2200b | 212 | if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) != EGL_TRUE) |
213 | { | |
214 | ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, FAILED to release OpenGL context!", Message_Warning); | |
215 | } | |
216 | eglDestroyContext ((EGLDisplay )myEglDisplay, (EGLContext )myEglContext); | |
217 | } | |
25b97fac | 218 | |
05e2200b | 219 | if (myEglDisplay != (Aspect_Display )EGL_NO_DISPLAY) |
25b97fac | 220 | { |
05e2200b | 221 | if (eglTerminate ((EGLDisplay )myEglDisplay) != EGL_TRUE) |
222 | { | |
223 | ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, EGL, eglTerminate FAILED!", Message_Warning); | |
224 | } | |
25b97fac | 225 | } |
25b97fac | 226 | } |
227 | ||
05e2200b | 228 | myEglDisplay = (Aspect_Display )EGL_NO_DISPLAY; |
229 | myEglContext = (Aspect_RenderingContext )EGL_NO_CONTEXT; | |
230 | myEglConfig = NULL; | |
231 | #endif | |
232 | myIsOwnContext = Standard_False; | |
233 | } | |
234 | ||
235 | // ======================================================================= | |
236 | // function : InitContext | |
237 | // purpose : | |
238 | // ======================================================================= | |
239 | Standard_Boolean OpenGl_GraphicDriver::InitContext() | |
240 | { | |
241 | ReleaseContext(); | |
d8d01f6e | 242 | #if defined(HAVE_EGL) || defined(__ANDROID__) || defined(__QNX__) |
05e2200b | 243 | |
d8d01f6e | 244 | #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)) |
05e2200b | 245 | if (myDisplayConnection.IsNull()) |
246 | { | |
247 | return Standard_False; | |
248 | } | |
249 | Display* aDisplay = myDisplayConnection->GetDisplay(); | |
25b97fac | 250 | myEglDisplay = (Aspect_Display )eglGetDisplay (aDisplay); |
251 | #else | |
252 | myEglDisplay = (Aspect_Display )eglGetDisplay (EGL_DEFAULT_DISPLAY); | |
253 | #endif | |
254 | if ((EGLDisplay )myEglDisplay == EGL_NO_DISPLAY) | |
255 | { | |
05e2200b | 256 | ::Message::DefaultMessenger()->Send ("Error: no EGL display!", Message_Fail); |
257 | return Standard_False; | |
25b97fac | 258 | } |
259 | ||
260 | EGLint aVerMajor = 0; EGLint aVerMinor = 0; | |
261 | if (eglInitialize ((EGLDisplay )myEglDisplay, &aVerMajor, &aVerMinor) != EGL_TRUE) | |
262 | { | |
05e2200b | 263 | ::Message::DefaultMessenger()->Send ("Error: EGL display is unavailable!", Message_Fail); |
264 | return Standard_False; | |
25b97fac | 265 | } |
266 | ||
05e2200b | 267 | EGLint aConfigAttribs[] = |
25b97fac | 268 | { |
269 | EGL_RED_SIZE, 8, | |
270 | EGL_GREEN_SIZE, 8, | |
271 | EGL_BLUE_SIZE, 8, | |
272 | EGL_ALPHA_SIZE, 0, | |
273 | EGL_DEPTH_SIZE, 24, | |
274 | EGL_STENCIL_SIZE, 8, | |
275 | #if defined(GL_ES_VERSION_2_0) | |
276 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, | |
277 | #else | |
278 | EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, | |
279 | #endif | |
280 | EGL_NONE | |
281 | }; | |
282 | ||
283 | EGLint aNbConfigs = 0; | |
05e2200b | 284 | if (eglChooseConfig ((EGLDisplay )myEglDisplay, aConfigAttribs, &myEglConfig, 1, &aNbConfigs) != EGL_TRUE |
285 | || myEglConfig == NULL) | |
25b97fac | 286 | { |
05e2200b | 287 | eglGetError(); |
288 | aConfigAttribs[4 * 2 + 1] = 16; // try config with smaller depth buffer | |
289 | if (eglChooseConfig ((EGLDisplay )myEglDisplay, aConfigAttribs, &myEglConfig, 1, &aNbConfigs) != EGL_TRUE | |
290 | || myEglConfig == NULL) | |
291 | { | |
292 | ::Message::DefaultMessenger()->Send ("Error: EGL does not provide compatible configurations!", Message_Fail); | |
293 | return Standard_False; | |
294 | } | |
25b97fac | 295 | } |
25b97fac | 296 | |
297 | #if defined(GL_ES_VERSION_2_0) | |
298 | EGLint anEglCtxAttribs[] = | |
299 | { | |
300 | EGL_CONTEXT_CLIENT_VERSION, 2, | |
301 | EGL_NONE | |
302 | }; | |
05e2200b | 303 | if (eglBindAPI (EGL_OPENGL_ES_API) != EGL_TRUE) |
304 | { | |
305 | ::Message::DefaultMessenger()->Send ("Error: EGL does not provide OpenGL ES client!", Message_Fail); | |
306 | return Standard_False; | |
307 | } | |
25b97fac | 308 | #else |
309 | EGLint* anEglCtxAttribs = NULL; | |
05e2200b | 310 | if (eglBindAPI (EGL_OPENGL_API) != EGL_TRUE) |
311 | { | |
312 | ::Message::DefaultMessenger()->Send ("Error: EGL does not provide OpenGL client!", Message_Fail); | |
313 | return Standard_False; | |
314 | } | |
25b97fac | 315 | #endif |
316 | ||
317 | myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs); | |
318 | if ((EGLContext )myEglContext == EGL_NO_CONTEXT) | |
319 | { | |
05e2200b | 320 | ::Message::DefaultMessenger()->Send ("Error: EGL is unable to create OpenGL context!", Message_Fail); |
321 | return Standard_False; | |
25b97fac | 322 | } |
323 | if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, (EGLContext )myEglContext) != EGL_TRUE) | |
324 | { | |
05e2200b | 325 | ::Message::DefaultMessenger()->Send ("Error: EGL is unable bind OpenGL context!", Message_Fail); |
326 | return Standard_False; | |
25b97fac | 327 | } |
25b97fac | 328 | #endif |
05e2200b | 329 | myIsOwnContext = Standard_True; |
330 | return Standard_True; | |
73192b37 | 331 | } |
332 | ||
d8d01f6e | 333 | #if defined(HAVE_EGL) || defined(__ANDROID__) || defined(__QNX__) |
05e2200b | 334 | // ======================================================================= |
335 | // function : InitEglContext | |
336 | // purpose : | |
337 | // ======================================================================= | |
338 | Standard_Boolean OpenGl_GraphicDriver::InitEglContext (Aspect_Display theEglDisplay, | |
339 | Aspect_RenderingContext theEglContext, | |
340 | void* theEglConfig) | |
341 | { | |
342 | ReleaseContext(); | |
d8d01f6e | 343 | #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)) |
05e2200b | 344 | if (myDisplayConnection.IsNull()) |
345 | { | |
346 | return Standard_False; | |
347 | } | |
348 | #endif | |
349 | ||
350 | if ((EGLDisplay )theEglDisplay == EGL_NO_DISPLAY | |
351 | || (EGLContext )theEglContext == EGL_NO_CONTEXT | |
352 | || theEglConfig == NULL) | |
353 | { | |
354 | return Standard_False; | |
355 | } | |
356 | myEglDisplay = theEglDisplay; | |
357 | myEglContext = theEglContext; | |
358 | myEglConfig = theEglConfig; | |
359 | return Standard_True; | |
360 | } | |
361 | #endif | |
362 | ||
73192b37 | 363 | // ======================================================================= |
364 | // function : InquireLightLimit | |
365 | // purpose : | |
366 | // ======================================================================= | |
367 | Standard_Integer OpenGl_GraphicDriver::InquireLightLimit() | |
368 | { | |
369 | return OpenGLMaxLights; | |
370 | } | |
371 | ||
372 | // ======================================================================= | |
373 | // function : InquireViewLimit | |
374 | // purpose : | |
375 | // ======================================================================= | |
376 | Standard_Integer OpenGl_GraphicDriver::InquireViewLimit() | |
377 | { | |
378 | return 10000; | |
379 | } | |
380 | ||
381 | // ======================================================================= | |
382 | // function : InquirePlaneLimit | |
383 | // purpose : | |
384 | // ======================================================================= | |
385 | Standard_Integer OpenGl_GraphicDriver::InquirePlaneLimit() | |
386 | { | |
387 | // NOTE the 2 first planes are reserved for ZClipping | |
388 | const Handle(OpenGl_Context)& aCtx = GetSharedContext(); | |
389 | return aCtx.IsNull() ? 0 : Max (aCtx->MaxClipPlanes() - 2, 0); | |
390 | } | |
391 | ||
f0430952 | 392 | // ======================================================================= |
5e27df78 | 393 | // function : DefaultTextHeight |
f0430952 | 394 | // purpose : |
395 | // ======================================================================= | |
5e27df78 | 396 | Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const |
2166f0fa | 397 | { |
5e27df78 | 398 | return 16.; |
2166f0fa SK |
399 | } |
400 | ||
f0430952 | 401 | // ======================================================================= |
402 | // function : EnableVBO | |
403 | // purpose : | |
404 | // ======================================================================= | |
2166f0fa | 405 | void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn) |
7fd59977 | 406 | { |
58655684 | 407 | myCaps->vboDisable = !theToTurnOn; |
7fd59977 | 408 | } |
f0430952 | 409 | |
5e27df78 | 410 | // ======================================================================= |
411 | // function : GetSharedContext | |
412 | // purpose : | |
413 | // ======================================================================= | |
414 | const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext() const | |
415 | { | |
c357e426 | 416 | if (myMapOfView.IsEmpty()) |
5e27df78 | 417 | { |
418 | return TheNullGlCtx; | |
419 | } | |
420 | ||
c357e426 | 421 | NCollection_Map<Handle(OpenGl_View)>::Iterator anIter (myMapOfView); |
422 | for (; anIter.More(); anIter.Next()) | |
423 | { | |
424 | Handle(OpenGl_Window) aWindow = anIter.Value()->GlWindow(); | |
425 | if (aWindow.IsNull()) | |
426 | { | |
427 | continue; | |
428 | } | |
429 | ||
430 | return aWindow->GetGlContext(); | |
431 | } | |
432 | ||
433 | return TheNullGlCtx; | |
5e27df78 | 434 | } |
435 | ||
f0430952 | 436 | // ======================================================================= |
437 | // function : MemoryInfo | |
438 | // purpose : | |
439 | // ======================================================================= | |
440 | Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes, | |
441 | TCollection_AsciiString& theInfo) const | |
442 | { | |
443 | // this is extra work (for OpenGl_Context initialization)... | |
444 | OpenGl_Context aGlCtx; | |
445 | if (!aGlCtx.Init()) | |
446 | { | |
447 | return Standard_False; | |
448 | } | |
449 | theFreeBytes = aGlCtx.AvailableMemory(); | |
450 | theInfo = aGlCtx.MemoryInfo(); | |
451 | return !theInfo.IsEmpty(); | |
452 | } | |
1981cb22 | 453 | |
454 | // ======================================================================= | |
c357e426 | 455 | // function : SetBuffersNoSwap |
1981cb22 | 456 | // purpose : |
457 | // ======================================================================= | |
c357e426 | 458 | void OpenGl_GraphicDriver::SetBuffersNoSwap (const Standard_Boolean theIsNoSwap) |
1981cb22 | 459 | { |
c357e426 | 460 | myCaps->buffersNoSwap = theIsNoSwap; |
1981cb22 | 461 | } |
462 | ||
a174a3c5 | 463 | // ======================================================================= |
c357e426 | 464 | // function : TextSize |
a174a3c5 | 465 | // purpose : |
466 | // ======================================================================= | |
4b1c8733 | 467 | void OpenGl_GraphicDriver::TextSize (const Handle(Graphic3d_CView)& theView, |
468 | const Standard_CString theText, | |
469 | const Standard_ShortReal theHeight, | |
470 | Standard_ShortReal& theWidth, | |
471 | Standard_ShortReal& theAscent, | |
472 | Standard_ShortReal& theDescent) const | |
a174a3c5 | 473 | { |
c357e426 | 474 | const Handle(OpenGl_Context)& aCtx = GetSharedContext(); |
475 | if (aCtx.IsNull()) | |
a174a3c5 | 476 | { |
c357e426 | 477 | return; |
a174a3c5 | 478 | } |
479 | ||
c357e426 | 480 | const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight; |
481 | OpenGl_TextParam aTextParam; | |
482 | aTextParam.Height = (int )aHeight; | |
483 | OpenGl_AspectText aTextAspect; | |
484 | CALL_DEF_CONTEXTTEXT aDefaultContextText = | |
485 | { | |
486 | 1, //IsDef | |
487 | 1, //IsSet | |
488 | "Courier", //Font | |
489 | 0.3F, //Space | |
490 | 1.F, //Expan | |
491 | { 1.F, 1.F, 1.F }, //Color | |
492 | (int)Aspect_TOST_NORMAL, //Style | |
493 | (int)Aspect_TODT_NORMAL, //DisplayType | |
494 | { 1.F, 1.F, 1.F }, //ColorSubTitle | |
495 | 0, //TextZoomable | |
496 | 0.F, //TextAngle | |
04e93070 | 497 | (int)Font_FA_Regular, //TextFontAspect |
498 | 0 //ShaderProgram | |
c357e426 | 499 | }; |
500 | aTextAspect.SetAspect(aDefaultContextText); | |
501 | TCollection_ExtendedString anExtText = theText; | |
502 | NCollection_String aText = (Standard_Utf16Char* )anExtText.ToExtString(); | |
4b1c8733 | 503 | OpenGl_Text::StringSize(aCtx, aText, aTextAspect, aTextParam, theView->RenderingParams().Resolution, theWidth, theAscent, theDescent); |
a174a3c5 | 504 | } |
505 | ||
c357e426 | 506 | //======================================================================= |
507 | //function : AddZLayer | |
508 | //purpose : | |
509 | //======================================================================= | |
510 | void OpenGl_GraphicDriver::AddZLayer (const Graphic3d_ZLayerId theLayerId) | |
a174a3c5 | 511 | { |
c357e426 | 512 | if (theLayerId < 1) |
536d98e2 | 513 | { |
c357e426 | 514 | Standard_ASSERT_RAISE (theLayerId > 0, |
515 | "OpenGl_GraphicDriver::AddZLayer, " | |
516 | "negative and zero IDs are reserved"); | |
536d98e2 | 517 | } |
518 | ||
c357e426 | 519 | myLayerIds.Add (theLayerId); |
520 | myLayerSeq.Append (theLayerId); | |
a174a3c5 | 521 | |
c357e426 | 522 | // Default z-layer settings |
523 | myMapOfZLayerSettings.Bind (theLayerId, Graphic3d_ZLayerSettings()); | |
524 | ||
525 | // Add layer to all views | |
526 | NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); | |
527 | for (; aViewIt.More(); aViewIt.Next()) | |
a174a3c5 | 528 | { |
c357e426 | 529 | aViewIt.Value()->AddZLayer (theLayerId); |
a174a3c5 | 530 | } |
531 | } | |
532 | ||
c357e426 | 533 | //======================================================================= |
534 | //function : RemoveZLayer | |
535 | //purpose : | |
536 | //======================================================================= | |
537 | void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_ZLayerId theLayerId) | |
a174a3c5 | 538 | { |
c357e426 | 539 | Standard_ASSERT_RAISE (theLayerId > 0, |
540 | "OpenGl_GraphicDriver::AddZLayer, " | |
541 | "negative and zero IDs are reserved" | |
542 | "and can not be removed"); | |
543 | ||
544 | Standard_ASSERT_RAISE (myLayerIds.Contains (theLayerId), | |
545 | "OpenGl_GraphicDriver::RemoveZLayer, " | |
546 | "Layer with theLayerId does not exist"); | |
547 | ||
548 | // Remove layer from all of the views | |
549 | NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); | |
550 | for (; aViewIt.More(); aViewIt.Next()) | |
551 | { | |
552 | aViewIt.Value()->RemoveZLayer (theLayerId); | |
553 | } | |
554 | ||
555 | // Unset Z layer for all of the structures. | |
556 | NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure); | |
557 | for( ; aStructIt.More (); aStructIt.Next ()) | |
a174a3c5 | 558 | { |
c357e426 | 559 | OpenGl_Structure* aStruct = aStructIt.ChangeValue (); |
560 | if (aStruct->ZLayer() == theLayerId) | |
561 | aStruct->SetZLayer (Graphic3d_ZLayerId_Default); | |
a174a3c5 | 562 | } |
c357e426 | 563 | |
564 | // Remove index | |
565 | for (int aIdx = 1; aIdx <= myLayerSeq.Length (); aIdx++) | |
566 | { | |
567 | if (myLayerSeq (aIdx) == theLayerId) | |
568 | { | |
569 | myLayerSeq.Remove (aIdx); | |
570 | break; | |
571 | } | |
572 | } | |
573 | ||
574 | myMapOfZLayerSettings.UnBind (theLayerId); | |
575 | myLayerIds.Remove (theLayerId); | |
a174a3c5 | 576 | } |
577 | ||
c357e426 | 578 | //======================================================================= |
579 | //function : ZLayers | |
580 | //purpose : | |
581 | //======================================================================= | |
582 | void OpenGl_GraphicDriver::ZLayers (TColStd_SequenceOfInteger& theLayerSeq) const | |
a174a3c5 | 583 | { |
c357e426 | 584 | theLayerSeq.Assign (myLayerSeq); |
a174a3c5 | 585 | } |
586 | ||
c357e426 | 587 | //======================================================================= |
588 | //function : SetZLayerSettings | |
589 | //purpose : | |
590 | //======================================================================= | |
591 | void OpenGl_GraphicDriver::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId, | |
592 | const Graphic3d_ZLayerSettings& theSettings) | |
a174a3c5 | 593 | { |
c357e426 | 594 | // Change Z layer settings in all managed views |
595 | NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); | |
596 | for (; aViewIt.More(); aViewIt.Next()) | |
a174a3c5 | 597 | { |
c357e426 | 598 | aViewIt.Value()->SetZLayerSettings (theLayerId, theSettings); |
a174a3c5 | 599 | } |
600 | ||
c357e426 | 601 | if (myMapOfZLayerSettings.IsBound (theLayerId)) |
602 | { | |
603 | myMapOfZLayerSettings.ChangeFind (theLayerId) = theSettings; | |
604 | } | |
605 | else | |
606 | { | |
607 | myMapOfZLayerSettings.Bind (theLayerId, theSettings); | |
608 | } | |
a174a3c5 | 609 | } |
610 | ||
c357e426 | 611 | //======================================================================= |
612 | //function : ZLayerSettings | |
613 | //purpose : | |
614 | //======================================================================= | |
615 | Graphic3d_ZLayerSettings OpenGl_GraphicDriver::ZLayerSettings (const Graphic3d_ZLayerId theLayerId) | |
a174a3c5 | 616 | { |
c357e426 | 617 | Standard_ASSERT_RAISE (myLayerIds.Contains (theLayerId), |
618 | "OpenGl_GraphicDriver::ZLayerSettings, " | |
619 | "Layer with theLayerId does not exist"); | |
620 | ||
621 | return myMapOfZLayerSettings.Find (theLayerId); | |
a174a3c5 | 622 | } |
623 | ||
624 | // ======================================================================= | |
c357e426 | 625 | // function : Structure |
a174a3c5 | 626 | // purpose : |
627 | // ======================================================================= | |
c357e426 | 628 | Handle(Graphic3d_CStructure) OpenGl_GraphicDriver::CreateStructure (const Handle(Graphic3d_StructureManager)& theManager) |
a174a3c5 | 629 | { |
c357e426 | 630 | Handle(OpenGl_Structure) aStructure = new OpenGl_Structure (theManager); |
631 | myMapOfStructure.Bind (aStructure->Id, aStructure.operator->()); | |
632 | return aStructure; | |
a174a3c5 | 633 | } |
634 | ||
635 | // ======================================================================= | |
c357e426 | 636 | // function : Structure |
a174a3c5 | 637 | // purpose : |
638 | // ======================================================================= | |
c357e426 | 639 | void OpenGl_GraphicDriver::RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure) |
a174a3c5 | 640 | { |
c357e426 | 641 | OpenGl_Structure* aStructure = NULL; |
642 | if (!myMapOfStructure.Find (theCStructure->Id, aStructure)) | |
a174a3c5 | 643 | { |
c357e426 | 644 | return; |
a174a3c5 | 645 | } |
c357e426 | 646 | |
647 | myMapOfStructure.UnBind (theCStructure->Id); | |
648 | aStructure->Release (GetSharedContext()); | |
649 | theCStructure.Nullify(); | |
a174a3c5 | 650 | } |
651 | ||
652 | // ======================================================================= | |
c357e426 | 653 | // function : View |
a174a3c5 | 654 | // purpose : |
655 | // ======================================================================= | |
c357e426 | 656 | Handle(Graphic3d_CView) OpenGl_GraphicDriver::CreateView (const Handle(Graphic3d_StructureManager)& theMgr) |
a174a3c5 | 657 | { |
c357e426 | 658 | Handle(OpenGl_View) aView = new OpenGl_View (theMgr, this, myCaps, myDeviceLostFlag, &myStateCounter); |
659 | ||
660 | myMapOfView.Add (aView); | |
661 | ||
662 | for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next()) | |
a174a3c5 | 663 | { |
c357e426 | 664 | const Graphic3d_ZLayerId aLayerID = aLayerIt.Value(); |
665 | const Graphic3d_ZLayerSettings& aSettings = myMapOfZLayerSettings.Find (aLayerID); | |
666 | aView->AddZLayer (aLayerID); | |
667 | aView->SetZLayerSettings (aLayerID, aSettings); | |
a174a3c5 | 668 | } |
c357e426 | 669 | |
670 | return aView; | |
a174a3c5 | 671 | } |
672 | ||
673 | // ======================================================================= | |
c357e426 | 674 | // function : RemoveView |
a174a3c5 | 675 | // purpose : |
676 | // ======================================================================= | |
c357e426 | 677 | void OpenGl_GraphicDriver::RemoveView (const Handle(Graphic3d_CView)& theView) |
a174a3c5 | 678 | { |
c357e426 | 679 | Handle(OpenGl_Context) aCtx = GetSharedContext(); |
680 | Handle(OpenGl_View) aView = Handle(OpenGl_View)::DownCast (theView); | |
681 | if (aView.IsNull()) | |
a174a3c5 | 682 | { |
c357e426 | 683 | return; |
a174a3c5 | 684 | } |
a174a3c5 | 685 | |
c357e426 | 686 | if (!myMapOfView.Remove (aView)) |
687 | { | |
688 | return; | |
689 | } | |
690 | ||
691 | Handle(OpenGl_Window) aWindow = aView->GlWindow(); | |
692 | if (!aWindow.IsNull() | |
693 | && aWindow->GetGlContext()->MakeCurrent()) | |
a79f67f8 | 694 | { |
c357e426 | 695 | aCtx = aWindow->GetGlContext(); |
696 | } | |
697 | else | |
698 | { | |
699 | // try to hijack another context if any | |
700 | const Handle(OpenGl_Context)& anOtherCtx = GetSharedContext(); | |
701 | if (!anOtherCtx.IsNull() | |
702 | && anOtherCtx != aWindow->GetGlContext()) | |
703 | { | |
704 | aCtx = anOtherCtx; | |
705 | aCtx->MakeCurrent(); | |
706 | } | |
707 | } | |
708 | ||
709 | aView->ReleaseGlResources (aCtx); | |
710 | if (myMapOfView.IsEmpty()) | |
711 | { | |
712 | // The last view removed but some objects still present. | |
713 | // Release GL resources now without object destruction. | |
714 | for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure); | |
715 | aStructIt.More (); aStructIt.Next()) | |
716 | { | |
717 | OpenGl_Structure* aStruct = aStructIt.ChangeValue(); | |
718 | aStruct->ReleaseGlResources (aCtx); | |
719 | } | |
720 | myDeviceLostFlag = !myMapOfStructure.IsEmpty(); | |
a79f67f8 | 721 | } |
a174a3c5 | 722 | } |
62e1beed | 723 | |
724 | // ======================================================================= | |
c357e426 | 725 | // function : Window |
62e1beed | 726 | // purpose : |
727 | // ======================================================================= | |
c357e426 | 728 | Handle(OpenGl_Window) OpenGl_GraphicDriver::CreateRenderWindow (const Handle(Aspect_Window)& theWindow, |
729 | const Aspect_RenderingContext theContext) | |
62e1beed | 730 | { |
c357e426 | 731 | Handle(OpenGl_Context) aShareCtx = GetSharedContext(); |
732 | Handle(OpenGl_Window) aWindow = new OpenGl_Window (this, theWindow, theContext, myCaps, aShareCtx); | |
733 | return aWindow; | |
62e1beed | 734 | } |
27f85086 | 735 | |
c357e426 | 736 | //======================================================================= |
737 | //function : ViewExists | |
738 | //purpose : | |
739 | //======================================================================= | |
740 | Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)& AWindow, Handle(Graphic3d_CView)& theView) | |
27f85086 | 741 | { |
c357e426 | 742 | Standard_Boolean isExist = Standard_False; |
743 | ||
744 | // Parse the list of views to find | |
745 | // a view with the specified window | |
746 | ||
747 | #if defined(_WIN32) | |
748 | const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow); | |
749 | Aspect_Handle TheSpecifiedWindowId = THEWindow->HWindow (); | |
750 | #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX) | |
751 | const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow); | |
752 | #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE | |
753 | UIView* TheSpecifiedWindowId = THEWindow->HView(); | |
754 | #else | |
755 | NSView* TheSpecifiedWindowId = THEWindow->HView(); | |
756 | #endif | |
d8d01f6e | 757 | #elif defined(__ANDROID__) || defined(__QNX__) |
20aeeb7b | 758 | (void )AWindow; |
c357e426 | 759 | int TheSpecifiedWindowId = -1; |
760 | #else | |
761 | const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow); | |
762 | int TheSpecifiedWindowId = int (THEWindow->XWindow ()); | |
763 | #endif | |
764 | ||
765 | NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); | |
766 | for(; aViewIt.More(); aViewIt.Next()) | |
27f85086 | 767 | { |
c357e426 | 768 | const Handle(OpenGl_View)& aView = aViewIt.Value(); |
769 | if (aView->IsDefined() && aView->IsActive()) | |
770 | { | |
771 | const Handle(Aspect_Window) AspectWindow = aView->Window(); | |
772 | ||
773 | #if defined(_WIN32) | |
774 | const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow); | |
775 | Aspect_Handle TheWindowIdOfView = theWindow->HWindow (); | |
776 | #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX) | |
777 | const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow); | |
778 | #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE | |
779 | UIView* TheWindowIdOfView = theWindow->HView(); | |
780 | #else | |
781 | NSView* TheWindowIdOfView = theWindow->HView(); | |
782 | #endif | |
d8d01f6e | 783 | #elif defined(__ANDROID__) || defined(__QNX__) |
c357e426 | 784 | int TheWindowIdOfView = 0; |
785 | #else | |
786 | const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow); | |
787 | int TheWindowIdOfView = int (theWindow->XWindow ()); | |
788 | #endif // WNT | |
789 | // Comparaison on window IDs | |
790 | if (TheWindowIdOfView == TheSpecifiedWindowId) | |
791 | { | |
792 | isExist = Standard_True; | |
793 | theView = aView; | |
794 | } | |
795 | } | |
27f85086 | 796 | } |
797 | ||
c357e426 | 798 | return isExist; |
27f85086 | 799 | } |