[Tizen] Modify parameters of CreateNativeSurface()
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / tizen-wayland / native-render-surface-ecore-wl.cpp
1 /*
2  * Copyright (c) 2020 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 <dali/internal/window-system/tizen-wayland/native-render-surface-ecore-wl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/integration-api/debug.h>
24
25 #ifdef ECORE_WAYLAND2
26 #include <Ecore_Wl2.h>
27 #else
28 #include <Ecore_Wayland.h>
29 #endif
30
31 #include <tbm_bufmgr.h>
32 #include <tbm_surface_internal.h>
33
34 // INTERNAL INCLUDES
35 #include <dali/integration-api/adaptor-framework/thread-synchronization-interface.h>
36 #include <dali/internal/adaptor/common/adaptor-impl.h>
37 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
38 #include <dali/internal/graphics/gles/egl-graphics.h>
39 #include <dali/internal/graphics/gles/egl-implementation.h>
40 #include <dali/internal/system/common/trigger-event.h>
41 #include <dali/internal/window-system/common/display-connection.h>
42 #include <dali/internal/window-system/common/window-system.h>
43
44 namespace Dali
45 {
46
47 namespace
48 {
49
50 #if defined(DEBUG_ENABLED)
51 Debug::Filter* gNativeSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_NATIVE_RENDER_SURFACE");
52 #endif
53
54 } // unnamed namespace
55
56 NativeRenderSurfaceEcoreWl::NativeRenderSurfaceEcoreWl( SurfaceSize surfaceSize, Any surface, bool isTransparent )
57 : mSurfaceSize( surfaceSize ),
58   mRenderNotification( NULL ),
59   mGraphics( NULL ),
60   mEGL( nullptr ),
61   mEGLSurface( nullptr ),
62   mEGLContext( nullptr ),
63   mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
64   mTbmFormat( isTransparent ? TBM_FORMAT_ARGB8888 : TBM_FORMAT_RGB888 ),
65   mOwnSurface( false ),
66   mDrawableCompleted( false ),
67   mTbmQueue( NULL ),
68   mConsumeSurface( NULL ),
69   mThreadSynchronization( NULL )
70 {
71   Dali::Internal::Adaptor::WindowSystem::Initialize();
72
73   if( surface.Empty() )
74   {
75     CreateNativeRenderable();
76   }
77   else
78   {
79     // check we have a valid type
80     DALI_ASSERT_ALWAYS( ( surface.GetType() == typeid (tbm_surface_queue_h) ) && "Surface type is invalid" );
81     mTbmQueue = AnyCast< tbm_surface_queue_h >( surface );
82   }
83
84   setenv( "EGL_PLATFORM", "tbm", 1 );
85 }
86
87 NativeRenderSurfaceEcoreWl::~NativeRenderSurfaceEcoreWl()
88 {
89   if ( mEGLSurface )
90   {
91     DestroySurface();
92   }
93
94   // release the surface if we own one
95   if( mOwnSurface )
96   {
97     ReleaseDrawable();
98
99     if( mTbmQueue )
100     {
101       tbm_surface_queue_destroy( mTbmQueue );
102     }
103
104     DALI_LOG_INFO( gNativeSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n" );
105   }
106
107   Dali::Internal::Adaptor::WindowSystem::Shutdown();
108 }
109
110 Any NativeRenderSurfaceEcoreWl::GetDrawable()
111 {
112   return mConsumeSurface;
113 }
114
115 void NativeRenderSurfaceEcoreWl::SetRenderNotification( TriggerEventInterface* renderNotification )
116 {
117   mRenderNotification = renderNotification;
118 }
119
120 void NativeRenderSurfaceEcoreWl::WaitUntilSurfaceReplaced()
121 {
122   ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
123   while( !mDrawableCompleted )
124   {
125     mTbmSurfaceCondition.Wait( lock );
126   }
127
128   mDrawableCompleted = false;
129 }
130
131 PositionSize NativeRenderSurfaceEcoreWl::GetPositionSize() const
132 {
133   return PositionSize( 0, 0, static_cast<int>( mSurfaceSize.GetWidth() ), static_cast<int>( mSurfaceSize.GetHeight() ) );
134 }
135
136 void NativeRenderSurfaceEcoreWl::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
137 {
138   // calculate DPI
139   float xres, yres;
140
141   // 1 inch = 25.4 millimeters
142 #ifdef ECORE_WAYLAND2
143   // TODO: Application should set dpi value in wayland2
144   xres = 96;
145   yres = 96;
146 #else
147   xres = ecore_wl_dpi_get();
148   yres = ecore_wl_dpi_get();
149 #endif
150
151   dpiHorizontal = int( xres + 0.5f );  // rounding
152   dpiVertical   = int( yres + 0.5f );
153 }
154
155 int NativeRenderSurfaceEcoreWl::GetOrientation() const
156 {
157   return 0;
158 }
159
160 void NativeRenderSurfaceEcoreWl::InitializeGraphics()
161 {
162   DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
163
164   mGraphics = &mAdaptor->GetGraphicsInterface();
165   auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
166
167   mEGL = &eglGraphics->GetEglInterface();
168
169   if ( mEGLContext == NULL )
170   {
171     // Create the OpenGL context for this window
172     Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
173     eglImpl.CreateWindowContext( mEGLContext );
174
175     // Create the OpenGL surface
176     CreateSurface();
177   }
178 }
179
180 void NativeRenderSurfaceEcoreWl::CreateSurface()
181 {
182   DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
183
184   auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
185   Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
186
187   mEGLSurface = eglImpl.CreateSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ), mColorDepth );
188 }
189
190 void NativeRenderSurfaceEcoreWl::DestroySurface()
191 {
192   DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
193
194   auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
195   Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
196
197   eglImpl.DestroySurface( mEGLSurface );
198 }
199
200 bool NativeRenderSurfaceEcoreWl::ReplaceGraphicsSurface()
201 {
202   DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
203
204   if( !mTbmQueue )
205   {
206     return false;
207   }
208
209   auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
210   Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
211
212   return eglImpl.ReplaceSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ), mEGLSurface, mEGLContext );
213 }
214
215 void NativeRenderSurfaceEcoreWl::MoveResize( Dali::PositionSize positionSize )
216 {
217   tbm_surface_queue_error_e error = TBM_SURFACE_QUEUE_ERROR_NONE;
218
219   error = tbm_surface_queue_reset( mTbmQueue, positionSize.width, positionSize.height, mTbmFormat );
220
221   if( error != TBM_SURFACE_QUEUE_ERROR_NONE )
222   {
223     DALI_LOG_ERROR( "Failed to resize tbm_surface_queue" );
224   }
225
226   mSurfaceSize.SetWidth( static_cast<uint16_t>( positionSize.width ) );
227   mSurfaceSize.SetHeight( static_cast<uint16_t>( positionSize.height ) );
228 }
229
230 void NativeRenderSurfaceEcoreWl::StartRender()
231 {
232 }
233
234 bool NativeRenderSurfaceEcoreWl::PreRender( bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect )
235 {
236   auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics*>(mGraphics);
237   if (eglGraphics)
238   {
239     Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
240     if (resizingSurface)
241     {
242       eglImpl.SetFullSwapNextFrame();
243     }
244
245     eglImpl.SetDamage(mEGLSurface, damagedRects, clippingRect);
246   }
247
248   return true;
249 }
250
251 void NativeRenderSurfaceEcoreWl::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface, const std::vector<Rect<int>>& damagedRects )
252 {
253   auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
254   if (eglGraphics)
255   {
256     Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
257     eglImpl.SwapBuffers( mEGLSurface, damagedRects );
258   }
259
260   if( mThreadSynchronization )
261   {
262     mThreadSynchronization->PostRenderStarted();
263   }
264
265   if( tbm_surface_queue_can_acquire( mTbmQueue, 1 ) )
266   {
267     if( tbm_surface_queue_acquire( mTbmQueue, &mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
268     {
269       DALI_LOG_ERROR( "Failed to acquire a tbm_surface\n" );
270       return;
271     }
272   }
273
274   tbm_surface_internal_ref( mConsumeSurface );
275
276   if( replacingSurface )
277   {
278     ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
279     mDrawableCompleted = true;
280     mTbmSurfaceCondition.Notify( lock );
281   }
282
283  // create damage for client applications which wish to know the update timing
284   if( !replacingSurface && mRenderNotification )
285   {
286     // use notification trigger
287     // Tell the event-thread to render the tbm_surface
288     mRenderNotification->Trigger();
289   }
290
291   if( mThreadSynchronization )
292   {
293     // wait until the event-thread completed to use the tbm_surface
294     mThreadSynchronization->PostRenderWaitForCompletion();
295   }
296
297   // release the consumed surface after post render was completed
298   ReleaseDrawable();
299 }
300
301 void NativeRenderSurfaceEcoreWl::StopRender()
302 {
303   ReleaseLock();
304 }
305
306 void NativeRenderSurfaceEcoreWl::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
307 {
308   mThreadSynchronization = &threadSynchronization;
309 }
310
311 Dali::RenderSurfaceInterface::Type NativeRenderSurfaceEcoreWl::GetSurfaceType()
312 {
313   return Dali::RenderSurfaceInterface::NATIVE_RENDER_SURFACE;
314 }
315
316 void NativeRenderSurfaceEcoreWl::MakeContextCurrent()
317 {
318   if ( mEGL != nullptr )
319   {
320     mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
321   }
322 }
323
324 Integration::DepthBufferAvailable NativeRenderSurfaceEcoreWl::GetDepthBufferRequired()
325 {
326   return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
327 }
328
329 Integration::StencilBufferAvailable NativeRenderSurfaceEcoreWl::GetStencilBufferRequired()
330 {
331   return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
332 }
333
334 void NativeRenderSurfaceEcoreWl::ReleaseLock()
335 {
336   if( mThreadSynchronization )
337   {
338     mThreadSynchronization->PostRenderComplete();
339   }
340 }
341
342 void NativeRenderSurfaceEcoreWl::CreateNativeRenderable()
343 {
344   int width = static_cast<int>( mSurfaceSize.GetWidth() );
345   int height = static_cast<int>( mSurfaceSize.GetHeight() );
346
347   // check we're creating one with a valid size
348   DALI_ASSERT_ALWAYS( width > 0 && height > 0 && "tbm_surface size is invalid" );
349
350   mTbmQueue = tbm_surface_queue_create( 3, width, height, mTbmFormat, TBM_BO_DEFAULT );
351
352   if( mTbmQueue )
353   {
354     mOwnSurface = true;
355   }
356   else
357   {
358     mOwnSurface = false;
359   }
360 }
361
362 void NativeRenderSurfaceEcoreWl::ReleaseDrawable()
363 {
364   if( mConsumeSurface )
365   {
366     tbm_surface_internal_unref( mConsumeSurface );
367
368     if( tbm_surface_internal_is_valid( mConsumeSurface ) )
369     {
370       tbm_surface_queue_release( mTbmQueue, mConsumeSurface );
371     }
372     mConsumeSurface = NULL;
373   }
374 }
375
376 } // namespace Dali