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