Merge "fix buffer GpuBuffer" into devel/master
[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) 2014 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
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35 class FrameBufferTexture;
36 class ResourceManager;
37
38 namespace Render
39 {
40 class RenderTracker;
41 }
42
43 namespace SceneGraph
44 {
45 class Node;
46 class CameraAttachment;
47 class RenderInstruction;
48 class RenderMessageDispatcher;
49
50 /**
51  * RenderTasks describe how the Dali scene should be rendered.
52  */
53 class RenderTask : public PropertyOwner
54 {
55 public:
56
57   enum State
58   {
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
63   };
64
65   /**
66    * Create a new RenderTask
67    */
68   static RenderTask* New();
69
70   /**
71    * Virtual destructor
72    */
73   virtual ~RenderTask();
74
75   /**
76    * Initialize the render task. Called in update thread
77    * @param[in] renderMessageDispatcher to send messages to render thread
78    * @param[in] resourceManager to check and update status of FBOs
79    */
80   void Initialize( RenderMessageDispatcher& renderMessageDispatcher, ResourceManager& resourceManager );
81
82   /**
83    * Set the nodes to be rendered.
84    * @param[in] node This node and its children will be rendered.
85    */
86   void SetSourceNode( Node* node );
87
88   /**
89    * Retrieve the source node.
90    * @return This node and its children will be rendered.
91    */
92   Node* GetSourceNode() const;
93
94   /**
95    * Set whether the RenderTask has exclusive access to the source nodes.
96    * @param[in] exclusive True if the source nodes will only be rendered by this render-task.
97    */
98   void SetExclusive( bool exclusive );
99
100   /**
101    * Query whether the RenderTask has exclusive access to the source actors.
102    * @return True if the source actors will only be rendered by this render-task.
103    */
104   bool IsExclusive() const;
105
106   /**
107    * Set the node from which the scene is viewed.
108    * @param[in] node The scene is viewed from the perspective of this node.
109    */
110   void SetCameraNode( Node* node );
111
112   /**
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
116    */
117   void SetFrameBufferId( unsigned int resourceId, bool isNativeFBO );
118
119   /**
120    * Retrieve the resource ID of the frame-buffer.
121    * @return The resource ID, or zero if not rendering off-screen.
122    */
123   unsigned int GetFrameBufferId() const;
124
125   /**
126    * Set the value of property viewportPosition
127    * This value will persist only for the current frame.
128    * @param[in] updateBufferIndex The current update buffer index.
129    * @param[in] value The value of the property
130    */
131   void SetViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
132
133   /**
134    * Get the value of property viewportPosition
135    * @warning Should only be called from the Update thread
136    * @param[in] bufferIndex The buffer to read from.
137    * @return the value of the property.
138    */
139   const Vector2& GetViewportPosition( BufferIndex bufferIndex ) const;
140
141   /**
142    * Bake the value of the property viewportPosition
143    * This will also set the base value
144    * @param[in] updateBufferIndex The current update buffer index.
145    * @param[in] value The new value for property.
146    */
147   void BakeViewportPosition( BufferIndex updateBufferIndex, const Vector2& value );
148
149   /**
150    * Set the value of property viewportSize
151    * This value will persist only for the current frame.
152    * @param[in] updateBufferIndex The current update buffer index.
153    * @param[in] value The value of the property
154    */
155   void SetViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
156
157   /**
158    * Get the value of property viewportSize
159    * @warning Should only be called from the Update thread
160    * @param[in] bufferIndex The buffer to read from.
161    * @return the value of the property.
162    */
163   const Vector2& GetViewportSize( BufferIndex bufferIndex ) const;
164
165   /**
166    * Bake the value of the property viewportSize
167    * This will also set the base value
168    * @param[in] updateBufferIndex The current update buffer index.
169    * @param[in] value The new value for property.
170    */
171   void BakeViewportSize( BufferIndex updateBufferIndex, const Vector2& value );
172
173   /**
174    * Get the value of property viewportEnabled
175    * @warning Should only be called from the Update thread
176    * @param[in] bufferIndex The buffer to read from.
177    * @return the value of the property.
178    */
179   bool GetViewportEnabled( BufferIndex bufferIndex ) const;
180
181   /**
182    * Query whether the optional viewport is set.
183    * @param[in] bufferIndex The buffer to read from.
184    * @param[out] viewport The viewport position and size is populated.
185    * @return true if the viewport has been set
186    */
187   bool QueryViewport( BufferIndex bufferIndex, Viewport& viewport ) const;
188
189   /**
190    * Set the value of property clearColor
191    * This value will persist only for the current frame.
192    * @param[in] updateBufferIndex The current update buffer index.
193    * @param[in] value The value of the property
194    */
195   void SetClearColor( BufferIndex updateBufferIndex, const Vector4& value );
196
197   /**
198    * Get the value of property clearColor
199    * @warning Should only be called from the Update thread
200    * @param[in] bufferIndex The buffer to read from.
201    * @return the value of the property.
202    */
203   const Vector4& GetClearColor( BufferIndex bufferIndex ) const;
204
205   /**
206    * Bake the value of the property clearColor
207    * This will also set the base value
208    * @param[in] updateBufferIndex The current update buffer index.
209    * @param[in] value The new value for property.
210    */
211   void BakeClearColor( BufferIndex updateBufferIndex, const Vector4& value );
212
213   /**
214    * @copydoc Dali::RenderTask::SetClearEnabled()
215    */
216   void SetClearEnabled( bool enabled );
217
218   /**
219    * @copydoc Dali::RenderTask::GetClearEnabled()
220    */
221   bool GetClearEnabled() const;
222
223   /**
224    * @copydoc Dali::RenderTask::SetCullMode()
225    */
226   void SetCullMode( bool mode );
227
228   /**
229    * @copydoc Dali::RenderTask::GetCullMode()
230    */
231   bool GetCullMode() const;
232
233   /**
234    * Set the refresh-rate of the RenderTask.
235    * @param[in] refreshRate The new refresh rate.
236    */
237   void SetRefreshRate( unsigned int refreshRate );
238
239   /**
240    * Retrieve the refresh-rate of the RenderTask.
241    * @return The refresh rate.
242    */
243   unsigned int GetRefreshRate() const;
244
245   /**
246    * Check if the render task is ready for rendering.
247    * @param[in] updateBufferIndex The current update buffer index.
248    * @return True if the render-task is ready for rendering.
249    */
250   bool ReadyToRender( BufferIndex updateBufferIndex );
251
252   /**
253    * True if a render is required. If the current state is RENDER_CONTINUOUSLY, then
254    * 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.
255    * @return true if a render is required
256    */
257   bool IsRenderRequired();
258
259   /**
260    * Set whether all resources were available when the render-task was processed
261    * @param[in] resourcesComplete True if the resources of the source tree are completely loaded.
262    */
263   void SetResourcesFinished( bool resourcesFinished );
264
265   /**
266    * Process a frame. This method is called each frame for every ready render task, regardless
267    * of whether it needs to render (so that the frame counter can be updated).
268    */
269   void UpdateState();
270
271   /**
272    * Return true only if currently waiting for the render task to
273    * finish rendering and the update thread should be kept alive.
274    * @return true if waiting to be rendered
275    */
276   bool IsWaitingToRender();
277
278   /**
279    * Return true when the render task has finished rendering and a notification
280    * needs sending. (Only one notification is sent per render once request)
281    * @return true if notification is required.
282    */
283   bool HasRendered();
284
285   /**
286    * @return The number of times we have transited from RENDERED_ONCE to RENDERED_ONCE_AND_NOTIFIED state.
287    */
288   unsigned int GetRenderedOnceCounter() const;
289
290   /**
291    * Retrieve the view-matrix; this is double buffered for input handling.
292    * @pre GetCameraNode() returns a node with valid CameraAttachment.
293    * @param[in] bufferIndex The buffer to read from.
294    * @return The view-matrix.
295    */
296   const Matrix& GetViewMatrix( BufferIndex bufferIndex ) const;
297
298   /**
299    * @brief Retrieve the camera attachment.
300    * @pre GetCameraNode() returns a node with valid CameraAttachment.
301    *
302    * @return The camera attachment.
303    */
304   SceneGraph::CameraAttachment& GetCameraAttachment() const;
305
306   /**
307    * Retrieve the projection-matrix; this is double buffered for input handling.
308    * @pre GetCameraNode() returns a node with valid CameraAttachment.
309    * @param[in] bufferIndex The buffer to read from.
310    * @return The projection-matrix.
311    */
312   const Matrix& GetProjectionMatrix( BufferIndex bufferIndex ) const;
313
314   /**
315    * Prepares the render-instruction buffer to be populated with instructions.
316    * @param[out] instruction to prepare
317    * @param[in] updateBufferIndex The current update buffer index.
318    */
319   void PrepareRenderInstruction( RenderInstruction& instruction, BufferIndex updateBufferIndex );
320
321   /**
322    * @return true if the view matrix has been updated during this or last frame
323    */
324   bool ViewMatrixUpdated();
325
326   /**
327    * @return A pointer to the camera used by the RenderTask
328    */
329   Node* GetCamera() const;
330
331 private:
332
333   /**
334    * Protected constructor.
335    */
336   RenderTask();
337
338   // Undefined
339   RenderTask(const RenderTask&);
340
341   // Undefined
342   RenderTask& operator=(const RenderTask&);
343
344 private: // PropertyOwner
345
346   virtual void ResetDefaultProperties( BufferIndex currentBufferIndex );
347
348 public: // Animatable Properties
349   AnimatableProperty< Vector2 >   mViewportPosition;    ///< viewportPosition
350   AnimatableProperty< Vector2 >   mViewportSize;        ///< viewportSize
351   AnimatableProperty< Vector4 >   mClearColor;          ///< clearColor
352
353 private:
354   RenderMessageDispatcher* mRenderMessageDispatcher;
355   ResourceManager* mResourceManager;
356   Render::RenderTracker* mRenderSyncTracker;
357   Node* mSourceNode;
358   Node* mCameraNode;
359   CameraAttachment* mCameraAttachment;
360   unsigned int mFrameBufferResourceId;
361
362   bool mResourcesFinished:1; ///< True if all resources were available when the render-task was processed
363   bool mWaitingToRender:1; ///< True when an render once to FBO is waiting
364   bool mNotifyTrigger:1; ///< True if a render once render task has finished renderering
365   bool mExclusive: 1; ///< Whether the render task has exclusive access to the source actor (node in the scene graph implementation).
366   bool mClearEnabled: 1; ///< Whether previous results are cleared.
367   bool mCullMode: 1; ///< Whether renderers should be frustum culled
368
369   FrameBufferTexture* mRenderTarget;
370   Viewport mViewport;
371
372   State mState;                     ///< Render state.
373   unsigned int mRefreshRate;        ///< REFRESH_ONCE, REFRESH_ALWAYS or render every N frames
374   unsigned int mFrameCounter;       ///< counter for rendering every N frames
375
376   unsigned int mRenderedOnceCounter;  ///< Incremented whenever state changes to RENDERED_ONCE_AND_NOTIFIED
377   bool mTargetIsNativeFramebuffer; ///< Tells if our target is a native framebuffer
378
379 };
380
381 // Messages for RenderTask
382
383 inline void SetFrameBufferIdMessage( EventThreadServices& eventThreadServices, RenderTask& task, unsigned int resourceId, bool isNativeFBO )
384 {
385   typedef MessageValue2< RenderTask, unsigned int, bool > LocalType;
386
387   // Reserve some memory inside the message queue
388   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
389
390   // Construct message in the message queue memory; note that delete should not be called on the return value
391   new (slot) LocalType( &task, &RenderTask::SetFrameBufferId, resourceId, isNativeFBO );
392 }
393
394 inline void SetClearColorMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Vector4& value )
395 {
396   typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
397
398   // Reserve some memory inside the message queue
399   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
400
401   // Construct message in the message queue memory; note that delete should not be called on the return value
402   new (slot) LocalType( &task, &RenderTask::SetClearColor, value );
403 }
404
405 inline void BakeClearColorMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector4& value )
406 {
407   typedef MessageDoubleBuffered1< RenderTask, Vector4 > LocalType;
408
409   // Reserve some memory inside the message queue
410   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
411
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::BakeClearColor, value );
414 }
415
416 inline void SetClearEnabledMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool enabled )
417 {
418   typedef MessageValue1< RenderTask, bool > LocalType;
419
420   // Reserve some memory inside the message queue
421   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
422
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::SetClearEnabled, enabled );
425 }
426
427 inline void SetCullModeMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool mode )
428 {
429   typedef MessageValue1< RenderTask, bool > LocalType;
430
431   // Reserve some memory inside the message queue
432   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
433
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::SetCullMode, mode );
436 }
437
438 inline void SetRefreshRateMessage( EventThreadServices& eventThreadServices, RenderTask& task, unsigned int refreshRate )
439 {
440   typedef MessageValue1< RenderTask, unsigned int > LocalType;
441
442   // Reserve some memory inside the message queue
443   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
444
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::SetRefreshRate, refreshRate );
447 }
448
449 inline void SetSourceNodeMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode )
450 {
451   // Scene graph thread can destroy this object.
452   Node* node = const_cast< Node* >( constNode );
453
454   typedef MessageValue1< RenderTask, Node* > LocalType;
455
456   // Reserve some memory inside the message queue
457   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
458
459   // Construct message in the message queue memory; note that delete should not be called on the return value
460   new (slot) LocalType( &task, &RenderTask::SetSourceNode, node );
461 }
462
463 inline void SetCameraNodeMessage( EventThreadServices& eventThreadServices, RenderTask& task, const Node* constNode )
464 {
465   // Scene graph thread can destroy this object.
466   Node* node = const_cast< Node* >( constNode );
467
468   typedef MessageValue1< RenderTask, Node* > LocalType;
469
470   // Reserve some memory inside the message queue
471   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
472
473   // Construct message in the message queue memory; note that delete should not be called on the return value
474   new (slot) LocalType( &task, &RenderTask::SetCameraNode, node );
475 }
476
477 inline void SetExclusiveMessage( EventThreadServices& eventThreadServices, RenderTask& task, bool exclusive )
478 {
479   typedef MessageValue1< RenderTask, bool > LocalType;
480
481   // Reserve some memory inside the message queue
482   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
483
484   // Construct message in the message queue memory; note that delete should not be called on the return value
485   new (slot) LocalType( &task, &RenderTask::SetExclusive, exclusive );
486 }
487
488 inline void BakeViewportPositionMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
489 {
490   typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
491
492   // Reserve some memory inside the message queue
493   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
494
495   // Construct message in the message queue memory; note that delete should not be called on the return value
496   new (slot) LocalType( &task, &RenderTask::BakeViewportPosition, value );
497 }
498
499 inline void BakeViewportSizeMessage( EventThreadServices& eventThreadServices, const RenderTask& task, const Vector2& value )
500 {
501   typedef MessageDoubleBuffered1< RenderTask, Vector2 > LocalType;
502
503   // Reserve some memory inside the message queue
504   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
505
506   // Construct message in the message queue memory; note that delete should not be called on the return value
507   new (slot) LocalType( &task, &RenderTask::BakeViewportSize, value );
508 }
509
510 } // namespace SceneGraph
511
512 } // namespace Internal
513
514 } // namespace Dali
515
516 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDER_TASK_H__