Call MakeContextCurrent in PreRender
[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   MakeContextCurrent();
245   return true;
246 }
247
248 void NativeRenderSurfaceEcoreWl::PostRender(bool renderToFbo, bool replacingSurface, bool resizingSurface, const std::vector<Rect<int>>& damagedRects)
249 {
250   auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
251   if(eglGraphics)
252   {
253     Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
254     eglImpl.SwapBuffers(mEGLSurface, damagedRects);
255   }
256
257   //TODO: Move calling tbm_surface_queue_acruie to OffscreenWindow and Scene in EvasPlugin
258   if(mOwnSurface)
259   {
260     if(mThreadSynchronization)
261     {
262       mThreadSynchronization->PostRenderStarted();
263     }
264
265     if(tbm_surface_queue_can_acquire(mTbmQueue, 1))
266     {
267       if(tbm_surface_queue_acquire(mTbmQueue, &mConsumeSurface) != TBM_SURFACE_QUEUE_ERROR_NONE)
268       {
269         DALI_LOG_ERROR("Failed to acquire a tbm_surface\n");
270         return;
271       }
272     }
273
274     if(mConsumeSurface)
275     {
276       tbm_surface_internal_ref(mConsumeSurface);
277     }
278
279     if(replacingSurface)
280     {
281       ConditionalWait::ScopedLock lock(mTbmSurfaceCondition);
282       mDrawableCompleted = true;
283       mTbmSurfaceCondition.Notify(lock);
284     }
285
286     // create damage for client applications which wish to know the update timing
287     if(!replacingSurface && mRenderNotification)
288     {
289       // use notification trigger
290       // Tell the event-thread to render the tbm_surface
291       mRenderNotification->Trigger();
292     }
293
294     if(mThreadSynchronization)
295     {
296       // wait until the event-thread completed to use the tbm_surface
297       mThreadSynchronization->PostRenderWaitForCompletion();
298     }
299
300     // release the consumed surface after post render was completed
301     ReleaseDrawable();
302   }
303   else
304   {
305     // create damage for client applications which wish to know the update timing
306     if(!replacingSurface && mRenderNotification)
307     {
308       // use notification trigger
309       // Tell the event-thread to render the tbm_surface
310       mRenderNotification->Trigger();
311     }
312   }
313 }
314
315 void NativeRenderSurfaceEcoreWl::StopRender()
316 {
317   ReleaseLock();
318 }
319
320 void NativeRenderSurfaceEcoreWl::SetThreadSynchronization(ThreadSynchronizationInterface& threadSynchronization)
321 {
322   mThreadSynchronization = &threadSynchronization;
323 }
324
325 Dali::RenderSurfaceInterface::Type NativeRenderSurfaceEcoreWl::GetSurfaceType()
326 {
327   return Dali::RenderSurfaceInterface::NATIVE_RENDER_SURFACE;
328 }
329
330 void NativeRenderSurfaceEcoreWl::MakeContextCurrent()
331 {
332   if(mEGL != nullptr)
333   {
334     mEGL->MakeContextCurrent(mEGLSurface, mEGLContext);
335   }
336 }
337
338 Integration::DepthBufferAvailable NativeRenderSurfaceEcoreWl::GetDepthBufferRequired()
339 {
340   return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
341 }
342
343 Integration::StencilBufferAvailable NativeRenderSurfaceEcoreWl::GetStencilBufferRequired()
344 {
345   return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
346 }
347
348 void NativeRenderSurfaceEcoreWl::ReleaseLock()
349 {
350   if(mThreadSynchronization)
351   {
352     mThreadSynchronization->PostRenderComplete();
353   }
354 }
355
356 void NativeRenderSurfaceEcoreWl::CreateNativeRenderable()
357 {
358   int width  = static_cast<int>(mSurfaceSize.GetWidth());
359   int height = static_cast<int>(mSurfaceSize.GetHeight());
360
361   // check we're creating one with a valid size
362   DALI_ASSERT_ALWAYS(width > 0 && height > 0 && "tbm_surface size is invalid");
363
364   mTbmQueue = tbm_surface_queue_create(3, width, height, mTbmFormat, TBM_BO_DEFAULT);
365
366   if(mTbmQueue)
367   {
368     mOwnSurface = true;
369   }
370   else
371   {
372     mOwnSurface = false;
373   }
374 }
375
376 void NativeRenderSurfaceEcoreWl::ReleaseDrawable()
377 {
378   if(mConsumeSurface)
379   {
380     tbm_surface_internal_unref(mConsumeSurface);
381
382     if(tbm_surface_internal_is_valid(mConsumeSurface))
383     {
384       tbm_surface_queue_release(mTbmQueue, mConsumeSurface);
385     }
386     mConsumeSurface = NULL;
387   }
388 }
389
390 } // namespace Dali