1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 // http://floralicense.org/license/
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.
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>
35 class FrameBufferTexture;
36 class CompleteStatusManager;
41 class CameraAttachment;
42 class RenderInstruction;
45 * RenderTasks describe how the Dali scene should be rendered.
47 class RenderTask : public PropertyOwner
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
59 * Create a new RenderTask
61 static RenderTask* New();
66 virtual ~RenderTask();
69 * Set the nodes to be rendered.
70 * @param[in] node This node and its children will be rendered.
72 void SetSourceNode( Node* node );
75 * Retrieve the source node.
76 * @return This node and its children will be rendered.
78 Node* GetSourceNode() const;
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.
84 void SetExclusive( bool exclusive );
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.
90 bool IsExclusive() const;
93 * Set the node from which the scene is viewed.
94 * @param[in] node The scene is viewed from the perspective of this node.
96 void SetCameraNode( Node* node );
99 * Retrieve the camera node.
100 * @return The scene is viewed from the perspective of this node.
102 Node* GetCameraNode() const;
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.
108 void SetFrameBufferId( unsigned int resourceId );
111 * Retrieve the resource ID of the frame-buffer.
112 * @return The resource ID, or zero if not rendering off-screen.
114 unsigned int GetFrameBufferId() const;
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
122 void SetViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
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.
130 const Vector2& GetViewportPosition( BufferIndex bufferIndex ) const;
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.
138 void BakeViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
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
146 void SetViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
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.
154 const Vector2& GetViewportSize( BufferIndex bufferIndex ) const;
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.
162 void BakeViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
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.
170 bool GetViewportEnabled( BufferIndex bufferIndex ) const;
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
178 bool QueryViewport( BufferIndex bufferIndex, Viewport& viewport ) const;
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
186 void SetClearColor( BufferIndex updateBufferIndex, const Vector4& value );
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.
194 const Vector4& GetClearColor( BufferIndex bufferIndex ) const;
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.
202 void BakeClearColor( BufferIndex updateBufferIndex, const Vector4& value );
205 * @copydoc Dali::RenderTask::SetClearEnabled()
207 void SetClearEnabled( bool enabled );
210 * @copydoc Dali::RenderTask::GetClearEnabled()
212 bool GetClearEnabled() const;
215 * Set the refresh-rate of the RenderTask.
216 * @param[in] refreshRate The new refresh rate.
218 void SetRefreshRate( unsigned int refreshRate );
221 * Retrieve the refresh-rate of the RenderTask.
222 * @return The refresh rate.
224 unsigned int GetRefreshRate() const;
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.
231 bool ReadyToRender( BufferIndex updateBufferIndex );
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
238 bool IsRenderRequired();
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.
245 void UpdateState( bool resourcesComplete );
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
252 bool IsWaitingToRender();
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.
262 * @return The number of times we have transited from RENDERED_ONCE to RENDERED_ONCE_AND_NOTIFIED state.
264 unsigned int GetRenderedOnceCounter() const;
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.
272 const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
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.
280 const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
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.
287 void PrepareRenderInstruction( RenderInstruction& instruction, BufferIndex updateBufferIndex );
290 * @return true if the view matrix has been updated during this or last frame
292 bool ViewMatrixUpdated();
295 * Set the complete status tracker.
296 * @param[in] completeStatusManager The complete status Tracker (not owned)
298 void SetCompleteStatusManager( CompleteStatusManager* completeStatusManager );
303 * Protected constructor.
308 RenderTask(const RenderTask&);
311 RenderTask& operator=(const RenderTask&);
313 private: // PropertyOwner
315 virtual void ResetDefaultProperties( BufferIndex currentBufferIndex );
317 public: // Animatable Properties
318 AnimatableProperty< Vector2 > mViewportPosition; ///< viewport-position
319 AnimatableProperty< Vector2 > mViewportSize; ///< viewport-size
320 AnimatableProperty< Vector4 > mClearColor; ///< clear-color
323 CompleteStatusManager* mCompleteStatusManager;
326 CameraAttachment* mCameraAttachment;
327 unsigned int mFrameBufferResourceId;
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.
334 FrameBufferTexture* mRenderTarget;
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
341 unsigned int mRenderedOnceCounter; ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
345 // Messages for RenderTask
347 inline void SetFrameBufferIdMessage( EventToUpdate& eventToUpdate, RenderTask& task, unsigned int resourceId )
349 typedef MessageValue1< RenderTask, unsigned int > LocalType;
351 // Reserve some memory inside the message queue
352 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
358 inline void SetClearColorMessage( EventToUpdate& eventToUpdate, RenderTask& task, const Vector4& value )
360 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
362 // Reserve some memory inside the message queue
363 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
369 inline void BakeClearColorMessage( EventToUpdate& eventToUpdate, const RenderTask& task, const Vector4& value )
371 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
373 // Reserve some memory inside the message queue
374 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
380 inline void SetClearEnabledMessage( EventToUpdate& eventToUpdate, RenderTask& task, bool enabled )
382 typedef MessageValue1< RenderTask, bool > LocalType;
384 // Reserve some memory inside the message queue
385 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
391 inline void SetRefreshRateMessage( EventToUpdate& eventToUpdate, RenderTask& task, unsigned int refreshRate )
393 typedef MessageValue1< RenderTask, unsigned int > LocalType;
395 // Reserve some memory inside the message queue
396 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
402 inline void SetSourceNodeMessage( EventToUpdate& eventToUpdate, RenderTask& task, const Node* constNode )
404 // Scene graph thread can destroy this object.
405 Node* node = const_cast< Node* >( constNode );
407 typedef MessageValue1< RenderTask, Node* > LocalType;
409 // Reserve some memory inside the message queue
410 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
416 inline void SetCameraNodeMessage( EventToUpdate& eventToUpdate, RenderTask& task, const Node* constNode )
418 // Scene graph thread can destroy this object.
419 Node* node = const_cast< Node* >( constNode );
421 typedef MessageValue1< RenderTask, Node* > LocalType;
423 // Reserve some memory inside the message queue
424 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
430 inline void SetExclusiveMessage( EventToUpdate& eventToUpdate, RenderTask& task, bool exclusive )
432 typedef MessageValue1< RenderTask, bool > LocalType;
434 // Reserve some memory inside the message queue
435 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
441 inline void BakeViewportPositionMessage( EventToUpdate& eventToUpdate, const RenderTask& task, const Vector2& value )
443 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
445 // Reserve some memory inside the message queue
446 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
452 inline void BakeViewportSizeMessage( EventToUpdate& eventToUpdate, const RenderTask& task, const Vector2& value )
454 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
456 // Reserve some memory inside the message queue
457 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
463 } // namespace SceneGraph
465 } // namespace Internal
469 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__