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();
77 setenv( "EGL_PLATFORM", "tbm", 1 );
80 NativeRenderSurface::~NativeRenderSurface()
82 // release the surface if we own one
83 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 );
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( (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->mConsumeSurface )
152 tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
153 mImpl->mConsumeSurface = NULL;
156 if( mImpl->mTbmQueue )
158 tbm_surface_queue_destroy( mImpl->mTbmQueue );
161 CreateNativeRenderable();
163 if( !mImpl->mTbmQueue )
168 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
170 return eglImpl.ReplaceSurfaceWindow( (EGLNativeWindowType)mImpl->mTbmQueue ); // reinterpret_cast does not compile
173 void NativeRenderSurface::StartRender()
177 bool NativeRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
179 // nothing to do for pixmaps
183 void NativeRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface )
185 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
186 eglImpl.SwapBuffers();
188 if( mImpl->mThreadSynchronization )
190 mImpl->mThreadSynchronization->PostRenderStarted();
193 if( tbm_surface_queue_can_acquire( mImpl->mTbmQueue, 1 ) )
195 if( tbm_surface_queue_acquire( mImpl->mTbmQueue, &mImpl->mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
197 DALI_LOG_ERROR( "Failed to aquire a tbm_surface\n" );
202 // create damage for client applications which wish to know the update timing
203 if( mImpl->mRenderNotification )
205 // use notification trigger
206 // Tell the event-thread to render the tbm_surface
207 mImpl->mRenderNotification->Trigger();
210 if( mImpl->mThreadSynchronization )
212 // wait until the event-thread completed to use the tbm_surface
213 mImpl->mThreadSynchronization->PostRenderWaitForCompletion();
216 // release the consumed surface after post render was completed
217 if( mImpl->mConsumeSurface )
219 tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
220 mImpl->mConsumeSurface = NULL;
224 void NativeRenderSurface::StopRender()
229 PositionSize NativeRenderSurface::GetPositionSize() const
231 return mImpl->mPosition;
234 void NativeRenderSurface::MoveResize( Dali::PositionSize positionSize )
238 void NativeRenderSurface::SetViewMode( ViewMode viewMode )
242 void NativeRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
244 mImpl->mThreadSynchronization = &threadSynchronization;
247 RenderSurface::Type NativeRenderSurface::GetSurfaceType()
249 return RenderSurface::NATIVE_RENDER_SURFACE;
252 void NativeRenderSurface::CreateNativeRenderable()
254 // check we're creating one with a valid size
255 DALI_ASSERT_ALWAYS( mImpl->mPosition.width > 0 && mImpl->mPosition.height > 0 && "Pixmap size is invalid" );
257 mImpl->mTbmQueue = tbm_surface_queue_create( 3, mImpl->mPosition.width, mImpl->mPosition.height, mImpl->mTbmFormat, TBM_BO_DEFAULT );
259 if( mImpl->mTbmQueue )
261 mImpl->mOwnSurface = true;
265 mImpl->mOwnSurface = false;
269 void NativeRenderSurface::ReleaseLock()
271 if( mImpl->mThreadSynchronization )
273 mImpl->mThreadSynchronization->PostRenderComplete();