2 * Copyright (c) 2016 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 ),
54 mConsumeSurface( NULL ),
55 mThreadSynchronization( NULL )
59 PositionSize mPosition;
61 TriggerEventInterface* mRenderNotification;
62 ColorDepth mColorDepth;
63 tbm_format mTbmFormat;
65 bool mDrawableCompleted;
67 tbm_surface_queue_h mTbmQueue;
68 tbm_surface_h mConsumeSurface;
69 ThreadSynchronizationInterface* mThreadSynchronization; ///< A pointer to the thread-synchronization
70 ConditionalWait mTbmSurfaceCondition;
73 NativeRenderSurface::NativeRenderSurface(Dali::PositionSize positionSize,
74 const std::string& name,
76 : mImpl( new Impl( positionSize, name, isTransparent ) )
79 CreateNativeRenderable();
80 setenv( "EGL_PLATFORM", "tbm", 1 );
83 NativeRenderSurface::~NativeRenderSurface()
85 // release the surface if we own one
86 if( mImpl->mOwnSurface )
90 if( mImpl->mTbmQueue )
92 tbm_surface_queue_destroy( mImpl->mTbmQueue );
97 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n" );
101 void NativeRenderSurface::SetRenderNotification( TriggerEventInterface* renderNotification )
103 mImpl->mRenderNotification = renderNotification;
106 tbm_surface_h NativeRenderSurface::GetDrawable()
108 return mImpl->mConsumeSurface;
111 Any NativeRenderSurface::GetSurface()
116 void NativeRenderSurface::InitializeEgl( EglInterface& egl )
118 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
119 unsetenv( "EGL_PLATFORM" );
121 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
123 eglImpl.ChooseConfig( true, mImpl->mColorDepth );
126 void NativeRenderSurface::CreateEglSurface( EglInterface& egl )
128 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
130 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
132 eglImpl.CreateSurfaceWindow( (EGLNativeWindowType)mImpl->mTbmQueue, mImpl->mColorDepth );
135 void NativeRenderSurface::DestroyEglSurface( EglInterface& egl )
137 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
139 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
140 eglImpl.DestroySurface();
143 bool NativeRenderSurface::ReplaceEGLSurface( EglInterface& egl )
145 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
147 if( !mImpl->mTbmQueue )
152 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
154 return eglImpl.ReplaceSurfaceWindow( (EGLNativeWindowType)mImpl->mTbmQueue ); // reinterpret_cast does not compile
157 void NativeRenderSurface::StartRender()
161 bool NativeRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
163 // nothing to do for pixmaps
167 void NativeRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface )
169 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
170 eglImpl.SwapBuffers();
172 if( mImpl->mThreadSynchronization )
174 mImpl->mThreadSynchronization->PostRenderStarted();
177 if( tbm_surface_queue_can_acquire( mImpl->mTbmQueue, 1 ) )
179 if( tbm_surface_queue_acquire( mImpl->mTbmQueue, &mImpl->mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
181 DALI_LOG_ERROR( "Failed to aquire a tbm_surface\n" );
186 tbm_surface_internal_ref( mImpl->mConsumeSurface );
188 if( replacingSurface )
190 ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
191 mImpl->mDrawableCompleted = true;
192 mImpl->mTbmSurfaceCondition.Notify( lock );
195 // create damage for client applications which wish to know the update timing
196 if( !replacingSurface && mImpl->mRenderNotification )
198 // use notification trigger
199 // Tell the event-thread to render the tbm_surface
200 mImpl->mRenderNotification->Trigger();
203 if( mImpl->mThreadSynchronization )
205 // wait until the event-thread completed to use the tbm_surface
206 mImpl->mThreadSynchronization->PostRenderWaitForCompletion();
209 // release the consumed surface after post render was completed
213 void NativeRenderSurface::StopRender()
218 PositionSize NativeRenderSurface::GetPositionSize() const
220 return mImpl->mPosition;
223 void NativeRenderSurface::MoveResize( Dali::PositionSize positionSize )
227 void NativeRenderSurface::SetViewMode( ViewMode viewMode )
231 void NativeRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
233 mImpl->mThreadSynchronization = &threadSynchronization;
236 RenderSurface::Type NativeRenderSurface::GetSurfaceType()
238 return RenderSurface::NATIVE_RENDER_SURFACE;
241 void NativeRenderSurface::CreateNativeRenderable()
243 // check we're creating one with a valid size
244 DALI_ASSERT_ALWAYS( mImpl->mPosition.width > 0 && mImpl->mPosition.height > 0 && "tbm_surface size is invalid" );
246 mImpl->mTbmQueue = tbm_surface_queue_create( 3, mImpl->mPosition.width, mImpl->mPosition.height, mImpl->mTbmFormat, TBM_BO_DEFAULT );
248 if( mImpl->mTbmQueue )
250 mImpl->mOwnSurface = true;
254 mImpl->mOwnSurface = false;
258 void NativeRenderSurface::ReleaseLock()
260 if( mImpl->mThreadSynchronization )
262 mImpl->mThreadSynchronization->PostRenderComplete();
266 void NativeRenderSurface::WaitUntilSurfaceReplaced()
268 ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
269 while( !mImpl->mDrawableCompleted )
271 mImpl->mTbmSurfaceCondition.Wait( lock );
274 mImpl->mDrawableCompleted = false;
277 void NativeRenderSurface::ReleaseDrawable()
279 if( mImpl->mConsumeSurface )
281 tbm_surface_internal_unref( mImpl->mConsumeSurface );
283 if( tbm_surface_internal_is_valid( mImpl->mConsumeSurface ) )
285 tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
287 mImpl->mConsumeSurface = NULL;