0027750: Visualization, V3d_View - remove unused functionality ZClipping and ZCueing
[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
f0430952 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{
73192b37 389 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
89a929ea 390 return aCtx.IsNull() ? aCtx->MaxClipPlanes() : 0;
73192b37 391}
392
393// =======================================================================
5e27df78 394// function : DefaultTextHeight
f0430952 395// purpose :
396// =======================================================================
5e27df78 397Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const
2166f0fa 398{
5e27df78 399 return 16.;
2166f0fa
SK
400}
401
f0430952 402// =======================================================================
f0430952 403// function : EnableVBO
404// purpose :
405// =======================================================================
2166f0fa 406void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
7fd59977 407{
58655684 408 myCaps->vboDisable = !theToTurnOn;
7fd59977 409}
f0430952 410
411// =======================================================================
5e27df78 412// function : GetSharedContext
413// purpose :
414// =======================================================================
415const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext() const
416{
c357e426 417 if (myMapOfView.IsEmpty())
5e27df78 418 {
419 return TheNullGlCtx;
420 }
421
c357e426 422 NCollection_Map<Handle(OpenGl_View)>::Iterator anIter (myMapOfView);
423 for (; anIter.More(); anIter.Next())
424 {
425 Handle(OpenGl_Window) aWindow = anIter.Value()->GlWindow();
426 if (aWindow.IsNull())
427 {
428 continue;
429 }
430
431 return aWindow->GetGlContext();
432 }
433
434 return TheNullGlCtx;
5e27df78 435}
436
437// =======================================================================
f0430952 438// function : MemoryInfo
439// purpose :
440// =======================================================================
441Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes,
442 TCollection_AsciiString& theInfo) const
443{
444 // this is extra work (for OpenGl_Context initialization)...
445 OpenGl_Context aGlCtx;
446 if (!aGlCtx.Init())
447 {
448 return Standard_False;
449 }
450 theFreeBytes = aGlCtx.AvailableMemory();
451 theInfo = aGlCtx.MemoryInfo();
452 return !theInfo.IsEmpty();
453}
1981cb22 454
455// =======================================================================
c357e426 456// function : SetBuffersNoSwap
1981cb22 457// purpose :
458// =======================================================================
c357e426 459void OpenGl_GraphicDriver::SetBuffersNoSwap (const Standard_Boolean theIsNoSwap)
1981cb22 460{
c357e426 461 myCaps->buffersNoSwap = theIsNoSwap;
1981cb22 462}
463
464// =======================================================================
c357e426 465// function : TextSize
a174a3c5 466// purpose :
467// =======================================================================
4b1c8733 468void OpenGl_GraphicDriver::TextSize (const Handle(Graphic3d_CView)& theView,
469 const Standard_CString theText,
470 const Standard_ShortReal theHeight,
471 Standard_ShortReal& theWidth,
472 Standard_ShortReal& theAscent,
473 Standard_ShortReal& theDescent) const
a174a3c5 474{
c357e426 475 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
476 if (aCtx.IsNull())
a174a3c5 477 {
c357e426 478 return;
a174a3c5 479 }
480
c357e426 481 const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
482 OpenGl_TextParam aTextParam;
483 aTextParam.Height = (int )aHeight;
484 OpenGl_AspectText aTextAspect;
b6472664 485 aTextAspect.Aspect()->SetSpace (0.3);
c357e426 486 TCollection_ExtendedString anExtText = theText;
487 NCollection_String aText = (Standard_Utf16Char* )anExtText.ToExtString();
4b1c8733 488 OpenGl_Text::StringSize(aCtx, aText, aTextAspect, aTextParam, theView->RenderingParams().Resolution, theWidth, theAscent, theDescent);
a174a3c5 489}
490
c357e426 491//=======================================================================
492//function : AddZLayer
493//purpose :
494//=======================================================================
495void OpenGl_GraphicDriver::AddZLayer (const Graphic3d_ZLayerId theLayerId)
a174a3c5 496{
c357e426 497 if (theLayerId < 1)
536d98e2 498 {
c357e426 499 Standard_ASSERT_RAISE (theLayerId > 0,
500 "OpenGl_GraphicDriver::AddZLayer, "
501 "negative and zero IDs are reserved");
536d98e2 502 }
503
c357e426 504 myLayerIds.Add (theLayerId);
505 myLayerSeq.Append (theLayerId);
a174a3c5 506
c357e426 507 // Default z-layer settings
508 myMapOfZLayerSettings.Bind (theLayerId, Graphic3d_ZLayerSettings());
509
510 // Add layer to all views
511 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
512 for (; aViewIt.More(); aViewIt.Next())
a174a3c5 513 {
c357e426 514 aViewIt.Value()->AddZLayer (theLayerId);
a174a3c5 515 }
516}
517
c357e426 518//=======================================================================
519//function : RemoveZLayer
520//purpose :
521//=======================================================================
522void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
a174a3c5 523{
c357e426 524 Standard_ASSERT_RAISE (theLayerId > 0,
525 "OpenGl_GraphicDriver::AddZLayer, "
526 "negative and zero IDs are reserved"
527 "and can not be removed");
528
529 Standard_ASSERT_RAISE (myLayerIds.Contains (theLayerId),
530 "OpenGl_GraphicDriver::RemoveZLayer, "
531 "Layer with theLayerId does not exist");
532
533 // Remove layer from all of the views
534 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
535 for (; aViewIt.More(); aViewIt.Next())
536 {
537 aViewIt.Value()->RemoveZLayer (theLayerId);
538 }
539
540 // Unset Z layer for all of the structures.
541 NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
542 for( ; aStructIt.More (); aStructIt.Next ())
a174a3c5 543 {
c357e426 544 OpenGl_Structure* aStruct = aStructIt.ChangeValue ();
545 if (aStruct->ZLayer() == theLayerId)
546 aStruct->SetZLayer (Graphic3d_ZLayerId_Default);
a174a3c5 547 }
c357e426 548
549 // Remove index
550 for (int aIdx = 1; aIdx <= myLayerSeq.Length (); aIdx++)
551 {
552 if (myLayerSeq (aIdx) == theLayerId)
553 {
554 myLayerSeq.Remove (aIdx);
555 break;
556 }
557 }
558
559 myMapOfZLayerSettings.UnBind (theLayerId);
560 myLayerIds.Remove (theLayerId);
a174a3c5 561}
562
c357e426 563//=======================================================================
564//function : ZLayers
565//purpose :
566//=======================================================================
567void OpenGl_GraphicDriver::ZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
a174a3c5 568{
c357e426 569 theLayerSeq.Assign (myLayerSeq);
a174a3c5 570}
571
c357e426 572//=======================================================================
573//function : SetZLayerSettings
574//purpose :
575//=======================================================================
576void OpenGl_GraphicDriver::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
577 const Graphic3d_ZLayerSettings& theSettings)
a174a3c5 578{
c357e426 579 // Change Z layer settings in all managed views
580 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
581 for (; aViewIt.More(); aViewIt.Next())
a174a3c5 582 {
c357e426 583 aViewIt.Value()->SetZLayerSettings (theLayerId, theSettings);
a174a3c5 584 }
585
c357e426 586 if (myMapOfZLayerSettings.IsBound (theLayerId))
587 {
588 myMapOfZLayerSettings.ChangeFind (theLayerId) = theSettings;
589 }
590 else
591 {
592 myMapOfZLayerSettings.Bind (theLayerId, theSettings);
593 }
a174a3c5 594}
595
c357e426 596//=======================================================================
597//function : ZLayerSettings
598//purpose :
599//=======================================================================
600Graphic3d_ZLayerSettings OpenGl_GraphicDriver::ZLayerSettings (const Graphic3d_ZLayerId theLayerId)
a174a3c5 601{
c357e426 602 Standard_ASSERT_RAISE (myLayerIds.Contains (theLayerId),
603 "OpenGl_GraphicDriver::ZLayerSettings, "
604 "Layer with theLayerId does not exist");
605
606 return myMapOfZLayerSettings.Find (theLayerId);
a174a3c5 607}
608
609// =======================================================================
c357e426 610// function : Structure
a174a3c5 611// purpose :
612// =======================================================================
c357e426 613Handle(Graphic3d_CStructure) OpenGl_GraphicDriver::CreateStructure (const Handle(Graphic3d_StructureManager)& theManager)
a174a3c5 614{
c357e426 615 Handle(OpenGl_Structure) aStructure = new OpenGl_Structure (theManager);
616 myMapOfStructure.Bind (aStructure->Id, aStructure.operator->());
617 return aStructure;
a174a3c5 618}
619
620// =======================================================================
c357e426 621// function : Structure
a174a3c5 622// purpose :
623// =======================================================================
c357e426 624void OpenGl_GraphicDriver::RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure)
a174a3c5 625{
c357e426 626 OpenGl_Structure* aStructure = NULL;
627 if (!myMapOfStructure.Find (theCStructure->Id, aStructure))
a174a3c5 628 {
c357e426 629 return;
a174a3c5 630 }
c357e426 631
632 myMapOfStructure.UnBind (theCStructure->Id);
633 aStructure->Release (GetSharedContext());
634 theCStructure.Nullify();
a174a3c5 635}
636
637// =======================================================================
c357e426 638// function : View
a174a3c5 639// purpose :
640// =======================================================================
c357e426 641Handle(Graphic3d_CView) OpenGl_GraphicDriver::CreateView (const Handle(Graphic3d_StructureManager)& theMgr)
a174a3c5 642{
c357e426 643 Handle(OpenGl_View) aView = new OpenGl_View (theMgr, this, myCaps, myDeviceLostFlag, &myStateCounter);
644
645 myMapOfView.Add (aView);
646
647 for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
a174a3c5 648 {
c357e426 649 const Graphic3d_ZLayerId aLayerID = aLayerIt.Value();
650 const Graphic3d_ZLayerSettings& aSettings = myMapOfZLayerSettings.Find (aLayerID);
651 aView->AddZLayer (aLayerID);
652 aView->SetZLayerSettings (aLayerID, aSettings);
a174a3c5 653 }
c357e426 654
655 return aView;
a174a3c5 656}
657
658// =======================================================================
c357e426 659// function : RemoveView
a174a3c5 660// purpose :
661// =======================================================================
c357e426 662void OpenGl_GraphicDriver::RemoveView (const Handle(Graphic3d_CView)& theView)
a174a3c5 663{
c357e426 664 Handle(OpenGl_Context) aCtx = GetSharedContext();
665 Handle(OpenGl_View) aView = Handle(OpenGl_View)::DownCast (theView);
666 if (aView.IsNull())
a174a3c5 667 {
c357e426 668 return;
a174a3c5 669 }
a174a3c5 670
c357e426 671 if (!myMapOfView.Remove (aView))
672 {
673 return;
674 }
675
676 Handle(OpenGl_Window) aWindow = aView->GlWindow();
677 if (!aWindow.IsNull()
678 && aWindow->GetGlContext()->MakeCurrent())
a79f67f8 679 {
c357e426 680 aCtx = aWindow->GetGlContext();
681 }
682 else
683 {
684 // try to hijack another context if any
685 const Handle(OpenGl_Context)& anOtherCtx = GetSharedContext();
686 if (!anOtherCtx.IsNull()
687 && anOtherCtx != aWindow->GetGlContext())
688 {
689 aCtx = anOtherCtx;
690 aCtx->MakeCurrent();
691 }
692 }
693
694 aView->ReleaseGlResources (aCtx);
695 if (myMapOfView.IsEmpty())
696 {
697 // The last view removed but some objects still present.
698 // Release GL resources now without object destruction.
699 for (NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
700 aStructIt.More (); aStructIt.Next())
701 {
702 OpenGl_Structure* aStruct = aStructIt.ChangeValue();
703 aStruct->ReleaseGlResources (aCtx);
704 }
705 myDeviceLostFlag = !myMapOfStructure.IsEmpty();
a79f67f8 706 }
a174a3c5 707}
62e1beed 708
709// =======================================================================
c357e426 710// function : Window
62e1beed 711// purpose :
712// =======================================================================
c357e426 713Handle(OpenGl_Window) OpenGl_GraphicDriver::CreateRenderWindow (const Handle(Aspect_Window)& theWindow,
714 const Aspect_RenderingContext theContext)
62e1beed 715{
c357e426 716 Handle(OpenGl_Context) aShareCtx = GetSharedContext();
717 Handle(OpenGl_Window) aWindow = new OpenGl_Window (this, theWindow, theContext, myCaps, aShareCtx);
718 return aWindow;
62e1beed 719}
27f85086 720
c357e426 721//=======================================================================
722//function : ViewExists
723//purpose :
724//=======================================================================
725Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)& AWindow, Handle(Graphic3d_CView)& theView)
27f85086 726{
c357e426 727 Standard_Boolean isExist = Standard_False;
728
729 // Parse the list of views to find
730 // a view with the specified window
731
732#if defined(_WIN32)
733 const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
734 Aspect_Handle TheSpecifiedWindowId = THEWindow->HWindow ();
735#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
736 const Handle(Cocoa_Window) THEWindow = Handle(Cocoa_Window)::DownCast (AWindow);
737 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
738 UIView* TheSpecifiedWindowId = THEWindow->HView();
739 #else
740 NSView* TheSpecifiedWindowId = THEWindow->HView();
741 #endif
d8d01f6e 742#elif defined(__ANDROID__) || defined(__QNX__)
20aeeb7b 743 (void )AWindow;
c357e426 744 int TheSpecifiedWindowId = -1;
745#else
746 const Handle(Xw_Window) THEWindow = Handle(Xw_Window)::DownCast (AWindow);
747 int TheSpecifiedWindowId = int (THEWindow->XWindow ());
748#endif
749
750 NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
751 for(; aViewIt.More(); aViewIt.Next())
27f85086 752 {
c357e426 753 const Handle(OpenGl_View)& aView = aViewIt.Value();
754 if (aView->IsDefined() && aView->IsActive())
755 {
756 const Handle(Aspect_Window) AspectWindow = aView->Window();
757
758#if defined(_WIN32)
759 const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
760 Aspect_Handle TheWindowIdOfView = theWindow->HWindow ();
761#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
762 const Handle(Cocoa_Window) theWindow = Handle(Cocoa_Window)::DownCast (AspectWindow);
763 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
764 UIView* TheWindowIdOfView = theWindow->HView();
765 #else
766 NSView* TheWindowIdOfView = theWindow->HView();
767 #endif
d8d01f6e 768#elif defined(__ANDROID__) || defined(__QNX__)
c357e426 769 int TheWindowIdOfView = 0;
770#else
771 const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
772 int TheWindowIdOfView = int (theWindow->XWindow ());
773#endif // WNT
774 // Comparaison on window IDs
775 if (TheWindowIdOfView == TheSpecifiedWindowId)
776 {
777 isExist = Standard_True;
778 theView = aView;
779 }
780 }
27f85086 781 }
782
c357e426 783 return isExist;
27f85086 784}