2 * Copyright (c) 2014 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 <capture-impl.h>
22 #include <dali/public-api/common/vector-wrapper.h>
23 #include <dali/public-api/render-tasks/render-task-list.h>
24 #include <dali/public-api/common/stage.h>
25 #include <dali/integration-api/debug.h>
34 unsigned int TIME_OUT_DURATION = 1000;
49 mNativeImageSourcePtr( NULL ),
54 Capture::Capture( Dali::CameraActor cameraActor )
55 : mCameraActor( cameraActor ),
58 mNativeImageSourcePtr( NULL ),
67 CapturePtr Capture::New()
69 CapturePtr pWorker = new Capture();
74 CapturePtr Capture::New( Dali::CameraActor cameraActor )
76 CapturePtr pWorker = new Capture( cameraActor );
81 void Capture::Start( Dali::Actor source, const Dali::Vector2& size, const std::string &path, const Dali::Vector4& clearColor )
83 DALI_ASSERT_ALWAYS(path.size() > 4 && "Path is invalid.");
85 // Increase the reference count focely to avoid application mistake.
90 DALI_ASSERT_ALWAYS(source && "Source is NULL.");
93 SetupResources( size, clearColor, source );
96 Dali::Capture::CaptureFinishedSignalType& Capture::FinishedSignal()
98 return mFinishedSignal;
101 void Capture::CreateSurface( const Vector2& size )
103 DALI_ASSERT_ALWAYS(!mTbmSurface && "mTbmSurface is already created.");
105 mTbmSurface = tbm_surface_create( size.width, size.height, TBM_FORMAT_RGBA8888 );
108 void Capture::DeleteSurface()
110 DALI_ASSERT_ALWAYS(mTbmSurface && "mTbmSurface is empty.");
112 tbm_surface_destroy( mTbmSurface );
116 void Capture::ClearSurface( const Vector2& size )
118 DALI_ASSERT_ALWAYS(mTbmSurface && "mTbmSurface is empty.");
120 tbm_surface_info_s surface_info;
122 if( tbm_surface_map( mTbmSurface, TBM_SURF_OPTION_WRITE, &surface_info ) == TBM_SURFACE_ERROR_NONE )
124 //DALI_ASSERT_ALWAYS(surface_info.bpp == 32 && "unsupported tbm format");
126 unsigned char* ptr = surface_info.planes[0].ptr;
127 memset( ptr, 0, surface_info.size ); // TODO: support color
129 if( tbm_surface_unmap( mTbmSurface ) != TBM_SURFACE_ERROR_NONE )
131 DALI_LOG_ERROR( "Fail to unmap tbm_surface\n" );
136 DALI_ASSERT_ALWAYS(0 && "tbm_surface_map failed");
140 bool Capture::IsSurfaceCreated()
142 return mTbmSurface != 0;
145 void Capture::CreateNativeImageSource()
147 Dali::Adaptor& adaptor = Dali::Adaptor::Get();
149 DALI_ASSERT_ALWAYS(adaptor.IsAvailable() && "Dali::Adaptor is not available.");
151 DALI_ASSERT_ALWAYS(mTbmSurface && "mTbmSurface is empty.");
153 DALI_ASSERT_ALWAYS(!mNativeImageSourcePtr && "NativeImageSource is already created.");
155 // create the NativeImageSource object with our surface
156 mNativeImageSourcePtr = Dali::NativeImageSource::New( mTbmSurface );
159 void Capture::DeleteNativeImageSource()
161 DALI_ASSERT_ALWAYS(mNativeImageSourcePtr && "mNativeImageSource is NULL.");
163 mNativeImageSourcePtr.Reset();
166 bool Capture::IsNativeImageSourceCreated()
168 return mNativeImageSourcePtr;
171 void Capture::CreateFrameBuffer()
173 DALI_ASSERT_ALWAYS(mNativeImageSourcePtr && "NativeImageSource is NULL.");
175 DALI_ASSERT_ALWAYS(!mFrameBuffer && "FrameBuffer is already created.");
177 mNativeTexture = Dali::Texture::New( *mNativeImageSourcePtr );
179 // Create a FrameBuffer object with no default attachments.
180 mFrameBuffer = Dali::FrameBuffer::New( mNativeTexture.GetWidth(), mNativeTexture.GetHeight(), Dali::FrameBuffer::Attachment::NONE );
181 // Add a color attachment to the FrameBuffer object.
182 mFrameBuffer.AttachColorTexture( mNativeTexture );
185 void Capture::DeleteFrameBuffer()
187 DALI_ASSERT_ALWAYS(mFrameBuffer && "FrameBuffer is NULL.");
189 mFrameBuffer.Reset();
190 mNativeTexture.Reset();
193 bool Capture::IsFrameBufferCreated()
198 void Capture::SetupRenderTask( Dali::Actor source, const Dali::Vector4& clearColor )
200 DALI_ASSERT_ALWAYS(source && "Source is empty.");
204 // Check the original parent about source.
205 mParent = mSource.GetParent();
207 Dali::Stage stage = Dali::Stage::GetCurrent();
208 Dali::Size stageSize = stage.GetSize();
210 // Add to stage for rendering the source. If source isn't on the stage then it never be rendered.
211 stage.Add( mSource );
215 mCameraActor = Dali::CameraActor::New( stageSize );
216 mCameraActor.SetParentOrigin( ParentOrigin::CENTER );
217 mCameraActor.SetAnchorPoint( AnchorPoint::CENTER );
220 stage.Add( mCameraActor );
222 DALI_ASSERT_ALWAYS(mFrameBuffer && "Framebuffer is NULL.");
224 DALI_ASSERT_ALWAYS(!mRenderTask && "RenderTask is already created.");
226 Dali::RenderTaskList taskList = stage.GetRenderTaskList();
227 mRenderTask = taskList.CreateTask();
228 mRenderTask.SetRefreshRate( Dali::RenderTask::REFRESH_ONCE );
229 mRenderTask.SetSourceActor( source );
230 mRenderTask.SetCameraActor( mCameraActor );
231 mRenderTask.SetScreenToFrameBufferFunction( Dali::RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
232 mRenderTask.SetFrameBuffer( mFrameBuffer );
233 mRenderTask.SetClearColor( clearColor );
234 mRenderTask.SetClearEnabled( true );
235 mRenderTask.SetProperty( Dali::RenderTask::Property::REQUIRES_SYNC, true );
236 mRenderTask.FinishedSignal().Connect( this, &Capture::OnRenderFinished );
237 mRenderTask.GetCameraActor().SetInvertYAxis( true );
239 mTimer = Dali::Timer::New( TIME_OUT_DURATION );
240 mTimer.TickSignal().Connect( this, &Capture::OnTimeOut );
244 void Capture::UnsetRenderTask()
246 DALI_ASSERT_ALWAYS(mCameraActor && "CameraActor is NULL.");
250 // Restore the parent of source.
251 mParent.Add( mSource );
263 mCameraActor.Unparent();
264 mCameraActor.Reset();
266 DALI_ASSERT_ALWAYS(mRenderTask && "RenderTask is NULL.");
268 Dali::RenderTaskList taskList = Dali::Stage::GetCurrent().GetRenderTaskList();
269 Dali::RenderTask firstTask = taskList.GetTask( 0u );
271 // Stop rendering via frame-buffers as empty handle is used to clear target
272 firstTask.SetFrameBuffer( Dali::FrameBuffer() );
274 taskList.RemoveTask( mRenderTask );
278 bool Capture::IsRenderTaskSetup()
280 return mCameraActor && mRenderTask;
283 void Capture::SetupResources( const Dali::Vector2& size, const Dali::Vector4& clearColor, Dali::Actor source )
285 CreateSurface( size );
286 ClearSurface( size );
288 CreateNativeImageSource();
292 SetupRenderTask( source, clearColor );
295 void Capture::UnsetResources()
297 if( IsRenderTaskSetup() )
302 if( IsFrameBufferCreated() )
307 if( IsNativeImageSourceCreated() )
309 DeleteNativeImageSource();
312 if( IsSurfaceCreated() )
318 void Capture::OnRenderFinished( Dali::RenderTask& task )
320 Dali::Capture::FinishState state = Dali::Capture::FinishState::SUCCEEDED;
326 state = Dali::Capture::FinishState::FAILED;
327 DALI_LOG_ERROR("Fail to Capture mTbmSurface[%p] Path[%s]", mTbmSurface, mPath.c_str());
330 Dali::Capture handle( this );
331 mFinishedSignal.Emit( handle, state );
335 // Decrease the reference count forcely. It is increased at Start().
339 bool Capture::OnTimeOut()
341 Dali::Capture::FinishState state = Dali::Capture::FinishState::FAILED;
343 Dali::Capture handle( this );
344 mFinishedSignal.Emit( handle, state );
348 // Decrease the reference count forcely. It is increased at Start().
356 DALI_ASSERT_ALWAYS(mNativeImageSourcePtr && "mNativeImageSourcePtr is NULL");
358 return mNativeImageSourcePtr->EncodeToFile( mPath );
361 } // End of namespace Adaptor
363 } // End of namespace Internal
365 } // End of namespace Dali