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 } // unnamed namespace
56 NativeRenderSurfaceEcoreWl::NativeRenderSurfaceEcoreWl( Dali::PositionSize positionSize, bool isTransparent )
57 : mPosition( positionSize ),
58 mRenderNotification( NULL ),
61 mEGLSurface( nullptr ),
62 mEGLContext( nullptr ),
63 mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
64 mTbmFormat( isTransparent ? TBM_FORMAT_ARGB8888 : TBM_FORMAT_RGB888 ),
66 mDrawableCompleted( false ),
68 mConsumeSurface( NULL ),
69 mThreadSynchronization( NULL )
71 Dali::Internal::Adaptor::WindowSystem::Initialize();
73 CreateNativeRenderable();
74 setenv( "EGL_PLATFORM", "tbm", 1 );
77 NativeRenderSurfaceEcoreWl::~NativeRenderSurfaceEcoreWl()
79 // release the surface if we own one
86 tbm_surface_queue_destroy( mTbmQueue );
89 DALI_LOG_INFO( gNativeSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n" );
92 Dali::Internal::Adaptor::WindowSystem::Shutdown();
95 Any NativeRenderSurfaceEcoreWl::GetDrawable()
97 return mConsumeSurface;
100 void NativeRenderSurfaceEcoreWl::SetRenderNotification( TriggerEventInterface* renderNotification )
102 mRenderNotification = renderNotification;
105 void NativeRenderSurfaceEcoreWl::WaitUntilSurfaceReplaced()
107 ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
108 while( !mDrawableCompleted )
110 mTbmSurfaceCondition.Wait( lock );
113 mDrawableCompleted = false;
116 PositionSize NativeRenderSurfaceEcoreWl::GetPositionSize() const
121 void NativeRenderSurfaceEcoreWl::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
126 // 1 inch = 25.4 millimeters
127 #ifdef ECORE_WAYLAND2
128 // TODO: Application should set dpi value in wayland2
132 xres = ecore_wl_dpi_get();
133 yres = ecore_wl_dpi_get();
136 dpiHorizontal = int( xres + 0.5f ); // rounding
137 dpiVertical = int( yres + 0.5f );
140 void NativeRenderSurfaceEcoreWl::InitializeGraphics()
142 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
143 unsetenv( "EGL_PLATFORM" );
145 mGraphics = &mAdaptor->GetGraphicsInterface();
146 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
148 mEGL = &eglGraphics->GetEglInterface();
150 if ( mEGLContext == NULL )
152 // Create the OpenGL context for this window
153 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
154 eglImpl.CreateWindowContext( mEGLContext );
156 // Create the OpenGL surface
161 void NativeRenderSurfaceEcoreWl::CreateSurface()
163 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
165 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
166 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
168 mEGLSurface = eglImpl.CreateSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ), mColorDepth );
171 void NativeRenderSurfaceEcoreWl::DestroySurface()
173 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
175 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
176 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
178 eglImpl.DestroySurface( mEGLSurface );
181 bool NativeRenderSurfaceEcoreWl::ReplaceGraphicsSurface()
183 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
190 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
191 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
193 return eglImpl.ReplaceSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ), mEGLSurface, mEGLContext );
196 void NativeRenderSurfaceEcoreWl::MoveResize( Dali::PositionSize positionSize )
198 tbm_surface_queue_error_e error = TBM_SURFACE_QUEUE_ERROR_NONE;
200 error = tbm_surface_queue_reset( mTbmQueue, positionSize.width, positionSize.height, mTbmFormat );
202 if( error != TBM_SURFACE_QUEUE_ERROR_NONE )
204 DALI_LOG_ERROR( "Failed to resize tbm_surface_queue" );
207 mPosition = positionSize;
210 void NativeRenderSurfaceEcoreWl::StartRender()
214 bool NativeRenderSurfaceEcoreWl::PreRender( bool )
216 // nothing to do for pixmaps
220 void NativeRenderSurfaceEcoreWl::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
222 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
225 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
227 eglImpl.SwapBuffers( mEGLSurface );
230 if( mThreadSynchronization )
232 mThreadSynchronization->PostRenderStarted();
235 if( tbm_surface_queue_can_acquire( mTbmQueue, 1 ) )
237 if( tbm_surface_queue_acquire( mTbmQueue, &mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
239 DALI_LOG_ERROR( "Failed to acquire a tbm_surface\n" );
244 tbm_surface_internal_ref( mConsumeSurface );
246 if( replacingSurface )
248 ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
249 mDrawableCompleted = true;
250 mTbmSurfaceCondition.Notify( lock );
253 // create damage for client applications which wish to know the update timing
254 if( !replacingSurface && mRenderNotification )
256 // use notification trigger
257 // Tell the event-thread to render the tbm_surface
258 mRenderNotification->Trigger();
261 if( mThreadSynchronization )
263 // wait until the event-thread completed to use the tbm_surface
264 mThreadSynchronization->PostRenderWaitForCompletion();
267 // release the consumed surface after post render was completed
271 void NativeRenderSurfaceEcoreWl::StopRender()
276 void NativeRenderSurfaceEcoreWl::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
278 mThreadSynchronization = &threadSynchronization;
281 Integration::RenderSurface::Type NativeRenderSurfaceEcoreWl::GetSurfaceType()
283 return Integration::RenderSurface::NATIVE_RENDER_SURFACE;
286 void NativeRenderSurfaceEcoreWl::MakeContextCurrent()
288 if ( mEGL != nullptr )
290 mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
294 Integration::DepthBufferAvailable NativeRenderSurfaceEcoreWl::GetDepthBufferRequired()
296 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
299 Integration::StencilBufferAvailable NativeRenderSurfaceEcoreWl::GetStencilBufferRequired()
301 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
304 void NativeRenderSurfaceEcoreWl::ReleaseLock()
306 if( mThreadSynchronization )
308 mThreadSynchronization->PostRenderComplete();
312 void NativeRenderSurfaceEcoreWl::CreateNativeRenderable()
314 // check we're creating one with a valid size
315 DALI_ASSERT_ALWAYS( mPosition.width > 0 && mPosition.height > 0 && "tbm_surface size is invalid" );
317 mTbmQueue = tbm_surface_queue_create( 3, mPosition.width, mPosition.height, mTbmFormat, TBM_BO_DEFAULT );
329 void NativeRenderSurfaceEcoreWl::ReleaseDrawable()
331 if( mConsumeSurface )
333 tbm_surface_internal_unref( mConsumeSurface );
335 if( tbm_surface_internal_is_valid( mConsumeSurface ) )
337 tbm_surface_queue_release( mTbmQueue, mConsumeSurface );
339 mConsumeSurface = NULL;