0028668: Point Cloud Rendering - standard selection API returns invalid values
[occt.git] / src / OpenGl / OpenGl_GraphicDriver.cxx
index 2047328..b60b7cd 100644 (file)
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
+#if defined(_WIN32)
+  #include <windows.h>
+#endif
+
 #include <OpenGl_GraphicDriver.hxx>
 #include <OpenGl_Context.hxx>
 #include <OpenGl_Flipper.hxx>
 #include <OpenGl_View.hxx>
 #include <OpenGl_StencilTest.hxx>
 #include <OpenGl_Text.hxx>
-#include <OpenGl_Trihedron.hxx>
 #include <OpenGl_Workspace.hxx>
 
 #include <Aspect_GraphicDeviceDefinitionError.hxx>
 #include <Aspect_IdentDefinitionError.hxx>
+#include <Graphic3d_StructureManager.hxx>
 #include <Message_Messenger.hxx>
 #include <OSD_Environment.hxx>
 #include <Standard_NotImplemented.hxx>
@@ -44,13 +48,68 @@ IMPLEMENT_STANDARD_RTTIEXT(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
   #include <X11/Xlib.h> // XOpenDisplay()
 #endif
 
-#if defined(HAVE_EGL) || defined(__ANDROID__) || defined(__QNX__)
+#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
   #include <EGL/egl.h>
+  #ifndef EGL_OPENGL_ES3_BIT
+    #define EGL_OPENGL_ES3_BIT 0x00000040
+  #endif
 #endif
 
 namespace
 {
   static const Handle(OpenGl_Context) TheNullGlCtx;
+
+#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
+  //! Wrapper over eglChooseConfig() called with preferred defaults.
+  static EGLConfig chooseEglSurfConfig (EGLDisplay theDisplay)
+  {
+    EGLint aConfigAttribs[] =
+    {
+      EGL_RED_SIZE,     8,
+      EGL_GREEN_SIZE,   8,
+      EGL_BLUE_SIZE,    8,
+      EGL_ALPHA_SIZE,   0,
+      EGL_DEPTH_SIZE,   24,
+      EGL_STENCIL_SIZE, 8,
+    #if defined(GL_ES_VERSION_2_0)
+      EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
+    #else
+      EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
+    #endif
+      EGL_NONE
+    };
+
+    EGLConfig aCfg = NULL;
+    EGLint aNbConfigs = 0;
+    for (Standard_Integer aGlesVer = 3; aGlesVer >= 2; --aGlesVer)
+    {
+    #if defined(GL_ES_VERSION_2_0)
+      aConfigAttribs[6 * 2 + 1] = aGlesVer == 3 ? EGL_OPENGL_ES3_BIT : EGL_OPENGL_ES2_BIT;
+    #else
+      if (aGlesVer == 2)
+      {
+        break;
+      }
+    #endif
+
+      if (eglChooseConfig (theDisplay, aConfigAttribs, &aCfg, 1, &aNbConfigs) == EGL_TRUE
+       && aCfg != NULL)
+      {
+        return aCfg;
+      }
+      eglGetError();
+
+      aConfigAttribs[4 * 2 + 1] = 16; // try config with smaller depth buffer
+      if (eglChooseConfig (theDisplay, aConfigAttribs, &aCfg, 1, &aNbConfigs) == EGL_TRUE
+       && aCfg != NULL)
+      {
+        return aCfg;
+      }
+      eglGetError();
+    }
+    return aCfg;
+  }
+#endif
 }
 
 // =======================================================================
@@ -61,7 +120,7 @@ OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnectio
                                             const Standard_Boolean                  theToInitialize)
 : Graphic3d_GraphicDriver (theDisp),
   myIsOwnContext (Standard_False),
-#if defined(HAVE_EGL) || defined(__ANDROID__) || defined(__QNX__)
+#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
   myEglDisplay ((Aspect_Display )EGL_NO_DISPLAY),
   myEglContext ((Aspect_RenderingContext )EGL_NO_CONTEXT),
   myEglConfig  (NULL),
