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 unsigned int mFrameBufferResourceId;
381 Render::FrameBuffer* mFrameBuffer;
383 bool mResourcesFinished:1; ///< True if all resources were available when the render-task was processed
384 bool mWaitingToRender:1; ///< True when an render once to FBO is waiting
385 bool mNotifyTrigger:1; ///< True if a render once render task has finished renderering
386 bool mExclusive: 1; ///< Whether the render task has exclusive access to the source actor (node in the scene graph implementation).
387 bool mClearEnabled: 1; ///< Whether previous results are cleared.
388 bool mCullMode: 1; ///< Whether renderers should be frustum culled
390 FrameBufferTexture* mRenderTarget;
393 State mState; ///< Render state.
394 unsigned int mRefreshRate; ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
395 unsigned int mFrameCounter; ///< counter for rendering every N frames
397 unsigned int mRenderedOnceCounter; ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
398 bool mTargetIsNativeFramebuffer; ///< Tells if our target is a native framebuffer
399 bool mRequiresSync; ///< Whether sync is needed to track the render
403 // Messages for RenderTask
405 inline void SetFrameBufferIdMessage( EventThreadServices& eventThreadServices, RenderTask& task, unsigned int resourceId, bool isNativeFBO )
407 typedef MessageValue2< RenderTask, unsigned int, 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::SetFrameBufferId, resourceId, isNativeFBO );
416 inline void SetFrameBufferMessage( EventThreadServices& eventThreadServices, RenderTask& task, Render::FrameBuffer* frameBuffer )
418 typedef MessageValue1< RenderTask, Render::FrameBuffer*> 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::SetFrameBuffer, frameBuffer );
427 inline void SetClearColorMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Vector4& value )
429 typedef MessageDoubleBuffered1< RenderTask, Vector4 > 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::SetClearColor, value );
438 inline void BakeClearColorMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector4& value )
440 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
442 // Reserve some memory inside the message queue
443 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
445 // Construct message in the message queue memory; note that delete should not be called on the return value
446 new (slot) LocalType( &task, &RenderTask::BakeClearColor, value );
449 inline void SetClearEnabledMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool enabled )
451 typedef MessageValue1< RenderTask, bool > LocalType;
453 // Reserve some memory inside the message queue
454 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
456 // Construct message in the message queue memory; note that delete should not be called on the return value
457 new (slot) LocalType( &task, &RenderTask::SetClearEnabled, enabled );
460 inline void SetCullModeMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool mode )
462 typedef MessageValue1< RenderTask, bool > LocalType;
464 // Reserve some memory inside the message queue
465 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
467 // Construct message in the message queue memory; note that delete should not be called on the return value
468 new (slot) LocalType( &task, &RenderTask::SetCullMode, mode );
471 inline void SetRefreshRateMessage( EventThreadServices& eventThreadServices, RenderTask& task, unsigned int refreshRate )
473 typedef MessageValue1< RenderTask, unsigned int > LocalType;
475 // Reserve some memory inside the message queue
476 unsigned int* 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::SetRefreshRate, refreshRate );
482 inline void SetSourceNodeMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode )
484 // Scene graph thread can destroy this object.
485 Node* node = const_cast< Node* >( constNode );
487 typedef MessageValue1< RenderTask, Node* > LocalType;
489 // Reserve some memory inside the message queue
490 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
492 // Construct message in the message queue memory; note that delete should not be called on the return value
493 new (slot) LocalType( &task, &RenderTask::SetSourceNode, node );
496 inline void SetCameraMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode, const Camera* constCamera )
498 typedef MessageValue2< RenderTask, Node*, Camera* > LocalType;
500 Node* node = const_cast< Node* >( constNode );
501 Camera* camera = const_cast< Camera* >( constCamera );
502 // Reserve 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::SetCamera, node, camera );
509 inline void SetExclusiveMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool exclusive )
511 typedef MessageValue1< RenderTask, bool > LocalType;
513 // Reserve some memory inside the message queue
514 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
516 // Construct message in the message queue memory; note that delete should not be called on the return value
517 new (slot) LocalType( &task, &RenderTask::SetExclusive, exclusive );
520 inline void SetSyncRequiredMessage(EventThreadServices& eventThreadServices, RenderTask& task, bool requiresSync )
522 typedef MessageValue1< RenderTask, bool > LocalType;
524 // Reserve some memory inside the message queue
525 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
527 // Construct message in the message queue memory; note that delete should not be called on the return value
528 new (slot) LocalType( &task, &RenderTask::SetSyncRequired, requiresSync );
531 inline void BakeViewportPositionMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
533 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
535 // Reserve some memory inside the message queue
536 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
538 // Construct message in the message queue memory; note that delete should not be called on the return value
539 new (slot) LocalType( &task, &RenderTask::BakeViewportPosition, value );
542 inline void BakeViewportSizeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
544 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
546 // Reserve some memory inside the message queue
547 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
549 // Construct message in the message queue memory; note that delete should not be called on the return value
550 new (slot) LocalType( &task, &RenderTask::BakeViewportSize, value );
553 } // namespace SceneGraph
555 } // namespace Internal
559 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__