diff Framework/Scene2D/Internals/OpenGLTextProgram.cpp @ 947:1091b2adeb5a toa2019081001

Fixed animation frame stopping when returning false + big work on the OpenGL objects to make them lost context-safe + debug code to forcefully tag a context as lost + debug macros
author Benjamin Golinvaux <bgo@osimis.io>
date Sat, 10 Aug 2019 13:07:31 +0200
parents 61ba4b504e9a
children a7351ad54960
line wrap: on
line diff
--- a/Framework/Scene2D/Internals/OpenGLTextProgram.cpp	Tue Aug 06 15:07:23 2019 +0200
+++ b/Framework/Scene2D/Internals/OpenGLTextProgram.cpp	Sat Aug 10 13:07:31 2019 +0200
@@ -60,14 +60,16 @@
     OpenGLTextProgram::OpenGLTextProgram(OpenGL::IOpenGLContext&  context) :
       context_(context)
     {
-
-      context_.MakeCurrent();
+      if (!context_.IsContextLost())
+      {
+        context_.MakeCurrent();
 
-      program_.reset(new OpenGL::OpenGLProgram);
-      program_->CompileShaders(VERTEX_SHADER, FRAGMENT_SHADER);
+        program_.reset(new OpenGL::OpenGLProgram(context_));
+        program_->CompileShaders(VERTEX_SHADER, FRAGMENT_SHADER);
 
-      positionLocation_ = program_->GetAttributeLocation("a_position");
-      textureLocation_ = program_->GetAttributeLocation("a_texcoord");
+        positionLocation_ = program_->GetAttributeLocation("a_position");
+        textureLocation_ = program_->GetAttributeLocation("a_texcoord");
+      }
     }
 
 
@@ -95,26 +97,58 @@
       {
         coordinatesCount_ = coordinates.GetRenderingCoords().size();
 
-        context_.MakeCurrent();
-        glGenBuffers(2, buffers_);
+        if (!context_.IsContextLost())
+        {
+          context_.MakeCurrent();
+          glGenBuffers(2, buffers_);
+          ORTHANC_OPENGL_CHECK("glGenBuffers");
 
-        glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]);
-        glBufferData(GL_ARRAY_BUFFER, sizeof(float) * coordinatesCount_,
-                     &coordinates.GetRenderingCoords() [0], GL_STATIC_DRAW);
+          glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]);
+          ORTHANC_OPENGL_CHECK("glBindBuffer");
+          glBufferData(GL_ARRAY_BUFFER, sizeof(float) * coordinatesCount_,
+            &coordinates.GetRenderingCoords()[0], GL_STATIC_DRAW);
+          ORTHANC_OPENGL_CHECK("glBufferData");
 
-        glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]);
-        glBufferData(GL_ARRAY_BUFFER, sizeof(float) * coordinatesCount_,
-                     &coordinates.GetTextureCoords() [0], GL_STATIC_DRAW);
+          glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]);
+          ORTHANC_OPENGL_CHECK("glBindBuffer");
+          glBufferData(GL_ARRAY_BUFFER, sizeof(float) * coordinatesCount_,
+            &coordinates.GetTextureCoords()[0], GL_STATIC_DRAW);
+          ORTHANC_OPENGL_CHECK("glBufferData");
+        }
       }
     }
 
     
     OpenGLTextProgram::Data::~Data()
     {
-      if (!IsEmpty())
+      if (!context_.IsContextLost() && !IsEmpty())
       {
-        context_.MakeCurrent();
-        glDeleteBuffers(2, buffers_);
+        try
+        {
+          context_.MakeCurrent();
+          ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glDeleteBuffers");
+          glDeleteBuffers(2, buffers_);
+          ORTHANC_OPENGL_CHECK("glDeleteBuffers");
+        }
+        catch (const Orthanc::OrthancException& e)
+        {
+          if (e.HasDetails())
+          {
+            LOG(ERROR) << "OrthancException in ~Data: " << e.What() << " Details: " << e.GetDetails();
+          }
+          else
+          {
+            LOG(ERROR) << "OrthancException in ~Data: " << e.What();
+          }
+        }
+        catch (const std::exception& e)
+        {
+          LOG(ERROR) << "std::exception in ~Data: " << e.what();
+        }
+        catch (...)
+        {
+          LOG(ERROR) << "Unknown exception in ~Data";
+        }
       }
     }
 
@@ -131,7 +165,6 @@
       }
     }
 
-    
     GLuint OpenGLTextProgram::Data::GetTextureLocationsBuffer() const
     {
       if (IsEmpty())
@@ -144,12 +177,11 @@
       }
     }
 
-
     void OpenGLTextProgram::Apply(OpenGL::OpenGLTexture& fontTexture,
                                   const Data& data,
                                   const AffineTransform2D& transform)
     {
-      if (!data.IsEmpty())
+      if (!context_.IsContextLost() && !data.IsEmpty())
       {
         context_.MakeCurrent();
         program_->Use();