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/gles/egl-implementation.h>
37 #include <dali/internal/graphics/gles/egl-graphics.h>
38 #include <dali/internal/window-system/common/display-connection.h>
39 #include <dali/internal/window-system/common/window-system.h>
40 #include <dali/integration-api/thread-synchronization-interface.h>
41 #include <dali/internal/adaptor/common/adaptor-impl.h>
42 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
50 #if defined(DEBUG_ENABLED)
51 Debug::Filter* gNativeSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_NATIVE_RENDER_SURFACE");
54 inline void PrintTBMSurfaceQueueError( int errorCode )
58 case TBM_SURFACE_QUEUE_ERROR_EMPTY:
59 DALI_LOG_ERROR( "TBM_SURFACE_QUEUE_ERROR_EMPTY" );
61 case TBM_SURFACE_QUEUE_ERROR_INVALID_QUEUE:
62 DALI_LOG_ERROR( "TBM_SURFACE_QUEUE_ERROR_INVALID_QUEUE" );
64 case TBM_SURFACE_QUEUE_ERROR_INVALID_SURFACE:
65 DALI_LOG_ERROR( "TBM_SURFACE_QUEUE_ERROR_INVALID_SURFACE" );
67 case TBM_SURFACE_QUEUE_ERROR_SURFACE_ALLOC_FAILED:
68 DALI_LOG_ERROR( "TBM_SURFACE_QUEUE_ERROR_SURFACE_ALLOC_FAILED" );
70 case TBM_SURFACE_QUEUE_ERROR_ALREADY_EXIST:
71 DALI_LOG_ERROR( "TBM_SURFACE_QUEUE_ERROR_ALREADY_EXIST" );
73 case TBM_SURFACE_QUEUE_ERROR_UNKNOWN_SURFACE:
74 DALI_LOG_ERROR( "TBM_SURFACE_QUEUE_ERROR_UNKNOWN_SURFACE" );
76 case TBM_SURFACE_QUEUE_ERROR_INVALID_SEQUENCE:
77 DALI_LOG_ERROR( "TBM_SURFACE_QUEUE_ERROR_INVALID_SEQUENCE" );
79 case TBM_SURFACE_QUEUE_ERROR_TIMEOUT:
80 DALI_LOG_ERROR( "TBM_SURFACE_QUEUE_ERROR_TIMEOUT" );
83 DALI_LOG_ERROR( "TBM_SURFACE_QUEUE_ERROR_UNKNOWN" );
88 } // unnamed namespace
90 NativeRenderSurfaceEcoreWl::NativeRenderSurfaceEcoreWl( Dali::PositionSize positionSize, bool isTransparent )
91 : mPosition( positionSize ),
92 mRenderNotification( NULL ),
95 mEGLSurface( nullptr ),
96 mEGLContext( nullptr ),
97 mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
98 mTbmFormat( isTransparent ? TBM_FORMAT_ARGB8888 : TBM_FORMAT_RGB888 ),
100 mDrawableCompleted( false ),
102 mConsumeSurface( NULL ),
103 mThreadSynchronization( NULL )
105 Dali::Internal::Adaptor::WindowSystem::Initialize();
107 CreateNativeRenderable();
108 setenv( "EGL_PLATFORM", "tbm", 1 );
111 NativeRenderSurfaceEcoreWl::~NativeRenderSurfaceEcoreWl()
113 // release the surface if we own one
120 tbm_surface_queue_destroy( mTbmQueue );
123 DALI_LOG_INFO( gNativeSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n" );
126 Dali::Internal::Adaptor::WindowSystem::Shutdown();
129 Any NativeRenderSurfaceEcoreWl::GetDrawable()
131 return mConsumeSurface;
134 void NativeRenderSurfaceEcoreWl::SetRenderNotification( TriggerEventInterface* renderNotification )
136 mRenderNotification = renderNotification;
139 void NativeRenderSurfaceEcoreWl::WaitUntilSurfaceReplaced()
141 ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
142 while( !mDrawableCompleted )
144 mTbmSurfaceCondition.Wait( lock );
147 mDrawableCompleted = false;
150 PositionSize NativeRenderSurfaceEcoreWl::GetPositionSize() const
155 void NativeRenderSurfaceEcoreWl::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
160 // 1 inch = 25.4 millimeters
161 #ifdef ECORE_WAYLAND2
162 // TODO: Application should set dpi value in wayland2
166 xres = ecore_wl_dpi_get();
167 yres = ecore_wl_dpi_get();
170 dpiHorizontal = int( xres + 0.5f ); // rounding
171 dpiVertical = int( yres + 0.5f );
174 void NativeRenderSurfaceEcoreWl::InitializeGraphics()
176 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
177 unsetenv( "EGL_PLATFORM" );
179 mGraphics = &mAdaptor->GetGraphicsInterface();
180 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
182 mEGL = &eglGraphics->GetEglInterface();
184 if ( mEGLContext == NULL )
186 // Create the OpenGL context for this window
187 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
188 eglImpl.CreateWindowContext( mEGLContext );
190 // Create the OpenGL surface
195 void NativeRenderSurfaceEcoreWl::CreateSurface()
197 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
199 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
200 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
202 mEGLSurface = eglImpl.CreateSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ), mColorDepth );
205 void NativeRenderSurfaceEcoreWl::DestroySurface()
207 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
209 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
210 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
212 eglImpl.DestroySurface( mEGLSurface );
215 bool NativeRenderSurfaceEcoreWl::ReplaceGraphicsSurface()
217 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
224 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
225 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
227 return eglImpl.ReplaceSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ), mEGLSurface, mEGLContext );
230 void NativeRenderSurfaceEcoreWl::MoveResize( Dali::PositionSize positionSize )
232 tbm_surface_queue_error_e error = TBM_SURFACE_QUEUE_ERROR_NONE;
234 error = tbm_surface_queue_reset( mTbmQueue, positionSize.width, positionSize.height, mTbmFormat );
236 if( error != TBM_SURFACE_QUEUE_ERROR_NONE )
238 DALI_LOG_ERROR( "Failed to resize tbm_surface_queue" );
240 PrintTBMSurfaceQueueError( error );
243 mPosition = positionSize;
246 void NativeRenderSurfaceEcoreWl::StartRender()
250 bool NativeRenderSurfaceEcoreWl::PreRender( bool )
252 // nothing to do for pixmaps
256 void NativeRenderSurfaceEcoreWl::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
258 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
261 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
263 eglImpl.SwapBuffers( mEGLSurface );
266 if( mThreadSynchronization )
268 mThreadSynchronization->PostRenderStarted();
271 if( tbm_surface_queue_can_acquire( mTbmQueue, 1 ) )
273 if( tbm_surface_queue_acquire( mTbmQueue, &mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
275 DALI_LOG_ERROR( "Failed to acquire a tbm_surface\n" );
280 tbm_surface_internal_ref( mConsumeSurface );
282 if( replacingSurface )
284 ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
285 mDrawableCompleted = true;
286 mTbmSurfaceCondition.Notify( lock );
289 // create damage for client applications which wish to know the update timing
290 if( !replacingSurface && mRenderNotification )
292 // use notification trigger
293 // Tell the event-thread to render the tbm_surface
294 mRenderNotification->Trigger();
297 if( mThreadSynchronization )
299 // wait until the event-thread completed to use the tbm_surface
300 mThreadSynchronization->PostRenderWaitForCompletion();
303 // release the consumed surface after post render was completed
307 void NativeRenderSurfaceEcoreWl::StopRender()
312 void NativeRenderSurfaceEcoreWl::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
314 mThreadSynchronization = &threadSynchronization;
317 Integration::RenderSurface::Type NativeRenderSurfaceEcoreWl::GetSurfaceType()
319 return Integration::RenderSurface::NATIVE_RENDER_SURFACE;
322 void NativeRenderSurfaceEcoreWl::MakeContextCurrent()
324 if ( mEGL != nullptr )
326 mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
330 Integration::DepthBufferAvailable NativeRenderSurfaceEcoreWl::GetDepthBufferRequired()
332 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
335 Integration::StencilBufferAvailable NativeRenderSurfaceEcoreWl::GetStencilBufferRequired()
337 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
340 void NativeRenderSurfaceEcoreWl::ReleaseLock()
342 if( mThreadSynchronization )
344 mThreadSynchronization->PostRenderComplete();
348 void NativeRenderSurfaceEcoreWl::CreateNativeRenderable()
350 // check we're creating one with a valid size
351 DALI_ASSERT_ALWAYS( mPosition.width > 0 && mPosition.height > 0 && "tbm_surface size is invalid" );
353 mTbmQueue = tbm_surface_queue_create( 3, mPosition.width, mPosition.height, mTbmFormat, TBM_BO_DEFAULT );
365 void NativeRenderSurfaceEcoreWl::ReleaseDrawable()
367 if( mConsumeSurface )
369 tbm_surface_internal_unref( mConsumeSurface );
371 if( tbm_surface_internal_is_valid( mConsumeSurface ) )
373 tbm_surface_queue_release( mTbmQueue, mConsumeSurface );
375 mConsumeSurface = NULL;