/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
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
InitializeGPUBuffer();
}
+ // make sure recycling mode is disabled after (re)initializing resource
+ mSetForGLRecycling = false;
return true;
}
// 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);
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()
else
{
auto gl = mController.GetGL();
- gl->DeleteBuffers(1, &mBufferId);
+ if(gl)
+ {
+ gl->DeleteBuffers(1, &mBufferId);
+ }
}
}
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
{
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:
}
}
-} // namespace Dali::Graphics::GLES
\ No newline at end of file
+} // namespace Dali::Graphics::GLES