1bb2f160cf8e23be0daf934fb05d81245ec9d571
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / native-render-surface-tizen.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <native-render-surface.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/devel-api/threading/conditional-wait.h>
25
26 #include <Ecore_Wayland.h>
27 #include <tbm_bufmgr.h>
28 #include <tbm_surface_queue.h>
29 #include <tbm_surface_internal.h>
30
31 // INTERNAL INCLUDES
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>
36
37 namespace Dali
38 {
39
40 #if defined(DEBUG_ENABLED)
41 extern Debug::Filter* gRenderSurfaceLogFilter;
42 #endif
43
44 struct NativeRenderSurface::Impl
45 {
46   Impl( Dali::PositionSize positionSize, const std::string& name, bool isTransparent )
47   : mPosition( positionSize ),
48     mTitle( name ),
49     mRenderNotification( NULL ),
50     mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
51     mTbmFormat( isTransparent ? TBM_FORMAT_ARGB8888 : TBM_FORMAT_RGB888 ),
52     mOwnSurface( false ),
53     mDrawableCompleted( false ),
54     mConsumeSurface( NULL ),
55     mThreadSynchronization( NULL )
56   {
57   }
58
59   PositionSize mPosition;
60   std::string mTitle;
61   TriggerEventInterface* mRenderNotification;
62   ColorDepth mColorDepth;
63   tbm_format mTbmFormat;
64   bool mOwnSurface;
65   bool mDrawableCompleted;
66
67   tbm_surface_queue_h mTbmQueue;
68   tbm_surface_h mConsumeSurface;
69   ThreadSynchronizationInterface* mThreadSynchronization;     ///< A pointer to the thread-synchronization
70   ConditionalWait mTbmSurfaceCondition;
71 };
72
73 NativeRenderSurface::NativeRenderSurface(Dali::PositionSize positionSize,
74                                          const std::string& name,
75                                          bool isTransparent)
76 : mImpl( new Impl( positionSize, name, isTransparent ) )
77 {
78   ecore_wl_init(NULL);
79   CreateNativeRenderable();
80   setenv( "EGL_PLATFORM", "tbm", 1 );
81 }
82
83 NativeRenderSurface::~NativeRenderSurface()
84 {
85   // release the surface if we own one
86   if( mImpl->mOwnSurface )
87   {
88     ReleaseDrawable();
89
90     if( mImpl->mTbmQueue )
91     {
92       tbm_surface_queue_destroy( mImpl->mTbmQueue );
93     }
94
95     delete mImpl;
96
97     DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n" );
98   }
99 }
100
101 void NativeRenderSurface::SetRenderNotification( TriggerEventInterface* renderNotification )
102 {
103   mImpl->mRenderNotification = renderNotification;
104 }
105
106 tbm_surface_h NativeRenderSurface::GetDrawable()
107 {
108   return mImpl->mConsumeSurface;
109 }
110
111 Any NativeRenderSurface::GetSurface()
112 {
113   return Any( NULL );
114 }
115
116 void NativeRenderSurface::InitializeEgl( EglInterface& egl )
117 {
118   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
119   unsetenv( "EGL_PLATFORM" );
120
121   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
122
123   eglImpl.ChooseConfig( true, mImpl->mColorDepth );
124 }
125
126 void NativeRenderSurface::CreateEglSurface( EglInterface& egl )
127 {
128   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
129
130   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
131
132   eglImpl.CreateSurfaceWindow( (EGLNativeWindowType)mImpl->mTbmQueue, mImpl->mColorDepth );
133 }
134
135 void NativeRenderSurface::DestroyEglSurface( EglInterface& egl )
136 {
137   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
138
139   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
140   eglImpl.DestroySurface();
141 }
142
143 bool NativeRenderSurface::ReplaceEGLSurface( EglInterface& egl )
144 {
145   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
146
147   if( !mImpl->mTbmQueue )
148   {
149     return false;
150   }
151
152   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
153
154   return eglImpl.ReplaceSurfaceWindow( (EGLNativeWindowType)mImpl->mTbmQueue ); // reinterpret_cast does not compile
155 }
156
157 void NativeRenderSurface::StartRender()
158 {
159 }
160
161 bool NativeRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
162 {
163   // nothing to do for pixmaps
164   return true;
165 }
166
167 void NativeRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface )
168 {
169   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
170   eglImpl.SwapBuffers();
171
172   if( tbm_surface_queue_can_acquire( mImpl->mTbmQueue, 1 ) )
173   {
174     if( tbm_surface_queue_acquire( mImpl->mTbmQueue, &mImpl->mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
175     {
176       DALI_LOG_ERROR( "Failed to aquire a tbm_surface\n" );
177       return;
178     }
179   }
180
181   tbm_surface_internal_ref( mImpl->mConsumeSurface );
182
183   if( replacingSurface )
184   {
185     ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
186     mImpl->mDrawableCompleted = true;
187     mImpl->mTbmSurfaceCondition.Notify( lock );
188   }
189
190  // create damage for client applications which wish to know the update timing
191   if( !replacingSurface && mImpl->mRenderNotification )
192   {
193     // use notification trigger
194     // Tell the event-thread to render the tbm_surface
195     mImpl->mRenderNotification->Trigger();
196   }
197
198   // release the consumed surface after post render was completed
199   ReleaseDrawable();
200 }
201
202 void NativeRenderSurface::StopRender()
203 {
204   ReleaseLock();
205 }
206
207 PositionSize NativeRenderSurface::GetPositionSize() const
208 {
209   return mImpl->mPosition;
210 }
211
212 void NativeRenderSurface::MoveResize( Dali::PositionSize positionSize )
213 {
214 }
215
216 void NativeRenderSurface::SetViewMode( ViewMode viewMode )
217 {
218 }
219
220 void NativeRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
221 {
222   mImpl->mThreadSynchronization = &threadSynchronization;
223 }
224
225 RenderSurface::Type NativeRenderSurface::GetSurfaceType()
226 {
227   return RenderSurface::NATIVE_RENDER_SURFACE;
228 }
229
230 void NativeRenderSurface::CreateNativeRenderable()
231 {
232   // check we're creating one with a valid size
233   DALI_ASSERT_ALWAYS( mImpl->mPosition.width > 0 && mImpl->mPosition.height > 0 && "tbm_surface size is invalid" );
234
235   mImpl->mTbmQueue = tbm_surface_queue_create( 3, mImpl->mPosition.width, mImpl->mPosition.height, mImpl->mTbmFormat, TBM_BO_DEFAULT );
236
237   if( mImpl->mTbmQueue )
238   {
239     mImpl->mOwnSurface = true;
240   }
241   else
242   {
243     mImpl->mOwnSurface = false;
244   }
245 }
246
247 void NativeRenderSurface::ReleaseLock()
248 {
249   ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
250   if( mImpl->mConsumeSurface )
251   {
252     mImpl->mThreadSynchronization->PostRenderComplete();
253   }
254 }
255
256 void NativeRenderSurface::WaitUntilSurfaceReplaced()
257 {
258   ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
259   while( !mImpl->mDrawableCompleted )
260   {
261     mImpl->mTbmSurfaceCondition.Wait( lock );
262   }
263
264   mImpl->mDrawableCompleted = false;
265 }
266
267 void NativeRenderSurface::ReleaseDrawable()
268 {
269   if( mImpl->mConsumeSurface )
270   {
271     tbm_surface_internal_unref( mImpl->mConsumeSurface );
272
273     if( tbm_surface_internal_is_valid( mImpl->mConsumeSurface ) )
274     {
275       tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
276     }
277     mImpl->mConsumeSurface = NULL;
278   }
279 }
280
281 } // namespace Dali