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