Revert "[Tizen] Fix Coverity issue"
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / uniform-buffer-manager.h
1 #ifndef DALI_INTERNAL_UNIFORM_BUFFER_MANAGER_H
2 #define DALI_INTERNAL_UNIFORM_BUFFER_MANAGER_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #include <dali/graphics-api/graphics-controller.h>
22
23 #include <memory>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29 namespace Render
30 {
31 class UniformBuffer
32 {
33   friend class UniformBufferManager;
34
35 private:
36   /**
37    * Constructor of UniformBuffer
38    *
39    * @param[in] mController Pointer of the graphics controller
40    * @param[in] sizeInBytes initial size of allocated buffer
41    * @param[in] alignment memory alignment in bytes
42    * @param[in] persistentMappingEnabled if true, buffer is mapped persistently
43    * @param[in] usageFlags type of usage ( Graphics::BufferUsage )
44    */
45   UniformBuffer(Dali::Graphics::Controller* mController,
46                 uint32_t                    sizeInBytes,
47                 uint32_t                    alignment,
48                 bool                        persistentMappingEnabled,
49                 Graphics::BufferUsageFlags  usageFlags);
50
51 public:
52   /**
53    * Destructor of UniformBuffer
54    */
55   ~UniformBuffer();
56
57   /**
58    * Writes data into the buffer
59    *
60    * @param[in] data pointer to the source data
61    * @param[in] size size of source data
62    * @param[in] offset destination offset
63    */
64   void Write(const void* data, uint32_t size, uint32_t offset);
65
66   /**
67    * Flushes whole buffer range
68    */
69   void Flush();
70
71   /**
72    * Returns allocated ( requested ) size
73    * @return size of buffer
74    */
75   uint32_t GetSize() const
76   {
77     return mSize;
78   }
79
80   /**
81    * Return Graphics API buffer
82    * @return pointer to the buffer object
83    */
84   Dali::Graphics::Buffer* GetBuffer() const
85   {
86     return mBuffer.get();
87   }
88
89   /**
90    * Returns memory alignment
91    * @return memory alignment
92    */
93   uint32_t GetAlignment() const
94   {
95     return mAlignment;
96   }
97
98   /**
99    * Reserves buffer memory
100    *
101    * @param size requested size
102    */
103   void Reserve(uint32_t size);
104
105   /**
106    * Maps buffer memory
107    */
108   void Map();
109
110   /**
111    * Unmaps buffer memory
112    */
113   void Unmap();
114
115   /**
116    * Fills the buffer from the given offset with given data ( single 8bit value )
117    * @param data char type data
118    * @param offset start offset
119    * @param size size to write, 0 if whole size
120    */
121   void Fill(char data, uint32_t offset, uint32_t size);
122
123 private:
124   Graphics::UniquePtr<Graphics::Buffer> mBuffer;
125   Dali::Graphics::Controller*           mController;
126   Graphics::UniquePtr<Graphics::Memory> mMemory{nullptr};
127
128   Graphics::MapBufferInfo mMapBufferInfo{};
129
130   uint32_t mCapacity{0}; ///< buffer capacity
131   uint32_t mSize;        ///< buffer size
132   uint32_t mAlignment;
133   bool     mPersistentMappedEnabled;
134
135   Graphics::BufferUsageFlags mUsageFlags;
136 };
137
138 class UniformBufferManager
139 {
140 public:
141   UniformBufferManager(Dali::Graphics::Controller* controller);
142
143   ~UniformBufferManager();
144
145   /**
146    * Allocates uniform buffer with given size
147    * @param size
148    * @return
149    */
150   Graphics::UniquePtr<UniformBuffer> AllocateUniformBuffer(uint32_t size);
151
152 private:
153   Dali::Graphics::Controller* mController;
154 };
155
156 } // namespace Render
157 } // namespace Internal
158 } // namespace Dali
159
160 #endif // DALI_INTERNAL_UNIFORM_BUFFER_MANAGER_H