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 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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/message.h>
26 #include <dali/internal/event/common/event-thread-services.h>
27 #include <dali/internal/update/common/property-owner.h>
28 #include <dali/internal/update/common/animatable-property.h>
35 class FrameBufferTexture;
36 class ResourceManager;
46 class CameraAttachment;
47 class RenderInstruction;
48 class RenderMessageDispatcher;
51 * RenderTasks describe how the Dali scene should be rendered.
53 class RenderTask : public PropertyOwner
59 RENDER_CONTINUOUSLY, ///< mRefreshRate > 0
60 RENDER_ONCE_WAITING_FOR_RESOURCES, ///< mRefreshRate = REFRESH_ONCE
61 RENDERED_ONCE, ///< mRefreshRate = REFRESH_ONCE & rendered
62 RENDERED_ONCE_AND_NOTIFIED ///< mRefreshRate = REFRESH_ONCE & rendered & notified
66 * Create a new RenderTask
68 static RenderTask* New();
73 virtual ~RenderTask();
76 * Initialize the render task. Called in update thread
77 * @param[in] renderMessageDispatcher to send messages to render thread
78 * @param[in] resourceManager to check and update status of FBOs
80 void Initialize( RenderMessageDispatcher& renderMessageDispatcher, ResourceManager& resourceManager );
83 * Set the nodes to be rendered.
84 * @param[in] node This node and its children will be rendered.
86 void SetSourceNode( Node* node );
89 * Retrieve the source node.
90 * @return This node and its children will be rendered.
92 Node* GetSourceNode() const;
95 * Set whether the RenderTask has exclusive access to the source nodes.
96 * @param[in] exclusive True if the source nodes will only be rendered by this render-task.
98 void SetExclusive( bool exclusive );
101 * Query whether the RenderTask has exclusive access to the source actors.
102 * @return True if the source actors will only be rendered by this render-task.
104 bool IsExclusive() const;
107 * Set the node from which the scene is viewed.
108 * @param[in] node The scene is viewed from the perspective of this node.
110 void SetCameraNode( Node* node );
113 * Set the frame-buffer used as a render target.
114 * @param[in] resourceId The resource ID of the frame-buffer, or zero if not rendering off-screen.
115 * @param[in] isNativeFBO if this render task is targeting a native FBO
117 void SetFrameBufferId( unsigned int resourceId, bool isNativeFBO );
120 * Retrieve the resource ID of the frame-buffer.
121 * @return The resource ID, or zero if not rendering off-screen.
123 unsigned int GetFrameBufferId() const;
126 * Set the value of property viewportPosition
127 * This value will persist only for the current frame.
128 * @param[in] updateBufferIndex The current update buffer index.
129 * @param[in] value The value of the property
131 void SetViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
134 * Get the value of property viewportPosition
135 * @warning Should only be called from the Update thread
136 * @param[in] bufferIndex The buffer to read from.
137 * @return the value of the property.
139 const Vector2& GetViewportPosition( BufferIndex bufferIndex ) const;
142 * Bake the value of the property viewportPosition
143 * This will also set the base value
144 * @param[in] updateBufferIndex The current update buffer index.
145 * @param[in] value The new value for property.
147 void BakeViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
150 * Set the value of property viewportSize
151 * This value will persist only for the current frame.
152 * @param[in] updateBufferIndex The current update buffer index.
153 * @param[in] value The value of the property
155 void SetViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
158 * Get the value of property viewportSize
159 * @warning Should only be called from the Update thread
160 * @param[in] bufferIndex The buffer to read from.
161 * @return the value of the property.
163 const Vector2& GetViewportSize( BufferIndex bufferIndex ) const;
166 * Bake the value of the property viewportSize
167 * This will also set the base value
168 * @param[in] updateBufferIndex The current update buffer index.
169 * @param[in] value The new value for property.
171 void BakeViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
174 * Get the value of property viewportEnabled
175 * @warning Should only be called from the Update thread
176 * @param[in] bufferIndex The buffer to read from.
177 * @return the value of the property.
179 bool GetViewportEnabled( BufferIndex bufferIndex ) const;
182 * Query whether the optional viewport is set.
183 * @param[in] bufferIndex The buffer to read from.
184 * @param[out] viewport The viewport position and size is populated.
185 * @return true if the viewport has been set
187 bool QueryViewport( BufferIndex bufferIndex, Viewport& viewport ) const;
190 * Set the value of property clearColor
191 * This value will persist only for the current frame.
192 * @param[in] updateBufferIndex The current update buffer index.
193 * @param[in] value The value of the property
195 void SetClearColor( BufferIndex updateBufferIndex, const Vector4& value );
198 * Get the value of property clearColor
199 * @warning Should only be called from the Update thread
200 * @param[in] bufferIndex The buffer to read from.
201 * @return the value of the property.
203 const Vector4& GetClearColor( BufferIndex bufferIndex ) const;
206 * Bake the value of the property clearColor
207 * This will also set the base value
208 * @param[in] updateBufferIndex The current update buffer index.
209 * @param[in] value The new value for property.
211 void BakeClearColor( BufferIndex updateBufferIndex, const Vector4& value );
214 * @copydoc Dali::RenderTask::SetClearEnabled()
216 void SetClearEnabled( bool enabled );
219 * @copydoc Dali::RenderTask::GetClearEnabled()
221 bool GetClearEnabled() const;
224 * @copydoc Dali::RenderTask::SetCullMode()
226 void SetCullMode( bool mode );
229 * @copydoc Dali::RenderTask::GetCullMode()
231 bool GetCullMode() const;
234 * Set the refresh-rate of the RenderTask.
235 * @param[in] refreshRate The new refresh rate.
237 void SetRefreshRate( unsigned int refreshRate );
240 * Retrieve the refresh-rate of the RenderTask.
241 * @return The refresh rate.
243 unsigned int GetRefreshRate() const;
246 * Check if the render task is ready for rendering.
247 * @param[in] updateBufferIndex The current update buffer index.
248 * @return True if the render-task is ready for rendering.
250 bool ReadyToRender( BufferIndex updateBufferIndex );
253 * True if a render is required. If the current state is RENDER_CONTINUOUSLY, then
254 * 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.
255 * @return true if a render is required
257 bool IsRenderRequired();
260 * Set whether all resources were available when the render-task was processed
261 * @param[in] resourcesComplete True if the resources of the source tree are completely loaded.
263 void SetResourcesFinished( bool resourcesFinished );
266 * Process a frame. This method is called each frame for every ready render task, regardless
267 * of whether it needs to render (so that the frame counter can be updated).
272 * Return true only if currently waiting for the render task to
273 * finish rendering and the update thread should be kept alive.
274 * @return true if waiting to be rendered
276 bool IsWaitingToRender();
279 * Return true when the render task has finished rendering and a notification
280 * needs sending. (Only one notification is sent per render once request)
281 * @return true if notification is required.
286 * @return The number of times we have transited from RENDERED_ONCE to RENDERED_ONCE_AND_NOTIFIED state.
288 unsigned int GetRenderedOnceCounter() const;
291 * Retrieve the view-matrix; this is double buffered for input handling.
292 * @pre GetCameraNode() returns a node with valid CameraAttachment.
293 * @param[in] bufferIndex The buffer to read from.
294 * @return The view-matrix.
296 const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
299 * @brief Retrieve the camera attachment.
300 * @pre GetCameraNode() returns a node with valid CameraAttachment.
302 * @return The camera attachment.
304 SceneGraph::CameraAttachment& GetCameraAttachment() const;
307 * Retrieve the projection-matrix; this is double buffered for input handling.
308 * @pre GetCameraNode() returns a node with valid CameraAttachment.
309 * @param[in] bufferIndex The buffer to read from.
310 * @return The projection-matrix.
312 const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
315 * Prepares the render-instruction buffer to be populated with instructions.
316 * @param[out] instruction to prepare
317 * @param[in] updateBufferIndex The current update buffer index.
319 void PrepareRenderInstruction( RenderInstruction& instruction, BufferIndex updateBufferIndex );
322 * @return true if the view matrix has been updated during this or last frame
324 bool ViewMatrixUpdated();
327 * @return A pointer to the camera used by the RenderTask
329 Node* GetCamera() const;
334 * Protected constructor.
339 RenderTask(const RenderTask&);
342 RenderTask& operator=(const RenderTask&);
344 private: // PropertyOwner
346 virtual void ResetDefaultProperties( BufferIndex currentBufferIndex );
348 public: // Animatable Properties
349 AnimatableProperty< Vector2 > mViewportPosition; ///< viewportPosition
350 AnimatableProperty< Vector2 > mViewportSize; ///< viewportSize
351 AnimatableProperty< Vector4 > mClearColor; ///< clearColor
354 RenderMessageDispatcher* mRenderMessageDispatcher;
355 ResourceManager* mResourceManager;
356 Render::RenderTracker* mRenderSyncTracker;
359 CameraAttachment* mCameraAttachment;
360 unsigned int mFrameBufferResourceId;
362 bool mResourcesFinished:1; ///< True if all resources were available when the render-task was processed
363 bool mWaitingToRender:1; ///< True when an render once to FBO is waiting
364 bool mNotifyTrigger:1; ///< True if a render once render task has finished renderering
365 bool mExclusive: 1; ///< Whether the render task has exclusive access to the source actor (node in the scene graph implementation).
366 bool mClearEnabled: 1; ///< Whether previous results are cleared.
367 bool mCullMode: 1; ///< Whether renderers should be frustum culled
369 FrameBufferTexture* mRenderTarget;
372 State mState; ///< Render state.
373 unsigned int mRefreshRate; ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
374 unsigned int mFrameCounter; ///< counter for rendering every N frames
376 unsigned int mRenderedOnceCounter; ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
377 bool mTargetIsNativeFramebuffer; ///< Tells if our target is a native framebuffer
381 // Messages for RenderTask
383 inline void SetFrameBufferIdMessage( EventThreadServices& eventThreadServices, RenderTask& task, unsigned int resourceId, bool isNativeFBO )
385 typedef MessageValue2< RenderTask, unsigned int, bool > LocalType;
387 // Reserve some memory inside the message queue
388 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
390 // Construct message in the message queue memory; note that delete should not be called on the return value
391 new (slot) LocalType( &task, &RenderTask::SetFrameBufferId, resourceId, isNativeFBO );
394 inline void SetClearColorMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Vector4& value )
396 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
398 // Reserve some memory inside the message queue
399 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
401 // Construct message in the message queue memory; note that delete should not be called on the return value
402 new (slot) LocalType( &task, &RenderTask::SetClearColor, value );
405 inline void BakeClearColorMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector4& value )
407 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
409 // Reserve some memory inside the message queue
410 unsigned int* slot = eventThreadServices.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::BakeClearColor, value );
416 inline void SetClearEnabledMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool enabled )
418 typedef MessageValue1< RenderTask, bool > LocalType;
420 // Reserve some memory inside the message queue
421 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
423 // Construct message in the message queue memory; note that delete should not be called on the return value
424 new (slot) LocalType( &task, &RenderTask::SetClearEnabled, enabled );
427 inline void SetCullModeMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool mode )
429 typedef MessageValue1< RenderTask, bool > LocalType;
431 // Reserve some memory inside the message queue
432 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
434 // Construct message in the message queue memory; note that delete should not be called on the return value
435 new (slot) LocalType( &task, &RenderTask::SetCullMode, mode );
438 inline void SetRefreshRateMessage( EventThreadServices& eventThreadServices, RenderTask& task, unsigned int refreshRate )
440 typedef MessageValue1< RenderTask, unsigned int > LocalType;
442 // Reserve some memory inside the message queue
443 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
445 // Construct message in the message queue memory; note that delete should not be called on the return value
446 new (slot) LocalType( &task, &RenderTask::SetRefreshRate, refreshRate );
449 inline void SetSourceNodeMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode )
451 // Scene graph thread can destroy this object.
452 Node* node = const_cast< Node* >( constNode );
454 typedef MessageValue1< RenderTask, Node* > LocalType;
456 // Reserve some memory inside the message queue
457 unsigned int* slot = eventThreadServices.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::SetSourceNode, node );
463 inline void SetCameraNodeMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode )
465 // Scene graph thread can destroy this object.
466 Node* node = const_cast< Node* >( constNode );
468 typedef MessageValue1< RenderTask, Node* > LocalType;
470 // Reserve some memory inside the message queue
471 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
473 // Construct message in the message queue memory; note that delete should not be called on the return value
474 new (slot) LocalType( &task, &RenderTask::SetCameraNode, node );
477 inline void SetExclusiveMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool exclusive )
479 typedef MessageValue1< RenderTask, bool > LocalType;
481 // Reserve some memory inside the message queue
482 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
484 // Construct message in the message queue memory; note that delete should not be called on the return value
485 new (slot) LocalType( &task, &RenderTask::SetExclusive, exclusive );
488 inline void BakeViewportPositionMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
490 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
492 // Reserve some memory inside the message queue
493 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
495 // Construct message in the message queue memory; note that delete should not be called on the return value
496 new (slot) LocalType( &task, &RenderTask::BakeViewportPosition, value );
499 inline void BakeViewportSizeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
501 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
503 // Reserve some memory inside the message queue
504 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
506 // Construct message in the message queue memory; note that delete should not be called on the return value
507 new (slot) LocalType( &task, &RenderTask::BakeViewportSize, value );
510 } // namespace SceneGraph
512 } // namespace Internal
516 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__