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