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] 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&);
342 private: // PropertyOwner
344 virtual void ResetDefaultProperties( BufferIndex currentBufferIndex );
346 public: // Animatable Properties
347 AnimatableProperty< Vector2 > mViewportPosition; ///< viewportPosition
348 AnimatableProperty< Vector2 > mViewportSize; ///< viewportSize
349 AnimatableProperty< Vector4 > mClearColor; ///< clearColor
352 RenderMessageDispatcher* mRenderMessageDispatcher;
353 Render::RenderTracker* mRenderSyncTracker;
356 SceneGraph::Camera* mCamera;
357 Render::FrameBuffer* mFrameBuffer;
359 bool mWaitingToRender:1; ///< True when an render once to FBO is waiting
360 bool mNotifyTrigger:1; ///< True if a render once render task has finished renderering
361 bool mExclusive: 1; ///< Whether the render task has exclusive access to the source actor (node in the scene graph implementation).
362 bool mClearEnabled: 1; ///< Whether previous results are cleared.
363 bool mCullMode: 1; ///< Whether renderers should be frustum culled
365 State mState; ///< Render state.
366 unsigned int mRefreshRate; ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
367 unsigned int mFrameCounter; ///< counter for rendering every N frames
369 unsigned int mRenderedOnceCounter; ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
370 bool mRequiresSync; ///< Whether sync is needed to track the render
374 // Messages for RenderTask
375 inline void SetFrameBufferMessage( EventThreadServices& eventThreadServices, RenderTask& task, Render::FrameBuffer* frameBuffer )
377 typedef MessageValue1< RenderTask, Render::FrameBuffer*> LocalType;
379 // Reserve some memory inside the message queue
380 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
382 // Construct message in the message queue memory; note that delete should not be called on the return value
383 new (slot) LocalType( &task, &RenderTask::SetFrameBuffer, frameBuffer );
386 inline void SetClearColorMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Vector4& value )
388 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
390 // Reserve some memory inside the message queue
391 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
393 // Construct message in the message queue memory; note that delete should not be called on the return value
394 new (slot) LocalType( &task, &RenderTask::SetClearColor, value );
397 inline void BakeClearColorMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector4& value )
399 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
401 // Reserve some memory inside the message queue
402 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
404 // Construct message in the message queue memory; note that delete should not be called on the return value
405 new (slot) LocalType( &task, &RenderTask::BakeClearColor, value );
408 inline void SetClearEnabledMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool enabled )
410 typedef MessageValue1< RenderTask, bool > LocalType;
412 // Reserve some memory inside the message queue
413 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
415 // Construct message in the message queue memory; note that delete should not be called on the return value
416 new (slot) LocalType( &task, &RenderTask::SetClearEnabled, enabled );
419 inline void SetCullModeMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool mode )
421 typedef MessageValue1< RenderTask, bool > LocalType;
423 // Reserve some memory inside the message queue
424 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
426 // Construct message in the message queue memory; note that delete should not be called on the return value
427 new (slot) LocalType( &task, &RenderTask::SetCullMode, mode );
430 inline void SetRefreshRateMessage( EventThreadServices& eventThreadServices, RenderTask& task, unsigned int refreshRate )
432 typedef MessageValue1< RenderTask, unsigned int > LocalType;
434 // Reserve some memory inside the message queue
435 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
437 // Construct message in the message queue memory; note that delete should not be called on the return value
438 new (slot) LocalType( &task, &RenderTask::SetRefreshRate, refreshRate );
441 inline void SetSourceNodeMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode )
443 // Scene graph thread can destroy this object.
444 Node* node = const_cast< Node* >( constNode );
446 typedef MessageValue1< RenderTask, Node* > LocalType;
448 // Reserve some memory inside the message queue
449 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
451 // Construct message in the message queue memory; note that delete should not be called on the return value
452 new (slot) LocalType( &task, &RenderTask::SetSourceNode, node );
455 inline void SetCameraMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode, const Camera* constCamera )
457 typedef MessageValue2< RenderTask, Node*, Camera* > LocalType;
459 Node* node = const_cast< Node* >( constNode );
460 Camera* camera = const_cast< Camera* >( constCamera );
461 // Reserve 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::SetCamera, node, camera );
468 inline void SetExclusiveMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool exclusive )
470 typedef MessageValue1< RenderTask, bool > LocalType;
472 // Reserve some memory inside the message queue
473 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
475 // Construct message in the message queue memory; note that delete should not be called on the return value
476 new (slot) LocalType( &task, &RenderTask::SetExclusive, exclusive );
479 inline void SetSyncRequiredMessage(EventThreadServices& eventThreadServices, RenderTask& task, bool requiresSync )
481 typedef MessageValue1< RenderTask, bool > LocalType;
483 // Reserve some memory inside the message queue
484 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
486 // Construct message in the message queue memory; note that delete should not be called on the return value
487 new (slot) LocalType( &task, &RenderTask::SetSyncRequired, requiresSync );
490 inline void BakeViewportPositionMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
492 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
494 // Reserve some memory inside the message queue
495 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
497 // Construct message in the message queue memory; note that delete should not be called on the return value
498 new (slot) LocalType( &task, &RenderTask::BakeViewportPosition, value );
501 inline void BakeViewportSizeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
503 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
505 // Reserve some memory inside the message queue
506 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
508 // Construct message in the message queue memory; note that delete should not be called on the return value
509 new (slot) LocalType( &task, &RenderTask::BakeViewportSize, value );
512 } // namespace SceneGraph
514 } // namespace Internal
518 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H