@@ -73,7 +132,7 @@ OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnectio
 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
   if (myDisplayConnection.IsNull())
   {
-    //Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_GraphicDriver: cannot connect to X server!");
+    //throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: cannot connect to X server!");
     return;
   }
 
@@ -82,7 +141,7 @@ OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnectio
              || ::getenv ("CALL_SYNCHRO_X")  != NULL;
   XSynchronize (aDisplay, toSync);
 
-#if !defined(HAVE_EGL)
+#if !defined(HAVE_EGL) && !defined(HAVE_GLES2)
   // does the server know about OpenGL & GLX?
   int aDummy;
   if (!XQueryExtension (aDisplay, "GLX", &aDummy, &aDummy, &aDummy))
@@ -94,50 +153,8 @@ OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnectio
   if (theToInitialize
   && !InitContext())
   {
-    Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_GraphicDriver: default context can not be initialized!");
-  }
-
-  // default layers are always presented in display layer sequence it can not be removed
-  Graphic3d_ZLayerSettings anUnderlaySettings;
-  anUnderlaySettings.Flags = 0;
-  anUnderlaySettings.IsImmediate = false;
-  anUnderlaySettings.UseEnvironmentTexture = false;
-  myLayerIds.Add             (Graphic3d_ZLayerId_BotOSD);
-  myLayerSeq.Append          (Graphic3d_ZLayerId_BotOSD);
-  myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_BotOSD, anUnderlaySettings);
-
-  Graphic3d_ZLayerSettings aDefSettings;
-  aDefSettings.Flags = Graphic3d_ZLayerDepthTest
-                     | Graphic3d_ZLayerDepthWrite;
-  aDefSettings.IsImmediate = false;
-  myLayerIds.Add             (Graphic3d_ZLayerId_Default);
-  myLayerSeq.Append          (Graphic3d_ZLayerId_Default);
-  myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Default, aDefSettings);
-
-  Graphic3d_ZLayerSettings aTopSettings;
-  aTopSettings.Flags = Graphic3d_ZLayerDepthTest
-                     | Graphic3d_ZLayerDepthWrite;
-  aTopSettings.IsImmediate = true;
-  myLayerIds.Add             (Graphic3d_ZLayerId_Top);
-  myLayerSeq.Append          (Graphic3d_ZLayerId_Top);
-  myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Top, aTopSettings);
-
-  Graphic3d_ZLayerSettings aTopmostSettings;
-  aTopmostSettings.Flags = Graphic3d_ZLayerDepthTest
-                         | Graphic3d_ZLayerDepthWrite
-                         | Graphic3d_ZLayerDepthClear;
-  aTopmostSettings.IsImmediate = true;
-  myLayerIds.Add             (Graphic3d_ZLayerId_Topmost);
-  myLayerSeq.Append          (Graphic3d_ZLayerId_Topmost);
-  myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_Topmost, aTopmostSettings);
-
-  Graphic3d_ZLayerSettings anOsdSettings;
-  anOsdSettings.Flags = 0;
-  anOsdSettings.IsImmediate = true;
-  anOsdSettings.UseEnvironmentTexture = false;
-  myLayerIds.Add             (Graphic3d_ZLayerId_TopOSD);
-  myLayerSeq.Append          (Graphic3d_ZLayerId_TopOSD);
-  myMapOfZLayerSettings.Bind (Graphic3d_ZLayerId_TopOSD, anOsdSettings);
+    throw Aspect_GraphicDeviceDefinitionError("OpenGl_GraphicDriver: default context can not be initialized!");
+  }
 }
 
 // =======================================================================
@@ -159,7 +176,7 @@ void OpenGl_GraphicDriver::ReleaseContext()
   for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
        aViewIter.More(); aViewIter.Next())
   {
-    const Handle(OpenGl_View)& aView = aViewIter.ChangeValue();
+    const Handle(OpenGl_View)& aView = aViewIter.Value();
     const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
     if (aWindow.IsNull())
     {
@@ -181,7 +198,7 @@ void OpenGl_GraphicDriver::ReleaseContext()
   for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
        aViewIter.More(); aViewIter.Next())
   {
-    const Handle(OpenGl_View)& aView = aViewIter.ChangeValue();
+    const Handle(OpenGl_View)& aView = aViewIter.Value();
     aView->ReleaseGlResources (aCtxShared);
   }
 
@@ -191,12 +208,17 @@ void OpenGl_GraphicDriver::ReleaseContext()
     OpenGl_Structure* aStruct = aStructIt.ChangeValue();
     aStruct->ReleaseGlResources (aCtxShared);
   }
