1 #ifndef DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H
2 #define DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H
5 * Copyright (c) 2020 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>
30 #include <dali/internal/render/common/render-instruction.h>
47 class RenderInstruction;
48 class RenderMessageDispatcher;
51 * RenderTasks describe how the Dali scene should be rendered.
53 class RenderTask : public PropertyOwner, public PropertyOwner::Observer
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
79 void Initialize( RenderMessageDispatcher& renderMessageDispatcher );
82 * Set the nodes to be rendered.
83 * @param[in] node This node and its children will be rendered.
85 void SetSourceNode( Node* node );
88 * Retrieve the source node.
89 * @return This node and its children will be rendered.
91 Node* GetSourceNode() const;
94 * Set whether the RenderTask has exclusive access to the source nodes.
95 * @param[in] exclusive True if the source nodes will only be rendered by this render-task.
97 void SetExclusive( bool exclusive );
100 * Query whether the RenderTask has exclusive access to the source actors.
101 * @return True if the source actors will only be rendered by this render-task.
103 bool IsExclusive() const;
106 * Set the camera from which the scene is viewed.
107 * @param[in] cameraNode that camera is connected with
108 * @param[in] camera to use.
110 void SetCamera( Node* cameraNode, Camera* camera );
113 * Set the frame-buffer used as a render target.
114 * @param[in] frameBuffer The framebuffer
116 void SetFrameBuffer( Render::FrameBuffer* frameBuffer );
119 * Retrieve the resource ID of the frame-buffer.
120 * @return The resource ID, or zero if not rendering off-screen.
122 Render::FrameBuffer* GetFrameBuffer();
125 * Set the value of property viewportPosition
126 * This value will persist only for the current frame.
127 * @param[in] updateBufferIndex The current update buffer index.
128 * @param[in] value The value of the property
130 void SetViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
133 * Get the value of property viewportPosition
134 * @warning Should only be called from the Update thread
135 * @param[in] bufferIndex The buffer to read from.
136 * @return the value of the property.
138 const Vector2& GetViewportPosition( BufferIndex bufferIndex ) const;
141 * Bake the value of the property viewportPosition
142 * This will also set the base value
143 * @param[in] updateBufferIndex The current update buffer index.
144 * @param[in] value The new value for property.
146 void BakeViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
149 * Set the value of property viewportSize
150 * This value will persist only for the current frame.
151 * @param[in] updateBufferIndex The current update buffer index.
152 * @param[in] value The value of the property
154 void SetViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
157 * Get the value of property viewportSize
158 * @warning Should only be called from the Update thread
159 * @param[in] bufferIndex The buffer to read from.
160 * @return the value of the property.
162 const Vector2& GetViewportSize( BufferIndex bufferIndex ) const;
165 * Bake the value of the property viewportSize
166 * This will also set the base value
167 * @param[in] updateBufferIndex The current update buffer index.
168 * @param[in] value The new value for property.
170 void BakeViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
173 * Get the value of property viewportEnabled
174 * @warning Should only be called from the Update thread
175 * @param[in] bufferIndex The buffer to read from.
176 * @return the value of the property.
178 bool GetViewportEnabled( BufferIndex bufferIndex ) const;
181 * Query whether the optional viewport is set.
182 * @param[in] bufferIndex The buffer to read from.
183 * @param[out] viewport The viewport position and size is populated.
184 * @return true if the viewport has been set
186 bool QueryViewport( BufferIndex bufferIndex, Viewport& viewport ) const;
189 * Set the value of property clearColor
190 * This value will persist only for the current frame.
191 * @param[in] updateBufferIndex The current update buffer index.
192 * @param[in] value The value of the property
194 void SetClearColor( BufferIndex updateBufferIndex, const Vector4& value );
197 * Get the value of property clearColor
198 * @warning Should only be called from the Update thread
199 * @param[in] bufferIndex The buffer to read from.
200 * @return the value of the property.
202 const Vector4& GetClearColor( BufferIndex bufferIndex ) const;
205 * Bake the value of the property clearColor
206 * This will also set the base value
207 * @param[in] updateBufferIndex The current update buffer index.
208 * @param[in] value The new value for property.
210 void BakeClearColor( BufferIndex updateBufferIndex, const Vector4& value );
213 * @copydoc Dali::RenderTask::SetClearEnabled()
215 void SetClearEnabled( bool enabled );
218 * @copydoc Dali::RenderTask::GetClearEnabled()
220 bool GetClearEnabled() const;
223 * @copydoc Dali::RenderTask::SetCullMode()
225 void SetCullMode( bool mode );
228 * @copydoc Dali::RenderTask::GetCullMode()
230 bool GetCullMode() const;
233 * Set the refresh-rate of the RenderTask.
234 * @param[in] refreshRate The new refresh rate.
236 void SetRefreshRate( uint32_t refreshRate );
239 * Retrieve the refresh-rate of the RenderTask.
240 * @return The refresh rate.
242 uint32_t GetRefreshRate() const;
245 * Check if the render task is ready for rendering.
246 * @param[in] updateBufferIndex The current update buffer index.
247 * @return True if the render-task is ready for rendering.
249 bool ReadyToRender( BufferIndex updateBufferIndex );
252 * True if a render is required. If the current state is RENDER_CONTINUOUSLY, then
253 * 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.
254 * @return true if a render is required
256 bool IsRenderRequired();
259 * Process a frame. This method is called each frame for every ready render task, regardless
260 * of whether it needs to render (so that the frame counter can be updated).
265 * Return true only if currently waiting for the render task to
266 * finish rendering and the update thread should be kept alive.
267 * @return true if waiting to be rendered
269 bool IsWaitingToRender();
272 * Return true when the render task has finished rendering and a notification
273 * needs sending. (Only one notification is sent per render once request)
274 * @return true if notification is required.
279 * @return The number of times we have transited from RENDERED_ONCE to RENDERED_ONCE_AND_NOTIFIED state.
281 uint32_t GetRenderedOnceCounter() const;
284 * Retrieve the view-matrix; this is double buffered for input handling.
285 * @pre GetCameraNode() returns a node with valid Camera.
286 * @param[in] bufferIndex The buffer to read from.
287 * @return The view-matrix.
289 const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
292 * @brief Retrieve the camera.
293 * @pre GetCameraNode() returns a node with valid Camera.
295 * @return The camera.
297 SceneGraph::Camera& GetCamera() const;
300 * Retrieve the projection-matrix; this is double buffered for input handling.
301 * @pre GetCameraNode() returns a node with valid Camera.
302 * @param[in] bufferIndex The buffer to read from.
303 * @return The projection-matrix.
305 const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
308 * Prepares the render-instruction buffer to be populated with instructions.
310 * If the render task is a render-once framebuffer backed by a native image,
311 * then this method will ensure that a GL sync object is created to track
312 * when the rendering has finished.
314 * @param[in] updateBufferIndex The current update buffer index.
315 * @return instruction to prepare
317 RenderInstruction& PrepareRenderInstruction( BufferIndex updateBufferIndex );
320 * @return true if the view matrix has been updated during this or last frame
322 bool ViewMatrixUpdated();
325 * Indicate whether GL sync is required for native render target.
326 * @param[in] requiresSync whether GL sync is required for native render target
328 void SetSyncRequired( bool requiresSync );
331 * Retrieve the render instruction.
332 * @param[in] updateBufferIndex The current update buffer index.
333 * @return The render instruction
335 RenderInstruction& GetRenderInstruction( BufferIndex updateBufferIndex )
337 return mRenderInstruction[updateBufferIndex];
340 private: // from PropertyOwner::Observer
343 * @copydoc PropertyOwner::Observer::PropertyOwnerConnected( PropertyOwner& owner )
345 void PropertyOwnerConnected( PropertyOwner& owner );
348 * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected( BufferIndex updateBufferIndex, PropertyOwner& owner )
350 void PropertyOwnerDisconnected( BufferIndex updateBufferIndex, PropertyOwner& owner );
353 * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed( PropertyOwner& owner )
355 void PropertyOwnerDestroyed( PropertyOwner& owner );
359 void SetActiveStatus();
367 RenderTask(const RenderTask&) = delete;
368 RenderTask& operator=(const RenderTask&) = delete;
370 public: // Animatable Properties
371 AnimatableProperty< Vector2 > mViewportPosition; ///< viewportPosition
372 AnimatableProperty< Vector2 > mViewportSize; ///< viewportSize
373 AnimatableProperty< Vector4 > mClearColor; ///< clearColor
376 RenderMessageDispatcher* mRenderMessageDispatcher;
377 Render::RenderTracker* mRenderSyncTracker;
380 SceneGraph::Camera* mCamera;
381 Render::FrameBuffer* mFrameBuffer;
383 RenderInstruction mRenderInstruction[2]; ///< Owned double buffered render instruction. (Double buffered because this owns render commands for the currently drawn frame)
385 uint32_t mRefreshRate; ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
386 uint32_t mFrameCounter; ///< counter for rendering every N frames
387 uint32_t mRenderedOnceCounter;///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
389 State mState; ///< Render state.
391 bool mRequiresSync:1; ///< Whether sync is needed to track the render
392 bool mActive:1; ///< True when the task is active, i.e. has valid source and camera
393 bool mWaitingToRender:1;///< True when an render once to FBO is waiting
394 bool mNotifyTrigger:1; ///< True if a render once render task has finished renderering
395 bool mExclusive:1; ///< Whether the render task has exclusive access to the source actor (node in the scene graph).
396 bool mClearEnabled:1; ///< Whether previous results are cleared.
397 bool mCullMode:1; ///< Whether renderers should be frustum culled
401 // Messages for RenderTask
402 inline void SetFrameBufferMessage( EventThreadServices& eventThreadServices, const RenderTask& task, Render::FrameBuffer* frameBuffer )
404 typedef MessageValue1< RenderTask, Render::FrameBuffer*> LocalType;
406 // Reserve some memory inside the message queue
407 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
409 // Construct message in the message queue memory; note that delete should not be called on the return value
410 new (slot) LocalType( &task, &RenderTask::SetFrameBuffer, frameBuffer );
413 inline void SetClearColorMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector4& value )
415 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
417 // Reserve some memory inside the message queue
418 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
420 // Construct message in the message queue memory; note that delete should not be called on the return value
421 new (slot) LocalType( &task, &RenderTask::SetClearColor, value );
424 inline void BakeClearColorMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector4& value )
426 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
428 // Reserve some memory inside the message queue
429 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
431 // Construct message in the message queue memory; note that delete should not be called on the return value
432 new (slot) LocalType( &task, &RenderTask::BakeClearColor, value );
435 inline void SetClearEnabledMessage( EventThreadServices& eventThreadServices, const RenderTask& task, bool enabled )
437 typedef MessageValue1< RenderTask, bool > LocalType;
439 // Reserve some memory inside the message queue
440 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
442 // Construct message in the message queue memory; note that delete should not be called on the return value
443 new (slot) LocalType( &task, &RenderTask::SetClearEnabled, enabled );
446 inline void SetCullModeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, bool mode )
448 typedef MessageValue1< RenderTask, bool > LocalType;
450 // Reserve some memory inside the message queue
451 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
453 // Construct message in the message queue memory; note that delete should not be called on the return value
454 new (slot) LocalType( &task, &RenderTask::SetCullMode, mode );
457 inline void SetRefreshRateMessage( EventThreadServices& eventThreadServices, const RenderTask& task, uint32_t refreshRate )
459 typedef MessageValue1< RenderTask, uint32_t > LocalType;
461 // Reserve some memory inside the message queue
462 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
464 // Construct message in the message queue memory; note that delete should not be called on the return value
465 new (slot) LocalType( &task, &RenderTask::SetRefreshRate, refreshRate );
468 inline void SetSourceNodeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Node* constNode )
470 // Scene graph thread can destroy this object.
471 Node* node = const_cast< Node* >( constNode );
473 typedef MessageValue1< RenderTask, Node* > LocalType;
475 // Reserve some 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::SetSourceNode, node );
482 inline void SetCameraMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Node* constNode, const Camera* constCamera )
484 typedef MessageValue2< RenderTask, Node*, Camera* > LocalType;
486 Node* node = const_cast< Node* >( constNode );
487 Camera* camera = const_cast< Camera* >( constCamera );
488 // Reserve memory inside the message queue
489 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
491 // Construct message in the message queue memory; note that delete should not be called on the return value
492 new (slot) LocalType( &task, &RenderTask::SetCamera, node, camera );
495 inline void SetExclusiveMessage( EventThreadServices& eventThreadServices, const RenderTask& task, bool exclusive )
497 typedef MessageValue1< RenderTask, bool > LocalType;
499 // Reserve some memory inside the message queue
500 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
502 // Construct message in the message queue memory; note that delete should not be called on the return value
503 new (slot) LocalType( &task, &RenderTask::SetExclusive, exclusive );
506 inline void SetSyncRequiredMessage(EventThreadServices& eventThreadServices, const RenderTask& task, bool requiresSync )
508 typedef MessageValue1< RenderTask, bool > LocalType;
510 // Reserve some memory inside the message queue
511 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
513 // Construct message in the message queue memory; note that delete should not be called on the return value
514 new (slot) LocalType( &task, &RenderTask::SetSyncRequired, requiresSync );
517 inline void BakeViewportPositionMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
519 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
521 // Reserve some memory inside the message queue
522 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
524 // Construct message in the message queue memory; note that delete should not be called on the return value
525 new (slot) LocalType( &task, &RenderTask::BakeViewportPosition, value );
528 inline void BakeViewportSizeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
530 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
532 // Reserve some memory inside the message queue
533 uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
535 // Construct message in the message queue memory; note that delete should not be called on the return value
536 new (slot) LocalType( &task, &RenderTask::BakeViewportSize, value );
539 } // namespace SceneGraph
541 } // namespace Internal
545 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H