2 * Copyright (c) 2023 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>
25 #include <tbm_bufmgr.h>
26 #include <tbm_surface_internal.h>
29 #include <dali/integration-api/adaptor-framework/thread-synchronization-interface.h>
30 #include <dali/internal/adaptor/common/adaptor-impl.h>
31 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
32 #include <dali/internal/graphics/gles/egl-graphics.h>
33 #include <dali/internal/graphics/gles/egl-implementation.h>
34 #include <dali/internal/system/common/trigger-event.h>
35 #include <dali/internal/window-system/common/display-connection.h>
41 #if defined(DEBUG_ENABLED)
42 Debug::Filter* gNativeSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_NATIVE_RENDER_SURFACE");
45 // The callback of tbm_surface_queue_add_acquirable_cb()
46 static void TbmAcquirableCallback(tbm_surface_queue_h queue, void* data)
48 NativeRenderSurfaceEcoreWl* surface = static_cast<NativeRenderSurfaceEcoreWl*>(data);
51 surface->TriggerFrameRenderedCallback();
55 } // unnamed namespace
57 NativeRenderSurfaceEcoreWl::NativeRenderSurfaceEcoreWl(SurfaceSize surfaceSize, Any surface, bool isTransparent)
58 : mRenderNotification(NULL),
65 mThreadSynchronization(NULL)
69 mSurfaceSize = surfaceSize;
70 mColorDepth = isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24;
71 mTbmFormat = isTransparent ? TBM_FORMAT_ARGB8888 : TBM_FORMAT_RGB888;
72 CreateNativeRenderable();
76 mTbmQueue = AnyCast<tbm_surface_queue_h>(surface);
78 uint16_t width = static_cast<uint16_t>(tbm_surface_queue_get_width(mTbmQueue));
79 uint16_t height = static_cast<uint16_t>(tbm_surface_queue_get_height(mTbmQueue));
80 mSurfaceSize = SurfaceSize(width, height);
82 mTbmFormat = tbm_surface_queue_get_format(mTbmQueue);
84 mColorDepth = (mTbmFormat == TBM_FORMAT_ARGB8888) ? COLOR_DEPTH_32 : COLOR_DEPTH_24;
88 NativeRenderSurfaceEcoreWl::~NativeRenderSurfaceEcoreWl()
95 // release the surface if we own one
100 tbm_surface_queue_destroy(mTbmQueue);
103 DALI_LOG_INFO(gNativeSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n");
107 void NativeRenderSurfaceEcoreWl::SetRenderNotification(TriggerEventInterface* renderNotification)
109 mRenderNotification = renderNotification;
112 Any NativeRenderSurfaceEcoreWl::GetNativeRenderable()
117 void NativeRenderSurfaceEcoreWl::TriggerFrameRenderedCallback()
119 if(mFrameRenderedCallback)
121 mFrameRenderedCallback->Trigger();
125 void NativeRenderSurfaceEcoreWl::SetFrameRenderedCallback(CallbackBase* callback)
127 mFrameRenderedCallback = std::unique_ptr<EventThreadCallback>(new EventThreadCallback(callback));
129 tbm_surface_queue_error_e result = tbm_surface_queue_add_acquirable_cb(mTbmQueue, TbmAcquirableCallback, this);
130 if(result != TBM_ERROR_NONE)
132 DALI_LOG_ERROR("Failed calling tbm_surface_queue_add_acquirable_cb(), error : %x", result);
136 PositionSize NativeRenderSurfaceEcoreWl::GetPositionSize() const
138 return PositionSize(0, 0, static_cast<int>(mSurfaceSize.GetWidth()), static_cast<int>(mSurfaceSize.GetHeight()));
141 void NativeRenderSurfaceEcoreWl::GetDpi(unsigned int& dpiHorizontal, unsigned int& dpiVertical)
146 // 1 inch = 25.4 millimeters
150 dpiHorizontal = int(xres + 0.5f); // rounding
151 dpiVertical = int(yres + 0.5f);
154 int NativeRenderSurfaceEcoreWl::GetSurfaceOrientation() const
159 int NativeRenderSurfaceEcoreWl::GetScreenOrientation() const
164 void NativeRenderSurfaceEcoreWl::InitializeGraphics()
166 DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
168 mGraphics = &mAdaptor->GetGraphicsInterface();
169 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
171 mEGL = &eglGraphics->GetEglInterface();
173 if(mEGLContext == NULL)
175 // Create the OpenGL context for this window
176 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
177 eglImpl.CreateWindowContext(mEGLContext);
179 // Create the OpenGL surface
184 void NativeRenderSurfaceEcoreWl::CreateSurface()
186 DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
188 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
189 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
191 mEGLSurface = eglImpl.CreateSurfaceWindow(reinterpret_cast<EGLNativeWindowType>(mTbmQueue), mColorDepth);
194 void NativeRenderSurfaceEcoreWl::DestroySurface()
196 DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
198 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
199 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
201 eglImpl.DestroySurface(mEGLSurface);
204 bool NativeRenderSurfaceEcoreWl::ReplaceGraphicsSurface()
206 DALI_LOG_TRACE_METHOD(gNativeSurfaceLogFilter);
213 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
214 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
216 return eglImpl.ReplaceSurfaceWindow(reinterpret_cast<EGLNativeWindowType>(mTbmQueue), mEGLSurface, mEGLContext);
219 void NativeRenderSurfaceEcoreWl::MoveResize(Dali::PositionSize positionSize)
221 tbm_surface_queue_error_e error = TBM_SURFACE_QUEUE_ERROR_NONE;
223 error = tbm_surface_queue_reset(mTbmQueue, positionSize.width, positionSize.height, mTbmFormat);
225 if(error != TBM_SURFACE_QUEUE_ERROR_NONE)
227 DALI_LOG_ERROR("Failed to resize tbm_surface_queue");
230 mSurfaceSize.SetWidth(static_cast<uint16_t>(positionSize.width));
231 mSurfaceSize.SetHeight(static_cast<uint16_t>(positionSize.height));
234 void NativeRenderSurfaceEcoreWl::StartRender()
238 bool NativeRenderSurfaceEcoreWl::PreRender(bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
240 // Not support partial update
241 clippingRect = Rect<int32_t>(0, 0, mSurfaceSize.GetWidth(), mSurfaceSize.GetHeight());
245 void NativeRenderSurfaceEcoreWl::PostRender()
247 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
250 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
251 eglImpl.SwapBuffers(mEGLSurface);
254 if(mRenderNotification)
256 if(mThreadSynchronization)
258 mThreadSynchronization->PostRenderStarted();
261 // Tell the event-thread to render the tbm_surface
262 mRenderNotification->Trigger();
264 if(mThreadSynchronization)
266 // wait until the event-thread completed to use the tbm_surface
267 mThreadSynchronization->PostRenderWaitForCompletion();
272 void NativeRenderSurfaceEcoreWl::StopRender()
277 void NativeRenderSurfaceEcoreWl::SetThreadSynchronization(ThreadSynchronizationInterface& threadSynchronization)
279 mThreadSynchronization = &threadSynchronization;
282 Dali::RenderSurfaceInterface::Type NativeRenderSurfaceEcoreWl::GetSurfaceType()
284 return Dali::RenderSurfaceInterface::NATIVE_RENDER_SURFACE;
287 void NativeRenderSurfaceEcoreWl::MakeContextCurrent()
291 mEGL->MakeContextCurrent(mEGLSurface, mEGLContext);
295 Integration::DepthBufferAvailable NativeRenderSurfaceEcoreWl::GetDepthBufferRequired()
297 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
300 Integration::StencilBufferAvailable NativeRenderSurfaceEcoreWl::GetStencilBufferRequired()
302 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
305 void NativeRenderSurfaceEcoreWl::ReleaseLock()
307 if(mThreadSynchronization)
309 mThreadSynchronization->PostRenderComplete();
313 void NativeRenderSurfaceEcoreWl::CreateNativeRenderable()
315 int width = static_cast<int>(mSurfaceSize.GetWidth());
316 int height = static_cast<int>(mSurfaceSize.GetHeight());
318 // check we're creating one with a valid size
319 DALI_ASSERT_ALWAYS(width > 0 && height > 0 && "tbm_surface size is invalid");
321 mTbmQueue = tbm_surface_queue_create(3, width, height, mTbmFormat, TBM_BO_DEFAULT);