1 #ifndef DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H
2 #define DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H
5 * Copyright (c) 2018 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>
29 #include <dali/internal/render/renderers/render-frame-buffer.h>
46 class RenderInstruction;
47 class RenderMessageDispatcher;
50 * RenderTasks describe how the Dali scene should be rendered.
52 class RenderTask : public PropertyOwner, public PropertyOwner::Observer
58 RENDER_CONTINUOUSLY, ///< mRefreshRate > 0
59 RENDER_ONCE_WAITING_FOR_RESOURCES, ///< mRefreshRate = REFRESH_ONCE
60 RENDERED_ONCE, ///< mRefreshRate = REFRESH_ONCE & rendered
61 RENDERED_ONCE_AND_NOTIFIED ///< mRefreshRate = REFRESH_ONCE & rendered & notified
65 * Create a new RenderTask
67 static RenderTask* New();
72 virtual ~RenderTask();
75 * Initialize the render task. Called in update thread
76 * @param[in] renderMessageDispatcher to send messages to render thread
78 void Initialize( RenderMessageDispatcher& renderMessageDispatcher );
81 * Set the nodes to be rendered.
82 * @param[in] node This node and its children will be rendered.
84 void SetSourceNode( Node* node );
87 * Retrieve the source node.
88 * @return This node and its children will be rendered.
90 Node* GetSourceNode() const;
93 * Set whether the RenderTask has exclusive access to the source nodes.
94 * @param[in] exclusive True if the source nodes will only be rendered by this render-task.
96 void SetExclusive( bool exclusive );
99 * Query whether the RenderTask has exclusive access to the source actors.
100 * @return True if the source actors will only be rendered by this render-task.
102 bool IsExclusive() const;
105 * Set the camera from which the scene is viewed.
106 * @param[in] cameraNode that camera is connected with
107 * @param[in] camera to use.
109 void SetCamera( Node* cameraNode, Camera* camera );
112 * Set the frame-buffer used as a render target.
113 * @param[in] frameBuffer The framebuffer
115 void SetFrameBuffer( Render::FrameBuffer* frameBuffer );
118 * Retrieve the resource ID of the frame-buffer.
119 * @return The resource ID, or zero if not rendering off-screen.
121 Render::FrameBuffer* GetFrameBuffer();
124 * Set the value of property viewportPosition
125 * This value will persist only for the current frame.
126 * @param[in] updateBufferIndex The current update buffer index.
127 * @param[in] value The value of the property
129 void SetViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
132 * Get the value of property viewportPosition
133 * @warning Should only be called from the Update thread
134 * @param[in] bufferIndex The buffer to read from.
135 * @return the value of the property.
137 const Vector2& GetViewportPosition( BufferIndex bufferIndex ) const;
140 * Bake the value of the property viewportPosition
141 * This will also set the base value
142 * @param[in] updateBufferIndex The current update buffer index.
143 * @param[in] value The new value for property.
145 void BakeViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
148 * Set the value of property viewportSize
149 * This value will persist only for the current frame.
150 * @param[in] updateBufferIndex The current update buffer index.
151 * @param[in] value The value of the property
153 void SetViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
156 * Get the value of property viewportSize
157 * @warning Should only be called from the Update thread
158 * @param[in] bufferIndex The buffer to read from.
159 * @return the value of the property.
161 const Vector2& GetViewportSize( BufferIndex bufferIndex ) const;
164 * Bake the value of the property viewportSize
165 * This will also set the base value
166 * @param[in] updateBufferIndex The current update buffer index.
167 * @param[in] value The new value for property.
169 void BakeViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
172 * Get the value of property viewportEnabled
173 * @warning Should only be called from the Update thread
174 * @param[in] bufferIndex The buffer to read from.
175 * @return the value of the property.
177 bool GetViewportEnabled( BufferIndex bufferIndex ) const;
180 * Query whether the optional viewport is set.
181 * @param[in] bufferIndex The buffer to read from.
182 * @param[out] viewport The viewport position and size is populated.
183 * @return true if the viewport has been set
185 bool QueryViewport( BufferIndex bufferIndex, Viewport& viewport ) const;
188 * Set the value of property clearColor
189 * This value will persist only for the current frame.
190 * @param[in] updateBufferIndex The current update buffer index.
191 * @param[in] value The value of the property
193 void SetClearColor( BufferIndex updateBufferIndex, const Vector4& value );
196 * Get the value of property clearColor
197 * @warning Should only be called from the Update thread
198 * @param[in] bufferIndex The buffer to read from.
199 * @return the value of the property.
201 const Vector4& GetClearColor( BufferIndex bufferIndex ) const;
204 * Bake the value of the property clearColor
205 * This will also set the base value
206 * @param[in] updateBufferIndex The current update buffer index.
207 * @param[in] value The new value for property.
209 void BakeClearColor( BufferIndex updateBufferIndex, const Vector4& value );
212 * @copydoc Dali::RenderTask::SetClearEnabled()
214 void SetClearEnabled( bool enabled );
217 * @copydoc Dali::RenderTask::GetClearEnabled()
219 bool GetClearEnabled() const;
222 * @copydoc Dali::RenderTask::SetCullMode()
224 void SetCullMode( bool mode );
227 * @copydoc Dali::RenderTask::GetCullMode()
229 bool GetCullMode() const;
232 * Set the refresh-rate of the RenderTask.
233 * @param[in] refreshRate The new refresh rate.
235 void SetRefreshRate( uint32_t refreshRate );
238 * Retrieve the refresh-rate of the RenderTask.
239 * @return The refresh rate.
241 uint32_t GetRefreshRate() const;
244 * Check if the render task is ready for rendering.
245 * @param[in] updateBufferIndex The current update buffer index.
246 * @return True if the render-task is ready for rendering.
248 bool ReadyToRender( BufferIndex updateBufferIndex );
251 * True if a render is required. If the current state is RENDER_CONTINUOUSLY, then
252 * 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.
253 * @return true if a render is required
255 bool IsRenderRequired();
258 * Process a frame. This method is called each frame for every ready render task, regardless
259 * of whether it needs to render (so that the frame counter can be updated).
264 * Return true only if currently waiting for the render task to
265 * finish rendering and the update thread should be kept alive.
266 * @return true if waiting to be rendered
268 bool IsWaitingToRender();
271 * Return true when the render task has finished rendering and a notification
272 * needs sending. (Only one notification is sent per render once request)
273 * @return true if notification is required.
278 * @return The number of times we have transited from RENDERED_ONCE to RENDERED_ONCE_AND_NOTIFIED state.
280 uint32_t GetRenderedOnceCounter() const;
283 * Retrieve the view-matrix; this is double buffered for input handling.
284 * @pre GetCameraNode() returns a node with valid Camera.
285 * @param[in] bufferIndex The buffer to read from.
286 * @return The view-matrix.
288 const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
291 * @brief Retrieve the camera.
292 * @pre GetCameraNode() returns a node with valid Camera.
294 * @return The camera.
296 SceneGraph::Camera& GetCamera() const;
299 * Retrieve the projection-matrix; this is double buffered for input handling.
300 * @pre GetCameraNode() returns a node with valid Camera.
301 * @param[in] bufferIndex The buffer to read from.
302 * @return The projection-matrix.
304 const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
307 * Prepares the render-instruction buffer to be populated with instructions.
309 * If the render task is a render-once framebuffer backed by a native image,
310 * then this method will ensure that a GL sync object is created to track
311 * when the rendering has finished.
313 * @param[out] instruction to prepare
314 * @param[in] updateBufferIndex The current update buffer index.
316 void PrepareRenderInstruction( RenderInstruction& instruction, BufferIndex updateBufferIndex );
319 * @return true if the view matrix has been updated during this or last frame
321 bool ViewMatrixUpdated();
324 * Indicate whether GL sync is required for native render target.
325 * @param[in] requiresSync whether GL sync is required for native render target
327 void SetSyncRequired( bool requiresSync );
329 private: // from PropertyOwner::Observer
332 * @copydoc PropertyOwner::Observer::PropertyOwnerConnected( PropertyOwner& owner )
334 void PropertyOwnerConnected( PropertyOwner& owner );
337 * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected( BufferIndex updateBufferIndex, PropertyOwner& owner )
339 void PropertyOwnerDisconnected( BufferIndex updateBufferIndex, PropertyOwner& owner );
342 * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed( PropertyOwner& owner )
344 void PropertyOwnerDestroyed( PropertyOwner& owner );
348 void SetActiveStatus();
356 RenderTask(const RenderTask&) = delete;
357 RenderTask& operator=(const RenderTask&) = delete;
359 public: // Animatable Properties
360 AnimatableProperty< Vector2 > mViewportPosition; ///< viewportPosition
361 AnimatableProperty< Vector2 > mViewportSize; ///< viewportSize
362 AnimatableProperty< Vector4 > mClearColor; ///< clearColor
365 RenderMessageDispatcher* mRenderMessageDispatcher;
366 Render::RenderTracker* mRenderSyncTracker;
369 SceneGraph::Camera* mCamera;
370 Render::FrameBuffer* mFrameBuffer;
372 uint32_t mRefreshRate; ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
373 uint32_t mFrameCounter; ///< counter for rendering every N frames
374 uint32_t mRenderedOnceCounter;///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
376 State mState; ///< Render state.
378 bool mRequiresSync:1; ///< Whether sync is needed to track the render
379 bool mActive:1; ///< True when the task is active, i.e. has valid source and camera
380 bool mWaitingToRender:1;///< True when an render once to FBO is waiting
381 bool mNotifyTrigger:1; ///< True if a render once render task has finished renderering
382 bool mExclusive:1; ///< Whether the render task has exclusive access to the source actor (node in the scene graph).
383 bool mClearEnabled:1; ///< Whether previous results are cleared.
384 bool mCullMode:1; ///< Whether renderers should be frustum culled
388 // Messages for RenderTask
389 inline void SetFrameBufferMessage( EventThreadServices& eventThreadServices, const RenderTask& task, Render::FrameBuffer* frameBuffer )
391 typedef MessageValue1< RenderTask, Render::FrameBuffer*> LocalType;
393 // Reserve some memory inside the message queue
394 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
396 // Construct message in the message queue memory; note that delete should not be called on the return value
397 new (slot) LocalType( &task, &RenderTask::SetFrameBuffer, frameBuffer );
400 inline void SetClearColorMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector4& value )
402 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
404 // Reserve some memory inside the message queue
405 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
407 // Construct message in the message queue memory; note that delete should not be called on the return value
408 new (slot) LocalType( &task, &RenderTask::SetClearColor, value );
411 inline void BakeClearColorMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector4& value )
413 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
415 // Reserve some memory inside the message queue
416 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
418 // Construct message in the message queue memory; note that delete should not be called on the return value
419 new (slot) LocalType( &task, &RenderTask::BakeClearColor, value );
422 inline void SetClearEnabledMessage( EventThreadServices& eventThreadServices, const RenderTask& task, bool enabled )
424 typedef MessageValue1< RenderTask, bool > LocalType;
426 // Reserve some memory inside the message queue
427 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
429 // Construct message in the message queue memory; note that delete should not be called on the return value
430 new (slot) LocalType( &task, &RenderTask::SetClearEnabled, enabled );
433 inline void SetCullModeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, bool mode )
435 typedef MessageValue1< RenderTask, bool > LocalType;
437 // Reserve some memory inside the message queue
438 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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::SetCullMode, mode );
444 inline void SetRefreshRateMessage( EventThreadServices& eventThreadServices, const RenderTask& task, uint32_t refreshRate )
446 typedef MessageValue1< RenderTask, uint32_t > LocalType;
448 // Reserve some memory inside the message queue
449 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
451 // Construct message in the message queue memory; note that delete should not be called on the return value
452 new (slot) LocalType( &task, &RenderTask::SetRefreshRate, refreshRate );
455 inline void SetSourceNodeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Node* constNode )
457 // Scene graph thread can destroy this object.
458 Node* node = const_cast< Node* >( constNode );
460 typedef MessageValue1< RenderTask, Node* > LocalType;
462 // Reserve some memory inside the message queue
463 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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::SetSourceNode, node );
469 inline void SetCameraMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Node* constNode, const Camera* constCamera )
471 typedef MessageValue2< RenderTask, Node*, Camera* > LocalType;
473 Node* node = const_cast< Node* >( constNode );
474 Camera* camera = const_cast< Camera* >( constCamera );
475 // Reserve memory inside the message queue
476 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
478 // Construct message in the message queue memory; note that delete should not be called on the return value
479 new (slot) LocalType( &task, &RenderTask::SetCamera, node, camera );
482 inline void SetExclusiveMessage( EventThreadServices& eventThreadServices, const RenderTask& task, bool exclusive )
484 typedef MessageValue1< RenderTask, bool > LocalType;
486 // Reserve some memory inside the message queue
487 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
489 // Construct message in the message queue memory; note that delete should not be called on the return value
490 new (slot) LocalType( &task, &RenderTask::SetExclusive, exclusive );
493 inline void SetSyncRequiredMessage(EventThreadServices& eventThreadServices, const RenderTask& task, bool requiresSync )
495 typedef MessageValue1< RenderTask, bool > LocalType;
497 // Reserve some memory inside the message queue
498 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
500 // Construct message in the message queue memory; note that delete should not be called on the return value
501 new (slot) LocalType( &task, &RenderTask::SetSyncRequired, requiresSync );
504 inline void BakeViewportPositionMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
506 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
508 // Reserve some memory inside the message queue
509 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
511 // Construct message in the message queue memory; note that delete should not be called on the return value
512 new (slot) LocalType( &task, &RenderTask::BakeViewportPosition, value );
515 inline void BakeViewportSizeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
517 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
519 // Reserve some memory inside the message queue
520 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
522 // Construct message in the message queue memory; note that delete should not be called on the return value
523 new (slot) LocalType( &task, &RenderTask::BakeViewportSize, value );
526 } // namespace SceneGraph
528 } // namespace Internal
532 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H