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