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/vector-wrapper.h>
25 #include <dali/public-api/render-tasks/render-task-list.h>
26 #include <dali/integration-api/debug.h>
29 #include <dali/integration-api/adaptor-framework/adaptor.h>
30 #include <dali/devel-api/adaptor-framework/native-image-source-devel.h>
31 #include <dali/devel-api/adaptor-framework/window-devel.h>
32 #include <dali/devel-api/adaptor-framework/bitmap-saver.h>
33 #include <dali/internal/adaptor/common/adaptor-impl.h>
34 #include <dali/internal/graphics/gles/egl-graphics.h>
47 constexpr int32_t VERSION_NATIVE_IMAGE_SOURCE = 30;
48 constexpr uint32_t TIME_OUT_DURATION = 1000;
52 : mQuality( DEFAULT_QUALITY ),
55 mNativeImageSourcePtr( NULL ),
57 mIsNativeImageSourcePossible(true)
61 Capture::Capture( Dali::CameraActor cameraActor )
62 : mQuality( DEFAULT_QUALITY ),
63 mCameraActor( cameraActor ),
66 mNativeImageSourcePtr( NULL ),
68 mIsNativeImageSourcePossible(true)
74 DeleteNativeImageSource();
78 CapturePtr Capture::New()
80 CapturePtr pWorker = new Capture();
85 CapturePtr Capture::New( Dali::CameraActor cameraActor )
87 CapturePtr pWorker = new Capture( cameraActor );
92 void Capture::Start( Dali::Actor source, const Dali::Vector2& position, const Dali::Vector2& size, const std::string &path, const Dali::Vector4& clearColor, const uint32_t quality )
95 Start( source, position, size, path, clearColor );
98 void Capture::Start( Dali::Actor source, const Dali::Vector2& position, const Dali::Vector2& size, const std::string &path, const Dali::Vector4& clearColor )
100 DALI_ASSERT_ALWAYS(path.size() > 4 && "Path is invalid.");
102 // Increase the reference count focely to avoid application mistake.
106 if( mPath.size() > 0 )
111 DALI_ASSERT_ALWAYS(source && "Source is NULL.");
114 SetupResources( position, size, clearColor, source );
117 void Capture::SetImageQuality( uint32_t quality )
122 Dali::NativeImageSourcePtr Capture::GetNativeImageSource() const
124 return mNativeImageSourcePtr;
127 Dali::Texture Capture::GetTexture()
132 Dali::Devel::PixelBuffer Capture::GetCapturedBuffer()
134 if(!mPixelBuffer || !mPixelBuffer.GetBuffer())
136 uint8_t* bufferPointer;
137 uint32_t width, height;
138 Dali::Pixel::Format pixelFormat;
139 if(mIsNativeImageSourcePossible)
141 std::vector<uint8_t> buffer;
142 if(!mNativeImageSourcePtr->GetPixels(buffer, width, height, pixelFormat))
144 return Dali::Devel::PixelBuffer();
146 bufferPointer = &buffer[0];
150 width = mTexture.GetWidth();
151 height = mTexture.GetHeight();
152 pixelFormat = Dali::Pixel::RGBA8888;
153 bufferPointer = mFrameBuffer.GetRenderedBuffer();
155 mPixelBuffer = Dali::Devel::PixelBuffer::New(width, height, pixelFormat);
157 memcpy(mPixelBuffer.GetBuffer(), bufferPointer, width * height * Dali::Pixel::GetBytesPerPixel(pixelFormat));
163 Dali::Capture::CaptureFinishedSignalType& Capture::FinishedSignal()
165 return mFinishedSignal;
168 void Capture::CreateTexture(const Vector2& size)
170 Dali::Adaptor& adaptor = Dali::Adaptor::Get();
172 DALI_ASSERT_ALWAYS(adaptor.IsAvailable() && "Dali::Adaptor is not available.");
174 if(mIsNativeImageSourcePossible)
176 DALI_ASSERT_ALWAYS(!mNativeImageSourcePtr && "NativeImageSource is already created.");
177 // create the NativeImageSource object with our surface
178 mNativeImageSourcePtr = Dali::NativeImageSource::New(size.width, size.height, Dali::NativeImageSource::COLOR_DEPTH_DEFAULT);
179 mTexture = Dali::Texture::New(*mNativeImageSourcePtr);
183 mTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGB888, unsigned(size.width), unsigned(size.height));
187 void Capture::DeleteNativeImageSource()
189 if(mNativeImageSourcePtr)
191 mNativeImageSourcePtr.Reset();
195 void Capture::CreateFrameBuffer()
197 DALI_ASSERT_ALWAYS(!mFrameBuffer && "FrameBuffer is already created.");
199 // Create a FrameBuffer object with depth attachments.
200 mFrameBuffer = Dali::FrameBuffer::New(mTexture.GetWidth(), mTexture.GetHeight(), Dali::FrameBuffer::Attachment::DEPTH);
201 // Add a color attachment to the FrameBuffer object.
202 mFrameBuffer.AttachColorTexture(mTexture);
205 void Capture::DeleteFrameBuffer()
207 DALI_ASSERT_ALWAYS(mFrameBuffer && "FrameBuffer is NULL.");
209 mFrameBuffer.Reset();
212 bool Capture::IsFrameBufferCreated()
217 void Capture::SetupRenderTask( const Dali::Vector2& position, const Dali::Vector2& size, Dali::Actor source, const Dali::Vector4& clearColor )
219 DALI_ASSERT_ALWAYS(source && "Source is empty.");
221 Dali::Window window = DevelWindow::Get( source );
224 DALI_LOG_ERROR("The source is not added on the window\n");
232 mCameraActor = Dali::CameraActor::New( size );
233 // Because input position and size are for 2 dimentional area,
234 // default z-directional position of the camera is required to be used for the new camera position.
235 float cameraDefaultZPosition = mCameraActor.GetProperty<float>( Dali::Actor::Property::POSITION_Z );
236 Vector2 positionTransition = position + size / 2;
237 mCameraActor.SetProperty( Dali::Actor::Property::POSITION, Vector3( positionTransition.x, positionTransition.y, cameraDefaultZPosition ) );
238 mCameraActor.SetProperty( Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
239 mCameraActor.SetProperty( Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
242 window.Add( mCameraActor );
244 DALI_ASSERT_ALWAYS(mFrameBuffer && "Framebuffer is NULL.");
246 DALI_ASSERT_ALWAYS(!mRenderTask && "RenderTask is already created.");
248 Dali::RenderTaskList taskList = window.GetRenderTaskList();
249 mRenderTask = taskList.CreateTask();
250 mRenderTask.SetRefreshRate( Dali::RenderTask::REFRESH_ONCE );
251 mRenderTask.SetSourceActor( source );
252 mRenderTask.SetCameraActor( mCameraActor );
253 mRenderTask.SetScreenToFrameBufferFunction( Dali::RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
254 mRenderTask.SetFrameBuffer( mFrameBuffer );
255 mRenderTask.SetClearColor( clearColor );
256 mRenderTask.SetClearEnabled( true );
257 mRenderTask.SetProperty( Dali::RenderTask::Property::REQUIRES_SYNC, true );
258 mRenderTask.FinishedSignal().Connect( this, &Capture::OnRenderFinished );
259 mRenderTask.GetCameraActor().SetInvertYAxis( true );
260 if(!mIsNativeImageSourcePossible)
262 mFrameBuffer.CaptureRenderedResult();
265 mTimer = Dali::Timer::New( TIME_OUT_DURATION );
266 mTimer.TickSignal().Connect( this, &Capture::OnTimeOut );
270 void Capture::UnsetRenderTask()
272 DALI_ASSERT_ALWAYS(mCameraActor && "CameraActor is NULL.");
276 mCameraActor.Unparent();
277 mCameraActor.Reset();
279 DALI_ASSERT_ALWAYS( mRenderTask && "RenderTask is NULL." );
281 Dali::Window window = DevelWindow::Get( mSource );
282 Dali::RenderTaskList taskList = window.GetRenderTaskList();
283 taskList.RemoveTask( mRenderTask );
288 bool Capture::IsRenderTaskSetup()
290 return mCameraActor && mRenderTask;
293 void Capture::SetupResources(const Dali::Vector2& position, const Dali::Vector2& size, const Dali::Vector4& clearColor, Dali::Actor source)
295 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
296 GraphicsInterface* graphics = &adaptor.GetGraphicsInterface();
297 auto eglGraphics = static_cast<EglGraphics*>(graphics);
299 if(eglGraphics->GetEglImplementation().GetGlesVersion() < VERSION_NATIVE_IMAGE_SOURCE)
301 mIsNativeImageSourcePossible = false;
308 SetupRenderTask( position, size, source, clearColor );
311 void Capture::UnsetResources()
313 if( IsRenderTaskSetup() )
318 if( IsFrameBufferCreated() )
324 void Capture::OnRenderFinished( Dali::RenderTask& task )
326 Dali::Capture::FinishState state = Dali::Capture::FinishState::SUCCEEDED;
334 state = Dali::Capture::FinishState::FAILED;
335 DALI_LOG_ERROR( "Fail to Capture Path[%s]", mPath.c_str() );
339 Dali::Capture handle( this );
340 mFinishedSignal.Emit( handle, state );
344 // Decrease the reference count forcely. It is increased at Start().
348 bool Capture::OnTimeOut()
350 Dali::Capture::FinishState state = Dali::Capture::FinishState::FAILED;
352 Dali::Capture handle( this );
353 mFinishedSignal.Emit( handle, state );
357 // Decrease the reference count forcely. It is increased at Start().
363 bool Capture::SaveFile()
365 if(mIsNativeImageSourcePossible)
367 DALI_ASSERT_ALWAYS(mNativeImageSourcePtr && "mNativeImageSourcePtr is NULL");
368 return Dali::DevelNativeImageSource::EncodeToFile(*mNativeImageSourcePtr, mPath, mQuality);
372 uint8_t* buffer = mFrameBuffer.GetRenderedBuffer();
373 return Dali::EncodeToFile(buffer, mPath, Dali::Pixel::RGBA8888, mTexture.GetWidth(), mTexture.GetHeight(), mQuality);
377 } // End of namespace Adaptor
379 } // End of namespace Internal
381 } // End of namespace Dali