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 void WindowRenderSurface::OutputTransformed()
135 if( ecore_wl_window_ignore_output_transform_get( mWlWindow ) )
141 transform = ecore_wl_output_transform_get( ecore_wl_window_output_find( mWlWindow ) );
144 mScreenRotationAngle = transform * 90;
145 mScreenRotationFinished = false;
147 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
150 void WindowRenderSurface::SetTransparency( bool transparent )
152 ecore_wl_window_alpha_set( mWlWindow, transparent );
155 void WindowRenderSurface::InitializeEgl( EglInterface& eglIf )
157 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
159 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
161 eglImpl.ChooseConfig(true, mColorDepth);
164 void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
166 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
168 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
170 // create the EGL window
171 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
173 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.width, mPositionSize.height );
177 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.height, mPositionSize.width );
180 EGLNativeWindowType windowType( mEglWindow );
181 eglImpl.CreateSurfaceWindow( windowType, mColorDepth );
184 wl_egl_window_capability capability = static_cast< wl_egl_window_capability >( wl_egl_window_get_capabilities( mEglWindow ) );
185 if( capability == WL_EGL_WINDOW_CAPABILITY_ROTATION_SUPPORTED )
187 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::CreateEglSurface: capability = %d\n", capability );
188 mRotationSupported = true;
191 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::CreateEglSurface: w = %d h = %d angle = %d screen rotation = %d\n", mPositionSize.width, mPositionSize.height, mRotationAngle, mScreenRotationAngle );
194 void WindowRenderSurface::DestroyEglSurface( EglInterface& eglIf )
196 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
198 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
199 eglImpl.DestroySurface();
201 if( mEglWindow != NULL )
203 wl_egl_window_destroy(mEglWindow);
208 bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& egl )
210 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
212 if( mEglWindow != NULL )
214 wl_egl_window_destroy(mEglWindow);
218 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
220 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.width, mPositionSize.height );
224 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.height, mPositionSize.width );
227 // Set screen rotation
228 mScreenRotationFinished = false;
230 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
231 EGLNativeWindowType windowType( mEglWindow );
232 return eglImpl.ReplaceSurfaceWindow( windowType );
235 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
237 bool needToMove = false;
238 bool needToResize = false;
241 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
242 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
248 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
249 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
256 ecore_wl_window_position_set( mWlWindow, positionSize.x, positionSize.y );
260 ecore_wl_window_update_size( mWlWindow, positionSize.width, positionSize.height );
261 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_wl_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()
423 // if width or height are zero, go full screen.
424 if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
426 // Default window size == screen size
430 ecore_wl_screen_size_get( &mPositionSize.width, &mPositionSize.height );
433 mWlWindow = ecore_wl_window_new( 0, mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height, ECORE_WL_WINDOW_BUFFER_TYPE_EGL_WINDOW );
435 if ( mWlWindow == 0 )
437 DALI_ASSERT_ALWAYS(0 && "Failed to create Wayland window");
440 mWlSurface = ecore_wl_window_surface_create( mWlWindow );
442 if( mColorDepth == COLOR_DEPTH_32 )
444 ecore_wl_window_alpha_set( mWlWindow, true );
448 ecore_wl_window_alpha_set( mWlWindow, false );
451 // Get output transform
452 if( !ecore_wl_window_ignore_output_transform_get( mWlWindow ) )
454 Ecore_Wl_Output* output = ecore_wl_window_output_find( mWlWindow );
456 int transform = ecore_wl_output_transform_get( output );
458 mScreenRotationAngle = transform * 90;
459 mScreenRotationFinished = false;
463 void WindowRenderSurface::UseExistingRenderable( unsigned int surfaceId )
465 mWlWindow = AnyCast< Ecore_Wl_Window* >( surfaceId );
468 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
470 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
472 mThreadSynchronization = &threadSynchronization;
475 void WindowRenderSurface::ReleaseLock()
480 void WindowRenderSurface::ProcessRotationRequest()
482 mRotationFinished = true;
484 ecore_wl_window_rotation_change_done_send( mWlWindow );
486 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
488 if( mThreadSynchronization )
490 mThreadSynchronization->PostRenderComplete();