Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-core.git] / dali / internal / update / render-tasks / scene-graph-render-task.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // INTERNAL INCLUDES
21 #include <dali/public-api/math/viewport.h>
22 #include <dali/public-api/render-tasks/render-task.h>
23 #include <dali/internal/common/buffer-index.h>
24 #include <dali/internal/common/event-to-update.h>
25 #include <dali/internal/common/message.h>
26 #include <dali/internal/update/common/double-buffered.h>
27 #include <dali/internal/update/common/property-owner.h>
28 #include <dali/internal/update/common/animatable-property.h>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35 class FrameBufferTexture;
36 class CompleteStatusManager;
37
38 namespace SceneGraph
39 {
40 class Node;
41 class CameraAttachment;
42 class RenderInstruction;
43
44 /**
45  * RenderTasks describe how the Dali scene should be rendered.
46  */
47 class RenderTask : public PropertyOwner
48 {
49 public:
50   enum State
51   {
52     RENDER_CONTINUOUSLY,               ///< mRefreshRate > 0
53     RENDER_ONCE_WAITING_FOR_RESOURCES, ///< mRefreshRate = REFRESH_ONCE
54     RENDERED_ONCE,                     ///< mRefreshRate = REFRESH_ONCE & rendered
55     RENDERED_ONCE_AND_NOTIFIED         ///< mRefreshRate = REFRESH_ONCE & rendered & notified
56   };
57
58   /**
59    * Create a new RenderTask
60    */
61   static RenderTask* New();
62
63   /**
64    * Virtual destructor
65    */
66   virtual ~RenderTask();
67
68   /**
69    * Set the nodes to be rendered.
70    * @param[in] node This node and its children will be rendered.
71    */
72   void SetSourceNode( Node* node );
73
74   /**
75    * Retrieve the source node.
76    * @return This node and its children will be rendered.
77    */
78   Node* GetSourceNode() const;
79
80   /**
81    * Set whether the RenderTask has exclusive access to the source nodes.
82    * @param[in] exclusive True if the source nodes will only be rendered by this render-task.
83    */
84   void SetExclusive( bool exclusive );
85
86   /**
87    * Query whether the RenderTask has exclusive access to the source actors.
88    * @return True if the source actors will only be rendered by this render-task.
89    */
90   bool IsExclusive() const;
91
92   /**
93    * Set the node from which the scene is viewed.
94    * @param[in] node The scene is viewed from the perspective of this node.
95    */
96   void SetCameraNode( Node* node );
97
98   /**
99    * Retrieve the camera node.
100    * @return The scene is viewed from the perspective of this node.
101    */
102   Node* GetCameraNode() const;
103
104   /**
105    * Set the frame-buffer used as a render target.
106    * @param[in] resourceId The resource ID of the frame-buffer, or zero if not rendering off-screen.
107    */
108   void SetFrameBufferId( unsigned int resourceId );
109
110   /**
111    * Retrieve the resource ID of the frame-buffer.
112    * @return The resource ID, or zero if not rendering off-screen.
113    */
114   unsigned int GetFrameBufferId() const;
115
116   /**
117    * Set the value of property viewport-position
118    * This value will persist only for the current frame.
119    * @param[in] updateBufferIndex The current update buffer index.
120    * @param[in] value The value of the property
121    */
122   void SetViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
123
124   /**
125    * Get the value of property viewport-position
126    * @warning Should only be called from the Update thread
127    * @param[in] bufferIndex The buffer to read from.
128    * @return the value of the property.
129    */
130   const Vector2& GetViewportPosition( BufferIndex bufferIndex ) const;
131
132   /**
133    * Bake the value of the property viewport-position
134    * This will also set the base value
135    * @param[in] updateBufferIndex The current update buffer index.
136    * @param[in] value The new value for property.
137    */
138   void BakeViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
139
140   /**
141    * Set the value of property viewport-size
142    * This value will persist only for the current frame.
143    * @param[in] updateBufferIndex The current update buffer index.
144    * @param[in] value The value of the property
145    */
146   void SetViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
147
148   /**
149    * Get the value of property viewport-size
150    * @warning Should only be called from the Update thread
151    * @param[in] bufferIndex The buffer to read from.
152    * @return the value of the property.
153    */
154   const Vector2& GetViewportSize( BufferIndex bufferIndex ) const;
155
156   /**
157    * Bake the value of the property viewport-size
158    * This will also set the base value
159    * @param[in] updateBufferIndex The current update buffer index.
160    * @param[in] value The new value for property.
161    */
162   void BakeViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
163
164   /**
165    * Get the value of property viewport-enabled
166    * @warning Should only be called from the Update thread
167    * @param[in] bufferIndex The buffer to read from.
168    * @return the value of the property.
169    */
170   bool GetViewportEnabled( BufferIndex bufferIndex ) const;
171
172   /**
173    * Query whether the optional viewport is set.
174    * @param[in] bufferIndex The buffer to read from.
175    * @param[out] viewport The viewport position and size is populated.
176    * @return true if the viewport has been set
177    */
178   bool QueryViewport( BufferIndex bufferIndex, Viewport& viewport ) const;
179
180   /**
181    * Set the value of property clear-color
182    * This value will persist only for the current frame.
183    * @param[in] updateBufferIndex The current update buffer index.
184    * @param[in] value The value of the property
185    */
186   void SetClearColor( BufferIndex updateBufferIndex, const Vector4& value );
187
188   /**
189    * Get the value of property clear-color
190    * @warning Should only be called from the Update thread
191    * @param[in] bufferIndex The buffer to read from.
192    * @return the value of the property.
193    */
194   const Vector4& GetClearColor( BufferIndex bufferIndex ) const;
195
196   /**
197    * Bake the value of the property clear-color
198    * This will also set the base value
199    * @param[in] updateBufferIndex The current update buffer index.
200    * @param[in] value The new value for property.
201    */
202   void BakeClearColor( BufferIndex updateBufferIndex, const Vector4& value );
203
204   /**
205    * @copydoc Dali::RenderTask::SetClearEnabled()
206    */
207   void SetClearEnabled( bool enabled );
208
209   /**
210    * @copydoc Dali::RenderTask::GetClearEnabled()
211    */
212   bool GetClearEnabled() const;
213
214   /**
215    * Set the refresh-rate of the RenderTask.
216    * @param[in] refreshRate The new refresh rate.
217    */
218   void SetRefreshRate( unsigned int refreshRate );
219
220   /**
221    * Retrieve the refresh-rate of the RenderTask.
222    * @return The refresh rate.
223    */
224   unsigned int GetRefreshRate() const;
225
226   /**
227    * Check if the render task is ready for rendering.
228    * @param[in] updateBufferIndex The current update buffer index.
229    * @return True if the render-task is ready for rendering.
230    */
231   bool ReadyToRender( BufferIndex updateBufferIndex );
232
233   /**
234    * True if a render is required. If the current state is RENDER_CONTINUOUSLY, then
235    * this returns true if the frame count is zero. If the current state is RENDER_ONCE_WAITING_FOR_RESOURCES, then it always returns true. In all other states, it returns false.
236    * @return true if a render is required
237    */
238   bool IsRenderRequired();
239
240   /**
241    * Process a frame. This method is called each frame for every ready render task, regardless
242    * of whether it needs to render (so that the frame counter can be updated).
243    * @param[in] resourcesComplete true if the resources of the source tree are completely loaded.
244    */
245   void UpdateState( bool resourcesComplete );
246
247   /**
248    * Return true only if currently waiting for the render task to
249    * finish rendering and the update thread should be kept alive.
250    * @return true if waiting to be rendered
251    */
252   bool IsWaitingToRender();
253
254   /**
255    * Return true when the render task has finished rendering and a notification
256    * needs sending. (Only one notification is sent per render once request)
257    * @return true if notification is required.
258    */
259   bool HasRendered();
260
261   /**
262    * @return The number of times we have transited from RENDERED_ONCE to RENDERED_ONCE_AND_NOTIFIED state.
263    */
264   unsigned int GetRenderedOnceCounter() const;
265
266   /**
267    * Retrieve the view-matrix; this is double buffered for input handling.
268    * @pre GetCameraNode() returns a node with valid CameraAttachment.
269    * @param[in] bufferIndex The buffer to read from.
270    * @return The view-matrix.
271    */
272   const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
273
274   /**
275    * Retrieve the projection-matrix; this is double buffered for input handling.
276    * @pre GetCameraNode() returns a node with valid CameraAttachment.
277    * @param[in] bufferIndex The buffer to read from.
278    * @return The projection-matrix.
279    */
280   const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
281
282   /**
283    * Prepares the render-instruction buffer to be populated with instructions.
284    * @param[out] instruction to prepare
285    * @param[in] updateBufferIndex The current update buffer index.
286    */
287   void PrepareRenderInstruction( RenderInstruction& instruction, BufferIndex updateBufferIndex );
288
289   /**
290    * @return true if the view matrix has been updated during this or last frame
291    */
292   bool ViewMatrixUpdated();
293
294   /**
295    * Set the complete status tracker.
296    * @param[in] completeStatusManager The complete status Tracker (not owned)
297    */
298   void SetCompleteStatusManager( CompleteStatusManager* completeStatusManager );
299
300 private:
301
302   /**
303    * Protected constructor.
304    */
305   RenderTask();
306
307   // Undefined
308   RenderTask(const RenderTask&);
309
310   // Undefined
311   RenderTask& operator=(const RenderTask&);
312
313 private: // PropertyOwner
314
315   virtual void ResetDefaultProperties( BufferIndex currentBufferIndex );
316
317 public: // Animatable Properties
318   AnimatableProperty< Vector2 >   mViewportPosition;    ///< viewport-position
319   AnimatableProperty< Vector2 >   mViewportSize;        ///< viewport-size
320   AnimatableProperty< Vector4 >   mClearColor;          ///< clear-color
321
322 private:
323   CompleteStatusManager* mCompleteStatusManager;
324   Node* mSourceNode;
325   Node* mCameraNode;
326   CameraAttachment* mCameraAttachment;
327   unsigned int mFrameBufferResourceId;
328
329   bool mWaitingToRender:1; ///< True when an render once to FBO is waiting
330   bool mNotifyTrigger:1; ///< True if a render once render task has finished renderering
331   bool mExclusive: 1; ///< Whether the render task has exclusive access to the source actor (node in the scene graph implementation).
332   bool mClearEnabled: 1; ///< Whether previous results are cleared.
333
334   FrameBufferTexture* mRenderTarget;
335   Viewport mViewport;
336
337   State mState;                     ///< Render state.
338   unsigned int mRefreshRate;        ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
339   unsigned int mFrameCounter;       ///< counter for rendering every N frames
340
341   unsigned int mRenderedOnceCounter;  ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
342
343 };
344
345 // Messages for RenderTask
346
347 inline void SetFrameBufferIdMessage( EventToUpdate& eventToUpdate, RenderTask& task, unsigned int resourceId )
348 {
349   typedef MessageValue1< RenderTask, unsigned int > LocalType;
350
351   // Reserve some memory inside the message queue
352   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
353
354   // Construct message in the message queue memory; note that delete should not be called on the return value
355   new (slot) LocalType( &task, &RenderTask::SetFrameBufferId, resourceId );
356 }
357
358 inline void SetClearColorMessage( EventToUpdate& eventToUpdate, RenderTask& task, const Vector4& value )
359 {
360   typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
361
362   // Reserve some memory inside the message queue
363   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
364
365   // Construct message in the message queue memory; note that delete should not be called on the return value
366   new (slot) LocalType( &task, &RenderTask::SetClearColor, value );
367 }
368
369 inline void BakeClearColorMessage( EventToUpdate& eventToUpdate, const RenderTask& task, const Vector4& value )
370 {
371   typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
372
373   // Reserve some memory inside the message queue
374   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
375
376   // Construct message in the message queue memory; note that delete should not be called on the return value
377   new (slot) LocalType( &task, &RenderTask::BakeClearColor, value );
378 }
379
380 inline void SetClearEnabledMessage( EventToUpdate& eventToUpdate, RenderTask& task, bool enabled )
381 {
382   typedef MessageValue1< RenderTask, bool > LocalType;
383
384   // Reserve some memory inside the message queue
385   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
386
387   // Construct message in the message queue memory; note that delete should not be called on the return value
388   new (slot) LocalType( &task, &RenderTask::SetClearEnabled, enabled );
389 }
390
391 inline void SetRefreshRateMessage( EventToUpdate& eventToUpdate, RenderTask& task, unsigned int refreshRate )
392 {
393   typedef MessageValue1< RenderTask, unsigned int > LocalType;
394
395   // Reserve some memory inside the message queue
396   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
397
398   // Construct message in the message queue memory; note that delete should not be called on the return value
399   new (slot) LocalType( &task, &RenderTask::SetRefreshRate, refreshRate );
400 }
401
402 inline void SetSourceNodeMessage( EventToUpdate& eventToUpdate, RenderTask& task, const Node* constNode )
403 {
404   // Scene graph thread can destroy this object.
405   Node* node = const_cast< Node* >( constNode );
406
407   typedef MessageValue1< RenderTask, Node* > LocalType;
408
409   // Reserve some memory inside the message queue
410   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
411
412   // Construct message in the message queue memory; note that delete should not be called on the return value
413   new (slot) LocalType( &task, &RenderTask::SetSourceNode, node );
414 }
415
416 inline void SetCameraNodeMessage( EventToUpdate& eventToUpdate, RenderTask& task, const Node* constNode )
417 {
418   // Scene graph thread can destroy this object.
419   Node* node = const_cast< Node* >( constNode );
420
421   typedef MessageValue1< RenderTask, Node* > LocalType;
422
423   // Reserve some memory inside the message queue
424   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
425
426   // Construct message in the message queue memory; note that delete should not be called on the return value
427   new (slot) LocalType( &task, &RenderTask::SetCameraNode, node );
428 }
429
430 inline void SetExclusiveMessage( EventToUpdate& eventToUpdate, RenderTask& task, bool exclusive )
431 {
432   typedef MessageValue1< RenderTask, bool > LocalType;
433
434   // Reserve some memory inside the message queue
435   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
436
437   // Construct message in the message queue memory; note that delete should not be called on the return value
438   new (slot) LocalType( &task, &RenderTask::SetExclusive, exclusive );
439 }
440
441 inline void BakeViewportPositionMessage( EventToUpdate& eventToUpdate, const RenderTask& task, const Vector2& value )
442 {
443   typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
444
445   // Reserve some memory inside the message queue
446   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
447
448   // Construct message in the message queue memory; note that delete should not be called on the return value
449   new (slot) LocalType( &task, &RenderTask::BakeViewportPosition, value );
450 }
451
452 inline void BakeViewportSizeMessage( EventToUpdate& eventToUpdate, const RenderTask& task, const Vector2& value )
453 {
454   typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
455
456   // Reserve some memory inside the message queue
457   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
458
459   // Construct message in the message queue memory; note that delete should not be called on the return value
460   new (slot) LocalType( &task, &RenderTask::BakeViewportSize, value );
461 }
462
463 } // namespace SceneGraph
464
465 } // namespace Internal
466
467 } // namespace Dali
468
469 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__