Merge "Fix a crash of the RenderTask" into devel/master
[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) 2019 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/object/weak-handle.h>
24 #include <dali/public-api/render-tasks/render-task.h>
25 #include <dali/internal/event/images/frame-buffer-image-impl.h>
26 #include <dali/internal/event/common/object-impl.h>
27 #include <dali/internal/event/rendering/frame-buffer-impl.h>
28 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
29 #include <dali/internal/event/events/actor-observer.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 class Actor;
38 class CameraActor;
39 class EventThreadServices;
40 class RenderTaskList;
41
42 namespace SceneGraph
43 {
44 class RenderTaskList;
45 class RenderTask;
46 class Camera;
47 }
48
49 using RenderTaskPtr = IntrusivePtr<RenderTask>;
50
51 class RenderTask : public Object
52 {
53 public:
54
55   using ScreenToFrameBufferFunction = Dali::RenderTask::ScreenToFrameBufferFunction;
56
57   /**
58    * Creates a new RenderTask.
59    *
60    * @param[in] sourceActor The source actor.
61    * @param[in] cameraActor The camera actor.
62    * @param[in] renderTaskList The render task list.
63    * @return The created render task
64    */
65   static RenderTaskPtr New( Actor* sourceActor, CameraActor* cameraActor, RenderTaskList& renderTaskList );
66
67   /**
68    * @copydoc Dali::RenderTask::SetSourceActor()
69    */
70   void SetSourceActor( Actor* actor );
71
72   /**
73    * @copydoc Dali::RenderTask::GetSourceActor()
74    */
75   Actor* GetSourceActor() const;
76
77   /**
78    * @copydoc Dali::RenderTask::SetExclusive()
79    */
80   void SetExclusive( bool exclusive );
81
82   /**
83    * @copydoc Dali::RenderTask::IsExclusive()
84    */
85   bool IsExclusive() const;
86
87   /**
88    * @copydoc Dali::RenderTask::SetInputEnabled()
89    */
90   void SetInputEnabled( bool enabled );
91
92   /**
93    * @copydoc Dali::RenderTask::GetInputEnabled()
94    */
95   bool GetInputEnabled() const;
96
97   /**
98    * @copydoc Dali::RenderTask::SetCameraActor()
99    */
100   void SetCameraActor( CameraActor* cameraActor );
101
102   /**
103    * @copydoc Dali::RenderTask::GetCameraActor()
104    */
105   CameraActor* GetCameraActor() const;
106
107   /**
108    * @copydoc Dali::RenderTask::SetTargetFrameBuffer()
109    */
110   void SetTargetFrameBuffer( FrameBufferImagePtr frameBuffer );
111
112   /**
113    * @copydoc Dali::RenderTask::GetTargetFrameBuffer
114    */
115   FrameBufferImage* GetTargetFrameBuffer() const;
116
117   /**
118     * @copydoc Dali::RenderTask::SetFrameBuffer()
119     */
120    void SetFrameBuffer( FrameBufferPtr frameBuffer );
121
122    /**
123     * @copydoc Dali::RenderTask::GetFrameBuffer
124     */
125    FrameBuffer* GetFrameBuffer() const;
126
127   /**
128    * @copydoc Dali::RenderTask::SetScreenToFrameBufferFunction
129    */
130   void SetScreenToFrameBufferFunction( ScreenToFrameBufferFunction conversionFunction );
131
132   /**
133    * @copydoc Dali::RenderTask::GetScreenToFrameBufferFunction
134    */
135   ScreenToFrameBufferFunction GetScreenToFrameBufferFunction() const;
136
137   /**
138    * copydoc Dali::RenderTask::SetScreenToFrameBufferMappingActor
139    */
140   void SetScreenToFrameBufferMappingActor( Dali::Actor& mappingActor );
141
142   /**
143    * copydoc Dali::RenderTask::GetScreenToFrameBufferMAppingActor
144    */
145   Dali::Actor GetScreenToFrameBufferMappingActor() const;
146
147   /**
148    * @copydoc Dali::RenderTask::SetViewportPosition
149    */
150   void SetViewportPosition(const Vector2& value);
151
152   /**
153    * @copydoc Dali::RenderTask::GetCurrentViewportPosition
154    */
155   Vector2 GetCurrentViewportPosition() const;
156
157   /**
158    * @copydoc Dali::RenderTask::SetViewportSize
159    */
160   void SetViewportSize(const Vector2& value);
161
162   /**
163    * @copydoc Dali::RenderTask::GetCurrentViewportSize
164    */
165   Vector2 GetCurrentViewportSize() const;
166
167   /**
168    * @copydoc Dali::RenderTask::SetViewport()
169    */
170   void SetViewport( const Viewport& viewport );
171
172   /**
173    * @param[out] viewPort instance to copy the values into
174    */
175   void GetViewport( Viewport& viewPort ) const;
176
177   /**
178    * @copydoc Dali::RenderTask::SetClearColor()
179    */
180   void SetClearColor( const Vector4& color );
181
182   /**
183    * @copydoc Dali::RenderTask::GetClearColor()
184    */
185   const Vector4& GetClearColor() const;
186
187   /**
188    * Indicate whether GL sync is required for native render target.
189    * @param[in] requiresSync whether GL sync is required.
190    */
191   void SetSyncRequired( bool requiresSync );
192
193   /**
194    * Query whether the sync object is required for native render target.
195    * @return True if the sync object is required, false otherwise.
196    */
197  bool IsSyncRequired() const;
198
199   /**
200    * @copydoc Dali::RenderTask::SetClearEnabled()
201    */
202   void SetClearEnabled( bool enabled );
203
204   /**
205    * @copydoc Dali::RenderTask::GetClearEnabled()
206    */
207   bool GetClearEnabled() const;
208
209   /**
210    * @copydoc Dali::RenderTask::SetCullMode()
211    */
212   void SetCullMode( bool mode );
213
214   /**
215    * @copydoc Dali::RenderTask::GetCullMode()
216    */
217   bool GetCullMode() const;
218
219   /**
220    * @copydoc Dali::RenderTask::SetRefreshRate()
221    */
222   void SetRefreshRate( uint32_t refreshRate );
223
224   /**
225    * @copydoc Dali::RenderTask::GetRefreshRate()
226    */
227   uint32_t GetRefreshRate() const;
228
229   /**
230    * Check if the render-task is hittable. If render task is offscreen, the screen coordinates may be translated.
231    * @param[in,out] screenCoords The screen coordinate, which may be converted (for hit-testing actors which are rendered off-screen).
232    * @return True the render-task can be used for input-handling; otherwise the output parameters are not valid.
233    */
234   bool IsHittable( Vector2& screenCoords ) const;
235
236   /**
237    * Translates screen coordinates to render task coordinates for offscreen render tasks
238    * @param[in,out] screenCoords The screen coordinates, which may be converted (for off-screen).
239    * @return false if the conversion function decides the coordinates are not inside. returns true if there is no conversion function
240    */
241   bool TranslateCoordinates( Vector2& screenCoords ) const;
242
243   /**
244    * @copydoc Dali::RenderTask::WorldToViewport()
245    */
246   bool WorldToViewport(const Vector3 &position, float& viewportX, float& viewportY) const;
247
248   /**
249    * @copydoc Dali::RenderTask::ViewportToLocal()
250    */
251   bool ViewportToLocal(Actor* actor, float viewportX, float viewportY, float &localX, float &localY) const;
252
253 public: // Used by RenderTaskList, which owns the SceneGraph::RenderTasks
254
255   /**
256    * Retrieve the scene-graph RenderTask object.
257    * @return The scene-graph object
258    */
259   const SceneGraph::RenderTask& GetRenderTaskSceneObject() const;
260
261   /**
262    * Retrieve the render task list RenderTask object belongs to.
263    * @return The render task list
264    */
265   RenderTaskList& GetRenderTaskList() const;
266
267 public: // Implementation of Object
268
269   /**
270    * @copydoc Dali::Internal::Object::SetDefaultProperty()
271    */
272   virtual void SetDefaultProperty(Property::Index index, const Property::Value& propertyValue);
273
274   /**
275    * @copydoc Dali::Internal::Object::GetDefaultProperty()
276    */
277   virtual Property::Value GetDefaultProperty( Property::Index index ) const;
278
279   /**
280    * @copydoc Dali::Internal::Object::GetDefaultPropertyCurrentValue()
281    */
282   virtual Property::Value GetDefaultPropertyCurrentValue( Property::Index index ) const;
283
284   /**
285    * @copydoc Dali::Internal::Object::OnNotifyDefaultPropertyAnimation()
286    */
287   virtual void OnNotifyDefaultPropertyAnimation( Animation& animation, Property::Index index, const Property::Value& value, Animation::Type animationType );
288
289   /**
290    * @copydoc Dali::Internal::Object::GetSceneObjectAnimatableProperty()
291    */
292   virtual const SceneGraph::PropertyBase* GetSceneObjectAnimatableProperty( Property::Index index ) const;
293
294   /**
295    * @copydoc Dali::Internal::Object::GetSceneObjectInputProperty()
296    */
297   virtual const PropertyInputImpl* GetSceneObjectInputProperty( Property::Index index ) const;
298
299 public: //signals
300
301   /**
302    * Query whether a Finished signal should be emitted for this render-task.
303    * This should only be called by NotificationManager, before signals are emitted.
304    * @pre The refresh rate must be REFRESH_ONCE.
305    * @post HasFinished() will return false on subsequent calls, until the render-task is processed again.
306    * @return \e true if the refresh once render task has finished. Otherwise it returns \e false.
307    */
308   bool HasFinished();
309
310   /**
311    * Emit the Finished signal
312    */
313   void EmitSignalFinish();
314
315   /**
316    * @copydoc Dali::RenderTask::FinishedSignal()
317    */
318   Dali::RenderTask::RenderTaskSignalType& FinishedSignal();
319
320   /**
321    * Connects a callback function with the object's signals.
322    * @param[in] object The object providing the signal.
323    * @param[in] tracker Used to disconnect the signal.
324    * @param[in] signalName The signal to connect to.
325    * @param[in] functor A newly allocated FunctorDelegate.
326    * @return True if the signal was connected.
327    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
328    */
329   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
330
331 protected:
332
333   /**
334    * Constructor.
335    *
336    * @param[in] sceneObject The scene graph object
337    * @param[in] renderTaskList The render task list
338    */
339   RenderTask( const SceneGraph::RenderTask* sceneObject, RenderTaskList& renderTaskList );
340
341   /**
342    * A reference counted object may only be deleted by calling Unreference()
343    */
344   virtual ~RenderTask();
345
346 private: // not copyable
347
348   RenderTask() = delete;
349   RenderTask( const RenderTask& ) = delete;
350   RenderTask& operator=( const RenderTask& ) = delete;
351
352 private:
353
354   ActorObserver mSourceActor; ///< Source actor
355   ActorObserver mCameraActor; ///< Camera actor
356   WeakHandle<Dali::Actor> mInputMappingActor; /// used to mapping screen to frame buffer coordinate, not kept alive by rendertask
357   RenderTaskList& mRenderTaskList; ///< The render task list
358
359   Vector4 mClearColor;       ///< Optional clear color
360
361   Vector2 mViewportPosition; ///< The cached viewport position
362   Vector2 mViewportSize;     ///< The cached viewport size
363
364   uint32_t mRefreshRate; ///< Determines how often the task is processed.
365
366   uint32_t mRefreshOnceCounter;
367
368   FrameBufferImagePtr mFrameBufferImage;  ///< Optional off-screen render target.
369   FrameBufferPtr  mFrameBuffer;
370
371
372   Dali::RenderTask::ScreenToFrameBufferFunction mScreenToFrameBufferFunction; ///< Used to convert screen to frame-buffer coordinates
373
374   bool mExclusive     : 1; ///< True if the render-task has exclusive access to the source Nodes.
375   bool mInputEnabled  : 1; ///< True if the render-task should be considered for input handling.
376   bool mClearEnabled  : 1; ///< True if the render-task should be clear the color buffer.
377   bool mCullMode      : 1; ///< True if the render-task's actors should be culled
378   bool mRequiresSync  : 1; ///< True if the GL sync is required to track the render of.
379
380   //Signals
381   Dali::RenderTask::RenderTaskSignalType  mSignalFinished; ///< Signal emmited when the render task has been processed.
382 };
383
384 } // namespace Internal
385
386 // Helpers for public-api forwarding methods
387
388 inline Internal::RenderTask& GetImplementation(Dali::RenderTask& task)
389 {
390   DALI_ASSERT_ALWAYS(task && "RenderTask handle is empty");
391
392   BaseObject& handle = task.GetBaseObject();
393
394   return static_cast<Internal::RenderTask&>(handle);
395 }
396
397 inline const Internal::RenderTask& GetImplementation(const Dali::RenderTask& task)
398 {
399   DALI_ASSERT_ALWAYS(task && "RenderTask handle is empty");
400
401   const BaseObject& handle = task.GetBaseObject();
402
403   return static_cast<const Internal::RenderTask&>(handle);
404 }
405
406 } // namespace Dali
407
408 #endif // DALI_INTERNAL_RENDER_TASK_H