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