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