[Tizen] Return captured results with PixelBuffer
[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/frame-buffer.h>
28
29 // INTERNAL INCLUDES
30 #include <dali/public-api/dali-adaptor-common.h>
31 #include <dali/public-api/capture/capture.h>
32 #include <dali/public-api/adaptor-framework/native-image-source.h>
33 #include <dali/public-api/adaptor-framework/timer.h>
34 #include <dali/devel-api/adaptor-framework/pixel-buffer.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::GetTexture
93    */
94   Dali::Texture GetTexture();
95
96   /**
97    * @copydoc Dali::Capture::GetCapturedBuffer
98    */
99   Dali::Devel::PixelBuffer GetCapturedBuffer();
100
101   /**
102    * @copydoc Dali::Capture::FinishedSignal
103    */
104   Dali::Capture::CaptureFinishedSignalType& FinishedSignal();
105
106 protected:
107
108   /**
109    * @brief A reference counted object may only be deleted by calling Unreference()
110    */
111   virtual ~Capture();
112
113 private:
114   /**
115    * @brief Create native image source.
116    */
117   void CreateTexture( const Dali::Vector2& size );
118
119   /**
120    * @brief Delete native image source.
121    */
122   void DeleteNativeImageSource();
123
124   /**
125    * @brief Create frame buffer.
126    */
127   void CreateFrameBuffer();
128
129   /**
130    * @brief Delete frame buffer.
131    */
132   void DeleteFrameBuffer();
133
134   /**
135    * @brief Query whether frame buffer is created or not.
136    *
137    * @return True is frame buffer is created.
138    */
139   bool IsFrameBufferCreated();
140
141   /**
142    * @brief Setup render task.
143    *
144    * @param[in] position top-left position of area to be captured
145    *            this position is defined in the window.
146    * @param[in] size two dimensional size of area to be captured
147    * @param[in] source sub-scene tree to be captured.
148    * @param[in] clearColor background color
149    */
150   void SetupRenderTask( const Dali::Vector2& position, const Dali::Vector2& size, Dali::Actor source, const Dali::Vector4& clearColor );
151
152   /**
153    * @brief Unset render task.
154    */
155   void UnsetRenderTask();
156
157   /**
158    * @brief Query whether render task is setup or not.
159    *
160    * @return True is render task is setup.
161    */
162   bool IsRenderTaskSetup();
163
164   /**
165    * @brief Setup resources for capture.
166    *
167    * @param[in] position top-left position of area to be captured
168    *            this position is defined in the window.
169    * @param[in] size two dimensional size of area to be captured
170    * @param[in] clearColor color to clear background surface.
171    * @param[in] source sub-scene tree to be captured.
172    */
173   void SetupResources( const Dali::Vector2& position, const Dali::Vector2& size, const Dali::Vector4& clearColor, Dali::Actor source );
174
175   /**
176    * @brief Unset resources for capture.
177    */
178   void UnsetResources();
179
180   /**
181    * @brief Callback when render is finished.
182    *
183    * @param[in] task is used for capture.
184    */
185   void OnRenderFinished( Dali::RenderTask& task );
186
187   /**
188    * @brief Callback when timer is finished.
189    *
190    * @return True is timer start again.
191    */
192   bool OnTimeOut();
193
194   /**
195    * @brief Save framebuffer.
196    *
197    * @return True is success to save, false is fail.
198    */
199   bool SaveFile();
200
201 private:
202
203   // Undefined
204   Capture( const Capture& );
205
206   // Undefined
207   Capture& operator=( const Capture& rhs );
208
209 private:
210   uint32_t                                    mQuality;
211   Dali::Texture                               mTexture;
212   Dali::FrameBuffer                           mFrameBuffer;
213   Dali::RenderTask                            mRenderTask;
214   Dali::Actor                                 mSource;
215   Dali::CameraActor                           mCameraActor;
216   Dali::Timer                                 mTimer; ///< For timeout.
217   Dali::Capture::CaptureFinishedSignalType    mFinishedSignal;
218   std::string                                 mPath;
219   Dali::NativeImageSourcePtr                  mNativeImageSourcePtr; ///< pointer to surface image
220   Dali::Devel::PixelBuffer                    mPixelBuffer;
221   bool                                        mFileSave;
222   bool                                        mIsNativeImageSourcePossible;
223 };
224
225 }  // End of namespace Adaptor
226 }  // End of namespace Internal
227
228 // Helpers for public-api forwarding methods
229
230 inline Internal::Adaptor::Capture& GetImpl( Dali::Capture& captureWorker)
231 {
232   DALI_ASSERT_ALWAYS( captureWorker && "Capture handle is empty" );
233
234   BaseObject& handle = captureWorker.GetBaseObject();
235
236   return static_cast< Internal::Adaptor::Capture& >( handle );
237 }
238
239 inline const Internal::Adaptor::Capture& GetImpl( const Dali::Capture& captureWorker )
240 {
241   DALI_ASSERT_ALWAYS( captureWorker && "Capture handle is empty" );
242
243   const BaseObject& handle = captureWorker.GetBaseObject();
244
245   return static_cast< const Internal::Adaptor::Capture& >( handle );
246 }
247
248 }  // End of namespace Dali
249
250 #endif // DALI_INTERNAL_CAPTURE_H