-  myDeviceLostFlag = myDeviceLostFlag || !myMapOfStructure.IsEmpty();
 
+  const bool isDeviceLost = !myMapOfStructure.IsEmpty();
   for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView);
        aViewIter.More(); aViewIter.Next())
   {
-    const Handle(OpenGl_View)& aView = aViewIter.ChangeValue();
+    const Handle(OpenGl_View)& aView = aViewIter.Value();
+    if (isDeviceLost)
+    {
+      aView->StructureManager()->SetDeviceLost();
+    }
+
     const Handle(OpenGl_Window)& aWindow = aView->GlWindow();
     if (aWindow.IsNull())
     {
@@ -206,7 +228,7 @@ void OpenGl_GraphicDriver::ReleaseContext()
     aWindow->GetGlContext()->forcedRelease();
   }
 
-#if defined(HAVE_EGL) || defined(__ANDROID__) || defined(__QNX__)
+#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
   if (myIsOwnContext)
   {
     if (myEglContext != (Aspect_RenderingContext )EGL_NO_CONTEXT)
@@ -241,7 +263,7 @@ void OpenGl_GraphicDriver::ReleaseContext()
 Standard_Boolean OpenGl_GraphicDriver::InitContext()
 {
   ReleaseContext();
-#if defined(HAVE_EGL) || defined(__ANDROID__) || defined(__QNX__)
+#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
 
 #if !defined(_WIN32) && !defined(__ANDROID__) && !defined(__QNX__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
   if (myDisplayConnection.IsNull())
@@ -266,47 +288,26 @@ Standard_Boolean OpenGl_GraphicDriver::InitContext()
     return Standard_False;
   }
 
-  EGLint aConfigAttribs[] =
+  myEglConfig = chooseEglSurfConfig ((EGLDisplay )myEglDisplay);
+  if (myEglConfig == NULL)
   {
-    EGL_RED_SIZE,     8,
-    EGL_GREEN_SIZE,   8,
-    EGL_BLUE_SIZE,    8,
-    EGL_ALPHA_SIZE,   0,
-    EGL_DEPTH_SIZE,   24,
-    EGL_STENCIL_SIZE, 8,
-  #if defined(GL_ES_VERSION_2_0)
-    EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
-  #else
-    EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
-  #endif
-    EGL_NONE
-  };
-
-  EGLint aNbConfigs = 0;
-  if (eglChooseConfig ((EGLDisplay )myEglDisplay, aConfigAttribs, &myEglConfig, 1, &aNbConfigs) != EGL_TRUE
-   || myEglConfig == NULL)
-  {
-    eglGetError();
-    aConfigAttribs[4 * 2 + 1] = 16; // try config with smaller depth buffer
-    if (eglChooseConfig ((EGLDisplay )myEglDisplay, aConfigAttribs, &myEglConfig, 1, &aNbConfigs) != EGL_TRUE
-     || myEglConfig == NULL)
-    {
-      ::Message::DefaultMessenger()->Send ("Error: EGL does not provide compatible configurations!", Message_Fail);
-      return Standard_False;
-    }
+    ::Message::DefaultMessenger()->Send ("Error: EGL does not provide compatible configurations!", Message_Fail);
+    return Standard_False;
   }
 
 #if defined(GL_ES_VERSION_2_0)
-  EGLint anEglCtxAttribs[] =
-  {
-    EGL_CONTEXT_CLIENT_VERSION, 2,
-    EGL_NONE
-  };
+  EGLint anEglCtxAttribs3[] = { EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE, EGL_NONE };
+  EGLint anEglCtxAttribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };
   if (eglBindAPI (EGL_OPENGL_ES_API) != EGL_TRUE)
   {
     ::Message::DefaultMessenger()->Send ("Error: EGL does not provide OpenGL ES client!", Message_Fail);
     return Standard_False;
   }
+  myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs3);
+  if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
+  {
+    myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs2);
+  }
 #else
   EGLint* anEglCtxAttribs = NULL;
   if (eglBindAPI (EGL_OPENGL_API) != EGL_TRUE)
