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 ),
63 mResizeFinished( true )
65 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
69 WindowRenderSurface::~WindowRenderSurface()
71 if( mEglWindow != NULL )
73 wl_egl_window_destroy(mEglWindow);
79 ecore_wl_window_free( mWlWindow );
82 if( mRotationTrigger )
84 delete mRotationTrigger;
89 Ecore_Wl_Window* WindowRenderSurface::GetDrawable()
91 // already an e-core type
95 Any WindowRenderSurface::GetSurface()
97 // already an e-core type
98 return Any( mWlWindow );
101 Ecore_Wl_Window* WindowRenderSurface::GetWlWindow()
106 void WindowRenderSurface::RequestRotation( int angle, int width, int height )
108 if( !mRotationSupported )
110 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: Rotation is not supported!\n" );
114 if( !mRotationTrigger )
116 TriggerEventFactoryInterface& triggerFactory = Internal::Adaptor::Adaptor::GetImplementation( Adaptor::Get() ).GetTriggerEventFactoryInterface();
117 mRotationTrigger = triggerFactory.CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
120 mPositionSize.width = width;
121 mPositionSize.height = height;
123 mRotationAngle = angle;
124 mRotationFinished = false;
126 ecore_wl_window_rotation_set( mWlWindow, mRotationAngle );
128 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
131 int WindowRenderSurface::OutputTransformed()
135 if( !ecore_wl_window_ignore_output_transform_get( mWlWindow ) )
137 transform = ecore_wl_output_transform_get( ecore_wl_window_output_find( mWlWindow ) );
138 mScreenRotationAngle = transform * 90;
139 mScreenRotationFinished = false;
140 mResizeFinished = false;
142 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
143 return (mRotationAngle + mScreenRotationAngle) % 360;
146 void WindowRenderSurface::SetTransparency( bool transparent )
148 ecore_wl_window_alpha_set( mWlWindow, transparent );
151 void WindowRenderSurface::InitializeEgl( EglInterface& eglIf )
153 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
155 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
157 eglImpl.ChooseConfig(true, mColorDepth);
160 void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
162 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
164 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
166 // create the EGL window
167 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
169 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.width, mPositionSize.height );
173 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.height, mPositionSize.width );
176 EGLNativeWindowType windowType( mEglWindow );
177 eglImpl.CreateSurfaceWindow( windowType, mColorDepth );
180 wl_egl_window_capability capability = static_cast< wl_egl_window_capability >( wl_egl_window_get_capabilities( mEglWindow ) );
181 if( capability == WL_EGL_WINDOW_CAPABILITY_ROTATION_SUPPORTED )
183 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::CreateEglSurface: capability = %d\n", capability );
184 mRotationSupported = true;
187 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::CreateEglSurface: w = %d h = %d angle = %d screen rotation = %d\n", mPositionSize.width, mPositionSize.height, mRotationAngle, mScreenRotationAngle );
190 void WindowRenderSurface::DestroyEglSurface( EglInterface& eglIf )
192 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
194 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
195 eglImpl.DestroySurface();
197 if( mEglWindow != NULL )
199 wl_egl_window_destroy(mEglWindow);
204 bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& egl )
206 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
208 if( mEglWindow != NULL )
210 wl_egl_window_destroy(mEglWindow);
214 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
216 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.width, mPositionSize.height );
220 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.height, mPositionSize.width );
223 // Set screen rotation
224 mScreenRotationFinished = false;
226 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
227 EGLNativeWindowType windowType( mEglWindow );
228 return eglImpl.ReplaceSurfaceWindow( windowType );
231 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
233 bool needToMove = false;
234 bool needToResize = false;
237 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
238 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
244 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
245 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
252 ecore_wl_window_position_set( mWlWindow, positionSize.x, positionSize.y );
256 ecore_wl_window_update_size( mWlWindow, positionSize.width, positionSize.height );
257 mResizeFinished = false;
260 mPositionSize = positionSize;
262 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
265 void WindowRenderSurface::Map()
267 ecore_wl_window_show(mWlWindow);
270 void WindowRenderSurface::StartRender()
274 bool WindowRenderSurface::PreRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, bool resizingSurface )
276 if( resizingSurface )
278 int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
279 #ifdef OVER_TIZEN_VERSION_4
280 // Window rotate or screen rotate
281 if( !mRotationFinished || !mScreenRotationFinished )
283 wl_egl_window_rotation rotation;
284 wl_output_transform bufferTransform;
290 rotation = ROTATION_0;
291 bufferTransform = WL_OUTPUT_TRANSFORM_NORMAL;
296 rotation = ROTATION_270;
297 bufferTransform = WL_OUTPUT_TRANSFORM_90;
302 rotation = ROTATION_180;
303 bufferTransform = WL_OUTPUT_TRANSFORM_180;
308 rotation = ROTATION_90;
309 bufferTransform = WL_OUTPUT_TRANSFORM_270;
314 rotation = ROTATION_0;
315 bufferTransform = WL_OUTPUT_TRANSFORM_NORMAL;
320 wl_egl_window_set_rotation( mEglWindow, rotation );
322 wl_egl_window_set_buffer_transform( mEglWindow, bufferTransform );
324 // Reset only screen rotation flag
325 mScreenRotationFinished = true;
327 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mRotationAngle, mScreenRotationAngle );
330 // Only window rotate
331 if( !mRotationFinished )
333 wl_output_transform windowTransform;
335 switch( mRotationAngle )
339 windowTransform = WL_OUTPUT_TRANSFORM_NORMAL;
344 windowTransform = WL_OUTPUT_TRANSFORM_90;
349 windowTransform = WL_OUTPUT_TRANSFORM_180;
354 windowTransform = WL_OUTPUT_TRANSFORM_270;
359 windowTransform = WL_OUTPUT_TRANSFORM_NORMAL;
364 wl_egl_window_set_window_transform( mEglWindow, windowTransform );
369 if( !mResizeFinished )
371 if( totalAngle == 0 || totalAngle == 180 )
373 wl_egl_window_resize( mEglWindow, mPositionSize.width, mPositionSize.height, mPositionSize.x, mPositionSize.y );
377 wl_egl_window_resize( mEglWindow, mPositionSize.height, mPositionSize.width, mPositionSize.x, mPositionSize.y );
379 mResizeFinished = true;
381 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
388 void WindowRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface, bool resizingSurface )
390 if( resizingSurface )
392 if( !mRotationFinished )
394 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PostRender: Trigger rotation event\n" );
396 mRotationTrigger->Trigger();
398 if( mThreadSynchronization )
400 // Wait until the event-thread complete the rotation event processing
401 mThreadSynchronization->PostRenderWaitForCompletion();
406 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
407 eglImpl.SwapBuffers();
409 if( mRenderNotification )
411 mRenderNotification->Trigger();
415 void WindowRenderSurface::StopRender()
419 void WindowRenderSurface::SetViewMode( ViewMode viewMode )
424 void WindowRenderSurface::CreateWlRenderable()
426 // if width or height are zero, go full screen.
427 if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
429 // Default window size == screen size
433 ecore_wl_screen_size_get( &mPositionSize.width, &mPositionSize.height );
436 mWlWindow = ecore_wl_window_new( 0, mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height, ECORE_WL_WINDOW_BUFFER_TYPE_EGL_WINDOW );
438 if ( mWlWindow == 0 )
440 DALI_ASSERT_ALWAYS(0 && "Failed to create Wayland window");
443 mWlSurface = ecore_wl_window_surface_create( mWlWindow );
445 if( mColorDepth == COLOR_DEPTH_32 )
447 ecore_wl_window_alpha_set( mWlWindow, true );
451 ecore_wl_window_alpha_set( mWlWindow, false );
454 // Get output transform
455 if( !ecore_wl_window_ignore_output_transform_get( mWlWindow ) )
457 Ecore_Wl_Output* output = ecore_wl_window_output_find( mWlWindow );
459 int transform = ecore_wl_output_transform_get( output );
461 mScreenRotationAngle = transform * 90;
462 mScreenRotationFinished = false;
466 void WindowRenderSurface::UseExistingRenderable( unsigned int surfaceId )
468 mWlWindow = AnyCast< Ecore_Wl_Window* >( surfaceId );
471 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
473 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
475 mThreadSynchronization = &threadSynchronization;
478 void WindowRenderSurface::ReleaseLock()
483 void WindowRenderSurface::ProcessRotationRequest()
485 mRotationFinished = true;
487 ecore_wl_window_rotation_change_done_send( mWlWindow );
489 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
491 if( mThreadSynchronization )
493 mThreadSynchronization->PostRenderComplete();