Updated all code to new format
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / tizen-wayland / native-render-surface-ecore-wl.cpp
1 /*
2  * Copyright (c) 2021 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/window-system/tizen-wayland/native-render-surface-ecore-wl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/gl-abstraction.h>
24
25 #ifdef ECORE_WAYLAND2
26 #include <Ecore_Wl2.h>
27 #else
28 #include <Ecore_Wayland.h>
29 #endif
30
31 #include <tbm_bufmgr.h>
32 #include <tbm_surface_internal.h>
33
34 // INTERNAL INCLUDES
35 #include <dali/integration-api/adaptor-framework/thread-synchronization-interface.h>
36 #include <dali/internal/adaptor/common/adaptor-impl.h>
37 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
38 #include <dali/internal/graphics/gles/egl-graphics.h>
39 #include <dali/internal/graphics/gles/egl-implementation.h>
40 #include <dali/internal/system/common/trigger-event.h>
41 #include <dali/internal/window-system/common/display-connection.h>
42 #include <dali/internal/window-system/common/window-system.h>
43
44 namespace Dali
45 {
46 namespace
47 {
48 #if defined(DEBUG_ENABLED)
49 Debug::Filter* gNativeSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_NATIVE_RENDER_SURFACE");
50 #endif
51
52 } // unnamed namespace
53
54 NativeRenderSurfaceEcoreWl::NativeRenderSurfaceEcoreWl(SurfaceSize surfaceSize, Any surface, bool isTransparent)
55 : mRenderNotification(NULL),
56   mGraphics(NULL),
57   mEGL(nullptr),
58   mEGLSurface(nullptr),
59   mEGLContext(nullptr),
60   mOwnSurface(false),
61   mDrawableCompleted(false),
62   mTbmQueue(NULL),
63   mConsumeSurface(NULL),
64   mThreadSynchronization(NULL)
65 {
66   Dali::Internal::Adaptor::WindowSystem::Initialize();
67
68   if(surface.Empty())
69   {
70     mSurfaceSize = surfaceSize;
71     mColorDepth  = isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24;
72     mTbmFormat   = isTransparent ? TBM_FORMAT_ARGB8888 : TBM_FORMAT_RGB888;
73     CreateNativeRenderable();
74   }
75   else
76   {
77     mTbmQueue = AnyCast<tbm_surface_queue_h>(surface);
78
79     uint16_t width  = static_cast<uint16_t>(tbm_surface_queue_get_width(mTbmQueue));
80     uint16_t height = static_cast<uint16_t>(tbm_surface_queue_get_height(mTbmQueue));
81     mSurfaceSize    = SurfaceSize(width, height);
82
83     mTbmFormat = tbm_surface_queue_get_format(mTbmQueue);
84
85     mColorDepth = (mTbmFormat == TBM_FORMAT_ARGB8888) ? COLOR_DEPTH_32 : COLOR_DEPTH_24;
86   }
87 }
88
89 NativeRenderSurfaceEcoreWl::~NativeRenderSurfaceEcoreWl()
90 {
91   if(mEGLSurface)
92   {
93     DestroySurface();
94   }
95
96   // release the surface if we own one
97   if(mOwnSurface)
98   {
99     ReleaseDrawable();
100
101     if(mTbmQueue)
102     {
103       tbm_surface_queue_destroy(mTbmQueue);
104     }
105
106     DALI_LOG_INFO(gNativeSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n");
107   }
108
109   Dali::Internal::Adaptor::WindowSystem::Shutdown();
110 }
111
112 Any NativeRenderSurfaceEcoreWl::GetDrawable()
113 {
114   return mConsumeSurface;
115 }
116
117 void NativeRenderSurfaceEcoreWl::SetRenderNotification(TriggerEventInterface* renderNotification)
118 {
119   mRenderNotification = renderNotification;
120 }
121
122 void NativeRenderSurfaceEcoreWl::WaitUntilSurfaceReplaced()
123 {
124   ConditionalWait::ScopedLock lock(mTbmSurfaceCondition);
125   while(!mDrawableCompleted)
126   {
127     mTbmSurfaceCondition.Wait(lock);
128   }
129
130   mDrawableCompleted = false;
131 }
132
133 Any NativeRenderSurfaceEcoreWl::GetNativeRenderable()
134 {
135   return mTbmQueue;
136 }
137
138 PositionSize NativeRenderSurfaceEcoreWl::GetPositionSize() const
139 {
140   return PositionSize(0, 0, static_cast<int>(mSurfaceSize.GetWidth()), static_cast<int>(mSurfaceSize.GetHeight()));
141 }
142
143 void NativeRenderSurfaceEcoreWl::GetDpi(unsigned int& dpiHorizontal, unsigned int& dpiVertical)
144 {
145   // calculate DPI
146   float xres, yres;
147
148   // 1 inch = 25.4 millimeters
149 #ifdef ECORE_WAYLAND2
150   // TODO: Application should set dpi value in wayland2
151   xres = 96;
152   yres = 96;
153 #else
154   xres = ecore_wl_dpi_get();
155   yres = ecore_wl_dpi_get();
156 #endif
157
158   dpiHorizontal = int(xres + 0.5f); // rounding
159   dpiVertical   = int(yres + 0.5f);
160 }
161
162 int NativeRenderSurfaceEcoreWl::GetOrientation() const
163 {
164   return 0;
165 }
166
167 void NativeRenderSurfaceEcoreWl::InitializeGraphics()
168 {
169   DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
170
171   mGraphics        = &mAdaptor->GetGraphicsInterface();
172   auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
173
174   mEGL = &eglGraphics->GetEglInterface();
175
176   if(mEGLContext == NULL)
177   {
178     // Create the OpenGL context for this window
179     Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
180     eglImpl.CreateWindowContext(mEGLContext);
181
182     // Create the OpenGL surface
183     CreateSurface();
184   }
185 }
186
187 void NativeRenderSurfaceEcoreWl::CreateSurface()
188 {
189   DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
190
191   auto                                  eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
192   Internal::Adaptor::EglImplementation& eglImpl     = eglGraphics->GetEglImplementation();
193
194   mEGLSurface = eglImpl.CreateSurfaceWindow(reinterpret_cast<EGLNativeWindowType>(mTbmQueue), mColorDepth);
195 }
196
197 void NativeRenderSurfaceEcoreWl::DestroySurface()
198 {
199   DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
200
201   auto                                  eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
202   Internal::Adaptor::EglImplementation& eglImpl     = eglGraphics->GetEglImplementation();
203
204   eglImpl.DestroySurface(mEGLSurface);
205 }
206
207 bool NativeRenderSurfaceEcoreWl::ReplaceGraphicsSurface()
208 {
209   DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
210
211   if(!mTbmQueue)
212   {
213     return false;
214   }
215
216   auto                                  eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
217   Internal::Adaptor::EglImplementation& eglImpl     = eglGraphics->GetEglImplementation();
218
219   return eglImpl.ReplaceSurfaceWindow(reinterpret_cast<EGLNativeWindowType>(mTbmQueue), mEGLSurface, mEGLContext);
220 }
221
222 void NativeRenderSurfaceEcoreWl::MoveResize(Dali::PositionSize positionSize)
223 {
224   tbm_surface_queue_error_e error = TBM_SURFACE_QUEUE_ERROR_NONE;
225
226   error = tbm_surface_queue_reset(mTbmQueue, positionSize.width, positionSize.height, mTbmFormat);
227
228   if(error != TBM_SURFACE_QUEUE_ERROR_NONE)
229   {
230     DALI_LOG_ERROR("Failed to resize tbm_surface_queue");
231   }
232
233   mSurfaceSize.SetWidth(static_cast<uint16_t>(positionSize.width));
234   mSurfaceSize.SetHeight(static_cast<uint16_t>(positionSize.height));
235 }
236
237 void NativeRenderSurfaceEcoreWl::StartRender()
238 {
239 }
240
241 bool NativeRenderSurfaceEcoreWl::PreRender(bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
242 {
243   //TODO: Need to support partial update
244   return true;
245 }
246
247 void NativeRenderSurfaceEcoreWl::PostRender(bool renderToFbo, bool replacingSurface, bool resizingSurface, const std::vector<Rect<int>>& damagedRects)
248 {
249   auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
250   if(eglGraphics)
251   {
252     Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
253     eglImpl.SwapBuffers(mEGLSurface, damagedRects);
254   }
255
256   //TODO: Move calling tbm_surface_queue_acruie to OffscreenWindow and Scene in EvasPlugin
257   if(mOwnSurface)
258   {
259     if(mThreadSynchronization)
260     {
261       mThreadSynchronization->PostRenderStarted();
262     }
263
264     if(tbm_surface_queue_can_acquire(mTbmQueue, 1))
265     {
266       if(tbm_surface_queue_acquire(mTbmQueue, &mConsumeSurface) != TBM_SURFACE_QUEUE_ERROR_NONE)
267       {
268         DALI_LOG_ERROR("Failed to acquire a tbm_surface\n");
269         return;
270       }
271     }
272
273     if(mConsumeSurface)
274     {
275       tbm_surface_internal_ref(mConsumeSurface);
276     }
277
278     if(replacingSurface)
279     {
280       ConditionalWait::ScopedLock lock(mTbmSurfaceCondition);
281       mDrawableCompleted = true;
282       mTbmSurfaceCondition.Notify(lock);
283     }
284
285     // create damage for client applications which wish to know the update timing
286     if(!replacingSurface && mRenderNotification)
287     {
288       // use notification trigger
289       // Tell the event-thread to render the tbm_surface
290       mRenderNotification->Trigger();
291     }
292
293     if(mThreadSynchronization)
294     {
295       // wait until the event-thread completed to use the tbm_surface
296       mThreadSynchronization->PostRenderWaitForCompletion();
297     }
298
299     // release the consumed surface after post render was completed
300     ReleaseDrawable();
301   }
302   else
303   {
304     // create damage for client applications which wish to know the update timing
305     if(!replacingSurface && mRenderNotification)
306     {
307       // use notification trigger
308       // Tell the event-thread to render the tbm_surface
309       mRenderNotification->Trigger();
310     }
311   }
312 }
313
314 void NativeRenderSurfaceEcoreWl::StopRender()
315 {
316   ReleaseLock();
317 }
318
319 void NativeRenderSurfaceEcoreWl::SetThreadSynchronization(ThreadSynchronizationInterface& threadSynchronization)
320 {
321   mThreadSynchronization = &threadSynchronization;
322 }
323
324 Dali::RenderSurfaceInterface::Type NativeRenderSurfaceEcoreWl::GetSurfaceType()
325 {
326   return Dali::RenderSurfaceInterface::NATIVE_RENDER_SURFACE;
327 }
328
329 void NativeRenderSurfaceEcoreWl::MakeContextCurrent()
330 {
331   if(mEGL != nullptr)
332   {
333     mEGL->MakeContextCurrent(mEGLSurface, mEGLContext);
334   }
335 }
336
337 Integration::DepthBufferAvailable NativeRenderSurfaceEcoreWl::GetDepthBufferRequired()
338 {
339   return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
340 }
341
342 Integration::StencilBufferAvailable NativeRenderSurfaceEcoreWl::GetStencilBufferRequired()
343 {
344   return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
345 }
346
347 void NativeRenderSurfaceEcoreWl::ReleaseLock()
348 {
349   if(mThreadSynchronization)
350   {
351     mThreadSynchronization->PostRenderComplete();
352   }
353 }
354
355 void NativeRenderSurfaceEcoreWl::CreateNativeRenderable()
356 {
357   int width  = static_cast<int>(mSurfaceSize.GetWidth());
358   int height = static_cast<int>(mSurfaceSize.GetHeight());
359
360   // check we're creating one with a valid size
361   DALI_ASSERT_ALWAYS(width > 0 && height > 0 && "tbm_surface size is invalid");
362
363   mTbmQueue = tbm_surface_queue_create(3, width, height, mTbmFormat, TBM_BO_DEFAULT);
364
365   if(mTbmQueue)
366   {
367     mOwnSurface = true;
368   }
369   else
370   {
371     mOwnSurface = false;
372   }
373 }
374
375 void NativeRenderSurfaceEcoreWl::ReleaseDrawable()
376 {
377   if(mConsumeSurface)
378   {
379     tbm_surface_internal_unref(mConsumeSurface);
380
381     if(tbm_surface_internal_is_valid(mConsumeSurface))
382     {
383       tbm_surface_queue_release(mTbmQueue, mConsumeSurface);
384     }
385     mConsumeSurface = NULL;
386   }
387 }
388
389 } // namespace Dali