[Tizen] Add log to check when wl_egl_window_destroy is called
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / tizen-wayland / ecore-wl / window-render-surface-ecore-wl.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 <dali/internal/window-system/tizen-wayland/ecore-wl/window-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 // INTERNAL INCLUDES
26 #include <dali/integration-api/wayland/wl-types.h>
27 #include <dali/internal/graphics/gles20/egl-implementation.h>
28 #include <dali/internal/adaptor/common/adaptor-impl.h>
29 #include <dali/integration-api/trigger-event-factory-interface.h>
30
31 namespace Dali
32 {
33
34 #if defined(DEBUG_ENABLED)
35 extern Debug::Filter* gRenderSurfaceLogFilter;
36 #endif
37
38 namespace ECore
39 {
40
41 namespace
42 {
43
44 const int MINIMUM_DIMENSION_CHANGE( 1 ); ///< Minimum change for window to be considered to have moved
45
46 } // unnamed namespace
47
48 WindowRenderSurface::WindowRenderSurface( Dali::PositionSize positionSize,
49                                           Any surface,
50                                           const std::string& name,
51                                           bool isTransparent)
52 : EcoreWlRenderSurface( positionSize, surface, name, isTransparent ),
53   mWlWindow( NULL ),
54   mWlSurface( NULL ),
55   mEglWindow( NULL ),
56   mThreadSynchronization( NULL ),
57   mRotationTrigger( NULL ),
58   mRotationAngle( 0 ),
59   mScreenRotationAngle( 0 ),
60   mRotationSupported( false ),
61   mRotationFinished( true ),
62   mScreenRotationFinished( true ),
63   mResizeFinished( true )
64 {
65   DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
66   Init( surface );
67 }
68
69 WindowRenderSurface::~WindowRenderSurface()
70 {
71   if( mEglWindow != NULL )
72   {
73     DALI_LOG_RELEASE_INFO("Egl window is destroyed by WindowRenderSurface Destructor\n");
74     wl_egl_window_destroy(mEglWindow);
75     mEglWindow = NULL;
76   }
77
78   if( mOwnSurface )
79   {
80     ecore_wl_window_free( mWlWindow );
81   }
82
83   if( mRotationTrigger )
84   {
85     delete mRotationTrigger;
86   }
87
88 }
89
90 Ecore_Wl_Window* WindowRenderSurface::GetDrawable()
91 {
92   // already an e-core type
93   return mWlWindow;
94 }
95
96 Any WindowRenderSurface::GetSurface()
97 {
98   // already an e-core type
99   return Any( mWlWindow );
100 }
101
102 Ecore_Wl_Window* WindowRenderSurface::GetWlWindow()
103 {
104   return mWlWindow;
105 }
106
107 void WindowRenderSurface::RequestRotation( int angle, int width, int height )
108 {
109   if( !mRotationSupported )
110   {
111     DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: Rotation is not supported!\n" );
112     return;
113   }
114
115   if( !mRotationTrigger )
116   {
117     TriggerEventFactoryInterface& triggerFactory = Internal::Adaptor::Adaptor::GetImplementation( Adaptor::Get() ).GetTriggerEventFactoryInterface();
118     mRotationTrigger = triggerFactory.CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
119   }
120
121   mPositionSize.width = width;
122   mPositionSize.height = height;
123
124   mRotationAngle = angle;
125   mRotationFinished = false;
126
127   ecore_wl_window_rotation_set( mWlWindow, mRotationAngle );
128
129   DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
130 }
131
132 void WindowRenderSurface::OutputTransformed()
133 {
134   int transform;
135
136   if( ecore_wl_window_ignore_output_transform_get( mWlWindow ) )
137   {
138     transform = 0;
139   }
140   else
141   {
142     transform = ecore_wl_output_transform_get( ecore_wl_window_output_find( mWlWindow ) );
143   }
144
145   mScreenRotationAngle = transform * 90;
146   mScreenRotationFinished = false;
147
148   DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
149 }
150
151 void WindowRenderSurface::SetTransparency( bool transparent )
152 {
153   ecore_wl_window_alpha_set( mWlWindow, transparent );
154 }
155
156 void WindowRenderSurface::InitializeEgl( EglInterface& eglIf )
157 {
158   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
159
160   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
161
162   eglImpl.ChooseConfig(true, mColorDepth);
163 }
164
165 void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
166 {
167   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
168
169   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
170
171   // create the EGL window
172   if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
173   {
174     mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.width, mPositionSize.height );
175   }
176   else
177   {
178     mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.height, mPositionSize.width );
179   }
180
181   EGLNativeWindowType windowType( mEglWindow );
182   eglImpl.CreateSurfaceWindow( windowType, mColorDepth );
183
184   // Check capability
185   wl_egl_window_capability capability = static_cast< wl_egl_window_capability >( wl_egl_window_get_capabilities( mEglWindow ) );
186   if( capability == WL_EGL_WINDOW_CAPABILITY_ROTATION_SUPPORTED )
187   {
188     DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::CreateEglSurface: capability = %d\n", capability );
189     mRotationSupported = true;
190   }
191
192   DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::CreateEglSurface: w = %d h = %d angle = %d screen rotation = %d\n", mPositionSize.width, mPositionSize.height, mRotationAngle, mScreenRotationAngle );
193 }
194
195 void WindowRenderSurface::DestroyEglSurface( EglInterface& eglIf )
196 {
197   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
198
199   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
200   eglImpl.DestroySurface();
201
202   if( mEglWindow != NULL )
203   {
204     DALI_LOG_RELEASE_INFO("Egl window is destroyed by WindowRenderSurface::DestroyEglSurface()\n");
205     wl_egl_window_destroy(mEglWindow);
206     mEglWindow = NULL;
207   }
208 }
209
210 bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& egl )
211 {
212   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
213
214   if( mEglWindow != NULL )
215   {
216     DALI_LOG_RELEASE_INFO("Egl window is destroyed by WindowRenderSurface::ReplaceEGLSurface()\n");
217     wl_egl_window_destroy(mEglWindow);
218     mEglWindow = NULL;
219   }
220
221   if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
222   {
223     mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.width, mPositionSize.height );
224   }
225   else
226   {
227     mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.height, mPositionSize.width );
228   }
229
230   // Set screen rotation
231   mScreenRotationFinished = false;
232
233   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
234   EGLNativeWindowType windowType( mEglWindow );
235   return eglImpl.ReplaceSurfaceWindow( windowType );
236 }
237
238 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
239 {
240   bool needToMove = false;
241   bool needToResize = false;
242
243   // check moving
244   if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
245       (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
246   {
247     needToMove = true;
248   }
249
250   // check resizing
251   if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
252       (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
253   {
254     needToResize = true;
255   }
256
257   if( needToMove )
258   {
259     ecore_wl_window_position_set( mWlWindow, positionSize.x, positionSize.y );
260   }
261   if( needToResize )
262   {
263     ecore_wl_window_update_size( mWlWindow, positionSize.width, positionSize.height );
264     mResizeFinished = false;
265   }
266
267   mPositionSize = positionSize;
268
269   DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
270 }
271
272 void WindowRenderSurface::Map()
273 {
274   ecore_wl_window_show(mWlWindow);
275 }
276
277 void WindowRenderSurface::StartRender()
278 {
279 }
280
281 bool WindowRenderSurface::PreRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, bool resizingSurface )
282 {
283   if( resizingSurface )
284   {
285 #ifdef OVER_TIZEN_VERSION_4
286     // Window rotate or screen rotate
287     if( !mRotationFinished || !mScreenRotationFinished )
288     {
289       wl_egl_window_rotation rotation;
290       wl_output_transform bufferTransform;
291       int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
292
293       switch( totalAngle )
294       {
295         case 0:
296         {
297           rotation = ROTATION_0;
298           bufferTransform = WL_OUTPUT_TRANSFORM_NORMAL;
299           break;
300         }
301         case 90:
302         {
303           rotation = ROTATION_270;
304           bufferTransform = WL_OUTPUT_TRANSFORM_90;
305           break;
306         }
307         case 180:
308         {
309           rotation = ROTATION_180;
310           bufferTransform = WL_OUTPUT_TRANSFORM_180;
311           break;
312         }
313         case 270:
314         {
315           rotation = ROTATION_90;
316           bufferTransform = WL_OUTPUT_TRANSFORM_270;
317           break;
318         }
319         default:
320         {
321           rotation = ROTATION_0;
322           bufferTransform = WL_OUTPUT_TRANSFORM_NORMAL;
323           break;
324         }
325       }
326
327       wl_egl_window_set_rotation( mEglWindow, rotation );
328
329       wl_egl_window_set_buffer_transform( mEglWindow, bufferTransform );
330
331       // Reset only screen rotation flag
332       mScreenRotationFinished = true;
333
334       DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mRotationAngle, mScreenRotationAngle );
335     }
336
337     // Only window rotate
338     if( !mRotationFinished )
339     {
340       wl_output_transform windowTransform;
341
342       switch( mRotationAngle )
343       {
344         case 0:
345         {
346           windowTransform = WL_OUTPUT_TRANSFORM_NORMAL;
347           break;
348         }
349         case 90:
350         {
351           windowTransform = WL_OUTPUT_TRANSFORM_90;
352           break;
353         }
354         case 180:
355         {
356           windowTransform = WL_OUTPUT_TRANSFORM_180;
357           break;
358         }
359         case 270:
360         {
361           windowTransform = WL_OUTPUT_TRANSFORM_270;
362           break;
363         }
364         default:
365         {
366           windowTransform = WL_OUTPUT_TRANSFORM_NORMAL;
367           break;
368         }
369       }
370
371       wl_egl_window_set_window_transform( mEglWindow, windowTransform );
372     }
373 #endif
374
375     // Resize case
376     if( !mResizeFinished )
377     {
378       wl_egl_window_resize( mEglWindow, mPositionSize.width, mPositionSize.height, mPositionSize.x, mPositionSize.y );
379       mResizeFinished = true;
380
381       DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
382     }
383   }
384
385   return true;
386 }
387
388 void WindowRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface, bool resizingSurface )
389 {
390   if( resizingSurface )
391   {
392     if( !mRotationFinished )
393     {
394       DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PostRender: Trigger rotation event\n" );
395
396       mRotationTrigger->Trigger();
397
398       if( mThreadSynchronization )
399       {
400         // Wait until the event-thread complete the rotation event processing
401         mThreadSynchronization->PostRenderWaitForCompletion();
402       }
403     }
404   }
405
406   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
407   eglImpl.SwapBuffers();
408
409   if( mRenderNotification )
410   {
411     mRenderNotification->Trigger();
412   }
413 }
414
415 void WindowRenderSurface::StopRender()
416 {
417 }
418
419 void WindowRenderSurface::SetViewMode( ViewMode viewMode )
420 {
421   //FIXME
422 }
423
424 void WindowRenderSurface::CreateWlRenderable()
425 {
426    // if width or height are zero, go full screen.
427   if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
428   {
429     // Default window size == screen size
430     mPositionSize.x = 0;
431     mPositionSize.y = 0;
432
433     ecore_wl_screen_size_get( &mPositionSize.width, &mPositionSize.height );
434   }
435
436   mWlWindow = ecore_wl_window_new( 0, mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height, ECORE_WL_WINDOW_BUFFER_TYPE_EGL_WINDOW );
437
438   if ( mWlWindow == 0 )
439   {
440     DALI_ASSERT_ALWAYS(0 && "Failed to create Wayland window");
441   }
442
443   mWlSurface = ecore_wl_window_surface_create( mWlWindow );
444
445   if( mColorDepth == COLOR_DEPTH_32 )
446   {
447     ecore_wl_window_alpha_set( mWlWindow, true );
448   }
449   else
450   {
451     ecore_wl_window_alpha_set( mWlWindow, false );
452   }
453
454   // Get output transform
455   if( !ecore_wl_window_ignore_output_transform_get( mWlWindow ) )
456   {
457     Ecore_Wl_Output* output = ecore_wl_window_output_find( mWlWindow );
458
459     int transform = ecore_wl_output_transform_get( output );
460
461     mScreenRotationAngle = transform * 90;
462     mScreenRotationFinished = false;
463   }
464 }
465
466 void WindowRenderSurface::UseExistingRenderable( unsigned int surfaceId )
467 {
468   mWlWindow = AnyCast< Ecore_Wl_Window* >( surfaceId );
469 }
470
471 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
472 {
473   DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
474
475   mThreadSynchronization = &threadSynchronization;
476 }
477
478 void WindowRenderSurface::ReleaseLock()
479 {
480   // Nothing to do.
481 }
482
483 void WindowRenderSurface::ProcessRotationRequest()
484 {
485   mRotationFinished = true;
486
487   ecore_wl_window_rotation_change_done_send( mWlWindow );
488
489   DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
490
491   if( mThreadSynchronization )
492   {
493     mThreadSynchronization->PostRenderComplete();
494   }
495 }
496
497 } // namespace ECore
498
499 } // namespace Dali