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_Window.hxx>
29 #include <OpenGl_Workspace.hxx>
31 #include <Aspect_GraphicDeviceDefinitionError.hxx>
32 #include <Aspect_IdentDefinitionError.hxx>
33 #include <Graphic3d_StructureManager.hxx>
34 #include <Message_Messenger.hxx>
35 #include <OSD_Environment.hxx>
36 #include <Standard_NotImplemented.hxx>
38 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
41 #include <WNT_Window.hxx>
42 #elif defined(HAVE_XLIB)
43 #include <Xw_Window.hxx>
44 #elif defined(__APPLE__)
45 #include <Cocoa_Window.hxx>
47 #include <Aspect_NeutralWindow.hxx>
50 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && !defined(__EMSCRIPTEN__) && (!defined(__APPLE__) || defined(HAVE_XLIB))
51 #include <X11/Xlib.h> // XOpenDisplay()
55 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
57 #ifndef EGL_OPENGL_ES3_BIT
58 #define EGL_OPENGL_ES3_BIT 0x00000040
64 static const Handle(OpenGl_Context) TheNullGlCtx;
66 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
67 //! Wrapper over eglChooseConfig() called with preferred defaults.
68 static EGLConfig chooseEglSurfConfig (EGLDisplay theDisplay)
70 EGLint aConfigAttribs[] =
78 #if defined(GL_ES_VERSION_2_0)
79 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
81 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
86 EGLConfig aCfg = NULL;
87 EGLint aNbConfigs = 0;
88 for (Standard_Integer aGlesVer = 3; aGlesVer >= 2; --aGlesVer)
90 #if defined(GL_ES_VERSION_2_0)
91 aConfigAttribs[6 * 2 + 1] = aGlesVer == 3 ? EGL_OPENGL_ES3_BIT : EGL_OPENGL_ES2_BIT;
99 if (eglChooseConfig (theDisplay, aConfigAttribs, &aCfg, 1, &aNbConfigs) == EGL_TRUE
106 aConfigAttribs[4 * 2 + 1] = 16; // try config with smaller depth buffer
107 if (eglChooseConfig (theDisplay, aConfigAttribs, &aCfg, 1, &aNbConfigs) == EGL_TRUE
116 #elif defined(HAVE_XLIB)
117 //! Search for RGBA double-buffered visual with stencil buffer.
118 static int TheDoubleBuffVisual[] =
127 //! Search for RGBA double-buffered visual with stencil buffer.
128 static int TheDoubleBuffFBConfig[] =
130 GLX_X_RENDERABLE, True,
131 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
132 GLX_RENDER_TYPE, GLX_RGBA_BIT,
133 GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
136 GLX_DOUBLEBUFFER, True,
143 // =======================================================================
144 // function : OpenGl_GraphicDriver
146 // =======================================================================
147 OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp,
148 const Standard_Boolean theToInitialize)
149 : Graphic3d_GraphicDriver (theDisp),
150 myIsOwnContext (Standard_False),
154 myCaps (new OpenGl_Caps()),
155 myMapOfView (1, NCollection_BaseAllocator::CommonBaseAllocator()),
156 myMapOfStructure (1, NCollection_BaseAllocator::CommonBaseAllocator())
158 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
159 myEglDisplay = (Aspect_Display )EGL_NO_DISPLAY;
160 myEglContext = (Aspect_RenderingContext )EGL_NO_CONTEXT;
163 #if defined(HAVE_XLIB)
164 if (myDisplayConnection.IsNull())
166 //throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: cannot connect to X server!");
170 Display* aDisplay = (Display* )myDisplayConnection->GetDisplayAspect();
171 Bool toSync = ::getenv ("CSF_GraphicSync") != NULL
172 || ::getenv ("CALL_SYNCHRO_X") != NULL;
173 XSynchronize (aDisplay, toSync);
178 throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: default context can not be initialized!");
182 // =======================================================================
183 // function : ~OpenGl_GraphicDriver
185 // =======================================================================
186 OpenGl_GraphicDriver::~OpenGl_GraphicDriver()
191 // =======================================================================
192 // function : ReleaseContext
194 // =======================================================================
195 void OpenGl_GraphicDriver::ReleaseContext()
197 Handle(OpenGl_Context) aCtxShared;
198 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
199 aViewIter.More(); aViewIter.Next())
201 const Handle(OpenGl_View)& aView = aViewIter.Value();
202 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
203 if (aWindow.IsNull())
208 const Handle(OpenGl_Context)& aCtx = aWindow->GetGlContext();
209 if (aCtx->MakeCurrent()
210 && aCtxShared.IsNull())
216 if (!aCtxShared.IsNull())
218 aCtxShared->MakeCurrent();
220 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
221 aViewIter.More(); aViewIter.Next())
223 const Handle(OpenGl_View)& aView = aViewIter.Value();
224 aView->ReleaseGlResources (aCtxShared);
227 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
228 aStructIt.More (); aStructIt.Next())
230 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
231 aStruct->ReleaseGlResources (aCtxShared);
234 const bool isDeviceLost = !myMapOfStructure.IsEmpty();
235 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
236 aViewIter.More(); aViewIter.Next())
238 const Handle(OpenGl_View)& aView = aViewIter.Value();
241 aView->StructureManager()->SetDeviceLost();
244 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
245 if (aWindow.IsNull())
250 aWindow->GetGlContext()->forcedRelease();
253 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
256 if (myEglContext != (Aspect_RenderingContext )EGL_NO_CONTEXT)
258 if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) != EGL_TRUE)
260 ::Message::SendWarning ("OpenGl_GraphicDriver, FAILED to release OpenGL context");
262 eglDestroyContext ((EGLDisplay )myEglDisplay, (EGLContext )myEglContext);
265 if (myEglDisplay != (Aspect_Display )EGL_NO_DISPLAY)
267 if (eglTerminate ((EGLDisplay )myEglDisplay) != EGL_TRUE)
269 ::Message::SendWarning ("OpenGl_GraphicDriver, EGL, eglTerminate FAILED");
274 myEglDisplay = (Aspect_Display )EGL_NO_DISPLAY;
275 myEglContext = (Aspect_RenderingContext )EGL_NO_CONTEXT;
278 myIsOwnContext = Standard_False;
281 // =======================================================================
282 // function : InitContext
284 // =======================================================================
285 Standard_Boolean OpenGl_GraphicDriver::InitContext()
288 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
290 #if defined(HAVE_XLIB)
291 if (myDisplayConnection.IsNull())
293 return Standard_False;
295 Display* aDisplay = (Display* )myDisplayConnection->GetDisplayAspect();
296 myEglDisplay = (Aspect_Display )eglGetDisplay (aDisplay);
298 myEglDisplay = (Aspect_Display )eglGetDisplay (EGL_DEFAULT_DISPLAY);
300 if ((EGLDisplay )myEglDisplay == EGL_NO_DISPLAY)
302 ::Message::SendFail ("Error: no EGL display");
303 return Standard_False;
306 EGLint aVerMajor = 0; EGLint aVerMinor = 0;
307 if (eglInitialize ((EGLDisplay )myEglDisplay, &aVerMajor, &aVerMinor) != EGL_TRUE)
309 ::Message::SendFail ("Error: EGL display is unavailable");
310 return Standard_False;
313 myEglConfig = chooseEglSurfConfig ((EGLDisplay )myEglDisplay);
314 if (myEglConfig == NULL)
316 ::Message::SendFail ("Error: EGL does not provide compatible configurations");
317 return Standard_False;
320 #if defined(GL_ES_VERSION_2_0)
321 EGLint anEglCtxAttribs3[] = { EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE, EGL_NONE };
322 EGLint anEglCtxAttribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };
323 if (eglBindAPI (EGL_OPENGL_ES_API) != EGL_TRUE)
325 ::Message::SendFail ("Error: EGL does not provide OpenGL ES client");
326 return Standard_False;
328 if (myCaps->contextMajorVersionUpper != 2)
330 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs3);
332 if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
334 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs2);
337 EGLint* anEglCtxAttribs = NULL;
338 if (eglBindAPI (EGL_OPENGL_API) != EGL_TRUE)
340 ::Message::SendFail ("Error: EGL does not provide OpenGL client");
341 return Standard_False;
343 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs);
346 if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
348 ::Message::SendFail ("Error: EGL is unable to create OpenGL context");
349 return Standard_False;
351 // eglMakeCurrent() fails or even crash with EGL_NO_SURFACE on some implementations
352 //if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, (EGLContext )myEglContext) != EGL_TRUE)
354 // ::Message::SendFail ("Error: EGL is unable bind OpenGL context");
355 // return Standard_False;
359 myIsOwnContext = Standard_True;
360 return Standard_True;
363 // =======================================================================
364 // function : InitEglContext
366 // =======================================================================
367 Standard_Boolean OpenGl_GraphicDriver::InitEglContext (Aspect_Display theEglDisplay,
368 Aspect_RenderingContext theEglContext,
372 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
373 #if defined(HAVE_XLIB)
374 if (myDisplayConnection.IsNull())
376 return Standard_False;
380 if ((EGLDisplay )theEglDisplay == EGL_NO_DISPLAY
381 || (EGLContext )theEglContext == EGL_NO_CONTEXT)
383 return Standard_False;
385 myEglDisplay = theEglDisplay;
386 myEglContext = theEglContext;
387 myEglConfig = theEglConfig;
388 if (theEglConfig == NULL)
390 myEglConfig = chooseEglSurfConfig ((EGLDisplay )myEglDisplay);
391 if (myEglConfig == NULL)
393 ::Message::SendFail ("Error: EGL does not provide compatible configurations");
394 return Standard_False;
398 return Standard_True;
400 (void )theEglDisplay;
401 (void )theEglContext;
403 throw Standard_NotImplemented ("OpenGl_GraphicDriver::InitEglContext() is not implemented");
407 // =======================================================================
408 // function : chooseVisualInfo
410 // =======================================================================
411 void OpenGl_GraphicDriver::chooseVisualInfo()
413 if (myDisplayConnection.IsNull())
418 #if defined(HAVE_XLIB)
419 Display* aDisp = (Display* )myDisplayConnection->GetDisplayAspect();
421 XVisualInfo* aVisInfo = NULL;
422 Aspect_FBConfig anFBConfig = NULL;
423 #if defined(HAVE_EGL) || defined(HAVE_GLES2)
424 XVisualInfo aVisInfoTmp;
425 memset (&aVisInfoTmp, 0, sizeof(aVisInfoTmp));
426 aVisInfoTmp.screen = DefaultScreen (aDisp);
427 if (myEglDisplay != EGL_NO_DISPLAY
428 && myEglConfig != NULL
429 && eglGetConfigAttrib ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NATIVE_VISUAL_ID, (EGLint* )&aVisInfoTmp.visualid) == EGL_TRUE)
432 aVisInfo = XGetVisualInfo (aDisp, VisualIDMask | VisualScreenMask, &aVisInfoTmp, &aNbVisuals);
435 int aScreen = DefaultScreen(aDisp);
437 if (!XQueryExtension (aDisp, "GLX", &aDummy, &aDummy, &aDummy)
438 || !glXQueryExtension (aDisp, &aDummy, &aDummy))
440 Message::SendFail ("Error: OpenGl_GraphicDriver, GLX extension is unavailable");
443 // FBConfigs were added in GLX version 1.3
444 int aGlxMajor = 0, aGlxMinor = 0;
445 const bool hasFBCfg = glXQueryVersion (aDisp, &aGlxMajor, &aGlxMinor)
446 && ((aGlxMajor == 1 && aGlxMinor >= 3) || (aGlxMajor > 1));
450 GLXFBConfig* aFBCfgList = NULL;
453 aFBCfgList = glXChooseFBConfig (aDisp, aScreen, TheDoubleBuffFBConfig, &aFBCount);
455 if(aFBCfgList != NULL
458 anFBConfig = aFBCfgList[0];
459 aVisInfo = glXGetVisualFromFBConfig (aDisp, anFBConfig);
461 /*int aDepthSize = 0, aStencilSize = 0;
462 glXGetFBConfigAttrib (aDisp, anFBConfig, GLX_DEPTH_SIZE, &aDepthSize);
463 glXGetFBConfigAttrib (aDisp, anFBConfig, GLX_STENCIL_SIZE, &aStencilSize);
464 Message::SendInfo() << "GLX FBConfig:"
465 << "\n DepthSize= " << aDepthSize
466 << "\n StencilSize= " << aStencilSize;*/
471 if (aVisInfo == NULL)
473 aVisInfo = glXChooseVisual (aDisp, aScreen, TheDoubleBuffVisual);
476 if (aVisInfo != NULL)
478 myDisplayConnection->SetDefaultVisualInfo ((Aspect_XVisualInfo* )aVisInfo, anFBConfig);
482 Message::SendWarning ("OpenGl_GraphicDriver, couldn't find compatible Visual (RGBA, double-buffered)");
487 // =======================================================================
488 // function : InquireLimit
490 // =======================================================================
491 Standard_Integer OpenGl_GraphicDriver::InquireLimit (const Graphic3d_TypeOfLimit theType) const
493 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
496 case Graphic3d_TypeOfLimit_MaxNbLights:
497 return Graphic3d_ShaderProgram::THE_MAX_LIGHTS_DEFAULT;
498 case Graphic3d_TypeOfLimit_MaxNbClipPlanes:
499 return !aCtx.IsNull() ? aCtx->MaxClipPlanes() : 0;
500 case Graphic3d_TypeOfLimit_MaxNbViews:
502 case Graphic3d_TypeOfLimit_MaxTextureSize:
503 return !aCtx.IsNull() ? aCtx->MaxTextureSize() : 1024;
504 case Graphic3d_TypeOfLimit_MaxCombinedTextureUnits:
505 return !aCtx.IsNull() ? aCtx->MaxCombinedTextureUnits() : 1;
506 case Graphic3d_TypeOfLimit_MaxMsaa:
507 return !aCtx.IsNull() ? aCtx->MaxMsaaSamples() : 0;
508 case Graphic3d_TypeOfLimit_MaxViewDumpSizeX:
509 return !aCtx.IsNull() ? aCtx->MaxDumpSizeX() : 1024;
510 case Graphic3d_TypeOfLimit_MaxViewDumpSizeY:
511 return !aCtx.IsNull() ? aCtx->MaxDumpSizeY() : 1024;
512 case Graphic3d_TypeOfLimit_HasPBR:
513 return (!aCtx.IsNull() && aCtx->HasPBR()) ? 1 : 0;
514 case Graphic3d_TypeOfLimit_HasRayTracing:
515 return (!aCtx.IsNull() && aCtx->HasRayTracing()) ? 1 : 0;
516 case Graphic3d_TypeOfLimit_HasRayTracingTextures:
517 return (!aCtx.IsNull() && aCtx->HasRayTracingTextures()) ? 1 : 0;
518 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSampling:
519 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSampling()) ? 1 : 0;
520 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSamplingAtomic:
521 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSamplingAtomic()) ? 1 : 0;
522 case Graphic3d_TypeOfLimit_HasSRGB:
523 return (!aCtx.IsNull() && aCtx->HasSRGB()) ? 1 : 0;
524 case Graphic3d_TypeOfLimit_HasBlendedOit:
525 return (!aCtx.IsNull()
526 && aCtx->hasDrawBuffers != OpenGl_FeatureNotAvailable
527 && (aCtx->hasFloatBuffer != OpenGl_FeatureNotAvailable || aCtx->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable)) ? 1 : 0;
528 case Graphic3d_TypeOfLimit_HasBlendedOitMsaa:
529 return (!aCtx.IsNull()
530 && aCtx->hasSampleVariables != OpenGl_FeatureNotAvailable
531 && (InquireLimit (Graphic3d_TypeOfLimit_HasBlendedOit) == 1)) ? 1 : 0;
532 case Graphic3d_TypeOfLimit_HasFlatShading:
533 return !aCtx.IsNull() && aCtx->hasFlatShading != OpenGl_FeatureNotAvailable ? 1 : 0;
534 case Graphic3d_TypeOfLimit_IsWorkaroundFBO:
535 return !aCtx.IsNull() && aCtx->MaxTextureSize() != aCtx->MaxDumpSizeX() ? 1 : 0;
536 case Graphic3d_TypeOfLimit_HasMeshEdges:
537 return !aCtx.IsNull() && aCtx->hasGeometryStage != OpenGl_FeatureNotAvailable ? 1 : 0;
538 case Graphic3d_TypeOfLimit_NB:
544 // =======================================================================
545 // function : DefaultTextHeight
547 // =======================================================================
548 Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const
553 // =======================================================================
554 // function : EnableVBO
556 // =======================================================================
557 void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
559 myCaps->vboDisable = !theToTurnOn;
562 // =======================================================================
563 // function : IsVerticalSync
565 // =======================================================================
566 bool OpenGl_GraphicDriver::IsVerticalSync() const
568 return myCaps->swapInterval == 1;
571 // =======================================================================
572 // function : SetVerticalSync
574 // =======================================================================
575 void OpenGl_GraphicDriver::SetVerticalSync (bool theToEnable)
577 myCaps->swapInterval = theToEnable ? 1 : 0;
580 // =======================================================================
581 // function : GetSharedContext
583 // =======================================================================
584 const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext (bool theBound) const
586 if (myMapOfView.IsEmpty())
591 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); aViewIter.More(); aViewIter.Next())
593 if (const Handle(OpenGl_Window)& aWindow = aViewIter.Value()->GlWindow())
597 return aWindow->GetGlContext();
599 else if (aWindow->GetGlContext()->IsCurrent())
601 return aWindow->GetGlContext();
609 // =======================================================================
610 // function : MemoryInfo
612 // =======================================================================
613 Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes,
614 TCollection_AsciiString& theInfo) const
616 // this is extra work (for OpenGl_Context initialization)...
617 OpenGl_Context aGlCtx;
620 return Standard_False;
622 theFreeBytes = aGlCtx.AvailableMemory();
623 theInfo = aGlCtx.MemoryInfo();
624 return !theInfo.IsEmpty();
627 // =======================================================================
628 // function : SetBuffersNoSwap
630 // =======================================================================
631 void OpenGl_GraphicDriver::SetBuffersNoSwap (const Standard_Boolean theIsNoSwap)
633 myCaps->buffersNoSwap = theIsNoSwap;
636 // =======================================================================
637 // function : TextSize
639 // =======================================================================
640 void OpenGl_GraphicDriver::TextSize (const Handle(Graphic3d_CView)& theView,
641 const Standard_CString theText,
642 const Standard_ShortReal theHeight,
643 Standard_ShortReal& theWidth,
644 Standard_ShortReal& theAscent,
645 Standard_ShortReal& theDescent) const
647 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
653 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
654 OpenGl_Aspects aTextAspect;
655 TCollection_ExtendedString anExtText = theText;
656 NCollection_String aText (anExtText.ToExtString());
657 OpenGl_Text::StringSize (aCtx, aText, aTextAspect, aHeight,
658 theView->RenderingParams().Resolution, theView->RenderingParams().FontHinting,
659 theWidth, theAscent, theDescent);
662 //=======================================================================
663 //function : InsertLayerBefore
665 //=======================================================================
666 void OpenGl_GraphicDriver::InsertLayerBefore (const Graphic3d_ZLayerId theLayerId,
667 const Graphic3d_ZLayerSettings& theSettings,
668 const Graphic3d_ZLayerId theLayerAfter)
670 base_type::InsertLayerBefore (theLayerId, theSettings, theLayerAfter);
672 // Add layer to all views
673 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
675 aViewIt.Value()->InsertLayerBefore (theLayerId, theSettings, theLayerAfter);
679 //=======================================================================
680 //function : InsertLayerAfter
682 //=======================================================================
683 void OpenGl_GraphicDriver::InsertLayerAfter (const Graphic3d_ZLayerId theNewLayerId,
684 const Graphic3d_ZLayerSettings& theSettings,
685 const Graphic3d_ZLayerId theLayerBefore)
687 base_type::InsertLayerAfter (theNewLayerId, theSettings, theLayerBefore);
689 // Add layer to all views
690 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
692 aViewIt.Value()->InsertLayerAfter (theNewLayerId, theSettings, theLayerBefore);
696 //=======================================================================
697 //function : RemoveZLayer
699 //=======================================================================
700 void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
702 base_type::RemoveZLayer (theLayerId);
704 // Remove layer from all of the views
705 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
707 aViewIt.Value()->RemoveZLayer (theLayerId);
710 // Unset Z layer for all of the structures.
711 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure); aStructIt.More(); aStructIt.Next())
713 OpenGl_Structure* aStruct = aStructIt.ChangeValue ();
714 if (aStruct->ZLayer() == theLayerId)
715 aStruct->SetZLayer (Graphic3d_ZLayerId_Default);
719 //=======================================================================
720 //function : SetZLayerSettings
722 //=======================================================================
723 void OpenGl_GraphicDriver::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
724 const Graphic3d_ZLayerSettings& theSettings)
726 base_type::SetZLayerSettings (theLayerId, theSettings);
728 // Change Z layer settings in all managed views
729 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
731 aViewIt.Value()->SetZLayerSettings (theLayerId, theSettings);
735 // =======================================================================
736 // function : Structure
738 // =======================================================================
739 Handle(Graphic3d_CStructure) OpenGl_GraphicDriver::CreateStructure (const Handle(Graphic3d_StructureManager)& theManager)
741 Handle(OpenGl_Structure) aStructure = new OpenGl_Structure (theManager);
742 myMapOfStructure.Bind (aStructure->Id, aStructure.operator->());
746 // =======================================================================
747 // function : Structure
749 // =======================================================================
750 void OpenGl_GraphicDriver::RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure)
752 OpenGl_Structure* aStructure = NULL;
753 if (!myMapOfStructure.Find (theCStructure->Id, aStructure))
758 myMapOfStructure.UnBind (theCStructure->Id);
759 aStructure->Release (GetSharedContext());
760 theCStructure.Nullify();
763 // =======================================================================
764 // function : CreateView
766 // =======================================================================
767 Handle(Graphic3d_CView) OpenGl_GraphicDriver::CreateView (const Handle(Graphic3d_StructureManager)& theMgr)
769 Handle(OpenGl_View) aView = new OpenGl_View (theMgr, this, myCaps, &myStateCounter);
770 myMapOfView.Add (aView);
771 for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (myLayers); aLayerIter.More(); aLayerIter.Next())
773 const Handle(Graphic3d_Layer)& aLayer = aLayerIter.Value();
774 aView->InsertLayerAfter (aLayer->LayerId(), aLayer->LayerSettings(), Graphic3d_ZLayerId_UNKNOWN);
779 // =======================================================================
780 // function : RemoveView
782 // =======================================================================
783 void OpenGl_GraphicDriver::RemoveView (const Handle(Graphic3d_CView)& theView)
785 Handle(OpenGl_Context) aCtx = GetSharedContext();
786 Handle(OpenGl_View) aView = Handle(OpenGl_View)::DownCast (theView);
792 if (!myMapOfView.Remove (aView))
797 Handle(OpenGl_Window) aWindow = aView->GlWindow();
798 if (!aWindow.IsNull()
799 && aWindow->GetGlContext()->MakeCurrent())
801 aCtx = aWindow->GetGlContext();
805 // try to hijack another context if any
806 const Handle(OpenGl_Context)& anOtherCtx = GetSharedContext();
807 if (!anOtherCtx.IsNull()
808 && anOtherCtx != aWindow->GetGlContext())
815 aView->ReleaseGlResources (aCtx);
816 if (myMapOfView.IsEmpty())
818 // The last view removed but some objects still present.
819 // Release GL resources now without object destruction.
820 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
821 aStructIt.More (); aStructIt.Next())
823 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
824 aStruct->ReleaseGlResources (aCtx);
827 if (!myMapOfStructure.IsEmpty())
829 aView->StructureManager()->SetDeviceLost();
834 // =======================================================================
837 // =======================================================================
838 Handle(OpenGl_Window) OpenGl_GraphicDriver::CreateRenderWindow (const Handle(Aspect_Window)& theWindow,
839 const Aspect_RenderingContext theContext)
841 Handle(OpenGl_Context) aShareCtx = GetSharedContext();
842 Handle(OpenGl_Window) aWindow = new OpenGl_Window (this, theWindow, theContext, myCaps, aShareCtx);
846 //=======================================================================
847 //function : ViewExists
849 //=======================================================================
850 Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)& theWindow,
851 Handle(Graphic3d_CView)& theView)
853 // Parse the list of views to find a view with the specified window
854 const Aspect_Drawable aNativeHandle = theWindow->NativeHandle();
855 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
857 const Handle(OpenGl_View)& aView = aViewIt.Value();
858 if (!aView->IsDefined()
859 || !aView->IsActive())
864 const Handle(Aspect_Window) anAspectWindow = aView->Window();
865 const Aspect_Drawable aViewNativeHandle = anAspectWindow->NativeHandle();
866 if (aViewNativeHandle == aNativeHandle)
876 //=======================================================================
877 //function : setDeviceLost
879 //=======================================================================
880 void OpenGl_GraphicDriver::setDeviceLost()
882 if (myMapOfStructure.IsEmpty())
887 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); aViewIter.More(); aViewIter.Next())
889 const Handle(OpenGl_View)& aView = aViewIter.Value();
890 if (aView->myWasRedrawnGL)
892 aView->StructureManager()->SetDeviceLost();