1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__
5 * Copyright (c) 2017 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] resourceId The resource ID of the frame-buffer, or zero if not rendering off-screen.
114 * @param[in] isNativeFBO if this render task is targeting a native FBO
116 void SetFrameBufferId( unsigned int resourceId, bool isNativeFBO );
119 * Retrieve the resource ID of the frame-buffer.
120 * @return The resource ID, or zero if not rendering off-screen.
122 unsigned int GetFrameBufferId() const;
125 * Set the frame-buffer used as a render target.
126 * @param[in] frameBuffer The framebuffer
128 void SetFrameBuffer( Render::FrameBuffer* frameBuffer );
131 * Retrieve the resource ID of the frame-buffer.
132 * @return The resource ID, or zero if not rendering off-screen.
134 Render::FrameBuffer* GetFrameBuffer();
137 * Set the value of property viewportPosition
138 * This value will persist only for the current frame.
139 * @param[in] updateBufferIndex The current update buffer index.
140 * @param[in] value The value of the property
142 void SetViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
145 * Get the value of property viewportPosition
146 * @warning Should only be called from the Update thread
147 * @param[in] bufferIndex The buffer to read from.
148 * @return the value of the property.
150 const Vector2& GetViewportPosition( BufferIndex bufferIndex ) const;
153 * Bake the value of the property viewportPosition
154 * This will also set the base value
155 * @param[in] updateBufferIndex The current update buffer index.
156 * @param[in] value The new value for property.
158 void BakeViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
161 * Set the value of property viewportSize
162 * This value will persist only for the current frame.
163 * @param[in] updateBufferIndex The current update buffer index.
164 * @param[in] value The value of the property
166 void SetViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
169 * Get the value of property viewportSize
170 * @warning Should only be called from the Update thread
171 * @param[in] bufferIndex The buffer to read from.
172 * @return the value of the property.
174 const Vector2& GetViewportSize( BufferIndex bufferIndex ) const;
177 * Bake the value of the property viewportSize
178 * This will also set the base value
179 * @param[in] updateBufferIndex The current update buffer index.
180 * @param[in] value The new value for property.
182 void BakeViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
185 * Get the value of property viewportEnabled
186 * @warning Should only be called from the Update thread
187 * @param[in] bufferIndex The buffer to read from.
188 * @return the value of the property.
190 bool GetViewportEnabled( BufferIndex bufferIndex ) const;
193 * Query whether the optional viewport is set.
194 * @param[in] bufferIndex The buffer to read from.
195 * @param[out] viewport The viewport position and size is populated.
196 * @return true if the viewport has been set
198 bool QueryViewport( BufferIndex bufferIndex, Viewport& viewport ) const;
201 * Set the value of property clearColor
202 * This value will persist only for the current frame.
203 * @param[in] updateBufferIndex The current update buffer index.
204 * @param[in] value The value of the property
206 void SetClearColor( BufferIndex updateBufferIndex, const Vector4& value );
209 * Get the value of property clearColor
210 * @warning Should only be called from the Update thread
211 * @param[in] bufferIndex The buffer to read from.
212 * @return the value of the property.
214 const Vector4& GetClearColor( BufferIndex bufferIndex ) const;
217 * Bake the value of the property clearColor
218 * This will also set the base value
219 * @param[in] updateBufferIndex The current update buffer index.
220 * @param[in] value The new value for property.
222 void BakeClearColor( BufferIndex updateBufferIndex, const Vector4& value );
225 * @copydoc Dali::RenderTask::SetClearEnabled()
227 void SetClearEnabled( bool enabled );
230 * @copydoc Dali::RenderTask::GetClearEnabled()
232 bool GetClearEnabled() const;
235 * @copydoc Dali::RenderTask::SetCullMode()
237 void SetCullMode( bool mode );
240 * @copydoc Dali::RenderTask::GetCullMode()
242 bool GetCullMode() const;
245 * Set the refresh-rate of the RenderTask.
246 * @param[in] refreshRate The new refresh rate.
248 void SetRefreshRate( unsigned int refreshRate );
251 * Retrieve the refresh-rate of the RenderTask.
252 * @return The refresh rate.
254 unsigned int GetRefreshRate() const;
257 * Check if the render task is ready for rendering.
258 * @param[in] updateBufferIndex The current update buffer index.
259 * @return True if the render-task is ready for rendering.
261 bool ReadyToRender( BufferIndex updateBufferIndex );
264 * True if a render is required. If the current state is RENDER_CONTINUOUSLY, then
265 * 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.
266 * @return true if a render is required
268 bool IsRenderRequired();
271 * Process a frame. This method is called each frame for every ready render task, regardless
272 * of whether it needs to render (so that the frame counter can be updated).
277 * Return true only if currently waiting for the render task to
278 * finish rendering and the update thread should be kept alive.
279 * @return true if waiting to be rendered
281 bool IsWaitingToRender();
284 * Return true when the render task has finished rendering and a notification
285 * needs sending. (Only one notification is sent per render once request)
286 * @return true if notification is required.
291 * @return The number of times we have transited from RENDERED_ONCE to RENDERED_ONCE_AND_NOTIFIED state.
293 unsigned int GetRenderedOnceCounter() const;
296 * Retrieve the view-matrix; this is double buffered for input handling.
297 * @pre GetCameraNode() returns a node with valid Camera.
298 * @param[in] bufferIndex The buffer to read from.
299 * @return The view-matrix.
301 const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
304 * @brief Retrieve the camera.
305 * @pre GetCameraNode() returns a node with valid Camera.
307 * @return The camera.
309 SceneGraph::Camera& GetCamera() const;
312 * Retrieve the projection-matrix; this is double buffered for input handling.
313 * @pre GetCameraNode() returns a node with valid Camera.
314 * @param[in] bufferIndex The buffer to read from.
315 * @return The projection-matrix.
317 const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
320 * Prepares the render-instruction buffer to be populated with instructions.
322 * If the render task is a render-once framebuffer backed by a native image,
323 * then this method will ensure that a GL sync object is created to track
324 * when the rendering has finished.
326 * @param[out] instruction to prepare
327 * @param[in] updateBufferIndex The current update buffer index.
329 void PrepareRenderInstruction( RenderInstruction& instruction, BufferIndex updateBufferIndex );
332 * @return true if the view matrix has been updated during this or last frame
334 bool ViewMatrixUpdated();
337 * Indicate whether GL sync is required for native render target.
338 * @param[in] requiresSync whether GL sync is required for native render target
340 void SetSyncRequired( bool requiresSync );
345 * Protected constructor.
350 RenderTask(const RenderTask&);
353 RenderTask& operator=(const RenderTask&);
355 private: // PropertyOwner
357 virtual void ResetDefaultProperties( BufferIndex currentBufferIndex );
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 bool mWaitingToRender:1; ///< True when an render once to FBO is waiting
373 bool mNotifyTrigger:1; ///< True if a render once render task has finished renderering
374 bool mExclusive: 1; ///< Whether the render task has exclusive access to the source actor (node in the scene graph implementation).
375 bool mClearEnabled: 1; ///< Whether previous results are cleared.
376 bool mCullMode: 1; ///< Whether renderers should be frustum culled
378 State mState; ///< Render state.
379 unsigned int mRefreshRate; ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
380 unsigned int mFrameCounter; ///< counter for rendering every N frames
382 unsigned int mRenderedOnceCounter; ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
383 bool mRequiresSync; ///< Whether sync is needed to track the render
387 // Messages for RenderTask
388 inline void SetFrameBufferMessage( EventThreadServices& eventThreadServices, RenderTask& task, Render::FrameBuffer* frameBuffer )
390 typedef MessageValue1< RenderTask, Render::FrameBuffer*> LocalType;
392 // Reserve some memory inside the message queue
393 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
395 // Construct message in the message queue memory; note that delete should not be called on the return value
396 new (slot) LocalType( &task, &RenderTask::SetFrameBuffer, frameBuffer );
399 inline void SetClearColorMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Vector4& value )
401 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
403 // Reserve some memory inside the message queue
404 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
406 // Construct message in the message queue memory; note that delete should not be called on the return value
407 new (slot) LocalType( &task, &RenderTask::SetClearColor, value );
410 inline void BakeClearColorMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector4& value )
412 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
414 // Reserve some memory inside the message queue
415 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
417 // Construct message in the message queue memory; note that delete should not be called on the return value
418 new (slot) LocalType( &task, &RenderTask::BakeClearColor, value );
421 inline void SetClearEnabledMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool enabled )
423 typedef MessageValue1< RenderTask, bool > LocalType;
425 // Reserve some memory inside the message queue
426 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
428 // Construct message in the message queue memory; note that delete should not be called on the return value
429 new (slot) LocalType( &task, &RenderTask::SetClearEnabled, enabled );
432 inline void SetCullModeMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool mode )
434 typedef MessageValue1< RenderTask, bool > LocalType;
436 // Reserve some memory inside the message queue
437 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
439 // Construct message in the message queue memory; note that delete should not be called on the return value
440 new (slot) LocalType( &task, &RenderTask::SetCullMode, mode );
443 inline void SetRefreshRateMessage( EventThreadServices& eventThreadServices, RenderTask& task, unsigned int refreshRate )
445 typedef MessageValue1< RenderTask, unsigned int > LocalType;
447 // Reserve some memory inside the message queue
448 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
450 // Construct message in the message queue memory; note that delete should not be called on the return value
451 new (slot) LocalType( &task, &RenderTask::SetRefreshRate, refreshRate );
454 inline void SetSourceNodeMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode )
456 // Scene graph thread can destroy this object.
457 Node* node = const_cast< Node* >( constNode );
459 typedef MessageValue1< RenderTask, Node* > LocalType;
461 // Reserve some memory inside the message queue
462 unsigned int* 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::SetSourceNode, node );
468 inline void SetCameraMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode, const Camera* constCamera )
470 typedef MessageValue2< RenderTask, Node*, Camera* > LocalType;
472 Node* node = const_cast< Node* >( constNode );
473 Camera* camera = const_cast< Camera* >( constCamera );
474 // Reserve memory inside the message queue
475 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
477 // Construct message in the message queue memory; note that delete should not be called on the return value
478 new (slot) LocalType( &task, &RenderTask::SetCamera, node, camera );
481 inline void SetExclusiveMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool exclusive )
483 typedef MessageValue1< RenderTask, bool > LocalType;
485 // Reserve some memory inside the message queue
486 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
488 // Construct message in the message queue memory; note that delete should not be called on the return value
489 new (slot) LocalType( &task, &RenderTask::SetExclusive, exclusive );
492 inline void SetSyncRequiredMessage(EventThreadServices& eventThreadServices, RenderTask& task, bool requiresSync )
494 typedef MessageValue1< RenderTask, bool > LocalType;
496 // Reserve some memory inside the message queue
497 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
499 // Construct message in the message queue memory; note that delete should not be called on the return value
500 new (slot) LocalType( &task, &RenderTask::SetSyncRequired, requiresSync );
503 inline void BakeViewportPositionMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
505 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
507 // Reserve some memory inside the message queue
508 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
510 // Construct message in the message queue memory; note that delete should not be called on the return value
511 new (slot) LocalType( &task, &RenderTask::BakeViewportPosition, value );
514 inline void BakeViewportSizeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
516 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
518 // Reserve some memory inside the message queue
519 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
521 // Construct message in the message queue memory; note that delete should not be called on the return value
522 new (slot) LocalType( &task, &RenderTask::BakeViewportSize, value );
525 } // namespace SceneGraph
527 } // namespace Internal
531 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__