[Tizen] Add OffscreenApplication
[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
85 NativeRenderSurfaceEcoreWl::~NativeRenderSurfaceEcoreWl()
86 {
87   if ( mEGLSurface )
88   {
89     DestroySurface();
90   }
91
92   // release the surface if we own one
93   if( mOwnSurface )
94   {
95     ReleaseDrawable();
96
97     if( mTbmQueue )
98     {
99       tbm_surface_queue_destroy( mTbmQueue );
100     }
101
102     DALI_LOG_INFO( gNativeSurfaceLogFilter, Debug::General, "Own tbm surface queue destroy\n" );
103   }
104
105   Dali::Internal::Adaptor::WindowSystem::Shutdown();
106 }
107
108 Any NativeRenderSurfaceEcoreWl::GetDrawable()
109 {
110   return mConsumeSurface;
111 }
112
113 void NativeRenderSurfaceEcoreWl::SetRenderNotification( TriggerEventInterface* renderNotification )
114 {
115   mRenderNotification = renderNotification;
116 }
117
118 void NativeRenderSurfaceEcoreWl::WaitUntilSurfaceReplaced()
119 {
120   ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
121   while( !mDrawableCompleted )
122   {
123     mTbmSurfaceCondition.Wait( lock );
124   }
125
126   mDrawableCompleted = false;
127 }
128
129 PositionSize NativeRenderSurfaceEcoreWl::GetPositionSize() const
130 {
131   return PositionSize( 0, 0, static_cast<int>( mSurfaceSize.GetWidth() ), static_cast<int>( mSurfaceSize.GetHeight() ) );
132 }
133
134 void NativeRenderSurfaceEcoreWl::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
135 {
136   // calculate DPI
137   float xres, yres;
138
139   // 1 inch = 25.4 millimeters
140 #ifdef ECORE_WAYLAND2
141   // TODO: Application should set dpi value in wayland2
142   xres = 96;
143   yres = 96;
144 #else
145   xres = ecore_wl_dpi_get();
146   yres = ecore_wl_dpi_get();
147 #endif
148
149   dpiHorizontal = int( xres + 0.5f );  // rounding
150   dpiVertical   = int( yres + 0.5f );
151 }
152
153 int NativeRenderSurfaceEcoreWl::GetOrientation() const
154 {
155   return 0;
156 }
157
158 void NativeRenderSurfaceEcoreWl::InitializeGraphics()
159 {
160   DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
161
162   mGraphics = &mAdaptor->GetGraphicsInterface();
163   auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
164
165   mEGL = &eglGraphics->GetEglInterface();
166
167   if ( mEGLContext == NULL )
168   {
169     // Create the OpenGL context for this window
170     Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
171     eglImpl.CreateWindowContext( mEGLContext );
172
173     // Create the OpenGL surface
174     CreateSurface();
175   }
176 }
177
178 void NativeRenderSurfaceEcoreWl::CreateSurface()
179 {
180   DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
181
182   auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
183   Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
184
185   mEGLSurface = eglImpl.CreateSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ), mColorDepth );
186 }
187
188 void NativeRenderSurfaceEcoreWl::DestroySurface()
189 {
190   DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
191
192   auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
193   Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
194
195   eglImpl.DestroySurface( mEGLSurface );
196 }
197
198 bool NativeRenderSurfaceEcoreWl::ReplaceGraphicsSurface()
199 {
200   DALI_LOG_TRACE_METHOD( gNativeSurfaceLogFilter );
201
202   if( !mTbmQueue )
203   {
204     return false;
205   }
206
207   auto eglGraphics = static_cast<Internal::Adaptor::EglGraphics *>(mGraphics);
208   Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
209
210   return eglImpl.ReplaceSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mTbmQueue ), mEGLSurface, mEGLContext );
211 }
212
213 void NativeRenderSurfaceEcoreWl::MoveResize( Dali::PositionSize positionSize )
214 {
215   tbm_surface_queue_error_e error = TBM_SURFACE_QUEUE_ERROR_NONE;
216
217   error = tbm_surface_queue_reset( mTbmQueue, positionSize.width, positionSize.height, mTbmFormat );
218
219   if( error != TBM_SURFACE_QUEUE_ERROR_NONE )
220   {
221     DALI_LOG_ERROR( "Failed to resize tbm_surface_queue" );
222   }
223
224   mSurfaceSize.SetWidth( static_cast<uint16_t>( positionSize.width ) );
225   mSurfaceSize.SetHeight( static_cast<uint16_t>( positionSize.height ) );
226 }
227
228 void NativeRenderSurfaceEcoreWl::StartRender()
229 {
230 }
231
232 bool NativeRenderSurfaceEcoreWl::PreRender( bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect )
233 {
234   MakeContextCurrent();
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 ( mOwnSurface )
261   {
262     if( mThreadSynchronization )
263     {
264       mThreadSynchronization->PostRenderStarted();
265     }
266
267     if( tbm_surface_queue_can_acquire( mTbmQueue, 1 ) )
268     {
269       if( tbm_surface_queue_acquire( mTbmQueue, &mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
270       {
271         DALI_LOG_ERROR( "Failed to acquire a tbm_surface\n" );
272         return;
273       }
274     }
275
276     if ( mConsumeSurface )
277     {
278       tbm_surface_internal_ref( mConsumeSurface );
279     }
280
281     if( replacingSurface )
282     {
283       ConditionalWait::ScopedLock lock( mTbmSurfaceCondition );
284       mDrawableCompleted = true;
285       mTbmSurfaceCondition.Notify( lock );
286     }
287
288    // create damage for client applications which wish to know the update timing
289     if( !replacingSurface && mRenderNotification )
290     {
291       // use notification trigger
292       // Tell the event-thread to render the tbm_surface
293       mRenderNotification->Trigger();
294     }
295
296     if( mThreadSynchronization )
297     {
298       // wait until the event-thread completed to use the tbm_surface
299       mThreadSynchronization->PostRenderWaitForCompletion();
300     }
301
302     // release the consumed surface after post render was completed
303     ReleaseDrawable();
304   }
305   else
306   {
307     // create damage for client applications which wish to know the update timing
308     if( !replacingSurface && mRenderNotification )
309     {
310       // use notification trigger
311       // Tell the event-thread to render the tbm_surface
312       mRenderNotification->Trigger();
313     }
314   }
315
316 }
317
318 void NativeRenderSurfaceEcoreWl::StopRender()
319 {
320   ReleaseLock();
321 }
322
323 void NativeRenderSurfaceEcoreWl::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
324 {
325   mThreadSynchronization = &threadSynchronization;
326 }
327
328 Dali::RenderSurfaceInterface::Type NativeRenderSurfaceEcoreWl::GetSurfaceType()
329 {
330   return Dali::RenderSurfaceInterface::NATIVE_RENDER_SURFACE;
331 }
332
333 void NativeRenderSurfaceEcoreWl::MakeContextCurrent()
334 {
335   if ( mEGL != nullptr )
336   {
337     mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
338   }
339 }
340
341 Integration::DepthBufferAvailable NativeRenderSurfaceEcoreWl::GetDepthBufferRequired()
342 {
343   return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
344 }
345
346 Integration::StencilBufferAvailable NativeRenderSurfaceEcoreWl::GetStencilBufferRequired()
347 {
348   return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
349 }
350
351 Any NativeRenderSurfaceEcoreWl::GetNativeHandle()
352 {
353   return mTbmQueue;
354 }
355
356 void NativeRenderSurfaceEcoreWl::ReleaseLock()
357 {
358   if( mThreadSynchronization )
359   {
360     mThreadSynchronization->PostRenderComplete();
361   }
362 }
363
364 void NativeRenderSurfaceEcoreWl::CreateNativeRenderable()
365 {
366   int width = static_cast<int>( mSurfaceSize.GetWidth() );
367   int height = static_cast<int>( mSurfaceSize.GetHeight() );
368
369   // check we're creating one with a valid size
370   DALI_ASSERT_ALWAYS( width > 0 && height > 0 && "tbm_surface size is invalid" );
371
372   mTbmQueue = tbm_surface_queue_create( 3, width, height, mTbmFormat, TBM_BO_DEFAULT );
373
374   if( mTbmQueue )
375   {
376     mOwnSurface = true;
377   }
378   else
379   {
380     mOwnSurface = false;
381   }
382 }
383
384 void NativeRenderSurfaceEcoreWl::ReleaseDrawable()
385 {
386   if( mConsumeSurface )
387   {
388     tbm_surface_internal_unref( mConsumeSurface );
389
390     if( tbm_surface_internal_is_valid( mConsumeSurface ) )
391     {
392       tbm_surface_queue_release( mTbmQueue, mConsumeSurface );
393     }
394     mConsumeSurface = NULL;
395   }
396 }
397
398 } // namespace Dali