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" );
102 void NativeRenderSurface::SetRenderNotification( TriggerEventInterface* renderNotification )
104 mImpl->mRenderNotification = renderNotification;
107 tbm_surface_h NativeRenderSurface::GetDrawable()
109 return mImpl->mConsumeSurface;
112 Any NativeRenderSurface::GetSurface()
117 void NativeRenderSurface::InitializeEgl( EglInterface& egl )
119 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
120 unsetenv( "EGL_PLATFORM" );
122 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
124 eglImpl.ChooseConfig( true, mImpl->mColorDepth );
127 void NativeRenderSurface::CreateEglSurface( EglInterface& egl )
129 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
131 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
133 eglImpl.CreateSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mImpl->mTbmQueue ), mImpl->mColorDepth );
136 void NativeRenderSurface::DestroyEglSurface( EglInterface& egl )
138 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
140 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
141 eglImpl.DestroySurface();
144 bool NativeRenderSurface::ReplaceEGLSurface( EglInterface& egl )
146 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
148 if( !mImpl->mTbmQueue )
153 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
155 return eglImpl.ReplaceSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mImpl->mTbmQueue ) );
158 void NativeRenderSurface::StartRender()
162 bool NativeRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
164 // nothing to do for pixmaps
168 void NativeRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface )
170 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
171 eglImpl.SwapBuffers();
173 if( mImpl->mThreadSynchronization )
175 mImpl->mThreadSynchronization->PostRenderStarted();
178 if( tbm_surface_queue_can_acquire( mImpl->mTbmQueue, 1 ) )
180 if( tbm_surface_queue_acquire( mImpl->mTbmQueue, &mImpl->mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
182 DALI_LOG_ERROR( "Failed to aquire a tbm_surface\n" );
187 tbm_surface_internal_ref( mImpl->mConsumeSurface );
189 if( replacingSurface )
191 ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
192 mImpl->mDrawableCompleted = true;
193 mImpl->mTbmSurfaceCondition.Notify( lock );
196 // create damage for client applications which wish to know the update timing
197 if( !replacingSurface && mImpl->mRenderNotification )
199 // use notification trigger
200 // Tell the event-thread to render the tbm_surface
201 mImpl->mRenderNotification->Trigger();
204 if( mImpl->mThreadSynchronization )
206 // wait until the event-thread completed to use the tbm_surface
207 mImpl->mThreadSynchronization->PostRenderWaitForCompletion();
210 // release the consumed surface after post render was completed
214 void NativeRenderSurface::StopRender()
219 PositionSize NativeRenderSurface::GetPositionSize() const
221 return mImpl->mPosition;
224 void NativeRenderSurface::MoveResize( Dali::PositionSize positionSize )
228 void NativeRenderSurface::SetViewMode( ViewMode viewMode )
232 void NativeRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
234 mImpl->mThreadSynchronization = &threadSynchronization;
237 RenderSurface::Type NativeRenderSurface::GetSurfaceType()
239 return RenderSurface::NATIVE_RENDER_SURFACE;
242 void NativeRenderSurface::CreateNativeRenderable()
244 // check we're creating one with a valid size
245 DALI_ASSERT_ALWAYS( mImpl->mPosition.width > 0 && mImpl->mPosition.height > 0 && "tbm_surface size is invalid" );
247 mImpl->mTbmQueue = tbm_surface_queue_create( 3, mImpl->mPosition.width, mImpl->mPosition.height, mImpl->mTbmFormat, TBM_BO_DEFAULT );
249 if( mImpl->mTbmQueue )
251 mImpl->mOwnSurface = true;
255 mImpl->mOwnSurface = false;
259 void NativeRenderSurface::ReleaseLock()
261 if( mImpl->mThreadSynchronization )
263 mImpl->mThreadSynchronization->PostRenderComplete();
267 void NativeRenderSurface::WaitUntilSurfaceReplaced()
269 ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
270 while( !mImpl->mDrawableCompleted )
272 mImpl->mTbmSurfaceCondition.Wait( lock );
275 mImpl->mDrawableCompleted = false;
278 void NativeRenderSurface::ReleaseDrawable()
280 if( mImpl->mConsumeSurface )
282 tbm_surface_internal_unref( mImpl->mConsumeSurface );
284 if( tbm_surface_internal_is_valid( mImpl->mConsumeSurface ) )
286 tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
288 mImpl->mConsumeSurface = NULL;