Changed Render::Renderer to use MemoryPoolAllocator
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-list.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_RENDER_LIST_H
2 #define DALI_INTERNAL_SCENE_GRAPH_RENDER_LIST_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 // EXTERNAL INCLUDES
22 #include <cstdint>
23
24 // INTERNAL INCLUDES
25 #include <dali/devel-api/common/owner-container.h>
26 #include <dali/public-api/math/rect.h>
27
28 #include <dali/graphics-api/graphics-controller.h>
29 #include <dali/internal/common/owner-key-container.h>
30 #include <dali/internal/render/common/render-item.h>
31
32 namespace Dali
33 {
34 using ClippingBox = Rect<int>;
35
36 namespace Internal
37 {
38 namespace Render
39 {
40 class Renderer;
41 }
42
43 namespace SceneGraph
44 {
45 class Layer;
46
47 using RenderItemContainer = OwnerKeyContainer<RenderItem>;
48
49 struct RenderList;
50 using RenderListContainer = OwnerContainer<RenderList*>;
51
52 /**
53  * The RenderList structure provides the renderer manager with a list of renderers.
54  */
55 struct RenderList
56 {
57 public:
58   /**
59    * Constructor
60    */
61   RenderList()
62   : mNextFree(0),
63     mClippingBox(nullptr),
64     mSourceLayer(nullptr),
65     mHasColorRenderItems(false)
66   {
67   }
68
69   /**
70    * Destructor
71    */
72   ~RenderList()
73   {
74     // Pointer container deletes the render items
75     delete mClippingBox;
76   }
77
78   /*
79    * Copy constructor and assignment operator not defined
80    */
81   RenderList(const RenderList& rhs) = delete;
82   const RenderList& operator=(const RenderList& rhs) = delete;
83
84   /**
85    * Reset the render list for next frame
86    */
87   void Reset()
88   {
89     // We don't want to delete and re-create the render items every frame
90     mNextFree = 0;
91
92     delete mClippingBox;
93     mClippingBox = nullptr;
94   }
95
96   /**
97    * Reserve space in the render list
98    * @param size to reserve
99    */
100   void Reserve(RenderItemContainer::SizeType size)
101   {
102     mNextFree = 0;
103     mItems.Reserve(size);
104   }
105
106   /**
107    * @return the capacity of the render list
108    */
109   RenderItemContainer::SizeType Capacity()
110   {
111     return mItems.Capacity();
112   }
113
114   /**
115    * Get next free render item
116    * @return reference to the next available RenderItem
117    */
118   RenderItem& GetNextFreeItem()
119   {
120     // check if we have enough items, we can only be one behind at worst
121     if(mItems.Count() <= mNextFree)
122     {
123       mItems.PushBack(RenderItem::NewKey()); // Push a new empty render item
124     }
125     // get the item mNextFree points to and increase by one
126     RenderItem* item = mItems[mNextFree++].Get();
127     return *item;
128   }
129
130   /**
131    * Get item at a given position in the list
132    */
133   RenderItem& GetItem(uint32_t index) const
134   {
135     DALI_ASSERT_DEBUG(index < GetCachedItemCount());
136     RenderItem* item = mItems[index].Get();
137     return *item;
138   }
139
140   RenderItemKey GetItemKey(uint32_t index) const
141   {
142     DALI_ASSERT_DEBUG(index < GetCachedItemCount());
143     return mItems[index];
144   }
145
146   /**
147    * Get renderer from an item in the list
148    */
149   Render::RendererKey GetRenderer(uint32_t index) const
150   {
151     DALI_ASSERT_DEBUG(index < GetCachedItemCount());
152     return mItems[index]->mRenderer;
153   }
154
155   /**
156    * Get the number of real items
157    * Because of caching, the actual size may be bit more
158    * @return The number of items
159    */
160   uint32_t Count() const
161   {
162     return mNextFree;
163   }
164
165   /**
166    * @return the number of items cached by the list
167    */
168   uint32_t GetCachedItemCount() const
169   {
170     return static_cast<uint32_t>(mItems.Count());
171   }
172
173   /**
174    * Tells the render list to reuse the items from the cache
175    */
176   void ReuseCachedItems()
177   {
178     mNextFree = static_cast<uint32_t>(mItems.Count());
179   }
180
181   /**
182    * Predicate to inform if the list is empty
183    */
184   bool IsEmpty() const
185   {
186     return (mNextFree == 0);
187   }
188
189   /**
190    * Set clipping
191    * @param clipping on/off
192    * @param box for clipping
193    */
194   void SetClipping(bool clipping, const ClippingBox& box)
195   {
196     if(clipping)
197     {
198       delete mClippingBox;
199       mClippingBox = new ClippingBox(box);
200     }
201   }
202
203   /**
204    * @return true if clipping is on
205    */
206   bool IsClipping() const
207   {
208     return (nullptr != mClippingBox);
209   }
210
211   /**
212    * @return the clipping box
213    */
214   const ClippingBox& GetClippingBox() const
215   {
216     return *mClippingBox;
217   }
218
219   /**
220    * @return the container (for sorting)
221    */
222   RenderItemContainer& GetContainer()
223   {
224     return mItems;
225   }
226
227   /**
228    * Do some housekeeping to keep memory consumption low
229    */
230   void ReleaseUnusedItems()
231   {
232     // release any non-used RenderItems
233     if(mItems.Count() > mNextFree)
234     {
235       mItems.Resize(mNextFree);
236     }
237   }
238
239   /**
240    * @return the source layer these renderitems originate from
241    */
242   Layer* GetSourceLayer() const
243   {
244     return mSourceLayer;
245   }
246
247   /**
248    * @param layer The layer these RenderItems originate from
249    */
250   void SetSourceLayer(Layer* layer)
251   {
252     mSourceLayer = layer;
253   }
254
255   /**
256    * Set if the RenderList contains color RenderItems
257    * @param[in] hasColorRenderItems True if it contains color RenderItems, false otherwise
258    */
259   void SetHasColorRenderItems(bool hasColorRenderItems)
260   {
261     mHasColorRenderItems = hasColorRenderItems;
262   }
263
264   /**
265    * Check if the RenderList contains color RenderItems
266    * @return true if the RenderList contains color RenderItems, false otherwise
267    */
268   bool HasColorRenderItems() const
269   {
270     return mHasColorRenderItems;
271   }
272
273   Graphics::CommandBuffer& GetCommandBuffer(Graphics::Controller& controller)
274   {
275     if(!mGraphicsCommandBuffer)
276     {
277       mGraphicsCommandBuffer = controller.CreateCommandBuffer(
278         Graphics::CommandBufferCreateInfo().SetLevel(Graphics::CommandBufferLevel::SECONDARY), nullptr);
279     }
280     return *mGraphicsCommandBuffer.get();
281   }
282
283   const Graphics::CommandBuffer* GetCommandBuffer() const
284   {
285     return mGraphicsCommandBuffer.get();
286   }
287
288 private:
289   RenderItemContainer mItems;    ///< Container of render items
290   uint32_t            mNextFree; ///< index for the next free item to use
291
292   mutable Graphics::UniquePtr<Graphics::CommandBuffer> mGraphicsCommandBuffer{nullptr};
293
294   ClippingBox* mClippingBox;             ///< The clipping box, in window coordinates, when clipping is enabled
295   Layer*       mSourceLayer;             ///< The originating layer where the renderers are from
296   bool         mHasColorRenderItems : 1; ///< True if list contains color render items
297 };
298
299 } // namespace SceneGraph
300
301 } // namespace Internal
302
303 } // namespace Dali
304
305 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDER_LIST_H