1 // Created on: 2011-10-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
20 #include <OpenGl_GraphicDriver.hxx>
21 #include <OpenGl_Context.hxx>
22 #include <OpenGl_Flipper.hxx>
23 #include <OpenGl_GraduatedTrihedron.hxx>
24 #include <OpenGl_Group.hxx>
25 #include <OpenGl_View.hxx>
26 #include <OpenGl_StencilTest.hxx>
27 #include <OpenGl_Text.hxx>
28 #include <OpenGl_Workspace.hxx>
30 #include <Aspect_GraphicDeviceDefinitionError.hxx>
31 #include <Aspect_IdentDefinitionError.hxx>
32 #include <Graphic3d_StructureManager.hxx>
33 #include <Message_Messenger.hxx>
34 #include <OSD_Environment.hxx>
35 #include <Standard_NotImplemented.hxx>
37 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
40 #include <WNT_Window.hxx>
41 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
42 #include <Cocoa_Window.hxx>
44 #include <Xw_Window.hxx>
47 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
48 #include <X11/Xlib.h> // XOpenDisplay()
51 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
57 static const Handle(OpenGl_Context) TheNullGlCtx;
60 // =======================================================================
61 // function : OpenGl_GraphicDriver
63 // =======================================================================
64 OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp,
65 const Standard_Boolean theToInitialize)
66 : Graphic3d_GraphicDriver (theDisp),
67 myIsOwnContext (Standard_False),
68 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
69 myEglDisplay ((Aspect_Display )EGL_NO_DISPLAY),
70 myEglContext ((Aspect_RenderingContext )EGL_NO_CONTEXT),
73 myCaps (new OpenGl_Caps()),
74 myMapOfView (1, NCollection_BaseAllocator::CommonBaseAllocator()),
75 myMapOfStructure (1, NCollection_BaseAllocator::CommonBaseAllocator())
77 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
78 if (myDisplayConnection.IsNull())
80 //throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: cannot connect to X server!");
84 Display* aDisplay = myDisplayConnection->GetDisplay();
85 Bool toSync = ::getenv ("CSF_GraphicSync") != NULL
86 || ::getenv ("CALL_SYNCHRO_X") != NULL;
87 XSynchronize (aDisplay, toSync);
89 #if !defined(HAVE_EGL) && !defined(HAVE_GLES2)
90 // does the server know about OpenGL & GLX?
92 if (!XQueryExtension (aDisplay, "GLX", &aDummy, &aDummy, &aDummy))
94 ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, this system doesn't appear to support OpenGL!", Message_Warning);
101 throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: default context can not be initialized!");
104 // default layers are always presented in display layer sequence it can not be removed
106 Graphic3d_ZLayerSettings aSettings;
107 aSettings.SetImmediate (Standard_False);
108 aSettings.SetEnvironmentTexture (Standard_False);
109 aSettings.SetEnableDepthTest (Standard_False);
110 aSettings.SetEnableDepthWrite (Standard_False);
111 aSettings.SetClearDepth (Standard_False);
112 aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
113 myLayerIds.Add (Graphic3d_ZLayerId_BotOSD);
114 myLayerSeq.Append (Graphic3d_ZLayerId_BotOSD);
115 myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_BotOSD, aSettings);
119 Graphic3d_ZLayerSettings aSettings;
120 aSettings.SetImmediate (Standard_False);
121 aSettings.SetEnvironmentTexture (Standard_True);
122 aSettings.SetEnableDepthTest (Standard_True);
123 aSettings.SetEnableDepthWrite (Standard_True);
124 aSettings.SetClearDepth (Standard_False);
125 aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
126 myLayerIds.Add (Graphic3d_ZLayerId_Default);
127 myLayerSeq.Append (Graphic3d_ZLayerId_Default);
128 myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Default, aSettings);
132 Graphic3d_ZLayerSettings aSettings;
133 aSettings.SetImmediate (Standard_True);
134 aSettings.SetEnvironmentTexture (Standard_True);
135 aSettings.SetEnableDepthTest (Standard_True);
136 aSettings.SetEnableDepthWrite (Standard_True);
137 aSettings.SetClearDepth (Standard_False);
138 aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
139 myLayerIds.Add (Graphic3d_ZLayerId_Top);
140 myLayerSeq.Append (Graphic3d_ZLayerId_Top);
141 myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Top, aSettings);
145 Graphic3d_ZLayerSettings aSettings;
146 aSettings.SetImmediate (Standard_True);
147 aSettings.SetEnvironmentTexture (Standard_True);
148 aSettings.SetEnableDepthTest (Standard_True);
149 aSettings.SetEnableDepthWrite (Standard_True);
150 aSettings.SetClearDepth (Standard_True);
151 aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
152 myLayerIds.Add (Graphic3d_ZLayerId_Topmost);
153 myLayerSeq.Append (Graphic3d_ZLayerId_Topmost);
154 myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Topmost, aSettings);
158 Graphic3d_ZLayerSettings aSettings;
159 aSettings.SetImmediate (Standard_True);
160 aSettings.SetEnvironmentTexture (Standard_False);
161 aSettings.SetEnableDepthTest (Standard_False);
162 aSettings.SetEnableDepthWrite (Standard_False);
163 aSettings.SetClearDepth (Standard_False);
164 aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
165 myLayerIds.Add (Graphic3d_ZLayerId_TopOSD);
166 myLayerSeq.Append (Graphic3d_ZLayerId_TopOSD);
167 myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_TopOSD, aSettings);
171 // =======================================================================
172 // function : ~OpenGl_GraphicDriver
174 // =======================================================================
175 OpenGl_GraphicDriver::~OpenGl_GraphicDriver()
180 // =======================================================================
181 // function : ReleaseContext
183 // =======================================================================
184 void OpenGl_GraphicDriver::ReleaseContext()
186 Handle(OpenGl_Context) aCtxShared;
187 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
188 aViewIter.More(); aViewIter.Next())
190 const Handle(OpenGl_View)& aView = aViewIter.Value();
191 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
192 if (aWindow.IsNull())
197 const Handle(OpenGl_Context)& aCtx = aWindow->GetGlContext();
198 if (aCtx->MakeCurrent()
199 && aCtxShared.IsNull())
205 if (!aCtxShared.IsNull())
207 aCtxShared->MakeCurrent();
209 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
210 aViewIter.More(); aViewIter.Next())
212 const Handle(OpenGl_View)& aView = aViewIter.Value();
213 aView->ReleaseGlResources (aCtxShared);
216 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
217 aStructIt.More (); aStructIt.Next())
219 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
220 aStruct->ReleaseGlResources (aCtxShared);
223 const bool isDeviceLost = !myMapOfStructure.IsEmpty();
224 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
225 aViewIter.More(); aViewIter.Next())
227 const Handle(OpenGl_View)& aView = aViewIter.Value();
230 aView->StructureManager()->SetDeviceLost();
233 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
234 if (aWindow.IsNull())
239 aWindow->GetGlContext()->forcedRelease();
242 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
245 if (myEglContext != (Aspect_RenderingContext )EGL_NO_CONTEXT)
247 if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) != EGL_TRUE)
249 ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, FAILED to release OpenGL context!", Message_Warning);
251 eglDestroyContext ((EGLDisplay )myEglDisplay, (EGLContext )myEglContext);
254 if (myEglDisplay != (Aspect_Display )EGL_NO_DISPLAY)
256 if (eglTerminate ((EGLDisplay )myEglDisplay) != EGL_TRUE)
258 ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, EGL, eglTerminate FAILED!", Message_Warning);
263 myEglDisplay = (Aspect_Display )EGL_NO_DISPLAY;
264 myEglContext = (Aspect_RenderingContext )EGL_NO_CONTEXT;
267 myIsOwnContext = Standard_False;
270 // =======================================================================
271 // function : InitContext
273 // =======================================================================
274 Standard_Boolean OpenGl_GraphicDriver::InitContext()
277 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
279 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
280 if (myDisplayConnection.IsNull())
282 return Standard_False;
284 Display* aDisplay = myDisplayConnection->GetDisplay();
285 myEglDisplay = (Aspect_Display )eglGetDisplay (aDisplay);
287 myEglDisplay = (Aspect_Display )eglGetDisplay (EGL_DEFAULT_DISPLAY);
289 if ((EGLDisplay )myEglDisplay == EGL_NO_DISPLAY)
291 ::Message::DefaultMessenger()->Send ("Error: no EGL display!", Message_Fail);
292 return Standard_False;
295 EGLint aVerMajor = 0; EGLint aVerMinor = 0;
296 if (eglInitialize ((EGLDisplay )myEglDisplay, &aVerMajor, &aVerMinor) != EGL_TRUE)
298 ::Message::DefaultMessenger()->Send ("Error: EGL display is unavailable!", Message_Fail);
299 return Standard_False;
302 EGLint aConfigAttribs[] =
310 #if defined(GL_ES_VERSION_2_0)
311 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
313 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
318 EGLint aNbConfigs = 0;
319 if (eglChooseConfig ((EGLDisplay )myEglDisplay, aConfigAttribs, &myEglConfig, 1, &aNbConfigs) != EGL_TRUE
320 || myEglConfig == NULL)
323 aConfigAttribs[4 * 2 + 1] = 16; // try config with smaller depth buffer
324 if (eglChooseConfig ((EGLDisplay )myEglDisplay, aConfigAttribs, &myEglConfig, 1, &aNbConfigs) != EGL_TRUE
325 || myEglConfig == NULL)
327 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide compatible configurations!", Message_Fail);
328 return Standard_False;
332 #if defined(GL_ES_VERSION_2_0)
333 EGLint anEglCtxAttribs[] =
335 EGL_CONTEXT_CLIENT_VERSION, 2,
338 if (eglBindAPI (EGL_OPENGL_ES_API) != EGL_TRUE)
340 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide OpenGL ES client!", Message_Fail);
341 return Standard_False;
344 EGLint* anEglCtxAttribs = NULL;
345 if (eglBindAPI (EGL_OPENGL_API) != EGL_TRUE)
347 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide OpenGL client!", Message_Fail);
348 return Standard_False;
352 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs);
353 if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
355 ::Message::DefaultMessenger()->Send ("Error: EGL is unable to create OpenGL context!", Message_Fail);
356 return Standard_False;
358 // eglMakeCurrent() fails or even crash with EGL_NO_SURFACE on some implementations
359 //if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, (EGLContext )myEglContext) != EGL_TRUE)
361 // ::Message::DefaultMessenger()->Send ("Error: EGL is unable bind OpenGL context!", Message_Fail);
362 // return Standard_False;
365 myIsOwnContext = Standard_True;
366 return Standard_True;
369 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
370 // =======================================================================
371 // function : InitEglContext
373 // =======================================================================
374 Standard_Boolean OpenGl_GraphicDriver::InitEglContext (Aspect_Display theEglDisplay,
375 Aspect_RenderingContext theEglContext,
379 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
380 if (myDisplayConnection.IsNull())
382 return Standard_False;
386 if ((EGLDisplay )theEglDisplay == EGL_NO_DISPLAY
387 || (EGLContext )theEglContext == EGL_NO_CONTEXT
388 || theEglConfig == NULL)
390 return Standard_False;
392 myEglDisplay = theEglDisplay;
393 myEglContext = theEglContext;
394 myEglConfig = theEglConfig;
395 return Standard_True;
399 // =======================================================================
400 // function : InquireLimit
402 // =======================================================================
403 Standard_Integer OpenGl_GraphicDriver::InquireLimit (const Graphic3d_TypeOfLimit theType) const
405 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
408 case Graphic3d_TypeOfLimit_MaxNbLights:
409 return OpenGLMaxLights;
410 case Graphic3d_TypeOfLimit_MaxNbClipPlanes:
411 return !aCtx.IsNull() ? aCtx->MaxClipPlanes() : 0;
412 case Graphic3d_TypeOfLimit_MaxNbViews:
414 case Graphic3d_TypeOfLimit_MaxTextureSize:
415 return !aCtx.IsNull() ? aCtx->MaxTextureSize() : 1024;
416 case Graphic3d_TypeOfLimit_MaxCombinedTextureUnits:
417 return !aCtx.IsNull() ? aCtx->MaxCombinedTextureUnits() : 1;
418 case Graphic3d_TypeOfLimit_MaxMsaa:
419 return !aCtx.IsNull() ? aCtx->MaxMsaaSamples() : 0;
420 case Graphic3d_TypeOfLimit_HasRayTracing:
421 return (!aCtx.IsNull() && aCtx->HasRayTracing()) ? 1 : 0;
422 case Graphic3d_TypeOfLimit_HasRayTracingTextures:
423 return (!aCtx.IsNull() && aCtx->HasRayTracingTextures()) ? 1 : 0;
424 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSampling:
425 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSampling()) ? 1 : 0;
426 case Graphic3d_TypeOfLimit_HasBlendedOit:
427 return (!aCtx.IsNull()
428 && aCtx->hasDrawBuffers != OpenGl_FeatureNotAvailable
429 && (aCtx->hasFloatBuffer != OpenGl_FeatureNotAvailable || aCtx->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable)) ? 1 : 0;
430 case Graphic3d_TypeOfLimit_HasBlendedOitMsaa:
431 return (!aCtx.IsNull()
432 && aCtx->hasSampleVariables != OpenGl_FeatureNotAvailable
433 && (InquireLimit (Graphic3d_TypeOfLimit_HasBlendedOit) == 1)) ? 1 : 0;
434 case Graphic3d_TypeOfLimit_NB:
440 // =======================================================================
441 // function : DefaultTextHeight
443 // =======================================================================
444 Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const
449 // =======================================================================
450 // function : EnableVBO
452 // =======================================================================
453 void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
455 myCaps->vboDisable = !theToTurnOn;
458 // =======================================================================
459 // function : GetSharedContext
461 // =======================================================================
462 const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext() const
464 if (myMapOfView.IsEmpty())
469 NCollection_Map<Handle(OpenGl_View)>::Iterator anIter (myMapOfView);
470 for (; anIter.More(); anIter.Next())
472 Handle(OpenGl_Window) aWindow = anIter.Value()->GlWindow();
473 if (aWindow.IsNull())
478 return aWindow->GetGlContext();
484 // =======================================================================
485 // function : MemoryInfo
487 // =======================================================================
488 Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes,
489 TCollection_AsciiString& theInfo) const
491 // this is extra work (for OpenGl_Context initialization)...
492 OpenGl_Context aGlCtx;
495 return Standard_False;
497 theFreeBytes = aGlCtx.AvailableMemory();
498 theInfo = aGlCtx.MemoryInfo();
499 return !theInfo.IsEmpty();
502 // =======================================================================
503 // function : SetBuffersNoSwap
505 // =======================================================================
506 void OpenGl_GraphicDriver::SetBuffersNoSwap (const Standard_Boolean theIsNoSwap)
508 myCaps->buffersNoSwap = theIsNoSwap;
511 // =======================================================================
512 // function : TextSize
514 // =======================================================================
515 void OpenGl_GraphicDriver::TextSize (const Handle(Graphic3d_CView)& theView,
516 const Standard_CString theText,
517 const Standard_ShortReal theHeight,
518 Standard_ShortReal& theWidth,
519 Standard_ShortReal& theAscent,
520 Standard_ShortReal& theDescent) const
522 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
528 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
529 OpenGl_TextParam aTextParam;
530 aTextParam.Height = (int )aHeight;
531 OpenGl_AspectText aTextAspect;
532 aTextAspect.Aspect()->SetSpace (0.3);
533 TCollection_ExtendedString anExtText = theText;
534 NCollection_String aText (anExtText.ToExtString());
535 OpenGl_Text::StringSize(aCtx, aText, aTextAspect, aTextParam, theView->RenderingParams().Resolution, theWidth, theAscent, theDescent);
538 //=======================================================================
539 //function : addZLayerIndex
541 //=======================================================================
542 void OpenGl_GraphicDriver::addZLayerIndex (const Graphic3d_ZLayerId theLayerId)
545 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
547 if (aLayerIt.Value() == theLayerId)
549 myLayerSeq.Remove (aLayerIt);
554 if (myMapOfZLayerSettings.Find (theLayerId).IsImmediate())
556 myLayerSeq.Append (theLayerId);
560 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
562 const Graphic3d_ZLayerSettings& aSettings = myMapOfZLayerSettings.Find (aLayerIt.Value());
563 if (aSettings.IsImmediate())
568 myLayerSeq.InsertAfter (aLayerIt, theLayerId);
572 // first non-immediate layer
573 myLayerSeq.Prepend (theLayerId);
578 // no immediate layers
579 myLayerSeq.Append (theLayerId);
582 //=======================================================================
583 //function : AddZLayer
585 //=======================================================================
586 void OpenGl_GraphicDriver::AddZLayer (const Graphic3d_ZLayerId theLayerId)
590 Standard_ASSERT_RAISE (theLayerId > 0,
591 "OpenGl_GraphicDriver::AddZLayer, "
592 "negative and zero IDs are reserved");
595 myLayerIds.Add (theLayerId);
597 // Default z-layer settings
598 myMapOfZLayerSettings.Bind (theLayerId, Graphic3d_ZLayerSettings());
599 addZLayerIndex (theLayerId);
601 // Add layer to all views
602 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
603 for (; aViewIt.More(); aViewIt.Next())
605 aViewIt.Value()->AddZLayer (theLayerId);
609 //=======================================================================
610 //function : RemoveZLayer
612 //=======================================================================
613 void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
615 Standard_ASSERT_RAISE (theLayerId > 0,
616 "OpenGl_GraphicDriver::AddZLayer, "
617 "negative and zero IDs are reserved"
618 "and can not be removed");
620 Standard_ASSERT_RAISE (myLayerIds.Contains (theLayerId),
621 "OpenGl_GraphicDriver::RemoveZLayer, "
622 "Layer with theLayerId does not exist");
624 // Remove layer from all of the views
625 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
626 for (; aViewIt.More(); aViewIt.Next())
628 aViewIt.Value()->RemoveZLayer (theLayerId);
631 // Unset Z layer for all of the structures.
632 NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
633 for( ; aStructIt.More (); aStructIt.Next ())
635 OpenGl_Structure* aStruct = aStructIt.ChangeValue ();
636 if (aStruct->ZLayer() == theLayerId)
637 aStruct->SetZLayer (Graphic3d_ZLayerId_Default);
641 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
643 if (aLayerIt.Value() == theLayerId)
645 myLayerSeq.Remove (aLayerIt);
650 myMapOfZLayerSettings.UnBind (theLayerId);
651 myLayerIds.Remove (theLayerId);
654 //=======================================================================
657 //=======================================================================
658 void OpenGl_GraphicDriver::ZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
660 theLayerSeq.Assign (myLayerSeq);
663 //=======================================================================
664 //function : SetZLayerSettings
666 //=======================================================================
667 void OpenGl_GraphicDriver::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
668 const Graphic3d_ZLayerSettings& theSettings)
670 Graphic3d_ZLayerSettings* aSettings = myMapOfZLayerSettings.ChangeSeek (theLayerId);
671 if (aSettings != NULL)
673 const bool isChanged = (aSettings->IsImmediate() != theSettings.IsImmediate());
674 *aSettings = theSettings;
677 addZLayerIndex (theLayerId);
683 myMapOfZLayerSettings.Bind (theLayerId, theSettings);
684 addZLayerIndex (theLayerId);
687 // Change Z layer settings in all managed views
688 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
690 aViewIt.Value()->SetZLayerSettings (theLayerId, theSettings);
694 //=======================================================================
695 //function : ZLayerSettings
697 //=======================================================================
698 const Graphic3d_ZLayerSettings& OpenGl_GraphicDriver::ZLayerSettings (const Graphic3d_ZLayerId theLayerId) const
700 Standard_ASSERT_RAISE (myLayerIds.Contains (theLayerId),
701 "OpenGl_GraphicDriver::ZLayerSettings, "
702 "Layer with theLayerId does not exist");
704 return myMapOfZLayerSettings.Find (theLayerId);
707 // =======================================================================
708 // function : Structure
710 // =======================================================================
711 Handle(Graphic3d_CStructure) OpenGl_GraphicDriver::CreateStructure (const Handle(Graphic3d_StructureManager)& theManager)
713 Handle(OpenGl_Structure) aStructure = new OpenGl_Structure (theManager);
714 myMapOfStructure.Bind (aStructure->Id, aStructure.operator->());
718 // =======================================================================
719 // function : Structure
721 // =======================================================================
722 void OpenGl_GraphicDriver::RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure)
724 OpenGl_Structure* aStructure = NULL;
725 if (!myMapOfStructure.Find (theCStructure->Id, aStructure))
730 myMapOfStructure.UnBind (theCStructure->Id);
731 aStructure->Release (GetSharedContext());
732 theCStructure.Nullify();
735 // =======================================================================
738 // =======================================================================
739 Handle(Graphic3d_CView) OpenGl_GraphicDriver::CreateView (const Handle(Graphic3d_StructureManager)& theMgr)
741 Handle(OpenGl_View) aView = new OpenGl_View (theMgr, this, myCaps, &myStateCounter);
743 myMapOfView.Add (aView);
745 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
747 const Graphic3d_ZLayerId aLayerID = aLayerIt.Value();
748 const Graphic3d_ZLayerSettings& aSettings = myMapOfZLayerSettings.Find (aLayerID);
749 aView->AddZLayer (aLayerID);
750 aView->SetZLayerSettings (aLayerID, aSettings);
756 // =======================================================================
757 // function : RemoveView
759 // =======================================================================
760 void OpenGl_GraphicDriver::RemoveView (const Handle(Graphic3d_CView)& theView)
762 Handle(OpenGl_Context) aCtx = GetSharedContext();
763 Handle(OpenGl_View) aView = Handle(OpenGl_View)::DownCast (theView);
769 if (!myMapOfView.Remove (aView))
774 Handle(OpenGl_Window) aWindow = aView->GlWindow();
775 if (!aWindow.IsNull()
776 && aWindow->GetGlContext()->MakeCurrent())
778 aCtx = aWindow->GetGlContext();
782 // try to hijack another context if any
783 const Handle(OpenGl_Context)& anOtherCtx = GetSharedContext();
784 if (!anOtherCtx.IsNull()
785 && anOtherCtx != aWindow->GetGlContext())
792 aView->ReleaseGlResources (aCtx);
793 if (myMapOfView.IsEmpty())
795 // The last view removed but some objects still present.
796 // Release GL resources now without object destruction.
797 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
798 aStructIt.More (); aStructIt.Next())
800 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
801 aStruct->ReleaseGlResources (aCtx);
804 if (!myMapOfStructure.IsEmpty())
806 aView->StructureManager()->SetDeviceLost();
811 // =======================================================================
814 // =======================================================================
815 Handle(OpenGl_Window) OpenGl_GraphicDriver::CreateRenderWindow (const Handle(Aspect_Window)& theWindow,
816 const Aspect_RenderingContext theContext)
818 Handle(OpenGl_Context) aShareCtx = GetSharedContext();
819 Handle(OpenGl_Window) aWindow = new OpenGl_Window (this, theWindow, theContext, myCaps, aShareCtx);
823 //=======================================================================
824 //function : ViewExists
826 //=======================================================================
827 Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)& AWindow, Handle(Graphic3d_CView)& theView)
829 Standard_Boolean isExist = Standard_False;
831 // Parse the list of views to find
832 // a view with the specified window
834 #if defined(_WIN32) && !defined(OCCT_UWP)
835 const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
836 Aspect_Handle TheSpecifiedWindowId = THEWindow->HWindow ();
837 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
838 const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow);
839 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
840 UIView* TheSpecifiedWindowId = THEWindow->HView();
842 NSView* TheSpecifiedWindowId = THEWindow->HView();
844 #elif defined(__ANDROID__) || defined(__QNX__) || defined(OCCT_UWP)
846 int TheSpecifiedWindowId = -1;
848 const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow);
849 int TheSpecifiedWindowId = int (THEWindow->XWindow ());
852 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
853 for(; aViewIt.More(); aViewIt.Next())
855 const Handle(OpenGl_View)& aView = aViewIt.Value();
856 if (aView->IsDefined() && aView->IsActive())
858 const Handle(Aspect_Window) AspectWindow = aView->Window();
860 #if defined(_WIN32) && !defined(OCCT_UWP)
861 const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
862 Aspect_Handle TheWindowIdOfView = theWindow->HWindow ();
863 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
864 const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow);
865 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
866 UIView* TheWindowIdOfView = theWindow->HView();
868 NSView* TheWindowIdOfView = theWindow->HView();
870 #elif defined(__ANDROID__) || defined(__QNX__) || defined(OCCT_UWP)
871 int TheWindowIdOfView = 0;
873 const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
874 int TheWindowIdOfView = int (theWindow->XWindow ());
876 // Comparaison on window IDs
877 if (TheWindowIdOfView == TheSpecifiedWindowId)
879 isExist = Standard_True;
888 //=======================================================================
889 //function : setDeviceLost
891 //=======================================================================
892 void OpenGl_GraphicDriver::setDeviceLost()
894 if (myMapOfStructure.IsEmpty())
899 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); aViewIter.More(); aViewIter.Next())
901 const Handle(OpenGl_View)& aView = aViewIter.Value();
902 if (aView->myWasRedrawnGL)
904 aView->StructureManager()->SetDeviceLost();