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