2 * Copyright (c) 2017 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 <native-render-surface.h>
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/devel-api/threading/conditional-wait.h>
26 #include <Ecore_Wayland.h>
27 #include <tbm_bufmgr.h>
28 #include <tbm_surface_queue.h>
29 #include <tbm_surface_internal.h>
32 #include <trigger-event.h>
33 #include <gl/egl-implementation.h>
34 #include <base/display-connection.h>
35 #include <integration-api/thread-synchronization-interface.h>
40 #if defined(DEBUG_ENABLED)
41 extern Debug::Filter* gRenderSurfaceLogFilter;
44 struct NativeRenderSurface::Impl
46 Impl( Dali::PositionSize positionSize, const std::string& name, bool isTransparent )
47 : mPosition( positionSize ),
49 mRenderNotification( NULL ),
50 mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
51 mTbmFormat( isTransparent ? TBM_FORMAT_ARGB8888 : TBM_FORMAT_RGB888 ),
53 mDrawableCompleted( false ),
55 mConsumeSurface( NULL ),
56 mThreadSynchronization( NULL )
60 PositionSize mPosition;
62 TriggerEventInterface* mRenderNotification;
63 ColorDepth mColorDepth;
64 tbm_format mTbmFormat;
66 bool mDrawableCompleted;
68 tbm_surface_queue_h mTbmQueue;
69 tbm_surface_h mConsumeSurface;
70 ThreadSynchronizationInterface* mThreadSynchronization; ///< A pointer to the thread-synchronization
71 ConditionalWait mTbmSurfaceCondition;
74 NativeRenderSurface::NativeRenderSurface(Dali::PositionSize positionSize,
75 const std::string& name,
77 : mImpl( new Impl( positionSize, name, isTransparent ) )
80 CreateNativeRenderable();
81 setenv( "EGL_PLATFORM", "tbm", 1 );
84 NativeRenderSurface::~NativeRenderSurface()
86 // release the surface if we own one
87 if( mImpl->mOwnSurface )
91 if( mImpl->mTbmQueue )
93 tbm_surface_queue_destroy( mImpl->mTbmQueue );
98 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n" );
104 void NativeRenderSurface::SetRenderNotification( TriggerEventInterface* renderNotification )
106 mImpl->mRenderNotification = renderNotification;
109 tbm_surface_h NativeRenderSurface::GetDrawable()
111 return mImpl->mConsumeSurface;
114 Any NativeRenderSurface::GetSurface()
119 void NativeRenderSurface::InitializeEgl( EglInterface& egl )
121 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
122 unsetenv( "EGL_PLATFORM" );
124 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
126 eglImpl.ChooseConfig( true, mImpl->mColorDepth );
129 void NativeRenderSurface::CreateEglSurface( EglInterface& egl )
131 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
133 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
135 eglImpl.CreateSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mImpl->mTbmQueue ), mImpl->mColorDepth );
138 void NativeRenderSurface::DestroyEglSurface( EglInterface& egl )
140 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
142 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
143 eglImpl.DestroySurface();
146 bool NativeRenderSurface::ReplaceEGLSurface( EglInterface& egl )
148 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
150 if( !mImpl->mTbmQueue )
155 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
157 return eglImpl.ReplaceSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mImpl->mTbmQueue ) );
160 void NativeRenderSurface::StartRender()
164 bool NativeRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction&, bool )
166 // nothing to do for pixmaps
170 void NativeRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface, bool resizingSurface )
172 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
173 eglImpl.SwapBuffers();
175 if( mImpl->mThreadSynchronization )
177 mImpl->mThreadSynchronization->PostRenderStarted();
180 if( tbm_surface_queue_can_acquire( mImpl->mTbmQueue, 1 ) )
182 if( tbm_surface_queue_acquire( mImpl->mTbmQueue, &mImpl->mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
184 DALI_LOG_ERROR( "Failed to aquire a tbm_surface\n" );
189 tbm_surface_internal_ref( mImpl->mConsumeSurface );
191 if( replacingSurface )
193 ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
194 mImpl->mDrawableCompleted = true;
195 mImpl->mTbmSurfaceCondition.Notify( lock );
198 // create damage for client applications which wish to know the update timing
199 if( !replacingSurface && mImpl->mRenderNotification )
201 // use notification trigger
202 // Tell the event-thread to render the tbm_surface
203 mImpl->mRenderNotification->Trigger();
206 if( mImpl->mThreadSynchronization )
208 // wait until the event-thread completed to use the tbm_surface
209 mImpl->mThreadSynchronization->PostRenderWaitForCompletion();
212 // release the consumed surface after post render was completed
216 void NativeRenderSurface::StopRender()
221 PositionSize NativeRenderSurface::GetPositionSize() const
223 return mImpl->mPosition;
226 void NativeRenderSurface::MoveResize( Dali::PositionSize positionSize )
230 void NativeRenderSurface::SetViewMode( ViewMode viewMode )
234 void NativeRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
236 mImpl->mThreadSynchronization = &threadSynchronization;
239 RenderSurface::Type NativeRenderSurface::GetSurfaceType()
241 return RenderSurface::NATIVE_RENDER_SURFACE;
244 void NativeRenderSurface::CreateNativeRenderable()
246 // check we're creating one with a valid size
247 DALI_ASSERT_ALWAYS( mImpl->mPosition.width > 0 && mImpl->mPosition.height > 0 && "tbm_surface size is invalid" );
249 mImpl->mTbmQueue = tbm_surface_queue_create( 3, mImpl->mPosition.width, mImpl->mPosition.height, mImpl->mTbmFormat, TBM_BO_DEFAULT );
251 if( mImpl->mTbmQueue )
253 mImpl->mOwnSurface = true;
257 mImpl->mOwnSurface = false;
261 void NativeRenderSurface::ReleaseLock()
263 if( mImpl->mThreadSynchronization )
265 mImpl->mThreadSynchronization->PostRenderComplete();
269 void NativeRenderSurface::WaitUntilSurfaceReplaced()
271 ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
272 while( !mImpl->mDrawableCompleted )
274 mImpl->mTbmSurfaceCondition.Wait( lock );
277 mImpl->mDrawableCompleted = false;
280 void NativeRenderSurface::ReleaseDrawable()
282 if( mImpl->mConsumeSurface )
284 tbm_surface_internal_unref( mImpl->mConsumeSurface );
286 if( tbm_surface_internal_is_valid( mImpl->mConsumeSurface ) )
288 tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
290 mImpl->mConsumeSurface = NULL;