0030698: Volume Rendering - Early clipping of volume object's bounding box
[occt.git] / src / OpenGl / OpenGl_GraphicDriver.cxx
CommitLineData
b311480e 1// Created on: 2011-10-20
2// Created by: Sergey ZERCHANINOV
1981cb22 3// Copyright (c) 2011-2013 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
1ce0716b 16#if defined(_WIN32)
17 #include <windows.h>
18#endif
19
2166f0fa 20#include <OpenGl_GraphicDriver.hxx>
f0430952 21#include <OpenGl_Context.hxx>
938d4544 22#include <OpenGl_Flipper.hxx>
a174a3c5 23#include <OpenGl_GraduatedTrihedron.hxx>
24#include <OpenGl_Group.hxx>
2166f0fa 25#include <OpenGl_View.hxx>
a6eb515f 26#include <OpenGl_StencilTest.hxx>
a174a3c5 27#include <OpenGl_Text.hxx>
2166f0fa 28#include <OpenGl_Workspace.hxx>
7fd59977 29
25b97fac 30#include <Aspect_GraphicDeviceDefinitionError.hxx>
c357e426 31#include <Aspect_IdentDefinitionError.hxx>
851dacdb 32#include <Graphic3d_StructureManager.hxx>
25b97fac 33#include <Message_Messenger.hxx>
73192b37 34#include <OSD_Environment.hxx>
a174a3c5 35#include <Standard_NotImplemented.hxx>
36
92efcf78 37IMPLEMENT_STANDARD_RTTIEXT(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
38
c357e426 39#if defined(_WIN32)
40 #include <WNT_Window.hxx>
41#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
42 #include <Cocoa_Window.hxx>
43#else
44 #include <Xw_Window.hxx>
45#endif
46
d8d01f6e 47#if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
73192b37 48 #include <X11/Xlib.h> // XOpenDisplay()
49#endif
50
1ce0716b 51#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
25b97fac 52 #include <EGL/egl.h>
53#endif
54
2166f0fa
SK
55namespace
56{
5e27df78 57 static const Handle(OpenGl_Context) TheNullGlCtx;
39235bed 58
59#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
60 //! Wrapper over eglChooseConfig() called with preferred defaults.
61 static EGLConfig chooseEglSurfConfig (EGLDisplay theDisplay)
62 {
63 EGLint aConfigAttribs[] =
64 {
65 EGL_RED_SIZE, 8,
66 EGL_GREEN_SIZE, 8,
67 EGL_BLUE_SIZE, 8,
68 EGL_ALPHA_SIZE, 0,
69 EGL_DEPTH_SIZE, 24,
70 EGL_STENCIL_SIZE, 8,
71 #if defined(GL_ES_VERSION_2_0)
72 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
73 #else
74 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
75 #endif
76 EGL_NONE
77 };
78
79 EGLConfig aCfg = NULL;
80 EGLint aNbConfigs = 0;
81 if (eglChooseConfig (theDisplay, aConfigAttribs, &aCfg, 1, &aNbConfigs) == EGL_TRUE
82 || aCfg != NULL)
83 {
84 return aCfg;
85 }
86
87 eglGetError();
88 aConfigAttribs[4 * 2 + 1] = 16; // try config with smaller depth buffer
89 if (eglChooseConfig (theDisplay, aConfigAttribs, &aCfg, 1, &aNbConfigs) != EGL_TRUE
90 || aCfg == NULL)
91 {
92 eglGetError();
93 }
94 return aCfg;
95 }
96#endif
7fd59977 97}
98
65993a95 99// =======================================================================
100// function : OpenGl_GraphicDriver
101// purpose :
102// =======================================================================
25b97fac 103OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp,
05e2200b 104 const Standard_Boolean theToInitialize)
fe9fc669 105: Graphic3d_GraphicDriver (theDisp),
05e2200b 106 myIsOwnContext (Standard_False),
1ce0716b 107#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
25b97fac 108 myEglDisplay ((Aspect_Display )EGL_NO_DISPLAY),
109 myEglContext ((Aspect_RenderingContext )EGL_NO_CONTEXT),
110 myEglConfig (NULL),
111#endif
65993a95 112 myCaps (new OpenGl_Caps()),
113 myMapOfView (1, NCollection_BaseAllocator::CommonBaseAllocator()),
a521d90d 114 myMapOfStructure (1, NCollection_BaseAllocator::CommonBaseAllocator())
65993a95 115{
d8d01f6e 116#if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
73192b37 117 if (myDisplayConnection.IsNull())
118 {
9775fa61 119 //throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: cannot connect to X server!");
fe9fc669 120 return;
73192b37 121 }
122
73192b37 123 Display* aDisplay = myDisplayConnection->GetDisplay();
124 Bool toSync = ::getenv ("CSF_GraphicSync") != NULL
125 || ::getenv ("CALL_SYNCHRO_X") != NULL;
126 XSynchronize (aDisplay, toSync);
127
1ce0716b 128#if !defined(HAVE_EGL) && !defined(HAVE_GLES2)
73192b37 129 // does the server know about OpenGL & GLX?
130 int aDummy;
131 if (!XQueryExtension (aDisplay, "GLX", &aDummy, &aDummy, &aDummy))
132 {
05e2200b 133 ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, this system doesn't appear to support OpenGL!", Message_Warning);
73192b37 134 }
135#endif
25b97fac 136#endif
05e2200b 137 if (theToInitialize
138 && !InitContext())
139 {
9775fa61 140 throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: default context can not be initialized!");
05e2200b 141 }
142}
25b97fac 143
05e2200b 144// =======================================================================
145// function : ~OpenGl_GraphicDriver
146// purpose :
147// =======================================================================
148OpenGl_GraphicDriver::~OpenGl_GraphicDriver()
149{
150 ReleaseContext();
151}
152
153// =======================================================================
154// function : ReleaseContext
155// purpose :
156// =======================================================================
157void OpenGl_GraphicDriver::ReleaseContext()
158{
159 Handle(OpenGl_Context) aCtxShared;
c357e426 160 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
161 aViewIter.More(); aViewIter.Next())
25b97fac 162 {
9775fa61 163 const Handle(OpenGl_View)& aView = aViewIter.Value();
c357e426 164 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
165 if (aWindow.IsNull())
166 {
167 continue;
168 }
169
170 const Handle(OpenGl_Context)& aCtx = aWindow->GetGlContext();
05e2200b 171 if (aCtx->MakeCurrent()
172 && aCtxShared.IsNull())
25b97fac 173 {
05e2200b 174 aCtxShared = aCtx;
25b97fac 175 }
05e2200b 176 }
177
178 if (!aCtxShared.IsNull())
179 {
180 aCtxShared->MakeCurrent();
181 }
a272ed94 182 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
05e2200b 183 aViewIter.More(); aViewIter.Next())
184 {
9775fa61 185 const Handle(OpenGl_View)& aView = aViewIter.Value();
05e2200b 186 aView->ReleaseGlResources (aCtxShared);
187 }
188
189 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
190 aStructIt.More (); aStructIt.Next())
191 {
192 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
193 aStruct->ReleaseGlResources (aCtxShared);
194 }
05e2200b 195
851dacdb 196 const bool isDeviceLost = !myMapOfStructure.IsEmpty();
c357e426 197 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
198 aViewIter.More(); aViewIter.Next())
05e2200b 199 {
9775fa61 200 const Handle(OpenGl_View)& aView = aViewIter.Value();
851dacdb 201 if (isDeviceLost)
202 {
203 aView->StructureManager()->SetDeviceLost();
204 }
205
c357e426 206 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
207 if (aWindow.IsNull())
208 {
209 continue;
210 }
211
212 aWindow->GetGlContext()->forcedRelease();
05e2200b 213 }
25b97fac 214
1ce0716b 215#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
05e2200b 216 if (myIsOwnContext)
217 {
218 if (myEglContext != (Aspect_RenderingContext )EGL_NO_CONTEXT)
25b97fac 219 {
05e2200b 220 if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) != EGL_TRUE)
221 {
222 ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, FAILED to release OpenGL context!", Message_Warning);
223 }
224 eglDestroyContext ((EGLDisplay )myEglDisplay, (EGLContext )myEglContext);
225 }
25b97fac 226
05e2200b 227 if (myEglDisplay != (Aspect_Display )EGL_NO_DISPLAY)
25b97fac 228 {
05e2200b 229 if (eglTerminate ((EGLDisplay )myEglDisplay) != EGL_TRUE)
230 {
231 ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, EGL, eglTerminate FAILED!", Message_Warning);
232 }
25b97fac 233 }
25b97fac 234 }
235
05e2200b 236 myEglDisplay = (Aspect_Display )EGL_NO_DISPLAY;
237 myEglContext = (Aspect_RenderingContext )EGL_NO_CONTEXT;
238 myEglConfig = NULL;
239#endif
240 myIsOwnContext = Standard_False;
241}
242
243// =======================================================================
244// function : InitContext
245// purpose :
246// =======================================================================
247Standard_Boolean OpenGl_GraphicDriver::InitContext()
248{
249 ReleaseContext();
1ce0716b 250#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
05e2200b 251
d8d01f6e 252#if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
05e2200b 253 if (myDisplayConnection.IsNull())
254 {
255 return Standard_False;
256 }
257 Display* aDisplay = myDisplayConnection->GetDisplay();
25b97fac 258 myEglDisplay = (Aspect_Display )eglGetDisplay (aDisplay);
259#else
260 myEglDisplay = (Aspect_Display )eglGetDisplay (EGL_DEFAULT_DISPLAY);
261#endif
262 if ((EGLDisplay )myEglDisplay == EGL_NO_DISPLAY)
263 {
05e2200b 264 ::Message::DefaultMessenger()->Send ("Error: no EGL display!", Message_Fail);
265 return Standard_False;
25b97fac 266 }
267
268 EGLint aVerMajor = 0; EGLint aVerMinor = 0;
269 if (eglInitialize ((EGLDisplay )myEglDisplay, &aVerMajor, &aVerMinor) != EGL_TRUE)
270 {
05e2200b 271 ::Message::DefaultMessenger()->Send ("Error: EGL display is unavailable!", Message_Fail);
272 return Standard_False;
25b97fac 273 }
274
39235bed 275 myEglConfig = chooseEglSurfConfig ((EGLDisplay )myEglDisplay);
276 if (myEglConfig == NULL)
25b97fac 277 {
39235bed 278 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide compatible configurations!", Message_Fail);
279 return Standard_False;
25b97fac 280 }
25b97fac 281
282#if defined(GL_ES_VERSION_2_0)
283 EGLint anEglCtxAttribs[] =
284 {
285 EGL_CONTEXT_CLIENT_VERSION, 2,
286 EGL_NONE
287 };
05e2200b 288 if (eglBindAPI (EGL_OPENGL_ES_API) != EGL_TRUE)
289 {
290 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide OpenGL ES client!", Message_Fail);
291 return Standard_False;
292 }
25b97fac 293#else
294 EGLint* anEglCtxAttribs = NULL;
05e2200b 295 if (eglBindAPI (EGL_OPENGL_API) != EGL_TRUE)
296 {
297 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide OpenGL client!", Message_Fail);
298 return Standard_False;
299 }
25b97fac 300#endif
301
302 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs);
303 if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
304 {
05e2200b 305 ::Message::DefaultMessenger()->Send ("Error: EGL is unable to create OpenGL context!", Message_Fail);
306 return Standard_False;
25b97fac 307 }
1ce0716b 308 // eglMakeCurrent() fails or even crash with EGL_NO_SURFACE on some implementations
309 //if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, (EGLContext )myEglContext) != EGL_TRUE)
310 //{
311 // ::Message::DefaultMessenger()->Send ("Error: EGL is unable bind OpenGL context!", Message_Fail);
312 // return Standard_False;
313 //}
25b97fac 314#endif
05e2200b 315 myIsOwnContext = Standard_True;
316 return Standard_True;
73192b37 317}
318
1ce0716b 319#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
05e2200b 320// =======================================================================
321// function : InitEglContext
322// purpose :
323// =======================================================================
324Standard_Boolean OpenGl_GraphicDriver::InitEglContext (Aspect_Display theEglDisplay,
325 Aspect_RenderingContext theEglContext,
326 void* theEglConfig)
327{
328 ReleaseContext();
d8d01f6e 329#if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
05e2200b 330 if (myDisplayConnection.IsNull())
331 {
332 return Standard_False;
333 }
334#endif
335
336 if ((EGLDisplay )theEglDisplay == EGL_NO_DISPLAY
39235bed 337 || (EGLContext )theEglContext == EGL_NO_CONTEXT)
05e2200b 338 {
339 return Standard_False;
340 }
341 myEglDisplay = theEglDisplay;
342 myEglContext = theEglContext;
343 myEglConfig = theEglConfig;
39235bed 344 if (theEglConfig == NULL)
345 {
346 myEglConfig = chooseEglSurfConfig ((EGLDisplay )myEglDisplay);
347 if (myEglConfig == NULL)
348 {
349 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide compatible configurations!", Message_Fail);
350 return Standard_False;
351 }
352 }
05e2200b 353 return Standard_True;
354}
355#endif
356
73192b37 357// =======================================================================
3bffef55 358// function : InquireLimit
73192b37 359// purpose :
360// =======================================================================
3bffef55 361Standard_Integer OpenGl_GraphicDriver::InquireLimit (const Graphic3d_TypeOfLimit theType) const
73192b37 362{
73192b37 363 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
3bffef55 364 switch (theType)
365 {
366 case Graphic3d_TypeOfLimit_MaxNbLights:
daf73ab7 367 return Graphic3d_ShaderProgram::THE_MAX_LIGHTS_DEFAULT;
3bffef55 368 case Graphic3d_TypeOfLimit_MaxNbClipPlanes:
369 return !aCtx.IsNull() ? aCtx->MaxClipPlanes() : 0;
370 case Graphic3d_TypeOfLimit_MaxNbViews:
371 return 10000;
372 case Graphic3d_TypeOfLimit_MaxTextureSize:
373 return !aCtx.IsNull() ? aCtx->MaxTextureSize() : 1024;
cc8cbabe 374 case Graphic3d_TypeOfLimit_MaxCombinedTextureUnits:
375 return !aCtx.IsNull() ? aCtx->MaxCombinedTextureUnits() : 1;
3bffef55 376 case Graphic3d_TypeOfLimit_MaxMsaa:
377 return !aCtx.IsNull() ? aCtx->MaxMsaaSamples() : 0;
6997ff1c 378 case Graphic3d_TypeOfLimit_MaxViewDumpSizeX:
379 return !aCtx.IsNull() ? aCtx->MaxDumpSizeX() : 1024;
380 case Graphic3d_TypeOfLimit_MaxViewDumpSizeY:
381 return !aCtx.IsNull() ? aCtx->MaxDumpSizeY() : 1024;
3a9b5dc8 382 case Graphic3d_TypeOfLimit_HasRayTracing:
383 return (!aCtx.IsNull() && aCtx->HasRayTracing()) ? 1 : 0;
384 case Graphic3d_TypeOfLimit_HasRayTracingTextures:
385 return (!aCtx.IsNull() && aCtx->HasRayTracingTextures()) ? 1 : 0;
386 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSampling:
387 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSampling()) ? 1 : 0;
e084dbbc 388 case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSamplingAtomic:
389 return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSamplingAtomic()) ? 1 : 0;
a1073ae2 390 case Graphic3d_TypeOfLimit_HasBlendedOit:
177781da 391 return (!aCtx.IsNull()
392 && aCtx->hasDrawBuffers != OpenGl_FeatureNotAvailable
393 && (aCtx->hasFloatBuffer != OpenGl_FeatureNotAvailable || aCtx->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable)) ? 1 : 0;
a1073ae2 394 case Graphic3d_TypeOfLimit_HasBlendedOitMsaa:
177781da 395 return (!aCtx.IsNull()
396 && aCtx->hasSampleVariables != OpenGl_FeatureNotAvailable
397 && (InquireLimit (Graphic3d_TypeOfLimit_HasBlendedOit) == 1)) ? 1 : 0;
c39bb31b 398 case Graphic3d_TypeOfLimit_HasFlatShading:
399 return !aCtx.IsNull() && aCtx->hasFlatShading != OpenGl_FeatureNotAvailable ? 1 : 0;
6997ff1c 400 case Graphic3d_TypeOfLimit_IsWorkaroundFBO:
401 return !aCtx.IsNull() && aCtx->MaxTextureSize() != aCtx->MaxDumpSizeX() ? 1 : 0;
2a332745 402 case Graphic3d_TypeOfLimit_HasMeshEdges:
403 return !aCtx.IsNull() && aCtx->hasGeometryStage != OpenGl_FeatureNotAvailable ? 1 : 0;
3bffef55 404 case Graphic3d_TypeOfLimit_NB:
405 return 0;
406 }
407 return 0;
73192b37 408}
409
f0430952 410// =======================================================================
5e27df78 411// function : DefaultTextHeight
f0430952 412// purpose :
413// =======================================================================
5e27df78 414Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const
2166f0fa 415{
5e27df78 416 return 16.;
2166f0fa
SK
417}
418
f0430952 419// =======================================================================
420// function : EnableVBO
421// purpose :
422// =======================================================================
2166f0fa 423void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
7fd59977 424{
58655684 425 myCaps->vboDisable = !theToTurnOn;
7fd59977 426}
f0430952 427
5e27df78 428// =======================================================================
429// function : GetSharedContext
430// purpose :
431// =======================================================================
432const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext() const
433{
c357e426 434 if (myMapOfView.IsEmpty())
5e27df78 435 {
436 return TheNullGlCtx;
437 }
438
c357e426 439 NCollection_Map<Handle(OpenGl_View)>::Iterator anIter (myMapOfView);
440 for (; anIter.More(); anIter.Next())
441 {
442 Handle(OpenGl_Window) aWindow = anIter.Value()->GlWindow();
443 if (aWindow.IsNull())
444 {
445 continue;
446 }
447
448 return aWindow->GetGlContext();
449 }
450
451 return TheNullGlCtx;
5e27df78 452}
453
f0430952 454// =======================================================================
455// function : MemoryInfo
456// purpose :
457// =======================================================================
458Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes,
459 TCollection_AsciiString& theInfo) const
460{
461 // this is extra work (for OpenGl_Context initialization)...
462 OpenGl_Context aGlCtx;
463 if (!aGlCtx.Init())
464 {
465 return Standard_False;
466 }
467 theFreeBytes = aGlCtx.AvailableMemory();
468 theInfo = aGlCtx.MemoryInfo();
469 return !theInfo.IsEmpty();
470}
1981cb22 471
472// =======================================================================
c357e426 473// function : SetBuffersNoSwap
1981cb22 474// purpose :
475// =======================================================================
c357e426 476void OpenGl_GraphicDriver::SetBuffersNoSwap (const Standard_Boolean theIsNoSwap)
1981cb22 477{
c357e426 478 myCaps->buffersNoSwap = theIsNoSwap;
1981cb22 479}
480
a174a3c5 481// =======================================================================
c357e426 482// function : TextSize
a174a3c5 483// purpose :
484// =======================================================================
4b1c8733 485void OpenGl_GraphicDriver::TextSize (const Handle(Graphic3d_CView)& theView,
486 const Standard_CString theText,
487 const Standard_ShortReal theHeight,
488 Standard_ShortReal& theWidth,
489 Standard_ShortReal& theAscent,
490 Standard_ShortReal& theDescent) const
a174a3c5 491{
c357e426 492 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
493 if (aCtx.IsNull())
a174a3c5 494 {
c357e426 495 return;
a174a3c5 496 }
497
c357e426 498 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
499 OpenGl_TextParam aTextParam;
500 aTextParam.Height = (int )aHeight;
bf5f0ca2 501 OpenGl_Aspects aTextAspect;
c357e426 502 TCollection_ExtendedString anExtText = theText;
fb0b0531 503 NCollection_String aText (anExtText.ToExtString());
4b1c8733 504 OpenGl_Text::StringSize(aCtx, aText, aTextAspect, aTextParam, theView->RenderingParams().Resolution, theWidth, theAscent, theDescent);
a174a3c5 505}
506
c357e426 507//=======================================================================
508//function : AddZLayer
509//purpose :
510//=======================================================================
511void OpenGl_GraphicDriver::AddZLayer (const Graphic3d_ZLayerId theLayerId)
a174a3c5 512{
c357e426 513 if (theLayerId < 1)
536d98e2 514 {
c357e426 515 Standard_ASSERT_RAISE (theLayerId > 0,
516 "OpenGl_GraphicDriver::AddZLayer, "
517 "negative and zero IDs are reserved");
536d98e2 518 }
519
1593b4ee 520 myLayerIds.Add (theLayerId);
a174a3c5 521
c357e426 522 // Default z-layer settings
523 myMapOfZLayerSettings.Bind (theLayerId, Graphic3d_ZLayerSettings());
1593b4ee 524 addZLayerIndex (theLayerId);
c357e426 525
526 // Add layer to all views
527 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
528 for (; aViewIt.More(); aViewIt.Next())
a174a3c5 529 {
c357e426 530 aViewIt.Value()->AddZLayer (theLayerId);
a174a3c5 531 }
532}
533
c357e426 534//=======================================================================
535//function : RemoveZLayer
536//purpose :
537//=======================================================================
538void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
a174a3c5 539{
c357e426 540 Standard_ASSERT_RAISE (theLayerId > 0,
541 "OpenGl_GraphicDriver::AddZLayer, "
542 "negative and zero IDs are reserved"
543 "and can not be removed");
544
545 Standard_ASSERT_RAISE (myLayerIds.Contains (theLayerId),
546 "OpenGl_GraphicDriver::RemoveZLayer, "
547 "Layer with theLayerId does not exist");
548
549 // Remove layer from all of the views
550 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
551 for (; aViewIt.More(); aViewIt.Next())
552 {
553 aViewIt.Value()->RemoveZLayer (theLayerId);
554 }
555
556 // Unset Z layer for all of the structures.
557 NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
558 for( ; aStructIt.More (); aStructIt.Next ())
a174a3c5 559 {
c357e426 560 OpenGl_Structure* aStruct = aStructIt.ChangeValue ();
561 if (aStruct->ZLayer() == theLayerId)
562 aStruct->SetZLayer (Graphic3d_ZLayerId_Default);
a174a3c5 563 }
c357e426 564
565 // Remove index
1593b4ee 566 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
c357e426 567 {
1593b4ee 568 if (aLayerIt.Value() == theLayerId)
c357e426 569 {
1593b4ee 570 myLayerSeq.Remove (aLayerIt);
c357e426 571 break;
572 }
573 }
574
575 myMapOfZLayerSettings.UnBind (theLayerId);
576 myLayerIds.Remove (theLayerId);
a174a3c5 577}
578
c357e426 579//=======================================================================
580//function : SetZLayerSettings
581//purpose :
582//=======================================================================
583void OpenGl_GraphicDriver::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
584 const Graphic3d_ZLayerSettings& theSettings)
a174a3c5 585{
d325cb7f 586 base_type::SetZLayerSettings (theLayerId, theSettings);
7c3ef2f7 587
588 // Change Z layer settings in all managed views
589 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
590 {
591 aViewIt.Value()->SetZLayerSettings (theLayerId, theSettings);
592 }
a174a3c5 593}
594
a174a3c5 595// =======================================================================
c357e426 596// function : Structure
a174a3c5 597// purpose :
598// =======================================================================
c357e426 599Handle(Graphic3d_CStructure) OpenGl_GraphicDriver::CreateStructure (const Handle(Graphic3d_StructureManager)& theManager)
a174a3c5 600{
c357e426 601 Handle(OpenGl_Structure) aStructure = new OpenGl_Structure (theManager);
602 myMapOfStructure.Bind (aStructure->Id, aStructure.operator->());
603 return aStructure;
a174a3c5 604}
605
606// =======================================================================
c357e426 607// function : Structure
a174a3c5 608// purpose :
609// =======================================================================
c357e426 610void OpenGl_GraphicDriver::RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure)
a174a3c5 611{
c357e426 612 OpenGl_Structure* aStructure = NULL;
613 if (!myMapOfStructure.Find (theCStructure->Id, aStructure))
a174a3c5 614 {
c357e426 615 return;
a174a3c5 616 }
c357e426 617
618 myMapOfStructure.UnBind (theCStructure->Id);
619 aStructure->Release (GetSharedContext());
620 theCStructure.Nullify();
a174a3c5 621}
622
623// =======================================================================
c357e426 624// function : View
a174a3c5 625// purpose :
626// =======================================================================
c357e426 627Handle(Graphic3d_CView) OpenGl_GraphicDriver::CreateView (const Handle(Graphic3d_StructureManager)& theMgr)
a174a3c5 628{
851dacdb 629 Handle(OpenGl_View) aView = new OpenGl_View (theMgr, this, myCaps, &myStateCounter);
c357e426 630
631 myMapOfView.Add (aView);
632
633 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
a174a3c5 634 {
c357e426 635 const Graphic3d_ZLayerId aLayerID = aLayerIt.Value();
636 const Graphic3d_ZLayerSettings& aSettings = myMapOfZLayerSettings.Find (aLayerID);
637 aView->AddZLayer (aLayerID);
638 aView->SetZLayerSettings (aLayerID, aSettings);
a174a3c5 639 }
c357e426 640
641 return aView;
a174a3c5 642}
643
644// =======================================================================
c357e426 645// function : RemoveView
a174a3c5 646// purpose :
647// =======================================================================
c357e426 648void OpenGl_GraphicDriver::RemoveView (const Handle(Graphic3d_CView)& theView)
a174a3c5 649{
c357e426 650 Handle(OpenGl_Context) aCtx = GetSharedContext();
651 Handle(OpenGl_View) aView = Handle(OpenGl_View)::DownCast (theView);
652 if (aView.IsNull())
a174a3c5 653 {
c357e426 654 return;
a174a3c5 655 }
a174a3c5 656
c357e426 657 if (!myMapOfView.Remove (aView))
658 {
659 return;
660 }
661
662 Handle(OpenGl_Window) aWindow = aView->GlWindow();
663 if (!aWindow.IsNull()
664 && aWindow->GetGlContext()->MakeCurrent())
a79f67f8 665 {
c357e426 666 aCtx = aWindow->GetGlContext();
667 }
668 else
669 {
670 // try to hijack another context if any
671 const Handle(OpenGl_Context)& anOtherCtx = GetSharedContext();
672 if (!anOtherCtx.IsNull()
673 && anOtherCtx != aWindow->GetGlContext())
674 {
675 aCtx = anOtherCtx;
676 aCtx->MakeCurrent();
677 }
678 }
679
680 aView->ReleaseGlResources (aCtx);
681 if (myMapOfView.IsEmpty())
682 {
683 // The last view removed but some objects still present.
684 // Release GL resources now without object destruction.
685 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
686 aStructIt.More (); aStructIt.Next())
687 {
688 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
689 aStruct->ReleaseGlResources (aCtx);
690 }
851dacdb 691
692 if (!myMapOfStructure.IsEmpty())
693 {
694 aView->StructureManager()->SetDeviceLost();
695 }
a79f67f8 696 }
a174a3c5 697}
62e1beed 698
699// =======================================================================
c357e426 700// function : Window
62e1beed 701// purpose :
702// =======================================================================
c357e426 703Handle(OpenGl_Window) OpenGl_GraphicDriver::CreateRenderWindow (const Handle(Aspect_Window)& theWindow,
704 const Aspect_RenderingContext theContext)
62e1beed 705{
c357e426 706 Handle(OpenGl_Context) aShareCtx = GetSharedContext();
707 Handle(OpenGl_Window) aWindow = new OpenGl_Window (this, theWindow, theContext, myCaps, aShareCtx);
708 return aWindow;
62e1beed 709}
27f85086 710
c357e426 711//=======================================================================
712//function : ViewExists
713//purpose :
714//=======================================================================
715Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)& AWindow, Handle(Graphic3d_CView)& theView)
27f85086 716{
c357e426 717 Standard_Boolean isExist = Standard_False;
718
719 // Parse the list of views to find
720 // a view with the specified window
721
1ce0716b 722#if defined(_WIN32) && !defined(OCCT_UWP)
c357e426 723 const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
724 Aspect_Handle TheSpecifiedWindowId = THEWindow->HWindow ();
725#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
726 const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow);
727 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
728 UIView* TheSpecifiedWindowId = THEWindow->HView();
729 #else
730 NSView* TheSpecifiedWindowId = THEWindow->HView();
731 #endif
1ce0716b 732#elif defined(__ANDROID__) || defined(__QNX__) || defined(OCCT_UWP)
20aeeb7b 733 (void )AWindow;
c357e426 734 int TheSpecifiedWindowId = -1;
735#else
736 const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow);
737 int TheSpecifiedWindowId = int (THEWindow->XWindow ());
738#endif
739
740 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
741 for(; aViewIt.More(); aViewIt.Next())
27f85086 742 {
c357e426 743 const Handle(OpenGl_View)& aView = aViewIt.Value();
744 if (aView->IsDefined() && aView->IsActive())
745 {
746 const Handle(Aspect_Window) AspectWindow = aView->Window();
747
1ce0716b 748#if defined(_WIN32) && !defined(OCCT_UWP)
c357e426 749 const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
750 Aspect_Handle TheWindowIdOfView = theWindow->HWindow ();
751#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
752 const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow);
753 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
754 UIView* TheWindowIdOfView = theWindow->HView();
755 #else
756 NSView* TheWindowIdOfView = theWindow->HView();
757 #endif
1ce0716b 758#elif defined(__ANDROID__) || defined(__QNX__) || defined(OCCT_UWP)
c357e426 759 int TheWindowIdOfView = 0;
760#else
761 const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
762 int TheWindowIdOfView = int (theWindow->XWindow ());
763#endif // WNT
764 // Comparaison on window IDs
765 if (TheWindowIdOfView == TheSpecifiedWindowId)
766 {
767 isExist = Standard_True;
768 theView = aView;
769 }
770 }
27f85086 771 }
772
c357e426 773 return isExist;
27f85086 774}
851dacdb 775
776//=======================================================================
777//function : setDeviceLost
778//purpose :
779//=======================================================================
780void OpenGl_GraphicDriver::setDeviceLost()
781{
782 if (myMapOfStructure.IsEmpty())
783 {
784 return;
785 }
786
787 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); aViewIter.More(); aViewIter.Next())
788 {
789 const Handle(OpenGl_View)& aView = aViewIter.Value();
790 if (aView->myWasRedrawnGL)
791 {
792 aView->StructureManager()->SetDeviceLost();
793 }
794 }
795}