2 * Copyright (c) 2020 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/gl-abstraction.h>
23 #include <dali/integration-api/debug.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>
50 #if defined(DEBUG_ENABLED)
51 Debug::Filter* gNativeSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_NATIVE_RENDER_SURFACE");
54 } // unnamed namespace
56 NativeRenderSurfaceEcoreWl::NativeRenderSurfaceEcoreWl( SurfaceSize surfaceSize, Any surface, bool isTransparent )
57 : mRenderNotification( NULL ),
60 mEGLSurface( nullptr ),
61 mEGLContext( nullptr ),
63 mDrawableCompleted( false ),
65 mConsumeSurface( NULL ),
66 mThreadSynchronization( NULL )
68 Dali::Internal::Adaptor::WindowSystem::Initialize();
72 mSurfaceSize = surfaceSize;
73 mColorDepth = isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24;
74 mTbmFormat = isTransparent ? TBM_FORMAT_ARGB8888 : TBM_FORMAT_RGB888;
75 CreateNativeRenderable();
79 mTbmQueue = AnyCast< tbm_surface_queue_h >( surface );
81 uint16_t width = static_cast<uint16_t>( tbm_surface_queue_get_width( mTbmQueue ) );
82 uint16_t height = static_cast<uint16_t>( tbm_surface_queue_get_height( mTbmQueue ) );
83 mSurfaceSize = SurfaceSize( width, height );
85 mTbmFormat = tbm_surface_queue_get_format( mTbmQueue );
87 mColorDepth = ( mTbmFormat == TBM_FORMAT_ARGB8888 ) ? COLOR_DEPTH_32 : COLOR_DEPTH_24;
91 NativeRenderSurfaceEcoreWl::~NativeRenderSurfaceEcoreWl()
98 // release the surface if we own one
105 tbm_surface_queue_destroy( mTbmQueue );
108 DALI_LOG_INFO( gNativeSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n" );
111 Dali::Internal::Adaptor::WindowSystem::Shutdown();
114 Any NativeRenderSurfaceEcoreWl::GetDrawable()
116 return mConsumeSurface;
119 void NativeRenderSurfaceEcoreWl::SetRenderNotification( TriggerEventInterface* renderNotification )
121 mRenderNotification = renderNotification;
124 void NativeRenderSurfaceEcoreWl::WaitUntilSurfaceReplaced()
126 ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
127 while( !mDrawableCompleted )
129 mTbmSurfaceCondition.Wait( lock );
132 mDrawableCompleted = false;
135 Any NativeRenderSurfaceEcoreWl::GetNativeRenderable()
140 PositionSize NativeRenderSurfaceEcoreWl::GetPositionSize() const
142 return PositionSize( 0, 0, static_cast<int>( mSurfaceSize.GetWidth() ), static_cast<int>( mSurfaceSize.GetHeight() ) );
145 void NativeRenderSurfaceEcoreWl::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
150 // 1 inch = 25.4 millimeters
151 #ifdef ECORE_WAYLAND2
152 // TODO: Application should set dpi value in wayland2
156 xres = ecore_wl_dpi_get();
157 yres = ecore_wl_dpi_get();
160 dpiHorizontal = int( xres + 0.5f ); // rounding
161 dpiVertical = int( yres + 0.5f );
164 int NativeRenderSurfaceEcoreWl::GetOrientation() const
169 void NativeRenderSurfaceEcoreWl::InitializeGraphics()
171 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
173 mGraphics = &mAdaptor->GetGraphicsInterface();
174 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
176 mEGL = &eglGraphics->GetEglInterface();
178 if ( mEGLContext == NULL )
180 // Create the OpenGL context for this window
181 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
182 eglImpl.CreateWindowContext( mEGLContext );
184 // Create the OpenGL surface
189 void NativeRenderSurfaceEcoreWl::CreateSurface()
191 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
193 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
194 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
196 mEGLSurface = eglImpl.CreateSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ), mColorDepth );
199 void NativeRenderSurfaceEcoreWl::DestroySurface()
201 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
203 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
204 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
206 eglImpl.DestroySurface( mEGLSurface );
209 bool NativeRenderSurfaceEcoreWl::ReplaceGraphicsSurface()
211 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
218 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
219 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
221 return eglImpl.ReplaceSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ), mEGLSurface, mEGLContext );
224 void NativeRenderSurfaceEcoreWl::MoveResize( Dali::PositionSize positionSize )
226 tbm_surface_queue_error_e error = TBM_SURFACE_QUEUE_ERROR_NONE;
228 error = tbm_surface_queue_reset( mTbmQueue, positionSize.width, positionSize.height, mTbmFormat );
230 if( error != TBM_SURFACE_QUEUE_ERROR_NONE )
232 DALI_LOG_ERROR( "Failed to resize tbm_surface_queue" );
235 mSurfaceSize.SetWidth( static_cast<uint16_t>( positionSize.width ) );
236 mSurfaceSize.SetHeight( static_cast<uint16_t>( positionSize.height ) );
239 void NativeRenderSurfaceEcoreWl::StartRender()
243 bool NativeRenderSurfaceEcoreWl::PreRender( bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect )
245 //TODO: Need to support partial update
249 void NativeRenderSurfaceEcoreWl::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface, const std::vector<Rect<int>>& damagedRects )
251 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
254 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
255 eglImpl.SwapBuffers( mEGLSurface, damagedRects );
258 //TODO: Move calling tbm_surface_queue_acruie to OffscreenWindow and Scene in EvasPlugin
261 if( mThreadSynchronization )
263 mThreadSynchronization->PostRenderStarted();
266 if( tbm_surface_queue_can_acquire( mTbmQueue, 1 ) )
268 if( tbm_surface_queue_acquire( mTbmQueue, &mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
270 DALI_LOG_ERROR( "Failed to acquire a tbm_surface\n" );
275 if ( mConsumeSurface )
277 tbm_surface_internal_ref( mConsumeSurface );
280 if( replacingSurface )
282 ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
283 mDrawableCompleted = true;
284 mTbmSurfaceCondition.Notify( lock );
287 // create damage for client applications which wish to know the update timing
288 if( !replacingSurface && mRenderNotification )
290 // use notification trigger
291 // Tell the event-thread to render the tbm_surface
292 mRenderNotification->Trigger();
295 if( mThreadSynchronization )
297 // wait until the event-thread completed to use the tbm_surface
298 mThreadSynchronization->PostRenderWaitForCompletion();
301 // release the consumed surface after post render was completed
306 // create damage for client applications which wish to know the update timing
307 if( !replacingSurface && mRenderNotification )
309 // use notification trigger
310 // Tell the event-thread to render the tbm_surface
311 mRenderNotification->Trigger();
316 void NativeRenderSurfaceEcoreWl::StopRender()
321 void NativeRenderSurfaceEcoreWl::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
323 mThreadSynchronization = &threadSynchronization;
326 Dali::RenderSurfaceInterface::Type NativeRenderSurfaceEcoreWl::GetSurfaceType()
328 return Dali::RenderSurfaceInterface::NATIVE_RENDER_SURFACE;
331 void NativeRenderSurfaceEcoreWl::MakeContextCurrent()
333 if ( mEGL != nullptr )
335 mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
339 Integration::DepthBufferAvailable NativeRenderSurfaceEcoreWl::GetDepthBufferRequired()
341 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
344 Integration::StencilBufferAvailable NativeRenderSurfaceEcoreWl::GetStencilBufferRequired()
346 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
349 void NativeRenderSurfaceEcoreWl::ReleaseLock()
351 if( mThreadSynchronization )
353 mThreadSynchronization->PostRenderComplete();
357 void NativeRenderSurfaceEcoreWl::CreateNativeRenderable()
359 int width = static_cast<int>( mSurfaceSize.GetWidth() );
360 int height = static_cast<int>( mSurfaceSize.GetHeight() );
362 // check we're creating one with a valid size
363 DALI_ASSERT_ALWAYS( width > 0 && height > 0 && "tbm_surface size is invalid" );
365 mTbmQueue = tbm_surface_queue_create( 3, width, height, mTbmFormat, TBM_BO_DEFAULT );
377 void NativeRenderSurfaceEcoreWl::ReleaseDrawable()
379 if( mConsumeSurface )
381 tbm_surface_internal_unref( mConsumeSurface );
383 if( tbm_surface_internal_is_valid( mConsumeSurface ) )
385 tbm_surface_queue_release( mTbmQueue, mConsumeSurface );
387 mConsumeSurface = NULL;