2 * Copyright (c) 2024 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);
119 if(DALI_UNLIKELY(mBufferPtr == nullptr))
121 DALI_LOG_ERROR("malloc is failed. request malloc size : %u\n", mCreateInfo.size);
126 void Buffer::InitializeGPUBuffer()
128 auto context = mController.GetCurrentContext();
129 auto gl = mController.GetGL();
135 // If mBufferId is already set and we recycling the buffer (orphaning)
136 if(!mSetForGLRecycling && !mBufferId)
138 gl->GenBuffers(1, &mBufferId);
140 context->BindBuffer(GL_ARRAY_BUFFER, mBufferId);
141 gl->BufferData(GL_ARRAY_BUFFER, GLsizeiptr(mCreateInfo.size), nullptr, GL_STATIC_DRAW);
144 void Buffer::DestroyResource()
146 // Destroy CPU allocated buffer
147 if(mCpuAllocated && mBufferPtr)
149 const auto allocators = GetCreateInfo().allocationCallbacks;
152 allocators->freeCallback(mBufferPtr, allocators->userData);
158 mBufferPtr = nullptr;
160 // Deestroy GPU allocation
163 auto gl = mController.GetGL();
166 gl->DeleteBuffers(1, &mBufferId);
171 void Buffer::DiscardResource()
173 mController.DiscardResource(this);
176 void Buffer::Bind(Graphics::BufferUsage bindingTarget) const
178 auto context = mController.GetCurrentContext();
179 auto gl = mController.GetGL();
185 // CPU allocated buffer may be bound only as Uniform Buffer
186 // on special binding point
187 if(mCpuAllocated && mBufferPtr)
189 if(bindingTarget == Graphics::BufferUsage::UNIFORM_BUFFER)
191 // TODO: probably nothing to do, the GLES Context
192 // we may use CPU backed buffer for future data
193 // transfers (copy operations)
198 switch(bindingTarget)
200 case Graphics::BufferUsage::VERTEX_BUFFER:
202 context->BindBuffer(GL_ARRAY_BUFFER, mBufferId);
205 case Graphics::BufferUsage::INDEX_BUFFER:
207 context->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferId);
218 } // namespace Dali::Graphics::GLES