Added VertexBufferUpdateCallback
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / gpu-buffer.h
1 #ifndef DALI_INTERNAL_RENDERERS_GPU_BUFFER_H
2 #define DALI_INTERNAL_RENDERERS_GPU_BUFFER_H
3
4 /*
5  * Copyright (c) 2023 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 // INTERNAL INCLUDES
22 #include <dali/graphics-api/graphics-controller.h>
23
24 namespace Dali
25 {
26 class VertexBufferUpdateCallback;
27
28 namespace Internal
29 {
30 /**
31  * Used to create and update a GPU memory buffer.
32  *
33  * The buffer can be used for storing vertex data, index arrays (indices)
34  * or pixel data (PBO).
35  *
36  * The buffer allows data to be stored in high-performance
37  * graphics memory on the server side and
38  * promotes efficient data transfer.
39  */
40 class GpuBuffer
41 {
42 public:
43   /**
44    * When writing into the buffer, the WritePolicy
45    * determines whether the current content would be preserved
46    * or discarded.
47    *
48    * RETAIN - buffer content is retained
49    *
50    * DISCARD - buffer content is discarded. In this case, writing into
51    *           a part of buffer will result with undefined content outside
52    *           the specified area. The client should rewrite whole area
53    *           in order to have coherent and valid data.
54    */
55   enum class WritePolicy
56   {
57     RETAIN, ///< Buffer content is preserved
58     DISCARD ///< Buffer content is invalidated and discarded
59   };
60
61   /**
62    * constructor
63    * @param[in] graphicsController the graphics controller
64    * @param[in] usage The type of buffer
65    * @param[in] writePolicy The buffer data write policy to be used, default is WritePolicy::RETAIN
66    */
67   GpuBuffer(Graphics::Controller& graphicsController, Graphics::BufferUsageFlags usage, GpuBuffer::WritePolicy writePolicy);
68
69   /**
70    * Destructor, non virtual as no virtual methods or inheritance
71    */
72   ~GpuBuffer() = default;
73
74   /**
75    * Creates or updates a buffer object and binds it to the target.
76    * @param graphicsController The graphics controller
77    * @param size Specifies the size in bytes of the buffer object's new data store.
78    * @param data pointer to the data to load
79    */
80   void UpdateDataBuffer(Graphics::Controller& graphicsController, uint32_t size, const void* data);
81
82   /**
83    * Updates existing buffer by calling associated VertexBufferUpdateCallback
84    *
85    * bytesUpdatedCount limits next draw call to that amount of data.
86    *
87    * @param[in] graphicsController Valid controller
88    * @param[in] callback  Valid pointer to the VertexBufferUpdateCallback
89    * @param[out] bytesUpdatedCount Number of bytes updated
90    */
91   void UpdateDataBufferWithCallback(Graphics::Controller& graphicsController, Dali::VertexBufferUpdateCallback* callback, uint32_t& bytesUpdatedCount);
92
93   /**
94    * Get the size of the buffer
95    * @return size Size of the buffer in bytes
96    */
97   [[nodiscard]] uint32_t GetBufferSize() const
98   {
99     return mSize;
100   }
101
102   [[nodiscard]] inline const Graphics::Buffer* GetGraphicsObject() const
103   {
104     return mGraphicsObject.get();
105   }
106
107   /**
108    * Destroy the graphics buffer and reset.
109    */
110   void Destroy();
111
112 private:
113   Graphics::UniquePtr<Graphics::Buffer> mGraphicsObject;
114   uint32_t                              mCapacity{0}; ///< buffer capacity
115   uint32_t                              mSize{0};     ///< buffer size
116   Graphics::BufferUsageFlags            mUsage;
117   WritePolicy                           mWritePolicy{WritePolicy::RETAIN}; ///< data write policy for the buffer
118 };
119
120 } // namespace Internal
121 } // namespace Dali
122
123 #endif // DALI_INTERNAL_RENDERERS_GPU_BUFFER_H