0028389: Data Exchange - Import of STEP Saved Views and Clipping Planes
[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>
25b97fac 32#include <Message_Messenger.hxx>
73192b37 33#include <OSD_Environment.hxx>
a174a3c5 34#include <Standard_NotImplemented.hxx>
35
92efcf78 36IMPLEMENT_STANDARD_RTTIEXT(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
37
c357e426 38#if defined(_WIN32)
39 #include <WNT_Window.hxx>
40#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
41 #include <Cocoa_Window.hxx>
42#else
43 #include <Xw_Window.hxx>
44#endif
45
d8d01f6e 46#if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
73192b37 47 #include <X11/Xlib.h> // XOpenDisplay()
48#endif
49
1ce0716b 50#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
25b97fac 51 #include <EGL/egl.h>
52#endif
53
2166f0fa
SK
54namespace
55{
5e27df78 56 static const Handle(OpenGl_Context) TheNullGlCtx;
7fd59977 57}
58
65993a95 59// =======================================================================
60// function : OpenGl_GraphicDriver
61// purpose :
62// =======================================================================
25b97fac 63OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp,
05e2200b 64 const Standard_Boolean theToInitialize)
fe9fc669 65: Graphic3d_GraphicDriver (theDisp),
05e2200b 66 myIsOwnContext (Standard_False),
1ce0716b 67#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
25b97fac 68 myEglDisplay ((Aspect_Display )EGL_NO_DISPLAY),
69 myEglContext ((Aspect_RenderingContext )EGL_NO_CONTEXT),
70 myEglConfig (NULL),
71#endif
65993a95 72 myCaps (new OpenGl_Caps()),
73 myMapOfView (1, NCollection_BaseAllocator::CommonBaseAllocator()),
a521d90d 74 myMapOfStructure (1, NCollection_BaseAllocator::CommonBaseAllocator())
65993a95 75{
d8d01f6e 76#if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
73192b37 77 if (myDisplayConnection.IsNull())
78 {
73192b37 79 //Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_GraphicDriver: cannot connect to X server!");
fe9fc669 80 return;
73192b37 81 }
82
73192b37 83 Display* aDisplay = myDisplayConnection->GetDisplay();
84 Bool toSync = ::getenv ("CSF_GraphicSync") != NULL
85 || ::getenv ("CALL_SYNCHRO_X") != NULL;
86 XSynchronize (aDisplay, toSync);
87
1ce0716b 88#if !defined(HAVE_EGL) && !defined(HAVE_GLES2)
73192b37 89 // does the server know about OpenGL & GLX?
90 int aDummy;
91 if (!XQueryExtension (aDisplay, "GLX", &aDummy, &aDummy, &aDummy))
92 {
05e2200b 93 ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, this system doesn't appear to support OpenGL!", Message_Warning);
73192b37 94 }
95#endif
25b97fac 96#endif
05e2200b 97 if (theToInitialize
98 && !InitContext())
99 {
100 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_GraphicDriver: default context can not be initialized!");
101 }
c357e426 102
103 // default layers are always presented in display layer sequence it can not be removed
7c3ef2f7 104 {
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);
115 }
116
117 {
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);
128 }
129
130 {
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);
141 }
142
143 {
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);
154 }
155
156 {
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);
167 }
05e2200b 168}
25b97fac 169
05e2200b 170// =======================================================================
171// function : ~OpenGl_GraphicDriver
172// purpose :
173// =======================================================================
174OpenGl_GraphicDriver::~OpenGl_GraphicDriver()
175{
176 ReleaseContext();
177}
178
179// =======================================================================
180// function : ReleaseContext
181// purpose :
182// =======================================================================
183void OpenGl_GraphicDriver::ReleaseContext()
184{
185 Handle(OpenGl_Context) aCtxShared;
c357e426 186 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
187 aViewIter.More(); aViewIter.Next())
25b97fac 188 {
c357e426 189 const Handle(OpenGl_View)& aView = aViewIter.ChangeValue();
190 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
191 if (aWindow.IsNull())
192 {
193 continue;
194 }
195
196 const Handle(OpenGl_Context)& aCtx = aWindow->GetGlContext();
05e2200b 197 if (aCtx->MakeCurrent()
198 && aCtxShared.IsNull())
25b97fac 199 {
05e2200b 200 aCtxShared = aCtx;
25b97fac 201 }
05e2200b 202 }
203
204 if (!aCtxShared.IsNull())
205 {
206 aCtxShared->MakeCurrent();
207 }
a272ed94 208 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
05e2200b 209 aViewIter.More(); aViewIter.Next())
210 {
211 const Handle(OpenGl_View)& aView = aViewIter.ChangeValue();
212 aView->ReleaseGlResources (aCtxShared);
213 }
214
215 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
216 aStructIt.More (); aStructIt.Next())
217 {
218 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
219 aStruct->ReleaseGlResources (aCtxShared);
220 }
05e2200b 221 myDeviceLostFlag = myDeviceLostFlag || !myMapOfStructure.IsEmpty();
222
c357e426 223 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
224 aViewIter.More(); aViewIter.Next())
05e2200b 225 {
c357e426 226 const Handle(OpenGl_View)& aView = aViewIter.ChangeValue();
227 const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
228 if (aWindow.IsNull())
229 {
230 continue;
231 }
232
233 aWindow->GetGlContext()->forcedRelease();
05e2200b 234 }
25b97fac 235
1ce0716b 236#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
05e2200b 237 if (myIsOwnContext)
238 {
239 if (myEglContext != (Aspect_RenderingContext )EGL_NO_CONTEXT)
25b97fac 240 {
05e2200b 241 if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) != EGL_TRUE)
242 {
243 ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, FAILED to release OpenGL context!", Message_Warning);
244 }
245 eglDestroyContext ((EGLDisplay )myEglDisplay, (EGLContext )myEglContext);
246 }
25b97fac 247
05e2200b 248 if (myEglDisplay != (Aspect_Display )EGL_NO_DISPLAY)
25b97fac 249 {
05e2200b 250 if (eglTerminate ((EGLDisplay )myEglDisplay) != EGL_TRUE)
251 {
252 ::Message::DefaultMessenger()->Send ("OpenGl_GraphicDriver, EGL, eglTerminate FAILED!", Message_Warning);
253 }
25b97fac 254 }
25b97fac 255 }
256
05e2200b 257 myEglDisplay = (Aspect_Display )EGL_NO_DISPLAY;
258 myEglContext = (Aspect_RenderingContext )EGL_NO_CONTEXT;
259 myEglConfig = NULL;
260#endif
261 myIsOwnContext = Standard_False;
262}
263
264// =======================================================================
265// function : InitContext
266// purpose :
267// =======================================================================
268Standard_Boolean OpenGl_GraphicDriver::InitContext()
269{
270 ReleaseContext();
1ce0716b 271#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
05e2200b 272
d8d01f6e 273#if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
05e2200b 274 if (myDisplayConnection.IsNull())
275 {
276 return Standard_False;
277 }
278 Display* aDisplay = myDisplayConnection->GetDisplay();
25b97fac 279 myEglDisplay = (Aspect_Display )eglGetDisplay (aDisplay);
280#else
281 myEglDisplay = (Aspect_Display )eglGetDisplay (EGL_DEFAULT_DISPLAY);
282#endif
283 if ((EGLDisplay )myEglDisplay == EGL_NO_DISPLAY)
284 {
05e2200b 285 ::Message::DefaultMessenger()->Send ("Error: no EGL display!", Message_Fail);
286 return Standard_False;
25b97fac 287 }
288
289 EGLint aVerMajor = 0; EGLint aVerMinor = 0;
290 if (eglInitialize ((EGLDisplay )myEglDisplay, &aVerMajor, &aVerMinor) != EGL_TRUE)
291 {
05e2200b 292 ::Message::DefaultMessenger()->Send ("Error: EGL display is unavailable!", Message_Fail);
293 return Standard_False;
25b97fac 294 }
295
05e2200b 296 EGLint aConfigAttribs[] =
25b97fac 297 {
298 EGL_RED_SIZE, 8,
299 EGL_GREEN_SIZE, 8,
300 EGL_BLUE_SIZE, 8,
301 EGL_ALPHA_SIZE, 0,
302 EGL_DEPTH_SIZE, 24,
303 EGL_STENCIL_SIZE, 8,
304 #if defined(GL_ES_VERSION_2_0)
305 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
306 #else
307 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
308 #endif
309 EGL_NONE
310 };
311
312 EGLint aNbConfigs = 0;
05e2200b 313 if (eglChooseConfig ((EGLDisplay )myEglDisplay, aConfigAttribs, &myEglConfig, 1, &aNbConfigs) != EGL_TRUE
314 || myEglConfig == NULL)
25b97fac 315 {
05e2200b 316 eglGetError();
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)
320 {
321 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide compatible configurations!", Message_Fail);
322 return Standard_False;
323 }
25b97fac 324 }
25b97fac 325
326#if defined(GL_ES_VERSION_2_0)
327 EGLint anEglCtxAttribs[] =
328 {
329 EGL_CONTEXT_CLIENT_VERSION, 2,
330 EGL_NONE
331 };
05e2200b 332 if (eglBindAPI (EGL_OPENGL_ES_API) != EGL_TRUE)
333 {
334 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide OpenGL ES client!", Message_Fail);
335 return Standard_False;
336 }
25b97fac 337#else
338 EGLint* anEglCtxAttribs = NULL;
05e2200b 339 if (eglBindAPI (EGL_OPENGL_API) != EGL_TRUE)
340 {
341 ::Message::DefaultMessenger()->Send ("Error: EGL does not provide OpenGL client!", Message_Fail);
342 return Standard_False;
343 }
25b97fac 344#endif
345
346 myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs);
347 if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
348 {
05e2200b 349 ::Message::DefaultMessenger()->Send ("Error: EGL is unable to create OpenGL context!", Message_Fail);
350 return Standard_False;
25b97fac 351 }
1ce0716b 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)
354 //{
355 // ::Message::DefaultMessenger()->Send ("Error: EGL is unable bind OpenGL context!", Message_Fail);
356 // return Standard_False;
357 //}
25b97fac 358#endif
05e2200b 359 myIsOwnContext = Standard_True;
360 return Standard_True;
73192b37 361}
362
1ce0716b 363#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
05e2200b 364// =======================================================================
365// function : InitEglContext
366// purpose :
367// =======================================================================
368Standard_Boolean OpenGl_GraphicDriver::InitEglContext (Aspect_Display theEglDisplay,
369 Aspect_RenderingContext theEglContext,
370 void* theEglConfig)
371{
372 ReleaseContext();
d8d01f6e 373#if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
05e2200b 374 if (myDisplayConnection.IsNull())
375 {
376 return Standard_False;
377 }
378#endif
379
380 if ((EGLDisplay )theEglDisplay == EGL_NO_DISPLAY
381 || (EGLContext )theEglContext == EGL_NO_CONTEXT
382 || theEglConfig == NULL)
383 {
384 return Standard_False;
385 }
386 myEglDisplay = theEglDisplay;
387 myEglContext = theEglContext;
388 myEglConfig = theEglConfig;
389 return Standard_True;
390}
391#endif
392
73192b37 393// =======================================================================
3bffef55 394// function : InquireLimit
73192b37 395// purpose :
396// =======================================================================
3bffef55 397Standard_Integer OpenGl_GraphicDriver::InquireLimit (const Graphic3d_TypeOfLimit theType) const
73192b37 398{
73192b37 399 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
3bffef55 400 switch (theType)
401 {
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:
407 return 10000;
408 case Graphic3d_TypeOfLimit_MaxTextureSize:
409 return !aCtx.IsNull() ? aCtx->MaxTextureSize() : 1024;
410 case Graphic3d_TypeOfLimit_MaxMsaa:
411 return !aCtx.IsNull() ? aCtx->MaxMsaaSamples() : 0;
3a9b5dc8 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;
3bffef55 418 case Graphic3d_TypeOfLimit_NB:
419 return 0;
420 }
421 return 0;
73192b37 422}
423
f0430952 424// =======================================================================
5e27df78 425// function : DefaultTextHeight
f0430952 426// purpose :
427// =======================================================================
5e27df78 428Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const
2166f0fa 429{
5e27df78 430 return 16.;
2166f0fa
SK
431}
432
f0430952 433// =======================================================================
434// function : EnableVBO
435// purpose :
436// =======================================================================
2166f0fa 437void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
7fd59977 438{
58655684 439 myCaps->vboDisable = !theToTurnOn;
7fd59977 440}
f0430952 441
5e27df78 442// =======================================================================
443// function : GetSharedContext
444// purpose :
445// =======================================================================
446const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext() const
447{
c357e426 448 if (myMapOfView.IsEmpty())
5e27df78 449 {
450 return TheNullGlCtx;
451 }
452
c357e426 453 NCollection_Map<Handle(OpenGl_View)>::Iterator anIter (myMapOfView);
454 for (; anIter.More(); anIter.Next())
455 {
456 Handle(OpenGl_Window) aWindow = anIter.Value()->GlWindow();
457 if (aWindow.IsNull())
458 {
459 continue;
460 }
461
462 return aWindow->GetGlContext();
463 }
464
465 return TheNullGlCtx;
5e27df78 466}
467
f0430952 468// =======================================================================
469// function : MemoryInfo
470// purpose :
471// =======================================================================
472Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes,
473 TCollection_AsciiString& theInfo) const
474{
475 // this is extra work (for OpenGl_Context initialization)...
476 OpenGl_Context aGlCtx;
477 if (!aGlCtx.Init())
478 {
479 return Standard_False;
480 }
481 theFreeBytes = aGlCtx.AvailableMemory();
482 theInfo = aGlCtx.MemoryInfo();
483 return !theInfo.IsEmpty();
484}
1981cb22 485
486// =======================================================================
c357e426 487// function : SetBuffersNoSwap
1981cb22 488// purpose :
489// =======================================================================
c357e426 490void OpenGl_GraphicDriver::SetBuffersNoSwap (const Standard_Boolean theIsNoSwap)
1981cb22 491{
c357e426 492 myCaps->buffersNoSwap = theIsNoSwap;
1981cb22 493}
494
a174a3c5 495// =======================================================================
c357e426 496// function : TextSize
a174a3c5 497// purpose :
498// =======================================================================
4b1c8733 499void OpenGl_GraphicDriver::TextSize (const Handle(Graphic3d_CView)& theView,
500 const Standard_CString theText,
501 const Standard_ShortReal theHeight,
502 Standard_ShortReal& theWidth,
503 Standard_ShortReal& theAscent,
504 Standard_ShortReal& theDescent) const
a174a3c5 505{
c357e426 506 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
507 if (aCtx.IsNull())
a174a3c5 508 {
c357e426 509 return;
a174a3c5 510 }
511
c357e426 512 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
513 OpenGl_TextParam aTextParam;
514 aTextParam.Height = (int )aHeight;
515 OpenGl_AspectText aTextAspect;
b6472664 516 aTextAspect.Aspect()->SetSpace (0.3);
c357e426 517 TCollection_ExtendedString anExtText = theText;
fb0b0531 518 NCollection_String aText (anExtText.ToExtString());
4b1c8733 519 OpenGl_Text::StringSize(aCtx, aText, aTextAspect, aTextParam, theView->RenderingParams().Resolution, theWidth, theAscent, theDescent);
a174a3c5 520}
521
1593b4ee 522//=======================================================================
523//function : addZLayerIndex
524//purpose :
525//=======================================================================
526void OpenGl_GraphicDriver::addZLayerIndex (const Graphic3d_ZLayerId theLayerId)
527{
528 // remove index
529 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
530 {
531 if (aLayerIt.Value() == theLayerId)
532 {
533 myLayerSeq.Remove (aLayerIt);
534 break;
535 }
536 }
537
7c3ef2f7 538 if (myMapOfZLayerSettings.Find (theLayerId).IsImmediate())
1593b4ee 539 {
540 myLayerSeq.Append (theLayerId);
541 return;
542 }
543
544 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
545 {
546 const Graphic3d_ZLayerSettings& aSettings = myMapOfZLayerSettings.Find (aLayerIt.Value());
7c3ef2f7 547 if (aSettings.IsImmediate())
1593b4ee 548 {
549 aLayerIt.Previous();
550 if (aLayerIt.More())
551 {
552 myLayerSeq.InsertAfter (aLayerIt, theLayerId);
553 return;
554 }
555
556 // first non-immediate layer
557 myLayerSeq.Prepend (theLayerId);
558 return;
559 }
560 }
561
562 // no immediate layers
563 myLayerSeq.Append (theLayerId);
564}
565
c357e426 566//=======================================================================
567//function : AddZLayer
568//purpose :
569//=======================================================================
570void OpenGl_GraphicDriver::AddZLayer (const Graphic3d_ZLayerId theLayerId)
a174a3c5 571{
c357e426 572 if (theLayerId < 1)
536d98e2 573 {
c357e426 574 Standard_ASSERT_RAISE (theLayerId > 0,
575 "OpenGl_GraphicDriver::AddZLayer, "
576 "negative and zero IDs are reserved");
536d98e2 577 }
578
1593b4ee 579 myLayerIds.Add (theLayerId);
a174a3c5 580
c357e426 581 // Default z-layer settings
582 myMapOfZLayerSettings.Bind (theLayerId, Graphic3d_ZLayerSettings());
1593b4ee 583 addZLayerIndex (theLayerId);
c357e426 584
585 // Add layer to all views
586 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
587 for (; aViewIt.More(); aViewIt.Next())
a174a3c5 588 {
c357e426 589 aViewIt.Value()->AddZLayer (theLayerId);
a174a3c5 590 }
591}
592
c357e426 593//=======================================================================
594//function : RemoveZLayer
595//purpose :
596//=======================================================================
597void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
a174a3c5 598{
c357e426 599 Standard_ASSERT_RAISE (theLayerId > 0,
600 "OpenGl_GraphicDriver::AddZLayer, "
601 "negative and zero IDs are reserved"
602 "and can not be removed");
603
604 Standard_ASSERT_RAISE (myLayerIds.Contains (theLayerId),
605 "OpenGl_GraphicDriver::RemoveZLayer, "
606 "Layer with theLayerId does not exist");
607
608 // Remove layer from all of the views
609 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
610 for (; aViewIt.More(); aViewIt.Next())
611 {
612 aViewIt.Value()->RemoveZLayer (theLayerId);
613 }
614
615 // Unset Z layer for all of the structures.
616 NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
617 for( ; aStructIt.More (); aStructIt.Next ())
a174a3c5 618 {
c357e426 619 OpenGl_Structure* aStruct = aStructIt.ChangeValue ();
620 if (aStruct->ZLayer() == theLayerId)
621 aStruct->SetZLayer (Graphic3d_ZLayerId_Default);
a174a3c5 622 }
c357e426 623
624 // Remove index
1593b4ee 625 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
c357e426 626 {
1593b4ee 627 if (aLayerIt.Value() == theLayerId)
c357e426 628 {
1593b4ee 629 myLayerSeq.Remove (aLayerIt);
c357e426 630 break;
631 }
632 }
633
634 myMapOfZLayerSettings.UnBind (theLayerId);
635 myLayerIds.Remove (theLayerId);
a174a3c5 636}
637
c357e426 638//=======================================================================
639//function : ZLayers
640//purpose :
641//=======================================================================
642void OpenGl_GraphicDriver::ZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
a174a3c5 643{
c357e426 644 theLayerSeq.Assign (myLayerSeq);
a174a3c5 645}
646
c357e426 647//=======================================================================
648//function : SetZLayerSettings
649//purpose :
650//=======================================================================
651void OpenGl_GraphicDriver::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
652 const Graphic3d_ZLayerSettings& theSettings)
a174a3c5 653{
1593b4ee 654 Graphic3d_ZLayerSettings* aSettings = myMapOfZLayerSettings.ChangeSeek (theLayerId);
655 if (aSettings != NULL)
c357e426 656 {
7c3ef2f7 657 const bool isChanged = (aSettings->IsImmediate() != theSettings.IsImmediate());
1593b4ee 658 *aSettings = theSettings;
659 if (isChanged)
660 {
661 addZLayerIndex (theLayerId);
662 }
c357e426 663 }
664 else
665 {
1593b4ee 666 // abnormal case
c357e426 667 myMapOfZLayerSettings.Bind (theLayerId, theSettings);
1593b4ee 668 addZLayerIndex (theLayerId);
c357e426 669 }
7c3ef2f7 670
671 // Change Z layer settings in all managed views
672 for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
673 {
674 aViewIt.Value()->SetZLayerSettings (theLayerId, theSettings);
675 }
a174a3c5 676}
677
c357e426 678//=======================================================================
679//function : ZLayerSettings
680//purpose :
681//=======================================================================
7c3ef2f7 682const Graphic3d_ZLayerSettings& OpenGl_GraphicDriver::ZLayerSettings (const Graphic3d_ZLayerId theLayerId) const
a174a3c5 683{
c357e426 684 Standard_ASSERT_RAISE (myLayerIds.Contains (theLayerId),
685 "OpenGl_GraphicDriver::ZLayerSettings, "
686 "Layer with theLayerId does not exist");
687
688 return myMapOfZLayerSettings.Find (theLayerId);
a174a3c5 689}
690
691// =======================================================================
c357e426 692// function : Structure
a174a3c5 693// purpose :
694// =======================================================================
c357e426 695Handle(Graphic3d_CStructure) OpenGl_GraphicDriver::CreateStructure (const Handle(Graphic3d_StructureManager)& theManager)
a174a3c5 696{
c357e426 697 Handle(OpenGl_Structure) aStructure = new OpenGl_Structure (theManager);
698 myMapOfStructure.Bind (aStructure->Id, aStructure.operator->());
699 return aStructure;
a174a3c5 700}
701
702// =======================================================================
c357e426 703// function : Structure
a174a3c5 704// purpose :
705// =======================================================================
c357e426 706void OpenGl_GraphicDriver::RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure)
a174a3c5 707{
c357e426 708 OpenGl_Structure* aStructure = NULL;
709 if (!myMapOfStructure.Find (theCStructure->Id, aStructure))
a174a3c5 710 {
c357e426 711 return;
a174a3c5 712 }
c357e426 713
714 myMapOfStructure.UnBind (theCStructure->Id);
715 aStructure->Release (GetSharedContext());
716 theCStructure.Nullify();
a174a3c5 717}
718
719// =======================================================================
c357e426 720// function : View
a174a3c5 721// purpose :
722// =======================================================================
c357e426 723Handle(Graphic3d_CView) OpenGl_GraphicDriver::CreateView (const Handle(Graphic3d_StructureManager)& theMgr)
a174a3c5 724{
c357e426 725 Handle(OpenGl_View) aView = new OpenGl_View (theMgr, this, myCaps, myDeviceLostFlag, &myStateCounter);
726
727 myMapOfView.Add (aView);
728
729 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
a174a3c5 730 {
c357e426 731 const Graphic3d_ZLayerId aLayerID = aLayerIt.Value();
732 const Graphic3d_ZLayerSettings& aSettings = myMapOfZLayerSettings.Find (aLayerID);
733 aView->AddZLayer (aLayerID);
734 aView->SetZLayerSettings (aLayerID, aSettings);
a174a3c5 735 }
c357e426 736
737 return aView;
a174a3c5 738}
739
740// =======================================================================
c357e426 741// function : RemoveView
a174a3c5 742// purpose :
743// =======================================================================
c357e426 744void OpenGl_GraphicDriver::RemoveView (const Handle(Graphic3d_CView)& theView)
a174a3c5 745{
c357e426 746 Handle(OpenGl_Context) aCtx = GetSharedContext();
747 Handle(OpenGl_View) aView = Handle(OpenGl_View)::DownCast (theView);
748 if (aView.IsNull())
a174a3c5 749 {
c357e426 750 return;
a174a3c5 751 }
a174a3c5 752
c357e426 753 if (!myMapOfView.Remove (aView))
754 {
755 return;
756 }
757
758 Handle(OpenGl_Window) aWindow = aView->GlWindow();
759 if (!aWindow.IsNull()
760 && aWindow->GetGlContext()->MakeCurrent())
a79f67f8 761 {
c357e426 762 aCtx = aWindow->GetGlContext();
763 }
764 else
765 {
766 // try to hijack another context if any
767 const Handle(OpenGl_Context)& anOtherCtx = GetSharedContext();
768 if (!anOtherCtx.IsNull()
769 && anOtherCtx != aWindow->GetGlContext())
770 {
771 aCtx = anOtherCtx;
772 aCtx->MakeCurrent();
773 }
774 }
775
776 aView->ReleaseGlResources (aCtx);
777 if (myMapOfView.IsEmpty())
778 {
779 // The last view removed but some objects still present.
780 // Release GL resources now without object destruction.
781 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
782 aStructIt.More (); aStructIt.Next())
783 {
784 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
785 aStruct->ReleaseGlResources (aCtx);
786 }
787 myDeviceLostFlag = !myMapOfStructure.IsEmpty();
a79f67f8 788 }
a174a3c5 789}
62e1beed 790
791// =======================================================================
c357e426 792// function : Window
62e1beed 793// purpose :
794// =======================================================================
c357e426 795Handle(OpenGl_Window) OpenGl_GraphicDriver::CreateRenderWindow (const Handle(Aspect_Window)& theWindow,
796 const Aspect_RenderingContext theContext)
62e1beed 797{
c357e426 798 Handle(OpenGl_Context) aShareCtx = GetSharedContext();
799 Handle(OpenGl_Window) aWindow = new OpenGl_Window (this, theWindow, theContext, myCaps, aShareCtx);
800 return aWindow;
62e1beed 801}
27f85086 802
c357e426 803//=======================================================================
804//function : ViewExists
805//purpose :
806//=======================================================================
807Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)& AWindow, Handle(Graphic3d_CView)& theView)
27f85086 808{
c357e426 809 Standard_Boolean isExist = Standard_False;
810
811 // Parse the list of views to find
812 // a view with the specified window
813
1ce0716b 814#if defined(_WIN32) && !defined(OCCT_UWP)
c357e426 815 const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
816 Aspect_Handle TheSpecifiedWindowId = THEWindow->HWindow ();
817#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
818 const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow);
819 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
820 UIView* TheSpecifiedWindowId = THEWindow->HView();
821 #else
822 NSView* TheSpecifiedWindowId = THEWindow->HView();
823 #endif
1ce0716b 824#elif defined(__ANDROID__) || defined(__QNX__) || defined(OCCT_UWP)
20aeeb7b 825 (void )AWindow;
c357e426 826 int TheSpecifiedWindowId = -1;
827#else
828 const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow);
829 int TheSpecifiedWindowId = int (THEWindow->XWindow ());
830#endif
831
832 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
833 for(; aViewIt.More(); aViewIt.Next())
27f85086 834 {
c357e426 835 const Handle(OpenGl_View)& aView = aViewIt.Value();
836 if (aView->IsDefined() && aView->IsActive())
837 {
838 const Handle(Aspect_Window) AspectWindow = aView->Window();
839
1ce0716b 840#if defined(_WIN32) && !defined(OCCT_UWP)
c357e426 841 const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
842 Aspect_Handle TheWindowIdOfView = theWindow->HWindow ();
843#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
844 const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow);
845 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
846 UIView* TheWindowIdOfView = theWindow->HView();
847 #else
848 NSView* TheWindowIdOfView = theWindow->HView();
849 #endif
1ce0716b 850#elif defined(__ANDROID__) || defined(__QNX__) || defined(OCCT_UWP)
c357e426 851 int TheWindowIdOfView = 0;
852#else
853 const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
854 int TheWindowIdOfView = int (theWindow->XWindow ());
855#endif // WNT
856 // Comparaison on window IDs
857 if (TheWindowIdOfView == TheSpecifiedWindowId)
858 {
859 isExist = Standard_True;
860 theView = aView;
861 }
862 }
27f85086 863 }
864
c357e426 865 return isExist;
27f85086 866}