2 * Copyright (c) 2019 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()
84 // release the surface if we own one
91 tbm_surface_queue_destroy( mTbmQueue );
94 DALI_LOG_INFO( gNativeSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n" );
97 Dali::Internal::Adaptor::WindowSystem::Shutdown();
100 Any NativeRenderSurfaceEcoreWl::GetDrawable()
102 return mConsumeSurface;
105 void NativeRenderSurfaceEcoreWl::SetRenderNotification( TriggerEventInterface* renderNotification )
107 mRenderNotification = renderNotification;
110 void NativeRenderSurfaceEcoreWl::WaitUntilSurfaceReplaced()
112 ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
113 while( !mDrawableCompleted )
115 mTbmSurfaceCondition.Wait( lock );
118 mDrawableCompleted = false;
121 PositionSize NativeRenderSurfaceEcoreWl::GetPositionSize() const
126 void NativeRenderSurfaceEcoreWl::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
131 // 1 inch = 25.4 millimeters
132 #ifdef ECORE_WAYLAND2
133 // TODO: Application should set dpi value in wayland2
137 xres = ecore_wl_dpi_get();
138 yres = ecore_wl_dpi_get();
141 dpiHorizontal = int( xres + 0.5f ); // rounding
142 dpiVertical = int( yres + 0.5f );
145 int NativeRenderSurfaceEcoreWl::GetOrientation() const
150 void NativeRenderSurfaceEcoreWl::InitializeGraphics()
152 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
153 unsetenv( "EGL_PLATFORM" );
155 mGraphics = &mAdaptor->GetGraphicsInterface();
156 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
158 mEGL = &eglGraphics->GetEglInterface();
160 if ( mEGLContext == NULL )
162 // Create the OpenGL context for this window
163 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
164 eglImpl.CreateWindowContext( mEGLContext );
166 // Create the OpenGL surface
171 void NativeRenderSurfaceEcoreWl::CreateSurface()
173 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
175 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
176 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
178 mEGLSurface = eglImpl.CreateSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ), mColorDepth );
181 void NativeRenderSurfaceEcoreWl::DestroySurface()
183 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
185 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
186 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
188 eglImpl.DestroySurface( mEGLSurface );
191 bool NativeRenderSurfaceEcoreWl::ReplaceGraphicsSurface()
193 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
200 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
201 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
203 return eglImpl.ReplaceSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ), mEGLSurface, mEGLContext );
206 void NativeRenderSurfaceEcoreWl::MoveResize( Dali::PositionSize positionSize )
208 tbm_surface_queue_error_e error = TBM_SURFACE_QUEUE_ERROR_NONE;
210 error = tbm_surface_queue_reset( mTbmQueue, positionSize.width, positionSize.height, mTbmFormat );
212 if( error != TBM_SURFACE_QUEUE_ERROR_NONE )
214 DALI_LOG_ERROR( "Failed to resize tbm_surface_queue" );
217 mPosition = positionSize;
220 void NativeRenderSurfaceEcoreWl::StartRender()
224 bool NativeRenderSurfaceEcoreWl::PreRender( bool )
226 // nothing to do for pixmaps
230 void NativeRenderSurfaceEcoreWl::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
232 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
235 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
237 eglImpl.SwapBuffers( mEGLSurface );
240 if( mThreadSynchronization )
242 mThreadSynchronization->PostRenderStarted();
245 if( tbm_surface_queue_can_acquire( mTbmQueue, 1 ) )
247 if( tbm_surface_queue_acquire( mTbmQueue, &mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
249 DALI_LOG_ERROR( "Failed to acquire a tbm_surface\n" );
254 tbm_surface_internal_ref( mConsumeSurface );
256 if( replacingSurface )
258 ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
259 mDrawableCompleted = true;
260 mTbmSurfaceCondition.Notify( lock );
263 // create damage for client applications which wish to know the update timing
264 if( !replacingSurface && mRenderNotification )
266 // use notification trigger
267 // Tell the event-thread to render the tbm_surface
268 mRenderNotification->Trigger();
271 if( mThreadSynchronization )
273 // wait until the event-thread completed to use the tbm_surface
274 mThreadSynchronization->PostRenderWaitForCompletion();
277 // release the consumed surface after post render was completed
281 void NativeRenderSurfaceEcoreWl::StopRender()
286 void NativeRenderSurfaceEcoreWl::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
288 mThreadSynchronization = &threadSynchronization;
291 Integration::RenderSurface::Type NativeRenderSurfaceEcoreWl::GetSurfaceType()
293 return Integration::RenderSurface::NATIVE_RENDER_SURFACE;
296 void NativeRenderSurfaceEcoreWl::MakeContextCurrent()
298 if ( mEGL != nullptr )
300 mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
304 Integration::DepthBufferAvailable NativeRenderSurfaceEcoreWl::GetDepthBufferRequired()
306 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
309 Integration::StencilBufferAvailable NativeRenderSurfaceEcoreWl::GetStencilBufferRequired()
311 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
314 void NativeRenderSurfaceEcoreWl::ReleaseLock()
316 if( mThreadSynchronization )
318 mThreadSynchronization->PostRenderComplete();
322 void NativeRenderSurfaceEcoreWl::CreateNativeRenderable()
324 // check we're creating one with a valid size
325 DALI_ASSERT_ALWAYS( mPosition.width > 0 && mPosition.height > 0 && "tbm_surface size is invalid" );
327 mTbmQueue = tbm_surface_queue_create( 3, mPosition.width, mPosition.height, mTbmFormat, TBM_BO_DEFAULT );
339 void NativeRenderSurfaceEcoreWl::ReleaseDrawable()
341 if( mConsumeSurface )
343 tbm_surface_internal_unref( mConsumeSurface );
345 if( tbm_surface_internal_is_valid( mConsumeSurface ) )
347 tbm_surface_queue_release( mTbmQueue, mConsumeSurface );
349 mConsumeSurface = NULL;