2 * Copyright (c) 2019 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>
34 unsigned int TIME_OUT_DURATION = 1000;
49 mNativeImageSourcePtr( NULL )
53 Capture::Capture( Dali::CameraActor cameraActor )
54 : mCameraActor( cameraActor ),
57 mNativeImageSourcePtr( NULL )
63 DeleteNativeImageSource();
66 CapturePtr Capture::New()
68 CapturePtr pWorker = new Capture();
73 CapturePtr Capture::New( Dali::CameraActor cameraActor )
75 CapturePtr pWorker = new Capture( cameraActor );
80 void Capture::Start( Dali::Actor source, const Dali::Vector2& size, const std::string &path, const Dali::Vector4& clearColor )
82 DALI_ASSERT_ALWAYS(path.size() > 4 && "Path is invalid.");
84 // Increase the reference count focely to avoid application mistake.
89 DALI_ASSERT_ALWAYS(source && "Source is NULL.");
92 SetupResources( size, clearColor, source );
95 Dali::Capture::CaptureFinishedSignalType& Capture::FinishedSignal()
97 return mFinishedSignal;
100 void Capture::CreateNativeImageSource( const Vector2& size )
102 Dali::Adaptor& adaptor = Dali::Adaptor::Get();
104 DALI_ASSERT_ALWAYS(adaptor.IsAvailable() && "Dali::Adaptor is not available.");
106 DALI_ASSERT_ALWAYS(!mNativeImageSourcePtr && "NativeImageSource is already created.");
108 // create the NativeImageSource object with our surface
109 mNativeImageSourcePtr = Dali::NativeImageSource::New( size.width, size.height, Dali::NativeImageSource::COLOR_DEPTH_DEFAULT );
112 void Capture::DeleteNativeImageSource()
114 DALI_ASSERT_ALWAYS(mNativeImageSourcePtr && "mNativeImageSource is NULL.");
116 mNativeImageSourcePtr.Reset();
119 bool Capture::IsNativeImageSourceCreated()
121 return mNativeImageSourcePtr;
124 void Capture::CreateFrameBuffer()
126 DALI_ASSERT_ALWAYS(mNativeImageSourcePtr && "NativeImageSource is NULL.");
128 DALI_ASSERT_ALWAYS(!mFrameBuffer && "FrameBuffer is already created.");
130 mNativeTexture = Dali::Texture::New( *mNativeImageSourcePtr );
132 // Create a FrameBuffer object with depth attachments.
133 mFrameBuffer = Dali::FrameBuffer::New( mNativeTexture.GetWidth(), mNativeTexture.GetHeight(), Dali::FrameBuffer::Attachment::DEPTH );
134 // Add a color attachment to the FrameBuffer object.
135 mFrameBuffer.AttachColorTexture( mNativeTexture );
138 void Capture::DeleteFrameBuffer()
140 DALI_ASSERT_ALWAYS(mFrameBuffer && "FrameBuffer is NULL.");
142 mFrameBuffer.Reset();
143 mNativeTexture.Reset();
146 bool Capture::IsFrameBufferCreated()
151 void Capture::SetupRenderTask( Dali::Actor source, const Dali::Vector4& clearColor )
153 DALI_ASSERT_ALWAYS(source && "Source is empty.");
157 // Check the original parent about source.
158 mParent = mSource.GetParent();
160 Dali::Stage stage = Dali::Stage::GetCurrent();
161 Dali::Size stageSize = stage.GetSize();
163 // Add to stage for rendering the source. If source isn't on the stage then it never be rendered.
164 stage.Add( mSource );
168 mCameraActor = Dali::CameraActor::New( stageSize );
169 mCameraActor.SetParentOrigin( ParentOrigin::CENTER );
170 mCameraActor.SetAnchorPoint( AnchorPoint::CENTER );
173 stage.Add( mCameraActor );
175 DALI_ASSERT_ALWAYS(mFrameBuffer && "Framebuffer is NULL.");
177 DALI_ASSERT_ALWAYS(!mRenderTask && "RenderTask is already created.");
179 Dali::RenderTaskList taskList = stage.GetRenderTaskList();
180 mRenderTask = taskList.CreateTask();
181 mRenderTask.SetRefreshRate( Dali::RenderTask::REFRESH_ONCE );
182 mRenderTask.SetSourceActor( source );
183 mRenderTask.SetCameraActor( mCameraActor );
184 mRenderTask.SetScreenToFrameBufferFunction( Dali::RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
185 mRenderTask.SetFrameBuffer( mFrameBuffer );
186 mRenderTask.SetClearColor( clearColor );
187 mRenderTask.SetClearEnabled( true );
188 mRenderTask.SetProperty( Dali::RenderTask::Property::REQUIRES_SYNC, true );
189 mRenderTask.FinishedSignal().Connect( this, &Capture::OnRenderFinished );
190 mRenderTask.GetCameraActor().SetInvertYAxis( true );
192 mTimer = Dali::Timer::New( TIME_OUT_DURATION );
193 mTimer.TickSignal().Connect( this, &Capture::OnTimeOut );
197 void Capture::UnsetRenderTask()
199 DALI_ASSERT_ALWAYS(mCameraActor && "CameraActor is NULL.");
203 // Restore the parent of source.
204 mParent.Add( mSource );
216 mCameraActor.Unparent();
217 mCameraActor.Reset();
219 DALI_ASSERT_ALWAYS( mRenderTask && "RenderTask is NULL." );
221 Dali::RenderTaskList taskList = Dali::Stage::GetCurrent().GetRenderTaskList();
222 taskList.RemoveTask( mRenderTask );
226 bool Capture::IsRenderTaskSetup()
228 return mCameraActor && mRenderTask;
231 void Capture::SetupResources( const Dali::Vector2& size, const Dali::Vector4& clearColor, Dali::Actor source )
233 CreateNativeImageSource( size );
237 SetupRenderTask( source, clearColor );
240 void Capture::UnsetResources()
242 if( IsRenderTaskSetup() )
247 if( IsFrameBufferCreated() )
253 void Capture::OnRenderFinished( Dali::RenderTask& task )
255 Dali::Capture::FinishState state = Dali::Capture::FinishState::SUCCEEDED;
261 state = Dali::Capture::FinishState::FAILED;
262 DALI_LOG_ERROR("Fail to Capture Path[%s]", mPath.c_str());
265 Dali::Capture handle( this );
266 mFinishedSignal.Emit( handle, state );
270 // Decrease the reference count forcely. It is increased at Start().
274 bool Capture::OnTimeOut()
276 Dali::Capture::FinishState state = Dali::Capture::FinishState::FAILED;
278 Dali::Capture handle( this );
279 mFinishedSignal.Emit( handle, state );
283 // Decrease the reference count forcely. It is increased at Start().
291 DALI_ASSERT_ALWAYS(mNativeImageSourcePtr && "mNativeImageSourcePtr is NULL");
293 return mNativeImageSourcePtr->EncodeToFile( mPath );
296 } // End of namespace Adaptor
298 } // End of namespace Internal
300 } // End of namespace Dali