@@ -314,25 +315,26 @@ Standard_Boolean OpenGl_GraphicDriver::InitContext()
     ::Message::DefaultMessenger()->Send ("Error: EGL does not provide OpenGL client!", Message_Fail);
     return Standard_False;
   }
+  myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs);
 #endif
 
-  myEglContext = (Aspect_RenderingContext )eglCreateContext ((EGLDisplay )myEglDisplay, myEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs);
   if ((EGLContext )myEglContext == EGL_NO_CONTEXT)
   {
     ::Message::DefaultMessenger()->Send ("Error: EGL is unable to create OpenGL context!", Message_Fail);
     return Standard_False;
   }
-  if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, (EGLContext )myEglContext) != EGL_TRUE)
-  {
-    ::Message::DefaultMessenger()->Send ("Error: EGL is unable bind OpenGL context!", Message_Fail);
-    return Standard_False;
-  }
+  // eglMakeCurrent() fails or even crash with EGL_NO_SURFACE on some implementations
+  //if (eglMakeCurrent ((EGLDisplay )myEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, (EGLContext )myEglContext) != EGL_TRUE)
+  //{
+  //  ::Message::DefaultMessenger()->Send ("Error: EGL is unable bind OpenGL context!", Message_Fail);
+  //  return Standard_False;
+  //}
 #endif
   myIsOwnContext = Standard_True;
   return Standard_True;
 }
 
-#if defined(HAVE_EGL) || defined(__ANDROID__) || defined(__QNX__)
+#if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
 // =======================================================================
 // function : InitEglContext
 // purpose  :
@@ -350,45 +352,77 @@ Standard_Boolean OpenGl_GraphicDriver::InitEglContext (Aspect_Display          t
 #endif
 
   if ((EGLDisplay )theEglDisplay == EGL_NO_DISPLAY
-   || (EGLContext )theEglContext == EGL_NO_CONTEXT
-   || theEglConfig == NULL)
+   || (EGLContext )theEglContext == EGL_NO_CONTEXT)
   {
     return Standard_False;
   }
   myEglDisplay = theEglDisplay;
   myEglContext = theEglContext;
   myEglConfig  = theEglConfig;
+  if (theEglConfig == NULL)
+  {
+    myEglConfig = chooseEglSurfConfig ((EGLDisplay )myEglDisplay);
+    if (myEglConfig == NULL)
+    {
+      ::Message::DefaultMessenger()->Send ("Error: EGL does not provide compatible configurations!", Message_Fail);
+      return Standard_False;
+    }
+  }
   return Standard_True;
 }
 #endif
 
 // =======================================================================
-// function : InquireLightLimit
-// purpose  :
-// =======================================================================
-Standard_Integer OpenGl_GraphicDriver::InquireLightLimit()
-{
-  return OpenGLMaxLights;
-}
-
-// =======================================================================
-// function : InquireViewLimit
-// purpose  :
-// =======================================================================
-Standard_Integer OpenGl_GraphicDriver::InquireViewLimit()
-{
-  return 10000;
-}
-
-// =======================================================================
-// function : InquirePlaneLimit
+// function : InquireLimit
 // purpose  :
 // =======================================================================
