7cf2aecb5945009d2e19188b32cf1681d76ba10b
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / capture-impl.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/system/common/capture-impl.h>
20
21 // EXTERNAL INCLUDES
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>
25 #include <string.h>
26 #include <fstream>
27
28 // INTERNAL INCLUDES
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>
35
36 namespace Dali
37 {
38 namespace Internal
39 {
40 namespace Adaptor
41 {
42 namespace
43 {
44 constexpr int32_t  GL_VERSION_NATIVE_IMAGE_SOURCE_AVAILABLE = 30;
45 constexpr uint32_t TIME_OUT_DURATION                        = 1000;
46 } // namespace
47
48 Capture::Capture()
49 : mQuality(DEFAULT_QUALITY),
50   mTimer(),
51   mPath(),
52   mNativeImageSourcePtr(NULL),
53   mFileSave(false),
54   mUseDefaultCamera(true),
55   mSceneOffCameraAfterCaptureFinished(false),
56   mIsNativeImageSourcePossible(true)
57 {
58 }
59
60 Capture::Capture(Dali::CameraActor cameraActor)
61 : mQuality(DEFAULT_QUALITY),
62   mCameraActor(cameraActor),
63   mTimer(),
64   mPath(),
65   mNativeImageSourcePtr(NULL),
66   mFileSave(false),
67   mUseDefaultCamera(!cameraActor),
68   mSceneOffCameraAfterCaptureFinished(false),
69   mIsNativeImageSourcePossible(true)
70 {
71 }
72
73 Capture::~Capture()
74 {
75   DeleteNativeImageSource();
76   mTexture.Reset();
77 }
78
79 CapturePtr Capture::New()
80 {
81   CapturePtr pWorker = new Capture();
82
83   return pWorker;
84 }
85
86 CapturePtr Capture::New(Dali::CameraActor cameraActor)
87 {
88   CapturePtr pWorker = new Capture(cameraActor);
89
90   return pWorker;
91 }
92
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)
94 {
95   mQuality = quality;
96   Start(source, position, size, path, clearColor);
97 }
98
99 void Capture::Start(Dali::Actor source, const Dali::Vector2& position, const Dali::Vector2& size, const std::string& path, const Dali::Vector4& clearColor)
100 {
101   if(!source)
102   {
103     return;
104   }
105
106   // Increase the reference count focely to avoid application mistake.
107   Reference();
108
109   mPath = path;
110   if(!mPath.empty())
111   {
112     mFileSave = true;
113   }
114
115   UnsetResources();
116   SetupResources(position, size, clearColor, source);
117 }
118
119 void Capture::SetImageQuality(uint32_t quality)
120 {
121   mQuality = quality;
122 }
123
124 Dali::NativeImageSourcePtr Capture::GetNativeImageSource() const
125 {
126   return mNativeImageSourcePtr;
127 }
128
129 Dali::Texture Capture::GetTexture()
130 {
131   return mTexture;
132 }
133
134 Dali::Devel::PixelBuffer Capture::GetCapturedBuffer()
135 {
136   if(!mPixelBuffer || (mPixelBuffer && !mPixelBuffer.GetBuffer()))
137   {
138     std::vector<uint8_t> buffer;
139     uint32_t             width, height;
140     Dali::Pixel::Format  pixelFormat;
141     if(!mNativeImageSourcePtr->GetPixels(buffer, width, height, pixelFormat))
142     {
143       return Dali::Devel::PixelBuffer();
144     }
145     mPixelBuffer = Dali::Devel::PixelBuffer::New(width, height, pixelFormat);
146     memcpy(mPixelBuffer.GetBuffer(), &buffer[0], width * height * Dali::Pixel::GetBytesPerPixel(pixelFormat));
147   }
148   return mPixelBuffer;
149 }
150
151 Dali::Capture::CaptureFinishedSignalType& Capture::FinishedSignal()
152 {
153   return mFinishedSignal;
154 }
155
156 void Capture::CreateTexture(const Vector2& size)
157 {
158   if(mIsNativeImageSourcePossible)
159   {
160     if(!mNativeImageSourcePtr)
161     {
162       mNativeImageSourcePtr = Dali::NativeImageSource::New(size.width, size.height, Dali::NativeImageSource::COLOR_DEPTH_DEFAULT);
163       mTexture              = Dali::Texture::New(*mNativeImageSourcePtr);
164     }
165   }
166   else
167   {
168     mTexture = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGB888, unsigned(size.width), unsigned(size.height));
169   }
170 }
171
172 void Capture::DeleteNativeImageSource()
173 {
174   if(mNativeImageSourcePtr)
175   {
176     mNativeImageSourcePtr.Reset();
177   }
178 }
179
180 void Capture::CreateFrameBuffer()
181 {
182   if(!mFrameBuffer)
183   {
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);
188   }
189 }
190
191 void Capture::DeleteFrameBuffer()
192 {
193   if(mFrameBuffer)
194   {
195     mFrameBuffer.Reset();
196   }
197 }
198
199 bool Capture::IsFrameBufferCreated()
200 {
201   return static_cast<bool>(mFrameBuffer);
202 }
203
204 void Capture::SetupRenderTask(const Dali::Vector2& position, const Dali::Vector2& size, Dali::Actor source, const Dali::Vector4& clearColor)
205 {
206   if(!source)
207   {
208     DALI_LOG_ERROR("Source is empty\n");
209     return;
210   }
211
212   Dali::Window window = DevelWindow::Get(source);
213   if(!window)
214   {
215     DALI_LOG_ERROR("The source is not added on the scene\n");
216     return;
217   }
218
219   mSource = source;
220
221   if(!mCameraActor)
222   {
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);
232   }
233
234   // Camera must be scene on. Add camera to window.
235   if(!mCameraActor.GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE))
236   {
237     if(!mUseDefaultCamera)
238     {
239       DALI_LOG_ERROR("Camera must be on scene. Camera is connected to window now.\n");
240     }
241     window.Add(mCameraActor);
242     mSceneOffCameraAfterCaptureFinished = true;
243   }
244
245   if(!mFrameBuffer)
246   {
247     DALI_LOG_ERROR("Frame buffer is not created.\n");
248     return;
249   }
250
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);
263
264   mTimer = Dali::Timer::New(TIME_OUT_DURATION);
265   mTimer.TickSignal().Connect(this, &Capture::OnTimeOut);
266   mTimer.Start();
267 }
268
269 void Capture::UnsetRenderTask()
270 {
271   mTimer.Reset();
272
273   if(mSceneOffCameraAfterCaptureFinished && mCameraActor)
274   {
275     if(!mUseDefaultCamera)
276     {
277       DALI_LOG_ERROR("Camera is disconnected from window now.\n");
278     }
279     mSceneOffCameraAfterCaptureFinished = false;
280     mCameraActor.Unparent();
281     mCameraActor.Reset();
282   }
283
284   if(mRenderTask)
285   {
286     Dali::Window         window   = DevelWindow::Get(mSource);
287     Dali::RenderTaskList taskList = window.GetRenderTaskList();
288     taskList.RemoveTask(mRenderTask);
289     mRenderTask.Reset();
290   }
291   mSource.Reset();
292 }
293
294 bool Capture::IsRenderTaskSetup()
295 {
296   return mCameraActor && mRenderTask;
297 }
298
299 void Capture::SetupResources(const Dali::Vector2& position, const Dali::Vector2& size, const Dali::Vector4& clearColor, Dali::Actor source)
300 {
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);
304
305   if(eglGraphics->GetEglImplementation().GetGlesVersion() < GL_VERSION_NATIVE_IMAGE_SOURCE_AVAILABLE)
306   {
307     DALI_LOG_ERROR("GLES is 2.0, we can't use native image source \n");
308     mIsNativeImageSourcePossible = false;
309   }
310
311   CreateTexture(size);
312
313   CreateFrameBuffer();
314
315   SetupRenderTask(position, size, source, clearColor);
316 }
317
318 void Capture::UnsetResources()
319 {
320   if(IsRenderTaskSetup())
321   {
322     UnsetRenderTask();
323   }
324
325   if(IsFrameBufferCreated())
326   {
327     DeleteFrameBuffer();
328   }
329 }
330
331 void Capture::OnRenderFinished(Dali::RenderTask& task)
332 {
333   Dali::Capture::FinishState state = Dali::Capture::FinishState::SUCCEEDED;
334
335   mTimer.Stop();
336
337   if(mFileSave)
338   {
339     if(!SaveFile())
340     {
341       DALI_LOG_ERROR("Fail to Capture Path[%s]\n", mPath.c_str());
342       state = Dali::Capture::FinishState::FAILED;
343     }
344   }
345
346   Dali::Capture handle(this);
347   mFinishedSignal.Emit(handle, state);
348
349   UnsetResources();
350
351   // Decrease the reference count forcely. It is increased at Start().
352   Unreference();
353 }
354
355 bool Capture::OnTimeOut()
356 {
357   Dali::Capture::FinishState state = Dali::Capture::FinishState::FAILED;
358
359   Dali::Capture handle(this);
360   mFinishedSignal.Emit(handle, state);
361
362   UnsetResources();
363
364   // Decrease the reference count forcely. It is increased at Start().
365   Unreference();
366
367   return false;
368 }
369
370 bool Capture::SaveFile()
371 {
372   if(mIsNativeImageSourcePossible)
373   {
374     if(mNativeImageSourcePtr)
375     {
376       return Dali::DevelNativeImageSource::EncodeToFile(*mNativeImageSourcePtr, mPath, mQuality);
377     }
378   }
379   else
380   {
381     DALI_LOG_ERROR("can't use Capture::SavceFile(). we don't support this function in gles 2.0 \n");
382   }
383
384   return false;
385 }
386
387 } // End of namespace Adaptor
388
389 } // End of namespace Internal
390
391 } // End of namespace Dali