2 * Copyright (c) 2022 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>
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/common/vector-wrapper.h>
24 #include <dali/public-api/render-tasks/render-task-list.h>
29 #include <dali/devel-api/adaptor-framework/bitmap-saver.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/integration-api/adaptor-framework/adaptor.h>
33 #include <dali/internal/adaptor/common/adaptor-impl.h>
34 #include <dali/internal/graphics/gles/egl-graphics.h>
44 constexpr int32_t GL_VERSION_NATIVE_IMAGE_SOURCE_AVAILABLE = 30;
45 constexpr uint32_t TIME_OUT_DURATION = 1000;
49 : mQuality(DEFAULT_QUALITY),
52 mNativeImageSourcePtr(NULL),
54 mUseDefaultCamera(true),
55 mSceneOffCameraAfterCaptureFinished(false),
56 mIsNativeImageSourcePossible(true)
60 Capture::Capture(Dali::CameraActor cameraActor)
61 : mQuality(DEFAULT_QUALITY),
62 mCameraActor(cameraActor),
65 mNativeImageSourcePtr(NULL),
67 mUseDefaultCamera(!cameraActor),
68 mSceneOffCameraAfterCaptureFinished(false),
69 mIsNativeImageSourcePossible(true)
75 DeleteNativeImageSource();
79 CapturePtr Capture::New()
81 CapturePtr pWorker = new Capture();
86 CapturePtr Capture::New(Dali::CameraActor cameraActor)
88 CapturePtr pWorker = new Capture(cameraActor);
93 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)
96 Start(source, position, size, path, clearColor);
99 void Capture::Start(Dali::Actor source, const Dali::Vector2& position, const Dali::Vector2& size, const std::string& path, const Dali::Vector4& clearColor)
106 // Increase the reference count focely to avoid application mistake.
116 SetupResources(position, size, clearColor, source);
119 void Capture::SetImageQuality(uint32_t quality)
124 Dali::NativeImageSourcePtr Capture::GetNativeImageSource() const
126 return mNativeImageSourcePtr;
129 Dali::Texture Capture::GetTexture()
134 Dali::Devel::PixelBuffer Capture::GetCapturedBuffer()
136 if(!mPixelBuffer || (mPixelBuffer && !mPixelBuffer.GetBuffer()))
138 std::vector<uint8_t> buffer;
139 uint32_t width, height;
140 Dali::Pixel::Format pixelFormat;
141 if(!mNativeImageSourcePtr->GetPixels(buffer, width, height, pixelFormat))
143 return Dali::Devel::PixelBuffer();
145 mPixelBuffer = Dali::Devel::PixelBuffer::New(width, height, pixelFormat);
146 memcpy(mPixelBuffer.GetBuffer(), &buffer[0], width * height * Dali::Pixel::GetBytesPerPixel(pixelFormat));
151 Dali::Capture::CaptureFinishedSignalType& Capture::FinishedSignal()
153 return mFinishedSignal;
156 void Capture::CreateTexture(const Vector2& size)
158 if(mIsNativeImageSourcePossible)
160 if(!mNativeImageSourcePtr)
162 mNativeImageSourcePtr = Dali::NativeImageSource::New(size.width, size.height, Dali::NativeImageSource::COLOR_DEPTH_DEFAULT);
163 mTexture = Dali::Texture::New(*mNativeImageSourcePtr);
168 mTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGB888, unsigned(size.width), unsigned(size.height));
172 void Capture::DeleteNativeImageSource()
174 if(mNativeImageSourcePtr)
176 mNativeImageSourcePtr.Reset();
180 void Capture::CreateFrameBuffer()
184 // Create a FrameBuffer object with depth attachments.
185 mFrameBuffer = Dali::FrameBuffer::New(mTexture.GetWidth(), mTexture.GetHeight(), Dali::FrameBuffer::Attachment::DEPTH);
186 // Add a color attachment to the FrameBuffer object.
187 mFrameBuffer.AttachColorTexture(mTexture);
191 void Capture::DeleteFrameBuffer()
195 mFrameBuffer.Reset();
199 bool Capture::IsFrameBufferCreated()
201 return static_cast<bool>(mFrameBuffer);
204 void Capture::SetupRenderTask(const Dali::Vector2& position, const Dali::Vector2& size, Dali::Actor source, const Dali::Vector4& clearColor)
208 DALI_LOG_ERROR("Source is empty\n");
212 Dali::Window window = DevelWindow::Get(source);
215 DALI_LOG_ERROR("The source is not added on the scene\n");
223 mUseDefaultCamera = true;
224 mCameraActor = Dali::CameraActor::New(size);
225 // Because input position and size are for 2 dimentional area,
226 // default z-directional position of the camera is required to be used for the new camera position.
227 float cameraDefaultZPosition = mCameraActor.GetProperty<float>(Dali::Actor::Property::POSITION_Z);
228 Vector2 positionTransition = position + size / 2;
229 mCameraActor.SetProperty(Dali::Actor::Property::POSITION, Vector3(positionTransition.x, positionTransition.y, cameraDefaultZPosition));
230 mCameraActor.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
231 mCameraActor.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
234 // Camera must be scene on. Add camera to window.
235 if(!mCameraActor.GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE))
237 if(!mUseDefaultCamera)
239 DALI_LOG_ERROR("Camera must be on scene. Camera is connected to window now.\n");
241 window.Add(mCameraActor);
242 mSceneOffCameraAfterCaptureFinished = true;
247 DALI_LOG_ERROR("Frame buffer is not created.\n");
251 Dali::RenderTaskList taskList = window.GetRenderTaskList();
252 mRenderTask = taskList.CreateTask();
253 mRenderTask.SetRefreshRate(Dali::RenderTask::REFRESH_ONCE);
254 mRenderTask.SetSourceActor(source);
255 mRenderTask.SetCameraActor(mCameraActor);
256 mRenderTask.SetScreenToFrameBufferFunction(Dali::RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION);
257 mRenderTask.SetFrameBuffer(mFrameBuffer);
258 mRenderTask.SetClearColor(clearColor);
259 mRenderTask.SetClearEnabled(true);
260 mRenderTask.SetProperty(Dali::RenderTask::Property::REQUIRES_SYNC, true);
261 mRenderTask.FinishedSignal().Connect(this, &Capture::OnRenderFinished);
262 mRenderTask.GetCameraActor().SetInvertYAxis(true);
264 mTimer = Dali::Timer::New(TIME_OUT_DURATION);
265 mTimer.TickSignal().Connect(this, &Capture::OnTimeOut);
269 void Capture::UnsetRenderTask()
273 if(mSceneOffCameraAfterCaptureFinished && mCameraActor)
275 if(!mUseDefaultCamera)
277 DALI_LOG_ERROR("Camera is disconnected from window now.\n");
279 mSceneOffCameraAfterCaptureFinished = false;
280 mCameraActor.Unparent();
281 mCameraActor.Reset();
286 Dali::Window window = DevelWindow::Get(mSource);
287 Dali::RenderTaskList taskList = window.GetRenderTaskList();
288 taskList.RemoveTask(mRenderTask);
294 bool Capture::IsRenderTaskSetup()
296 return mCameraActor && mRenderTask;
299 void Capture::SetupResources(const Dali::Vector2& position, const Dali::Vector2& size, const Dali::Vector4& clearColor, Dali::Actor source)
301 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
302 GraphicsInterface* graphics = &adaptor.GetGraphicsInterface();
303 auto eglGraphics = static_cast<EglGraphics*>(graphics);
305 if(eglGraphics->GetEglImplementation().GetGlesVersion() < GL_VERSION_NATIVE_IMAGE_SOURCE_AVAILABLE)
307 DALI_LOG_ERROR("GLES is 2.0, we can't use native image source \n");
308 mIsNativeImageSourcePossible = false;
315 SetupRenderTask(position, size, source, clearColor);
318 void Capture::UnsetResources()
320 if(IsRenderTaskSetup())
325 if(IsFrameBufferCreated())
331 void Capture::OnRenderFinished(Dali::RenderTask& task)
333 Dali::Capture::FinishState state = Dali::Capture::FinishState::SUCCEEDED;
341 DALI_LOG_ERROR("Fail to Capture Path[%s]\n", mPath.c_str());
342 state = Dali::Capture::FinishState::FAILED;
346 Dali::Capture handle(this);
347 mFinishedSignal.Emit(handle, state);
351 // Decrease the reference count forcely. It is increased at Start().
355 bool Capture::OnTimeOut()
357 Dali::Capture::FinishState state = Dali::Capture::FinishState::FAILED;
359 Dali::Capture handle(this);
360 mFinishedSignal.Emit(handle, state);
364 // Decrease the reference count forcely. It is increased at Start().
370 bool Capture::SaveFile()
372 if(mIsNativeImageSourcePossible)
374 if(mNativeImageSourcePtr)
376 return Dali::DevelNativeImageSource::EncodeToFile(*mNativeImageSourcePtr, mPath, mQuality);
381 DALI_LOG_ERROR("can't use Capture::SavceFile(). we don't support this function in gles 2.0 \n");
387 } // End of namespace Adaptor
389 } // End of namespace Internal
391 } // End of namespace Dali