c215b3074488948c1936f54b5d64044f2c981714
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / tizen-wayland / native-render-surface-ecore-wl.cpp
1 /*
2  * Copyright (c) 2023 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
43 namespace Dali
44 {
45 namespace
46 {
47 #if defined(DEBUG_ENABLED)
48 Debug::Filter* gNativeSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_NATIVE_RENDER_SURFACE");
49 #endif
50
51 } // unnamed namespace
52
53 NativeRenderSurfaceEcoreWl::NativeRenderSurfaceEcoreWl(SurfaceSize surfaceSize, Any surface, bool isTransparent)
54 : mRenderNotification(NULL),
55   mGraphics(NULL),
56   mEGL(nullptr),
57   mEGLSurface(nullptr),
58   mEGLContext(nullptr),
59   mOwnSurface(false),
60   mTbmQueue(NULL),
61   mThreadSynchronization(NULL)
62 {
63   if(surface.Empty())
64   {
65     mSurfaceSize = surfaceSize;
66     mColorDepth  = isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24;
67     mTbmFormat   = isTransparent ? TBM_FORMAT_ARGB8888 : TBM_FORMAT_RGB888;
68     CreateNativeRenderable();
69   }
70   else
71   {
72     mTbmQueue = AnyCast<tbm_surface_queue_h>(surface);
73
74     uint16_t width  = static_cast<uint16_t>(tbm_surface_queue_get_width(mTbmQueue));
75     uint16_t height = static_cast<uint16_t>(tbm_surface_queue_get_height(mTbmQueue));
76     mSurfaceSize    = SurfaceSize(width, height);
77
78     mTbmFormat = tbm_surface_queue_get_format(mTbmQueue);
79
80     mColorDepth = (mTbmFormat == TBM_FORMAT_ARGB8888) ? COLOR_DEPTH_32 : COLOR_DEPTH_24;
81   }
82 }
83
84 NativeRenderSurfaceEcoreWl::~NativeRenderSurfaceEcoreWl()
85 {
86   if(mEGLSurface)
87   {
88     DestroySurface();
89   }
90
91   // release the surface if we own one
92   if(mOwnSurface)
93   {
94     if(mTbmQueue)
95     {
96       tbm_surface_queue_destroy(mTbmQueue);
97     }
98
99     DALI_LOG_INFO(gNativeSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n");
100   }
101 }
102
103 void NativeRenderSurfaceEcoreWl::SetRenderNotification(TriggerEventInterface* renderNotification)
104 {
105   mRenderNotification = renderNotification;
106 }
107
108 Any NativeRenderSurfaceEcoreWl::GetNativeRenderable()
109 {
110   return mTbmQueue;
111 }
112
113 PositionSize NativeRenderSurfaceEcoreWl::GetPositionSize() const
114 {
115   return PositionSize(0, 0, static_cast<int>(mSurfaceSize.GetWidth()), static_cast<int>(mSurfaceSize.GetHeight()));
116 }
117
118 void NativeRenderSurfaceEcoreWl::GetDpi(unsigned int& dpiHorizontal, unsigned int& dpiVertical)
119 {
120   // calculate DPI
121   float xres, yres;
122
123   // 1 inch = 25.4 millimeters
124 #ifdef ECORE_WAYLAND2
125   // TODO: Application should set dpi value in wayland2
126   xres = 96;
127   yres = 96;
128 #else
129   xres = ecore_wl_dpi_get();
130   yres = ecore_wl_dpi_get();
131 #endif
132
133   dpiHorizontal = int(xres + 0.5f); // rounding
134   dpiVertical   = int(yres + 0.5f);
135 }
136
137 int NativeRenderSurfaceEcoreWl::GetSurfaceOrientation() const
138 {
139   return 0;
140 }
141
142 int NativeRenderSurfaceEcoreWl::GetScreenOrientation() const
143 {
144   return 0;
145 }
146
147 void NativeRenderSurfaceEcoreWl::InitializeGraphics()
148 {
149   DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
150
151   mGraphics        = &mAdaptor->GetGraphicsInterface();
152   auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
153
154   mEGL = &eglGraphics->GetEglInterface();
155
156   if(mEGLContext == NULL)
157   {
158     // Create the OpenGL context for this window
159     Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
160     eglImpl.CreateWindowContext(mEGLContext);
161
162     // Create the OpenGL surface
163     CreateSurface();
164   }
165 }
166
167 void NativeRenderSurfaceEcoreWl::CreateSurface()
168 {
169   DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
170
171   auto                                  eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
172   Internal::Adaptor::EglImplementation& eglImpl     = eglGraphics->GetEglImplementation();
173
174   mEGLSurface = eglImpl.CreateSurfaceWindow(reinterpret_cast<EGLNativeWindowType>(mTbmQueue), mColorDepth);
175 }
176
177 void NativeRenderSurfaceEcoreWl::DestroySurface()
178 {
179   DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
180
181   auto                                  eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
182   Internal::Adaptor::EglImplementation& eglImpl     = eglGraphics->GetEglImplementation();
183
184   eglImpl.DestroySurface(mEGLSurface);
185 }
186
187 bool NativeRenderSurfaceEcoreWl::ReplaceGraphicsSurface()
188 {
189   DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
190
191   if(!mTbmQueue)
192   {
193     return false;
194   }
195
196   auto                                  eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
197   Internal::Adaptor::EglImplementation& eglImpl     = eglGraphics->GetEglImplementation();
198
199   return eglImpl.ReplaceSurfaceWindow(reinterpret_cast<EGLNativeWindowType>(mTbmQueue), mEGLSurface, mEGLContext);
200 }
201
202 void NativeRenderSurfaceEcoreWl::MoveResize(Dali::PositionSize positionSize)
203 {
204   tbm_surface_queue_error_e error = TBM_SURFACE_QUEUE_ERROR_NONE;
205
206   error = tbm_surface_queue_reset(mTbmQueue, positionSize.width, positionSize.height, mTbmFormat);
207
208   if(error != TBM_SURFACE_QUEUE_ERROR_NONE)
209   {
210     DALI_LOG_ERROR("Failed to resize tbm_surface_queue");
211   }
212
213   mSurfaceSize.SetWidth(static_cast<uint16_t>(positionSize.width));
214   mSurfaceSize.SetHeight(static_cast<uint16_t>(positionSize.height));
215 }
216
217 void NativeRenderSurfaceEcoreWl::StartRender()
218 {
219 }
220
221 bool NativeRenderSurfaceEcoreWl::PreRender(bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
222 {
223   if(!clippingRect.IsEmpty())
224   {
225     mDamagedRects.assign(damagedRects.begin(), damagedRects.end());
226   }
227   else
228   {
229     mDamagedRects.clear();
230   }
231
232   //TODO: Need to support partial update
233   // This is now done when the render pass for the render surface begins
234   //  MakeContextCurrent();
235   return true;
236 }
237
238 void NativeRenderSurfaceEcoreWl::PostRender()
239 {
240   auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
241   if(eglGraphics)
242   {
243     Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
244     eglImpl.SwapBuffers(mEGLSurface, mDamagedRects);
245   }
246
247   if(mRenderNotification)
248   {
249     if(mThreadSynchronization)
250     {
251       mThreadSynchronization->PostRenderStarted();
252     }
253
254     // Tell the event-thread to render the tbm_surface
255     mRenderNotification->Trigger();
256
257     if(mThreadSynchronization)
258     {
259       // wait until the event-thread completed to use the tbm_surface
260       mThreadSynchronization->PostRenderWaitForCompletion();
261     }
262   }
263 }
264
265 void NativeRenderSurfaceEcoreWl::StopRender()
266 {
267   ReleaseLock();
268 }
269
270 void NativeRenderSurfaceEcoreWl::SetThreadSynchronization(ThreadSynchronizationInterface& threadSynchronization)
271 {
272   mThreadSynchronization = &threadSynchronization;
273 }
274
275 Dali::RenderSurfaceInterface::Type NativeRenderSurfaceEcoreWl::GetSurfaceType()
276 {
277   return Dali::RenderSurfaceInterface::NATIVE_RENDER_SURFACE;
278 }
279
280 void NativeRenderSurfaceEcoreWl::MakeContextCurrent()
281 {
282   if(mEGL != nullptr)
283   {
284     mEGL->MakeContextCurrent(mEGLSurface, mEGLContext);
285   }
286 }
287
288 Integration::DepthBufferAvailable NativeRenderSurfaceEcoreWl::GetDepthBufferRequired()
289 {
290   return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
291 }
292
293 Integration::StencilBufferAvailable NativeRenderSurfaceEcoreWl::GetStencilBufferRequired()
294 {
295   return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
296 }
297
298 void NativeRenderSurfaceEcoreWl::ReleaseLock()
299 {
300   if(mThreadSynchronization)
301   {
302     mThreadSynchronization->PostRenderComplete();
303   }
304 }
305
306 void NativeRenderSurfaceEcoreWl::CreateNativeRenderable()
307 {
308   int width  = static_cast<int>(mSurfaceSize.GetWidth());
309   int height = static_cast<int>(mSurfaceSize.GetHeight());
310
311   // check we're creating one with a valid size
312   DALI_ASSERT_ALWAYS(width > 0 && height > 0 && "tbm_surface size is invalid");
313
314   mTbmQueue = tbm_surface_queue_create(3, width, height, mTbmFormat, TBM_BO_DEFAULT);
315
316   if(mTbmQueue)
317   {
318     mOwnSurface = true;
319   }
320   else
321   {
322     mOwnSurface = false;
323   }
324 }
325
326 } // namespace Dali