-Standard_Integer OpenGl_GraphicDriver::InquirePlaneLimit()
+Standard_Integer OpenGl_GraphicDriver::InquireLimit (const Graphic3d_TypeOfLimit theType) const
 {
-  // NOTE the 2 first planes are reserved for ZClipping
   const Handle(OpenGl_Context)& aCtx = GetSharedContext();
-  return aCtx.IsNull() ? 0 : Max (aCtx->MaxClipPlanes() - 2, 0);
+  switch (theType)
+  {
+    case Graphic3d_TypeOfLimit_MaxNbLights:
+      return Graphic3d_ShaderProgram::THE_MAX_LIGHTS_DEFAULT;
+    case Graphic3d_TypeOfLimit_MaxNbClipPlanes:
+      return !aCtx.IsNull() ? aCtx->MaxClipPlanes() : 0;
+    case Graphic3d_TypeOfLimit_MaxNbViews:
+      return 10000;
+    case Graphic3d_TypeOfLimit_MaxTextureSize:
+      return !aCtx.IsNull() ? aCtx->MaxTextureSize() : 1024;
+    case Graphic3d_TypeOfLimit_MaxCombinedTextureUnits:
+      return !aCtx.IsNull() ? aCtx->MaxCombinedTextureUnits() : 1;
+    case Graphic3d_TypeOfLimit_MaxMsaa:
+      return !aCtx.IsNull() ? aCtx->MaxMsaaSamples() : 0;
+    case Graphic3d_TypeOfLimit_MaxViewDumpSizeX:
+      return !aCtx.IsNull() ? aCtx->MaxDumpSizeX() : 1024;
+    case Graphic3d_TypeOfLimit_MaxViewDumpSizeY:
+      return !aCtx.IsNull() ? aCtx->MaxDumpSizeY() : 1024;
+    case Graphic3d_TypeOfLimit_HasRayTracing:
+      return (!aCtx.IsNull() && aCtx->HasRayTracing()) ? 1 : 0;
+    case Graphic3d_TypeOfLimit_HasRayTracingTextures:
+      return (!aCtx.IsNull() && aCtx->HasRayTracingTextures()) ? 1 : 0;
+    case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSampling:
+      return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSampling()) ? 1 : 0;
+    case Graphic3d_TypeOfLimit_HasRayTracingAdaptiveSamplingAtomic:
+      return (!aCtx.IsNull() && aCtx->HasRayTracingAdaptiveSamplingAtomic()) ? 1 : 0;
+    case Graphic3d_TypeOfLimit_HasBlendedOit:
+      return (!aCtx.IsNull()
+            && aCtx->hasDrawBuffers != OpenGl_FeatureNotAvailable
+            && (aCtx->hasFloatBuffer != OpenGl_FeatureNotAvailable || aCtx->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable)) ? 1 : 0;
+    case Graphic3d_TypeOfLimit_HasBlendedOitMsaa:
+      return (!aCtx.IsNull()
+            && aCtx->hasSampleVariables != OpenGl_FeatureNotAvailable
+            && (InquireLimit (Graphic3d_TypeOfLimit_HasBlendedOit) == 1)) ? 1 : 0;
+    case Graphic3d_TypeOfLimit_HasFlatShading:
+      return !aCtx.IsNull() && aCtx->hasFlatShading != OpenGl_FeatureNotAvailable ? 1 : 0;
+    case Graphic3d_TypeOfLimit_IsWorkaroundFBO:
+      return !aCtx.IsNull() && aCtx->MaxTextureSize() != aCtx->MaxDumpSizeX() ? 1 : 0;
+    case Graphic3d_TypeOfLimit_HasMeshEdges:
+      return !aCtx.IsNull() && aCtx->hasGeometryStage != OpenGl_FeatureNotAvailable ? 1 : 0;
+    case Graphic3d_TypeOfLimit_NB:
+      return 0;
+  }
+  return 0;
 }
 
 // =======================================================================
@@ -413,23 +447,26 @@ void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
 // function : GetSharedContext
 // purpose  :
 // =======================================================================
