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
247 void NativeRenderSurfaceEcoreWl::PostRender(bool renderToFbo, bool replacingSurface, bool resizingSurface, const std::vector<Rect<int>>& damagedRects)
249 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
252 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
253 eglImpl.SwapBuffers(mEGLSurface, damagedRects);
256 //TODO: Move calling tbm_surface_queue_acruie to OffscreenWindow and Scene in EvasPlugin
259 if(mThreadSynchronization)
261 mThreadSynchronization->PostRenderStarted();
264 if(tbm_surface_queue_can_acquire(mTbmQueue, 1))
266 if(tbm_surface_queue_acquire(mTbmQueue, &mConsumeSurface) != TBM_SURFACE_QUEUE_ERROR_NONE)
268 DALI_LOG_ERROR("Failed to acquire a tbm_surface\n");
275 tbm_surface_internal_ref(mConsumeSurface);
280 ConditionalWait::ScopedLock lock(mTbmSurfaceCondition);
281 mDrawableCompleted = true;
282 mTbmSurfaceCondition.Notify(lock);
285 // create damage for client applications which wish to know the update timing
286 if(!replacingSurface && mRenderNotification)
288 // use notification trigger
289 // Tell the event-thread to render the tbm_surface
290 mRenderNotification->Trigger();
293 if(mThreadSynchronization)
295 // wait until the event-thread completed to use the tbm_surface
296 mThreadSynchronization->PostRenderWaitForCompletion();
299 // release the consumed surface after post render was completed
304 // create damage for client applications which wish to know the update timing
305 if(!replacingSurface && mRenderNotification)
307 // use notification trigger
308 // Tell the event-thread to render the tbm_surface
309 mRenderNotification->Trigger();
314 void NativeRenderSurfaceEcoreWl::StopRender()
319 void NativeRenderSurfaceEcoreWl::SetThreadSynchronization(ThreadSynchronizationInterface& threadSynchronization)
321 mThreadSynchronization = &threadSynchronization;
324 Dali::RenderSurfaceInterface::Type NativeRenderSurfaceEcoreWl::GetSurfaceType()
326 return Dali::RenderSurfaceInterface::NATIVE_RENDER_SURFACE;
329 void NativeRenderSurfaceEcoreWl::MakeContextCurrent()
333 mEGL->MakeContextCurrent(mEGLSurface, mEGLContext);
337 Integration::DepthBufferAvailable NativeRenderSurfaceEcoreWl::GetDepthBufferRequired()
339 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
342 Integration::StencilBufferAvailable NativeRenderSurfaceEcoreWl::GetStencilBufferRequired()
344 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
347 void NativeRenderSurfaceEcoreWl::ReleaseLock()
349 if(mThreadSynchronization)
351 mThreadSynchronization->PostRenderComplete();
355 void NativeRenderSurfaceEcoreWl::CreateNativeRenderable()
357 int width = static_cast<int>(mSurfaceSize.GetWidth());
358 int height = static_cast<int>(mSurfaceSize.GetHeight());
360 // check we're creating one with a valid size
361 DALI_ASSERT_ALWAYS(width > 0 && height > 0 && "tbm_surface size is invalid");
363 mTbmQueue = tbm_surface_queue_create(3, width, height, mTbmFormat, TBM_BO_DEFAULT);
375 void NativeRenderSurfaceEcoreWl::ReleaseDrawable()
379 tbm_surface_internal_unref(mConsumeSurface);
381 if(tbm_surface_internal_is_valid(mConsumeSurface))
383 tbm_surface_queue_release(mTbmQueue, mConsumeSurface);
385 mConsumeSurface = NULL;