49dae7581980b4cd1eeedf52c8ab114d774a1129
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / capture-impl.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/system/common/capture-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <fstream>
23 #include <string.h>
24 #include <dali/devel-api/common/stage.h>
25 #include <dali/public-api/common/vector-wrapper.h>
26 #include <dali/public-api/render-tasks/render-task-list.h>
27 #include <dali/integration-api/debug.h>
28
29 // INTERNAL INCLUDES
30 #include <dali/integration-api/adaptor-framework/adaptor.h>
31 #include <dali/devel-api/adaptor-framework/native-image-source-devel.h>
32
33 namespace
34 {
35 unsigned int TIME_OUT_DURATION = 1000;
36 }
37
38 namespace Dali
39 {
40
41 namespace Internal
42 {
43
44 namespace Adaptor
45 {
46
47 Capture::Capture()
48 : mQuality( DEFAULT_QUALITY ),
49   mTimer(),
50   mPath(),
51   mNativeImageSourcePtr( NULL ),
52   mFileSave( false )
53 {
54 }
55
56 Capture::Capture( Dali::CameraActor cameraActor )
57 : mQuality( DEFAULT_QUALITY ),
58   mCameraActor( cameraActor ),
59   mTimer(),
60   mPath(),
61   mNativeImageSourcePtr( NULL ),
62   mFileSave( false )
63 {
64 }
65
66 Capture::~Capture()
67 {
68   DeleteNativeImageSource();
69 }
70
71 CapturePtr Capture::New()
72 {
73   CapturePtr pWorker = new Capture();
74
75   return pWorker;
76 }
77
78 CapturePtr Capture::New( Dali::CameraActor cameraActor )
79 {
80   CapturePtr pWorker = new Capture( cameraActor );
81
82   return pWorker;
83 }
84
85 void Capture::Start( Dali::Actor source, const Dali::Vector2& size, const std::string &path, const Dali::Vector4& clearColor, const uint32_t quality )
86 {
87   mQuality = quality;
88   Start( source, size, path, clearColor );
89 }
90
91 void Capture::Start( Dali::Actor source, const Dali::Vector2& size, const std::string &path, const Dali::Vector4& clearColor )
92 {
93   DALI_ASSERT_ALWAYS(path.size() > 4 && "Path is invalid.");
94
95   // Increase the reference count focely to avoid application mistake.
96   Reference();
97
98   mPath = path;
99   if( mPath.size() > 0 )
100   {
101     mFileSave = true;
102   }
103
104   DALI_ASSERT_ALWAYS(source && "Source is NULL.");
105
106   UnsetResources();
107   SetupResources( size, clearColor, source );
108 }
109
110 Dali::NativeImageSourcePtr Capture::GetNativeImageSource() const
111 {
112   return mNativeImageSourcePtr;
113 }
114
115 Dali::Capture::CaptureFinishedSignalType& Capture::FinishedSignal()
116 {
117   return mFinishedSignal;
118 }
119
120 void Capture::CreateNativeImageSource( const Vector2& size )
121 {
122   Dali::Adaptor& adaptor = Dali::Adaptor::Get();
123
124   DALI_ASSERT_ALWAYS(adaptor.IsAvailable() && "Dali::Adaptor is not available.");
125
126   DALI_ASSERT_ALWAYS(!mNativeImageSourcePtr && "NativeImageSource is already created.");
127
128   // create the NativeImageSource object with our surface
129   mNativeImageSourcePtr = Dali::NativeImageSource::New( size.width, size.height, Dali::NativeImageSource::COLOR_DEPTH_DEFAULT );
130 }
131
132 void Capture::DeleteNativeImageSource()
133 {
134   mNativeImageSourcePtr.Reset();
135 }
136
137 bool Capture::IsNativeImageSourceCreated()
138 {
139   return mNativeImageSourcePtr;
140 }
141
142 void Capture::CreateFrameBuffer()
143 {
144   DALI_ASSERT_ALWAYS(mNativeImageSourcePtr && "NativeImageSource is NULL.");
145
146   DALI_ASSERT_ALWAYS(!mFrameBuffer && "FrameBuffer is already created.");
147
148   mNativeTexture = Dali::Texture::New( *mNativeImageSourcePtr );
149
150   // Create a FrameBuffer object with depth attachments.
151   mFrameBuffer = Dali::FrameBuffer::New( mNativeTexture.GetWidth(), mNativeTexture.GetHeight(), Dali::FrameBuffer::Attachment::DEPTH );
152   // Add a color attachment to the FrameBuffer object.
153   mFrameBuffer.AttachColorTexture( mNativeTexture );
154 }
155
156 void Capture::DeleteFrameBuffer()
157 {
158   DALI_ASSERT_ALWAYS(mFrameBuffer && "FrameBuffer is NULL.");
159
160   mFrameBuffer.Reset();
161   mNativeTexture.Reset();
162 }
163
164 bool Capture::IsFrameBufferCreated()
165 {
166   return mFrameBuffer;
167 }
168
169 void Capture::SetupRenderTask( Dali::Actor source, const Dali::Vector4& clearColor )
170 {
171   DALI_ASSERT_ALWAYS(source && "Source is empty.");
172
173   mSource = source;
174
175   // Check the original parent about source.
176   mParent = mSource.GetParent();
177
178   Dali::Stage stage = Dali::Stage::GetCurrent();
179   Dali::Size stageSize = stage.GetSize();
180
181   // Add to stage for rendering the source. If source isn't on the stage then it never be rendered.
182   stage.Add( mSource );
183
184   if( !mCameraActor )
185   {
186     mCameraActor = Dali::CameraActor::New( stageSize );
187     mCameraActor.SetProperty( Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
188     mCameraActor.SetProperty( Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
189   }
190
191   stage.Add( mCameraActor );
192
193   DALI_ASSERT_ALWAYS(mFrameBuffer && "Framebuffer is NULL.");
194
195   DALI_ASSERT_ALWAYS(!mRenderTask && "RenderTask is already created.");
196
197   Dali::RenderTaskList taskList = stage.GetRenderTaskList();
198   mRenderTask = taskList.CreateTask();
199   mRenderTask.SetRefreshRate( Dali::RenderTask::REFRESH_ONCE );
200   mRenderTask.SetSourceActor( source );
201   mRenderTask.SetCameraActor( mCameraActor );
202   mRenderTask.SetScreenToFrameBufferFunction( Dali::RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
203   mRenderTask.SetFrameBuffer( mFrameBuffer );
204   mRenderTask.SetClearColor( clearColor );
205   mRenderTask.SetClearEnabled( true );
206   mRenderTask.SetProperty( Dali::RenderTask::Property::REQUIRES_SYNC, true );
207   mRenderTask.FinishedSignal().Connect( this, &Capture::OnRenderFinished );
208   mRenderTask.GetCameraActor().SetInvertYAxis( true );
209
210   mTimer = Dali::Timer::New( TIME_OUT_DURATION );
211   mTimer.TickSignal().Connect( this, &Capture::OnTimeOut );
212   mTimer.Start();
213 }
214
215 void Capture::UnsetRenderTask()
216 {
217   DALI_ASSERT_ALWAYS(mCameraActor && "CameraActor is NULL.");
218
219   if( mParent )
220   {
221     // Restore the parent of source.
222     mParent.Add( mSource );
223     mParent.Reset();
224   }
225   else
226   {
227     mSource.Unparent();
228   }
229
230   mSource.Reset();
231
232   mTimer.Reset();
233
234   mCameraActor.Unparent();
235   mCameraActor.Reset();
236
237   DALI_ASSERT_ALWAYS( mRenderTask && "RenderTask is NULL." );
238
239   Dali::RenderTaskList taskList = Dali::Stage::GetCurrent().GetRenderTaskList();
240   taskList.RemoveTask( mRenderTask );
241   mRenderTask.Reset();
242 }
243
244 bool Capture::IsRenderTaskSetup()
245 {
246   return mCameraActor && mRenderTask;
247 }
248
249 void Capture::SetupResources( const Dali::Vector2& size, const Dali::Vector4& clearColor, Dali::Actor source )
250 {
251   CreateNativeImageSource( size );
252
253   CreateFrameBuffer();
254
255   SetupRenderTask( source, clearColor );
256 }
257
258 void Capture::UnsetResources()
259 {
260   if( IsRenderTaskSetup() )
261   {
262     UnsetRenderTask();
263   }
264
265   if( IsFrameBufferCreated() )
266   {
267     DeleteFrameBuffer();
268   }
269 }
270
271 void Capture::OnRenderFinished( Dali::RenderTask& task )
272 {
273   Dali::Capture::FinishState state = Dali::Capture::FinishState::SUCCEEDED;
274
275   mTimer.Stop();
276
277   if( mFileSave )
278   {
279     if( !SaveFile() )
280     {
281       state = Dali::Capture::FinishState::FAILED;
282       DALI_LOG_ERROR( "Fail to Capture Path[%s]", mPath.c_str() );
283     }
284   }
285
286   Dali::Capture handle( this );
287   mFinishedSignal.Emit( handle, state );
288
289   UnsetResources();
290
291   // Decrease the reference count forcely. It is increased at Start().
292   Unreference();
293 }
294
295 bool Capture::OnTimeOut()
296 {
297   Dali::Capture::FinishState state = Dali::Capture::FinishState::FAILED;
298
299   Dali::Capture handle( this );
300   mFinishedSignal.Emit( handle, state );
301
302   UnsetResources();
303
304   // Decrease the reference count forcely. It is increased at Start().
305   Unreference();
306
307   return false;
308 }
309
310 bool Capture::SaveFile()
311 {
312   DALI_ASSERT_ALWAYS(mNativeImageSourcePtr && "mNativeImageSourcePtr is NULL");
313
314   return Dali::DevelNativeImageSource::EncodeToFile( *mNativeImageSourcePtr, mPath, mQuality );
315 }
316
317 }  // End of namespace Adaptor
318
319 }  // End of namespace Internal
320
321 }  // End of namespace Dali