-const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext() const
+const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext (bool theBound) const
 {
   if (myMapOfView.IsEmpty())
   {
     return TheNullGlCtx;
   }
 
-  NCollection_Map<Handle(OpenGl_View)>::Iterator anIter (myMapOfView);
-  for (; anIter.More(); anIter.Next())
+  for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); aViewIter.More(); aViewIter.Next())
   {
-    Handle(OpenGl_Window) aWindow = anIter.Value()->GlWindow();
-    if (aWindow.IsNull())
+    if (const Handle(OpenGl_Window)& aWindow = aViewIter.Value()->GlWindow())
     {
-      continue;
+      if (!theBound)
+      {
+        return aWindow->GetGlContext();
+      }
+      else if (aWindow->GetGlContext()->IsCurrent())
+      {
+        return aWindow->GetGlContext();
+      }
     }
-
-    return aWindow->GetGlContext();
   }
 
   return TheNullGlCtx;
@@ -482,10 +519,9 @@ void OpenGl_GraphicDriver::TextSize (const Handle(Graphic3d_CView)& theView,
   const Standard_ShortReal aHeight = (theHeight < 2.0f) ? DefaultTextHeight() : theHeight;
   OpenGl_TextParam aTextParam;
   aTextParam.Height = (int )aHeight;
-  OpenGl_AspectText aTextAspect;
-  aTextAspect.Aspect()->SetSpace (0.3);
+  OpenGl_Aspects aTextAspect;
   TCollection_ExtendedString anExtText = theText;
-  NCollection_String aText = (Standard_Utf16Char* )anExtText.ToExtString();
+  NCollection_String aText (anExtText.ToExtString());
   OpenGl_Text::StringSize(aCtx, aText, aTextAspect, aTextParam, theView->RenderingParams().Resolution, theWidth, theAscent, theDescent);
 }
 
@@ -502,11 +538,11 @@ void OpenGl_GraphicDriver::AddZLayer (const Graphic3d_ZLayerId theLayerId)
                            "negative and zero IDs are reserved");
   }
 
-  myLayerIds.Add    (theLayerId);
-  myLayerSeq.Append (theLayerId);
+  myLayerIds.Add (theLayerId);
 
   // Default z-layer settings
   myMapOfZLayerSettings.Bind (theLayerId, Graphic3d_ZLayerSettings());
+  addZLayerIndex (theLayerId);
 
   // Add layer to all views
   NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
@@ -548,11 +584,11 @@ void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
   }
 
   // Remove index
-  for (int aIdx = 1; aIdx <= myLayerSeq.Length (); aIdx++)
+  for (TColStd_SequenceOfInteger::Iterator aLayerIt (myLayerSeq); aLayerIt.More(); aLayerIt.Next())
   {
-    if (myLayerSeq (aIdx) == theLayerId)
+    if (aLayerIt.Value() == theLayerId)
     {
-      myLayerSeq.Remove (aIdx);
+      myLayerSeq.Remove (aLayerIt);
       break;
     }
   }
@@ -561,15 +597,6 @@ void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
   myLayerIds.Remove  (theLayerId);
 }
 
-//=======================================================================
-//function : ZLayers
-//purpose  :
-//=======================================================================
-void OpenGl_GraphicDriver::ZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
-{
-  theLayerSeq.Assign (myLayerSeq);
-}
-
 //=======================================================================
 //function : SetZLayerSettings
 //purpose  :
@@ -577,34 +604,13 @@ void OpenGl_GraphicDriver::ZLayers (TColStd_SequenceOfInteger& theLayerSeq) cons
 void OpenGl_GraphicDriver::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
                                               const Graphic3d_ZLayerSettings& theSettings)
 {
+  base_type::SetZLayerSettings (theLayerId, theSettings);
+
   // Change Z layer settings in all managed views
-  NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView);
-  for (; aViewIt.More(); aViewIt.Next())
+  for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIt (myMapOfView); aViewIt.More(); aViewIt.Next())
   {
     aViewIt.Value()->SetZLayerSettings (theLayerId, theSettings);
   }
-
-  if (myMapOfZLayerSettings.IsBound (theLayerId))
-  {
-    myMapOfZLayerSettings.ChangeFind (theLayerId) = theSettings;
-  }
-  else
-  {
-    myMapOfZLayerSettings.Bind (theLayerId, theSettings);
-  }
-}
-
-//=======================================================================
-//function : ZLayerSettings
-//purpose  :
-//=======================================================================
-Graphic3d_ZLayerSettings OpenGl_GraphicDriver::ZLayerSettings (const Graphic3d_ZLayerId theLayerId)
-{
-  Standard_ASSERT_RAISE (myLayerIds.Contains (theLayerId),
-                         "OpenGl_GraphicDriver::ZLayerSettings, "
-                         "Layer with theLayerId does not exist");
-
-  return myMapOfZLayerSettings.Find (theLayerId);
 }
 
 // =======================================================================
