Fix capture issue of to capture sub-scene.
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / capture-impl.h
1 #ifndef DALI_INTERNAL_CAPTURE_H
2 #define DALI_INTERNAL_CAPTURE_H
3
4 /*
5  * Copyright (c) 2020 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 // EXTERNAL INCLUDES
22 #include <string>
23 #include <memory>
24 #include <dali/public-api/object/ref-object.h>
25 #include <dali/public-api/object/base-object.h>
26 #include <dali/public-api/render-tasks/render-task.h>
27 #include <dali/public-api/rendering/texture.h>
28 #include <dali/public-api/rendering/frame-buffer.h>
29
30 // INTERNAL INCLUDES
31 #include <dali/public-api/dali-adaptor-common.h>
32 #include <dali/public-api/capture/capture.h>
33 #include <dali/public-api/adaptor-framework/native-image-source.h>
34 #include <dali/public-api/adaptor-framework/timer.h>
35
36 namespace Dali
37 {
38
39 namespace Internal
40 {
41
42 namespace Adaptor
43 {
44
45 class Capture;
46 typedef IntrusivePtr<Capture> CapturePtr;
47
48 class Capture : public BaseObject, public ConnectionTracker
49 {
50 public:
51
52   static constexpr uint32_t DEFAULT_QUALITY = 100;
53
54   /**
55    * @brief Constructor.
56    */
57   Capture();
58
59   Capture( Dali::CameraActor cameraActor );
60
61   /**
62    * @copydoc Dali::Capture::New
63    */
64   static CapturePtr New();
65
66   /**
67    * @copydoc Dali::Capture::New
68    */
69   static CapturePtr New( Dali::CameraActor cameraActor );
70
71   /**
72    * @copydoc Dali::Capture::Start
73    */
74   void Start( Dali::Actor source, const Dali::Vector2& position, const Dali::Vector2& size, const std::string &path, const Dali::Vector4& clearColor, const uint32_t quality );
75
76   /**
77    * @copydoc Dali::Capture::Start
78    */
79   void Start( Dali::Actor source, const Dali::Vector2& position, const Dali::Vector2& size, const std::string &path, const Dali::Vector4& clearColor );
80
81   /**
82    * @copydoc Dali::Capture::SetImageQuality
83    */
84   void SetImageQuality( uint32_t quality );
85
86   /**
87    * @copydoc Dali::Capture::GetNativeImageSource
88    */
89   Dali::NativeImageSourcePtr GetNativeImageSource() const;
90
91   /**
92    * @copydoc Dali::Capture::FinishedSignal
93    */
94   Dali::Capture::CaptureFinishedSignalType& FinishedSignal();
95
96 protected:
97
98   /**
99    * @brief A reference counted object may only be deleted by calling Unreference()
100    */
101   virtual ~Capture();
102
103 private:
104   /**
105    * @brief Create native image source.
106    */
107   void CreateNativeImageSource( const Dali::Vector2& size );
108
109   /**
110    * @brief Delete native image source.
111    */
112   void DeleteNativeImageSource();
113
114   /**
115    * @brief Query whether native image source is created or not.
116    *
117    * @return True is native image source is created.
118    */
119   bool IsNativeImageSourceCreated();
120
121   /**
122    * @brief Create frame buffer.
123    */
124   void CreateFrameBuffer();
125
126   /**
127    * @brief Delete frame buffer.
128    */
129   void DeleteFrameBuffer();
130
131   /**
132    * @brief Query whether frame buffer is created or not.
133    *
134    * @return True is frame buffer is created.
135    */
136   bool IsFrameBufferCreated();
137
138   /**
139    * @brief Setup render task.
140    *
141    * @param[in] position top-left position of area to be captured
142    *            this position is defined in the window.
143    * @param[in] size two dimensional size of area to be captured
144    * @param[in] source sub-scene tree to be captured.
145    * @param[in] clearColor background color
146    */
147   void SetupRenderTask( const Dali::Vector2& position, const Dali::Vector2& size, Dali::Actor source, const Dali::Vector4& clearColor );
148
149   /**
150    * @brief Unset render task.
151    */
152   void UnsetRenderTask();
153
154   /**
155    * @brief Query whether render task is setup or not.
156    *
157    * @return True is render task is setup.
158    */
159   bool IsRenderTaskSetup();
160
161   /**
162    * @brief Setup resources for capture.
163    *
164    * @param[in] position top-left position of area to be captured
165    *            this position is defined in the window.
166    * @param[in] size two dimensional size of area to be captured
167    * @param[in] clearColor color to clear background surface.
168    * @param[in] source sub-scene tree to be captured.
169    */
170   void SetupResources( const Dali::Vector2& position, const Dali::Vector2& size, const Dali::Vector4& clearColor, Dali::Actor source );
171
172   /**
173    * @brief Unset resources for capture.
174    */
175   void UnsetResources();
176
177   /**
178    * @brief Callback when render is finished.
179    *
180    * @param[in] task is used for capture.
181    */
182   void OnRenderFinished( Dali::RenderTask& task );
183
184   /**
185    * @brief Callback when timer is finished.
186    *
187    * @return True is timer start again.
188    */
189   bool OnTimeOut();
190
191   /**
192    * @brief Save framebuffer.
193    *
194    * @return True is success to save, false is fail.
195    */
196   bool SaveFile();
197
198 private:
199
200   // Undefined
201   Capture( const Capture& );
202
203   // Undefined
204   Capture& operator=( const Capture& rhs );
205
206 private:
207   uint32_t                                    mQuality;
208   Dali::Texture                               mNativeTexture;
209   Dali::FrameBuffer                           mFrameBuffer;
210   Dali::RenderTask                            mRenderTask;
211   Dali::Actor                                 mSource;
212   Dali::CameraActor                           mCameraActor;
213   Dali::Timer                                 mTimer;           ///< For timeout.
214   Dali::Capture::CaptureFinishedSignalType    mFinishedSignal;
215   std::string                                 mPath;
216   Dali::NativeImageSourcePtr                  mNativeImageSourcePtr;  ///< pointer to surface image
217   bool                                        mFileSave;
218 };
219
220 }  // End of namespace Adaptor
221 }  // End of namespace Internal
222
223 // Helpers for public-api forwarding methods
224
225 inline Internal::Adaptor::Capture& GetImpl( Dali::Capture& captureWorker)
226 {
227   DALI_ASSERT_ALWAYS( captureWorker && "Capture handle is empty" );
228
229   BaseObject& handle = captureWorker.GetBaseObject();
230
231   return static_cast< Internal::Adaptor::Capture& >( handle );
232 }
233
234 inline const Internal::Adaptor::Capture& GetImpl( const Dali::Capture& captureWorker )
235 {
236   DALI_ASSERT_ALWAYS( captureWorker && "Capture handle is empty" );
237
238   const BaseObject& handle = captureWorker.GetBaseObject();
239
240   return static_cast< const Internal::Adaptor::Capture& >( handle );
241 }
242
243 }  // End of namespace Dali
244
245 #endif // DALI_INTERNAL_CAPTURE_H