2 * Copyright (c) 2018 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/internal/system/common/trigger-event.h>
36 #include <dali/internal/graphics/gles20/egl-implementation.h>
37 #include <dali/internal/window-system/common/display-connection.h>
38 #include <dali/internal/window-system/common/window-system.h>
39 #include <dali/integration-api/thread-synchronization-interface.h>
47 #if defined(DEBUG_ENABLED)
48 Debug::Filter* gNativeSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_NATIVE_RENDER_SURFACE");
51 } // unnamed namespace
53 NativeRenderSurfaceEcoreWl::NativeRenderSurfaceEcoreWl( Dali::PositionSize positionSize, bool isTransparent )
54 : mPosition( positionSize ),
55 mRenderNotification( NULL ),
56 mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
57 mTbmFormat( isTransparent ? TBM_FORMAT_ARGB8888 : TBM_FORMAT_RGB888 ),
59 mDrawableCompleted( false ),
61 mConsumeSurface( NULL ),
62 mThreadSynchronization( NULL )
64 Dali::Internal::Adaptor::WindowSystem::Initialize();
66 CreateNativeRenderable();
67 setenv( "EGL_PLATFORM", "tbm", 1 );
70 NativeRenderSurfaceEcoreWl::~NativeRenderSurfaceEcoreWl()
72 // release the surface if we own one
79 tbm_surface_queue_destroy( mTbmQueue );
82 DALI_LOG_INFO( gNativeSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n" );
85 Dali::Internal::Adaptor::WindowSystem::Shutdown();
88 Any NativeRenderSurfaceEcoreWl::GetDrawable()
90 return mConsumeSurface;
93 void NativeRenderSurfaceEcoreWl::SetRenderNotification( TriggerEventInterface* renderNotification )
95 mRenderNotification = renderNotification;
98 void NativeRenderSurfaceEcoreWl::WaitUntilSurfaceReplaced()
100 ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
101 while( !mDrawableCompleted )
103 mTbmSurfaceCondition.Wait( lock );
106 mDrawableCompleted = false;
109 PositionSize NativeRenderSurfaceEcoreWl::GetPositionSize() const
114 void NativeRenderSurfaceEcoreWl::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
119 // 1 inch = 25.4 millimeters
120 #ifdef ECORE_WAYLAND2
121 // TODO: Application should set dpi value in wayland2
125 xres = ecore_wl_dpi_get();
126 yres = ecore_wl_dpi_get();
129 dpiHorizontal = int( xres + 0.5f ); // rounding
130 dpiVertical = int( yres + 0.5f );
133 void NativeRenderSurfaceEcoreWl::InitializeEgl( EglInterface& egl )
135 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
136 unsetenv( "EGL_PLATFORM" );
138 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
140 eglImpl.ChooseConfig( true, mColorDepth );
143 void NativeRenderSurfaceEcoreWl::CreateEglSurface( EglInterface& egl )
145 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
147 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
149 eglImpl.CreateSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ), mColorDepth );
152 void NativeRenderSurfaceEcoreWl::DestroyEglSurface( EglInterface& egl )
154 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
156 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
157 eglImpl.DestroySurface();
160 bool NativeRenderSurfaceEcoreWl::ReplaceEGLSurface( EglInterface& egl )
162 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
169 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
171 return eglImpl.ReplaceSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ) );
174 void NativeRenderSurfaceEcoreWl::MoveResize( Dali::PositionSize positionSize )
178 void NativeRenderSurfaceEcoreWl::SetViewMode( ViewMode viewMode )
182 void NativeRenderSurfaceEcoreWl::StartRender()
186 bool NativeRenderSurfaceEcoreWl::PreRender( EglInterface&, Integration::GlAbstraction&, bool )
188 // nothing to do for pixmaps
192 void NativeRenderSurfaceEcoreWl::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface, bool resizingSurface )
194 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
195 eglImpl.SwapBuffers();
197 if( mThreadSynchronization )
199 mThreadSynchronization->PostRenderStarted();
202 if( tbm_surface_queue_can_acquire( mTbmQueue, 1 ) )
204 if( tbm_surface_queue_acquire( mTbmQueue, &mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
206 DALI_LOG_ERROR( "Failed to aquire a tbm_surface\n" );
211 tbm_surface_internal_ref( mConsumeSurface );
213 if( replacingSurface )
215 ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
216 mDrawableCompleted = true;
217 mTbmSurfaceCondition.Notify( lock );
220 // create damage for client applications which wish to know the update timing
221 if( !replacingSurface && mRenderNotification )
223 // use notification trigger
224 // Tell the event-thread to render the tbm_surface
225 mRenderNotification->Trigger();
228 if( mThreadSynchronization )
230 // wait until the event-thread completed to use the tbm_surface
231 mThreadSynchronization->PostRenderWaitForCompletion();
234 // release the consumed surface after post render was completed
238 void NativeRenderSurfaceEcoreWl::StopRender()
243 void NativeRenderSurfaceEcoreWl::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
245 mThreadSynchronization = &threadSynchronization;
248 RenderSurface::Type NativeRenderSurfaceEcoreWl::GetSurfaceType()
250 return RenderSurface::NATIVE_RENDER_SURFACE;
253 void NativeRenderSurfaceEcoreWl::ReleaseLock()
255 if( mThreadSynchronization )
257 mThreadSynchronization->PostRenderComplete();
261 void NativeRenderSurfaceEcoreWl::CreateNativeRenderable()
263 // check we're creating one with a valid size
264 DALI_ASSERT_ALWAYS( mPosition.width > 0 && mPosition.height > 0 && "tbm_surface size is invalid" );
266 mTbmQueue = tbm_surface_queue_create( 3, mPosition.width, mPosition.height, mTbmFormat, TBM_BO_DEFAULT );
278 void NativeRenderSurfaceEcoreWl::ReleaseDrawable()
280 if( mConsumeSurface )
282 tbm_surface_internal_unref( mConsumeSurface );
284 if( tbm_surface_internal_is_valid( mConsumeSurface ) )
286 tbm_surface_queue_release( mTbmQueue, mConsumeSurface );
288 mConsumeSurface = NULL;