From: György Straub Date: Mon, 24 Aug 2020 10:33:35 +0000 (+0100) Subject: [Windows version] WindowImpl cleanup, fixes and improvements. X-Git-Tag: dali_1.9.27~3^2 X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-adaptor.git;a=commitdiff_plain;h=a20dd2f9fa9c1dd30d93820271478da73e5d639a [Windows version] WindowImpl cleanup, fixes and improvements. - renamed the wordy WindowsPlatformImplementation namespace to simply WindowsPlatform; - Made the invariant windowStyle data member, and the edge width / height getters that depend on it, into static consts; - WindowImpl::CreateHwnd() is now static, and no longer takes the name of the window class; - The window class is only registered once, and then shared (instead of registering one for every window, since everything about it - except the name - was shared); - The window class is unregistered once there are no windows left; - WindowImpl unregisters itself from the event handlers when destroyed, or a new HWND was set; - removed redundant static keyword from constant; - renamed mHwndToListeners to fit the convention, and moved it into an anonymous namespace; Change-Id: Ic3e6c614539cc939080bf785ffd7d2443ea1703e Signed-off-by: György Straub --- diff --git a/dali/internal/system/windows/callback-manager-win.cpp b/dali/internal/system/windows/callback-manager-win.cpp index b840cc5..27f9465 100755 --- a/dali/internal/system/windows/callback-manager-win.cpp +++ b/dali/internal/system/windows/callback-manager-win.cpp @@ -62,7 +62,7 @@ bool WinCallbackManager::AddIdleCallback( CallbackBase* callback, bool hasReturn mCallbacks.insert(callback); - WindowsPlatformImplementation::PostWinThreadMessage( WIN_CALLBACK_EVENT, reinterpret_cast(callback), 0 ); + WindowsPlatform::PostWinThreadMessage( WIN_CALLBACK_EVENT, reinterpret_cast(callback), 0 ); return true; } diff --git a/dali/internal/system/windows/timer-impl-win.cpp b/dali/internal/system/windows/timer-impl-win.cpp index 7691199..c30c164 100755 --- a/dali/internal/system/windows/timer-impl-win.cpp +++ b/dali/internal/system/windows/timer-impl-win.cpp @@ -80,7 +80,7 @@ void Timer::Start() { if( 0 > mImpl->mId ) { - mImpl->mId = WindowsPlatformImplementation::SetTimer( mImpl->mInterval, TimerSourceFunc, this ); + mImpl->mId = WindowsPlatform::SetTimer( mImpl->mInterval, TimerSourceFunc, this ); } } @@ -88,7 +88,7 @@ void Timer::Stop() { if( 0 <= mImpl->mId ) { - WindowsPlatformImplementation::KillTimer( mImpl->mId ); + WindowsPlatform::KillTimer( mImpl->mId ); mImpl->mId = -1; } } diff --git a/dali/internal/system/windows/trigger-event.cpp b/dali/internal/system/windows/trigger-event.cpp index af42ad6..66221cb 100755 --- a/dali/internal/system/windows/trigger-event.cpp +++ b/dali/internal/system/windows/trigger-event.cpp @@ -42,7 +42,7 @@ TriggerEvent::TriggerEvent( CallbackBase* callback, TriggerEventInterface::Optio mOptions( options ) { // Create accompanying file descriptor. - mThreadID = WindowsPlatformImplementation::GetCurrentThreadId(); + mThreadID = WindowsPlatform::GetCurrentThreadId(); if ( mThreadID < 0) { @@ -70,7 +70,7 @@ void TriggerEvent::Trigger() // Increment event counter by 1. // Writing to the file descriptor triggers the Dispatch() method in the other thread // (if in multi-threaded environment). - WindowsPlatformImplementation::PostWinThreadMessage( WIN_CALLBACK_EVENT, reinterpret_cast( mSelfCallback ), 0, mThreadID ); + WindowsPlatform::PostWinThreadMessage( WIN_CALLBACK_EVENT, reinterpret_cast( mSelfCallback ), 0, mThreadID ); } else { diff --git a/dali/internal/window-system/windows/platform-implement-win.cpp b/dali/internal/window-system/windows/platform-implement-win.cpp index 65d76b4..d2b7374 100755 --- a/dali/internal/window-system/windows/platform-implement-win.cpp +++ b/dali/internal/window-system/windows/platform-implement-win.cpp @@ -27,7 +27,7 @@ namespace { -static constexpr float INCH = 25.4; +constexpr float INCH = 25.4; } namespace Dali @@ -39,7 +39,7 @@ namespace Internal namespace Adaptor { -namespace WindowsPlatformImplementation +namespace WindowsPlatform { LRESULT CALLBACK WinProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) @@ -50,7 +50,56 @@ LRESULT CALLBACK WinProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) return ret; } -std::map mHWndToListener; +namespace +{ + +const std::string DALI_WINDOW_CLASS_NAME = "DaliWindow"; + +uint32_t sNumWindows = 0; + +void EnsureWindowClassRegistered() +{ + if (sNumWindows == 0) + { + WNDCLASS cs = { 0 }; + cs.cbClsExtra = 0; + cs.cbWndExtra = 0; + cs.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2); + cs.hCursor = NULL; + cs.hIcon = NULL; + cs.hInstance = GetModuleHandle(NULL); + cs.lpfnWndProc = (WNDPROC)WinProc; + cs.lpszClassName = DALI_WINDOW_CLASS_NAME.c_str(); + cs.lpszMenuName = NULL; + cs.style = CS_VREDRAW | CS_HREDRAW; + RegisterClass(&cs); + } +} + +void EnsureWindowClassUnregistered() +{ + if (sNumWindows == 0) + { + UnregisterClass(DALI_WINDOW_CLASS_NAME.c_str(), GetModuleHandle(NULL)); + } +} + +std::map sHWndToListener; + +void RemoveListener(uint64_t hWnd) +{ + std::map::iterator x = sHWndToListener.find(hWnd); + if (sHWndToListener.end() != x) + { + sHWndToListener.erase(x); + } +} + +} + +const uint32_t WindowImpl::STYLE = WS_OVERLAPPED; +const int32_t WindowImpl::EDGE_WIDTH = 8; +const int32_t WindowImpl::EDGE_HEIGHT = 18; WindowImpl::WindowImpl() { @@ -58,19 +107,18 @@ WindowImpl::WindowImpl() mHWnd = 0; mHdc = 0; listener = NULL; - windowStyle = WS_OVERLAPPED; } WindowImpl::~WindowImpl() { - mHWndToListener.erase( mHWnd ); + RemoveListener(mHWnd); } void WindowImpl::ProcWinMessage( uint64_t hWnd, uint32_t uMsg, uint64_t wParam, uint64_t lParam ) { - std::map::iterator x = mHWndToListener.find( hWnd ); + std::map::iterator x = sHWndToListener.find( hWnd ); - if( mHWndToListener.end() != x ) + if( sHWndToListener.end() != x ) { CallbackBase* listener = x->second->listener; @@ -102,7 +150,6 @@ int WindowImpl::GetColorDepth() } uint64_t WindowImpl::CreateHwnd( - _In_opt_ const char *lpClassName, _In_opt_ const char *lpWindowName, _In_ int X, _In_ int Y, @@ -110,25 +157,25 @@ uint64_t WindowImpl::CreateHwnd( _In_ int nHeight, _In_opt_ uint64_t parent ) { - WNDCLASS cs = { 0 }; - cs.cbClsExtra = 0; - cs.cbWndExtra = 0; - cs.hbrBackground = (HBRUSH)( COLOR_WINDOW + 2 ); - cs.hCursor = NULL; - cs.hIcon = NULL; - cs.hInstance = GetModuleHandle( NULL ); - cs.lpfnWndProc = (WNDPROC)WinProc; - cs.lpszClassName = lpClassName; - cs.lpszMenuName = NULL; - cs.style = CS_VREDRAW | CS_HREDRAW; - RegisterClass( &cs ); - - HWND hWnd = CreateWindow( lpClassName, lpWindowName, windowStyle, X, Y, nWidth + 2 * GetEdgeWidth(), nHeight + 2 * GetEdgeHeight(), NULL, NULL, cs.hInstance, NULL ); + EnsureWindowClassRegistered(); + ++sNumWindows; + + HWND hWnd = CreateWindow( DALI_WINDOW_CLASS_NAME.c_str(), lpWindowName, STYLE, X, Y, + nWidth + 2 * EDGE_WIDTH, nHeight + 2 * EDGE_HEIGHT, NULL, NULL, GetModuleHandle(NULL), NULL ); ::ShowWindow( hWnd, SW_SHOW ); - SetHWND( reinterpret_cast(hWnd) ); + return reinterpret_cast(hWnd); +} - return mHWnd; +void WindowImpl::DestroyHWnd(uint64_t hWnd) +{ + if (hWnd != 0) + { + ::DestroyWindow(reinterpret_cast(hWnd)); + + --sNumWindows; + EnsureWindowClassUnregistered(); + } } void WindowImpl::SetListener( CallbackBase *callback ) @@ -144,49 +191,20 @@ bool WindowImpl::PostWinMessage( return (bool)PostMessage( reinterpret_cast( mHWnd ), Msg, wParam, lParam ); } -int32_t WindowImpl::GetEdgeWidth() -{ - switch( windowStyle ) - { - case WS_OVERLAPPED: - { - return 8; - } - default: - { - return 0; - } - } -} - -int32_t WindowImpl::GetEdgeHeight() -{ - switch( windowStyle ) - { - case WS_OVERLAPPED: - { - return 18; - } - default: - { - return 0; - } - } -} - void WindowImpl::SetHWND( uint64_t inHWnd ) { if (mHWnd != inHWnd) { + RemoveListener(mHWnd); + mHWnd = inHWnd; mHdc = reinterpret_cast(GetDC(reinterpret_cast(mHWnd))); colorDepth = GetDeviceCaps(reinterpret_cast(mHdc), BITSPIXEL) * GetDeviceCaps(reinterpret_cast(mHdc), PLANES); - std::map::iterator x = mHWndToListener.find(mHWnd); - - if (mHWndToListener.end() == x) + std::map::iterator x = sHWndToListener.find(mHWnd); + if (sHWndToListener.end() == x) { - mHWndToListener.insert(std::make_pair(mHWnd, this)); + sHWndToListener.insert(std::make_pair(mHWnd, this)); } else { diff --git a/dali/internal/window-system/windows/platform-implement-win.h b/dali/internal/window-system/windows/platform-implement-win.h index e84dce7..6c84b31 100755 --- a/dali/internal/window-system/windows/platform-implement-win.h +++ b/dali/internal/window-system/windows/platform-implement-win.h @@ -34,7 +34,7 @@ namespace Internal namespace Adaptor { -namespace WindowsPlatformImplementation +namespace WindowsPlatform { bool PostWinThreadMessage( @@ -60,18 +60,17 @@ unsigned int GetCurrentMilliSeconds( void ); class WindowImpl { public: + static const uint32_t STYLE; + static const int32_t EDGE_WIDTH; + static const int32_t EDGE_HEIGHT; + WindowImpl(); virtual ~WindowImpl(); static void ProcWinMessage( uint64_t hWnd, uint32_t uMsg, uint64_t wParam, uint64_t lParam ); - void GetDPI( float &xDpi, float &yDpi ); - - int GetColorDepth(); - - uint64_t CreateHwnd( - _In_opt_ const char *lpClassName, + static uint64_t CreateHwnd( _In_opt_ const char *lpWindowName, _In_ int X, _In_ int Y, @@ -79,6 +78,12 @@ public: _In_ int nHeight, _In_opt_ uint64_t parent ); + static void DestroyHWnd(uint64_t hWnd); + + void GetDPI( float &xDpi, float &yDpi ); + + int GetColorDepth(); + void SetListener( CallbackBase *callback ); bool PostWinMessage( @@ -86,21 +91,14 @@ public: _In_ uint64_t wParam, _In_ uint64_t lParam ); - int32_t GetEdgeWidth(); - - int32_t GetEdgeHeight(); - void SetHWND(uint64_t inHWnd); void SetWinProc(); protected: private: - - unsigned long windowStyle; - int colorDepth; - uint64_t mHWnd; + uint64_t mHWnd; // no ownership, managed outside uint64_t mHdc; CallbackBase *listener; diff --git a/dali/internal/window-system/windows/window-base-win.cpp b/dali/internal/window-system/windows/window-base-win.cpp index 86994da..90537a6 100755 --- a/dali/internal/window-system/windows/window-base-win.cpp +++ b/dali/internal/window-system/windows/window-base-win.cpp @@ -127,7 +127,7 @@ void WindowBaseWin::OnMouseButtonDown( int type, TWinEventInfo *event ) Integration::Point point; point.SetDeviceId( touchEvent.multi.device ); point.SetState( state ); - point.SetScreenPosition( Vector2( touchEvent.x, touchEvent.y + mWindowImpl.GetEdgeHeight() ) ); + point.SetScreenPosition( Vector2( touchEvent.x, touchEvent.y + WindowsPlatform::WindowImpl::EDGE_HEIGHT ) ); point.SetRadius( touchEvent.multi.radius, Vector2( touchEvent.multi.radius_x, touchEvent.multi.radius_y ) ); point.SetPressure( touchEvent.multi.pressure ); point.SetAngle( Degree( touchEvent.multi.angle ) ); @@ -151,7 +151,7 @@ void WindowBaseWin::OnMouseButtonUp( int type, TWinEventInfo *event ) Integration::Point point; point.SetDeviceId( touchEvent.multi.device ); point.SetState( state ); - point.SetScreenPosition( Vector2( touchEvent.x, touchEvent.y + mWindowImpl.GetEdgeHeight() ) ); + point.SetScreenPosition( Vector2( touchEvent.x, touchEvent.y + WindowsPlatform::WindowImpl::EDGE_HEIGHT ) ); point.SetRadius( touchEvent.multi.radius, Vector2( touchEvent.multi.radius_x, touchEvent.multi.radius_y ) ); point.SetPressure( touchEvent.multi.pressure ); point.SetAngle( Degree( touchEvent.multi.angle ) ); @@ -175,7 +175,7 @@ void WindowBaseWin::OnMouseButtonMove( int type, TWinEventInfo *event ) Integration::Point point; point.SetDeviceId( touchEvent.multi.device ); point.SetState( state ); - point.SetScreenPosition( Vector2( touchEvent.x, touchEvent.y + mWindowImpl.GetEdgeHeight() ) ); + point.SetScreenPosition( Vector2( touchEvent.x, touchEvent.y + WindowsPlatform::WindowImpl::EDGE_HEIGHT) ); point.SetRadius( touchEvent.multi.radius, Vector2( touchEvent.multi.radius_x, touchEvent.multi.radius_y ) ); point.SetPressure( touchEvent.multi.pressure ); point.SetAngle( Degree( touchEvent.multi.angle ) ); @@ -205,7 +205,7 @@ void WindowBaseWin::OnKeyDown( int type, TWinEventInfo *event ) DALI_LOG_INFO( gWindowBaseLogFilter, Debug::General, "WindowBaseWin::OnKeyDown\n" ); int keyCode = event->wParam; - std::string keyName( WindowsPlatformImplementation::GetKeyName( keyCode ) ); + std::string keyName( WindowsPlatform::GetKeyName( keyCode ) ); std::string keyString; std::string emptyString; @@ -228,7 +228,7 @@ void WindowBaseWin::OnKeyUp( int type, TWinEventInfo *event ) DALI_LOG_INFO( gWindowBaseLogFilter, Debug::General, "WindowBaseWin::OnKeyDown\n" ); int keyCode = event->wParam; - std::string keyName( WindowsPlatformImplementation::GetKeyName( keyCode ) ); + std::string keyName( WindowsPlatform::GetKeyName( keyCode ) ); std::string keyString; std::string emptyString; @@ -479,7 +479,8 @@ unsigned int WindowBaseWin::GetSurfaceId( Any surface ) const void WindowBaseWin::CreateWinWindow( PositionSize positionSize, bool isTransparent ) { - long hWnd = mWindowImpl.CreateHwnd( "Demo", "Demo", positionSize.x, positionSize.y, positionSize.width, positionSize.height, NULL ); + long hWnd = WindowsPlatform::WindowImpl::CreateHwnd( "Demo", positionSize.x, positionSize.y, positionSize.width, positionSize.height, NULL ); + mWindowImpl.SetHWND(hWnd); mWin32Window = static_cast(hWnd); diff --git a/dali/internal/window-system/windows/window-base-win.h b/dali/internal/window-system/windows/window-base-win.h index 48c0560..b4e5a88 100755 --- a/dali/internal/window-system/windows/window-base-win.h +++ b/dali/internal/window-system/windows/window-base-win.h @@ -392,7 +392,7 @@ private: bool mIsTransparent:1; ///< Whether the window is transparent (32 bit or 24 bit) bool mRotationAppSet:1; - WindowsPlatformImplementation::WindowImpl mWindowImpl; + WindowsPlatform::WindowImpl mWindowImpl; }; } // namespace Adaptor