2 * Copyright (c) 2021 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::InitializeResource()
55 // CPU allocated uniform buffer is a special "compatibility" mode
57 if(mCpuAllocated && !mTransient)
59 InitializeCPUBuffer();
61 else if(!mCpuAllocated)
63 InitializeGPUBuffer();
69 void Buffer::InitializeCPUBuffer()
71 // Just allocate memory
72 // @TODO put better CPU memory management in place
73 const auto allocators = GetCreateInfo().allocationCallbacks;
76 mBufferPtr = allocators->allocCallback(mCreateInfo.size, 0, allocators->userData);
80 mBufferPtr = malloc(mCreateInfo.size);
84 void Buffer::InitializeGPUBuffer()
86 auto gl = mController.GetGL();
87 gl->GenBuffers(1, &mBufferId);
88 gl->BindBuffer(GL_ARRAY_BUFFER, mBufferId);
89 gl->BufferData(GL_ARRAY_BUFFER, mCreateInfo.size, nullptr, GL_STATIC_DRAW);
92 void Buffer::DestroyResource()
94 // Destroy CPU allocated buffer
95 if(mCpuAllocated && mBufferPtr)
97 const auto allocators = GetCreateInfo().allocationCallbacks;
100 allocators->freeCallback(mBufferPtr, allocators->userData);
106 mBufferPtr = nullptr;
108 // Deestroy GPU allocation
111 auto gl = mController.GetGL();
114 gl->DeleteBuffers(1, &mBufferId);
119 void Buffer::DiscardResource()
121 mController.DiscardResource(this);
124 void Buffer::Bind(Graphics::BufferUsage bindingTarget) const
126 auto gl = mController.GetGL();
128 // CPU allocated buffer may be bound only as Uniform Buffer
129 // on special binding point
130 if(mCpuAllocated && mBufferPtr)
132 if(bindingTarget == Graphics::BufferUsage::UNIFORM_BUFFER)
134 // TODO: probably nothing to do, the GLES Context
135 // we may use CPU backed buffer for future data
136 // transfers (copy operations)
141 switch(bindingTarget)
143 case Graphics::BufferUsage::VERTEX_BUFFER:
145 gl->BindBuffer(GL_ARRAY_BUFFER, mBufferId);
148 case Graphics::BufferUsage::INDEX_BUFFER:
150 gl->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferId);
161 } // namespace Dali::Graphics::GLES