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_HasPBR:
404 return (!aCtx.IsNull() && aCtx->HasPBR()) ? 1 : 0;
405 case Graphic3d_TypeOfLimit_HasRayTracing:
406 return (!aCtx.IsNull() && aCtx->HasRayTracing()) ? 1 : 0;
407 case Graphic3d_TypeOfLimit_HasRayTracingTextures:
408 return (!aCtx.IsNull() && aCtx->HasRayTracingTextures()) ? 1 : 0;
409 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSampling:
410 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSampling()) ? 1 : 0;
411 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSamplingAtomic:
412 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSamplingAtomic()) ? 1 : 0;
413 case Graphic3d_TypeOfLimit_HasSRGB:
414 return (!aCtx.IsNull() && aCtx->HasSRGB()) ? 1 : 0;
415 case Graphic3d_TypeOfLimit_HasBlendedOit:
416 return (!aCtx.IsNull()
417 && aCtx->hasDrawBuffers != OpenGl_FeatureNotAvailable
418 && (aCtx->hasFloatBuffer != OpenGl_FeatureNotAvailable || aCtx->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable)) ? 1 : 0;
419 case Graphic3d_TypeOfLimit_HasBlendedOitMsaa:
420 return (!aCtx.IsNull()
421 && aCtx->hasSampleVariables != OpenGl_FeatureNotAvailable
422 && (InquireLimit (Graphic3d_TypeOfLimit_HasBlendedOit) == 1)) ? 1 : 0;
423 case Graphic3d_TypeOfLimit_HasFlatShading:
424 return !aCtx.IsNull() && aCtx->hasFlatShading != OpenGl_FeatureNotAvailable ? 1 : 0;
425 case Graphic3d_TypeOfLimit_IsWorkaroundFBO:
426 return !aCtx.IsNull() && aCtx->MaxTextureSize() != aCtx->MaxDumpSizeX() ? 1 : 0;
427 case Graphic3d_TypeOfLimit_HasMeshEdges:
428 return !aCtx.IsNull() && aCtx->hasGeometryStage != OpenGl_FeatureNotAvailable ? 1 : 0;
429 case Graphic3d_TypeOfLimit_NB:
435 // =======================================================================
436 // function : DefaultTextHeight
438 // =======================================================================
439 Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const
444 // =======================================================================
445 // function : EnableVBO
447 // =======================================================================
448 void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
450 myCaps->vboDisable = !theToTurnOn;
453 // =======================================================================
454 // function : GetSharedContext
456 // =======================================================================
457 const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext (bool theBound) const
459 if (myMapOfView.IsEmpty())
464 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); aViewIter.More(); aViewIter.Next())
466 if (const Handle(OpenGl_Window)& aWindow = aViewIter.Value()->GlWindow())
470 return aWindow->GetGlContext();
472 else if (aWindow->GetGlContext()->IsCurrent())
474 return aWindow->GetGlContext();
482 // =======================================================================
483 // function : MemoryInfo
485 // =======================================================================
486 Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes,
487 TCollection_AsciiString& theInfo) const
489 // this is extra work (for OpenGl_Context initialization)...
490 OpenGl_Context aGlCtx;
493 return Standard_False;
495 theFreeBytes = aGlCtx.AvailableMemory();
496 theInfo = aGlCtx.MemoryInfo();
497 return !theInfo.IsEmpty();
500 // =======================================================================
501 // function : SetBuffersNoSwap
503 // =======================================================================
504 void OpenGl_GraphicDriver::SetBuffersNoSwap (const Standard_Boolean theIsNoSwap)
506 myCaps->buffersNoSwap = theIsNoSwap;
509 // =======================================================================
510 // function : TextSize
512 // =======================================================================
513 void OpenGl_GraphicDriver::TextSize (const Handle(Graphic3d_CView)& theView,
514 const Standard_CString theText,
515 const Standard_ShortReal theHeight,
516 Standard_ShortReal& theWidth,
517 Standard_ShortReal& theAscent,
518 Standard_ShortReal& theDescent) const
520 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
526 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
527 OpenGl_Aspects aTextAspect;
528 TCollection_ExtendedString anExtText = theText;
529 NCollection_String aText (anExtText.ToExtString());
530 OpenGl_Text::StringSize(aCtx, aText, aTextAspect, aHeight, theView->RenderingParams().Resolution, theWidth, theAscent, theDescent);
533 //=======================================================================
534 //function : InsertLayerBefore
536 //=======================================================================
537 void OpenGl_GraphicDriver::InsertLayerBefore (const Graphic3d_ZLayerId theLayerId,
538 const Graphic3d_ZLayerSettings& theSettings,
539 const Graphic3d_ZLayerId theLayerAfter)
541 base_type::InsertLayerBefore (theLayerId, theSettings, theLayerAfter);
543 // Add layer to all views
544 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
546 aViewIt.Value()->InsertLayerBefore (theLayerId, theSettings, theLayerAfter);
550 //=======================================================================
551 //function : InsertLayerAfter
553 //=======================================================================
554 void OpenGl_GraphicDriver::InsertLayerAfter (const Graphic3d_ZLayerId theNewLayerId,
555 const Graphic3d_ZLayerSettings& theSettings,
556 const Graphic3d_ZLayerId theLayerBefore)
558 base_type::InsertLayerAfter (theNewLayerId, theSettings, theLayerBefore);
560 // Add layer to all views
561 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
563 aViewIt.Value()->InsertLayerAfter (theNewLayerId, theSettings, theLayerBefore);
567 //=======================================================================
568 //function : RemoveZLayer
570 //=======================================================================
571 void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
573 base_type::RemoveZLayer (theLayerId);
575 // Remove layer from all of the views
576 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
578 aViewIt.Value()->RemoveZLayer (theLayerId);
581 // Unset Z layer for all of the structures.
582 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure); aStructIt.More(); aStructIt.Next())
584 OpenGl_Structure* aStruct = aStructIt.ChangeValue ();
585 if (aStruct->ZLayer() == theLayerId)
586 aStruct->SetZLayer (Graphic3d_ZLayerId_Default);
590 //=======================================================================
591 //function : SetZLayerSettings
593 //=======================================================================
594 void OpenGl_GraphicDriver::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
595 const Graphic3d_ZLayerSettings& theSettings)
597 base_type::SetZLayerSettings (theLayerId, theSettings);
599 // Change Z layer settings in all managed views
600 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
602 aViewIt.Value()->SetZLayerSettings (theLayerId, theSettings);
606 // =======================================================================
607 // function : Structure
609 // =======================================================================
610 Handle(Graphic3d_CStructure) OpenGl_GraphicDriver::CreateStructure (const Handle(Graphic3d_StructureManager)& theManager)
612 Handle(OpenGl_Structure) aStructure = new OpenGl_Structure (theManager);
613 myMapOfStructure.Bind (aStructure->Id, aStructure.operator->());
617 // =======================================================================
618 // function : Structure
620 // =======================================================================
621 void OpenGl_GraphicDriver::RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure)
623 OpenGl_Structure* aStructure = NULL;
624 if (!myMapOfStructure.Find (theCStructure->Id, aStructure))
629 myMapOfStructure.UnBind (theCStructure->Id);
630 aStructure->Release (GetSharedContext());
631 theCStructure.Nullify();
634 // =======================================================================
635 // function : CreateView
637 // =======================================================================
638 Handle(Graphic3d_CView) OpenGl_GraphicDriver::CreateView (const Handle(Graphic3d_StructureManager)& theMgr)
640 Handle(OpenGl_View) aView = new OpenGl_View (theMgr, this, myCaps, &myStateCounter);
641 myMapOfView.Add (aView);
642 for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (myLayers); aLayerIter.More(); aLayerIter.Next())
644 const Handle(Graphic3d_Layer)& aLayer = aLayerIter.Value();
645 aView->InsertLayerAfter (aLayer->LayerId(), aLayer->LayerSettings(), Graphic3d_ZLayerId_UNKNOWN);
650 // =======================================================================
651 // function : RemoveView
653 // =======================================================================
654 void OpenGl_GraphicDriver::RemoveView (const Handle(Graphic3d_CView)& theView)
656 Handle(OpenGl_Context) aCtx = GetSharedContext();
657 Handle(OpenGl_View) aView = Handle(OpenGl_View)::DownCast (theView);
663 if (!myMapOfView.Remove (aView))
668 Handle(OpenGl_Window) aWindow = aView->GlWindow();
669 if (!aWindow.IsNull()
670 && aWindow->GetGlContext()->MakeCurrent())
672 aCtx = aWindow->GetGlContext();
676 // try to hijack another context if any
677 const Handle(OpenGl_Context)& anOtherCtx = GetSharedContext();
678 if (!anOtherCtx.IsNull()
679 && anOtherCtx != aWindow->GetGlContext())
686 aView->ReleaseGlResources (aCtx);
687 if (myMapOfView.IsEmpty())
689 // The last view removed but some objects still present.
690 // Release GL resources now without object destruction.
691 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
692 aStructIt.More (); aStructIt.Next())
694 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
695 aStruct->ReleaseGlResources (aCtx);
698 if (!myMapOfStructure.IsEmpty())
700 aView->StructureManager()->SetDeviceLost();
705 // =======================================================================
708 // =======================================================================
709 Handle(OpenGl_Window) OpenGl_GraphicDriver::CreateRenderWindow (const Handle(Aspect_Window)& theWindow,
710 const Aspect_RenderingContext theContext)
712 Handle(OpenGl_Context) aShareCtx = GetSharedContext();
713 Handle(OpenGl_Window) aWindow = new OpenGl_Window (this, theWindow, theContext, myCaps, aShareCtx);
717 //=======================================================================
718 //function : ViewExists
720 //=======================================================================
721 Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)& AWindow, Handle(Graphic3d_CView)& theView)
723 Standard_Boolean isExist = Standard_False;
725 // Parse the list of views to find
726 // a view with the specified window
728 #if defined(_WIN32) && !defined(OCCT_UWP)
729 const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
730 Aspect_Handle TheSpecifiedWindowId = THEWindow->HWindow ();
731 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
732 const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow);
733 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
734 UIView* TheSpecifiedWindowId = THEWindow->HView();
736 NSView* TheSpecifiedWindowId = THEWindow->HView();
738 #elif defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__) || defined(OCCT_UWP)
740 int TheSpecifiedWindowId = -1;
742 const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow);
743 int TheSpecifiedWindowId = int (THEWindow->XWindow ());
746 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
747 for(; aViewIt.More(); aViewIt.Next())
749 const Handle(OpenGl_View)& aView = aViewIt.Value();
750 if (aView->IsDefined() && aView->IsActive())
752 const Handle(Aspect_Window) AspectWindow = aView->Window();
754 #if defined(_WIN32) && !defined(OCCT_UWP)
755 const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
756 Aspect_Handle TheWindowIdOfView = theWindow->HWindow ();
757 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
758 const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow);
759 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
760 UIView* TheWindowIdOfView = theWindow->HView();
762 NSView* TheWindowIdOfView = theWindow->HView();
764 #elif defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__) || defined(OCCT_UWP)
765 int TheWindowIdOfView = 0;
767 const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
768 int TheWindowIdOfView = int (theWindow->XWindow ());
770 // Comparaison on window IDs
771 if (TheWindowIdOfView == TheSpecifiedWindowId)
773 isExist = Standard_True;
782 //=======================================================================
783 //function : setDeviceLost
785 //=======================================================================
786 void OpenGl_GraphicDriver::setDeviceLost()
788 if (myMapOfStructure.IsEmpty())
793 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); aViewIter.More(); aViewIter.Next())
795 const Handle(OpenGl_View)& aView = aViewIter.Value();
796 if (aView->myWasRedrawnGL)
798 aView->StructureManager()->SetDeviceLost();