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 : mSurfaceSize( surfaceSize ),
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();
75 CreateNativeRenderable();
79 // check we have a valid type
80 DALI_ASSERT_ALWAYS( ( surface.GetType() == typeid (tbm_surface_queue_h) ) && "Surface type is invalid" );
81 mTbmQueue = AnyCast< tbm_surface_queue_h >( surface );
84 setenv( "EGL_PLATFORM", "tbm", 1 );
87 NativeRenderSurfaceEcoreWl::~NativeRenderSurfaceEcoreWl()
94 // release the surface if we own one
101 tbm_surface_queue_destroy( mTbmQueue );
104 DALI_LOG_INFO( gNativeSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n" );
107 Dali::Internal::Adaptor::WindowSystem::Shutdown();
110 Any NativeRenderSurfaceEcoreWl::GetDrawable()
112 return mConsumeSurface;
115 void NativeRenderSurfaceEcoreWl::SetRenderNotification( TriggerEventInterface* renderNotification )
117 mRenderNotification = renderNotification;
120 void NativeRenderSurfaceEcoreWl::WaitUntilSurfaceReplaced()
122 ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
123 while( !mDrawableCompleted )
125 mTbmSurfaceCondition.Wait( lock );
128 mDrawableCompleted = false;
131 PositionSize NativeRenderSurfaceEcoreWl::GetPositionSize() const
133 return PositionSize( 0, 0, static_cast<int>( mSurfaceSize.GetWidth() ), static_cast<int>( mSurfaceSize.GetHeight() ) );
136 void NativeRenderSurfaceEcoreWl::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
141 // 1 inch = 25.4 millimeters
142 #ifdef ECORE_WAYLAND2
143 // TODO: Application should set dpi value in wayland2
147 xres = ecore_wl_dpi_get();
148 yres = ecore_wl_dpi_get();
151 dpiHorizontal = int( xres + 0.5f ); // rounding
152 dpiVertical = int( yres + 0.5f );
155 int NativeRenderSurfaceEcoreWl::GetOrientation() const
160 void NativeRenderSurfaceEcoreWl::InitializeGraphics()
162 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
163 unsetenv( "EGL_PLATFORM" );
165 mGraphics = &mAdaptor->GetGraphicsInterface();
166 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
168 mEGL = &eglGraphics->GetEglInterface();
170 if ( mEGLContext == NULL )
172 // Create the OpenGL context for this window
173 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
174 eglImpl.CreateWindowContext( mEGLContext );
176 // Create the OpenGL surface
181 void NativeRenderSurfaceEcoreWl::CreateSurface()
183 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
185 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
186 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
188 mEGLSurface = eglImpl.CreateSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ), mColorDepth );
191 void NativeRenderSurfaceEcoreWl::DestroySurface()
193 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
195 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
196 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
198 eglImpl.DestroySurface( mEGLSurface );
201 bool NativeRenderSurfaceEcoreWl::ReplaceGraphicsSurface()
203 DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
210 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
211 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
213 return eglImpl.ReplaceSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ), mEGLSurface, mEGLContext );
216 void NativeRenderSurfaceEcoreWl::MoveResize( Dali::PositionSize positionSize )
218 tbm_surface_queue_error_e error = TBM_SURFACE_QUEUE_ERROR_NONE;
220 error = tbm_surface_queue_reset( mTbmQueue, positionSize.width, positionSize.height, mTbmFormat );
222 if( error != TBM_SURFACE_QUEUE_ERROR_NONE )
224 DALI_LOG_ERROR( "Failed to resize tbm_surface_queue" );
227 mSurfaceSize.SetWidth( static_cast<uint16_t>( positionSize.width ) );
228 mSurfaceSize.SetHeight( static_cast<uint16_t>( positionSize.height ) );
231 void NativeRenderSurfaceEcoreWl::StartRender()
235 bool NativeRenderSurfaceEcoreWl::PreRender( bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect )
237 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
240 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
243 eglImpl.SetFullSwapNextFrame();
246 eglImpl.SetDamage(mEGLSurface, damagedRects, clippingRect);
252 void NativeRenderSurfaceEcoreWl::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface, const std::vector<Rect<int>>& damagedRects )
254 auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
257 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
258 eglImpl.SwapBuffers( mEGLSurface, damagedRects );
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 tbm_surface_internal_ref( mConsumeSurface );
277 if( replacingSurface )
279 ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
280 mDrawableCompleted = true;
281 mTbmSurfaceCondition.Notify( lock );
284 // create damage for client applications which wish to know the update timing
285 if( !replacingSurface && mRenderNotification )
287 // use notification trigger
288 // Tell the event-thread to render the tbm_surface
289 mRenderNotification->Trigger();
292 if( mThreadSynchronization )
294 // wait until the event-thread completed to use the tbm_surface
295 mThreadSynchronization->PostRenderWaitForCompletion();
298 // release the consumed surface after post render was completed
302 void NativeRenderSurfaceEcoreWl::StopRender()
307 void NativeRenderSurfaceEcoreWl::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
309 mThreadSynchronization = &threadSynchronization;
312 Dali::RenderSurfaceInterface::Type NativeRenderSurfaceEcoreWl::GetSurfaceType()
314 return Dali::RenderSurfaceInterface::NATIVE_RENDER_SURFACE;
317 void NativeRenderSurfaceEcoreWl::MakeContextCurrent()
319 if ( mEGL != nullptr )
321 mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
325 Integration::DepthBufferAvailable NativeRenderSurfaceEcoreWl::GetDepthBufferRequired()
327 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
330 Integration::StencilBufferAvailable NativeRenderSurfaceEcoreWl::GetStencilBufferRequired()
332 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
335 void NativeRenderSurfaceEcoreWl::ReleaseLock()
337 if( mThreadSynchronization )
339 mThreadSynchronization->PostRenderComplete();
343 void NativeRenderSurfaceEcoreWl::CreateNativeRenderable()
345 int width = static_cast<int>( mSurfaceSize.GetWidth() );
346 int height = static_cast<int>( mSurfaceSize.GetHeight() );
348 // check we're creating one with a valid size
349 DALI_ASSERT_ALWAYS( width > 0 && height > 0 && "tbm_surface size is invalid" );
351 mTbmQueue = tbm_surface_queue_create( 3, width, height, mTbmFormat, TBM_BO_DEFAULT );
363 void NativeRenderSurfaceEcoreWl::ReleaseDrawable()
365 if( mConsumeSurface )
367 tbm_surface_internal_unref( mConsumeSurface );
369 if( tbm_surface_internal_is_valid( mConsumeSurface ) )
371 tbm_surface_queue_release( mTbmQueue, mConsumeSurface );
373 mConsumeSurface = NULL;