Change RenderTaskList to behave like any other SceneGraph object
[platform/core/uifw/dali-core.git] / dali / internal / event / render-tasks / render-task-impl.h
1 #ifndef __DALI_INTERNAL_RENDER_TASK_H__
2 #define __DALI_INTERNAL_RENDER_TASK_H__
3
4 /*
5  * Copyright (c) 2018 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/object/base-object.h>
23 #include <dali/public-api/render-tasks/render-task.h>
24 #include <dali/internal/event/images/frame-buffer-image-impl.h>
25 #include <dali/internal/event/common/object-impl.h>
26 #include <dali/internal/event/rendering/frame-buffer-impl.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 class Actor;
35 class CameraActor;
36 class EventThreadServices;
37
38 namespace SceneGraph
39 {
40 class RenderTask;
41 class Camera;
42 }
43
44 class RenderTask : public Object
45 {
46 public:
47
48   typedef Dali::RenderTask::ScreenToFrameBufferFunction ScreenToFrameBufferFunction;
49
50   /**
51    * Creates a new RenderTask.
52    */
53   static RenderTask* New();
54
55   /**
56    * @copydoc Dali::RenderTask::SetSourceActor()
57    */
58   void SetSourceActor( Actor* actor );
59
60   /**
61    * @copydoc Dali::RenderTask::GetSourceActor()
62    */
63   Actor* GetSourceActor() const;
64
65   /**
66    * @copydoc Dali::RenderTask::SetExclusive()
67    */
68   void SetExclusive( bool exclusive );
69
70   /**
71    * @copydoc Dali::RenderTask::IsExclusive()
72    */
73   bool IsExclusive() const;
74
75   /**
76    * @copydoc Dali::RenderTask::SetInputEnabled()
77    */
78   void SetInputEnabled( bool enabled );
79
80   /**
81    * @copydoc Dali::RenderTask::GetInputEnabled()
82    */
83   bool GetInputEnabled() const;
84
85   /**
86    * @copydoc Dali::RenderTask::SetCameraActor()
87    */
88   void SetCameraActor( CameraActor* cameraActor );
89
90   /**
91    * @copydoc Dali::RenderTask::GetCameraActor()
92    */
93   CameraActor* GetCameraActor() const;
94
95   /**
96    * @copydoc Dali::RenderTask::SetTargetFrameBuffer()
97    */
98   void SetTargetFrameBuffer( FrameBufferImagePtr frameBuffer );
99
100   /**
101    * @copydoc Dali::RenderTask::GetTargetFrameBuffer
102    */
103   FrameBufferImage* GetTargetFrameBuffer() const;
104
105   /**
106     * @copydoc Dali::RenderTask::SetFrameBuffer()
107     */
108    void SetFrameBuffer( FrameBufferPtr frameBuffer );
109
110    /**
111     * @copydoc Dali::RenderTask::GetFrameBuffer
112     */
113    FrameBuffer* GetFrameBuffer() const;
114
115   /**
116    * @copydoc Dali::RenderTask::SetScreenToFrameBufferFunction
117    */
118   void SetScreenToFrameBufferFunction( ScreenToFrameBufferFunction conversionFunction );
119
120   /**
121    * @copydoc Dali::RenderTask::GetScreenToFrameBufferFunction
122    */
123   ScreenToFrameBufferFunction GetScreenToFrameBufferFunction() const;
124
125   /**
126    * copydoc Dali::RenderTask::SetScreenToFrameBufferMappingActor
127    */
128   void SetScreenToFrameBufferMappingActor( Actor* mappingActor );
129
130   /**
131    * copydoc Dali::RenderTask::GetScreenToFrameBufferMAppingActor
132    */
133   Actor* GetScreenToFrameBufferMappingActor() const;
134
135   /**
136    * @copydoc Dali::RenderTask::SetViewportPosition
137    */
138   void SetViewportPosition(const Vector2& value);
139
140   /**
141    * @copydoc Dali::RenderTask::GetCurrentViewportPosition
142    */
143   Vector2 GetCurrentViewportPosition() const;
144
145   /**
146    * @copydoc Dali::RenderTask::SetViewportSize
147    */
148   void SetViewportSize(const Vector2& value);
149
150   /**
151    * @copydoc Dali::RenderTask::GetCurrentViewportSize
152    */
153   Vector2 GetCurrentViewportSize() const;
154
155   /**
156    * @copydoc Dali::RenderTask::SetViewport()
157    */
158   void SetViewport( const Viewport& viewport );
159
160   /**
161    * @param[out] viewPort instance to copy the values into
162    */
163   void GetViewport( Viewport& viewPort ) const;
164
165   /**
166    * @copydoc Dali::RenderTask::SetClearColor()
167    */
168   void SetClearColor( const Vector4& color );
169
170   /**
171    * @copydoc Dali::RenderTask::GetClearColor()
172    */
173   const Vector4& GetClearColor() const;
174
175   /**
176    * Indicate whether GL sync is required for native render target.
177    * @param[in] requiresSync whether GL sync is required.
178    */
179   void SetSyncRequired( bool requiresSync );
180
181   /**
182    * Query whether the sync object is required for native render target.
183    * @return True if the sync object is required, false otherwise.
184    */
185  bool IsSyncRequired() const;
186
187   /**
188    * @copydoc Dali::RenderTask::SetClearEnabled()
189    */
190   void SetClearEnabled( bool enabled );
191
192   /**
193    * @copydoc Dali::RenderTask::GetClearEnabled()
194    */
195   bool GetClearEnabled() const;
196
197   /**
198    * @copydoc Dali::RenderTask::SetCullMode()
199    */
200   void SetCullMode( bool mode );
201
202   /**
203    * @copydoc Dali::RenderTask::GetCullMode()
204    */
205   bool GetCullMode() const;
206
207   /**
208    * @copydoc Dali::RenderTask::SetRefreshRate()
209    */
210   void SetRefreshRate( uint32_t refreshRate );
211
212   /**
213    * @copydoc Dali::RenderTask::GetRefreshRate()
214    */
215   uint32_t GetRefreshRate() const;
216
217   /**
218    * Check if the render-task is hittable. If render task is offscreen, the screen coordinates may be translated.
219    * @param[in,out] screenCoords The screen coordinate, which may be converted (for hit-testing actors which are rendered off-screen).
220    * @return True the render-task can be used for input-handling; otherwise the output parameters are not valid.
221    */
222   bool IsHittable( Vector2& screenCoords ) const;
223
224   /**
225    * Translates screen coordinates to render task coordinates for offscreen render tasks
226    * @param[in,out] screenCoords The screen coordinates, which may be converted (for off-screen).
227    * @return false if the conversion function decides the coordinates are not inside. returns true if there is no conversion function
228    */
229   bool TranslateCoordinates( Vector2& screenCoords ) const;
230
231   /**
232    * @copydoc Dali::RenderTask::WorldToViewport()
233    */
234   bool WorldToViewport(const Vector3 &position, float& viewportX, float& viewportY) const;
235
236   /**
237    * @copydoc Dali::RenderTask::ViewportToLocal()
238    */
239   bool ViewportToLocal(Actor* actor, float viewportX, float viewportY, float &localX, float &localY) const;
240
241 public: // Used by RenderTaskList, which owns the SceneGraph::RenderTasks
242
243   /**
244    * Create the scene-graph RenderTask object.
245    * @pre CreateSceneObject has not already been called.
246    * @return A newly allocated scene-graph object; the caller takes ownership.
247    */
248   SceneGraph::RenderTask* CreateSceneObject();
249
250   /**
251    * Retrieve the scene-graph RenderTask object.
252    * @return The scene-graph object, or NULL if this has been discarded.
253    */
254   SceneGraph::RenderTask* GetRenderTaskSceneObject();
255
256   /**
257    * Discard the scene-graph RenderTask object.
258    */
259   void DiscardSceneObject();
260
261 public: // Implementation of Object
262
263   /**
264    * @copydoc Dali::Internal::Object::GetDefaultPropertyCount()
265    */
266   virtual uint32_t GetDefaultPropertyCount() const;
267
268   /**
269    * @copydoc Dali::Internal::Object::GetDefaultPropertyIndices()
270    */
271   virtual void GetDefaultPropertyIndices( Property::IndexContainer& indices ) const;
272
273   /**
274    * @copydoc Dali::Internal::Object::GetDefaultPropertyName()
275    */
276   virtual const char* GetDefaultPropertyName(Property::Index index) const;
277
278   /**
279    * @copydoc Dali::Internal::Object::GetDefaultPropertyIndex()
280    */
281   virtual Property::Index GetDefaultPropertyIndex(const std::string& name) const;
282
283   /**
284    * @copydoc Dali::Internal::Object::IsDefaultPropertyWritable()
285    */
286   virtual bool IsDefaultPropertyWritable(Property::Index index) const;
287
288   /**
289    * @copydoc Dali::Internal::Object::IsDefaultPropertyAnimatable()
290    */
291   virtual bool IsDefaultPropertyAnimatable(Property::Index index) const;
292
293   /**
294    * @copydoc Dali::Internal::Object::IsDefaultPropertyAConstraintInput()
295    */
296   virtual bool IsDefaultPropertyAConstraintInput( Property::Index index ) const;
297
298   /**
299    * @copydoc Dali::Internal::Object::GetDefaultPropertyType()
300    */
301   virtual Property::Type GetDefaultPropertyType(Property::Index index) const;
302
303   /**
304    * @copydoc Dali::Internal::Object::SetDefaultProperty()
305    */
306   virtual void SetDefaultProperty(Property::Index index, const Property::Value& propertyValue);
307
308   /**
309    * @copydoc Dali::Internal::Object::GetDefaultProperty()
310    */
311   virtual Property::Value GetDefaultProperty( Property::Index index ) const;
312
313   /**
314    * @copydoc Dali::Internal::Object::GetDefaultPropertyCurrentValue()
315    */
316   virtual Property::Value GetDefaultPropertyCurrentValue( Property::Index index ) const;
317
318   /**
319    * @copydoc Dali::Internal::Object::OnNotifyDefaultPropertyAnimation()
320    */
321   virtual void OnNotifyDefaultPropertyAnimation( Animation& animation, Property::Index index, const Property::Value& value, Animation::Type animationType );
322
323   /**
324    * @copydoc Dali::Internal::Object::GetSceneObject()
325    */
326   virtual const SceneGraph::PropertyOwner* GetSceneObject() const;
327
328   /**
329    * @copydoc Dali::Internal::Object::GetSceneObjectAnimatableProperty()
330    */
331   virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty( Property::Index index ) const;
332
333   /**
334    * @copydoc Dali::Internal::Object::GetSceneObjectInputProperty()
335    */
336   virtual const PropertyInputImpl* GetSceneObjectInputProperty( Property::Index index ) const;
337
338 public: //signals
339
340   /**
341    * Query whether a Finished signal should be emitted for this render-task.
342    * This should only be called by NotificationManager, before signals are emitted.
343    * @pre The refresh rate must be REFRESH_ONCE.
344    * @post HasFinished() will return false on subsequent calls, until the render-task is processed again.
345    * @return \e true if the refresh once render task has finished. Otherwise it returns \e false.
346    */
347   bool HasFinished();
348
349   /**
350    * Emit the Finished signal
351    */
352   void EmitSignalFinish();
353
354   /**
355    * @copydoc Dali::RenderTask::FinishedSignal()
356    */
357   Dali::RenderTask::RenderTaskSignalType& FinishedSignal();
358
359   /**
360    * Connects a callback function with the object's signals.
361    * @param[in] object The object providing the signal.
362    * @param[in] tracker Used to disconnect the signal.
363    * @param[in] signalName The signal to connect to.
364    * @param[in] functor A newly allocated FunctorDelegate.
365    * @return True if the signal was connected.
366    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
367    */
368   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
369
370 protected:
371
372   /**
373    * Construct a new RenderTask.
374    */
375   RenderTask();
376
377   /**
378    * A reference counted object may only be deleted by calling Unreference()
379    */
380   virtual ~RenderTask();
381
382   /**
383    * Helper class for connecting Nodes to the scene-graph RenderTask
384    */
385   class Connector : public Object::Observer
386   {
387   public:
388
389     enum Type
390     {
391       SOURCE_CONNECTOR,
392       CAMERA_CONNECTOR,
393       MAPPING_CONNECTOR
394     };
395
396     /**
397      * Create the helper class
398      */
399     Connector( Type type, RenderTask& renderTask );
400
401     /**
402      * Non-virtual destructor; not suitable as a base object.
403      */
404     ~Connector();
405
406     /**
407      * Set the actor to be observed.
408      * @param[in] actor The actor to be observed.
409      */
410     void SetActor( Actor* actor );
411
412     /**
413      * Update the scene-graph RenderTask with a new source/camera Node.
414      */
415     void UpdateRenderTask();
416
417   public: // From Object::Observer
418
419     /**
420      * @copydoc Dali::Internal::Object::Observer::SceneObjectAdded
421      */
422     virtual void SceneObjectAdded( Object& object );
423
424     /**
425      * @copydoc Dali::Internal::Object::Observer::SceneObjectAdded
426      */
427     virtual void SceneObjectRemoved( Object& object );
428
429     /**
430      * @copydoc Dali::Internal::Object::Observer::ObjectDestroyed
431      */
432     virtual void ObjectDestroyed( Object& object );
433
434   private:
435
436     // Undefined
437     Connector(const Connector&);
438
439     // Undefined
440     Connector& operator=(const Connector& rhs);
441
442   public:
443
444     const Type mType;
445
446     RenderTask& mRenderTask;
447
448     Actor* mActor; ///< Raw-pointer to the actor; not owned.
449     const SceneGraph::Camera* mCamera;    ///< Raw-pointer to camera scene-graph object; not owned.
450   };
451
452 private:
453
454   SceneGraph::RenderTask* mSceneObject; ///< Raw-pointer to the scene-graph object; not owned.
455
456   Connector mSourceConnector; ///< Responsible for connecting/disconnecting source Nodes
457   Connector mCameraConnector; ///< Responsible for connecting/disconnecting camera Nodes
458   Connector mMappingConnector; /// Responsible for connecting/disconnecting actor node, which used to mapping screen to frame buffer coordinate
459
460   Vector4 mClearColor;       ///< Optional clear color
461
462   Vector2 mViewportPosition; ///< The cached viewport position
463   Vector2 mViewportSize;     ///< The cached viewport size
464
465   uint32_t mRefreshRate; ///< Determines how often the task is processed.
466
467   uint32_t mRefreshOnceCounter;
468
469   FrameBufferImagePtr mFrameBufferImage;  ///< Optional off-screen render target.
470   FrameBufferPtr  mFrameBuffer;
471
472
473   Dali::RenderTask::ScreenToFrameBufferFunction mScreenToFrameBufferFunction; ///< Used to convert screen to frame-buffer coordinates
474
475   bool mExclusive     : 1; ///< True if the render-task has exclusive access to the source Nodes.
476   bool mInputEnabled  : 1; ///< True if the render-task should be considered for input handling.
477   bool mClearEnabled  : 1; ///< True if the render-task should be clear the color buffer.
478   bool mCullMode      : 1; ///< True if the render-task's actors should be culled
479   bool mRequiresSync  : 1; ///< True if the GL sync is required to track the render of.
480
481   //Signals
482   Dali::RenderTask::RenderTaskSignalType  mSignalFinished; ///< Signal emmited when the render task has been processed.
483 };
484
485 } // namespace Internal
486
487 // Helpers for public-api forwarding methods
488
489 inline Internal::RenderTask& GetImplementation(Dali::RenderTask& task)
490 {
491   DALI_ASSERT_ALWAYS(task && "RenderTask handle is empty");
492
493   BaseObject& handle = task.GetBaseObject();
494
495   return static_cast<Internal::RenderTask&>(handle);
496 }
497
498 inline const Internal::RenderTask& GetImplementation(const Dali::RenderTask& task)
499 {
500   DALI_ASSERT_ALWAYS(task && "RenderTask handle is empty");
501
502   const BaseObject& handle = task.GetBaseObject();
503
504   return static_cast<const Internal::RenderTask&>(handle);
505 }
506
507 } // namespace Dali
508
509 #endif //__DALI_INTERNAL_RENDER_TASK_H__