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 <Message_Messenger.hxx>
33 #include <OSD_Environment.hxx>
34 #include <Standard_NotImplemented.hxx>
36 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
39 #include <WNT_Window.hxx>
40 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
41 #include <Cocoa_Window.hxx>
43 #include <Xw_Window.hxx>
46 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
47 #include <X11/Xlib.h> // XOpenDisplay()
50 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
56 static const Handle(OpenGl_Context) TheNullGlCtx;
59 // =======================================================================
60 // function : OpenGl_GraphicDriver
62 // =======================================================================
63 OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp,
64 const Standard_Boolean theToInitialize)
65 : Graphic3d_GraphicDriver (theDisp),
66 myIsOwnContext (Standard_False),
67 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
68 myEglDisplay ((Aspect_Display )EGL_NO_DISPLAY),
69 myEglContext ((Aspect_RenderingContext )EGL_NO_CONTEXT),
72 myCaps (new OpenGl_Caps()),
73 myMapOfView (1, NCollection_BaseAllocator::CommonBaseAllocator()),
74 myMapOfStructure (1, NCollection_BaseAllocator::CommonBaseAllocator())
76 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
77 if (myDisplayConnection.IsNull())
79 //throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: cannot connect to X server!");
83 Display* aDisplay = myDisplayConnection->GetDisplay();
84 Bool toSync = ::getenv ("CSF_GraphicSync") != NULL
85 || ::getenv ("CALL_SYNCHRO_X") != NULL;
86 XSynchronize (aDisplay, toSync);
88 #if !defined(HAVE_EGL) && !defined(HAVE_GLES2)
89 // does the server know about OpenGL & GLX?
91 if (!XQueryExtension (aDisplay, "GLX", &aDummy, &aDummy, &aDummy))
93 ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, this system doesn't appear to support OpenGL!", Message_Warning);
100 throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: default context can not be initialized!");
103 // default layers are always presented in display layer sequence it can not be removed
105 Graphic3d_ZLayerSettings aSettings;
106 aSettings.SetImmediate (Standard_False);
107 aSettings.SetEnvironmentTexture (Standard_False);
108 aSettings.SetEnableDepthTest (Standard_False);
109 aSettings.SetEnableDepthWrite (Standard_False);
110 aSettings.SetClearDepth (Standard_False);
111 aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
112 myLayerIds.Add (Graphic3d_ZLayerId_BotOSD);
113 myLayerSeq.Append (Graphic3d_ZLayerId_BotOSD);
114 myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_BotOSD, aSettings);
118 Graphic3d_ZLayerSettings aSettings;
119 aSettings.SetImmediate (Standard_False);
120 aSettings.SetEnvironmentTexture (Standard_True);
121 aSettings.SetEnableDepthTest (Standard_True);
122 aSettings.SetEnableDepthWrite (Standard_True);
123 aSettings.SetClearDepth (Standard_False);
124 aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
125 myLayerIds.Add (Graphic3d_ZLayerId_Default);
126 myLayerSeq.Append (Graphic3d_ZLayerId_Default);
127 myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Default, aSettings);
131 Graphic3d_ZLayerSettings aSettings;
132 aSettings.SetImmediate (Standard_True);
133 aSettings.SetEnvironmentTexture (Standard_True);
134 aSettings.SetEnableDepthTest (Standard_True);
135 aSettings.SetEnableDepthWrite (Standard_True);
136 aSettings.SetClearDepth (Standard_False);
137 aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
138 myLayerIds.Add (Graphic3d_ZLayerId_Top);
139 myLayerSeq.Append (Graphic3d_ZLayerId_Top);
140 myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Top, aSettings);
144 Graphic3d_ZLayerSettings aSettings;
145 aSettings.SetImmediate (Standard_True);
146 aSettings.SetEnvironmentTexture (Standard_True);
147 aSettings.SetEnableDepthTest (Standard_True);
148 aSettings.SetEnableDepthWrite (Standard_True);
149 aSettings.SetClearDepth (Standard_True);
150 aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
151 myLayerIds.Add (Graphic3d_ZLayerId_Topmost);
152 myLayerSeq.Append (Graphic3d_ZLayerId_Topmost);
153 myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Topmost, aSettings);
157 Graphic3d_ZLayerSettings aSettings;
158 aSettings.SetImmediate (Standard_True);
159 aSettings.SetEnvironmentTexture (Standard_False);
160 aSettings.SetEnableDepthTest (Standard_False);
161 aSettings.SetEnableDepthWrite (Standard_False);
162 aSettings.SetClearDepth (Standard_False);
163 aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
164 myLayerIds.Add (Graphic3d_ZLayerId_TopOSD);
165 myLayerSeq.Append (Graphic3d_ZLayerId_TopOSD);
166 myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_TopOSD, aSettings);
170 // =======================================================================
171 // function : ~OpenGl_GraphicDriver
173 // =======================================================================
174 OpenGl_GraphicDriver::~OpenGl_GraphicDriver()
179 // =======================================================================
180 // function : ReleaseContext
182 // =======================================================================
183 void OpenGl_GraphicDriver::ReleaseContext()
185 Handle(OpenGl_Context) aCtxShared;
186 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
187 aViewIter.More(); aViewIter.Next())
189 const Handle(OpenGl_View)& aView = aViewIter.Value();
190 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
191 if (aWindow.IsNull())
196 const Handle(OpenGl_Context)& aCtx = aWindow->GetGlContext();
197 if (aCtx->MakeCurrent()
198 && aCtxShared.IsNull())
204 if (!aCtxShared.IsNull())
206 aCtxShared->MakeCurrent();
208 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
209 aViewIter.More(); aViewIter.Next())
211 const Handle(OpenGl_View)& aView = aViewIter.Value();
212 aView->ReleaseGlResources (aCtxShared);
215 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
216 aStructIt.More (); aStructIt.Next())
218 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
219 aStruct->ReleaseGlResources (aCtxShared);
221 myDeviceLostFlag = myDeviceLostFlag || !myMapOfStructure.IsEmpty();
223 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
224 aViewIter.More(); aViewIter.Next())
226 const Handle(OpenGl_View)& aView = aViewIter.Value();
227 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
228 if (aWindow.IsNull())
233 aWindow->GetGlContext()->forcedRelease();
236 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
239 if (myEglContext != (Aspect_RenderingContext )EGL_NO_CONTEXT)
241 if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) != EGL_TRUE)
243 ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, FAILED to release OpenGL context!", Message_Warning);
245 eglDestroyContext ((EGLDisplay )myEglDisplay, (EGLContext )myEglContext);
248 if (myEglDisplay != (Aspect_Display )EGL_NO_DISPLAY)
250 if (eglTerminate ((EGLDisplay )myEglDisplay) != EGL_TRUE)
252 ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, EGL, eglTerminate FAILED!", Message_Warning);
257 myEglDisplay = (Aspect_Display )EGL_NO_DISPLAY;
258 myEglContext = (Aspect_RenderingContext )EGL_NO_CONTEXT;
261 myIsOwnContext = Standard_False;
264 // =======================================================================
265 // function : InitContext
267 // =======================================================================
268 Standard_Boolean OpenGl_GraphicDriver::InitContext()
271 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
273 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
274 if (myDisplayConnection.IsNull())
276 return Standard_False;
278 Display* aDisplay = myDisplayConnection->GetDisplay();
279 myEglDisplay = (Aspect_Display )eglGetDisplay (aDisplay);
281 myEglDisplay = (Aspect_Display )eglGetDisplay (EGL_DEFAULT_DISPLAY);
283 if ((EGLDisplay )myEglDisplay == EGL_NO_DISPLAY)
285 ::Message::DefaultMessenger()->Send ("Error: no EGL display!", Message_Fail);
286 return Standard_False;
289 EGLint aVerMajor = 0; EGLint aVerMinor = 0;
290 if (eglInitialize ((EGLDisplay )myEglDisplay, &aVerMajor, &aVerMinor) != EGL_TRUE)
292 ::Message::DefaultMessenger()->Send ("Error: EGL display is unavailable!", Message_Fail);
293 return Standard_False;
296 EGLint aConfigAttribs[] =
304 #if defined(GL_ES_VERSION_2_0)
305 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
307 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
312 EGLint aNbConfigs = 0;
313 if (eglChooseConfig ((EGLDisplay )myEglDisplay, aConfigAttribs, &myEglConfig, 1, &aNbConfigs) != EGL_TRUE
314 || myEglConfig == NULL)
317 aConfigAttribs[4 * 2 + 1] = 16; // try config with smaller depth buffer
318 if (eglChooseConfig ((EGLDisplay )myEglDisplay, aConfigAttribs, &myEglConfig, 1, &aNbConfigs) != EGL_TRUE
319 || myEglConfig == NULL)
321 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide compatible configurations!", Message_Fail);
322 return Standard_False;
326 #if defined(GL_ES_VERSION_2_0)
327 EGLint anEglCtxAttribs[] =
329 EGL_CONTEXT_CLIENT_VERSION, 2,
332 if (eglBindAPI (EGL_OPENGL_ES_API) != EGL_TRUE)
334 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide OpenGL ES client!", Message_Fail);
335 return Standard_False;
338 EGLint* anEglCtxAttribs = NULL;
339 if (eglBindAPI (EGL_OPENGL_API) != EGL_TRUE)
341 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide OpenGL client!", Message_Fail);
342 return Standard_False;
346 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs);
347 if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
349 ::Message::DefaultMessenger()->Send ("Error: EGL is unable to create OpenGL context!", Message_Fail);
350 return Standard_False;
352 // eglMakeCurrent() fails or even crash with EGL_NO_SURFACE on some implementations
353 //if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, (EGLContext )myEglContext) != EGL_TRUE)
355 // ::Message::DefaultMessenger()->Send ("Error: EGL is unable bind OpenGL context!", Message_Fail);
356 // return Standard_False;
359 myIsOwnContext = Standard_True;
360 return Standard_True;
363 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
364 // =======================================================================
365 // function : InitEglContext
367 // =======================================================================
368 Standard_Boolean OpenGl_GraphicDriver::InitEglContext (Aspect_Display theEglDisplay,
369 Aspect_RenderingContext theEglContext,
373 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
374 if (myDisplayConnection.IsNull())
376 return Standard_False;
380 if ((EGLDisplay )theEglDisplay == EGL_NO_DISPLAY
381 || (EGLContext )theEglContext == EGL_NO_CONTEXT
382 || theEglConfig == NULL)
384 return Standard_False;
386 myEglDisplay = theEglDisplay;
387 myEglContext = theEglContext;
388 myEglConfig = theEglConfig;
389 return Standard_True;
393 // =======================================================================
394 // function : InquireLimit
396 // =======================================================================
397 Standard_Integer OpenGl_GraphicDriver::InquireLimit (const Graphic3d_TypeOfLimit theType) const
399 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
402 case Graphic3d_TypeOfLimit_MaxNbLights:
403 return OpenGLMaxLights;
404 case Graphic3d_TypeOfLimit_MaxNbClipPlanes:
405 return !aCtx.IsNull() ? aCtx->MaxClipPlanes() : 0;
406 case Graphic3d_TypeOfLimit_MaxNbViews:
408 case Graphic3d_TypeOfLimit_MaxTextureSize:
409 return !aCtx.IsNull() ? aCtx->MaxTextureSize() : 1024;
410 case Graphic3d_TypeOfLimit_MaxMsaa:
411 return !aCtx.IsNull() ? aCtx->MaxMsaaSamples() : 0;
412 case Graphic3d_TypeOfLimit_HasRayTracing:
413 return (!aCtx.IsNull() && aCtx->HasRayTracing()) ? 1 : 0;
414 case Graphic3d_TypeOfLimit_HasRayTracingTextures:
415 return (!aCtx.IsNull() && aCtx->HasRayTracingTextures()) ? 1 : 0;
416 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSampling:
417 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSampling()) ? 1 : 0;
418 case Graphic3d_TypeOfLimit_HasBlendedOit:
419 return (!aCtx.IsNull() && aCtx->hasDrawBuffers && (aCtx->hasFloatBuffer || aCtx->hasHalfFloatBuffer)) ? 1 : 0;
420 case Graphic3d_TypeOfLimit_HasBlendedOitMsaa:
421 return (!aCtx.IsNull() && aCtx->hasSampleVariables && (InquireLimit (Graphic3d_TypeOfLimit_HasBlendedOit) == 1)) ? 1 : 0;
422 case Graphic3d_TypeOfLimit_NB:
428 // =======================================================================
429 // function : DefaultTextHeight
431 // =======================================================================
432 Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const
437 // =======================================================================
438 // function : EnableVBO
440 // =======================================================================
441 void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
443 myCaps->vboDisable = !theToTurnOn;
446 // =======================================================================
447 // function : GetSharedContext
449 // =======================================================================
450 const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext() const
452 if (myMapOfView.IsEmpty())
457 NCollection_Map<Handle(OpenGl_View)>::Iterator anIter (myMapOfView);
458 for (; anIter.More(); anIter.Next())
460 Handle(OpenGl_Window) aWindow = anIter.Value()->GlWindow();
461 if (aWindow.IsNull())
466 return aWindow->GetGlContext();
472 // =======================================================================
473 // function : MemoryInfo
475 // =======================================================================
476 Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes,
477 TCollection_AsciiString& theInfo) const
479 // this is extra work (for OpenGl_Context initialization)...
480 OpenGl_Context aGlCtx;
483 return Standard_False;
485 theFreeBytes = aGlCtx.AvailableMemory();
486 theInfo = aGlCtx.MemoryInfo();
487 return !theInfo.IsEmpty();
490 // =======================================================================
491 // function : SetBuffersNoSwap
493 // =======================================================================
494 void OpenGl_GraphicDriver::SetBuffersNoSwap (const Standard_Boolean theIsNoSwap)
496 myCaps->buffersNoSwap = theIsNoSwap;
499 // =======================================================================
500 // function : TextSize
502 // =======================================================================
503 void OpenGl_GraphicDriver::TextSize (const Handle(Graphic3d_CView)& theView,
504 const Standard_CString theText,
505 const Standard_ShortReal theHeight,
506 Standard_ShortReal& theWidth,
507 Standard_ShortReal& theAscent,
508 Standard_ShortReal& theDescent) const
510 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
516 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
517 OpenGl_TextParam aTextParam;
518 aTextParam.Height = (int )aHeight;
519 OpenGl_AspectText aTextAspect;
520 aTextAspect.Aspect()->SetSpace (0.3);
521 TCollection_ExtendedString anExtText = theText;
522 NCollection_String aText (anExtText.ToExtString());
523 OpenGl_Text::StringSize(aCtx, aText, aTextAspect, aTextParam, theView->RenderingParams().Resolution, theWidth, theAscent, theDescent);
526 //=======================================================================
527 //function : addZLayerIndex
529 //=======================================================================
530 void OpenGl_GraphicDriver::addZLayerIndex (const Graphic3d_ZLayerId theLayerId)
533 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
535 if (aLayerIt.Value() == theLayerId)
537 myLayerSeq.Remove (aLayerIt);
542 if (myMapOfZLayerSettings.Find (theLayerId).IsImmediate())
544 myLayerSeq.Append (theLayerId);
548 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
550 const Graphic3d_ZLayerSettings& aSettings = myMapOfZLayerSettings.Find (aLayerIt.Value());
551 if (aSettings.IsImmediate())
556 myLayerSeq.InsertAfter (aLayerIt, theLayerId);
560 // first non-immediate layer
561 myLayerSeq.Prepend (theLayerId);
566 // no immediate layers
567 myLayerSeq.Append (theLayerId);
570 //=======================================================================
571 //function : AddZLayer
573 //=======================================================================
574 void OpenGl_GraphicDriver::AddZLayer (const Graphic3d_ZLayerId theLayerId)
578 Standard_ASSERT_RAISE (theLayerId > 0,
579 "OpenGl_GraphicDriver::AddZLayer, "
580 "negative and zero IDs are reserved");
583 myLayerIds.Add (theLayerId);
585 // Default z-layer settings
586 myMapOfZLayerSettings.Bind (theLayerId, Graphic3d_ZLayerSettings());
587 addZLayerIndex (theLayerId);
589 // Add layer to all views
590 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
591 for (; aViewIt.More(); aViewIt.Next())
593 aViewIt.Value()->AddZLayer (theLayerId);
597 //=======================================================================
598 //function : RemoveZLayer
600 //=======================================================================
601 void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
603 Standard_ASSERT_RAISE (theLayerId > 0,
604 "OpenGl_GraphicDriver::AddZLayer, "
605 "negative and zero IDs are reserved"
606 "and can not be removed");
608 Standard_ASSERT_RAISE (myLayerIds.Contains (theLayerId),
609 "OpenGl_GraphicDriver::RemoveZLayer, "
610 "Layer with theLayerId does not exist");
612 // Remove layer from all of the views
613 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
614 for (; aViewIt.More(); aViewIt.Next())
616 aViewIt.Value()->RemoveZLayer (theLayerId);
619 // Unset Z layer for all of the structures.
620 NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
621 for( ; aStructIt.More (); aStructIt.Next ())
623 OpenGl_Structure* aStruct = aStructIt.ChangeValue ();
624 if (aStruct->ZLayer() == theLayerId)
625 aStruct->SetZLayer (Graphic3d_ZLayerId_Default);
629 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
631 if (aLayerIt.Value() == theLayerId)
633 myLayerSeq.Remove (aLayerIt);
638 myMapOfZLayerSettings.UnBind (theLayerId);
639 myLayerIds.Remove (theLayerId);
642 //=======================================================================
645 //=======================================================================
646 void OpenGl_GraphicDriver::ZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
648 theLayerSeq.Assign (myLayerSeq);
651 //=======================================================================
652 //function : SetZLayerSettings
654 //=======================================================================
655 void OpenGl_GraphicDriver::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
656 const Graphic3d_ZLayerSettings& theSettings)
658 Graphic3d_ZLayerSettings* aSettings = myMapOfZLayerSettings.ChangeSeek (theLayerId);
659 if (aSettings != NULL)
661 const bool isChanged = (aSettings->IsImmediate() != theSettings.IsImmediate());
662 *aSettings = theSettings;
665 addZLayerIndex (theLayerId);
671 myMapOfZLayerSettings.Bind (theLayerId, theSettings);
672 addZLayerIndex (theLayerId);
675 // Change Z layer settings in all managed views
676 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
678 aViewIt.Value()->SetZLayerSettings (theLayerId, theSettings);
682 //=======================================================================
683 //function : ZLayerSettings
685 //=======================================================================
686 const Graphic3d_ZLayerSettings& OpenGl_GraphicDriver::ZLayerSettings (const Graphic3d_ZLayerId theLayerId) const
688 Standard_ASSERT_RAISE (myLayerIds.Contains (theLayerId),
689 "OpenGl_GraphicDriver::ZLayerSettings, "
690 "Layer with theLayerId does not exist");
692 return myMapOfZLayerSettings.Find (theLayerId);
695 // =======================================================================
696 // function : Structure
698 // =======================================================================
699 Handle(Graphic3d_CStructure) OpenGl_GraphicDriver::CreateStructure (const Handle(Graphic3d_StructureManager)& theManager)
701 Handle(OpenGl_Structure) aStructure = new OpenGl_Structure (theManager);
702 myMapOfStructure.Bind (aStructure->Id, aStructure.operator->());
706 // =======================================================================
707 // function : Structure
709 // =======================================================================
710 void OpenGl_GraphicDriver::RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure)
712 OpenGl_Structure* aStructure = NULL;
713 if (!myMapOfStructure.Find (theCStructure->Id, aStructure))
718 myMapOfStructure.UnBind (theCStructure->Id);
719 aStructure->Release (GetSharedContext());
720 theCStructure.Nullify();
723 // =======================================================================
726 // =======================================================================
727 Handle(Graphic3d_CView) OpenGl_GraphicDriver::CreateView (const Handle(Graphic3d_StructureManager)& theMgr)
729 Handle(OpenGl_View) aView = new OpenGl_View (theMgr, this, myCaps, myDeviceLostFlag, &myStateCounter);
731 myMapOfView.Add (aView);
733 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
735 const Graphic3d_ZLayerId aLayerID = aLayerIt.Value();
736 const Graphic3d_ZLayerSettings& aSettings = myMapOfZLayerSettings.Find (aLayerID);
737 aView->AddZLayer (aLayerID);
738 aView->SetZLayerSettings (aLayerID, aSettings);
744 // =======================================================================
745 // function : RemoveView
747 // =======================================================================
748 void OpenGl_GraphicDriver::RemoveView (const Handle(Graphic3d_CView)& theView)
750 Handle(OpenGl_Context) aCtx = GetSharedContext();
751 Handle(OpenGl_View) aView = Handle(OpenGl_View)::DownCast (theView);
757 if (!myMapOfView.Remove (aView))
762 Handle(OpenGl_Window) aWindow = aView->GlWindow();
763 if (!aWindow.IsNull()
764 && aWindow->GetGlContext()->MakeCurrent())
766 aCtx = aWindow->GetGlContext();
770 // try to hijack another context if any
771 const Handle(OpenGl_Context)& anOtherCtx = GetSharedContext();
772 if (!anOtherCtx.IsNull()
773 && anOtherCtx != aWindow->GetGlContext())
780 aView->ReleaseGlResources (aCtx);
781 if (myMapOfView.IsEmpty())
783 // The last view removed but some objects still present.
784 // Release GL resources now without object destruction.
785 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
786 aStructIt.More (); aStructIt.Next())
788 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
789 aStruct->ReleaseGlResources (aCtx);
791 myDeviceLostFlag = !myMapOfStructure.IsEmpty();
795 // =======================================================================
798 // =======================================================================
799 Handle(OpenGl_Window) OpenGl_GraphicDriver::CreateRenderWindow (const Handle(Aspect_Window)& theWindow,
800 const Aspect_RenderingContext theContext)
802 Handle(OpenGl_Context) aShareCtx = GetSharedContext();
803 Handle(OpenGl_Window) aWindow = new OpenGl_Window (this, theWindow, theContext, myCaps, aShareCtx);
807 //=======================================================================
808 //function : ViewExists
810 //=======================================================================
811 Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)& AWindow, Handle(Graphic3d_CView)& theView)
813 Standard_Boolean isExist = Standard_False;
815 // Parse the list of views to find
816 // a view with the specified window
818 #if defined(_WIN32) && !defined(OCCT_UWP)
819 const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
820 Aspect_Handle TheSpecifiedWindowId = THEWindow->HWindow ();
821 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
822 const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow);
823 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
824 UIView* TheSpecifiedWindowId = THEWindow->HView();
826 NSView* TheSpecifiedWindowId = THEWindow->HView();
828 #elif defined(__ANDROID__) || defined(__QNX__) || defined(OCCT_UWP)
830 int TheSpecifiedWindowId = -1;
832 const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow);
833 int TheSpecifiedWindowId = int (THEWindow->XWindow ());
836 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
837 for(; aViewIt.More(); aViewIt.Next())
839 const Handle(OpenGl_View)& aView = aViewIt.Value();
840 if (aView->IsDefined() && aView->IsActive())
842 const Handle(Aspect_Window) AspectWindow = aView->Window();
844 #if defined(_WIN32) && !defined(OCCT_UWP)
845 const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
846 Aspect_Handle TheWindowIdOfView = theWindow->HWindow ();
847 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
848 const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow);
849 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
850 UIView* TheWindowIdOfView = theWindow->HView();
852 NSView* TheWindowIdOfView = theWindow->HView();
854 #elif defined(__ANDROID__) || defined(__QNX__) || defined(OCCT_UWP)
855 int TheWindowIdOfView = 0;
857 const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
858 int TheWindowIdOfView = int (theWindow->XWindow ());
860 // Comparaison on window IDs
861 if (TheWindowIdOfView == TheSpecifiedWindowId)
863 isExist = Standard_True;