Fix memory issue in key grab API
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / native-render-surface-tizen.cpp
1 /*
2  * Copyright (c) 2017 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     mTbmQueue( NULL ),
55     mConsumeSurface( NULL ),
56     mThreadSynchronization( NULL )
57   {
58   }
59
60   PositionSize mPosition;
61   std::string mTitle;
62   TriggerEventInterface* mRenderNotification;
63   ColorDepth mColorDepth;
64   tbm_format mTbmFormat;
65   bool mOwnSurface;
66   bool mDrawableCompleted;
67
68   tbm_surface_queue_h mTbmQueue;
69   tbm_surface_h mConsumeSurface;
70   ThreadSynchronizationInterface* mThreadSynchronization;     ///< A pointer to the thread-synchronization
71   ConditionalWait mTbmSurfaceCondition;
72 };
73
74 NativeRenderSurface::NativeRenderSurface(Dali::PositionSize positionSize,
75                                          const std::string& name,
76                                          bool isTransparent)
77 : mImpl( new Impl( positionSize, name, isTransparent ) )
78 {
79   ecore_wl_init(NULL);
80   CreateNativeRenderable();
81   setenv( "EGL_PLATFORM", "tbm", 1 );
82 }
83
84 NativeRenderSurface::~NativeRenderSurface()
85 {
86   // release the surface if we own one
87   if( mImpl->mOwnSurface )
88   {
89     ReleaseDrawable();
90
91     if( mImpl->mTbmQueue )
92     {
93       tbm_surface_queue_destroy( mImpl->mTbmQueue );
94     }
95
96     delete mImpl;
97
98     DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n" );
99   }
100
101   ecore_wl_shutdown();
102 }
103
104 void NativeRenderSurface::SetRenderNotification( TriggerEventInterface* renderNotification )
105 {
106   mImpl->mRenderNotification = renderNotification;
107 }
108
109 tbm_surface_h NativeRenderSurface::GetDrawable()
110 {
111   return mImpl->mConsumeSurface;
112 }
113
114 Any NativeRenderSurface::GetSurface()
115 {
116   return Any( NULL );
117 }
118
119 void NativeRenderSurface::InitializeEgl( EglInterface& egl )
120 {
121   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
122   unsetenv( "EGL_PLATFORM" );
123
124   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
125
126   eglImpl.ChooseConfig( true, mImpl->mColorDepth );
127 }
128
129 void NativeRenderSurface::CreateEglSurface( EglInterface& egl )
130 {
131   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
132
133   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
134
135   eglImpl.CreateSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mImpl->mTbmQueue ), mImpl->mColorDepth );
136 }
137
138 void NativeRenderSurface::DestroyEglSurface( EglInterface& egl )
139 {
140   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
141
142   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
143   eglImpl.DestroySurface();
144 }
145
146 bool NativeRenderSurface::ReplaceEGLSurface( EglInterface& egl )
147 {
148   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
149
150   if( !mImpl->mTbmQueue )
151   {
152     return false;
153   }
154
155   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
156
157   return eglImpl.ReplaceSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mImpl->mTbmQueue ) );
158 }
159
160 void NativeRenderSurface::StartRender()
161 {
162 }
163
164 bool NativeRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction&, bool )
165 {
166   // nothing to do for pixmaps
167   return true;
168 }
169
170 void NativeRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface, bool resizingSurface )
171 {
172   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
173   eglImpl.SwapBuffers();
174
175   if( mImpl->mThreadSynchronization )
176   {
177     mImpl->mThreadSynchronization->PostRenderStarted();
178   }
179
180   if( tbm_surface_queue_can_acquire( mImpl->mTbmQueue, 1 ) )
181   {
182     if( tbm_surface_queue_acquire( mImpl->mTbmQueue, &mImpl->mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
183     {
184       DALI_LOG_ERROR( "Failed to aquire a tbm_surface\n" );
185       return;
186     }
187   }
188
189   tbm_surface_internal_ref( mImpl->mConsumeSurface );
190
191   if( replacingSurface )
192   {
193     ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
194     mImpl->mDrawableCompleted = true;
195     mImpl->mTbmSurfaceCondition.Notify( lock );
196   }
197
198  // create damage for client applications which wish to know the update timing
199   if( !replacingSurface && mImpl->mRenderNotification )
200   {
201     // use notification trigger
202     // Tell the event-thread to render the tbm_surface
203     mImpl->mRenderNotification->Trigger();
204   }
205
206   if( mImpl->mThreadSynchronization )
207   {
208     // wait until the event-thread completed to use the tbm_surface
209     mImpl->mThreadSynchronization->PostRenderWaitForCompletion();
210   }
211
212   // release the consumed surface after post render was completed
213   ReleaseDrawable();
214 }
215
216 void NativeRenderSurface::StopRender()
217 {
218   ReleaseLock();
219 }
220
221 PositionSize NativeRenderSurface::GetPositionSize() const
222 {
223   return mImpl->mPosition;
224 }
225
226 void NativeRenderSurface::MoveResize( Dali::PositionSize positionSize )
227 {
228 }
229
230 void NativeRenderSurface::SetViewMode( ViewMode viewMode )
231 {
232 }
233
234 void NativeRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
235 {
236   mImpl->mThreadSynchronization = &threadSynchronization;
237 }
238
239 RenderSurface::Type NativeRenderSurface::GetSurfaceType()
240 {
241   return RenderSurface::NATIVE_RENDER_SURFACE;
242 }
243
244 void NativeRenderSurface::CreateNativeRenderable()
245 {
246   // check we're creating one with a valid size
247   DALI_ASSERT_ALWAYS( mImpl->mPosition.width > 0 && mImpl->mPosition.height > 0 && "tbm_surface size is invalid" );
248
249   mImpl->mTbmQueue = tbm_surface_queue_create( 3, mImpl->mPosition.width, mImpl->mPosition.height, mImpl->mTbmFormat, TBM_BO_DEFAULT );
250
251   if( mImpl->mTbmQueue )
252   {
253     mImpl->mOwnSurface = true;
254   }
255   else
256   {
257     mImpl->mOwnSurface = false;
258   }
259 }
260
261 void NativeRenderSurface::ReleaseLock()
262 {
263   if( mImpl->mThreadSynchronization )
264   {
265     mImpl->mThreadSynchronization->PostRenderComplete();
266   }
267 }
268
269 void NativeRenderSurface::WaitUntilSurfaceReplaced()
270 {
271   ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
272   while( !mImpl->mDrawableCompleted )
273   {
274     mImpl->mTbmSurfaceCondition.Wait( lock );
275   }
276
277   mImpl->mDrawableCompleted = false;
278 }
279
280 void NativeRenderSurface::ReleaseDrawable()
281 {
282   if( mImpl->mConsumeSurface )
283   {
284     tbm_surface_internal_unref( mImpl->mConsumeSurface );
285
286     if( tbm_surface_internal_is_valid( mImpl->mConsumeSurface ) )
287     {
288       tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
289     }
290     mImpl->mConsumeSurface = NULL;
291   }
292 }
293
294 } // namespace Dali