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