Merge branch 'devel/master (1.1.1)' 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     mHasColorRenderItems( false )
82   {
83   }
84
85   /**
86    * Destructor
87    */
88   ~RenderList()
89   {
90     // pointer container deletes the render items
91     delete mClippingBox;
92   }
93
94   /**
95    * Clear the render flags
96    */
97   void ClearFlags()
98   {
99     mRenderFlags = 0u;
100   }
101
102   /**
103    * Set particular render flags
104    * @param[in] flags The set of flags to bitwise or with existing flags
105    */
106   void SetFlags( unsigned int flags )
107   {
108     mRenderFlags |= flags;
109   }
110
111   /**
112    * Retrieve the render flags.
113    * @return the render flags.
114    */
115   unsigned int GetFlags() const
116   {
117     return mRenderFlags;
118   }
119
120   /**
121    * Reset the render list for next frame
122    */
123   void Reset()
124   {
125     // we dont want to delete and re-create the render items every frame
126     mNextFree = 0;
127     mRenderFlags = 0u;
128
129     delete mClippingBox;
130     mClippingBox = NULL;
131   }
132
133   /**
134    * Reserve space in the render list
135    * @param size to reserve
136    */
137   void Reserve( RenderItemContainer::SizeType size )
138   {
139     mNextFree = 0;
140     mItems.Reserve( size );
141   }
142
143   /**
144    * @return the capacity of the render list
145    */
146   RenderItemContainer::SizeType Capacity()
147   {
148     return mItems.Capacity();
149   }
150
151   /**
152    * Get next free render item
153    * @return reference to the next available RenderItem
154    */
155   RenderItem& GetNextFreeItem()
156   {
157     // check if we have enough items, we can only be one behind at worst
158     if( mItems.Count() <= mNextFree )
159     {
160       mItems.PushBack( new RenderItem ); // Push a new empty render item
161     }
162     // get the item mNextFree points to and increase by one
163     RenderItem& item = *mItems[ mNextFree++ ];
164     item.Reset();
165     return item;
166   }
167
168   /**
169    * Get item at a given position in the list
170    */
171   RenderItem& GetItem( RenderItemContainer::SizeType index ) const
172   {
173     DALI_ASSERT_DEBUG( index < GetCachedItemCount() );
174     return *mItems[ index ];
175   }
176
177   /**
178    * Get renderer from an item in the list
179    */
180   const Renderer* GetRenderer( RenderItemContainer::SizeType index ) const
181   {
182     DALI_ASSERT_DEBUG( index < GetCachedItemCount() );
183     return mItems[ index ]->GetRenderer();
184   }
185
186   /**
187    * Get the number of real items
188    * Because of caching, the actual size may be bit more
189    * @return The number of items
190    */
191   RenderItemContainer::SizeType Count() const
192   {
193     return mNextFree;
194   }
195
196   /**
197    * @return the number of items cached by the list
198    */
199   RenderItemContainer::SizeType GetCachedItemCount() const
200   {
201     return mItems.Count();
202   }
203
204   /**
205    * Tells the render list to reuse the items from the cache
206    */
207   void ReuseCachedItems()
208   {
209     mNextFree = mItems.Count();
210   }
211
212   /**
213    * Predicate to inform if the list is empty
214    */
215   bool IsEmpty() const
216   {
217     return (mNextFree == 0);
218   }
219
220   /**
221    * Set clipping
222    * @param clipping on/off
223    * @param box for clipping
224    */
225   void SetClipping( bool clipping, const ClippingBox& box )
226   {
227     if( clipping )
228     {
229       ClippingBox* newBox = new ClippingBox( box );
230       delete mClippingBox;
231       mClippingBox = newBox;
232     }
233   }
234
235   /**
236    * @return true if clipping is on
237    */
238   bool IsClipping() const
239   {
240     return (NULL != mClippingBox);
241   }
242
243   /**
244    * @return the clipping box
245    */
246   const ClippingBox& GetClippingBox() const
247   {
248     return *mClippingBox;
249   }
250
251   /**
252    * @return the container (for sorting)
253    */
254   RenderItemContainer& GetContainer()
255   {
256     return mItems;
257   }
258
259   /**
260    * Do some housekeeping to keep memory consumption low
261    */
262   void ReleaseUnusedItems()
263   {
264     // release any non-used RenderItems
265     if( mItems.Count() > mNextFree )
266     {
267       mItems.Resize( mNextFree );
268     }
269   }
270
271   /**
272    * @return the source layer these renderitems originate from
273    */
274   Layer* GetSourceLayer()
275   {
276     return mSourceLayer;
277   }
278
279   /**
280    * @param layer these renderitems originate from
281    */
282   void SetSourceLayer( Layer* layer )
283   {
284     mSourceLayer = layer;
285   }
286
287   /**
288    * Set if the RenderList contains color RenderItems
289    * @param[in] hasColorRenderItems True if it contains color RenderItems, false otherwise
290    */
291   void SetHasColorRenderItems( bool hasColorRenderItems )
292   {
293     mHasColorRenderItems = hasColorRenderItems;
294   }
295
296   /**
297    * Check if the RenderList contains color RenderItems
298    * @return true if the RenderList contains color RenderItems, false otherwise
299    */
300   bool HasColorRenderItems() const
301   {
302     return mHasColorRenderItems;
303   }
304
305 private:
306
307   /*
308    * Copy constructor and assignment operator not defined
309    */
310   RenderList( const RenderList& rhs );
311   const RenderList& operator=( const RenderList& rhs );
312
313   RenderItemContainer mItems; ///< Each item is a renderer and matrix pair
314   RenderItemContainer::SizeType mNextFree;              ///< index for the next free item to use
315
316   unsigned int mRenderFlags;    ///< The render flags
317
318   ClippingBox* mClippingBox;               ///< The clipping box, in window coordinates, when clipping is enabled
319   Layer*       mSourceLayer;              ///< The originating layer where the renderers are from
320   bool         mHasColorRenderItems : 1;  ///< True if list contains color render items
321 };
322
323 } // namespace SceneGraph
324
325 } // namespace Internal
326
327 } // namespace Dali
328
329 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDER_LIST_H__