Lock uniform buffer only 1 times per each render + minor fixup of uniforms
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / uniform-buffer-manager.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/render/renderers/uniform-buffer-manager.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/render/renderers/uniform-buffer-view-pool.h>
23 #include <dali/internal/render/renderers/uniform-buffer-view.h>
24 #include <dali/internal/render/renderers/uniform-buffer.h>
25
26 #include <dali/graphics-api/graphics-buffer-create-info.h>
27 #include <dali/graphics-api/graphics-buffer.h>
28
29 #include <cstring>
30 #include <memory>
31
32 namespace Dali::Internal::Render
33 {
34 UniformBufferManager::UniformBufferManager(Dali::Graphics::Controller* controller)
35 : mController(controller)
36 {
37 }
38
39 UniformBufferManager::~UniformBufferManager() = default;
40
41 Graphics::UniquePtr<UniformBuffer> UniformBufferManager::AllocateUniformBuffer(uint32_t size, uint32_t alignment)
42 {
43   // TODO : Current code only assume CPU_ALLOCATED uniform buffer now
44   return Graphics::UniquePtr<UniformBuffer>(
45     new UniformBuffer(mController,
46                       size,
47                       alignment,
48                       Dali::Graphics::BufferUsageFlags{0u} | Dali::Graphics::BufferUsage::TRANSFER_DST | Dali::Graphics::BufferUsage::UNIFORM_BUFFER,
49                       Dali::Graphics::BufferPropertiesFlags{0u} | Dali::Graphics::BufferPropertiesFlagBit::CPU_ALLOCATED));
50 }
51
52 Graphics::UniquePtr<UniformBufferView> UniformBufferManager::CreateUniformBufferView(UniformBuffer* uniformBuffer, uint32_t offset, uint32_t size)
53 {
54   // Allocate offset of given UBO (allocation strategy may reuse memory)
55   return Graphics::UniquePtr<UniformBufferView>(new UniformBufferView(*uniformBuffer, offset, size));
56 }
57
58 Graphics::UniquePtr<UniformBufferViewPool> UniformBufferManager::CreateUniformBufferViewPool()
59 {
60   return Graphics::UniquePtr<UniformBufferViewPool>(
61     new UniformBufferViewPool(*this, 1));
62 }
63
64 [[nodiscard]] UniformBufferViewPool* UniformBufferManager::GetUniformBufferViewPool(uint32_t bufferIndex)
65 {
66   if(!mUniformBufferPoolStorage[bufferIndex])
67   {
68     // create new uniform buffer view pool with default (initial) capacity
69     mUniformBufferPoolStorage[bufferIndex] = CreateUniformBufferViewPool();
70   }
71   return mUniformBufferPoolStorage[bufferIndex].get();
72 }
73
74 void UniformBufferManager::ReadyToLockUniformBuffer(uint32_t bufferIndex)
75 {
76   GetUniformBufferViewPool(bufferIndex)->ReadyToLockUniformBuffer();
77 }
78
79 void UniformBufferManager::UnlockUniformBuffer(uint32_t bufferIndex)
80 {
81   GetUniformBufferViewPool(bufferIndex)->UnlockUniformBuffer();
82 }
83
84 } // namespace Dali::Internal::Render