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(__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__)
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__)
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__)
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(__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__)
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__)
268 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!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 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs3);
307 if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
309 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs2);
312 EGLint* anEglCtxAttribs = NULL;
313 if (eglBindAPI (EGL_OPENGL_API) != EGL_TRUE)
315 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide OpenGL client!", Message_Fail);
316 return Standard_False;
318 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs);
321 if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
323 ::Message::DefaultMessenger()->Send ("Error: EGL is unable to create OpenGL context!", Message_Fail);
324 return Standard_False;
326 // eglMakeCurrent() fails or even crash with EGL_NO_SURFACE on some implementations
327 //if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, (EGLContext )myEglContext) != EGL_TRUE)
329 // ::Message::DefaultMessenger()->Send ("Error: EGL is unable bind OpenGL context!", Message_Fail);
330 // return Standard_False;
333 myIsOwnContext = Standard_True;
334 return Standard_True;
337 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
338 // =======================================================================
339 // function : InitEglContext
341 // =======================================================================
342 Standard_Boolean OpenGl_GraphicDriver::InitEglContext (Aspect_Display theEglDisplay,
343 Aspect_RenderingContext theEglContext,
347 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
348 if (myDisplayConnection.IsNull())
350 return Standard_False;
354 if ((EGLDisplay )theEglDisplay == EGL_NO_DISPLAY
355 || (EGLContext )theEglContext == EGL_NO_CONTEXT)
357 return Standard_False;
359 myEglDisplay = theEglDisplay;
360 myEglContext = theEglContext;
361 myEglConfig = theEglConfig;
362 if (theEglConfig == NULL)
364 myEglConfig = chooseEglSurfConfig ((EGLDisplay )myEglDisplay);
365 if (myEglConfig == NULL)
367 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide compatible configurations!", Message_Fail);
368 return Standard_False;
371 return Standard_True;
375 // =======================================================================
376 // function : InquireLimit
378 // =======================================================================
379 Standard_Integer OpenGl_GraphicDriver::InquireLimit (const Graphic3d_TypeOfLimit theType) const
381 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
384 case Graphic3d_TypeOfLimit_MaxNbLights:
385 return Graphic3d_ShaderProgram::THE_MAX_LIGHTS_DEFAULT;
386 case Graphic3d_TypeOfLimit_MaxNbClipPlanes:
387 return !aCtx.IsNull() ? aCtx->MaxClipPlanes() : 0;
388 case Graphic3d_TypeOfLimit_MaxNbViews:
390 case Graphic3d_TypeOfLimit_MaxTextureSize:
391 return !aCtx.IsNull() ? aCtx->MaxTextureSize() : 1024;
392 case Graphic3d_TypeOfLimit_MaxCombinedTextureUnits:
393 return !aCtx.IsNull() ? aCtx->MaxCombinedTextureUnits() : 1;
394 case Graphic3d_TypeOfLimit_MaxMsaa:
395 return !aCtx.IsNull() ? aCtx->MaxMsaaSamples() : 0;
396 case Graphic3d_TypeOfLimit_MaxViewDumpSizeX:
397 return !aCtx.IsNull() ? aCtx->MaxDumpSizeX() : 1024;
398 case Graphic3d_TypeOfLimit_MaxViewDumpSizeY:
399 return !aCtx.IsNull() ? aCtx->MaxDumpSizeY() : 1024;
400 case Graphic3d_TypeOfLimit_HasRayTracing:
401 return (!aCtx.IsNull() && aCtx->HasRayTracing()) ? 1 : 0;
402 case Graphic3d_TypeOfLimit_HasRayTracingTextures:
403 return (!aCtx.IsNull() && aCtx->HasRayTracingTextures()) ? 1 : 0;
404 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSampling:
405 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSampling()) ? 1 : 0;
406 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSamplingAtomic:
407 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSamplingAtomic()) ? 1 : 0;
408 case Graphic3d_TypeOfLimit_HasBlendedOit:
409 return (!aCtx.IsNull()
410 && aCtx->hasDrawBuffers != OpenGl_FeatureNotAvailable
411 && (aCtx->hasFloatBuffer != OpenGl_FeatureNotAvailable || aCtx->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable)) ? 1 : 0;
412 case Graphic3d_TypeOfLimit_HasBlendedOitMsaa:
413 return (!aCtx.IsNull()
414 && aCtx->hasSampleVariables != OpenGl_FeatureNotAvailable
415 && (InquireLimit (Graphic3d_TypeOfLimit_HasBlendedOit) == 1)) ? 1 : 0;
416 case Graphic3d_TypeOfLimit_HasFlatShading:
417 return !aCtx.IsNull() && aCtx->hasFlatShading != OpenGl_FeatureNotAvailable ? 1 : 0;
418 case Graphic3d_TypeOfLimit_IsWorkaroundFBO:
419 return !aCtx.IsNull() && aCtx->MaxTextureSize() != aCtx->MaxDumpSizeX() ? 1 : 0;
420 case Graphic3d_TypeOfLimit_HasMeshEdges:
421 return !aCtx.IsNull() && aCtx->hasGeometryStage != OpenGl_FeatureNotAvailable ? 1 : 0;
422 case Graphic3d_TypeOfLimit_NB:
428 // =======================================================================
429 // function : DefaultTextHeight
431 // =======================================================================
432 Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const
437 // =======================================================================
438 // function : EnableVBO
440 // =======================================================================
441 void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
443 myCaps->vboDisable = !theToTurnOn;
446 // =======================================================================
447 // function : GetSharedContext
449 // =======================================================================
450 const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext() const
452 if (myMapOfView.IsEmpty())
457 NCollection_Map<Handle(OpenGl_View)>::Iterator anIter (myMapOfView);
458 for (; anIter.More(); anIter.Next())
460 Handle(OpenGl_Window) aWindow = anIter.Value()->GlWindow();
461 if (aWindow.IsNull())
466 return aWindow->GetGlContext();
472 // =======================================================================
473 // function : MemoryInfo
475 // =======================================================================
476 Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes,
477 TCollection_AsciiString& theInfo) const
479 // this is extra work (for OpenGl_Context initialization)...
480 OpenGl_Context aGlCtx;
483 return Standard_False;
485 theFreeBytes = aGlCtx.AvailableMemory();
486 theInfo = aGlCtx.MemoryInfo();
487 return !theInfo.IsEmpty();
490 // =======================================================================
491 // function : SetBuffersNoSwap
493 // =======================================================================
494 void OpenGl_GraphicDriver::SetBuffersNoSwap (const Standard_Boolean theIsNoSwap)
496 myCaps->buffersNoSwap = theIsNoSwap;
499 // =======================================================================
500 // function : TextSize
502 // =======================================================================
503 void OpenGl_GraphicDriver::TextSize (const Handle(Graphic3d_CView)& theView,
504 const Standard_CString theText,
505 const Standard_ShortReal theHeight,
506 Standard_ShortReal& theWidth,
507 Standard_ShortReal& theAscent,
508 Standard_ShortReal& theDescent) const
510 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
516 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
517 OpenGl_TextParam aTextParam;
518 aTextParam.Height = (int )aHeight;
519 OpenGl_Aspects aTextAspect;
520 TCollection_ExtendedString anExtText = theText;
521 NCollection_String aText (anExtText.ToExtString());
522 OpenGl_Text::StringSize(aCtx, aText, aTextAspect, aTextParam, theView->RenderingParams().Resolution, theWidth, theAscent, theDescent);
525 //=======================================================================
526 //function : AddZLayer
528 //=======================================================================
529 void OpenGl_GraphicDriver::AddZLayer (const Graphic3d_ZLayerId theLayerId)
533 Standard_ASSERT_RAISE (theLayerId > 0,
534 "OpenGl_GraphicDriver::AddZLayer, "
535 "negative and zero IDs are reserved");
538 myLayerIds.Add (theLayerId);
540 // Default z-layer settings
541 myMapOfZLayerSettings.Bind (theLayerId, Graphic3d_ZLayerSettings());
542 addZLayerIndex (theLayerId);
544 // Add layer to all views
545 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
546 for (; aViewIt.More(); aViewIt.Next())
548 aViewIt.Value()->AddZLayer (theLayerId);
552 //=======================================================================
553 //function : RemoveZLayer
555 //=======================================================================
556 void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
558 Standard_ASSERT_RAISE (theLayerId > 0,
559 "OpenGl_GraphicDriver::AddZLayer, "
560 "negative and zero IDs are reserved"
561 "and can not be removed");
563 Standard_ASSERT_RAISE (myLayerIds.Contains (theLayerId),
564 "OpenGl_GraphicDriver::RemoveZLayer, "
565 "Layer with theLayerId does not exist");
567 // Remove layer from all of the views
568 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
569 for (; aViewIt.More(); aViewIt.Next())
571 aViewIt.Value()->RemoveZLayer (theLayerId);
574 // Unset Z layer for all of the structures.
575 NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
576 for( ; aStructIt.More (); aStructIt.Next ())
578 OpenGl_Structure* aStruct = aStructIt.ChangeValue ();
579 if (aStruct->ZLayer() == theLayerId)
580 aStruct->SetZLayer (Graphic3d_ZLayerId_Default);
584 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
586 if (aLayerIt.Value() == theLayerId)
588 myLayerSeq.Remove (aLayerIt);
593 myMapOfZLayerSettings.UnBind (theLayerId);
594 myLayerIds.Remove (theLayerId);
597 //=======================================================================
598 //function : SetZLayerSettings
600 //=======================================================================
601 void OpenGl_GraphicDriver::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
602 const Graphic3d_ZLayerSettings& theSettings)
604 base_type::SetZLayerSettings (theLayerId, theSettings);
606 // Change Z layer settings in all managed views
607 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
609 aViewIt.Value()->SetZLayerSettings (theLayerId, theSettings);
613 // =======================================================================
614 // function : Structure
616 // =======================================================================
617 Handle(Graphic3d_CStructure) OpenGl_GraphicDriver::CreateStructure (const Handle(Graphic3d_StructureManager)& theManager)
619 Handle(OpenGl_Structure) aStructure = new OpenGl_Structure (theManager);
620 myMapOfStructure.Bind (aStructure->Id, aStructure.operator->());
624 // =======================================================================
625 // function : Structure
627 // =======================================================================
628 void OpenGl_GraphicDriver::RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure)
630 OpenGl_Structure* aStructure = NULL;
631 if (!myMapOfStructure.Find (theCStructure->Id, aStructure))
636 myMapOfStructure.UnBind (theCStructure->Id);
637 aStructure->Release (GetSharedContext());
638 theCStructure.Nullify();
641 // =======================================================================
644 // =======================================================================
645 Handle(Graphic3d_CView) OpenGl_GraphicDriver::CreateView (const Handle(Graphic3d_StructureManager)& theMgr)
647 Handle(OpenGl_View) aView = new OpenGl_View (theMgr, this, myCaps, &myStateCounter);
649 myMapOfView.Add (aView);
651 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
653 const Graphic3d_ZLayerId aLayerID = aLayerIt.Value();
654 const Graphic3d_ZLayerSettings& aSettings = myMapOfZLayerSettings.Find (aLayerID);
655 aView->AddZLayer (aLayerID);
656 aView->SetZLayerSettings (aLayerID, aSettings);
662 // =======================================================================
663 // function : RemoveView
665 // =======================================================================
666 void OpenGl_GraphicDriver::RemoveView (const Handle(Graphic3d_CView)& theView)
668 Handle(OpenGl_Context) aCtx = GetSharedContext();
669 Handle(OpenGl_View) aView = Handle(OpenGl_View)::DownCast (theView);
675 if (!myMapOfView.Remove (aView))
680 Handle(OpenGl_Window) aWindow = aView->GlWindow();
681 if (!aWindow.IsNull()
682 && aWindow->GetGlContext()->MakeCurrent())
684 aCtx = aWindow->GetGlContext();
688 // try to hijack another context if any
689 const Handle(OpenGl_Context)& anOtherCtx = GetSharedContext();
690 if (!anOtherCtx.IsNull()
691 && anOtherCtx != aWindow->GetGlContext())
698 aView->ReleaseGlResources (aCtx);
699 if (myMapOfView.IsEmpty())
701 // The last view removed but some objects still present.
702 // Release GL resources now without object destruction.
703 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
704 aStructIt.More (); aStructIt.Next())
706 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
707 aStruct->ReleaseGlResources (aCtx);
710 if (!myMapOfStructure.IsEmpty())
712 aView->StructureManager()->SetDeviceLost();
717 // =======================================================================
720 // =======================================================================
721 Handle(OpenGl_Window) OpenGl_GraphicDriver::CreateRenderWindow (const Handle(Aspect_Window)& theWindow,
722 const Aspect_RenderingContext theContext)
724 Handle(OpenGl_Context) aShareCtx = GetSharedContext();
725 Handle(OpenGl_Window) aWindow = new OpenGl_Window (this, theWindow, theContext, myCaps, aShareCtx);
729 //=======================================================================
730 //function : ViewExists
732 //=======================================================================
733 Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)& AWindow, Handle(Graphic3d_CView)& theView)
735 Standard_Boolean isExist = Standard_False;
737 // Parse the list of views to find
738 // a view with the specified window
740 #if defined(_WIN32) && !defined(OCCT_UWP)
741 const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
742 Aspect_Handle TheSpecifiedWindowId = THEWindow->HWindow ();
743 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
744 const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow);
745 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
746 UIView* TheSpecifiedWindowId = THEWindow->HView();
748 NSView* TheSpecifiedWindowId = THEWindow->HView();
750 #elif defined(__ANDROID__) || defined(__QNX__) || defined(OCCT_UWP)
752 int TheSpecifiedWindowId = -1;
754 const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow);
755 int TheSpecifiedWindowId = int (THEWindow->XWindow ());
758 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
759 for(; aViewIt.More(); aViewIt.Next())
761 const Handle(OpenGl_View)& aView = aViewIt.Value();
762 if (aView->IsDefined() && aView->IsActive())
764 const Handle(Aspect_Window) AspectWindow = aView->Window();
766 #if defined(_WIN32) && !defined(OCCT_UWP)
767 const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
768 Aspect_Handle TheWindowIdOfView = theWindow->HWindow ();
769 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
770 const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow);
771 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
772 UIView* TheWindowIdOfView = theWindow->HView();
774 NSView* TheWindowIdOfView = theWindow->HView();
776 #elif defined(__ANDROID__) || defined(__QNX__) || defined(OCCT_UWP)
777 int TheWindowIdOfView = 0;
779 const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
780 int TheWindowIdOfView = int (theWindow->XWindow ());
782 // Comparaison on window IDs
783 if (TheWindowIdOfView == TheSpecifiedWindowId)
785 isExist = Standard_True;
794 //=======================================================================
795 //function : setDeviceLost
797 //=======================================================================
798 void OpenGl_GraphicDriver::setDeviceLost()
800 if (myMapOfStructure.IsEmpty())
805 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); aViewIter.More(); aViewIter.Next())
807 const Handle(OpenGl_View)& aView = aViewIter.Value();
808 if (aView->myWasRedrawnGL)
810 aView->StructureManager()->SetDeviceLost();