@@ -641,7 +647,7 @@ void OpenGl_GraphicDriver::RemoveStructure (Handle(Graphic3d_CStructure)& theCSt
 // =======================================================================
 Handle(Graphic3d_CView) OpenGl_GraphicDriver::CreateView (const Handle(Graphic3d_StructureManager)& theMgr)
 {
-  Handle(OpenGl_View) aView = new OpenGl_View (theMgr, this, myCaps, myDeviceLostFlag, &myStateCounter);
+  Handle(OpenGl_View) aView = new OpenGl_View (theMgr, this, myCaps, &myStateCounter);
 
   myMapOfView.Add (aView);
 
@@ -703,7 +709,11 @@ void OpenGl_GraphicDriver::RemoveView (const Handle(Graphic3d_CView)& theView)
       OpenGl_Structure* aStruct = aStructIt.ChangeValue();
       aStruct->ReleaseGlResources (aCtx);
     }
-    myDeviceLostFlag = !myMapOfStructure.IsEmpty();
+
+    if (!myMapOfStructure.IsEmpty())
+    {
+      aView->StructureManager()->SetDeviceLost();
+    }
   }
 }
 
@@ -730,7 +740,7 @@ Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)&
   // Parse the list of views to find
   // a view with the specified window
 
-#if defined(_WIN32)
+#if defined(_WIN32) && !defined(OCCT_UWP)
   const Handle(WNT_Window) THEWindow = Handle(WNT_Window)::DownCast (AWindow);
   Aspect_Handle TheSpecifiedWindowId = THEWindow->HWindow ();
 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
@@ -740,7 +750,7 @@ Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)&
   #else
     NSView* TheSpecifiedWindowId = THEWindow->HView();
   #endif
-#elif defined(__ANDROID__) || defined(__QNX__)
+#elif defined(__ANDROID__) || defined(__QNX__) || defined(OCCT_UWP)
   (void )AWindow;
   int TheSpecifiedWindowId = -1;
 #else
@@ -756,7 +766,7 @@ Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)&
     {
       const Handle(Aspect_Window) AspectWindow = aView->Window();
 
-#if defined(_WIN32)
+#if defined(_WIN32) && !defined(OCCT_UWP)
       const Handle(WNT_Window) theWindow = Handle(WNT_Window)::DownCast (AspectWindow);
       Aspect_Handle TheWindowIdOfView = theWindow->HWindow ();
 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
@@ -766,7 +776,7 @@ Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)&
       #else
         NSView* TheWindowIdOfView = theWindow->HView();
       #endif
-#elif defined(__ANDROID__) || defined(__QNX__)
+#elif defined(__ANDROID__) || defined(__QNX__) || defined(OCCT_UWP)
       int TheWindowIdOfView = 0;
 #else
       const Handle(Xw_Window) theWindow = Handle(Xw_Window)::DownCast (AspectWindow);
@@ -783,3 +793,24 @@ Standard_Boolean OpenGl_GraphicDriver::ViewExists (const Handle(Aspect_Window)&
 
   return isExist;
 }
+
+//=======================================================================
+//function : setDeviceLost
+//purpose  :
+//=======================================================================
+void OpenGl_GraphicDriver::setDeviceLost()
+{
+  if (myMapOfStructure.IsEmpty())
+  {
+    return;
+  }
+
+  for (NCollection_Map<Handle(OpenGl_View)>::Iterator aViewIter (myMapOfView); aViewIter.More(); aViewIter.Next())
+  {
+    const Handle(OpenGl_View)& aView = aViewIter.Value();
+    if (aView->myWasRedrawnGL)
+    {
+      aView->StructureManager()->SetDeviceLost();
+    }
+  }
+}