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