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
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( unsigned int refreshRate );
238 * Retrieve the refresh-rate of the RenderTask.
239 * @return The refresh rate.
241 unsigned int 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 unsigned int 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 );
332 * Protected constructor.
337 RenderTask(const RenderTask&);
340 RenderTask& operator=(const RenderTask&);
343 public: // Animatable Properties
344 AnimatableProperty< Vector2 > mViewportPosition; ///< viewportPosition
345 AnimatableProperty< Vector2 > mViewportSize; ///< viewportSize
346 AnimatableProperty< Vector4 > mClearColor; ///< clearColor
349 RenderMessageDispatcher* mRenderMessageDispatcher;
350 Render::RenderTracker* mRenderSyncTracker;
353 SceneGraph::Camera* mCamera;
354 Render::FrameBuffer* mFrameBuffer;
356 bool mWaitingToRender:1; ///< True when an render once to FBO is waiting
357 bool mNotifyTrigger:1; ///< True if a render once render task has finished renderering
358 bool mExclusive: 1; ///< Whether the render task has exclusive access to the source actor (node in the scene graph implementation).
359 bool mClearEnabled: 1; ///< Whether previous results are cleared.
360 bool mCullMode: 1; ///< Whether renderers should be frustum culled
362 State mState; ///< Render state.
363 unsigned int mRefreshRate; ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
364 unsigned int mFrameCounter; ///< counter for rendering every N frames
366 unsigned int mRenderedOnceCounter; ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
367 bool mRequiresSync; ///< Whether sync is needed to track the render
371 // Messages for RenderTask
372 inline void SetFrameBufferMessage( EventThreadServices& eventThreadServices, RenderTask& task, Render::FrameBuffer* frameBuffer )
374 typedef MessageValue1< RenderTask, Render::FrameBuffer*> LocalType;
376 // Reserve some memory inside the message queue
377 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
379 // Construct message in the message queue memory; note that delete should not be called on the return value
380 new (slot) LocalType( &task, &RenderTask::SetFrameBuffer, frameBuffer );
383 inline void SetClearColorMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Vector4& value )
385 typedef MessageDoubleBuffered1< RenderTask, Vector4 > 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::SetClearColor, value );
394 inline void BakeClearColorMessage( EventThreadServices& eventThreadServices, const 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::BakeClearColor, value );
405 inline void SetClearEnabledMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool enabled )
407 typedef MessageValue1< RenderTask, bool > 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::SetClearEnabled, enabled );
416 inline void SetCullModeMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool mode )
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::SetCullMode, mode );
427 inline void SetRefreshRateMessage( EventThreadServices& eventThreadServices, RenderTask& task, unsigned int refreshRate )
429 typedef MessageValue1< RenderTask, unsigned int > 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::SetRefreshRate, refreshRate );
438 inline void SetSourceNodeMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode )
440 // Scene graph thread can destroy this object.
441 Node* node = const_cast< Node* >( constNode );
443 typedef MessageValue1< RenderTask, Node* > LocalType;
445 // Reserve some memory inside the message queue
446 unsigned int* slot = eventThreadServices.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::SetSourceNode, node );
452 inline void SetCameraMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode, const Camera* constCamera )
454 typedef MessageValue2< RenderTask, Node*, Camera* > LocalType;
456 Node* node = const_cast< Node* >( constNode );
457 Camera* camera = const_cast< Camera* >( constCamera );
458 // Reserve memory inside the message queue
459 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
461 // Construct message in the message queue memory; note that delete should not be called on the return value
462 new (slot) LocalType( &task, &RenderTask::SetCamera, node, camera );
465 inline void SetExclusiveMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool exclusive )
467 typedef MessageValue1< RenderTask, bool > LocalType;
469 // Reserve some memory inside the message queue
470 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
472 // Construct message in the message queue memory; note that delete should not be called on the return value
473 new (slot) LocalType( &task, &RenderTask::SetExclusive, exclusive );
476 inline void SetSyncRequiredMessage(EventThreadServices& eventThreadServices, RenderTask& task, bool requiresSync )
478 typedef MessageValue1< RenderTask, bool > LocalType;
480 // Reserve some memory inside the message queue
481 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
483 // Construct message in the message queue memory; note that delete should not be called on the return value
484 new (slot) LocalType( &task, &RenderTask::SetSyncRequired, requiresSync );
487 inline void BakeViewportPositionMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
489 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
491 // Reserve some memory inside the message queue
492 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
494 // Construct message in the message queue memory; note that delete should not be called on the return value
495 new (slot) LocalType( &task, &RenderTask::BakeViewportPosition, value );
498 inline void BakeViewportSizeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
500 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
502 // Reserve some memory inside the message queue
503 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
505 // Construct message in the message queue memory; note that delete should not be called on the return value
506 new (slot) LocalType( &task, &RenderTask::BakeViewportSize, value );
509 } // namespace SceneGraph
511 } // namespace Internal
515 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H