1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__
5 * Copyright (c) 2016 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>
36 class FrameBufferTexture;
37 class ResourceManager;
48 class RenderInstruction;
49 class RenderMessageDispatcher;
52 * RenderTasks describe how the Dali scene should be rendered.
54 class RenderTask : public PropertyOwner
60 RENDER_CONTINUOUSLY, ///< mRefreshRate > 0
61 RENDER_ONCE_WAITING_FOR_RESOURCES, ///< mRefreshRate = REFRESH_ONCE
62 RENDERED_ONCE, ///< mRefreshRate = REFRESH_ONCE & rendered
63 RENDERED_ONCE_AND_NOTIFIED ///< mRefreshRate = REFRESH_ONCE & rendered & notified
67 * Create a new RenderTask
69 static RenderTask* New();
74 virtual ~RenderTask();
77 * Initialize the render task. Called in update thread
78 * @param[in] renderMessageDispatcher to send messages to render thread
79 * @param[in] resourceManager to check and update status of FBOs
81 void Initialize( RenderMessageDispatcher& renderMessageDispatcher, ResourceManager& resourceManager );
84 * Set the nodes to be rendered.
85 * @param[in] node This node and its children will be rendered.
87 void SetSourceNode( Node* node );
90 * Retrieve the source node.
91 * @return This node and its children will be rendered.
93 Node* GetSourceNode() const;
96 * Set whether the RenderTask has exclusive access to the source nodes.
97 * @param[in] exclusive True if the source nodes will only be rendered by this render-task.
99 void SetExclusive( bool exclusive );
102 * Query whether the RenderTask has exclusive access to the source actors.
103 * @return True if the source actors will only be rendered by this render-task.
105 bool IsExclusive() const;
108 * Set the camera from which the scene is viewed.
109 * @param[in] cameraNode that camera is connected with
110 * @param[in] camera to use.
112 void SetCamera( Node* cameraNode, Camera* camera );
115 * Set the frame-buffer used as a render target.
116 * @param[in] resourceId The resource ID of the frame-buffer, or zero if not rendering off-screen.
117 * @param[in] isNativeFBO if this render task is targeting a native FBO
119 void SetFrameBufferId( unsigned int resourceId, bool isNativeFBO );
122 * Retrieve the resource ID of the frame-buffer.
123 * @return The resource ID, or zero if not rendering off-screen.
125 unsigned int GetFrameBufferId() const;
128 * Set the frame-buffer used as a render target.
129 * @param[in] frameBuffer The framebuffer
131 void SetFrameBuffer( Render::FrameBuffer* frameBuffer );
134 * Retrieve the resource ID of the frame-buffer.
135 * @return The resource ID, or zero if not rendering off-screen.
137 Render::FrameBuffer* GetFrameBuffer();
140 * Set the value of property viewportPosition
141 * This value will persist only for the current frame.
142 * @param[in] updateBufferIndex The current update buffer index.
143 * @param[in] value The value of the property
145 void SetViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
148 * Get the value of property viewportPosition
149 * @warning Should only be called from the Update thread
150 * @param[in] bufferIndex The buffer to read from.
151 * @return the value of the property.
153 const Vector2& GetViewportPosition( BufferIndex bufferIndex ) const;
156 * Bake the value of the property viewportPosition
157 * This will also set the base value
158 * @param[in] updateBufferIndex The current update buffer index.
159 * @param[in] value The new value for property.
161 void BakeViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
164 * Set the value of property viewportSize
165 * This value will persist only for the current frame.
166 * @param[in] updateBufferIndex The current update buffer index.
167 * @param[in] value The value of the property
169 void SetViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
172 * Get the value of property viewportSize
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 const Vector2& GetViewportSize( BufferIndex bufferIndex ) const;
180 * Bake the value of the property viewportSize
181 * This will also set the base value
182 * @param[in] updateBufferIndex The current update buffer index.
183 * @param[in] value The new value for property.
185 void BakeViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
188 * Get the value of property viewportEnabled
189 * @warning Should only be called from the Update thread
190 * @param[in] bufferIndex The buffer to read from.
191 * @return the value of the property.
193 bool GetViewportEnabled( BufferIndex bufferIndex ) const;
196 * Query whether the optional viewport is set.
197 * @param[in] bufferIndex The buffer to read from.
198 * @param[out] viewport The viewport position and size is populated.
199 * @return true if the viewport has been set
201 bool QueryViewport( BufferIndex bufferIndex, Viewport& viewport ) const;
204 * Set the value of property clearColor
205 * This value will persist only for the current frame.
206 * @param[in] updateBufferIndex The current update buffer index.
207 * @param[in] value The value of the property
209 void SetClearColor( BufferIndex updateBufferIndex, const Vector4& value );
212 * Get the value of property clearColor
213 * @warning Should only be called from the Update thread
214 * @param[in] bufferIndex The buffer to read from.
215 * @return the value of the property.
217 const Vector4& GetClearColor( BufferIndex bufferIndex ) const;
220 * Bake the value of the property clearColor
221 * This will also set the base value
222 * @param[in] updateBufferIndex The current update buffer index.
223 * @param[in] value The new value for property.
225 void BakeClearColor( BufferIndex updateBufferIndex, const Vector4& value );
228 * @copydoc Dali::RenderTask::SetClearEnabled()
230 void SetClearEnabled( bool enabled );
233 * @copydoc Dali::RenderTask::GetClearEnabled()
235 bool GetClearEnabled() const;
238 * @copydoc Dali::RenderTask::SetCullMode()
240 void SetCullMode( bool mode );
243 * @copydoc Dali::RenderTask::GetCullMode()
245 bool GetCullMode() const;
248 * Set the refresh-rate of the RenderTask.
249 * @param[in] refreshRate The new refresh rate.
251 void SetRefreshRate( unsigned int refreshRate );
254 * Retrieve the refresh-rate of the RenderTask.
255 * @return The refresh rate.
257 unsigned int GetRefreshRate() const;
260 * Check if the render task is ready for rendering.
261 * @param[in] updateBufferIndex The current update buffer index.
262 * @return True if the render-task is ready for rendering.
264 bool ReadyToRender( BufferIndex updateBufferIndex );
267 * True if a render is required. If the current state is RENDER_CONTINUOUSLY, then
268 * 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.
269 * @return true if a render is required
271 bool IsRenderRequired();
274 * Set whether all resources were available when the render-task was processed
275 * @param[in] resourcesComplete True if the resources of the source tree are completely loaded.
277 void SetResourcesFinished( bool resourcesFinished );
280 * Process a frame. This method is called each frame for every ready render task, regardless
281 * of whether it needs to render (so that the frame counter can be updated).
286 * Return true only if currently waiting for the render task to
287 * finish rendering and the update thread should be kept alive.
288 * @return true if waiting to be rendered
290 bool IsWaitingToRender();
293 * Return true when the render task has finished rendering and a notification
294 * needs sending. (Only one notification is sent per render once request)
295 * @return true if notification is required.
300 * @return The number of times we have transited from RENDERED_ONCE to RENDERED_ONCE_AND_NOTIFIED state.
302 unsigned int GetRenderedOnceCounter() const;
305 * Retrieve the view-matrix; this is double buffered for input handling.
306 * @pre GetCameraNode() returns a node with valid Camera.
307 * @param[in] bufferIndex The buffer to read from.
308 * @return The view-matrix.
310 const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
313 * @brief Retrieve the camera.
314 * @pre GetCameraNode() returns a node with valid Camera.
316 * @return The camera.
318 SceneGraph::Camera& GetCamera() const;
321 * Retrieve the projection-matrix; this is double buffered for input handling.
322 * @pre GetCameraNode() returns a node with valid Camera.
323 * @param[in] bufferIndex The buffer to read from.
324 * @return The projection-matrix.
326 const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
329 * Prepares the render-instruction buffer to be populated with instructions.
331 * If the render task is a render-once framebuffer backed by a native image,
332 * then this method will ensure that a GL sync object is created to track
333 * when the rendering has finished.
335 * @param[out] instruction to prepare
336 * @param[in] updateBufferIndex The current update buffer index.
338 void PrepareRenderInstruction( RenderInstruction& instruction, BufferIndex updateBufferIndex );
341 * @return true if the view matrix has been updated during this or last frame
343 bool ViewMatrixUpdated();
346 * Indicate whether GL sync is required for native render target.
347 * @param[in] requiresSync whether GL sync is required for native render target
349 void SetSyncRequired( bool requiresSync );
354 * Protected constructor.
359 RenderTask(const RenderTask&);
362 RenderTask& operator=(const RenderTask&);
364 private: // PropertyOwner
366 virtual void ResetDefaultProperties( BufferIndex currentBufferIndex );
368 public: // Animatable Properties
369 AnimatableProperty< Vector2 > mViewportPosition; ///< viewportPosition
370 AnimatableProperty< Vector2 > mViewportSize; ///< viewportSize
371 AnimatableProperty< Vector4 > mClearColor; ///< clearColor
374 RenderMessageDispatcher* mRenderMessageDispatcher;
375 ResourceManager* mResourceManager;
376 Render::RenderTracker* mRenderSyncTracker;
379 SceneGraph::Camera* mCamera;
380 Render::FrameBuffer* mFrameBuffer;
382 bool mResourcesFinished:1; ///< True if all resources were available when the render-task was processed
383 bool mWaitingToRender:1; ///< True when an render once to FBO is waiting
384 bool mNotifyTrigger:1; ///< True if a render once render task has finished renderering
385 bool mExclusive: 1; ///< Whether the render task has exclusive access to the source actor (node in the scene graph implementation).
386 bool mClearEnabled: 1; ///< Whether previous results are cleared.
387 bool mCullMode: 1; ///< Whether renderers should be frustum culled
389 FrameBufferTexture* mRenderTarget;
391 State mState; ///< Render state.
392 unsigned int mRefreshRate; ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
393 unsigned int mFrameCounter; ///< counter for rendering every N frames
395 unsigned int mRenderedOnceCounter; ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
396 bool mRequiresSync; ///< Whether sync is needed to track the render
400 // Messages for RenderTask
401 inline void SetFrameBufferMessage( EventThreadServices& eventThreadServices, RenderTask& task, Render::FrameBuffer* frameBuffer )
403 typedef MessageValue1< RenderTask, Render::FrameBuffer*> LocalType;
405 // Reserve some memory inside the message queue
406 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
408 // Construct message in the message queue memory; note that delete should not be called on the return value
409 new (slot) LocalType( &task, &RenderTask::SetFrameBuffer, frameBuffer );
412 inline void SetClearColorMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Vector4& value )
414 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
416 // Reserve some memory inside the message queue
417 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
419 // Construct message in the message queue memory; note that delete should not be called on the return value
420 new (slot) LocalType( &task, &RenderTask::SetClearColor, value );
423 inline void BakeClearColorMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector4& value )
425 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
427 // Reserve some memory inside the message queue
428 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
430 // Construct message in the message queue memory; note that delete should not be called on the return value
431 new (slot) LocalType( &task, &RenderTask::BakeClearColor, value );
434 inline void SetClearEnabledMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool enabled )
436 typedef MessageValue1< RenderTask, bool > LocalType;
438 // Reserve some memory inside the message queue
439 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
441 // Construct message in the message queue memory; note that delete should not be called on the return value
442 new (slot) LocalType( &task, &RenderTask::SetClearEnabled, enabled );
445 inline void SetCullModeMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool mode )
447 typedef MessageValue1< RenderTask, bool > LocalType;
449 // Reserve some memory inside the message queue
450 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
452 // Construct message in the message queue memory; note that delete should not be called on the return value
453 new (slot) LocalType( &task, &RenderTask::SetCullMode, mode );
456 inline void SetRefreshRateMessage( EventThreadServices& eventThreadServices, RenderTask& task, unsigned int refreshRate )
458 typedef MessageValue1< RenderTask, unsigned int > LocalType;
460 // Reserve some memory inside the message queue
461 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
463 // Construct message in the message queue memory; note that delete should not be called on the return value
464 new (slot) LocalType( &task, &RenderTask::SetRefreshRate, refreshRate );
467 inline void SetSourceNodeMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode )
469 // Scene graph thread can destroy this object.
470 Node* node = const_cast< Node* >( constNode );
472 typedef MessageValue1< RenderTask, Node* > LocalType;
474 // Reserve some 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::SetSourceNode, node );
481 inline void SetCameraMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode, const Camera* constCamera )
483 typedef MessageValue2< RenderTask, Node*, Camera* > LocalType;
485 Node* node = const_cast< Node* >( constNode );
486 Camera* camera = const_cast< Camera* >( constCamera );
487 // Reserve memory inside the message queue
488 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
490 // Construct message in the message queue memory; note that delete should not be called on the return value
491 new (slot) LocalType( &task, &RenderTask::SetCamera, node, camera );
494 inline void SetExclusiveMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool exclusive )
496 typedef MessageValue1< RenderTask, bool > LocalType;
498 // Reserve some memory inside the message queue
499 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
501 // Construct message in the message queue memory; note that delete should not be called on the return value
502 new (slot) LocalType( &task, &RenderTask::SetExclusive, exclusive );
505 inline void SetSyncRequiredMessage(EventThreadServices& eventThreadServices, RenderTask& task, bool requiresSync )
507 typedef MessageValue1< RenderTask, bool > LocalType;
509 // Reserve some memory inside the message queue
510 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
512 // Construct message in the message queue memory; note that delete should not be called on the return value
513 new (slot) LocalType( &task, &RenderTask::SetSyncRequired, requiresSync );
516 inline void BakeViewportPositionMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
518 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
520 // Reserve some memory inside the message queue
521 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
523 // Construct message in the message queue memory; note that delete should not be called on the return value
524 new (slot) LocalType( &task, &RenderTask::BakeViewportPosition, value );
527 inline void BakeViewportSizeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
529 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
531 // Reserve some memory inside the message queue
532 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
534 // Construct message in the message queue memory; note that delete should not be called on the return value
535 new (slot) LocalType( &task, &RenderTask::BakeViewportSize, value );
538 } // namespace SceneGraph
540 } // namespace Internal
544 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__