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>
23 #include <dali/integration-api/gl-abstraction.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/integration-api/gl-defines.h>
29 #include <gl/egl-implementation.h>
30 #include <adaptors/common/adaptor-impl.h>
31 #include <integration-api/trigger-event-factory-interface.h>
36 #if defined(DEBUG_ENABLED)
37 extern Debug::Filter* gRenderSurfaceLogFilter;
46 const int MINIMUM_DIMENSION_CHANGE( 1 ); ///< Minimum change for window to be considered to have moved
48 } // unnamed namespace
50 WindowRenderSurface::WindowRenderSurface( Dali::PositionSize positionSize,
52 const std::string& name,
54 : EcoreWlRenderSurface( positionSize, surface, name, isTransparent ),
58 mThreadSynchronization( NULL ),
59 mRotationTrigger( NULL ),
61 mScreenRotationAngle( 0 ),
62 mRotationSupported( false ),
63 mRotationFinished( true ),
64 mScreenRotationFinished( true ),
65 mResizeFinished( true )
67 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
71 WindowRenderSurface::~WindowRenderSurface()
73 if( mEglWindow != NULL )
75 wl_egl_window_destroy(mEglWindow);
81 ecore_wl_window_free( mWlWindow );
84 if( mRotationTrigger )
86 delete mRotationTrigger;
91 Ecore_Wl_Window* WindowRenderSurface::GetDrawable()
93 // already an e-core type
97 Any WindowRenderSurface::GetSurface()
99 // already an e-core type
100 return Any( mWlWindow );
103 Ecore_Wl_Window* WindowRenderSurface::GetWlWindow()
108 void WindowRenderSurface::RequestRotation( int angle, int width, int height )
110 if( !mRotationSupported )
112 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: Rotation is not supported!\n" );
116 if( !mRotationTrigger )
118 TriggerEventFactoryInterface& triggerFactory = Internal::Adaptor::Adaptor::GetImplementation( Adaptor::Get() ).GetTriggerEventFactoryInterface();
119 mRotationTrigger = triggerFactory.CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
122 mPositionSize.width = width;
123 mPositionSize.height = height;
125 mRotationAngle = angle;
126 mRotationFinished = false;
128 ecore_wl_window_rotation_set( mWlWindow, mRotationAngle );
130 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
133 void WindowRenderSurface::OutputTransformed()
137 if( ecore_wl_window_ignore_output_transform_get( mWlWindow ) )
143 transform = ecore_wl_output_transform_get( ecore_wl_window_output_find( mWlWindow ) );
146 ecore_wl_window_buffer_transform_set( mWlWindow, transform );
148 mScreenRotationAngle = transform * 90;
149 mScreenRotationFinished = false;
151 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
154 void WindowRenderSurface::SetTransparency( bool transparent )
156 ecore_wl_window_alpha_set( mWlWindow, transparent );
159 void WindowRenderSurface::InitializeEgl( EglInterface& eglIf )
161 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
163 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
165 eglImpl.ChooseConfig(true, mColorDepth);
168 void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
170 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
172 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
174 // create the EGL window
175 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
177 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.width, mPositionSize.height );
181 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.height, mPositionSize.width );
184 EGLNativeWindowType windowType( mEglWindow );
185 eglImpl.CreateSurfaceWindow( windowType, mColorDepth );
188 wl_egl_window_capability capability = static_cast< wl_egl_window_capability >( wl_egl_window_get_capabilities( mEglWindow ) );
189 if( capability == WL_EGL_WINDOW_CAPABILITY_ROTATION_SUPPORTED )
191 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::CreateEglSurface: capability = %d\n", capability );
192 mRotationSupported = true;
195 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::CreateEglSurface: w = %d h = %d angle = %d screen rotation = %d\n", mPositionSize.width, mPositionSize.height, mRotationAngle, mScreenRotationAngle );
198 void WindowRenderSurface::DestroyEglSurface( EglInterface& eglIf )
200 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
202 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
203 eglImpl.DestroySurface();
205 if( mEglWindow != NULL )
207 wl_egl_window_destroy(mEglWindow);
212 bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& egl )
214 DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
216 if( mEglWindow != NULL )
218 wl_egl_window_destroy(mEglWindow);
222 if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
224 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.width, mPositionSize.height );
228 mEglWindow = wl_egl_window_create( mWlSurface, mPositionSize.height, mPositionSize.width );
231 // Set screen rotation
232 mScreenRotationFinished = false;
234 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
235 EGLNativeWindowType windowType( mEglWindow );
236 return eglImpl.ReplaceSurfaceWindow( windowType );
239 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
241 bool needToMove = false;
242 bool needToResize = false;
245 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
246 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
252 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
253 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
260 ecore_wl_window_position_set( mWlWindow, positionSize.x, positionSize.y );
264 ecore_wl_window_update_size( mWlWindow, positionSize.width, positionSize.height );
265 mResizeFinished = false;
268 mPositionSize = positionSize;
270 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
273 void WindowRenderSurface::Map()
275 ecore_wl_window_show(mWlWindow);
278 void WindowRenderSurface::StartRender()
282 bool WindowRenderSurface::PreRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, bool resizingSurface )
284 if( resizingSurface )
286 #ifdef OVER_TIZEN_VERSION_4
287 // Window rotate or screen rotate
288 if( !mRotationFinished || !mScreenRotationFinished )
290 wl_egl_window_rotation rotation;
291 wl_output_transform bufferTransform;
292 int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
298 rotation = ROTATION_0;
299 bufferTransform = WL_OUTPUT_TRANSFORM_NORMAL;
304 rotation = ROTATION_270;
305 bufferTransform = WL_OUTPUT_TRANSFORM_90;
310 rotation = ROTATION_180;
311 bufferTransform = WL_OUTPUT_TRANSFORM_180;
316 rotation = ROTATION_90;
317 bufferTransform = WL_OUTPUT_TRANSFORM_270;
322 rotation = ROTATION_0;
323 bufferTransform = WL_OUTPUT_TRANSFORM_NORMAL;
328 wl_egl_window_set_rotation( mEglWindow, rotation );
330 wl_egl_window_set_buffer_transform( mEglWindow, bufferTransform );
332 // Reset only screen rotation flag
333 mScreenRotationFinished = true;
335 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mRotationAngle, mScreenRotationAngle );
338 // Only window rotate
339 if( !mRotationFinished )
341 wl_output_transform windowTransform;
343 switch( mRotationAngle )
347 windowTransform = WL_OUTPUT_TRANSFORM_NORMAL;
352 windowTransform = WL_OUTPUT_TRANSFORM_90;
357 windowTransform = WL_OUTPUT_TRANSFORM_180;
362 windowTransform = WL_OUTPUT_TRANSFORM_270;
367 windowTransform = WL_OUTPUT_TRANSFORM_NORMAL;
372 wl_egl_window_set_window_transform( mEglWindow, windowTransform );
377 if( !mResizeFinished )
379 wl_egl_window_resize( mEglWindow, mPositionSize.width, mPositionSize.height, mPositionSize.x, mPositionSize.y );
380 mResizeFinished = true;
382 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
389 void WindowRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, DisplayConnection* displayConnection, bool replacingSurface, bool resizingSurface )
391 if( resizingSurface )
393 if( !mRotationFinished )
395 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PostRender: Trigger rotation event\n" );
397 mRotationTrigger->Trigger();
399 if( mThreadSynchronization )
401 // Wait until the event-thread complete the rotation event processing
402 mThreadSynchronization->PostRenderWaitForCompletion();
407 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
408 eglImpl.SwapBuffers();
410 if( mRenderNotification )
412 mRenderNotification->Trigger();
416 void WindowRenderSurface::StopRender()
420 void WindowRenderSurface::SetViewMode( ViewMode viewMode )
425 void WindowRenderSurface::CreateWlRenderable()
427 // if width or height are zero, go full screen.
428 if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
430 // Default window size == screen size
434 ecore_wl_screen_size_get( &mPositionSize.width, &mPositionSize.height );
437 mWlWindow = ecore_wl_window_new( 0, mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height, ECORE_WL_WINDOW_BUFFER_TYPE_EGL_WINDOW );
439 if ( mWlWindow == 0 )
441 DALI_ASSERT_ALWAYS(0 && "Failed to create Wayland window");
444 mWlSurface = ecore_wl_window_surface_create( mWlWindow );
446 if( mColorDepth == COLOR_DEPTH_32 )
448 ecore_wl_window_alpha_set( mWlWindow, true );
452 ecore_wl_window_alpha_set( mWlWindow, false );
455 // Get output transform
456 if( !ecore_wl_window_ignore_output_transform_get( mWlWindow ) )
458 Ecore_Wl_Output* output = ecore_wl_window_output_find( mWlWindow );
460 int transform = ecore_wl_output_transform_get( output );
461 ecore_wl_window_buffer_transform_set( mWlWindow, transform );
463 mScreenRotationAngle = transform * 90;
464 mScreenRotationFinished = false;
468 void WindowRenderSurface::UseExistingRenderable( unsigned int surfaceId )
470 mWlWindow = AnyCast< Ecore_Wl_Window* >( surfaceId );
473 void WindowRenderSurface::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
475 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n" );
477 mThreadSynchronization = &threadSynchronization;
480 void WindowRenderSurface::ReleaseLock()
485 void WindowRenderSurface::ProcessRotationRequest()
487 mRotationFinished = true;
489 ecore_wl_window_rotation_change_done_send( mWlWindow );
491 DALI_LOG_INFO( gRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessRotationRequest: Rotation Done\n" );
493 if( mThreadSynchronization )
495 mThreadSynchronization->PostRenderComplete();