2 * Copyright (c) 2021 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/native-image-source-devel.h>
30 #include <dali/devel-api/adaptor-framework/window-devel.h>
31 #include <dali/integration-api/adaptor-framework/adaptor.h>
35 unsigned int TIME_OUT_DURATION = 1000;
45 : mQuality(DEFAULT_QUALITY),
48 mNativeImageSourcePtr(NULL),
53 Capture::Capture(Dali::CameraActor cameraActor)
54 : mQuality(DEFAULT_QUALITY),
55 mCameraActor(cameraActor),
58 mNativeImageSourcePtr(NULL),
65 DeleteNativeImageSource();
68 CapturePtr Capture::New()
70 CapturePtr pWorker = new Capture();
75 CapturePtr Capture::New(Dali::CameraActor cameraActor)
77 CapturePtr pWorker = new Capture(cameraActor);
82 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)
85 Start(source, position, size, path, clearColor);
88 void Capture::Start(Dali::Actor source, const Dali::Vector2& position, const Dali::Vector2& size, const std::string& path, const Dali::Vector4& clearColor)
90 // Increase the reference count focely to avoid application mistake.
99 DALI_ASSERT_ALWAYS(source && "Source is NULL.");
102 SetupResources(position, size, clearColor, source);
105 void Capture::SetImageQuality(uint32_t quality)
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(const Dali::Vector2& position, const Dali::Vector2& size, Dali::Actor source, const Dali::Vector4& clearColor)
171 DALI_ASSERT_ALWAYS(source && "Source is empty.");
173 Dali::Window window = DevelWindow::Get(source);
176 DALI_LOG_ERROR("The source is not added on the window\n");
184 mCameraActor = Dali::CameraActor::New(size);
185 // Because input position and size are for 2 dimentional area,
186 // default z-directional position of the camera is required to be used for the new camera position.
187 float cameraDefaultZPosition = mCameraActor.GetProperty<float>(Dali::Actor::Property::POSITION_Z);
188 Vector2 positionTransition = position + size / 2;
189 mCameraActor.SetProperty(Dali::Actor::Property::POSITION, Vector3(positionTransition.x, positionTransition.y, cameraDefaultZPosition));
190 mCameraActor.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
191 mCameraActor.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
194 window.Add(mCameraActor);
196 DALI_ASSERT_ALWAYS(mFrameBuffer && "Framebuffer is NULL.");
198 DALI_ASSERT_ALWAYS(!mRenderTask && "RenderTask is already created.");
200 Dali::RenderTaskList taskList = window.GetRenderTaskList();
201 mRenderTask = taskList.CreateTask();
202 mRenderTask.SetRefreshRate(Dali::RenderTask::REFRESH_ONCE);
203 mRenderTask.SetSourceActor(source);
204 mRenderTask.SetCameraActor(mCameraActor);
205 mRenderTask.SetScreenToFrameBufferFunction(Dali::RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION);
206 mRenderTask.SetFrameBuffer(mFrameBuffer);
207 mRenderTask.SetClearColor(clearColor);
208 mRenderTask.SetClearEnabled(true);
209 mRenderTask.SetProperty(Dali::RenderTask::Property::REQUIRES_SYNC, true);
210 mRenderTask.FinishedSignal().Connect(this, &Capture::OnRenderFinished);
211 mRenderTask.GetCameraActor().SetInvertYAxis(true);
213 mTimer = Dali::Timer::New(TIME_OUT_DURATION);
214 mTimer.TickSignal().Connect(this, &Capture::OnTimeOut);
218 void Capture::UnsetRenderTask()
220 DALI_ASSERT_ALWAYS(mCameraActor && "CameraActor is NULL.");
224 mCameraActor.Unparent();
225 mCameraActor.Reset();
227 DALI_ASSERT_ALWAYS(mRenderTask && "RenderTask is NULL.");
229 Dali::Window window = DevelWindow::Get(mSource);
230 Dali::RenderTaskList taskList = window.GetRenderTaskList();
231 taskList.RemoveTask(mRenderTask);
236 bool Capture::IsRenderTaskSetup()
238 return mCameraActor && mRenderTask;
241 void Capture::SetupResources(const Dali::Vector2& position, const Dali::Vector2& size, const Dali::Vector4& clearColor, Dali::Actor source)
243 CreateNativeImageSource(size);
247 SetupRenderTask(position, size, source, clearColor);
250 void Capture::UnsetResources()
252 if(IsRenderTaskSetup())
257 if(IsFrameBufferCreated())
263 void Capture::OnRenderFinished(Dali::RenderTask& task)
265 Dali::Capture::FinishState state = Dali::Capture::FinishState::SUCCEEDED;
273 state = Dali::Capture::FinishState::FAILED;
274 DALI_LOG_ERROR("Fail to Capture Path[%s]", mPath.c_str());
278 Dali::Capture handle(this);
279 mFinishedSignal.Emit(handle, state);
283 // Decrease the reference count forcely. It is increased at Start().
287 bool Capture::OnTimeOut()
289 Dali::Capture::FinishState state = Dali::Capture::FinishState::FAILED;
291 Dali::Capture handle(this);
292 mFinishedSignal.Emit(handle, state);
296 // Decrease the reference count forcely. It is increased at Start().
302 bool Capture::SaveFile()
304 DALI_ASSERT_ALWAYS(mNativeImageSourcePtr && "mNativeImageSourcePtr is NULL");
306 return Dali::DevelNativeImageSource::EncodeToFile(*mNativeImageSourcePtr, mPath, mQuality);
309 } // End of namespace Adaptor
311 } // End of namespace Internal
313 } // End of namespace Dali