Add function to set top margin of the stage
[platform/core/uifw/dali-core.git] / dali / internal / update / render-tasks / scene-graph-render-task.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__
3
4 /*
5  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // INTERNAL INCLUDES
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>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36 class FrameBufferTexture;
37 class ResourceManager;
38
39 namespace Render
40 {
41 class RenderTracker;
42 }
43
44 namespace SceneGraph
45 {
46 class Node;
47 class Camera;
48 class RenderInstruction;
49 class RenderMessageDispatcher;
50
51 /**
52  * RenderTasks describe how the Dali scene should be rendered.
53  */
54 class RenderTask : public PropertyOwner
55 {
56 public:
57
58   enum State
59   {
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
64   };
65
66   /**
67    * Create a new RenderTask
68    */
69   static RenderTask* New();
70
71   /**
72    * Virtual destructor
73    */
74   virtual ~RenderTask();
75
76   /**
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
80    */
81   void Initialize( RenderMessageDispatcher& renderMessageDispatcher, ResourceManager& resourceManager );
82
83   /**
84    * Set the nodes to be rendered.
85    * @param[in] node This node and its children will be rendered.
86    */
87   void SetSourceNode( Node* node );
88
89   /**
90    * Retrieve the source node.
91    * @return This node and its children will be rendered.
92    */
93   Node* GetSourceNode() const;
94
95   /**
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.
98    */
99   void SetExclusive( bool exclusive );
100
101   /**
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.
104    */
105   bool IsExclusive() const;
106
107   /**
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.
111    */
112   void SetCamera( Node* cameraNode, Camera* camera );
113
114   /**
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
118    */
119   void SetFrameBufferId( unsigned int resourceId, bool isNativeFBO );
120
121   /**
122    * Retrieve the resource ID of the frame-buffer.
123    * @return The resource ID, or zero if not rendering off-screen.
124    */
125   unsigned int GetFrameBufferId() const;
126
127   /**
128    * Set the frame-buffer used as a render target.
129    * @param[in] frameBuffer The framebuffer
130    */
131   void SetFrameBuffer( Render::FrameBuffer* frameBuffer );
132
133   /**
134    * Retrieve the resource ID of the frame-buffer.
135    * @return The resource ID, or zero if not rendering off-screen.
136    */
137   Render::FrameBuffer* GetFrameBuffer();
138
139   /**
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
144    */
145   void SetViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
146
147   /**
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.
152    */
153   const Vector2& GetViewportPosition( BufferIndex bufferIndex ) const;
154
155   /**
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.
160    */
161   void BakeViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
162
163   /**
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
168    */
169   void SetViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
170
171   /**
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.
176    */
177   const Vector2& GetViewportSize( BufferIndex bufferIndex ) const;
178
179   /**
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.
184    */
185   void BakeViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
186
187   /**
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.
192    */
193   bool GetViewportEnabled( BufferIndex bufferIndex ) const;
194
195   /**
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
200    */
201   bool QueryViewport( BufferIndex bufferIndex, Viewport& viewport ) const;
202
203   /**
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
208    */
209   void SetClearColor( BufferIndex updateBufferIndex, const Vector4& value );
210
211   /**
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.
216    */
217   const Vector4& GetClearColor( BufferIndex bufferIndex ) const;
218
219   /**
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.
224    */
225   void BakeClearColor( BufferIndex updateBufferIndex, const Vector4& value );
226
227   /**
228    * @copydoc Dali::RenderTask::SetClearEnabled()
229    */
230   void SetClearEnabled( bool enabled );
231
232   /**
233    * @copydoc Dali::RenderTask::GetClearEnabled()
234    */
235   bool GetClearEnabled() const;
236
237   /**
238    * @copydoc Dali::RenderTask::SetCullMode()
239    */
240   void SetCullMode( bool mode );
241
242   /**
243    * @copydoc Dali::RenderTask::GetCullMode()
244    */
245   bool GetCullMode() const;
246
247   /**
248    * Set the refresh-rate of the RenderTask.
249    * @param[in] refreshRate The new refresh rate.
250    */
251   void SetRefreshRate( unsigned int refreshRate );
252
253   /**
254    * Retrieve the refresh-rate of the RenderTask.
255    * @return The refresh rate.
256    */
257   unsigned int GetRefreshRate() const;
258
259   /**
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.
263    */
264   bool ReadyToRender( BufferIndex updateBufferIndex );
265
266   /**
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
270    */
271   bool IsRenderRequired();
272
273   /**
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.
276    */
277   void SetResourcesFinished( bool resourcesFinished );
278
279   /**
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).
282    */
283   void UpdateState();
284
285   /**
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
289    */
290   bool IsWaitingToRender();
291
292   /**
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.
296    */
297   bool HasRendered();
298
299   /**
300    * @return The number of times we have transited from RENDERED_ONCE to RENDERED_ONCE_AND_NOTIFIED state.
301    */
302   unsigned int GetRenderedOnceCounter() const;
303
304   /**
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.
309    */
310   const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
311
312   /**
313    * @brief Retrieve the camera.
314    * @pre GetCameraNode() returns a node with valid Camera.
315    *
316    * @return The camera.
317    */
318   SceneGraph::Camera& GetCamera() const;
319
320   /**
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.
325    */
326   const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
327
328   /**
329    * Prepares the render-instruction buffer to be populated with instructions.
330    *
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.
334    *
335    * @param[out] instruction to prepare
336    * @param[in] updateBufferIndex The current update buffer index.
337    */
338   void PrepareRenderInstruction( RenderInstruction& instruction, BufferIndex updateBufferIndex );
339
340   /**
341    * @return true if the view matrix has been updated during this or last frame
342    */
343   bool ViewMatrixUpdated();
344
345   /**
346    * Indicate whether GL sync is required for native render target.
347    * @param[in] requiresSync whether GL sync is required for native render target
348    */
349   void SetSyncRequired( bool requiresSync );
350
351 private:
352
353   /**
354    * Protected constructor.
355    */
356   RenderTask();
357
358   // Undefined
359   RenderTask(const RenderTask&);
360
361   // Undefined
362   RenderTask& operator=(const RenderTask&);
363
364 private: // PropertyOwner
365
366   virtual void ResetDefaultProperties( BufferIndex currentBufferIndex );
367
368 public: // Animatable Properties
369   AnimatableProperty< Vector2 >   mViewportPosition;    ///< viewportPosition
370   AnimatableProperty< Vector2 >   mViewportSize;        ///< viewportSize
371   AnimatableProperty< Vector4 >   mClearColor;          ///< clearColor
372
373 private:
374   RenderMessageDispatcher* mRenderMessageDispatcher;
375   ResourceManager* mResourceManager;
376   Render::RenderTracker* mRenderSyncTracker;
377   Node* mSourceNode;
378   Node* mCameraNode;
379   SceneGraph::Camera* mCamera;
380   unsigned int mFrameBufferResourceId;
381   Render::FrameBuffer* mFrameBuffer;
382
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
389
390   FrameBufferTexture* mRenderTarget;
391
392   State mState;                     ///< Render state.
393   unsigned int mRefreshRate;        ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
394   unsigned int mFrameCounter;       ///< counter for rendering every N frames
395
396   unsigned int mRenderedOnceCounter;  ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
397   bool mTargetIsNativeFramebuffer; ///< Tells if our target is a native framebuffer
398   bool mRequiresSync;              ///< Whether sync is needed to track the render
399
400 };
401
402 // Messages for RenderTask
403
404 inline void SetFrameBufferIdMessage( EventThreadServices& eventThreadServices, RenderTask& task, unsigned int resourceId, bool isNativeFBO )
405 {
406   typedef MessageValue2< RenderTask, unsigned int, bool > LocalType;
407
408   // Reserve some memory inside the message queue
409   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
410
411   // Construct message in the message queue memory; note that delete should not be called on the return value
412   new (slot) LocalType( &task, &RenderTask::SetFrameBufferId, resourceId, isNativeFBO );
413 }
414
415 inline void SetFrameBufferMessage( EventThreadServices& eventThreadServices, RenderTask& task, Render::FrameBuffer* frameBuffer )
416 {
417   typedef MessageValue1< RenderTask, Render::FrameBuffer*> LocalType;
418
419   // Reserve some memory inside the message queue
420   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
421
422   // Construct message in the message queue memory; note that delete should not be called on the return value
423   new (slot) LocalType( &task, &RenderTask::SetFrameBuffer, frameBuffer );
424 }
425
426 inline void SetClearColorMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Vector4& value )
427 {
428   typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
429
430   // Reserve some memory inside the message queue
431   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
432
433   // Construct message in the message queue memory; note that delete should not be called on the return value
434   new (slot) LocalType( &task, &RenderTask::SetClearColor, value );
435 }
436
437 inline void BakeClearColorMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector4& value )
438 {
439   typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
440
441   // Reserve some memory inside the message queue
442   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
443
444   // Construct message in the message queue memory; note that delete should not be called on the return value
445   new (slot) LocalType( &task, &RenderTask::BakeClearColor, value );
446 }
447
448 inline void SetClearEnabledMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool enabled )
449 {
450   typedef MessageValue1< RenderTask, bool > LocalType;
451
452   // Reserve some memory inside the message queue
453   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
454
455   // Construct message in the message queue memory; note that delete should not be called on the return value
456   new (slot) LocalType( &task, &RenderTask::SetClearEnabled, enabled );
457 }
458
459 inline void SetCullModeMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool mode )
460 {
461   typedef MessageValue1< RenderTask, bool > LocalType;
462
463   // Reserve some memory inside the message queue
464   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
465
466   // Construct message in the message queue memory; note that delete should not be called on the return value
467   new (slot) LocalType( &task, &RenderTask::SetCullMode, mode );
468 }
469
470 inline void SetRefreshRateMessage( EventThreadServices& eventThreadServices, RenderTask& task, unsigned int refreshRate )
471 {
472   typedef MessageValue1< RenderTask, unsigned int > LocalType;
473
474   // Reserve some memory inside the message queue
475   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
476
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::SetRefreshRate, refreshRate );
479 }
480
481 inline void SetSourceNodeMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode )
482 {
483   // Scene graph thread can destroy this object.
484   Node* node = const_cast< Node* >( constNode );
485
486   typedef MessageValue1< RenderTask, Node* > LocalType;
487
488   // Reserve some memory inside the message queue
489   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
490
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::SetSourceNode, node );
493 }
494
495 inline void SetCameraMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode, const Camera* constCamera )
496 {
497   typedef MessageValue2< RenderTask, Node*, Camera* > LocalType;
498
499   Node* node = const_cast< Node* >( constNode );
500   Camera* camera = const_cast< Camera* >( constCamera );
501   // Reserve memory inside the message queue
502   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
503
504   // Construct message in the message queue memory; note that delete should not be called on the return value
505   new (slot) LocalType( &task, &RenderTask::SetCamera, node, camera );
506 }
507
508 inline void SetExclusiveMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool exclusive )
509 {
510   typedef MessageValue1< RenderTask, bool > LocalType;
511
512   // Reserve some memory inside the message queue
513   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
514
515   // Construct message in the message queue memory; note that delete should not be called on the return value
516   new (slot) LocalType( &task, &RenderTask::SetExclusive, exclusive );
517 }
518
519 inline void SetSyncRequiredMessage(EventThreadServices& eventThreadServices, RenderTask& task, bool requiresSync )
520 {
521   typedef MessageValue1< RenderTask, bool > LocalType;
522
523   // Reserve some memory inside the message queue
524   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
525
526   // Construct message in the message queue memory; note that delete should not be called on the return value
527   new (slot) LocalType( &task, &RenderTask::SetSyncRequired, requiresSync );
528 }
529
530 inline void BakeViewportPositionMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
531 {
532   typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
533
534   // Reserve some memory inside the message queue
535   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
536
537   // Construct message in the message queue memory; note that delete should not be called on the return value
538   new (slot) LocalType( &task, &RenderTask::BakeViewportPosition, value );
539 }
540
541 inline void BakeViewportSizeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
542 {
543   typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
544
545   // Reserve some memory inside the message queue
546   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
547
548   // Construct message in the message queue memory; note that delete should not be called on the return value
549   new (slot) LocalType( &task, &RenderTask::BakeViewportSize, value );
550 }
551
552 } // namespace SceneGraph
553
554 } // namespace Internal
555
556 } // namespace Dali
557
558 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__