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/window-system/tizen-wayland/native-render-surface-ecore-wl.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/gl-abstraction.h>
26 #include <Ecore_Wl2.h>
28 #include <Ecore_Wayland.h>
31 #include <tbm_bufmgr.h>
32 #include <tbm_surface_internal.h>
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>
48 #if defined(DEBUG_ENABLED)
49 Debug::Filter* gNativeSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_NATIVE_RENDER_SURFACE");
52 } // unnamed namespace
54 NativeRenderSurfaceEcoreWl::NativeRenderSurfaceEcoreWl(SurfaceSize surfaceSize, Any surface, bool isTransparent)
55 : mRenderNotification(NULL),
61 mDrawableCompleted(false),
63 mConsumeSurface(NULL),
64 mThreadSynchronization(NULL)
66 Dali::Internal::Adaptor::WindowSystem::Initialize();
70 mSurfaceSize = surfaceSize;
71 mColorDepth = isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24;
72 mTbmFormat = isTransparent ? TBM_FORMAT_ARGB8888 : TBM_FORMAT_RGB888;
73 CreateNativeRenderable();
77 mTbmQueue = AnyCast<tbm_surface_queue_h>(surface);
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);
83 mTbmFormat = tbm_surface_queue_get_format(mTbmQueue);
85 mColorDepth = (mTbmFormat == TBM_FORMAT_ARGB8888) ? COLOR_DEPTH_32 : COLOR_DEPTH_24;
89 NativeRenderSurfaceEcoreWl::~NativeRenderSurfaceEcoreWl()
96 // release the surface if we own one
103 tbm_surface_queue_destroy(mTbmQueue);
106 DALI_LOG_INFO(gNativeSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n");
109 Dali::Internal::Adaptor::WindowSystem::Shutdown();
112 Any NativeRenderSurfaceEcoreWl::GetDrawable()
114 return mConsumeSurface;
117 void NativeRenderSurfaceEcoreWl::SetRenderNotification(TriggerEventInterface* renderNotification)
119 mRenderNotification = renderNotification;
122 void NativeRenderSurfaceEcoreWl::WaitUntilSurfaceReplaced()
124 ConditionalWait::ScopedLock lock(mTbmSurfaceCondition);
125 while(!mDrawableCompleted)
127 mTbmSurfaceCondition.Wait(lock);
130 mDrawableCompleted = false;
133 Any NativeRenderSurfaceEcoreWl::GetNativeRenderable()
138 PositionSize NativeRenderSurfaceEcoreWl::GetPositionSize() const
140 return PositionSize(0, 0, static_cast<int>(mSurfaceSize.GetWidth()), static_cast<int>(mSurfaceSize.GetHeight()));
143 void NativeRenderSurfaceEcoreWl::GetDpi(unsigned int& dpiHorizontal, unsigned int& dpiVertical)
148 // 1 inch = 25.4 millimeters
149 #ifdef ECORE_WAYLAND2
150 // TODO: Application should set dpi value in wayland2
154 xres = ecore_wl_dpi_get();
155 yres = ecore_wl_dpi_get();
158 dpiHorizontal = int(xres + 0.5f); // rounding
159 dpiVertical = int(yres + 0.5f);
162 int NativeRenderSurfaceEcoreWl::GetOrientation() const
167 void NativeRenderSurfaceEcoreWl::InitializeGraphics()
169 DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
171 mGraphics = &mAdaptor->GetGraphicsInterface();
172 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
174 mEGL = &eglGraphics->GetEglInterface();
176 if(mEGLContext == NULL)
178 // Create the OpenGL context for this window
179 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
180 eglImpl.CreateWindowContext(mEGLContext);
182 // Create the OpenGL surface
187 void NativeRenderSurfaceEcoreWl::CreateSurface()
189 DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
191 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
192 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
194 mEGLSurface = eglImpl.CreateSurfaceWindow(reinterpret_cast<EGLNativeWindowType>(mTbmQueue), mColorDepth);
197 void NativeRenderSurfaceEcoreWl::DestroySurface()
199 DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
201 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
202 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
204 eglImpl.DestroySurface(mEGLSurface);
207 bool NativeRenderSurfaceEcoreWl::ReplaceGraphicsSurface()
209 DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
216 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
217 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
219 return eglImpl.ReplaceSurfaceWindow(reinterpret_cast<EGLNativeWindowType>(mTbmQueue), mEGLSurface, mEGLContext);
222 void NativeRenderSurfaceEcoreWl::MoveResize(Dali::PositionSize positionSize)
224 tbm_surface_queue_error_e error = TBM_SURFACE_QUEUE_ERROR_NONE;
226 error = tbm_surface_queue_reset(mTbmQueue, positionSize.width, positionSize.height, mTbmFormat);
228 if(error != TBM_SURFACE_QUEUE_ERROR_NONE)
230 DALI_LOG_ERROR("Failed to resize tbm_surface_queue");
233 mSurfaceSize.SetWidth(static_cast<uint16_t>(positionSize.width));
234 mSurfaceSize.SetHeight(static_cast<uint16_t>(positionSize.height));
237 void NativeRenderSurfaceEcoreWl::StartRender()
241 bool NativeRenderSurfaceEcoreWl::PreRender(bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
243 //TODO: Need to support partial update
244 MakeContextCurrent();
248 void NativeRenderSurfaceEcoreWl::PostRender(bool renderToFbo, bool replacingSurface, bool resizingSurface, const std::vector<Rect<int>>& damagedRects)
250 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
253 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
254 eglImpl.SwapBuffers(mEGLSurface, damagedRects);
257 //TODO: Move calling tbm_surface_queue_acruie to OffscreenWindow and Scene in EvasPlugin
260 if(mThreadSynchronization)
262 mThreadSynchronization->PostRenderStarted();
265 if(tbm_surface_queue_can_acquire(mTbmQueue, 1))
267 if(tbm_surface_queue_acquire(mTbmQueue, &mConsumeSurface) != TBM_SURFACE_QUEUE_ERROR_NONE)
269 DALI_LOG_ERROR("Failed to acquire a tbm_surface\n");
276 tbm_surface_internal_ref(mConsumeSurface);
281 ConditionalWait::ScopedLock lock(mTbmSurfaceCondition);
282 mDrawableCompleted = true;
283 mTbmSurfaceCondition.Notify(lock);
286 // create damage for client applications which wish to know the update timing
287 if(!replacingSurface && mRenderNotification)
289 // use notification trigger
290 // Tell the event-thread to render the tbm_surface
291 mRenderNotification->Trigger();
294 if(mThreadSynchronization)
296 // wait until the event-thread completed to use the tbm_surface
297 mThreadSynchronization->PostRenderWaitForCompletion();
300 // release the consumed surface after post render was completed
305 // create damage for client applications which wish to know the update timing
306 if(!replacingSurface && mRenderNotification)
308 // use notification trigger
309 // Tell the event-thread to render the tbm_surface
310 mRenderNotification->Trigger();
315 void NativeRenderSurfaceEcoreWl::StopRender()
320 void NativeRenderSurfaceEcoreWl::SetThreadSynchronization(ThreadSynchronizationInterface& threadSynchronization)
322 mThreadSynchronization = &threadSynchronization;
325 Dali::RenderSurfaceInterface::Type NativeRenderSurfaceEcoreWl::GetSurfaceType()
327 return Dali::RenderSurfaceInterface::NATIVE_RENDER_SURFACE;
330 void NativeRenderSurfaceEcoreWl::MakeContextCurrent()
334 mEGL->MakeContextCurrent(mEGLSurface, mEGLContext);
338 Integration::DepthBufferAvailable NativeRenderSurfaceEcoreWl::GetDepthBufferRequired()
340 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
343 Integration::StencilBufferAvailable NativeRenderSurfaceEcoreWl::GetStencilBufferRequired()
345 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
348 void NativeRenderSurfaceEcoreWl::ReleaseLock()
350 if(mThreadSynchronization)
352 mThreadSynchronization->PostRenderComplete();
356 void NativeRenderSurfaceEcoreWl::CreateNativeRenderable()
358 int width = static_cast<int>(mSurfaceSize.GetWidth());
359 int height = static_cast<int>(mSurfaceSize.GetHeight());
361 // check we're creating one with a valid size
362 DALI_ASSERT_ALWAYS(width > 0 && height > 0 && "tbm_surface size is invalid");
364 mTbmQueue = tbm_surface_queue_create(3, width, height, mTbmFormat, TBM_BO_DEFAULT);
376 void NativeRenderSurfaceEcoreWl::ReleaseDrawable()
380 tbm_surface_internal_unref(mConsumeSurface);
382 if(tbm_surface_internal_is_valid(mConsumeSurface))
384 tbm_surface_queue_release(mTbmQueue, mConsumeSurface);
386 mConsumeSurface = NULL;