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/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>
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 return mNativeImageSourcePtr;
115 Dali::Capture::CaptureFinishedSignalType& Capture::FinishedSignal()
117 return mFinishedSignal;
120 void Capture::CreateNativeImageSource( const Vector2& size )
122 Dali::Adaptor& adaptor = Dali::Adaptor::Get();
124 DALI_ASSERT_ALWAYS(adaptor.IsAvailable() && "Dali::Adaptor is not available.");
126 DALI_ASSERT_ALWAYS(!mNativeImageSourcePtr && "NativeImageSource is already created.");
128 // create the NativeImageSource object with our surface
129 mNativeImageSourcePtr = Dali::NativeImageSource::New( size.width, size.height, Dali::NativeImageSource::COLOR_DEPTH_DEFAULT );
132 void Capture::DeleteNativeImageSource()
134 mNativeImageSourcePtr.Reset();
137 bool Capture::IsNativeImageSourceCreated()
139 return mNativeImageSourcePtr;
142 void Capture::CreateFrameBuffer()
144 DALI_ASSERT_ALWAYS(mNativeImageSourcePtr && "NativeImageSource is NULL.");
146 DALI_ASSERT_ALWAYS(!mFrameBuffer && "FrameBuffer is already created.");
148 mNativeTexture = Dali::Texture::New( *mNativeImageSourcePtr );
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 );
156 void Capture::DeleteFrameBuffer()
158 DALI_ASSERT_ALWAYS(mFrameBuffer && "FrameBuffer is NULL.");
160 mFrameBuffer.Reset();
161 mNativeTexture.Reset();
164 bool Capture::IsFrameBufferCreated()
169 void Capture::SetupRenderTask( Dali::Actor source, const Dali::Vector4& clearColor )
171 DALI_ASSERT_ALWAYS(source && "Source is empty.");
175 // Check the original parent about source.
176 mParent = mSource.GetParent();
178 Dali::Stage stage = Dali::Stage::GetCurrent();
179 Dali::Size stageSize = stage.GetSize();
181 // Add to stage for rendering the source. If source isn't on the stage then it never be rendered.
182 stage.Add( mSource );
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 );
191 stage.Add( mCameraActor );
193 DALI_ASSERT_ALWAYS(mFrameBuffer && "Framebuffer is NULL.");
195 DALI_ASSERT_ALWAYS(!mRenderTask && "RenderTask is already created.");
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 );
210 mTimer = Dali::Timer::New( TIME_OUT_DURATION );
211 mTimer.TickSignal().Connect( this, &Capture::OnTimeOut );
215 void Capture::UnsetRenderTask()
217 DALI_ASSERT_ALWAYS(mCameraActor && "CameraActor is NULL.");
221 // Restore the parent of source.
222 mParent.Add( mSource );
234 mCameraActor.Unparent();
235 mCameraActor.Reset();
237 DALI_ASSERT_ALWAYS( mRenderTask && "RenderTask is NULL." );
239 Dali::RenderTaskList taskList = Dali::Stage::GetCurrent().GetRenderTaskList();
240 taskList.RemoveTask( mRenderTask );
244 bool Capture::IsRenderTaskSetup()
246 return mCameraActor && mRenderTask;
249 void Capture::SetupResources( const Dali::Vector2& size, const Dali::Vector4& clearColor, Dali::Actor source )
251 CreateNativeImageSource( size );
255 SetupRenderTask( source, clearColor );
258 void Capture::UnsetResources()
260 if( IsRenderTaskSetup() )
265 if( IsFrameBufferCreated() )
271 void Capture::OnRenderFinished( Dali::RenderTask& task )
273 Dali::Capture::FinishState state = Dali::Capture::FinishState::SUCCEEDED;
281 state = Dali::Capture::FinishState::FAILED;
282 DALI_LOG_ERROR( "Fail to Capture Path[%s]", mPath.c_str() );
286 Dali::Capture handle( this );
287 mFinishedSignal.Emit( handle, state );
291 // Decrease the reference count forcely. It is increased at Start().
295 bool Capture::OnTimeOut()
297 Dali::Capture::FinishState state = Dali::Capture::FinishState::FAILED;
299 Dali::Capture handle( this );
300 mFinishedSignal.Emit( handle, state );
304 // Decrease the reference count forcely. It is increased at Start().
310 bool Capture::SaveFile()
312 DALI_ASSERT_ALWAYS(mNativeImageSourcePtr && "mNativeImageSourcePtr is NULL");
314 return Dali::DevelNativeImageSource::EncodeToFile( *mNativeImageSourcePtr, mPath, mQuality );
317 } // End of namespace Adaptor
319 } // End of namespace Internal
321 } // End of namespace Dali