2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "gles-graphics-buffer.h"
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/integration-api/gl-defines.h>
26 #include "egl-graphics-controller.h"
28 namespace Dali::Graphics::GLES
30 Buffer::Buffer(const Graphics::BufferCreateInfo& createInfo, Graphics::EglGraphicsController& controller)
31 : BufferResource(createInfo, controller)
33 // Check if buffer is CPU allocated
34 if(((0 | BufferUsage::UNIFORM_BUFFER) & mCreateInfo.usage) &&
35 (0 | BufferPropertiesFlagBit::CPU_ALLOCATED) & mCreateInfo.propertiesFlags)
37 // cpu allocated buffer
41 // Check if buffer is transient
42 if((0 | BufferPropertiesFlagBit::TRANSIENT_MEMORY) & mCreateInfo.propertiesFlags)
44 // memory is transient, may be lazily allocated when
45 // mapped, together with cpu allocated it may create emulated uniform
46 // buffer in this implementation
50 controller.AddBuffer(*this);
53 bool Buffer::TryRecycle(const Graphics::BufferCreateInfo& createInfo, Graphics::EglGraphicsController& controller)
55 // Compare whether specs are same and the buffer is allocated
56 mSetForGLRecycling = false;
58 // if different buffer spec, we need new buffer
59 if(!(createInfo.size == mCreateInfo.size
60 && createInfo.allocationCallbacks == mCreateInfo.allocationCallbacks
61 && createInfo.propertiesFlags == mCreateInfo.propertiesFlags
62 && createInfo.usage == mCreateInfo.usage
63 && createInfo.nextExtension == mCreateInfo.nextExtension ))
68 // GL resource hasn't been allocated yet, we need new buffer
74 // Make sure the buffer will be reinitialized
75 controller.AddBuffer(*this);
77 mSetForGLRecycling = true;
82 bool Buffer::InitializeResource()
84 // CPU allocated uniform buffer is a special "compatibility" mode
86 if(mCpuAllocated && !mTransient)
88 InitializeCPUBuffer();
90 else if(!mCpuAllocated)
92 InitializeGPUBuffer();
95 // make sure recycling mode is disabled after (re)initializing resource
96 mSetForGLRecycling = false;
100 void Buffer::InitializeCPUBuffer()
102 // Just allocate memory
103 // @TODO put better CPU memory management in place
104 const auto allocators = GetCreateInfo().allocationCallbacks;
106 // Early out if we recycle the buffer
107 if(mBufferPtr && mSetForGLRecycling)
114 mBufferPtr = allocators->allocCallback(mCreateInfo.size, 0, allocators->userData);
118 mBufferPtr = malloc(mCreateInfo.size);
122 void Buffer::InitializeGPUBuffer()
124 auto context = mController.GetCurrentContext();
125 auto gl = mController.GetGL();
131 // If mBufferId is already set and we recycling the buffer (orphaning)
132 if(!mSetForGLRecycling && !mBufferId)
134 gl->GenBuffers(1, &mBufferId);
136 context->BindBuffer(GL_ARRAY_BUFFER, mBufferId);
137 gl->BufferData(GL_ARRAY_BUFFER, GLsizeiptr(mCreateInfo.size), nullptr, GL_STATIC_DRAW);
140 void Buffer::DestroyResource()
142 // Destroy CPU allocated buffer
143 if(mCpuAllocated && mBufferPtr)
145 const auto allocators = GetCreateInfo().allocationCallbacks;
148 allocators->freeCallback(mBufferPtr, allocators->userData);
154 mBufferPtr = nullptr;
156 // Deestroy GPU allocation
159 auto gl = mController.GetGL();
162 gl->DeleteBuffers(1, &mBufferId);
167 void Buffer::DiscardResource()
169 mController.DiscardResource(this);
172 void Buffer::Bind(Graphics::BufferUsage bindingTarget) const
174 auto context = mController.GetCurrentContext();
175 auto gl = mController.GetGL();
181 // CPU allocated buffer may be bound only as Uniform Buffer
182 // on special binding point
183 if(mCpuAllocated && mBufferPtr)
185 if(bindingTarget == Graphics::BufferUsage::UNIFORM_BUFFER)
187 // TODO: probably nothing to do, the GLES Context
188 // we may use CPU backed buffer for future data
189 // transfers (copy operations)
194 switch(bindingTarget)
196 case Graphics::BufferUsage::VERTEX_BUFFER:
198 context->BindBuffer(GL_ARRAY_BUFFER, mBufferId);
201 case Graphics::BufferUsage::INDEX_BUFFER:
203 context->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferId);
214 } // namespace Dali::Graphics::GLES