Multi-level context caching
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-framebuffer.cpp
index 30ef207..d909146 100644 (file)
@@ -88,15 +88,18 @@ Framebuffer::Framebuffer(const Graphics::FramebufferCreateInfo& createInfo, Grap
   mController.AddFramebuffer(*this);
 }
 
+Framebuffer::~Framebuffer() = default;
+
 bool Framebuffer::InitializeResource()
 {
-  if(!mInitialized)
+  auto context = mController.GetCurrentContext();
+  auto gl      = mController.GetGL();
+  if(gl && context && !mInitialized)
   {
     mInitialized = true;
-    auto gl      = mController.GetGL();
 
-    gl->GenFramebuffers(1, &mFramebufferId);
-    gl->BindFramebuffer(GL_FRAMEBUFFER, mFramebufferId);
+    context->GenFramebuffers(1, &mFramebufferId);
+    context->BindFrameBuffer(GL_FRAMEBUFFER, mFramebufferId);
 
     for(Graphics::ColorAttachment& attachment : mCreateInfo.colorAttachments)
     {
@@ -104,7 +107,7 @@ bool Framebuffer::InitializeResource()
     }
 
     // @todo is this per framebuffer, or more immediate state that needs setting when framebuffer changed?
-    gl->DrawBuffers(mCreateInfo.colorAttachments.size(), COLOR_ATTACHMENTS);
+    context->DrawBuffers(mCreateInfo.colorAttachments.size(), COLOR_ATTACHMENTS);
 
     if(mCreateInfo.depthStencilAttachment.depthTexture)
     {
@@ -133,7 +136,8 @@ bool Framebuffer::InitializeResource()
 
       AttachTexture(stencilTexture, attachmentId, 0, mCreateInfo.depthStencilAttachment.stencilLevel);
     }
-    gl->BindFramebuffer(GL_FRAMEBUFFER, 0);
+
+    context->BindFrameBuffer(GL_FRAMEBUFFER, 0);
   }
 
   return mInitialized;
@@ -141,8 +145,9 @@ bool Framebuffer::InitializeResource()
 
 void Framebuffer::DestroyResource()
 {
-  auto gl = mController.GetGL();
-  if(mInitialized)
+  auto context = mController.GetCurrentContext();
+  auto gl      = mController.GetGL();
+  if(gl && context && mInitialized)
   {
     if(mDepthBufferId)
     {
@@ -153,7 +158,8 @@ void Framebuffer::DestroyResource()
       gl->DeleteRenderbuffers(1, &mStencilBufferId);
     }
 
-    gl->DeleteFramebuffers(1, &mFramebufferId);
+    context->DeleteFramebuffers(1, &mFramebufferId);
+
     mFramebufferId = 0u;
     mInitialized   = false;
   }
@@ -166,22 +172,45 @@ void Framebuffer::DiscardResource()
 
 void Framebuffer::Bind() const
 {
-  auto gl = mController.GetGL();
-  gl->BindFramebuffer(GL_FRAMEBUFFER, mFramebufferId);
+  auto context = mController.GetCurrentContext();
+  auto gl      = mController.GetGL();
+
+  if(gl && context)
+  {
+    context->BindFrameBuffer(GL_FRAMEBUFFER, mFramebufferId);
+  }
 }
 
 void Framebuffer::AttachTexture(const Graphics::Texture* texture, uint32_t attachmentId, uint32_t layerId, uint32_t levelId)
 {
-  auto gl              = mController.GetGL();
-  auto graphicsTexture = static_cast<const GLES::Texture*>(texture);
-  if(graphicsTexture->GetCreateInfo().textureType == Graphics::TextureType::TEXTURE_2D)
-  {
-    gl->FramebufferTexture2D(GL_FRAMEBUFFER, attachmentId, graphicsTexture->GetGlTarget(), graphicsTexture->GetGLTexture(), levelId);
-  }
-  else
+  auto gl = mController.GetGL();
+  if(gl)
   {
-    gl->FramebufferTexture2D(GL_FRAMEBUFFER, attachmentId, GL_TEXTURE_CUBE_MAP_POSITIVE_X + layerId, graphicsTexture->GetGLTexture(), levelId);
+    auto graphicsTexture = static_cast<const GLES::Texture*>(texture);
+    if(graphicsTexture->GetCreateInfo().textureType == Graphics::TextureType::TEXTURE_2D)
+    {
+      gl->FramebufferTexture2D(GL_FRAMEBUFFER, attachmentId, graphicsTexture->GetGlTarget(), graphicsTexture->GetGLTexture(), levelId);
+    }
+    else
+    {
+      gl->FramebufferTexture2D(GL_FRAMEBUFFER, attachmentId, GL_TEXTURE_CUBE_MAP_POSITIVE_X + layerId, graphicsTexture->GetGLTexture(), levelId);
+    }
   }
 }
 
+uint32_t Framebuffer::GetGlFramebufferId() const
+{
+  return mFramebufferId;
+}
+
+uint32_t Framebuffer::GetGlDepthBufferId() const
+{
+  return mDepthBufferId;
+}
+
+uint32_t Framebuffer::GetGlStencilBufferId() const
+{
+  return mStencilBufferId;
+}
+
 } //namespace Dali::Graphics::GLES