1 // Created on: 2011-10-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
20 #include <OpenGl_GraphicDriver.hxx>
21 #include <OpenGl_Context.hxx>
22 #include <OpenGl_Flipper.hxx>
23 #include <OpenGl_GraduatedTrihedron.hxx>
24 #include <OpenGl_Group.hxx>
25 #include <OpenGl_View.hxx>
26 #include <OpenGl_StencilTest.hxx>
27 #include <OpenGl_Text.hxx>
28 #include <OpenGl_Workspace.hxx>
30 #include <Aspect_GraphicDeviceDefinitionError.hxx>
31 #include <Aspect_IdentDefinitionError.hxx>
32 #include <Graphic3d_StructureManager.hxx>
33 #include <Message_Messenger.hxx>
34 #include <OSD_Environment.hxx>
35 #include <Standard_NotImplemented.hxx>
37 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
40 #include <WNT_Window.hxx>
41 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
42 #include <Cocoa_Window.hxx>
44 #include <Xw_Window.hxx>
47 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && !defined(__EMSCRIPTEN__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
48 #include <X11/Xlib.h> // XOpenDisplay()
51 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
53 #ifndef EGL_OPENGL_ES3_BIT
54 #define EGL_OPENGL_ES3_BIT 0x00000040
60 static const Handle(OpenGl_Context) TheNullGlCtx;
62 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
63 //! Wrapper over eglChooseConfig() called with preferred defaults.
64 static EGLConfig chooseEglSurfConfig (EGLDisplay theDisplay)
66 EGLint aConfigAttribs[] =
74 #if defined(GL_ES_VERSION_2_0)
75 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
77 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
82 EGLConfig aCfg = NULL;
83 EGLint aNbConfigs = 0;
84 for (Standard_Integer aGlesVer = 3; aGlesVer >= 2; --aGlesVer)
86 #if defined(GL_ES_VERSION_2_0)
87 aConfigAttribs[6 * 2 + 1] = aGlesVer == 3 ? EGL_OPENGL_ES3_BIT : EGL_OPENGL_ES2_BIT;
95 if (eglChooseConfig (theDisplay, aConfigAttribs, &aCfg, 1, &aNbConfigs) == EGL_TRUE
102 aConfigAttribs[4 * 2 + 1] = 16; // try config with smaller depth buffer
103 if (eglChooseConfig (theDisplay, aConfigAttribs, &aCfg, 1, &aNbConfigs) == EGL_TRUE
115 // =======================================================================
116 // function : OpenGl_GraphicDriver
118 // =======================================================================
119 OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp,
120 const Standard_Boolean theToInitialize)
121 : Graphic3d_GraphicDriver (theDisp),
122 myIsOwnContext (Standard_False),
123 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
124 myEglDisplay ((Aspect_Display )EGL_NO_DISPLAY),
125 myEglContext ((Aspect_RenderingContext )EGL_NO_CONTEXT),
128 myCaps (new OpenGl_Caps()),
129 myMapOfView (1, NCollection_BaseAllocator::CommonBaseAllocator()),
130 myMapOfStructure (1, NCollection_BaseAllocator::CommonBaseAllocator())
132 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && !defined(__EMSCRIPTEN__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
133 if (myDisplayConnection.IsNull())
135 //throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: cannot connect to X server!");
139 Display* aDisplay = myDisplayConnection->GetDisplay();
140 Bool toSync = ::getenv ("CSF_GraphicSync") != NULL
141 || ::getenv ("CALL_SYNCHRO_X") != NULL;
142 XSynchronize (aDisplay, toSync);
144 #if !defined(HAVE_EGL) && !defined(HAVE_GLES2)
145 // does the server know about OpenGL & GLX?
147 if (!XQueryExtension (aDisplay, "GLX", &aDummy, &aDummy, &aDummy))
149 ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, this system doesn't appear to support OpenGL!", Message_Warning);
156 throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: default context can not be initialized!");
160 // =======================================================================
161 // function : ~OpenGl_GraphicDriver
163 // =======================================================================
164 OpenGl_GraphicDriver::~OpenGl_GraphicDriver()
169 // =======================================================================
170 // function : ReleaseContext
172 // =======================================================================
173 void OpenGl_GraphicDriver::ReleaseContext()
175 Handle(OpenGl_Context) aCtxShared;
176 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
177 aViewIter.More(); aViewIter.Next())
179 const Handle(OpenGl_View)& aView = aViewIter.Value();
180 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
181 if (aWindow.IsNull())
186 const Handle(OpenGl_Context)& aCtx = aWindow->GetGlContext();
187 if (aCtx->MakeCurrent()
188 && aCtxShared.IsNull())
194 if (!aCtxShared.IsNull())
196 aCtxShared->MakeCurrent();
198 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
199 aViewIter.More(); aViewIter.Next())
201 const Handle(OpenGl_View)& aView = aViewIter.Value();
202 aView->ReleaseGlResources (aCtxShared);
205 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
206 aStructIt.More (); aStructIt.Next())
208 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
209 aStruct->ReleaseGlResources (aCtxShared);
212 const bool isDeviceLost = !myMapOfStructure.IsEmpty();
213 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
214 aViewIter.More(); aViewIter.Next())
216 const Handle(OpenGl_View)& aView = aViewIter.Value();
219 aView->StructureManager()->SetDeviceLost();
222 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
223 if (aWindow.IsNull())
228 aWindow->GetGlContext()->forcedRelease();
231 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
234 if (myEglContext != (Aspect_RenderingContext )EGL_NO_CONTEXT)
236 if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) != EGL_TRUE)
238 ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, FAILED to release OpenGL context!", Message_Warning);
240 eglDestroyContext ((EGLDisplay )myEglDisplay, (EGLContext )myEglContext);
243 if (myEglDisplay != (Aspect_Display )EGL_NO_DISPLAY)
245 if (eglTerminate ((EGLDisplay )myEglDisplay) != EGL_TRUE)
247 ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, EGL, eglTerminate FAILED!", Message_Warning);
252 myEglDisplay = (Aspect_Display )EGL_NO_DISPLAY;
253 myEglContext = (Aspect_RenderingContext )EGL_NO_CONTEXT;
256 myIsOwnContext = Standard_False;
259 // =======================================================================
260 // function : InitContext
262 // =======================================================================
263 Standard_Boolean OpenGl_GraphicDriver::InitContext()
266 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
268 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && !defined(__EMSCRIPTEN__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
269 if (myDisplayConnection.IsNull())
271 return Standard_False;
273 Display* aDisplay = myDisplayConnection->GetDisplay();
274 myEglDisplay = (Aspect_Display )eglGetDisplay (aDisplay);
276 myEglDisplay = (Aspect_Display )eglGetDisplay (EGL_DEFAULT_DISPLAY);
278 if ((EGLDisplay )myEglDisplay == EGL_NO_DISPLAY)
280 ::Message::DefaultMessenger()->Send ("Error: no EGL display!", Message_Fail);
281 return Standard_False;
284 EGLint aVerMajor = 0; EGLint aVerMinor = 0;
285 if (eglInitialize ((EGLDisplay )myEglDisplay, &aVerMajor, &aVerMinor) != EGL_TRUE)
287 ::Message::DefaultMessenger()->Send ("Error: EGL display is unavailable!", Message_Fail);
288 return Standard_False;
291 myEglConfig = chooseEglSurfConfig ((EGLDisplay )myEglDisplay);
292 if (myEglConfig == NULL)
294 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide compatible configurations!", Message_Fail);
295 return Standard_False;
298 #if defined(GL_ES_VERSION_2_0)
299 EGLint anEglCtxAttribs3[] = { EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE, EGL_NONE };
300 EGLint anEglCtxAttribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };
301 if (eglBindAPI (EGL_OPENGL_ES_API) != EGL_TRUE)
303 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide OpenGL ES client!", Message_Fail);
304 return Standard_False;
306 if (myCaps->contextMajorVersionUpper != 2)
308 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs3);
310 if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
312 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs2);
315 EGLint* anEglCtxAttribs = NULL;
316 if (eglBindAPI (EGL_OPENGL_API) != EGL_TRUE)
318 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide OpenGL client!", Message_Fail);
319 return Standard_False;
321 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs);
324 if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
326 ::Message::DefaultMessenger()->Send ("Error: EGL is unable to create OpenGL context!", Message_Fail);
327 return Standard_False;
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)
332 // ::Message::DefaultMessenger()->Send ("Error: EGL is unable bind OpenGL context!", Message_Fail);
333 // return Standard_False;
336 myIsOwnContext = Standard_True;
337 return Standard_True;
340 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
341 // =======================================================================
342 // function : InitEglContext
344 // =======================================================================
345 Standard_Boolean OpenGl_GraphicDriver::InitEglContext (Aspect_Display theEglDisplay,
346 Aspect_RenderingContext theEglContext,
350 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && !defined(__EMSCRIPTEN__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
351 if (myDisplayConnection.IsNull())
353 return Standard_False;
357 if ((EGLDisplay )theEglDisplay == EGL_NO_DISPLAY
358 || (EGLContext )theEglContext == EGL_NO_CONTEXT)
360 return Standard_False;
362 myEglDisplay = theEglDisplay;
363 myEglContext = theEglContext;
364 myEglConfig = theEglConfig;
365 if (theEglConfig == NULL)
367 myEglConfig = chooseEglSurfConfig ((EGLDisplay )myEglDisplay);
368 if (myEglConfig == NULL)
370 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide compatible configurations!", Message_Fail);
371 return Standard_False;
374 return Standard_True;
378 // =======================================================================
379 // function : InquireLimit
381 // =======================================================================
382 Standard_Integer OpenGl_GraphicDriver::InquireLimit (const Graphic3d_TypeOfLimit theType) const
384 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
387 case Graphic3d_TypeOfLimit_MaxNbLights:
388 return Graphic3d_ShaderProgram::THE_MAX_LIGHTS_DEFAULT;
389 case Graphic3d_TypeOfLimit_MaxNbClipPlanes:
390 return !aCtx.IsNull() ? aCtx->MaxClipPlanes() : 0;
391 case Graphic3d_TypeOfLimit_MaxNbViews:
393 case Graphic3d_TypeOfLimit_MaxTextureSize:
394 return !aCtx.IsNull() ? aCtx->MaxTextureSize() : 1024;
395 case Graphic3d_TypeOfLimit_MaxCombinedTextureUnits:
396 return !aCtx.IsNull() ? aCtx->MaxCombinedTextureUnits() : 1;
397 case Graphic3d_TypeOfLimit_MaxMsaa:
398 return !aCtx.IsNull() ? aCtx->MaxMsaaSamples() : 0;
399 case Graphic3d_TypeOfLimit_MaxViewDumpSizeX:
400 return !aCtx.IsNull() ? aCtx->MaxDumpSizeX() : 1024;
401 case Graphic3d_TypeOfLimit_MaxViewDumpSizeY:
402 return !aCtx.IsNull() ? aCtx->MaxDumpSizeY() : 1024;
403 case Graphic3d_TypeOfLimit_HasRayTracing:
404 return (!aCtx.IsNull() && aCtx->HasRayTracing()) ? 1 : 0;
405 case Graphic3d_TypeOfLimit_HasRayTracingTextures:
406 return (!aCtx.IsNull() && aCtx->HasRayTracingTextures()) ? 1 : 0;
407 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSampling:
408 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSampling()) ? 1 : 0;
409 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSamplingAtomic:
410 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSamplingAtomic()) ? 1 : 0;
411 case Graphic3d_TypeOfLimit_HasSRGB:
412 return (!aCtx.IsNull() && aCtx->HasSRGB()) ? 1 : 0;
413 case Graphic3d_TypeOfLimit_HasBlendedOit:
414 return (!aCtx.IsNull()
415 && aCtx->hasDrawBuffers != OpenGl_FeatureNotAvailable
416 && (aCtx->hasFloatBuffer != OpenGl_FeatureNotAvailable || aCtx->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable)) ? 1 : 0;
417 case Graphic3d_TypeOfLimit_HasBlendedOitMsaa:
418 return (!aCtx.IsNull()
419 && aCtx->hasSampleVariables != OpenGl_FeatureNotAvailable
420 && (InquireLimit (Graphic3d_TypeOfLimit_HasBlendedOit) == 1)) ? 1 : 0;
421 case Graphic3d_TypeOfLimit_HasFlatShading:
422 return !aCtx.IsNull() && aCtx->hasFlatShading != OpenGl_FeatureNotAvailable ? 1 : 0;
423 case Graphic3d_TypeOfLimit_IsWorkaroundFBO:
424 return !aCtx.IsNull() && aCtx->MaxTextureSize() != aCtx->MaxDumpSizeX() ? 1 : 0;
425 case Graphic3d_TypeOfLimit_HasMeshEdges:
426 return !aCtx.IsNull() && aCtx->hasGeometryStage != OpenGl_FeatureNotAvailable ? 1 : 0;
427 case Graphic3d_TypeOfLimit_NB:
433 // =======================================================================
434 // function : DefaultTextHeight
436 // =======================================================================
437 Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const
442 // =======================================================================
443 // function : EnableVBO
445 // =======================================================================
446 void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
448 myCaps->vboDisable = !theToTurnOn;
451 // =======================================================================
452 // function : GetSharedContext
454 // =======================================================================
455 const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext (bool theBound) const
457 if (myMapOfView.IsEmpty())
462 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); aViewIter.More(); aViewIter.Next())
464 if (const Handle(OpenGl_Window)& aWindow = aViewIter.Value()->GlWindow())
468 return aWindow->GetGlContext();
470 else if (aWindow->GetGlContext()->IsCurrent())
472 return aWindow->GetGlContext();
480 // =======================================================================
481 // function : MemoryInfo
483 // =======================================================================
484 Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes,
485 TCollection_AsciiString& theInfo) const
487 // this is extra work (for OpenGl_Context initialization)...
488 OpenGl_Context aGlCtx;
491 return Standard_False;
493 theFreeBytes = aGlCtx.AvailableMemory();
494 theInfo = aGlCtx.MemoryInfo();
495 return !theInfo.IsEmpty();
498 // =======================================================================
499 // function : SetBuffersNoSwap
501 // =======================================================================
502 void OpenGl_GraphicDriver::SetBuffersNoSwap (const Standard_Boolean theIsNoSwap)
504 myCaps->buffersNoSwap = theIsNoSwap;
507 // =======================================================================
508 // function : TextSize
510 // =======================================================================
511 void OpenGl_GraphicDriver::TextSize (const Handle(Graphic3d_CView)& theView,
512 const Standard_CString theText,
513 const Standard_ShortReal theHeight,
514 Standard_ShortReal& theWidth,
515 Standard_ShortReal& theAscent,
516 Standard_ShortReal& theDescent) const
518 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
524 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
525 OpenGl_Aspects aTextAspect;
526 TCollection_ExtendedString anExtText = theText;
527 NCollection_String aText (anExtText.ToExtString());
528 OpenGl_Text::StringSize(aCtx, aText, aTextAspect, aHeight, theView->RenderingParams().Resolution, theWidth, theAscent, theDescent);
531 //=======================================================================
532 //function : InsertLayerBefore
534 //=======================================================================
535 void OpenGl_GraphicDriver::InsertLayerBefore (const Graphic3d_ZLayerId theLayerId,
536 const Graphic3d_ZLayerSettings& theSettings,
537 const Graphic3d_ZLayerId theLayerAfter)
539 base_type::InsertLayerBefore (theLayerId, theSettings, theLayerAfter);
541 // Add layer to all views
542 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
544 aViewIt.Value()->InsertLayerBefore (theLayerId, theSettings, theLayerAfter);
548 //=======================================================================
549 //function : InsertLayerAfter
551 //=======================================================================
552 void OpenGl_GraphicDriver::InsertLayerAfter (const Graphic3d_ZLayerId theNewLayerId,
553 const Graphic3d_ZLayerSettings& theSettings,
554 const Graphic3d_ZLayerId theLayerBefore)
556 base_type::InsertLayerAfter (theNewLayerId, theSettings, theLayerBefore);
558 // Add layer to all views
559 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
561 aViewIt.Value()->InsertLayerAfter (theNewLayerId, theSettings, theLayerBefore);
565 //=======================================================================
566 //function : RemoveZLayer
568 //=======================================================================
569 void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
571 base_type::RemoveZLayer (theLayerId);
573 // Remove layer from all of the views
574 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
576 aViewIt.Value()->RemoveZLayer (theLayerId);
579 // Unset Z layer for all of the structures.
580 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure); aStructIt.More(); aStructIt.Next())
582 OpenGl_Structure* aStruct = aStructIt.ChangeValue ();
583 if (aStruct->ZLayer() == theLayerId)
584 aStruct->SetZLayer (Graphic3d_ZLayerId_Default);
588 //=======================================================================
589 //function : SetZLayerSettings
591 //=======================================================================
592 void OpenGl_GraphicDriver::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
593 const Graphic3d_ZLayerSettings& theSettings)
595 base_type::SetZLayerSettings (theLayerId, theSettings);
597 // Change Z layer settings in all managed views
598 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
600 aViewIt.Value()->SetZLayerSettings (theLayerId, theSettings);
604 // =======================================================================
605 // function : Structure
607 // =======================================================================
608 Handle(Graphic3d_CStructure) OpenGl_GraphicDriver::CreateStructure (const Handle(Graphic3d_StructureManager)& theManager)
610 Handle(OpenGl_Structure) aStructure = new OpenGl_Structure (theManager);
611 myMapOfStructure.Bind (aStructure->Id, aStructure.operator->());
615 // =======================================================================
616 // function : Structure
618 // =======================================================================
619 void OpenGl_GraphicDriver::RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure)
621 OpenGl_Structure* aStructure = NULL;
622 if (!myMapOfStructure.Find (theCStructure->Id, aStructure))
627 myMapOfStructure.UnBind (theCStructure->Id);
628 aStructure->Release (GetSharedContext());
629 theCStructure.Nullify();
632 // =======================================================================
633 // function : CreateView
635 // =======================================================================
636 Handle(Graphic3d_CView) OpenGl_GraphicDriver::CreateView (const Handle(Graphic3d_StructureManager)& theMgr)
638 Handle(OpenGl_View) aView = new OpenGl_View (theMgr, this, myCaps, &myStateCounter);
639 myMapOfView.Add (aView);
640 for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (myLayers); aLayerIter.More(); aLayerIter.Next())
642 const Handle(Graphic3d_Layer)& aLayer = aLayerIter.Value();
643 aView->InsertLayerAfter (aLayer->LayerId(), aLayer->LayerSettings(), Graphic3d_ZLayerId_UNKNOWN);
648 // =======================================================================
649 // function : RemoveView
651 // =======================================================================
652 void OpenGl_GraphicDriver::RemoveView (const Handle(Graphic3d_CView)& theView)
654 Handle(OpenGl_Context) aCtx = GetSharedContext();
655 Handle(OpenGl_View) aView = Handle(OpenGl_View)::DownCast (theView);
661 if (!myMapOfView.Remove (aView))
666 Handle(OpenGl_Window) aWindow = aView->GlWindow();
667 if (!aWindow.IsNull()
668 && aWindow->GetGlContext()->MakeCurrent())
670 aCtx = aWindow->GetGlContext();
674 // try to hijack another context if any
675 const Handle(OpenGl_Context)& anOtherCtx = GetSharedContext();
676 if (!anOtherCtx.IsNull()
677 && anOtherCtx != aWindow->GetGlContext())
684 aView->ReleaseGlResources (aCtx);
685 if (myMapOfView.IsEmpty())
687 // The last view removed but some objects still present.
688 // Release GL resources now without object destruction.
689 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
690 aStructIt.More (); aStructIt.Next())
692 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
693 aStruct->ReleaseGlResources (aCtx);
696 if (!myMapOfStructure.IsEmpty())
698 aView->StructureManager()->SetDeviceLost();
703 // =======================================================================
706 // =======================================================================
707 Handle(OpenGl_Window) OpenGl_GraphicDriver::CreateRenderWindow (const Handle(Aspect_Window)& theWindow,
708 const Aspect_RenderingContext theContext)
710 Handle(OpenGl_Context) aShareCtx = GetSharedContext();
711 Handle(OpenGl_Window) aWindow = new OpenGl_Window (this, theWindow, theContext, myCaps, aShareCtx);
715 //=======================================================================
716 //function : ViewExists
718 //=======================================================================
719 Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)& AWindow, Handle(Graphic3d_CView)& theView)
721 Standard_Boolean isExist = Standard_False;
723 // Parse the list of views to find
724 // a view with the specified window
726 #if defined(_WIN32) && !defined(OCCT_UWP)
727 const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
728 Aspect_Handle TheSpecifiedWindowId = THEWindow->HWindow ();
729 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
730 const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow);
731 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
732 UIView* TheSpecifiedWindowId = THEWindow->HView();
734 NSView* TheSpecifiedWindowId = THEWindow->HView();
736 #elif defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__) || defined(OCCT_UWP)
738 int TheSpecifiedWindowId = -1;
740 const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow);
741 int TheSpecifiedWindowId = int (THEWindow->XWindow ());
744 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
745 for(; aViewIt.More(); aViewIt.Next())
747 const Handle(OpenGl_View)& aView = aViewIt.Value();
748 if (aView->IsDefined() && aView->IsActive())
750 const Handle(Aspect_Window) AspectWindow = aView->Window();
752 #if defined(_WIN32) && !defined(OCCT_UWP)
753 const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
754 Aspect_Handle TheWindowIdOfView = theWindow->HWindow ();
755 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
756 const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow);
757 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
758 UIView* TheWindowIdOfView = theWindow->HView();
760 NSView* TheWindowIdOfView = theWindow->HView();
762 #elif defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__) || defined(OCCT_UWP)
763 int TheWindowIdOfView = 0;
765 const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
766 int TheWindowIdOfView = int (theWindow->XWindow ());
768 // Comparaison on window IDs
769 if (TheWindowIdOfView == TheSpecifiedWindowId)
771 isExist = Standard_True;
780 //=======================================================================
781 //function : setDeviceLost
783 //=======================================================================
784 void OpenGl_GraphicDriver::setDeviceLost()
786 if (myMapOfStructure.IsEmpty())
791 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); aViewIter.More(); aViewIter.Next())
793 const Handle(OpenGl_View)& aView = aViewIter.Value();
794 if (aView->myWasRedrawnGL)
796 aView->StructureManager()->SetDeviceLost();