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),
62 mThreadSynchronization(NULL)
64 Dali::Internal::Adaptor::WindowSystem::Initialize();
68 mSurfaceSize = surfaceSize;
69 mColorDepth = isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24;
70 mTbmFormat = isTransparent ? TBM_FORMAT_ARGB8888 : TBM_FORMAT_RGB888;
71 CreateNativeRenderable();
75 mTbmQueue = AnyCast<tbm_surface_queue_h>(surface);
77 uint16_t width = static_cast<uint16_t>(tbm_surface_queue_get_width(mTbmQueue));
78 uint16_t height = static_cast<uint16_t>(tbm_surface_queue_get_height(mTbmQueue));
79 mSurfaceSize = SurfaceSize(width, height);
81 mTbmFormat = tbm_surface_queue_get_format(mTbmQueue);
83 mColorDepth = (mTbmFormat == TBM_FORMAT_ARGB8888) ? COLOR_DEPTH_32 : COLOR_DEPTH_24;
87 NativeRenderSurfaceEcoreWl::~NativeRenderSurfaceEcoreWl()
94 // release the surface if we own one
99 tbm_surface_queue_destroy(mTbmQueue);
102 DALI_LOG_INFO(gNativeSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n");
105 Dali::Internal::Adaptor::WindowSystem::Shutdown();
108 void NativeRenderSurfaceEcoreWl::SetRenderNotification(TriggerEventInterface* renderNotification)
110 mRenderNotification = renderNotification;
113 Any NativeRenderSurfaceEcoreWl::GetNativeRenderable()
118 PositionSize NativeRenderSurfaceEcoreWl::GetPositionSize() const
120 return PositionSize(0, 0, static_cast<int>(mSurfaceSize.GetWidth()), static_cast<int>(mSurfaceSize.GetHeight()));
123 void NativeRenderSurfaceEcoreWl::GetDpi(unsigned int& dpiHorizontal, unsigned int& dpiVertical)
128 // 1 inch = 25.4 millimeters
129 #ifdef ECORE_WAYLAND2
130 // TODO: Application should set dpi value in wayland2
134 xres = ecore_wl_dpi_get();
135 yres = ecore_wl_dpi_get();
138 dpiHorizontal = int(xres + 0.5f); // rounding
139 dpiVertical = int(yres + 0.5f);
142 int NativeRenderSurfaceEcoreWl::GetOrientation() const
147 void NativeRenderSurfaceEcoreWl::InitializeGraphics()
149 DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
151 mGraphics = &mAdaptor->GetGraphicsInterface();
152 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
154 mEGL = &eglGraphics->GetEglInterface();
156 if(mEGLContext == NULL)
158 // Create the OpenGL context for this window
159 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
160 eglImpl.CreateWindowContext(mEGLContext);
162 // Create the OpenGL surface
167 void NativeRenderSurfaceEcoreWl::CreateSurface()
169 DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
171 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
172 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
174 mEGLSurface = eglImpl.CreateSurfaceWindow(reinterpret_cast<EGLNativeWindowType>(mTbmQueue), mColorDepth);
177 void NativeRenderSurfaceEcoreWl::DestroySurface()
179 DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
181 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
182 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
184 eglImpl.DestroySurface(mEGLSurface);
187 bool NativeRenderSurfaceEcoreWl::ReplaceGraphicsSurface()
189 DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
196 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
197 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
199 return eglImpl.ReplaceSurfaceWindow(reinterpret_cast<EGLNativeWindowType>(mTbmQueue), mEGLSurface, mEGLContext);
202 void NativeRenderSurfaceEcoreWl::MoveResize(Dali::PositionSize positionSize)
204 tbm_surface_queue_error_e error = TBM_SURFACE_QUEUE_ERROR_NONE;
206 error = tbm_surface_queue_reset(mTbmQueue, positionSize.width, positionSize.height, mTbmFormat);
208 if(error != TBM_SURFACE_QUEUE_ERROR_NONE)
210 DALI_LOG_ERROR("Failed to resize tbm_surface_queue");
213 mSurfaceSize.SetWidth(static_cast<uint16_t>(positionSize.width));
214 mSurfaceSize.SetHeight(static_cast<uint16_t>(positionSize.height));
217 void NativeRenderSurfaceEcoreWl::StartRender()
221 bool NativeRenderSurfaceEcoreWl::PreRender(bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
223 if(!clippingRect.IsEmpty())
225 mDamagedRects.assign(damagedRects.begin(), damagedRects.end());
229 mDamagedRects.clear();
232 //TODO: Need to support partial update
233 // This is now done when the render pass for the render surface begins
234 // MakeContextCurrent();
238 void NativeRenderSurfaceEcoreWl::PostRender()
240 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
243 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
244 eglImpl.SwapBuffers(mEGLSurface, mDamagedRects);
247 if(mRenderNotification)
249 if(mThreadSynchronization)
251 mThreadSynchronization->PostRenderStarted();
254 // Tell the event-thread to render the tbm_surface
255 mRenderNotification->Trigger();
257 if(mThreadSynchronization)
259 // wait until the event-thread completed to use the tbm_surface
260 mThreadSynchronization->PostRenderWaitForCompletion();
265 void NativeRenderSurfaceEcoreWl::StopRender()
270 void NativeRenderSurfaceEcoreWl::SetThreadSynchronization(ThreadSynchronizationInterface& threadSynchronization)
272 mThreadSynchronization = &threadSynchronization;
275 Dali::RenderSurfaceInterface::Type NativeRenderSurfaceEcoreWl::GetSurfaceType()
277 return Dali::RenderSurfaceInterface::NATIVE_RENDER_SURFACE;
280 void NativeRenderSurfaceEcoreWl::MakeContextCurrent()
284 mEGL->MakeContextCurrent(mEGLSurface, mEGLContext);
288 Integration::DepthBufferAvailable NativeRenderSurfaceEcoreWl::GetDepthBufferRequired()
290 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
293 Integration::StencilBufferAvailable NativeRenderSurfaceEcoreWl::GetStencilBufferRequired()
295 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
298 void NativeRenderSurfaceEcoreWl::ReleaseLock()
300 if(mThreadSynchronization)
302 mThreadSynchronization->PostRenderComplete();
306 void NativeRenderSurfaceEcoreWl::CreateNativeRenderable()
308 int width = static_cast<int>(mSurfaceSize.GetWidth());
309 int height = static_cast<int>(mSurfaceSize.GetHeight());
311 // check we're creating one with a valid size
312 DALI_ASSERT_ALWAYS(width > 0 && height > 0 && "tbm_surface size is invalid");
314 mTbmQueue = tbm_surface_queue_create(3, width, height, mTbmFormat, TBM_BO_DEFAULT);