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 <window-render-surface.h>
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/integration-api/debug.h>
27 #include <gl/egl-implementation.h>
28 #include <adaptors/common/adaptor-impl.h>
29 #include <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 ),
56 mThreadSynchronization( NULL ),
57 mRotationTrigger( NULL ),
59 mScreenRotationAngle( 0 ),
60 mRotationSupported( false ),
61 mRotationFinished( true ),
62 mScreenRotationFinished( true )
64 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
68 WindowRenderSurface::~WindowRenderSurface()
70 if( mEglWindow != NULL )
72 wl_egl_window_destroy(mEglWindow);
78 ecore_wl_window_free( mWlWindow );
81 if( mRotationTrigger )
83 delete mRotationTrigger;
88 Ecore_Wl_Window* WindowRenderSurface::GetDrawable()
90 // already an e-core type
94 Any WindowRenderSurface::GetSurface()
96 // already an e-core type
97 return Any( mWlWindow );
100 Ecore_Wl_Window* WindowRenderSurface::GetWlWindow()
105 void WindowRenderSurface::RequestRotation( int angle, int width, int height )
107 if( !mRotationSupported )
109 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: Rotation is not supported!\n" );
113 if( !mRotationTrigger )
115 TriggerEventFactoryInterface& triggerFactory = Internal::Adaptor::Adaptor::GetImplementation( Adaptor::Get() ).GetTriggerEventFactoryInterface();
116 mRotationTrigger = triggerFactory.CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
119 mPositionSize.width = width;
120 mPositionSize.height = height;
122 mRotationAngle = angle;
123 mRotationFinished = false;
125 ecore_wl_window_rotation_set( mWlWindow, mRotationAngle );
127 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
130 void WindowRenderSurface::OutputTransformed()
134 if( ecore_wl_window_ignore_output_transform_get( mWlWindow ) )
140 transform = ecore_wl_output_transform_get( ecore_wl_window_output_find( mWlWindow ) );
143 mScreenRotationAngle = transform * 90;
144 mScreenRotationFinished = false;
146 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
149 void WindowRenderSurface::InitializeEgl( EglInterface& eglIf )
151 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
153 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
155 eglImpl.ChooseConfig(true, mColorDepth);
158 void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
160 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
162 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
164 // create the EGL window
165 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
167 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.width, mPositionSize.height );
171 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.height, mPositionSize.width );
174 EGLNativeWindowType windowType( mEglWindow );
175 eglImpl.CreateSurfaceWindow( windowType, mColorDepth );
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 )
181 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::CreateEglSurface: capability = %d\n", capability );
182 mRotationSupported = true;
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 );
188 void WindowRenderSurface::DestroyEglSurface( EglInterface& eglIf )
190 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
192 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
193 eglImpl.DestroySurface();
195 if( mEglWindow != NULL )
197 wl_egl_window_destroy(mEglWindow);
202 bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& egl )
204 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
206 if( mEglWindow != NULL )
208 wl_egl_window_destroy(mEglWindow);
212 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
214 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.width, mPositionSize.height );
218 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.height, mPositionSize.width );
221 // Set screen rotation
222 mScreenRotationFinished = false;
224 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
225 EGLNativeWindowType windowType( mEglWindow );
226 return eglImpl.ReplaceSurfaceWindow( windowType );
229 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
231 bool needToMove = false;
232 bool needToResize = false;
235 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
236 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
242 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
243 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
250 ecore_wl_window_move(mWlWindow, positionSize.x, positionSize.y);
251 mPositionSize = positionSize;
255 ecore_wl_window_resize(mWlWindow, positionSize.width, positionSize.height, 0);
256 mPositionSize = positionSize;
261 void WindowRenderSurface::Map()
263 ecore_wl_window_show(mWlWindow);
266 void WindowRenderSurface::StartRender()
270 bool WindowRenderSurface::PreRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, bool resizingSurface )
272 if( resizingSurface )
274 // Window rotate or screen rotate
275 if( !mRotationFinished || !mScreenRotationFinished )
277 wl_egl_window_rotation rotation;
278 wl_output_transform bufferTransform;
279 int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
285 rotation = ROTATION_0;
286 bufferTransform = WL_OUTPUT_TRANSFORM_NORMAL;
291 rotation = ROTATION_270;
292 bufferTransform = WL_OUTPUT_TRANSFORM_90;
297 rotation = ROTATION_180;
298 bufferTransform = WL_OUTPUT_TRANSFORM_180;
303 rotation = ROTATION_90;
304 bufferTransform = WL_OUTPUT_TRANSFORM_270;
309 rotation = ROTATION_0;
310 bufferTransform = WL_OUTPUT_TRANSFORM_NORMAL;
315 wl_egl_window_set_rotation( mEglWindow, rotation );
317 wl_egl_window_set_buffer_transform( mEglWindow, bufferTransform );
319 // Reset only screen rotation flag
320 mScreenRotationFinished = true;
322 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mRotationAngle, mScreenRotationAngle );
325 // Only window rotate
326 if( !mRotationFinished )
328 wl_output_transform windowTransform;
330 switch( mRotationAngle )
334 windowTransform = WL_OUTPUT_TRANSFORM_NORMAL;
339 windowTransform = WL_OUTPUT_TRANSFORM_90;
344 windowTransform = WL_OUTPUT_TRANSFORM_180;
349 windowTransform = WL_OUTPUT_TRANSFORM_270;
354 windowTransform = WL_OUTPUT_TRANSFORM_NORMAL;
359 wl_egl_window_set_window_transform( mEglWindow, windowTransform );
366 void WindowRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface, bool resizingSurface )
368 if( resizingSurface )
370 if( !mRotationFinished )
372 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PostRender: Trigger rotation event\n" );
374 mRotationTrigger->Trigger();
376 if( mThreadSynchronization )
378 // Wait until the event-thread complete the rotation event processing
379 mThreadSynchronization->PostRenderWaitForCompletion();
384 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
385 eglImpl.SwapBuffers();
387 if( mRenderNotification )
389 mRenderNotification->Trigger();
393 void WindowRenderSurface::StopRender()
397 void WindowRenderSurface::SetViewMode( ViewMode viewMode )
402 void WindowRenderSurface::CreateWlRenderable()
404 // if width or height are zero, go full screen.
405 if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
407 // Default window size == screen size
411 ecore_wl_screen_size_get( &mPositionSize.width, &mPositionSize.height );
414 mWlWindow = ecore_wl_window_new( 0, mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height, ECORE_WL_WINDOW_BUFFER_TYPE_EGL_WINDOW );
416 if ( mWlWindow == 0 )
418 DALI_ASSERT_ALWAYS(0 && "Failed to create Wayland window");
421 mWlSurface = ecore_wl_window_surface_create( mWlWindow );
423 if( mColorDepth == COLOR_DEPTH_32 )
425 ecore_wl_window_alpha_set( mWlWindow, true );
429 ecore_wl_window_alpha_set( mWlWindow, false );
432 // Get output transform
433 if( !ecore_wl_window_ignore_output_transform_get( mWlWindow ) )
435 Ecore_Wl_Output* output = ecore_wl_window_output_find( mWlWindow );
437 int transform = ecore_wl_output_transform_get( output );
439 mScreenRotationAngle = transform * 90;
440 mScreenRotationFinished = false;
444 void WindowRenderSurface::UseExistingRenderable( unsigned int surfaceId )
446 mWlWindow = AnyCast< Ecore_Wl_Window* >( surfaceId );
449 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
451 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
453 mThreadSynchronization = &threadSynchronization;
456 void WindowRenderSurface::ReleaseLock()
461 void WindowRenderSurface::ProcessRotationRequest()
463 mRotationFinished = true;
465 ecore_wl_window_rotation_change_done_send( mWlWindow );
467 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
469 if( mThreadSynchronization )
471 mThreadSynchronization->PostRenderComplete();