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(HAVE_XLIB)
42 #include <Xw_Window.hxx>
43 #elif defined(__APPLE__)
44 #include <Cocoa_Window.hxx>
46 #include <Aspect_NeutralWindow.hxx>
49 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && !defined(__EMSCRIPTEN__) && (!defined(__APPLE__) || defined(HAVE_XLIB))
50 #include <X11/Xlib.h> // XOpenDisplay()
54 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
56 #ifndef EGL_OPENGL_ES3_BIT
57 #define EGL_OPENGL_ES3_BIT 0x00000040
63 static const Handle(OpenGl_Context) TheNullGlCtx;
65 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
66 //! Wrapper over eglChooseConfig() called with preferred defaults.
67 static EGLConfig chooseEglSurfConfig (EGLDisplay theDisplay)
69 EGLint aConfigAttribs[] =
77 #if defined(GL_ES_VERSION_2_0)
78 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
80 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
85 EGLConfig aCfg = NULL;
86 EGLint aNbConfigs = 0;
87 for (Standard_Integer aGlesVer = 3; aGlesVer >= 2; --aGlesVer)
89 #if defined(GL_ES_VERSION_2_0)
90 aConfigAttribs[6 * 2 + 1] = aGlesVer == 3 ? EGL_OPENGL_ES3_BIT : EGL_OPENGL_ES2_BIT;
98 if (eglChooseConfig (theDisplay, aConfigAttribs, &aCfg, 1, &aNbConfigs) == EGL_TRUE
105 aConfigAttribs[4 * 2 + 1] = 16; // try config with smaller depth buffer
106 if (eglChooseConfig (theDisplay, aConfigAttribs, &aCfg, 1, &aNbConfigs) == EGL_TRUE
115 #elif defined(HAVE_XLIB)
116 //! Search for RGBA double-buffered visual with stencil buffer.
117 static int TheDoubleBuffVisual[] =
126 //! Search for RGBA double-buffered visual with stencil buffer.
127 static int TheDoubleBuffFBConfig[] =
129 GLX_X_RENDERABLE, True,
130 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
131 GLX_RENDER_TYPE, GLX_RGBA_BIT,
132 GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
135 GLX_DOUBLEBUFFER, True,
142 // =======================================================================
143 // function : OpenGl_GraphicDriver
145 // =======================================================================
146 OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp,
147 const Standard_Boolean theToInitialize)
148 : Graphic3d_GraphicDriver (theDisp),
149 myIsOwnContext (Standard_False),
153 myCaps (new OpenGl_Caps()),
154 myMapOfView (1, NCollection_BaseAllocator::CommonBaseAllocator()),
155 myMapOfStructure (1, NCollection_BaseAllocator::CommonBaseAllocator())
157 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
158 myEglDisplay = (Aspect_Display )EGL_NO_DISPLAY;
159 myEglContext = (Aspect_RenderingContext )EGL_NO_CONTEXT;
162 #if defined(HAVE_XLIB)
163 if (myDisplayConnection.IsNull())
165 //throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: cannot connect to X server!");
169 Display* aDisplay = (Display* )myDisplayConnection->GetDisplayAspect();
170 Bool toSync = ::getenv ("CSF_GraphicSync") != NULL
171 || ::getenv ("CALL_SYNCHRO_X") != NULL;
172 XSynchronize (aDisplay, toSync);
177 throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: default context can not be initialized!");
181 // =======================================================================
182 // function : ~OpenGl_GraphicDriver
184 // =======================================================================
185 OpenGl_GraphicDriver::~OpenGl_GraphicDriver()
190 // =======================================================================
191 // function : ReleaseContext
193 // =======================================================================
194 void OpenGl_GraphicDriver::ReleaseContext()
196 Handle(OpenGl_Context) aCtxShared;
197 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
198 aViewIter.More(); aViewIter.Next())
200 const Handle(OpenGl_View)& aView = aViewIter.Value();
201 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
202 if (aWindow.IsNull())
207 const Handle(OpenGl_Context)& aCtx = aWindow->GetGlContext();
208 if (aCtx->MakeCurrent()
209 && aCtxShared.IsNull())
215 if (!aCtxShared.IsNull())
217 aCtxShared->MakeCurrent();
219 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
220 aViewIter.More(); aViewIter.Next())
222 const Handle(OpenGl_View)& aView = aViewIter.Value();
223 aView->ReleaseGlResources (aCtxShared);
226 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
227 aStructIt.More (); aStructIt.Next())
229 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
230 aStruct->ReleaseGlResources (aCtxShared);
233 const bool isDeviceLost = !myMapOfStructure.IsEmpty();
234 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
235 aViewIter.More(); aViewIter.Next())
237 const Handle(OpenGl_View)& aView = aViewIter.Value();
240 aView->StructureManager()->SetDeviceLost();
243 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
244 if (aWindow.IsNull())
249 aWindow->GetGlContext()->forcedRelease();
252 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
255 if (myEglContext != (Aspect_RenderingContext )EGL_NO_CONTEXT)
257 if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) != EGL_TRUE)
259 ::Message::SendWarning ("OpenGl_GraphicDriver, FAILED to release OpenGL context");
261 eglDestroyContext ((EGLDisplay )myEglDisplay, (EGLContext )myEglContext);
264 if (myEglDisplay != (Aspect_Display )EGL_NO_DISPLAY)
266 if (eglTerminate ((EGLDisplay )myEglDisplay) != EGL_TRUE)
268 ::Message::SendWarning ("OpenGl_GraphicDriver, EGL, eglTerminate FAILED");
273 myEglDisplay = (Aspect_Display )EGL_NO_DISPLAY;
274 myEglContext = (Aspect_RenderingContext )EGL_NO_CONTEXT;
277 myIsOwnContext = Standard_False;
280 // =======================================================================
281 // function : InitContext
283 // =======================================================================
284 Standard_Boolean OpenGl_GraphicDriver::InitContext()
287 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
289 #if defined(HAVE_XLIB)
290 if (myDisplayConnection.IsNull())
292 return Standard_False;
294 Display* aDisplay = (Display* )myDisplayConnection->GetDisplayAspect();
295 myEglDisplay = (Aspect_Display )eglGetDisplay (aDisplay);
297 myEglDisplay = (Aspect_Display )eglGetDisplay (EGL_DEFAULT_DISPLAY);
299 if ((EGLDisplay )myEglDisplay == EGL_NO_DISPLAY)
301 ::Message::SendFail ("Error: no EGL display");
302 return Standard_False;
305 EGLint aVerMajor = 0; EGLint aVerMinor = 0;
306 if (eglInitialize ((EGLDisplay )myEglDisplay, &aVerMajor, &aVerMinor) != EGL_TRUE)
308 ::Message::SendFail ("Error: EGL display is unavailable");
309 return Standard_False;
312 myEglConfig = chooseEglSurfConfig ((EGLDisplay )myEglDisplay);
313 if (myEglConfig == NULL)
315 ::Message::SendFail ("Error: EGL does not provide compatible configurations");
316 return Standard_False;
319 #if defined(GL_ES_VERSION_2_0)
320 EGLint anEglCtxAttribs3[] = { EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE, EGL_NONE };
321 EGLint anEglCtxAttribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };
322 if (eglBindAPI (EGL_OPENGL_ES_API) != EGL_TRUE)
324 ::Message::SendFail ("Error: EGL does not provide OpenGL ES client");
325 return Standard_False;
327 if (myCaps->contextMajorVersionUpper != 2)
329 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs3);
331 if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
333 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs2);
336 EGLint* anEglCtxAttribs = NULL;
337 if (eglBindAPI (EGL_OPENGL_API) != EGL_TRUE)
339 ::Message::SendFail ("Error: EGL does not provide OpenGL client");
340 return Standard_False;
342 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs);
345 if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
347 ::Message::SendFail ("Error: EGL is unable to create OpenGL context");
348 return Standard_False;
350 // eglMakeCurrent() fails or even crash with EGL_NO_SURFACE on some implementations
351 //if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, (EGLContext )myEglContext) != EGL_TRUE)
353 // ::Message::SendFail ("Error: EGL is unable bind OpenGL context");
354 // return Standard_False;
358 myIsOwnContext = Standard_True;
359 return Standard_True;
362 // =======================================================================
363 // function : InitEglContext
365 // =======================================================================
366 Standard_Boolean OpenGl_GraphicDriver::InitEglContext (Aspect_Display theEglDisplay,
367 Aspect_RenderingContext theEglContext,
371 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
372 #if defined(HAVE_XLIB)
373 if (myDisplayConnection.IsNull())
375 return Standard_False;
379 if ((EGLDisplay )theEglDisplay == EGL_NO_DISPLAY
380 || (EGLContext )theEglContext == EGL_NO_CONTEXT)
382 return Standard_False;
384 myEglDisplay = theEglDisplay;
385 myEglContext = theEglContext;
386 myEglConfig = theEglConfig;
387 if (theEglConfig == NULL)
389 myEglConfig = chooseEglSurfConfig ((EGLDisplay )myEglDisplay);
390 if (myEglConfig == NULL)
392 ::Message::SendFail ("Error: EGL does not provide compatible configurations");
393 return Standard_False;
397 return Standard_True;
399 (void )theEglDisplay;
400 (void )theEglContext;
402 throw Standard_NotImplemented ("OpenGl_GraphicDriver::InitEglContext() is not implemented");
406 // =======================================================================
407 // function : chooseVisualInfo
409 // =======================================================================
410 void OpenGl_GraphicDriver::chooseVisualInfo()
412 if (myDisplayConnection.IsNull())
417 #if defined(HAVE_XLIB)
418 Display* aDisp = (Display* )myDisplayConnection->GetDisplayAspect();
420 XVisualInfo* aVisInfo = NULL;
421 Aspect_FBConfig anFBConfig = NULL;
422 #if defined(HAVE_EGL) || defined(HAVE_GLES2)
423 XVisualInfo aVisInfoTmp;
424 memset (&aVisInfoTmp, 0, sizeof(aVisInfoTmp));
425 aVisInfoTmp.screen = DefaultScreen (aDisp);
426 if (myEglDisplay != EGL_NO_DISPLAY
427 && myEglConfig != NULL
428 && eglGetConfigAttrib ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NATIVE_VISUAL_ID, (EGLint* )&aVisInfoTmp.visualid) == EGL_TRUE)
431 aVisInfo = XGetVisualInfo (aDisp, VisualIDMask | VisualScreenMask, &aVisInfoTmp, &aNbVisuals);
434 int aScreen = DefaultScreen(aDisp);
436 if (!XQueryExtension (aDisp, "GLX", &aDummy, &aDummy, &aDummy)
437 || !glXQueryExtension (aDisp, &aDummy, &aDummy))
439 Message::SendFail ("Error: OpenGl_GraphicDriver, GLX extension is unavailable");
442 // FBConfigs were added in GLX version 1.3
443 int aGlxMajor = 0, aGlxMinor = 0;
444 const bool hasFBCfg = glXQueryVersion (aDisp, &aGlxMajor, &aGlxMinor)
445 && ((aGlxMajor == 1 && aGlxMinor >= 3) || (aGlxMajor > 1));
449 GLXFBConfig* aFBCfgList = NULL;
452 aFBCfgList = glXChooseFBConfig (aDisp, aScreen, TheDoubleBuffFBConfig, &aFBCount);
454 if(aFBCfgList != NULL
457 anFBConfig = aFBCfgList[0];
458 aVisInfo = glXGetVisualFromFBConfig (aDisp, anFBConfig);
460 /*int aDepthSize = 0, aStencilSize = 0;
461 glXGetFBConfigAttrib (aDisp, anFBConfig, GLX_DEPTH_SIZE, &aDepthSize);
462 glXGetFBConfigAttrib (aDisp, anFBConfig, GLX_STENCIL_SIZE, &aStencilSize);
463 Message::SendInfo() << "GLX FBConfig:"
464 << "\n DepthSize= " << aDepthSize
465 << "\n StencilSize= " << aStencilSize;*/
470 if (aVisInfo == NULL)
472 aVisInfo = glXChooseVisual (aDisp, aScreen, TheDoubleBuffVisual);
475 if (aVisInfo != NULL)
477 myDisplayConnection->SetDefaultVisualInfo ((Aspect_XVisualInfo* )aVisInfo, anFBConfig);
481 Message::SendWarning ("OpenGl_GraphicDriver, couldn't find compatible Visual (RGBA, double-buffered)");
486 // =======================================================================
487 // function : InquireLimit
489 // =======================================================================
490 Standard_Integer OpenGl_GraphicDriver::InquireLimit (const Graphic3d_TypeOfLimit theType) const
492 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
495 case Graphic3d_TypeOfLimit_MaxNbLights:
496 return Graphic3d_ShaderProgram::THE_MAX_LIGHTS_DEFAULT;
497 case Graphic3d_TypeOfLimit_MaxNbClipPlanes:
498 return !aCtx.IsNull() ? aCtx->MaxClipPlanes() : 0;
499 case Graphic3d_TypeOfLimit_MaxNbViews:
501 case Graphic3d_TypeOfLimit_MaxTextureSize:
502 return !aCtx.IsNull() ? aCtx->MaxTextureSize() : 1024;
503 case Graphic3d_TypeOfLimit_MaxCombinedTextureUnits:
504 return !aCtx.IsNull() ? aCtx->MaxCombinedTextureUnits() : 1;
505 case Graphic3d_TypeOfLimit_MaxMsaa:
506 return !aCtx.IsNull() ? aCtx->MaxMsaaSamples() : 0;
507 case Graphic3d_TypeOfLimit_MaxViewDumpSizeX:
508 return !aCtx.IsNull() ? aCtx->MaxDumpSizeX() : 1024;
509 case Graphic3d_TypeOfLimit_MaxViewDumpSizeY:
510 return !aCtx.IsNull() ? aCtx->MaxDumpSizeY() : 1024;
511 case Graphic3d_TypeOfLimit_HasPBR:
512 return (!aCtx.IsNull() && aCtx->HasPBR()) ? 1 : 0;
513 case Graphic3d_TypeOfLimit_HasRayTracing:
514 return (!aCtx.IsNull() && aCtx->HasRayTracing()) ? 1 : 0;
515 case Graphic3d_TypeOfLimit_HasRayTracingTextures:
516 return (!aCtx.IsNull() && aCtx->HasRayTracingTextures()) ? 1 : 0;
517 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSampling:
518 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSampling()) ? 1 : 0;
519 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSamplingAtomic:
520 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSamplingAtomic()) ? 1 : 0;
521 case Graphic3d_TypeOfLimit_HasSRGB:
522 return (!aCtx.IsNull() && aCtx->HasSRGB()) ? 1 : 0;
523 case Graphic3d_TypeOfLimit_HasBlendedOit:
524 return (!aCtx.IsNull()
525 && aCtx->hasDrawBuffers != OpenGl_FeatureNotAvailable
526 && (aCtx->hasFloatBuffer != OpenGl_FeatureNotAvailable || aCtx->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable)) ? 1 : 0;
527 case Graphic3d_TypeOfLimit_HasBlendedOitMsaa:
528 return (!aCtx.IsNull()
529 && aCtx->hasSampleVariables != OpenGl_FeatureNotAvailable
530 && (InquireLimit (Graphic3d_TypeOfLimit_HasBlendedOit) == 1)) ? 1 : 0;
531 case Graphic3d_TypeOfLimit_HasFlatShading:
532 return !aCtx.IsNull() && aCtx->hasFlatShading != OpenGl_FeatureNotAvailable ? 1 : 0;
533 case Graphic3d_TypeOfLimit_IsWorkaroundFBO:
534 return !aCtx.IsNull() && aCtx->MaxTextureSize() != aCtx->MaxDumpSizeX() ? 1 : 0;
535 case Graphic3d_TypeOfLimit_HasMeshEdges:
536 return !aCtx.IsNull() && aCtx->hasGeometryStage != OpenGl_FeatureNotAvailable ? 1 : 0;
537 case Graphic3d_TypeOfLimit_NB:
543 // =======================================================================
544 // function : DefaultTextHeight
546 // =======================================================================
547 Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const
552 // =======================================================================
553 // function : EnableVBO
555 // =======================================================================
556 void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
558 myCaps->vboDisable = !theToTurnOn;
561 // =======================================================================
562 // function : IsVerticalSync
564 // =======================================================================
565 bool OpenGl_GraphicDriver::IsVerticalSync() const
567 return myCaps->swapInterval == 1;
570 // =======================================================================
571 // function : SetVerticalSync
573 // =======================================================================
574 void OpenGl_GraphicDriver::SetVerticalSync (bool theToEnable)
576 myCaps->swapInterval = theToEnable ? 1 : 0;
579 // =======================================================================
580 // function : GetSharedContext
582 // =======================================================================
583 const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext (bool theBound) const
585 if (myMapOfView.IsEmpty())
590 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); aViewIter.More(); aViewIter.Next())
592 if (const Handle(OpenGl_Window)& aWindow = aViewIter.Value()->GlWindow())
596 return aWindow->GetGlContext();
598 else if (aWindow->GetGlContext()->IsCurrent())
600 return aWindow->GetGlContext();
608 // =======================================================================
609 // function : MemoryInfo
611 // =======================================================================
612 Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes,
613 TCollection_AsciiString& theInfo) const
615 // this is extra work (for OpenGl_Context initialization)...
616 OpenGl_Context aGlCtx;
619 return Standard_False;
621 theFreeBytes = aGlCtx.AvailableMemory();
622 theInfo = aGlCtx.MemoryInfo();
623 return !theInfo.IsEmpty();
626 // =======================================================================
627 // function : SetBuffersNoSwap
629 // =======================================================================
630 void OpenGl_GraphicDriver::SetBuffersNoSwap (const Standard_Boolean theIsNoSwap)
632 myCaps->buffersNoSwap = theIsNoSwap;
635 // =======================================================================
636 // function : TextSize
638 // =======================================================================
639 void OpenGl_GraphicDriver::TextSize (const Handle(Graphic3d_CView)& theView,
640 const Standard_CString theText,
641 const Standard_ShortReal theHeight,
642 Standard_ShortReal& theWidth,
643 Standard_ShortReal& theAscent,
644 Standard_ShortReal& theDescent) const
646 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
652 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
653 OpenGl_Aspects aTextAspect;
654 TCollection_ExtendedString anExtText = theText;
655 NCollection_String aText (anExtText.ToExtString());
656 OpenGl_Text::StringSize (aCtx, aText, aTextAspect, aHeight,
657 theView->RenderingParams().Resolution, theView->RenderingParams().FontHinting,
658 theWidth, theAscent, theDescent);
661 //=======================================================================
662 //function : InsertLayerBefore
664 //=======================================================================
665 void OpenGl_GraphicDriver::InsertLayerBefore (const Graphic3d_ZLayerId theLayerId,
666 const Graphic3d_ZLayerSettings& theSettings,
667 const Graphic3d_ZLayerId theLayerAfter)
669 base_type::InsertLayerBefore (theLayerId, theSettings, theLayerAfter);
671 // Add layer to all views
672 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
674 aViewIt.Value()->InsertLayerBefore (theLayerId, theSettings, theLayerAfter);
678 //=======================================================================
679 //function : InsertLayerAfter
681 //=======================================================================
682 void OpenGl_GraphicDriver::InsertLayerAfter (const Graphic3d_ZLayerId theNewLayerId,
683 const Graphic3d_ZLayerSettings& theSettings,
684 const Graphic3d_ZLayerId theLayerBefore)
686 base_type::InsertLayerAfter (theNewLayerId, theSettings, theLayerBefore);
688 // Add layer to all views
689 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
691 aViewIt.Value()->InsertLayerAfter (theNewLayerId, theSettings, theLayerBefore);
695 //=======================================================================
696 //function : RemoveZLayer
698 //=======================================================================
699 void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
701 base_type::RemoveZLayer (theLayerId);
703 // Remove layer from all of the views
704 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
706 aViewIt.Value()->RemoveZLayer (theLayerId);
709 // Unset Z layer for all of the structures.
710 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure); aStructIt.More(); aStructIt.Next())
712 OpenGl_Structure* aStruct = aStructIt.ChangeValue ();
713 if (aStruct->ZLayer() == theLayerId)
714 aStruct->SetZLayer (Graphic3d_ZLayerId_Default);
718 //=======================================================================
719 //function : SetZLayerSettings
721 //=======================================================================
722 void OpenGl_GraphicDriver::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
723 const Graphic3d_ZLayerSettings& theSettings)
725 base_type::SetZLayerSettings (theLayerId, theSettings);
727 // Change Z layer settings in all managed views
728 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
730 aViewIt.Value()->SetZLayerSettings (theLayerId, theSettings);
734 // =======================================================================
735 // function : Structure
737 // =======================================================================
738 Handle(Graphic3d_CStructure) OpenGl_GraphicDriver::CreateStructure (const Handle(Graphic3d_StructureManager)& theManager)
740 Handle(OpenGl_Structure) aStructure = new OpenGl_Structure (theManager);
741 myMapOfStructure.Bind (aStructure->Id, aStructure.operator->());
745 // =======================================================================
746 // function : Structure
748 // =======================================================================
749 void OpenGl_GraphicDriver::RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure)
751 OpenGl_Structure* aStructure = NULL;
752 if (!myMapOfStructure.Find (theCStructure->Id, aStructure))
757 myMapOfStructure.UnBind (theCStructure->Id);
758 aStructure->Release (GetSharedContext());
759 theCStructure.Nullify();
762 // =======================================================================
763 // function : CreateView
765 // =======================================================================
766 Handle(Graphic3d_CView) OpenGl_GraphicDriver::CreateView (const Handle(Graphic3d_StructureManager)& theMgr)
768 Handle(OpenGl_View) aView = new OpenGl_View (theMgr, this, myCaps, &myStateCounter);
769 myMapOfView.Add (aView);
770 for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (myLayers); aLayerIter.More(); aLayerIter.Next())
772 const Handle(Graphic3d_Layer)& aLayer = aLayerIter.Value();
773 aView->InsertLayerAfter (aLayer->LayerId(), aLayer->LayerSettings(), Graphic3d_ZLayerId_UNKNOWN);
778 // =======================================================================
779 // function : RemoveView
781 // =======================================================================
782 void OpenGl_GraphicDriver::RemoveView (const Handle(Graphic3d_CView)& theView)
784 Handle(OpenGl_Context) aCtx = GetSharedContext();
785 Handle(OpenGl_View) aView = Handle(OpenGl_View)::DownCast (theView);
791 if (!myMapOfView.Remove (aView))
796 Handle(OpenGl_Window) aWindow = aView->GlWindow();
797 if (!aWindow.IsNull()
798 && aWindow->GetGlContext()->MakeCurrent())
800 aCtx = aWindow->GetGlContext();
804 // try to hijack another context if any
805 const Handle(OpenGl_Context)& anOtherCtx = GetSharedContext();
806 if (!anOtherCtx.IsNull()
807 && anOtherCtx != aWindow->GetGlContext())
814 aView->ReleaseGlResources (aCtx);
815 if (myMapOfView.IsEmpty())
817 // The last view removed but some objects still present.
818 // Release GL resources now without object destruction.
819 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
820 aStructIt.More (); aStructIt.Next())
822 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
823 aStruct->ReleaseGlResources (aCtx);
826 if (!myMapOfStructure.IsEmpty())
828 aView->StructureManager()->SetDeviceLost();
833 // =======================================================================
836 // =======================================================================
837 Handle(OpenGl_Window) OpenGl_GraphicDriver::CreateRenderWindow (const Handle(Aspect_Window)& theWindow,
838 const Aspect_RenderingContext theContext)
840 Handle(OpenGl_Context) aShareCtx = GetSharedContext();
841 Handle(OpenGl_Window) aWindow = new OpenGl_Window (this, theWindow, theContext, myCaps, aShareCtx);
845 //=======================================================================
846 //function : ViewExists
848 //=======================================================================
849 Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)& theWindow,
850 Handle(Graphic3d_CView)& theView)
852 // Parse the list of views to find a view with the specified window
853 const Aspect_Drawable aNativeHandle = theWindow->NativeHandle();
854 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
856 const Handle(OpenGl_View)& aView = aViewIt.Value();
857 if (!aView->IsDefined()
858 || !aView->IsActive())
863 const Handle(Aspect_Window) anAspectWindow = aView->Window();
864 const Aspect_Drawable aViewNativeHandle = anAspectWindow->NativeHandle();
865 if (aViewNativeHandle == aNativeHandle)
875 //=======================================================================
876 //function : setDeviceLost
878 //=======================================================================
879 void OpenGl_GraphicDriver::setDeviceLost()
881 if (myMapOfStructure.IsEmpty())
886 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); aViewIter.More(); aViewIter.Next())
888 const Handle(OpenGl_View)& aView = aViewIter.Value();
889 if (aView->myWasRedrawnGL)
891 aView->StructureManager()->SetDeviceLost();