Allow multiple renderers per Actor and sharing renderers between actors
[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) 2014 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/public-api/math/rect.h>
23 #include <dali/internal/render/common/render-item.h>
24 #include <dali/internal/common/owner-container.h>
25
26 namespace Dali
27 {
28
29 typedef Rect<int> ClippingBox;
30
31 namespace Internal
32 {
33
34 namespace Render
35 {
36 class Renderer;
37 }
38
39 namespace SceneGraph
40 {
41
42 class Layer;
43
44
45 class RenderItem;
46 typedef OwnerContainer< RenderItem* > RenderItemContainer;
47
48 struct RenderList;
49 typedef OwnerContainer< RenderList* > RenderListContainer;
50
51 /**
52  * The RenderList structure provides the renderer with a list of renderers and
53  * a set of flags to tell it what depth buffering is required.
54  */
55 struct RenderList
56 {
57 public:
58
59   /**
60    * The RenderFlags describe how the objects are rendered using the depth and stencil buffer.
61    *
62    * The flags which relate to GL_DEPTH_TEST and GL_STENCIL_TEST are called
63    * DEPTH_BUFFER_ENABLED and STENCIL_BUFFER_ENABLED to avoid any confusion.
64    * E.g. if GL_DEPTH_TEST is not enabled you can't write to the depth buffer, which can cause confusion.
65    *
66    */
67   enum RenderFlags
68   {
69     DEPTH_BUFFER_ENABLED   = 1 << 0, ///< If depth buffer should be used for writing / test operations
70     DEPTH_WRITE            = 1 << 1, ///< If the depth buffer is writable
71     DEPTH_CLEAR            = 1 << 2, ///< If the depth buffer should first be cleared
72     STENCIL_BUFFER_ENABLED = 1 << 3, ///< If stencil buffer should be used for writing / test operation
73     STENCIL_WRITE          = 1 << 4, ///< If the stencil buffer is writable
74     STENCIL_CLEAR          = 1 << 5, ///< If the stencil buffer should first be cleared
75
76   };
77
78   /**
79    * Constructor
80    */
81   RenderList()
82   : mNextFree( 0 ),
83     mRenderFlags( 0u ),
84     mClippingBox( NULL ),
85     mSourceLayer( NULL ),
86     mHasColorRenderItems( false )
87   {
88   }
89
90   /**
91    * Destructor
92    */
93   ~RenderList()
94   {
95     // pointer container deletes the render items
96     delete mClippingBox;
97   }
98
99   /**
100    * Clear the render flags
101    */
102   void ClearFlags()
103   {
104     mRenderFlags = 0u;
105   }
106
107   /**
108    * Set particular render flags
109    * @param[in] flags The set of flags to bitwise or with existing flags
110    */
111   void SetFlags( unsigned int flags )
112   {
113     mRenderFlags |= flags;
114   }
115
116   /**
117    * Retrieve the render flags.
118    * @return the render flags.
119    */
120   unsigned int GetFlags() const
121   {
122     return mRenderFlags;
123   }
124
125   /**
126    * Reset the render list for next frame
127    */
128   void Reset()
129   {
130     // we dont want to delete and re-create the render items every frame
131     mNextFree = 0;
132     mRenderFlags = 0u;
133
134     delete mClippingBox;
135     mClippingBox = NULL;
136   }
137
138   /**
139    * Reserve space in the render list
140    * @param size to reserve
141    */
142   void Reserve( RenderItemContainer::SizeType size )
143   {
144     mNextFree = 0;
145     mItems.Reserve( size );
146   }
147
148   /**
149    * @return the capacity of the render list
150    */
151   RenderItemContainer::SizeType Capacity()
152   {
153     return mItems.Capacity();
154   }
155
156   /**
157    * Get next free render item
158    * @return reference to the next available RenderItem
159    */
160   RenderItem& GetNextFreeItem()
161   {
162     // check if we have enough items, we can only be one behind at worst
163     if( mItems.Count() <= mNextFree )
164     {
165       mItems.PushBack( new RenderItem ); // Push a new empty render item
166     }
167     // get the item mNextFree points to and increase by one
168     RenderItem& item = *mItems[ mNextFree++ ];
169     item.Reset();
170     return item;
171   }
172
173   /**
174    * Get item at a given position in the list
175    */
176   RenderItem& GetItem( RenderItemContainer::SizeType index ) const
177   {
178     DALI_ASSERT_DEBUG( index < GetCachedItemCount() );
179     return *mItems[ index ];
180   }
181
182   /**
183    * Get renderer from an item in the list
184    */
185   const Render::Renderer& GetRenderer( RenderItemContainer::SizeType index ) const
186   {
187     DALI_ASSERT_DEBUG( index < GetCachedItemCount() );
188     return mItems[ index ]->GetRenderer();
189   }
190
191   /**
192    * Get the number of real items
193    * Because of caching, the actual size may be bit more
194    * @return The number of items
195    */
196   RenderItemContainer::SizeType Count() const
197   {
198     return mNextFree;
199   }
200
201   /**
202    * @return the number of items cached by the list
203    */
204   RenderItemContainer::SizeType GetCachedItemCount() const
205   {
206     return mItems.Count();
207   }
208
209   /**
210    * Tells the render list to reuse the items from the cache
211    */
212   void ReuseCachedItems()
213   {
214     mNextFree = mItems.Count();
215   }
216
217   /**
218    * Predicate to inform if the list is empty
219    */
220   bool IsEmpty() const
221   {
222     return (mNextFree == 0);
223   }
224
225   /**
226    * Set clipping
227    * @param clipping on/off
228    * @param box for clipping
229    */
230   void SetClipping( bool clipping, const ClippingBox& box )
231   {
232     if( clipping )
233     {
234       ClippingBox* newBox = new ClippingBox( box );
235       delete mClippingBox;
236       mClippingBox = newBox;
237     }
238   }
239
240   /**
241    * @return true if clipping is on
242    */
243   bool IsClipping() const
244   {
245     return (NULL != mClippingBox);
246   }
247
248   /**
249    * @return the clipping box
250    */
251   const ClippingBox& GetClippingBox() const
252   {
253     return *mClippingBox;
254   }
255
256   /**
257    * @return the container (for sorting)
258    */
259   RenderItemContainer& GetContainer()
260   {
261     return mItems;
262   }
263
264   /**
265    * Do some housekeeping to keep memory consumption low
266    */
267   void ReleaseUnusedItems()
268   {
269     // release any non-used RenderItems
270     if( mItems.Count() > mNextFree )
271     {
272       mItems.Resize( mNextFree );
273     }
274   }
275
276   /**
277    * @return the source layer these renderitems originate from
278    */
279   Layer* GetSourceLayer()
280   {
281     return mSourceLayer;
282   }
283
284   /**
285    * @param layer these renderitems originate from
286    */
287   void SetSourceLayer( Layer* layer )
288   {
289     mSourceLayer = layer;
290   }
291
292   /**
293    * Set if the RenderList contains color RenderItems
294    * @param[in] hasColorRenderItems True if it contains color RenderItems, false otherwise
295    */
296   void SetHasColorRenderItems( bool hasColorRenderItems )
297   {
298     mHasColorRenderItems = hasColorRenderItems;
299   }
300
301   /**
302    * Check if the RenderList contains color RenderItems
303    * @return true if the RenderList contains color RenderItems, false otherwise
304    */
305   bool HasColorRenderItems() const
306   {
307     return mHasColorRenderItems;
308   }
309
310 private:
311
312   /*
313    * Copy constructor and assignment operator not defined
314    */
315   RenderList( const RenderList& rhs );
316   const RenderList& operator=( const RenderList& rhs );
317
318   RenderItemContainer mItems; ///< Each item is a renderer and matrix pair
319   RenderItemContainer::SizeType mNextFree;              ///< index for the next free item to use
320
321   unsigned int mRenderFlags;    ///< The render flags
322
323   ClippingBox* mClippingBox;               ///< The clipping box, in window coordinates, when clipping is enabled
324   Layer*       mSourceLayer;              ///< The originating layer where the renderers are from
325   bool         mHasColorRenderItems : 1;  ///< True if list contains color render items
326 };
327
328 } // namespace SceneGraph
329
330 } // namespace Internal
331
332 } // namespace Dali
333
334 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDER_LIST_H__