11c29e90ba5cb8cf42c13ebb1ff527c25103cf74
[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& 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& size, const std::string &path, const Dali::Vector4& clearColor );
80
81   /**
82    * @copydoc Dali::Capture::GetNativeImageSource
83    */
84   Dali::NativeImageSourcePtr GetNativeImageSource() const;
85
86   /**
87    * @copydoc Dali::Capture::FinishedSignal
88    */
89   Dali::Capture::CaptureFinishedSignalType& FinishedSignal();
90
91 protected:
92
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] source is captured.
137    * @param[in] clearColor background color
138    */
139   void SetupRenderTask( Dali::Actor source, const Dali::Vector4& clearColor );
140
141   /**
142    * @brief Unset render task.
143    */
144   void UnsetRenderTask();
145
146   /**
147    * @brief Query whether render task is setup or not.
148    *
149    * @return True is render task is setup.
150    */
151   bool IsRenderTaskSetup();
152
153   /**
154    * @brief Setup resources for capture.
155    *
156    * @param[in] size is surface size.
157    * @param[in] clearColor is clear color of surface.
158    * @param[in] source is captured.
159    */
160   void SetupResources( const Dali::Vector2& size, const Dali::Vector4& clearColor, Dali::Actor source );
161
162   /**
163    * @brief Unset resources for capture.
164    */
165   void UnsetResources();
166
167   /**
168    * @brief Callback when render is finished.
169    *
170    * @param[in] task is used for capture.
171    */
172   void OnRenderFinished( Dali::RenderTask& task );
173
174   /**
175    * @brief Callback when timer is finished.
176    *
177    * @return True is timer start again.
178    */
179   bool OnTimeOut();
180
181   /**
182    * @brief Save framebuffer.
183    *
184    * @return True is success to save, false is fail.
185    */
186   bool SaveFile();
187
188 private:
189
190   // Undefined
191   Capture( const Capture& );
192
193   // Undefined
194   Capture& operator=( const Capture& rhs );
195
196 private:
197   uint32_t                                    mQuality;
198   Dali::Texture                               mNativeTexture;
199   Dali::FrameBuffer                           mFrameBuffer;
200   Dali::RenderTask                            mRenderTask;
201   Dali::Actor                                 mParent;
202   Dali::Actor                                 mSource;
203   Dali::CameraActor                           mCameraActor;
204   Dali::Timer                                 mTimer;           ///< For timeout.
205   Dali::Capture::CaptureFinishedSignalType    mFinishedSignal;
206   std::string                                 mPath;
207   Dali::NativeImageSourcePtr                  mNativeImageSourcePtr;  ///< pointer to surface image
208   bool                                        mFileSave;
209 };
210
211 }  // End of namespace Adaptor
212 }  // End of namespace Internal
213
214 // Helpers for public-api forwarding methods
215
216 inline Internal::Adaptor::Capture& GetImpl( Dali::Capture& captureWorker)
217 {
218   DALI_ASSERT_ALWAYS( captureWorker && "Capture handle is empty" );
219
220   BaseObject& handle = captureWorker.GetBaseObject();
221
222   return static_cast< Internal::Adaptor::Capture& >( handle );
223 }
224
225 inline const Internal::Adaptor::Capture& GetImpl( const Dali::Capture& captureWorker )
226 {
227   DALI_ASSERT_ALWAYS( captureWorker && "Capture handle is empty" );
228
229   const BaseObject& handle = captureWorker.GetBaseObject();
230
231   return static_cast< const Internal::Adaptor::Capture& >( handle );
232 }
233
234 }  // End of namespace Dali
235
236 #endif // DALI_INTERNAL_CAPTURE_H