2 * Copyright (c) 2020 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/common/gl-window-impl.h>
22 #include <dali/integration-api/core.h>
23 #include <dali/devel-api/adaptor-framework/orientation.h>
24 #include <dali/devel-api/adaptor-framework/gl-window.h>
25 #include <dali/integration-api/events/touch-integ.h>
26 #include <dali/integration-api/events/key-event-integ.h>
27 #include <dali/devel-api/events/key-event-devel.h>
30 #include <dali/internal/window-system/common/event-handler.h>
31 #include <dali/internal/window-system/common/orientation-impl.h>
32 #include <dali/internal/window-system/common/window-factory.h>
33 #include <dali/internal/window-system/common/window-base.h>
34 #include <dali/internal/window-system/common/window-system.h>
35 #include <dali/internal/window-system/common/window-impl.h>
36 #include <dali/internal/window-system/common/window-render-surface.h>
37 #include <dali/internal/window-system/common/window-visibility-observer.h>
38 #include <dali/internal/graphics/gles/egl-graphics-factory.h>
39 #include <dali/internal/window-system/common/display-utils.h>
50 const int MINIMUM_DIMENSION_CHANGE( 1 );
52 #if defined(DEBUG_ENABLED)
53 Debug::Filter* gWindowLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_WINDOW" );
56 } // unnamed namespace
58 GlWindow* GlWindow::New( const PositionSize& positionSize, const std::string& name, const std::string& className, bool isTransparent )
60 GlWindow* window = new GlWindow();
61 window->mIsTransparent = isTransparent;
62 window->Initialize( positionSize, name, className );
69 mDisplayConnection( nullptr ),
70 mEventHandler( nullptr ),
72 mColorDepth( COLOR_DEPTH_24 ),
73 mIsTransparent( false ),
74 mIsFocusAcceptable( false ),
76 mOpaqueState( false ),
77 mResizeEnabled( false ),
80 mIsWindowRotated( false ),
84 mTotalRotationAngle( 0 ),
85 mWindowRotationAngle( 0 ),
86 mScreenRotationAngle( 0 ),
87 mOrientationMode( 0 ),
90 mNativeWindowId( -1 ),
95 mVisibilityChangedSignal(),
97 mGLRenderFrameCallback( 0 ),
98 mGLTerminateCallback( 0 ),
99 mGLRenderCallback( nullptr ),
100 mEGLSurface( nullptr ),
101 mEGLContext( nullptr ),
102 mGLESVersion( Dali::GlWindow::GlesVersion::VERSION_3_0 ),
103 mInitCallback( false ),
106 mIsEGLInitialize( false ),
111 GlWindow::~GlWindow()
115 mEventHandler->RemoveObserver( *this );
118 if( mGLTerminateCallback )
120 mGLTerminateCallback();
123 if( mIsEGLInitialize )
125 GraphicsInterface* graphics = mGraphics.get();
126 EglGraphics *eglGraphics = static_cast<EglGraphics*>( graphics );
127 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
131 eglImpl.DestroySurface( mEGLSurface );
132 mEGLSurface = nullptr;
137 eglImpl.DestroyContext( mEGLContext );
138 mEGLContext = nullptr;
141 eglImpl.TerminateGles();
143 mGraphics->Destroy();
147 void GlWindow::Initialize( const PositionSize& positionSize, const std::string& name, const std::string& className )
149 int screenWidth, screenHeight;
151 mPositionSize = positionSize;
152 WindowSystem::GetScreenSize( screenWidth, screenHeight );
153 if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
157 mPositionSize.width = screenWidth;
158 mPositionSize.height = screenHeight;
161 if( screenWidth > screenHeight )
163 mOrientationMode = 1; // Default mode is landscape
167 mOrientationMode = 0; // Default mode is portrate
170 // Create a window base
171 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
173 mWindowBase = windowFactory->CreateWindowBase( mPositionSize, surface, ( mIsTransparent ? true : false ) );
174 mWindowBase->IconifyChangedSignal().Connect( this, &GlWindow::OnIconifyChanged );
175 mWindowBase->FocusChangedSignal().Connect( this, &GlWindow::OnFocusChanged );
177 if( Dali::Adaptor::IsAvailable() )
182 if( !mPositionSize.IsEmpty() )
184 AddAuxiliaryHint( "wm.policy.win.user.geometry", "1" );
185 mResizeEnabled = true;
192 mColorDepth = COLOR_DEPTH_32;
196 mColorDepth = COLOR_DEPTH_24;
199 SetClass( name, className );
202 mNativeWindowId = mWindowBase->GetNativeWindowId();
205 void GlWindow::SetEventHandler()
207 mEventHandler = EventHandlerPtr( new EventHandler( mWindowBase.get(), *this ) );
208 mEventHandler->AddObserver( *this );
211 void GlWindow::SetClass( const std::string& name, const std::string className )
214 mClassName = className;
215 mWindowBase->SetClass( name, className );
218 void GlWindow::SetEglConfig( bool depth, bool stencil, int msaa, Dali::GlWindow::GlesVersion version )
224 mGLESVersion = version;
226 InitializeGraphics();
229 void GlWindow::Raise()
231 mWindowBase->Raise();
232 DALI_LOG_RELEASE_INFO( "Window (%p), WinId (%d), Raise() \n", this, mNativeWindowId );
235 void GlWindow::Lower()
237 mWindowBase->Lower();
238 DALI_LOG_RELEASE_INFO( "Window (%p), WinId (%d), Lower() \n", this, mNativeWindowId );
241 void GlWindow::Activate()
243 mWindowBase->Activate();
244 DALI_LOG_RELEASE_INFO( "Window (%p), WinId (%d), Activate() \n", this, mNativeWindowId );
247 void GlWindow::Show()
255 Dali::GlWindow handle( this );
256 mVisibilityChangedSignal.Emit( handle, true );
261 mEventHandler->Resume();
264 DALI_LOG_RELEASE_INFO( "Window (%p), WinId (%d), Show(): iconified = %d, visible = %d\n", this, mNativeWindowId, mIconified, mVisible );
267 void GlWindow::Hide()
275 Dali::GlWindow handle( this );
276 mVisibilityChangedSignal.Emit( handle, false );
281 mEventHandler->Pause();
284 DALI_LOG_RELEASE_INFO( "Window (%p), WinId (%d), Hide(): iconified = %d, visible = %d\n", this, mNativeWindowId, mIconified, mVisible );
287 unsigned int GlWindow::GetSupportedAuxiliaryHintCount() const
289 return mWindowBase->GetSupportedAuxiliaryHintCount();
292 std::string GlWindow::GetSupportedAuxiliaryHint( unsigned int index ) const
294 return mWindowBase->GetSupportedAuxiliaryHint( index );
297 unsigned int GlWindow::AddAuxiliaryHint( const std::string& hint, const std::string& value )
299 return mWindowBase->AddAuxiliaryHint( hint, value );
302 bool GlWindow::RemoveAuxiliaryHint( unsigned int id )
304 return mWindowBase->RemoveAuxiliaryHint( id );
307 bool GlWindow::SetAuxiliaryHintValue( unsigned int id, const std::string& value )
309 return mWindowBase->SetAuxiliaryHintValue( id, value );
312 std::string GlWindow::GetAuxiliaryHintValue( unsigned int id ) const
314 return mWindowBase->GetAuxiliaryHintValue( id );
317 unsigned int GlWindow::GetAuxiliaryHintId( const std::string& hint ) const
319 return mWindowBase->GetAuxiliaryHintId( hint );
322 void GlWindow::SetInputRegion( const Rect< int >& inputRegion )
324 mWindowBase->SetInputRegion( inputRegion );
326 DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "GlWindow::SetInputRegion: x = %d, y = %d, w = %d, h = %d\n", inputRegion.x, inputRegion.y, inputRegion.width, inputRegion.height );
329 void GlWindow::SetOpaqueState( bool opaque )
331 mOpaqueState = opaque;
333 mWindowBase->SetOpaqueState( opaque );
335 DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "GlWindow::SetOpaqueState: opaque = %d\n", opaque );
338 bool GlWindow::IsOpaqueState() const
343 void GlWindow::SetPositionSize( PositionSize positionSize )
345 if( !mResizeEnabled )
347 AddAuxiliaryHint( "wm.policy.win.user.geometry", "1" );
348 mResizeEnabled = true;
351 bool needToMove = false;
352 bool needToResize = false;
355 if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
356 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
362 if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
363 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
372 mWindowBase->MoveResize( positionSize );
376 mWindowBase->Resize( positionSize );
378 mPositionSize = positionSize;
384 mWindowBase->Move( positionSize );
385 mPositionSize = positionSize;
389 // If window's size or position is changed, the signal will be emitted to user.
390 if( ( needToMove ) || ( needToResize ) )
392 Uint16Pair newSize( mPositionSize.width, mPositionSize.height );
393 Dali::GlWindow handle( this );
394 mResizeSignal.Emit( newSize );
398 PositionSize GlWindow::GetPositionSize() const
400 PositionSize positionSize( mPositionSize );
401 if( mTotalRotationAngle == 90 || mTotalRotationAngle == 270 )
403 positionSize.height = mPositionSize.width;
404 positionSize.width = mPositionSize.height;
410 void GlWindow::OnIconifyChanged( bool iconified )
418 Dali::GlWindow handle( this );
419 mVisibilityChangedSignal.Emit( handle, false );
424 mEventHandler->Pause();
427 DALI_LOG_RELEASE_INFO( "Window (%p), WinId (%d), Iconified: visible = %d\n", this, mNativeWindowId, mVisible );
435 Dali::GlWindow handle( this );
436 mVisibilityChangedSignal.Emit( handle, true );
441 mEventHandler->Resume();
444 DALI_LOG_RELEASE_INFO( "Window (%p), WinId (%d), Deiconified: visible = %d\n", this, mNativeWindowId, mVisible );
448 void GlWindow::OnFocusChanged( bool focusIn )
450 Dali::GlWindow handle( this );
451 mFocusChangeSignal.Emit( handle, focusIn );
454 void GlWindow::OnOutputTransformed()
456 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
457 if( screenRotationAngle != mScreenRotationAngle )
459 mScreenRotationAngle = screenRotationAngle;
460 mTotalRotationAngle = (mWindowRotationAngle + mScreenRotationAngle) % 360;
462 if( mTotalRotationAngle == 90 || mTotalRotationAngle == 270 )
464 mWindowWidth = mPositionSize.height;
465 mWindowHeight = mPositionSize.width;
469 mWindowWidth = mPositionSize.width;
470 mWindowHeight = mPositionSize.height;
473 // Emit Resize signal
474 Dali::GlWindow handle( this );
475 mResizeSignal.Emit( Dali::Uint16Pair( mWindowWidth, mWindowHeight ) );
479 void GlWindow::OnTouchPoint( Dali::Integration::Point& point, int timeStamp )
481 PointState::Type state = point.GetState();
483 if( state == PointState::DOWN )
488 if( state == PointState::UP )
493 if( !mIsTouched && state == PointState::MOTION )
498 RecalculateTouchPosition( point );
499 Dali::TouchEvent touchEvent = Dali::Integration::NewTouchEvent( timeStamp, point );
500 Dali::GlWindow handle( this );
501 mTouchedSignal.Emit( touchEvent );
504 void GlWindow::OnWheelEvent( Dali::Integration::WheelEvent& wheelEvent )
507 //FeedWheelEvent( wheelEvent );
510 void GlWindow::OnKeyEvent( Dali::Integration::KeyEvent& keyEvent )
512 Dali::KeyEvent event = Dali::DevelKeyEvent::New( keyEvent.keyName, keyEvent.logicalKey, keyEvent.keyString, keyEvent.keyCode,
513 keyEvent.keyModifier, keyEvent.time, static_cast<Dali::KeyEvent::State>(keyEvent.state),
514 keyEvent.compose, keyEvent.deviceName, keyEvent.deviceClass, keyEvent.deviceSubclass );
515 Dali::GlWindow handle( this );
516 mKeyEventSignal.Emit( event );
519 void GlWindow::OnRotation( const RotationEvent& rotation )
521 mWindowRotationAngle = rotation.angle;
522 mTotalRotationAngle = ( mWindowRotationAngle + mScreenRotationAngle ) % 360;
523 if( mTotalRotationAngle == 90 || mTotalRotationAngle == 270 )
525 mWindowWidth = mPositionSize.height;
526 mWindowHeight = mPositionSize.width;
530 mWindowWidth = mPositionSize.width;
531 mWindowHeight = mPositionSize.height;
535 mIsWindowRotated = true;
536 DALI_LOG_RELEASE_INFO( "Window (%p), WinId (%d), OnRotation(): resize signal emit [%d x %d]\n", this, mNativeWindowId, mWindowWidth, mWindowHeight );
538 // Emit Resize signal
539 Dali::GlWindow handle( this );
540 mResizeSignal.Emit( Dali::Uint16Pair( mWindowWidth, mWindowHeight ) );
543 void GlWindow::RecalculateTouchPosition( Integration::Point& point )
545 Vector2 position = point.GetScreenPosition();
546 Vector2 convertedPosition;
548 switch( mTotalRotationAngle )
552 convertedPosition.x = static_cast<float>( mWindowWidth ) - position.y;
553 convertedPosition.y = position.x;
558 convertedPosition.x = static_cast<float>( mWindowWidth ) - position.x;
559 convertedPosition.y = static_cast<float>( mWindowHeight ) - position.y;
564 convertedPosition.x = position.y;
565 convertedPosition.y = static_cast<float>( mWindowHeight ) - position.x;
570 convertedPosition = position;
575 point.SetScreenPosition( convertedPosition );
578 void GlWindow::SetAvailableAnlges( const std::vector< int >& angles )
580 if( angles.size() > 4 )
582 DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::SetAvailableAnlges: Invalid vector size! [%d]\n", angles.size() );
586 mWindowBase->SetAvailableAnlges( angles );
589 bool GlWindow::IsOrientationAvailable( Dali::GlWindow::GlWindowOrientation orientation ) const
591 if( orientation <= Dali::GlWindow::GlWindowOrientation::NO_ORIENTATION_PREFERENCE
592 || orientation > Dali::GlWindow::GlWindowOrientation::LANDSCAPE_INVERSE )
594 DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::IsOrientationAvailable: Invalid input orientation [%d]\n", orientation );
600 int GlWindow::ConvertToAngle( Dali::GlWindow::GlWindowOrientation orientation )
602 int convertAngle = 0;
603 if ( mOrientationMode == 0 )
605 convertAngle = ( static_cast< int >( orientation ) ) * 90;
607 else if( mOrientationMode == 1)
609 switch( orientation )
611 case Dali::GlWindow::GlWindowOrientation::LANDSCAPE:
616 case Dali::GlWindow::GlWindowOrientation::PORTRAIT:
621 case Dali::GlWindow::GlWindowOrientation::LANDSCAPE_INVERSE:
626 case Dali::GlWindow::GlWindowOrientation::PORTRAIT_INVERSE:
631 case Dali::GlWindow::GlWindowOrientation::NO_ORIENTATION_PREFERENCE:
641 Dali::GlWindow::GlWindowOrientation GlWindow::ConvertToOrientation( int angle ) const
643 Dali::GlWindow::GlWindowOrientation orientation = Dali::GlWindow::GlWindowOrientation::NO_ORIENTATION_PREFERENCE;
644 if ( mOrientationMode == 0 ) // Portrate mode
646 orientation = static_cast< Dali::GlWindow::GlWindowOrientation >( angle / 90 );
648 else if( mOrientationMode == 1 ) // Landscape mode
654 orientation = Dali::GlWindow::GlWindowOrientation::LANDSCAPE;
659 orientation = Dali::GlWindow::GlWindowOrientation::PORTRAIT;
664 orientation = Dali::GlWindow::GlWindowOrientation::LANDSCAPE_INVERSE;
669 orientation = Dali::GlWindow::GlWindowOrientation::PORTRAIT_INVERSE;
674 orientation = Dali::GlWindow::GlWindowOrientation::NO_ORIENTATION_PREFERENCE;
682 Dali::GlWindow::GlWindowOrientation GlWindow::GetCurrentOrientation() const
684 DALI_LOG_RELEASE_INFO( "Window (%p), WinId (%d), GetCurrentOrientation(): %d\n", this, mNativeWindowId, mTotalRotationAngle );
685 return ConvertToOrientation( mTotalRotationAngle );
688 void GlWindow::SetAvailableOrientations( const Dali::Vector< Dali::GlWindow::GlWindowOrientation >& orientations )
690 Dali::Vector<float>::SizeType count = orientations.Count();
691 for( Dali::Vector<float>::SizeType index = 0; index < count; ++index )
693 if( IsOrientationAvailable( orientations[index] ) == false )
695 DALI_LOG_ERROR("Window::SetAvailableRotationAngles, invalid angle: %d\n", orientations[index]);
700 int angle = ConvertToAngle( orientations[index] );
702 for( std::size_t i = 0; i < mAvailableAngles.size(); i++ )
704 if( mAvailableAngles[i] == angle )
713 DALI_LOG_RELEASE_INFO( "Window (%p), WinId (%d), SetAvailableOrientations: %d\n", this, mNativeWindowId, angle );
714 mAvailableAngles.push_back( angle );
717 SetAvailableAnlges( mAvailableAngles );
720 void GlWindow::SetPreferredOrientation( Dali::GlWindow::GlWindowOrientation orientation )
722 if( IsOrientationAvailable( orientation ) == false )
724 DALI_LOG_ERROR( "Window::SetPreferredOrientation, invalid orientation: %d\n", orientation );
727 mPreferredAngle = ConvertToAngle( orientation );
728 DALI_LOG_RELEASE_INFO( "Window (%p), WinId (%d), SetPreferredOrientation: %d\n", this, mNativeWindowId, mPreferredAngle );
729 mWindowBase->SetPreferredAngle( mPreferredAngle );
732 void GlWindow::SetChild( Dali::Window& child )
734 if( DALI_UNLIKELY( child ) )
736 mChildWindow = child;
737 Internal::Adaptor::Window& windowImpl = Dali::GetImplementation( mChildWindow );
738 WindowRenderSurface* renderSurface = static_cast<WindowRenderSurface*>( windowImpl.GetSurface() );
741 WindowBase* childWindowBase = renderSurface->GetWindowBase();
742 if( childWindowBase )
744 childWindowBase->SetParent( mWindowBase.get() );
750 void GlWindow::RegisterGlCallback( GlInitialize glInit, GlRenderFrame glRenderFrame, GlTerminate glTerminate )
752 if( mIsEGLInitialize == false )
754 InitializeGraphics();
756 mGLInitCallback = glInit;
757 mGLRenderFrameCallback = glRenderFrame;
758 mGLTerminateCallback = glTerminate;
760 mInitCallback = false;
762 if( !mGLRenderCallback )
764 mGLRenderCallback = MakeCallback( this, &GlWindow::RunCallback );
766 if( Dali::Adaptor::IsAvailable() )
768 Dali::Adaptor::Get().AddIdle( mGLRenderCallback, true );
772 DALI_LOG_RELEASE_INFO( "RegisterGlCallback: Adaptor is not avaiable\n" );
778 bool GlWindow::RunCallback()
780 GraphicsInterface* graphics = mGraphics.get();
781 EglGraphics *eglGraphics = static_cast<EglGraphics*>( graphics );
782 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
784 eglImpl.MakeContextCurrent( mEGLSurface, mEGLContext );
788 mWindowBase->SetEglWindowBufferTransform( mTotalRotationAngle );
789 if( mIsWindowRotated )
791 mWindowBase->SetEglWindowTransform( mWindowRotationAngle );
798 if( mGLInitCallback )
802 mInitCallback = true;
805 if( mGLRenderFrameCallback )
807 mGLRenderFrameCallback();
810 if( mIsWindowRotated )
812 mWindowBase->WindowRotationCompleted( mWindowRotationAngle, mPositionSize.width, mPositionSize.height );
813 mIsWindowRotated = false;
816 eglImpl.SwapBuffers( mEGLSurface );
821 void GlWindow::RenderOnce()
826 void GlWindow::InitializeGraphics()
828 if( !mIsEGLInitialize )
831 std::unique_ptr< GraphicsFactory > graphicsFactoryPtr = Utils::MakeUnique< GraphicsFactory >();
832 auto graphicsFactory = *graphicsFactoryPtr.get();
834 mGraphics = std::unique_ptr< GraphicsInterface >( &graphicsFactory.Create() );
835 GraphicsInterface* graphics = mGraphics.get();
836 EglGraphics *eglGraphics = static_cast<EglGraphics*>( graphics );
837 eglGraphics->Initialize( mDepth, mStencil, mMSAA );
838 eglGraphics->Create();
840 mDisplayConnection = std::unique_ptr< Dali::DisplayConnection >( Dali::DisplayConnection::New( *graphics, Dali::RenderSurfaceInterface::Type::WINDOW_RENDER_SURFACE ) );
841 mDisplayConnection->Initialize();
843 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
844 if( mGLESVersion == Dali::GlWindow::GlesVersion::VERSION_2_0 )
846 eglImpl.SetGlesVersion( 20 );
848 else if( mGLESVersion == Dali::GlWindow::GlesVersion::VERSION_3_0 )
850 eglImpl.SetGlesVersion( 30 );
853 if( eglImpl.ChooseConfig(true, mColorDepth) == false )
855 if( mGLESVersion == Dali::GlWindow::GlesVersion::VERSION_3_0 )
857 DALI_LOG_RELEASE_INFO( "InitializeGraphics: Fail to choose config with GLES30, retry with GLES20\n" );
858 eglImpl.SetGlesVersion( 20 );
859 mGLESVersion = Dali::GlWindow::GlesVersion::VERSION_2_0;
860 if( eglImpl.ChooseConfig(true, mColorDepth) == false )
862 DALI_LOG_ERROR("InitializeGraphics: Fail to choose config with GLES20");
868 DALI_LOG_ERROR("InitializeGraphics: Fail to choose config with GLES20");
872 eglImpl.CreateWindowContext( mEGLContext );
874 // Create the EGL window
875 EGLNativeWindowType window = mWindowBase->CreateEglWindow( mPositionSize.width, mPositionSize.height );
876 mEGLSurface = eglImpl.CreateSurfaceWindow( window, mColorDepth );
878 mIsEGLInitialize = true;
882 void GlWindow::OnDamaged( const DamageArea& area )