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