2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/system/common/capture-impl.h>
24 #include <dali/public-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>
30 #include <dali/integration-api/adaptor-framework/adaptor.h>
31 #include <dali/devel-api/adaptor-framework/native-image-source-devel.h>
35 unsigned int TIME_OUT_DURATION = 1000;
48 : mQuality( DEFAULT_QUALITY ),
51 mNativeImageSourcePtr( NULL ),
56 Capture::Capture( Dali::CameraActor cameraActor )
57 : mQuality( DEFAULT_QUALITY ),
58 mCameraActor( cameraActor ),
61 mNativeImageSourcePtr( NULL ),
68 DeleteNativeImageSource();
71 CapturePtr Capture::New()
73 CapturePtr pWorker = new Capture();
78 CapturePtr Capture::New( Dali::CameraActor cameraActor )
80 CapturePtr pWorker = new Capture( cameraActor );
85 void Capture::Start( Dali::Actor source, const Dali::Vector2& size, const std::string &path, const Dali::Vector4& clearColor, const uint32_t quality )
88 Start( source, size, path, clearColor );
91 void Capture::Start( Dali::Actor source, const Dali::Vector2& size, const std::string &path, const Dali::Vector4& clearColor )
93 DALI_ASSERT_ALWAYS(path.size() > 4 && "Path is invalid.");
95 // Increase the reference count focely to avoid application mistake.
99 if( mPath.size() > 0 )
104 DALI_ASSERT_ALWAYS(source && "Source is NULL.");
107 SetupResources( size, clearColor, source );
110 Dali::NativeImageSourcePtr Capture::GetNativeImageSource() const
112 DALI_ASSERT_ALWAYS( mNativeImageSourcePtr && "mNativeImageSourcePtr is NULL.");
114 return mNativeImageSourcePtr;
117 Dali::Capture::CaptureFinishedSignalType& Capture::FinishedSignal()
119 return mFinishedSignal;
122 void Capture::CreateNativeImageSource( const Vector2& size )
124 Dali::Adaptor& adaptor = Dali::Adaptor::Get();
126 DALI_ASSERT_ALWAYS(adaptor.IsAvailable() && "Dali::Adaptor is not available.");
128 DALI_ASSERT_ALWAYS(!mNativeImageSourcePtr && "NativeImageSource is already created.");
130 // create the NativeImageSource object with our surface
131 mNativeImageSourcePtr = Dali::NativeImageSource::New( size.width, size.height, Dali::NativeImageSource::COLOR_DEPTH_DEFAULT );
134 void Capture::DeleteNativeImageSource()
136 DALI_ASSERT_ALWAYS(mNativeImageSourcePtr && "mNativeImageSource is NULL.");
138 mNativeImageSourcePtr.Reset();
141 bool Capture::IsNativeImageSourceCreated()
143 return mNativeImageSourcePtr;
146 void Capture::CreateFrameBuffer()
148 DALI_ASSERT_ALWAYS(mNativeImageSourcePtr && "NativeImageSource is NULL.");
150 DALI_ASSERT_ALWAYS(!mFrameBuffer && "FrameBuffer is already created.");
152 mNativeTexture = Dali::Texture::New( *mNativeImageSourcePtr );
154 // Create a FrameBuffer object with depth attachments.
155 mFrameBuffer = Dali::FrameBuffer::New( mNativeTexture.GetWidth(), mNativeTexture.GetHeight(), Dali::FrameBuffer::Attachment::DEPTH );
156 // Add a color attachment to the FrameBuffer object.
157 mFrameBuffer.AttachColorTexture( mNativeTexture );
160 void Capture::DeleteFrameBuffer()
162 DALI_ASSERT_ALWAYS(mFrameBuffer && "FrameBuffer is NULL.");
164 mFrameBuffer.Reset();
165 mNativeTexture.Reset();
168 bool Capture::IsFrameBufferCreated()
173 void Capture::SetupRenderTask( Dali::Actor source, const Dali::Vector4& clearColor )
175 DALI_ASSERT_ALWAYS(source && "Source is empty.");
179 // Check the original parent about source.
180 mParent = mSource.GetParent();
182 Dali::Stage stage = Dali::Stage::GetCurrent();
183 Dali::Size stageSize = stage.GetSize();
185 // Add to stage for rendering the source. If source isn't on the stage then it never be rendered.
186 stage.Add( mSource );
190 mCameraActor = Dali::CameraActor::New( stageSize );
191 mCameraActor.SetParentOrigin( ParentOrigin::CENTER );
192 mCameraActor.SetAnchorPoint( AnchorPoint::CENTER );
195 stage.Add( mCameraActor );
197 DALI_ASSERT_ALWAYS(mFrameBuffer && "Framebuffer is NULL.");
199 DALI_ASSERT_ALWAYS(!mRenderTask && "RenderTask is already created.");
201 Dali::RenderTaskList taskList = stage.GetRenderTaskList();
202 mRenderTask = taskList.CreateTask();
203 mRenderTask.SetRefreshRate( Dali::RenderTask::REFRESH_ONCE );
204 mRenderTask.SetSourceActor( source );
205 mRenderTask.SetCameraActor( mCameraActor );
206 mRenderTask.SetScreenToFrameBufferFunction( Dali::RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
207 mRenderTask.SetFrameBuffer( mFrameBuffer );
208 mRenderTask.SetClearColor( clearColor );
209 mRenderTask.SetClearEnabled( true );
210 mRenderTask.SetProperty( Dali::RenderTask::Property::REQUIRES_SYNC, true );
211 mRenderTask.FinishedSignal().Connect( this, &Capture::OnRenderFinished );
212 mRenderTask.GetCameraActor().SetInvertYAxis( true );
214 mTimer = Dali::Timer::New( TIME_OUT_DURATION );
215 mTimer.TickSignal().Connect( this, &Capture::OnTimeOut );
219 void Capture::UnsetRenderTask()
221 DALI_ASSERT_ALWAYS(mCameraActor && "CameraActor is NULL.");
225 // Restore the parent of source.
226 mParent.Add( mSource );
238 mCameraActor.Unparent();
239 mCameraActor.Reset();
241 DALI_ASSERT_ALWAYS( mRenderTask && "RenderTask is NULL." );
243 Dali::RenderTaskList taskList = Dali::Stage::GetCurrent().GetRenderTaskList();
244 taskList.RemoveTask( mRenderTask );
248 bool Capture::IsRenderTaskSetup()
250 return mCameraActor && mRenderTask;
253 void Capture::SetupResources( const Dali::Vector2& size, const Dali::Vector4& clearColor, Dali::Actor source )
255 CreateNativeImageSource( size );
259 SetupRenderTask( source, clearColor );
262 void Capture::UnsetResources()
264 if( IsRenderTaskSetup() )
269 if( IsFrameBufferCreated() )
275 void Capture::OnRenderFinished( Dali::RenderTask& task )
277 Dali::Capture::FinishState state = Dali::Capture::FinishState::SUCCEEDED;
285 state = Dali::Capture::FinishState::FAILED;
286 DALI_LOG_ERROR( "Fail to Capture Path[%s]", mPath.c_str() );
290 Dali::Capture handle( this );
291 mFinishedSignal.Emit( handle, state );
295 // Decrease the reference count forcely. It is increased at Start().
299 bool Capture::OnTimeOut()
301 Dali::Capture::FinishState state = Dali::Capture::FinishState::FAILED;
303 Dali::Capture handle( this );
304 mFinishedSignal.Emit( handle, state );
308 // Decrease the reference count forcely. It is increased at Start().
314 bool Capture::SaveFile()
316 DALI_ASSERT_ALWAYS(mNativeImageSourcePtr && "mNativeImageSourcePtr is NULL");
318 return Dali::DevelNativeImageSource::EncodeToFile( *mNativeImageSourcePtr, mPath, mQuality );
321 } // End of namespace Adaptor
323 } // End of namespace Internal
325 } // End of namespace Dali