7f1b1a7ba94d272ecc27b4b70600e57830ee8be1
[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
30 // INTERNAL INCLUDES
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>
35
36 namespace Dali
37 {
38
39 #if defined(DEBUG_ENABLED)
40 extern Debug::Filter* gRenderSurfaceLogFilter;
41 #endif
42
43 struct NativeRenderSurface::Impl
44 {
45   Impl( Dali::PositionSize positionSize, const std::string& name, bool isTransparent )
46   : mPosition( positionSize ),
47     mTitle( name ),
48     mRenderNotification( NULL ),
49     mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
50     mTbmFormat( isTransparent ? TBM_FORMAT_ARGB8888 : TBM_FORMAT_RGB888 ),
51     mOwnSurface( false ),
52     mConsumeSurface( NULL ),
53     mThreadSynchronization( NULL )
54   {
55   }
56
57   PositionSize mPosition;
58   std::string mTitle;
59   TriggerEventInterface* mRenderNotification;
60   ColorDepth mColorDepth;
61   tbm_format mTbmFormat;
62   bool mOwnSurface;
63
64   tbm_surface_queue_h mTbmQueue;
65   tbm_surface_h mConsumeSurface;
66   ThreadSynchronizationInterface* mThreadSynchronization;     ///< A pointer to the thread-synchronization
67   ConditionalWait mTbmSurfaceCondition;
68 };
69
70 NativeRenderSurface::NativeRenderSurface(Dali::PositionSize positionSize,
71                                          const std::string& name,
72                                          bool isTransparent)
73 : mImpl( new Impl( positionSize, name, isTransparent ) )
74 {
75   ecore_wl_init(NULL);
76   CreateNativeRenderable();
77 }
78
79 NativeRenderSurface::~NativeRenderSurface()
80 {
81   // release the surface if we own one
82   if( mImpl->mOwnSurface )
83   {
84
85     if( mImpl->mConsumeSurface )
86     {
87       tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
88       mImpl->mConsumeSurface = NULL;
89     }
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
102 void NativeRenderSurface::SetRenderNotification( TriggerEventInterface* renderNotification )
103 {
104   mImpl->mRenderNotification = renderNotification;
105 }
106
107 tbm_surface_h NativeRenderSurface::GetDrawable()
108 {
109   ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
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
123   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
124
125   eglImpl.ChooseConfig( true, mImpl->mColorDepth );
126 }
127
128 void NativeRenderSurface::CreateEglSurface( EglInterface& egl )
129 {
130   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
131
132   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
133
134   eglImpl.CreateSurfaceWindow( (EGLNativeWindowType)mImpl->mTbmQueue, mImpl->mColorDepth );
135 }
136
137 void NativeRenderSurface::DestroyEglSurface( EglInterface& egl )
138 {
139   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
140
141   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
142   eglImpl.DestroySurface();
143 }
144
145 bool NativeRenderSurface::ReplaceEGLSurface( EglInterface& egl )
146 {
147   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
148
149   if( mImpl->mConsumeSurface )
150   {
151     tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
152     mImpl->mConsumeSurface = NULL;
153   }
154
155   if( mImpl->mTbmQueue )
156   {
157     tbm_surface_queue_destroy( mImpl->mTbmQueue );
158   }
159
160   CreateNativeRenderable();
161
162   if( !mImpl->mTbmQueue )
163   {
164     return false;
165   }
166
167   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
168
169   return eglImpl.ReplaceSurfaceWindow( (EGLNativeWindowType)mImpl->mTbmQueue ); // reinterpret_cast does not compile
170 }
171
172 void NativeRenderSurface::StartRender()
173 {
174 }
175
176 bool NativeRenderSurface::PreRender( EglInterface&, Integration::GlAbstraction& )
177 {
178   // nothing to do for pixmaps
179   return true;
180 }
181
182 void NativeRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface )
183 {
184   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
185   eglImpl.SwapBuffers();
186
187   // flush gl instruction queue
188   glAbstraction.Flush();
189
190   {
191     if( tbm_surface_queue_can_acquire( mImpl->mTbmQueue, 1 ) )
192     {
193       if( tbm_surface_queue_acquire( mImpl->mTbmQueue, &mImpl->mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
194       {
195         DALI_LOG_ERROR( "Failed aquire consume tbm_surface\n" );
196         return;
197       }
198     }
199   }
200
201  // create damage for client applications which wish to know the update timing
202   if( mImpl->mRenderNotification )
203   {
204     // use notification trigger
205     // Tell the event-thread to render the pixmap
206     mImpl->mRenderNotification->Trigger();
207   }
208   else
209   {
210     // FIXME
211   }
212
213 }
214
215 void NativeRenderSurface::StopRender()
216 {
217   ReleaseLock();
218 }
219
220 PositionSize NativeRenderSurface::GetPositionSize() const
221 {
222   return mImpl->mPosition;
223 }
224
225 void NativeRenderSurface::MoveResize( Dali::PositionSize positionSize )
226 {
227 }
228
229 void NativeRenderSurface::SetViewMode( ViewMode viewMode )
230 {
231 }
232
233 void NativeRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
234 {
235   mImpl->mThreadSynchronization = &threadSynchronization;
236 }
237
238 RenderSurface::Type NativeRenderSurface::GetSurfaceType()
239 {
240   return RenderSurface::NATIVE_RENDER_SURFACE;
241 }
242
243 void NativeRenderSurface::CreateNativeRenderable()
244 {
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" );
247
248   mImpl->mTbmQueue = tbm_surface_queue_create( 3, mImpl->mPosition.width, mImpl->mPosition.height, mImpl->mTbmFormat, TBM_BO_DEFAULT );
249
250   if( mImpl->mTbmQueue )
251   {
252     mImpl->mOwnSurface = true;
253   }
254   else
255   {
256     mImpl->mOwnSurface = false;
257   }
258 }
259
260 void NativeRenderSurface::ReleaseSurface()
261 {
262   if( mImpl->mConsumeSurface )
263   {
264     tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
265     mImpl->mConsumeSurface = NULL;
266   }
267 }
268
269 void NativeRenderSurface::ReleaseLock()
270 {
271   if( mImpl->mThreadSynchronization )
272   {
273     mImpl->mThreadSynchronization->PostRenderComplete();
274   }
275 }
276
277 } // namespace Dali