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