Merge "[AT-SPI] Rework intercepting key events" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-buffer.cpp
index 3e4805a..d2fbaaf 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -50,6 +50,35 @@ Buffer::Buffer(const Graphics::BufferCreateInfo& createInfo, Graphics::EglGraphi
   controller.AddBuffer(*this);
 }
 
+bool Buffer::TryRecycle(const Graphics::BufferCreateInfo& createInfo, Graphics::EglGraphicsController& controller)
+{
+  // Compare whether specs are same and the buffer is allocated
+  mSetForGLRecycling = false;
+
+  // if different buffer spec, we need new buffer
+  if(!(createInfo.size == mCreateInfo.size &&
+       createInfo.allocationCallbacks == mCreateInfo.allocationCallbacks &&
+       createInfo.propertiesFlags == mCreateInfo.propertiesFlags &&
+       createInfo.usage == mCreateInfo.usage &&
+       createInfo.nextExtension == mCreateInfo.nextExtension))
+  {
+    return false;
+  }
+
+  // GL resource hasn't been allocated yet, we need new buffer
+  if(mBufferId == 0)
+  {
+    return false;
+  }
+
+  // Make sure the buffer will be reinitialized
+  controller.AddBuffer(*this);
+
+  mSetForGLRecycling = true;
+
+  return true;
+}
+
 bool Buffer::InitializeResource()
 {
   // CPU allocated uniform buffer is a special "compatibility" mode
@@ -63,6 +92,8 @@ bool Buffer::InitializeResource()
     InitializeGPUBuffer();
   }
 
+  // make sure recycling mode is disabled after (re)initializing resource
+  mSetForGLRecycling = false;
   return true;
 }
 
@@ -71,6 +102,13 @@ void Buffer::InitializeCPUBuffer()
   // Just allocate memory
   // @TODO put better CPU memory management in place
   const auto allocators = GetCreateInfo().allocationCallbacks;
+
+  // Early out if we recycle the buffer
+  if(mBufferPtr && mSetForGLRecycling)
+  {
+    return;
+  }
+
   if(allocators)
   {
     mBufferPtr = allocators->allocCallback(mCreateInfo.size, 0, allocators->userData);
@@ -78,15 +116,29 @@ void Buffer::InitializeCPUBuffer()
   else
   {
     mBufferPtr = malloc(mCreateInfo.size);
+    if(DALI_UNLIKELY(mBufferPtr == nullptr))
+    {
+      DALI_LOG_ERROR("malloc is failed. request malloc size : %u\n", mCreateInfo.size);
+    }
   }
 }
 
 void Buffer::InitializeGPUBuffer()
 {
-  auto gl = mController.GetGL();
-  gl->GenBuffers(1, &mBufferId);
-  gl->BindBuffer(GL_ARRAY_BUFFER, mBufferId);
-  gl->BufferData(GL_ARRAY_BUFFER, mCreateInfo.size, nullptr, GL_STATIC_DRAW);
+  auto context = mController.GetCurrentContext();
+  auto gl      = mController.GetGL();
+  if(!gl || !context)
+  {
+    return;
+  }
+
+  // If mBufferId is already set and we recycling the buffer (orphaning)
+  if(!mSetForGLRecycling && !mBufferId)
+  {
+    gl->GenBuffers(1, &mBufferId);
+  }
+  context->BindBuffer(GL_ARRAY_BUFFER, mBufferId);
+  gl->BufferData(GL_ARRAY_BUFFER, GLsizeiptr(mCreateInfo.size), nullptr, GL_STATIC_DRAW);
 }
 
 void Buffer::DestroyResource()
@@ -123,7 +175,12 @@ void Buffer::DiscardResource()
 
 void Buffer::Bind(Graphics::BufferUsage bindingTarget) const
 {
-  auto gl = mController.GetGL();
+  auto context = mController.GetCurrentContext();
+  auto gl      = mController.GetGL();
+  if(!gl || !context)
+  {
+    return;
+  }
 
   // CPU allocated buffer may be bound only as Uniform Buffer
   // on special binding point
@@ -142,12 +199,12 @@ void Buffer::Bind(Graphics::BufferUsage bindingTarget) const
     {
       case Graphics::BufferUsage::VERTEX_BUFFER:
       {
-        gl->BindBuffer(GL_ARRAY_BUFFER, mBufferId);
+        context->BindBuffer(GL_ARRAY_BUFFER, mBufferId);
         break;
       }
       case Graphics::BufferUsage::INDEX_BUFFER:
       {
-        gl->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferId);
+        context->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferId);
         break;
       }
       default:
@@ -158,4 +215,4 @@ void Buffer::Bind(Graphics::BufferUsage bindingTarget) const
   }
 }
 
-} // namespace Dali::Graphics::GLES
\ No newline at end of file
+} // namespace Dali::Graphics::GLES