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()
52 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
54 #ifndef EGL_OPENGL_ES3_BIT
55 #define EGL_OPENGL_ES3_BIT 0x00000040
61 static const Handle(OpenGl_Context) TheNullGlCtx;
63 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
64 //! Wrapper over eglChooseConfig() called with preferred defaults.
65 static EGLConfig chooseEglSurfConfig (EGLDisplay theDisplay)
67 EGLint aConfigAttribs[] =
75 #if defined(GL_ES_VERSION_2_0)
76 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
78 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
83 EGLConfig aCfg = NULL;
84 EGLint aNbConfigs = 0;
85 for (Standard_Integer aGlesVer = 3; aGlesVer >= 2; --aGlesVer)
87 #if defined(GL_ES_VERSION_2_0)
88 aConfigAttribs[6 * 2 + 1] = aGlesVer == 3 ? EGL_OPENGL_ES3_BIT : EGL_OPENGL_ES2_BIT;
96 if (eglChooseConfig (theDisplay, aConfigAttribs, &aCfg, 1, &aNbConfigs) == EGL_TRUE
103 aConfigAttribs[4 * 2 + 1] = 16; // try config with smaller depth buffer
104 if (eglChooseConfig (theDisplay, aConfigAttribs, &aCfg, 1, &aNbConfigs) == EGL_TRUE
113 #elif !defined(_WIN32) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
114 //! Search for RGBA double-buffered visual with stencil buffer.
115 static int TheDoubleBuffVisual[] =
124 //! Search for RGBA double-buffered visual with stencil buffer.
125 static int TheDoubleBuffFBConfig[] =
127 GLX_X_RENDERABLE, True,
128 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
129 GLX_RENDER_TYPE, GLX_RGBA_BIT,
130 GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
133 GLX_DOUBLEBUFFER, True,
140 // =======================================================================
141 // function : OpenGl_GraphicDriver
143 // =======================================================================
144 OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp,
145 const Standard_Boolean theToInitialize)
146 : Graphic3d_GraphicDriver (theDisp),
147 myIsOwnContext (Standard_False),
148 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
149 myEglDisplay ((Aspect_Display )EGL_NO_DISPLAY),
150 myEglContext ((Aspect_RenderingContext )EGL_NO_CONTEXT),
153 myCaps (new OpenGl_Caps()),
154 myMapOfView (1, NCollection_BaseAllocator::CommonBaseAllocator()),
155 myMapOfStructure (1, NCollection_BaseAllocator::CommonBaseAllocator())
157 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && !defined(__EMSCRIPTEN__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
158 if (myDisplayConnection.IsNull())
160 //throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: cannot connect to X server!");
164 Display* aDisplay = myDisplayConnection->GetDisplay();
165 Bool toSync = ::getenv ("CSF_GraphicSync") != NULL
166 || ::getenv ("CALL_SYNCHRO_X") != NULL;
167 XSynchronize (aDisplay, toSync);
172 throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: default context can not be initialized!");
176 // =======================================================================
177 // function : ~OpenGl_GraphicDriver
179 // =======================================================================
180 OpenGl_GraphicDriver::~OpenGl_GraphicDriver()
185 // =======================================================================
186 // function : ReleaseContext
188 // =======================================================================
189 void OpenGl_GraphicDriver::ReleaseContext()
191 Handle(OpenGl_Context) aCtxShared;
192 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
193 aViewIter.More(); aViewIter.Next())
195 const Handle(OpenGl_View)& aView = aViewIter.Value();
196 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
197 if (aWindow.IsNull())
202 const Handle(OpenGl_Context)& aCtx = aWindow->GetGlContext();
203 if (aCtx->MakeCurrent()
204 && aCtxShared.IsNull())
210 if (!aCtxShared.IsNull())
212 aCtxShared->MakeCurrent();
214 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
215 aViewIter.More(); aViewIter.Next())
217 const Handle(OpenGl_View)& aView = aViewIter.Value();
218 aView->ReleaseGlResources (aCtxShared);
221 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
222 aStructIt.More (); aStructIt.Next())
224 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
225 aStruct->ReleaseGlResources (aCtxShared);
228 const bool isDeviceLost = !myMapOfStructure.IsEmpty();
229 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
230 aViewIter.More(); aViewIter.Next())
232 const Handle(OpenGl_View)& aView = aViewIter.Value();
235 aView->StructureManager()->SetDeviceLost();
238 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
239 if (aWindow.IsNull())
244 aWindow->GetGlContext()->forcedRelease();
247 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
250 if (myEglContext != (Aspect_RenderingContext )EGL_NO_CONTEXT)
252 if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) != EGL_TRUE)
254 ::Message::SendWarning ("OpenGl_GraphicDriver, FAILED to release OpenGL context");
256 eglDestroyContext ((EGLDisplay )myEglDisplay, (EGLContext )myEglContext);
259 if (myEglDisplay != (Aspect_Display )EGL_NO_DISPLAY)
261 if (eglTerminate ((EGLDisplay )myEglDisplay) != EGL_TRUE)
263 ::Message::SendWarning ("OpenGl_GraphicDriver, EGL, eglTerminate FAILED");
268 myEglDisplay = (Aspect_Display )EGL_NO_DISPLAY;
269 myEglContext = (Aspect_RenderingContext )EGL_NO_CONTEXT;
272 myIsOwnContext = Standard_False;
275 // =======================================================================
276 // function : InitContext
278 // =======================================================================
279 Standard_Boolean OpenGl_GraphicDriver::InitContext()
282 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
284 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && !defined(__EMSCRIPTEN__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
285 if (myDisplayConnection.IsNull())
287 return Standard_False;
289 Display* aDisplay = myDisplayConnection->GetDisplay();
290 myEglDisplay = (Aspect_Display )eglGetDisplay (aDisplay);
292 myEglDisplay = (Aspect_Display )eglGetDisplay (EGL_DEFAULT_DISPLAY);
294 if ((EGLDisplay )myEglDisplay == EGL_NO_DISPLAY)
296 ::Message::SendFail ("Error: no EGL display");
297 return Standard_False;
300 EGLint aVerMajor = 0; EGLint aVerMinor = 0;
301 if (eglInitialize ((EGLDisplay )myEglDisplay, &aVerMajor, &aVerMinor) != EGL_TRUE)
303 ::Message::SendFail ("Error: EGL display is unavailable");
304 return Standard_False;
307 myEglConfig = chooseEglSurfConfig ((EGLDisplay )myEglDisplay);
308 if (myEglConfig == NULL)
310 ::Message::SendFail ("Error: EGL does not provide compatible configurations");
311 return Standard_False;
314 #if defined(GL_ES_VERSION_2_0)
315 EGLint anEglCtxAttribs3[] = { EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE, EGL_NONE };
316 EGLint anEglCtxAttribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };
317 if (eglBindAPI (EGL_OPENGL_ES_API) != EGL_TRUE)
319 ::Message::SendFail ("Error: EGL does not provide OpenGL ES client");
320 return Standard_False;
322 if (myCaps->contextMajorVersionUpper != 2)
324 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs3);
326 if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
328 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs2);
331 EGLint* anEglCtxAttribs = NULL;
332 if (eglBindAPI (EGL_OPENGL_API) != EGL_TRUE)
334 ::Message::SendFail ("Error: EGL does not provide OpenGL client");
335 return Standard_False;
337 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs);
340 if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
342 ::Message::SendFail ("Error: EGL is unable to create OpenGL context");
343 return Standard_False;
345 // eglMakeCurrent() fails or even crash with EGL_NO_SURFACE on some implementations
346 //if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, (EGLContext )myEglContext) != EGL_TRUE)
348 // ::Message::SendFail ("Error: EGL is unable bind OpenGL context");
349 // return Standard_False;
353 myIsOwnContext = Standard_True;
354 return Standard_True;
357 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
358 // =======================================================================
359 // function : InitEglContext
361 // =======================================================================
362 Standard_Boolean OpenGl_GraphicDriver::InitEglContext (Aspect_Display theEglDisplay,
363 Aspect_RenderingContext theEglContext,
367 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && !defined(__EMSCRIPTEN__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
368 if (myDisplayConnection.IsNull())
370 return Standard_False;
374 if ((EGLDisplay )theEglDisplay == EGL_NO_DISPLAY
375 || (EGLContext )theEglContext == EGL_NO_CONTEXT)
377 return Standard_False;
379 myEglDisplay = theEglDisplay;
380 myEglContext = theEglContext;
381 myEglConfig = theEglConfig;
382 if (theEglConfig == NULL)
384 myEglConfig = chooseEglSurfConfig ((EGLDisplay )myEglDisplay);
385 if (myEglConfig == NULL)
387 ::Message::SendFail ("Error: EGL does not provide compatible configurations");
388 return Standard_False;
392 return Standard_True;
396 // =======================================================================
397 // function : chooseVisualInfo
399 // =======================================================================
400 void OpenGl_GraphicDriver::chooseVisualInfo()
402 if (myDisplayConnection.IsNull())
407 #if !defined(_WIN32) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)) && !defined(__ANDROID__) && !defined(__QNX__) && !defined(__EMSCRIPTEN__)
408 Display* aDisp = myDisplayConnection->GetDisplay();
410 XVisualInfo* aVisInfo = NULL;
411 Aspect_FBConfig anFBConfig = NULL;
412 #if defined(HAVE_EGL) || defined(HAVE_GLES2)
413 XVisualInfo aVisInfoTmp;
414 memset (&aVisInfoTmp, 0, sizeof(aVisInfoTmp));
415 aVisInfoTmp.screen = DefaultScreen (aDisp);
416 if (myEglDisplay != EGL_NO_DISPLAY
417 && myEglConfig != NULL
418 && eglGetConfigAttrib ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NATIVE_VISUAL_ID, (EGLint* )&aVisInfoTmp.visualid) == EGL_TRUE)
421 aVisInfo = XGetVisualInfo (aDisp, VisualIDMask | VisualScreenMask, &aVisInfoTmp, &aNbVisuals);
424 int aScreen = DefaultScreen(aDisp);
426 if (!XQueryExtension (aDisp, "GLX", &aDummy, &aDummy, &aDummy)
427 || !glXQueryExtension (aDisp, &aDummy, &aDummy))
429 Message::SendFail ("Error: OpenGl_GraphicDriver, GLX extension is unavailable");
432 // FBConfigs were added in GLX version 1.3
433 int aGlxMajor = 0, aGlxMinor = 0;
434 const bool hasFBCfg = glXQueryVersion (aDisp, &aGlxMajor, &aGlxMinor)
435 && ((aGlxMajor == 1 && aGlxMinor >= 3) || (aGlxMajor > 1));
439 GLXFBConfig* aFBCfgList = NULL;
442 aFBCfgList = glXChooseFBConfig (aDisp, aScreen, TheDoubleBuffFBConfig, &aFBCount);
444 if(aFBCfgList != NULL
447 anFBConfig = aFBCfgList[0];
448 aVisInfo = glXGetVisualFromFBConfig (aDisp, anFBConfig);
450 /*int aDepthSize = 0, aStencilSize = 0;
451 glXGetFBConfigAttrib (aDisp, anFBConfig, GLX_DEPTH_SIZE, &aDepthSize);
452 glXGetFBConfigAttrib (aDisp, anFBConfig, GLX_STENCIL_SIZE, &aStencilSize);
453 Message::SendInfo() << "GLX FBConfig:"
454 << "\n DepthSize= " << aDepthSize
455 << "\n StencilSize= " << aStencilSize;*/
460 if (aVisInfo == NULL)
462 aVisInfo = glXChooseVisual (aDisp, aScreen, TheDoubleBuffVisual);
465 if (aVisInfo != NULL)
467 myDisplayConnection->SetDefaultVisualInfo (aVisInfo, anFBConfig);
471 Message::SendWarning ("OpenGl_GraphicDriver, couldn't find compatible Visual (RGBA, double-buffered)");
476 // =======================================================================
477 // function : InquireLimit
479 // =======================================================================
480 Standard_Integer OpenGl_GraphicDriver::InquireLimit (const Graphic3d_TypeOfLimit theType) const
482 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
485 case Graphic3d_TypeOfLimit_MaxNbLights:
486 return Graphic3d_ShaderProgram::THE_MAX_LIGHTS_DEFAULT;
487 case Graphic3d_TypeOfLimit_MaxNbClipPlanes:
488 return !aCtx.IsNull() ? aCtx->MaxClipPlanes() : 0;
489 case Graphic3d_TypeOfLimit_MaxNbViews:
491 case Graphic3d_TypeOfLimit_MaxTextureSize:
492 return !aCtx.IsNull() ? aCtx->MaxTextureSize() : 1024;
493 case Graphic3d_TypeOfLimit_MaxCombinedTextureUnits:
494 return !aCtx.IsNull() ? aCtx->MaxCombinedTextureUnits() : 1;
495 case Graphic3d_TypeOfLimit_MaxMsaa:
496 return !aCtx.IsNull() ? aCtx->MaxMsaaSamples() : 0;
497 case Graphic3d_TypeOfLimit_MaxViewDumpSizeX:
498 return !aCtx.IsNull() ? aCtx->MaxDumpSizeX() : 1024;
499 case Graphic3d_TypeOfLimit_MaxViewDumpSizeY:
500 return !aCtx.IsNull() ? aCtx->MaxDumpSizeY() : 1024;
501 case Graphic3d_TypeOfLimit_HasPBR:
502 return (!aCtx.IsNull() && aCtx->HasPBR()) ? 1 : 0;
503 case Graphic3d_TypeOfLimit_HasRayTracing:
504 return (!aCtx.IsNull() && aCtx->HasRayTracing()) ? 1 : 0;
505 case Graphic3d_TypeOfLimit_HasRayTracingTextures:
506 return (!aCtx.IsNull() && aCtx->HasRayTracingTextures()) ? 1 : 0;
507 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSampling:
508 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSampling()) ? 1 : 0;
509 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSamplingAtomic:
510 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSamplingAtomic()) ? 1 : 0;
511 case Graphic3d_TypeOfLimit_HasSRGB:
512 return (!aCtx.IsNull() && aCtx->HasSRGB()) ? 1 : 0;
513 case Graphic3d_TypeOfLimit_HasBlendedOit:
514 return (!aCtx.IsNull()
515 && aCtx->hasDrawBuffers != OpenGl_FeatureNotAvailable
516 && (aCtx->hasFloatBuffer != OpenGl_FeatureNotAvailable || aCtx->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable)) ? 1 : 0;
517 case Graphic3d_TypeOfLimit_HasBlendedOitMsaa:
518 return (!aCtx.IsNull()
519 && aCtx->hasSampleVariables != OpenGl_FeatureNotAvailable
520 && (InquireLimit (Graphic3d_TypeOfLimit_HasBlendedOit) == 1)) ? 1 : 0;
521 case Graphic3d_TypeOfLimit_HasFlatShading:
522 return !aCtx.IsNull() && aCtx->hasFlatShading != OpenGl_FeatureNotAvailable ? 1 : 0;
523 case Graphic3d_TypeOfLimit_IsWorkaroundFBO:
524 return !aCtx.IsNull() && aCtx->MaxTextureSize() != aCtx->MaxDumpSizeX() ? 1 : 0;
525 case Graphic3d_TypeOfLimit_HasMeshEdges:
526 return !aCtx.IsNull() && aCtx->hasGeometryStage != OpenGl_FeatureNotAvailable ? 1 : 0;
527 case Graphic3d_TypeOfLimit_NB:
533 // =======================================================================
534 // function : DefaultTextHeight
536 // =======================================================================
537 Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const
542 // =======================================================================
543 // function : EnableVBO
545 // =======================================================================
546 void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
548 myCaps->vboDisable = !theToTurnOn;
551 // =======================================================================
552 // function : IsVerticalSync
554 // =======================================================================
555 bool OpenGl_GraphicDriver::IsVerticalSync() const
557 return myCaps->swapInterval == 1;
560 // =======================================================================
561 // function : SetVerticalSync
563 // =======================================================================
564 void OpenGl_GraphicDriver::SetVerticalSync (bool theToEnable)
566 myCaps->swapInterval = theToEnable ? 1 : 0;
569 // =======================================================================
570 // function : GetSharedContext
572 // =======================================================================
573 const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext (bool theBound) const
575 if (myMapOfView.IsEmpty())
580 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); aViewIter.More(); aViewIter.Next())
582 if (const Handle(OpenGl_Window)& aWindow = aViewIter.Value()->GlWindow())
586 return aWindow->GetGlContext();
588 else if (aWindow->GetGlContext()->IsCurrent())
590 return aWindow->GetGlContext();
598 // =======================================================================
599 // function : MemoryInfo
601 // =======================================================================
602 Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes,
603 TCollection_AsciiString& theInfo) const
605 // this is extra work (for OpenGl_Context initialization)...
606 OpenGl_Context aGlCtx;
609 return Standard_False;
611 theFreeBytes = aGlCtx.AvailableMemory();
612 theInfo = aGlCtx.MemoryInfo();
613 return !theInfo.IsEmpty();
616 // =======================================================================
617 // function : SetBuffersNoSwap
619 // =======================================================================
620 void OpenGl_GraphicDriver::SetBuffersNoSwap (const Standard_Boolean theIsNoSwap)
622 myCaps->buffersNoSwap = theIsNoSwap;
625 // =======================================================================
626 // function : TextSize
628 // =======================================================================
629 void OpenGl_GraphicDriver::TextSize (const Handle(Graphic3d_CView)& theView,
630 const Standard_CString theText,
631 const Standard_ShortReal theHeight,
632 Standard_ShortReal& theWidth,
633 Standard_ShortReal& theAscent,
634 Standard_ShortReal& theDescent) const
636 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
642 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
643 OpenGl_Aspects aTextAspect;
644 TCollection_ExtendedString anExtText = theText;
645 NCollection_String aText (anExtText.ToExtString());
646 OpenGl_Text::StringSize(aCtx, aText, aTextAspect, aHeight, theView->RenderingParams().Resolution, theWidth, theAscent, theDescent);
649 //=======================================================================
650 //function : InsertLayerBefore
652 //=======================================================================
653 void OpenGl_GraphicDriver::InsertLayerBefore (const Graphic3d_ZLayerId theLayerId,
654 const Graphic3d_ZLayerSettings& theSettings,
655 const Graphic3d_ZLayerId theLayerAfter)
657 base_type::InsertLayerBefore (theLayerId, theSettings, theLayerAfter);
659 // Add layer to all views
660 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
662 aViewIt.Value()->InsertLayerBefore (theLayerId, theSettings, theLayerAfter);
666 //=======================================================================
667 //function : InsertLayerAfter
669 //=======================================================================
670 void OpenGl_GraphicDriver::InsertLayerAfter (const Graphic3d_ZLayerId theNewLayerId,
671 const Graphic3d_ZLayerSettings& theSettings,
672 const Graphic3d_ZLayerId theLayerBefore)
674 base_type::InsertLayerAfter (theNewLayerId, theSettings, theLayerBefore);
676 // Add layer to all views
677 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
679 aViewIt.Value()->InsertLayerAfter (theNewLayerId, theSettings, theLayerBefore);
683 //=======================================================================
684 //function : RemoveZLayer
686 //=======================================================================
687 void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
689 base_type::RemoveZLayer (theLayerId);
691 // Remove layer from all of the views
692 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
694 aViewIt.Value()->RemoveZLayer (theLayerId);
697 // Unset Z layer for all of the structures.
698 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure); aStructIt.More(); aStructIt.Next())
700 OpenGl_Structure* aStruct = aStructIt.ChangeValue ();
701 if (aStruct->ZLayer() == theLayerId)
702 aStruct->SetZLayer (Graphic3d_ZLayerId_Default);
706 //=======================================================================
707 //function : SetZLayerSettings
709 //=======================================================================
710 void OpenGl_GraphicDriver::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
711 const Graphic3d_ZLayerSettings& theSettings)
713 base_type::SetZLayerSettings (theLayerId, theSettings);
715 // Change Z layer settings in all managed views
716 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
718 aViewIt.Value()->SetZLayerSettings (theLayerId, theSettings);
722 // =======================================================================
723 // function : Structure
725 // =======================================================================
726 Handle(Graphic3d_CStructure) OpenGl_GraphicDriver::CreateStructure (const Handle(Graphic3d_StructureManager)& theManager)
728 Handle(OpenGl_Structure) aStructure = new OpenGl_Structure (theManager);
729 myMapOfStructure.Bind (aStructure->Id, aStructure.operator->());
733 // =======================================================================
734 // function : Structure
736 // =======================================================================
737 void OpenGl_GraphicDriver::RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure)
739 OpenGl_Structure* aStructure = NULL;
740 if (!myMapOfStructure.Find (theCStructure->Id, aStructure))
745 myMapOfStructure.UnBind (theCStructure->Id);
746 aStructure->Release (GetSharedContext());
747 theCStructure.Nullify();
750 // =======================================================================
751 // function : CreateView
753 // =======================================================================
754 Handle(Graphic3d_CView) OpenGl_GraphicDriver::CreateView (const Handle(Graphic3d_StructureManager)& theMgr)
756 Handle(OpenGl_View) aView = new OpenGl_View (theMgr, this, myCaps, &myStateCounter);
757 myMapOfView.Add (aView);
758 for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (myLayers); aLayerIter.More(); aLayerIter.Next())
760 const Handle(Graphic3d_Layer)& aLayer = aLayerIter.Value();
761 aView->InsertLayerAfter (aLayer->LayerId(), aLayer->LayerSettings(), Graphic3d_ZLayerId_UNKNOWN);
766 // =======================================================================
767 // function : RemoveView
769 // =======================================================================
770 void OpenGl_GraphicDriver::RemoveView (const Handle(Graphic3d_CView)& theView)
772 Handle(OpenGl_Context) aCtx = GetSharedContext();
773 Handle(OpenGl_View) aView = Handle(OpenGl_View)::DownCast (theView);
779 if (!myMapOfView.Remove (aView))
784 Handle(OpenGl_Window) aWindow = aView->GlWindow();
785 if (!aWindow.IsNull()
786 && aWindow->GetGlContext()->MakeCurrent())
788 aCtx = aWindow->GetGlContext();
792 // try to hijack another context if any
793 const Handle(OpenGl_Context)& anOtherCtx = GetSharedContext();
794 if (!anOtherCtx.IsNull()
795 && anOtherCtx != aWindow->GetGlContext())
802 aView->ReleaseGlResources (aCtx);
803 if (myMapOfView.IsEmpty())
805 // The last view removed but some objects still present.
806 // Release GL resources now without object destruction.
807 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
808 aStructIt.More (); aStructIt.Next())
810 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
811 aStruct->ReleaseGlResources (aCtx);
814 if (!myMapOfStructure.IsEmpty())
816 aView->StructureManager()->SetDeviceLost();
821 // =======================================================================
824 // =======================================================================
825 Handle(OpenGl_Window) OpenGl_GraphicDriver::CreateRenderWindow (const Handle(Aspect_Window)& theWindow,
826 const Aspect_RenderingContext theContext)
828 Handle(OpenGl_Context) aShareCtx = GetSharedContext();
829 Handle(OpenGl_Window) aWindow = new OpenGl_Window (this, theWindow, theContext, myCaps, aShareCtx);
833 //=======================================================================
834 //function : ViewExists
836 //=======================================================================
837 Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)& AWindow, Handle(Graphic3d_CView)& theView)
839 Standard_Boolean isExist = Standard_False;
841 // Parse the list of views to find
842 // a view with the specified window
844 #if defined(_WIN32) && !defined(OCCT_UWP)
845 const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
846 Aspect_Handle TheSpecifiedWindowId = THEWindow->HWindow ();
847 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
848 const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow);
849 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
850 UIView* TheSpecifiedWindowId = THEWindow->HView();
852 NSView* TheSpecifiedWindowId = THEWindow->HView();
854 #elif defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__) || defined(OCCT_UWP)
856 int TheSpecifiedWindowId = -1;
858 const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow);
859 int TheSpecifiedWindowId = int (THEWindow->XWindow ());
862 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
863 for(; aViewIt.More(); aViewIt.Next())
865 const Handle(OpenGl_View)& aView = aViewIt.Value();
866 if (aView->IsDefined() && aView->IsActive())
868 const Handle(Aspect_Window) AspectWindow = aView->Window();
870 #if defined(_WIN32) && !defined(OCCT_UWP)
871 const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
872 Aspect_Handle TheWindowIdOfView = theWindow->HWindow ();
873 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
874 const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow);
875 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
876 UIView* TheWindowIdOfView = theWindow->HView();
878 NSView* TheWindowIdOfView = theWindow->HView();
880 #elif defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__) || defined(OCCT_UWP)
881 int TheWindowIdOfView = 0;
883 const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
884 int TheWindowIdOfView = int (theWindow->XWindow ());
886 // Comparaison on window IDs
887 if (TheWindowIdOfView == TheSpecifiedWindowId)
889 isExist = Standard_True;
898 //=======================================================================
899 //function : setDeviceLost
901 //=======================================================================
902 void OpenGl_GraphicDriver::setDeviceLost()
904 if (myMapOfStructure.IsEmpty())
909 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); aViewIter.More(); aViewIter.Next())
911 const Handle(OpenGl_View)& aView = aViewIter.Value();
912 if (aView->myWasRedrawnGL)
914 aView->StructureManager()->SetDeviceLost();