2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/window-system/tizen-wayland/ecore-wl2/window-render-surface-ecore-wl2.h>
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/integration-api/debug.h>
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>
34 #if defined(DEBUG_ENABLED)
35 extern Debug::Filter* gRenderSurfaceLogFilter;
44 const int MINIMUM_DIMENSION_CHANGE( 1 ); ///< Minimum change for window to be considered to have moved
46 } // unnamed namespace
48 WindowRenderSurface::WindowRenderSurface( Dali::PositionSize positionSize,
50 const std::string& name,
52 : EcoreWlRenderSurface( positionSize, surface, name, isTransparent ),
54 mEcore_Wl2_display( NULL ),
57 mThreadSynchronization( NULL ),
58 mRotationTrigger( NULL ),
60 mScreenRotationAngle( 0 ),
61 mRotationSupported( false ),
62 mRotationFinished( true ),
63 mScreenRotationFinished( true ),
64 mResizeFinished( true )
66 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
70 WindowRenderSurface::~WindowRenderSurface()
72 if( mEglWindow != NULL )
74 wl_egl_window_destroy(mEglWindow);
80 ecore_wl2_window_free( mWlWindow );
83 if( mRotationTrigger )
85 delete mRotationTrigger;
90 Ecore_Wl2_Window* WindowRenderSurface::GetDrawable()
92 // already an e-core type
96 Any WindowRenderSurface::GetSurface()
98 // already an e-core type
99 return Any( mWlWindow );
102 Ecore_Wl2_Window* WindowRenderSurface::GetWlWindow()
107 void WindowRenderSurface::RequestRotation( int angle, int width, int height )
109 if( !mRotationSupported )
111 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: Rotation is not supported!\n" );
115 if( !mRotationTrigger )
117 TriggerEventFactoryInterface& triggerFactory = Internal::Adaptor::Adaptor::GetImplementation( Adaptor::Get() ).GetTriggerEventFactoryInterface();
118 mRotationTrigger = triggerFactory.CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
121 mPositionSize.width = width;
122 mPositionSize.height = height;
124 mRotationAngle = angle;
125 mRotationFinished = false;
127 ecore_wl2_window_rotation_set( mWlWindow, mRotationAngle );
129 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
132 void WindowRenderSurface::OutputTransformed()
136 if( ecore_wl2_window_ignore_output_transform_get( mWlWindow ) )
142 transform = ecore_wl2_output_transform_get( ecore_wl2_window_output_find( mWlWindow ) );
145 mScreenRotationAngle = transform * 90;
146 mScreenRotationFinished = false;
148 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
151 void WindowRenderSurface::SetTransparency( bool transparent )
153 ecore_wl2_window_alpha_set( mWlWindow, transparent );
156 void WindowRenderSurface::InitializeEgl( EglInterface& eglIf )
158 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
160 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
162 eglImpl.ChooseConfig(true, mColorDepth);
165 void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
167 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
169 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
171 // create the EGL window
172 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
174 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.width, mPositionSize.height );
178 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.height, mPositionSize.width );
181 EGLNativeWindowType windowType( mEglWindow );
182 eglImpl.CreateSurfaceWindow( windowType, mColorDepth );
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 )
188 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::CreateEglSurface: capability = %d\n", capability );
189 mRotationSupported = true;
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 );
195 void WindowRenderSurface::DestroyEglSurface( EglInterface& eglIf )
197 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
199 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
200 eglImpl.DestroySurface();
202 if( mEglWindow != NULL )
204 wl_egl_window_destroy(mEglWindow);
209 bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& egl )
211 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
213 if( mEglWindow != NULL )
215 wl_egl_window_destroy(mEglWindow);
219 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
221 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.width, mPositionSize.height );
225 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.height, mPositionSize.width );
228 // Set screen rotation
229 mScreenRotationFinished = false;
231 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
232 EGLNativeWindowType windowType( mEglWindow );
233 return eglImpl.ReplaceSurfaceWindow( windowType );
236 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
238 bool needToMove = false;
239 bool needToResize = false;
242 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
243 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
249 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
250 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
255 if( needToMove || needToResize )
257 ecore_wl2_window_geometry_set( mWlWindow, positionSize.x, positionSize.y, positionSize.width, positionSize.height );
260 mResizeFinished = false;
264 mPositionSize = positionSize;
266 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
269 void WindowRenderSurface::Map()
271 ecore_wl2_window_show(mWlWindow);
274 void WindowRenderSurface::StartRender()
278 bool WindowRenderSurface::PreRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, bool resizingSurface )
280 if( resizingSurface )
282 #ifdef OVER_TIZEN_VERSION_4
283 // Window rotate or screen rotate
284 if( !mRotationFinished || !mScreenRotationFinished )
286 wl_egl_window_rotation rotation;
287 wl_output_transform bufferTransform;
288 int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
294 rotation = ROTATION_0;
295 bufferTransform = WL_OUTPUT_TRANSFORM_NORMAL;
300 rotation = ROTATION_270;
301 bufferTransform = WL_OUTPUT_TRANSFORM_90;
306 rotation = ROTATION_180;
307 bufferTransform = WL_OUTPUT_TRANSFORM_180;
312 rotation = ROTATION_90;
313 bufferTransform = WL_OUTPUT_TRANSFORM_270;
318 rotation = ROTATION_0;
319 bufferTransform = WL_OUTPUT_TRANSFORM_NORMAL;
324 wl_egl_window_set_rotation( mEglWindow, rotation );
326 wl_egl_window_set_buffer_transform( mEglWindow, bufferTransform );
328 // Reset only screen rotation flag
329 mScreenRotationFinished = true;
331 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mRotationAngle, mScreenRotationAngle );
334 // Only window rotate
335 if( !mRotationFinished )
337 wl_output_transform windowTransform;
339 switch( mRotationAngle )
343 windowTransform = WL_OUTPUT_TRANSFORM_NORMAL;
348 windowTransform = WL_OUTPUT_TRANSFORM_90;
353 windowTransform = WL_OUTPUT_TRANSFORM_180;
358 windowTransform = WL_OUTPUT_TRANSFORM_270;
363 windowTransform = WL_OUTPUT_TRANSFORM_NORMAL;
368 wl_egl_window_set_window_transform( mEglWindow, windowTransform );
373 if( !mResizeFinished )
375 wl_egl_window_resize( mEglWindow, mPositionSize.width, mPositionSize.height, mPositionSize.x, mPositionSize.y );
376 mResizeFinished = true;
378 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
385 void WindowRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface, bool resizingSurface )
387 if( resizingSurface )
389 if( !mRotationFinished )
391 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PostRender: Trigger rotation event\n" );
393 mRotationTrigger->Trigger();
395 if( mThreadSynchronization )
397 // Wait until the event-thread complete the rotation event processing
398 mThreadSynchronization->PostRenderWaitForCompletion();
403 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
404 eglImpl.SwapBuffers();
406 if( mRenderNotification )
408 mRenderNotification->Trigger();
412 void WindowRenderSurface::StopRender()
416 void WindowRenderSurface::SetViewMode( ViewMode viewMode )
421 void WindowRenderSurface::CreateWlRenderable()
424 // use default display name as getenv("WAYLAND_DISPLAY")
425 // If specific name is needed,
426 // the name will be changed and should change the code of using ecore_wl2_connected_display_get()
427 // Because default name is used in ecore_wl2_connected_display_get()'s use case.
428 if ( mEcore_Wl2_display == NULL )
430 mEcore_Wl2_display = ecore_wl2_display_connect(NULL);
432 if ( mEcore_Wl2_display == NULL )
434 DALI_LOG_ERROR("Fail to connect ecore_wl2_display");
438 // if width or height are zero, go full screen.
439 if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
441 // Default window size == screen size
445 ecore_wl2_display_screen_size_get( mEcore_Wl2_display, &mPositionSize.width, &mPositionSize.height );
448 mWlWindow = ecore_wl2_window_new( mEcore_Wl2_display, NULL, mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
449 if ( mWlWindow == 0 )
451 DALI_ASSERT_ALWAYS(0 && "Failed to create Wayland window");
454 mWlSurface = ecore_wl2_window_surface_get( mWlWindow );
456 if( mColorDepth == COLOR_DEPTH_32 )
458 ecore_wl2_window_alpha_set( mWlWindow, true );
462 ecore_wl2_window_alpha_set( mWlWindow, false );
465 ecore_wl2_window_type_set( mWlWindow, ECORE_WL2_WINDOW_TYPE_TOPLEVEL);
467 // Get output transform
468 if( !ecore_wl2_window_ignore_output_transform_get( mWlWindow ) )
470 Ecore_Wl2_Output* output = ecore_wl2_window_output_find( mWlWindow );
472 int transform = ecore_wl2_output_transform_get( output );
474 mScreenRotationAngle = transform * 90;
475 mScreenRotationFinished = false;
479 void WindowRenderSurface::UseExistingRenderable( unsigned int surfaceId )
481 mWlWindow = AnyCast< Ecore_Wl2_Window* >( surfaceId );
484 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
486 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
488 mThreadSynchronization = &threadSynchronization;
491 void WindowRenderSurface::ReleaseLock()
496 void WindowRenderSurface::ProcessRotationRequest()
498 mRotationFinished = true;
500 ecore_wl2_window_rotation_change_done_send( mWlWindow, mRotationAngle, mPositionSize.width, mPositionSize.height );
502 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
504 if( mThreadSynchronization )
506 mThreadSynchronization->PostRenderComplete();