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>
36 class FrameBufferTexture;
47 class RenderInstruction;
48 class RenderMessageDispatcher;
51 * RenderTasks describe how the Dali scene should be rendered.
53 class RenderTask : public PropertyOwner
59 RENDER_CONTINUOUSLY, ///< mRefreshRate > 0
60 RENDER_ONCE_WAITING_FOR_RESOURCES, ///< mRefreshRate = REFRESH_ONCE
61 RENDERED_ONCE, ///< mRefreshRate = REFRESH_ONCE & rendered
62 RENDERED_ONCE_AND_NOTIFIED ///< mRefreshRate = REFRESH_ONCE & rendered & notified
66 * Create a new RenderTask
68 static RenderTask* New();
73 virtual ~RenderTask();
76 * Initialize the render task. Called in update thread
77 * @param[in] renderMessageDispatcher to send messages to render thread
79 void Initialize( RenderMessageDispatcher& renderMessageDispatcher );
82 * Set the nodes to be rendered.
83 * @param[in] node This node and its children will be rendered.
85 void SetSourceNode( Node* node );
88 * Retrieve the source node.
89 * @return This node and its children will be rendered.
91 Node* GetSourceNode() const;
94 * Set whether the RenderTask has exclusive access to the source nodes.
95 * @param[in] exclusive True if the source nodes will only be rendered by this render-task.
97 void SetExclusive( bool exclusive );
100 * Query whether the RenderTask has exclusive access to the source actors.
101 * @return True if the source actors will only be rendered by this render-task.
103 bool IsExclusive() const;
106 * Set the camera from which the scene is viewed.
107 * @param[in] cameraNode that camera is connected with
108 * @param[in] camera to use.
110 void SetCamera( Node* cameraNode, Camera* camera );
113 * Set the frame-buffer used as a render target.
114 * @param[in] resourceId The resource ID of the frame-buffer, or zero if not rendering off-screen.
115 * @param[in] isNativeFBO if this render task is targeting a native FBO
117 void SetFrameBufferId( unsigned int resourceId, bool isNativeFBO );
120 * Retrieve the resource ID of the frame-buffer.
121 * @return The resource ID, or zero if not rendering off-screen.
123 unsigned int GetFrameBufferId() const;
126 * Set the frame-buffer used as a render target.
127 * @param[in] frameBuffer The framebuffer
129 void SetFrameBuffer( Render::FrameBuffer* frameBuffer );
132 * Retrieve the resource ID of the frame-buffer.
133 * @return The resource ID, or zero if not rendering off-screen.
135 Render::FrameBuffer* GetFrameBuffer();
138 * Set the value of property viewportPosition
139 * This value will persist only for the current frame.
140 * @param[in] updateBufferIndex The current update buffer index.
141 * @param[in] value The value of the property
143 void SetViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
146 * Get the value of property viewportPosition
147 * @warning Should only be called from the Update thread
148 * @param[in] bufferIndex The buffer to read from.
149 * @return the value of the property.
151 const Vector2& GetViewportPosition( BufferIndex bufferIndex ) const;
154 * Bake the value of the property viewportPosition
155 * This will also set the base value
156 * @param[in] updateBufferIndex The current update buffer index.
157 * @param[in] value The new value for property.
159 void BakeViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
162 * Set the value of property viewportSize
163 * This value will persist only for the current frame.
164 * @param[in] updateBufferIndex The current update buffer index.
165 * @param[in] value The value of the property
167 void SetViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
170 * Get the value of property viewportSize
171 * @warning Should only be called from the Update thread
172 * @param[in] bufferIndex The buffer to read from.
173 * @return the value of the property.
175 const Vector2& GetViewportSize( BufferIndex bufferIndex ) const;
178 * Bake the value of the property viewportSize
179 * This will also set the base value
180 * @param[in] updateBufferIndex The current update buffer index.
181 * @param[in] value The new value for property.
183 void BakeViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
186 * Get the value of property viewportEnabled
187 * @warning Should only be called from the Update thread
188 * @param[in] bufferIndex The buffer to read from.
189 * @return the value of the property.
191 bool GetViewportEnabled( BufferIndex bufferIndex ) const;
194 * Query whether the optional viewport is set.
195 * @param[in] bufferIndex The buffer to read from.
196 * @param[out] viewport The viewport position and size is populated.
197 * @return true if the viewport has been set
199 bool QueryViewport( BufferIndex bufferIndex, Viewport& viewport ) const;
202 * Set the value of property clearColor
203 * This value will persist only for the current frame.
204 * @param[in] updateBufferIndex The current update buffer index.
205 * @param[in] value The value of the property
207 void SetClearColor( BufferIndex updateBufferIndex, const Vector4& value );
210 * Get the value of property clearColor
211 * @warning Should only be called from the Update thread
212 * @param[in] bufferIndex The buffer to read from.
213 * @return the value of the property.
215 const Vector4& GetClearColor( BufferIndex bufferIndex ) const;
218 * Bake the value of the property clearColor
219 * This will also set the base value
220 * @param[in] updateBufferIndex The current update buffer index.
221 * @param[in] value The new value for property.
223 void BakeClearColor( BufferIndex updateBufferIndex, const Vector4& value );
226 * @copydoc Dali::RenderTask::SetClearEnabled()
228 void SetClearEnabled( bool enabled );
231 * @copydoc Dali::RenderTask::GetClearEnabled()
233 bool GetClearEnabled() const;
236 * @copydoc Dali::RenderTask::SetCullMode()
238 void SetCullMode( bool mode );
241 * @copydoc Dali::RenderTask::GetCullMode()
243 bool GetCullMode() const;
246 * Set the refresh-rate of the RenderTask.
247 * @param[in] refreshRate The new refresh rate.
249 void SetRefreshRate( unsigned int refreshRate );
252 * Retrieve the refresh-rate of the RenderTask.
253 * @return The refresh rate.
255 unsigned int GetRefreshRate() const;
258 * Check if the render task is ready for rendering.
259 * @param[in] updateBufferIndex The current update buffer index.
260 * @return True if the render-task is ready for rendering.
262 bool ReadyToRender( BufferIndex updateBufferIndex );
265 * True if a render is required. If the current state is RENDER_CONTINUOUSLY, then
266 * 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.
267 * @return true if a render is required
269 bool IsRenderRequired();
272 * Process a frame. This method is called each frame for every ready render task, regardless
273 * of whether it needs to render (so that the frame counter can be updated).
278 * Return true only if currently waiting for the render task to
279 * finish rendering and the update thread should be kept alive.
280 * @return true if waiting to be rendered
282 bool IsWaitingToRender();
285 * Return true when the render task has finished rendering and a notification
286 * needs sending. (Only one notification is sent per render once request)
287 * @return true if notification is required.
292 * @return The number of times we have transited from RENDERED_ONCE to RENDERED_ONCE_AND_NOTIFIED state.
294 unsigned int GetRenderedOnceCounter() const;
297 * Retrieve the view-matrix; this is double buffered for input handling.
298 * @pre GetCameraNode() returns a node with valid Camera.
299 * @param[in] bufferIndex The buffer to read from.
300 * @return The view-matrix.
302 const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
305 * @brief Retrieve the camera.
306 * @pre GetCameraNode() returns a node with valid Camera.
308 * @return The camera.
310 SceneGraph::Camera& GetCamera() const;
313 * Retrieve the projection-matrix; this is double buffered for input handling.
314 * @pre GetCameraNode() returns a node with valid Camera.
315 * @param[in] bufferIndex The buffer to read from.
316 * @return The projection-matrix.
318 const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
321 * Prepares the render-instruction buffer to be populated with instructions.
323 * If the render task is a render-once framebuffer backed by a native image,
324 * then this method will ensure that a GL sync object is created to track
325 * when the rendering has finished.
327 * @param[out] instruction to prepare
328 * @param[in] updateBufferIndex The current update buffer index.
330 void PrepareRenderInstruction( RenderInstruction& instruction, BufferIndex updateBufferIndex );
333 * @return true if the view matrix has been updated during this or last frame
335 bool ViewMatrixUpdated();
338 * Indicate whether GL sync is required for native render target.
339 * @param[in] requiresSync whether GL sync is required for native render target
341 void SetSyncRequired( bool requiresSync );
346 * Protected constructor.
351 RenderTask(const RenderTask&);
354 RenderTask& operator=(const RenderTask&);
356 private: // PropertyOwner
358 virtual void ResetDefaultProperties( BufferIndex currentBufferIndex );
360 public: // Animatable Properties
361 AnimatableProperty< Vector2 > mViewportPosition; ///< viewportPosition
362 AnimatableProperty< Vector2 > mViewportSize; ///< viewportSize
363 AnimatableProperty< Vector4 > mClearColor; ///< clearColor
366 RenderMessageDispatcher* mRenderMessageDispatcher;
367 Render::RenderTracker* mRenderSyncTracker;
370 SceneGraph::Camera* mCamera;
371 Render::FrameBuffer* mFrameBuffer;
373 bool mWaitingToRender:1; ///< True when an render once to FBO is waiting
374 bool mNotifyTrigger:1; ///< True if a render once render task has finished renderering
375 bool mExclusive: 1; ///< Whether the render task has exclusive access to the source actor (node in the scene graph implementation).
376 bool mClearEnabled: 1; ///< Whether previous results are cleared.
377 bool mCullMode: 1; ///< Whether renderers should be frustum culled
379 FrameBufferTexture* mRenderTarget;
381 State mState; ///< Render state.
382 unsigned int mRefreshRate; ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
383 unsigned int mFrameCounter; ///< counter for rendering every N frames
385 unsigned int mRenderedOnceCounter; ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
386 bool mRequiresSync; ///< Whether sync is needed to track the render
390 // Messages for RenderTask
391 inline void SetFrameBufferMessage( EventThreadServices& eventThreadServices, RenderTask& task, Render::FrameBuffer* frameBuffer )
393 typedef MessageValue1< RenderTask, Render::FrameBuffer*> LocalType;
395 // Reserve some memory inside the message queue
396 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
398 // Construct message in the message queue memory; note that delete should not be called on the return value
399 new (slot) LocalType( &task, &RenderTask::SetFrameBuffer, frameBuffer );
402 inline void SetClearColorMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Vector4& value )
404 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
406 // Reserve some memory inside the message queue
407 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
409 // Construct message in the message queue memory; note that delete should not be called on the return value
410 new (slot) LocalType( &task, &RenderTask::SetClearColor, value );
413 inline void BakeClearColorMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector4& value )
415 typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
417 // Reserve some memory inside the message queue
418 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
420 // Construct message in the message queue memory; note that delete should not be called on the return value
421 new (slot) LocalType( &task, &RenderTask::BakeClearColor, value );
424 inline void SetClearEnabledMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool enabled )
426 typedef MessageValue1< RenderTask, bool > LocalType;
428 // Reserve some memory inside the message queue
429 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
431 // Construct message in the message queue memory; note that delete should not be called on the return value
432 new (slot) LocalType( &task, &RenderTask::SetClearEnabled, enabled );
435 inline void SetCullModeMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool mode )
437 typedef MessageValue1< RenderTask, bool > LocalType;
439 // Reserve some memory inside the message queue
440 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
442 // Construct message in the message queue memory; note that delete should not be called on the return value
443 new (slot) LocalType( &task, &RenderTask::SetCullMode, mode );
446 inline void SetRefreshRateMessage( EventThreadServices& eventThreadServices, RenderTask& task, unsigned int refreshRate )
448 typedef MessageValue1< RenderTask, unsigned int > LocalType;
450 // Reserve some memory inside the message queue
451 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
453 // Construct message in the message queue memory; note that delete should not be called on the return value
454 new (slot) LocalType( &task, &RenderTask::SetRefreshRate, refreshRate );
457 inline void SetSourceNodeMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode )
459 // Scene graph thread can destroy this object.
460 Node* node = const_cast< Node* >( constNode );
462 typedef MessageValue1< RenderTask, Node* > 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::SetSourceNode, node );
471 inline void SetCameraMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode, const Camera* constCamera )
473 typedef MessageValue2< RenderTask, Node*, Camera* > LocalType;
475 Node* node = const_cast< Node* >( constNode );
476 Camera* camera = const_cast< Camera* >( constCamera );
477 // Reserve memory inside the message queue
478 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
480 // Construct message in the message queue memory; note that delete should not be called on the return value
481 new (slot) LocalType( &task, &RenderTask::SetCamera, node, camera );
484 inline void SetExclusiveMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool exclusive )
486 typedef MessageValue1< RenderTask, bool > LocalType;
488 // Reserve some memory inside the message queue
489 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
491 // Construct message in the message queue memory; note that delete should not be called on the return value
492 new (slot) LocalType( &task, &RenderTask::SetExclusive, exclusive );
495 inline void SetSyncRequiredMessage(EventThreadServices& eventThreadServices, RenderTask& task, bool requiresSync )
497 typedef MessageValue1< RenderTask, bool > LocalType;
499 // Reserve some memory inside the message queue
500 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
502 // Construct message in the message queue memory; note that delete should not be called on the return value
503 new (slot) LocalType( &task, &RenderTask::SetSyncRequired, requiresSync );
506 inline void BakeViewportPositionMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
508 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
510 // Reserve some memory inside the message queue
511 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
513 // Construct message in the message queue memory; note that delete should not be called on the return value
514 new (slot) LocalType( &task, &RenderTask::BakeViewportPosition, value );
517 inline void BakeViewportSizeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
519 typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
521 // Reserve some memory inside the message queue
522 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
524 // Construct message in the message queue memory; note that delete should not be called on the return value
525 new (slot) LocalType( &task, &RenderTask::BakeViewportSize, value );
528 } // namespace SceneGraph
530 } // namespace Internal
534 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__