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>
31 #include <trigger-event.h>
32 #include <gl/egl-implementation.h>
33 #include <base/display-connection.h>
34 #include <integration-api/thread-synchronization-interface.h>
39 #if defined(DEBUG_ENABLED)
40 extern Debug::Filter* gRenderSurfaceLogFilter;
43 struct NativeRenderSurface::Impl
45 Impl( Dali::PositionSize positionSize, const std::string& name, bool isTransparent )
46 : mPosition( positionSize ),
48 mRenderNotification( NULL ),
49 mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
50 mTbmFormat( isTransparent ? TBM_FORMAT_ARGB8888 : TBM_FORMAT_RGB888 ),
52 mConsumeSurface( NULL ),
53 mThreadSynchronization( NULL )
57 PositionSize mPosition;
59 TriggerEventInterface* mRenderNotification;
60 ColorDepth mColorDepth;
61 tbm_format mTbmFormat;
64 tbm_surface_queue_h mTbmQueue;
65 tbm_surface_h mConsumeSurface;
66 ThreadSynchronizationInterface* mThreadSynchronization; ///< A pointer to the thread-synchronization
67 ConditionalWait mTbmSurfaceCondition;
70 NativeRenderSurface::NativeRenderSurface(Dali::PositionSize positionSize,
71 const std::string& name,
73 : mImpl( new Impl( positionSize, name, isTransparent ) )
76 CreateNativeRenderable();
79 NativeRenderSurface::~NativeRenderSurface()
81 // release the surface if we own one
82 if( mImpl->mOwnSurface )
85 if( mImpl->mConsumeSurface )
87 tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
88 mImpl->mConsumeSurface = NULL;
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 ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
111 return mImpl->mConsumeSurface;
114 Any NativeRenderSurface::GetSurface()
119 void NativeRenderSurface::InitializeEgl( EglInterface& egl )
121 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
123 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
125 eglImpl.ChooseConfig( true, mImpl->mColorDepth );
128 void NativeRenderSurface::CreateEglSurface( EglInterface& egl )
130 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
132 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
134 eglImpl.CreateSurfaceWindow( (EGLNativeWindowType)mImpl->mTbmQueue, mImpl->mColorDepth );
137 void NativeRenderSurface::DestroyEglSurface( EglInterface& egl )
139 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
141 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
142 eglImpl.DestroySurface();
145 bool NativeRenderSurface::ReplaceEGLSurface( EglInterface& egl )
147 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
149 if( mImpl->mConsumeSurface )
151 tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
152 mImpl->mConsumeSurface = NULL;
155 if( mImpl->mTbmQueue )
157 tbm_surface_queue_destroy( mImpl->mTbmQueue );
160 CreateNativeRenderable();
162 if( !mImpl->mTbmQueue )
167 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
169 return eglImpl.ReplaceSurfaceWindow( (EGLNativeWindowType)mImpl->mTbmQueue ); // reinterpret_cast does not compile
172 void NativeRenderSurface::StartRender()
176 bool NativeRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
178 // nothing to do for pixmaps
182 void NativeRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface )
184 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
185 eglImpl.SwapBuffers();
187 // flush gl instruction queue
188 glAbstraction.Flush();
191 if( tbm_surface_queue_can_acquire( mImpl->mTbmQueue, 1 ) )
193 if( tbm_surface_queue_acquire( mImpl->mTbmQueue, &mImpl->mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
195 DALI_LOG_ERROR( "Failed aquire consume tbm_surface\n" );
201 // create damage for client applications which wish to know the update timing
202 if( mImpl->mRenderNotification )
204 // use notification trigger
205 // Tell the event-thread to render the pixmap
206 mImpl->mRenderNotification->Trigger();
215 void NativeRenderSurface::StopRender()
220 PositionSize NativeRenderSurface::GetPositionSize() const
222 return mImpl->mPosition;
225 void NativeRenderSurface::MoveResize( Dali::PositionSize positionSize )
229 void NativeRenderSurface::SetViewMode( ViewMode viewMode )
233 void NativeRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
235 mImpl->mThreadSynchronization = &threadSynchronization;
238 RenderSurface::Type NativeRenderSurface::GetSurfaceType()
240 return RenderSurface::NATIVE_RENDER_SURFACE;
243 void NativeRenderSurface::CreateNativeRenderable()
245 // check we're creating one with a valid size
246 DALI_ASSERT_ALWAYS( mImpl->mPosition.width > 0 && mImpl->mPosition.height > 0 && "Pixmap size is invalid" );
248 mImpl->mTbmQueue = tbm_surface_queue_create( 3, mImpl->mPosition.width, mImpl->mPosition.height, mImpl->mTbmFormat, TBM_BO_DEFAULT );
250 if( mImpl->mTbmQueue )
252 mImpl->mOwnSurface = true;
256 mImpl->mOwnSurface = false;
260 void NativeRenderSurface::ReleaseSurface()
262 if( mImpl->mConsumeSurface )
264 tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
265 mImpl->mConsumeSurface = NULL;
269 void NativeRenderSurface::ReleaseLock()
271 if( mImpl->mThreadSynchronization )
273 mImpl->mThreadSynchronization->PostRenderComplete();