2 * Copyright (c) 2021 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/devel-api/adaptor-framework/gl-window.h>
23 #include <dali/devel-api/adaptor-framework/orientation.h>
24 #include <dali/devel-api/events/key-event-devel.h>
25 #include <dali/integration-api/core.h>
26 #include <dali/integration-api/events/key-event-integ.h>
27 #include <dali/integration-api/events/touch-integ.h>
30 #include <dali/internal/graphics/gles/egl-graphics-factory.h>
31 #include <dali/internal/window-system/common/display-utils.h>
32 #include <dali/internal/window-system/common/event-handler.h>
33 #include <dali/internal/window-system/common/orientation-impl.h>
34 #include <dali/internal/window-system/common/window-base.h>
35 #include <dali/internal/window-system/common/window-factory.h>
36 #include <dali/internal/window-system/common/window-impl.h>
37 #include <dali/internal/window-system/common/window-render-surface.h>
38 #include <dali/internal/window-system/common/window-system.h>
39 #include <dali/internal/window-system/common/window-visibility-observer.h>
49 const int MINIMUM_DIMENSION_CHANGE(1);
51 #if defined(DEBUG_ENABLED)
52 Debug::Filter* gWindowLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_WINDOW");
55 } // unnamed namespace
57 GlWindow* GlWindow::New(const PositionSize& positionSize, const std::string& name, const std::string& className, bool isTransparent)
59 GlWindow* window = new GlWindow();
60 window->mIsTransparent = isTransparent;
61 window->Initialize(positionSize, name, className);
68 mDisplayConnection(nullptr),
69 mGlWindowRenderThread(nullptr),
70 mEventHandler(nullptr),
71 mIsTransparent(false),
72 mIsFocusAcceptable(false),
75 mResizeEnabled(false),
78 mIsEGLInitialized(false),
83 mColorDepth(COLOR_DEPTH_24),
84 mRenderingMode(Dali::GlWindow::RenderingMode::CONTINUOUS),
86 mTotalRotationAngle(0),
87 mWindowRotationAngle(0),
88 mScreenRotationAngle(0),
98 mVisibilityChangedSignal()
102 GlWindow::~GlWindow()
106 mEventHandler->RemoveObserver(*this);
109 if(mGlWindowRenderThread)
111 mGlWindowRenderThread->Stop();
112 mGlWindowRenderThread->Join();
115 if(mIsEGLInitialized)
117 mGraphics->Destroy();
121 void GlWindow::Initialize(const PositionSize& positionSize, const std::string& name, const std::string& className)
123 int screenWidth, screenHeight;
125 mPositionSize = positionSize;
126 WindowSystem::GetScreenSize(screenWidth, screenHeight);
127 if((mPositionSize.width == 0) || (mPositionSize.height == 0))
131 mPositionSize.width = screenWidth;
132 mPositionSize.height = screenHeight;
135 if(screenWidth > screenHeight)
137 mOrientationMode = 1; // Default mode is landscape
141 mOrientationMode = 0; // Default mode is portrate
144 // Create a window base
145 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
147 mWindowBase = windowFactory->CreateWindowBase(mPositionSize, surface, (mIsTransparent ? true : false));
148 mWindowBase->IconifyChangedSignal().Connect(this, &GlWindow::OnIconifyChanged);
149 mWindowBase->FocusChangedSignal().Connect(this, &GlWindow::OnFocusChanged);
150 mWindowBase->OutputTransformedSignal().Connect(this, &GlWindow::OnOutputTransformed);
152 if(Dali::Adaptor::IsAvailable())
157 if(!mPositionSize.IsEmpty())
159 AddAuxiliaryHint("wm.policy.win.user.geometry", "1");
160 mResizeEnabled = true;
167 mColorDepth = COLOR_DEPTH_32;
171 mColorDepth = COLOR_DEPTH_24;
174 SetClass(name, className);
177 mNativeWindowId = mWindowBase->GetNativeWindowId();
180 void GlWindow::SetEventHandler()
182 mEventHandler = EventHandlerPtr(new EventHandler(mWindowBase.get(), *this));
183 mEventHandler->AddObserver(*this);
186 void GlWindow::SetClass(const std::string& name, const std::string className)
189 mClassName = className;
190 mWindowBase->SetClass(name, className);
193 void GlWindow::SetEglConfig(bool depth, bool stencil, int msaa, Dali::GlWindow::GlesVersion version)
200 InitializeGraphics();
204 if(version == Dali::GlWindow::GlesVersion::VERSION_2_0)
208 else if(version == Dali::GlWindow::GlesVersion::VERSION_3_0)
213 mGlWindowRenderThread->SetEglConfig(depth, stencil, msaa, rVersion);
216 void GlWindow::Raise()
218 mWindowBase->Raise();
219 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Raise() \n", this, mNativeWindowId);
222 void GlWindow::Lower()
224 mWindowBase->Lower();
225 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Lower() \n", this, mNativeWindowId);
228 void GlWindow::Activate()
230 mWindowBase->Activate();
231 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Activate() \n", this, mNativeWindowId);
234 void GlWindow::Show()
242 Dali::GlWindow handle(this);
243 mVisibilityChangedSignal.Emit(handle, true);
248 mEventHandler->Resume();
251 if(mGlWindowRenderThread)
253 mGlWindowRenderThread->Resume();
256 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Show(): iconified = %d, visible = %d\n", this, mNativeWindowId, mIconified, mVisible);
259 void GlWindow::Hide()
267 Dali::GlWindow handle(this);
268 mVisibilityChangedSignal.Emit(handle, false);
273 mEventHandler->Pause();
276 if(mGlWindowRenderThread)
278 mGlWindowRenderThread->Pause();
281 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Hide(): iconified = %d, visible = %d\n", this, mNativeWindowId, mIconified, mVisible);
284 unsigned int GlWindow::GetSupportedAuxiliaryHintCount() const
286 return mWindowBase->GetSupportedAuxiliaryHintCount();
289 std::string GlWindow::GetSupportedAuxiliaryHint(unsigned int index) const
291 return mWindowBase->GetSupportedAuxiliaryHint(index);
294 unsigned int GlWindow::AddAuxiliaryHint(const std::string& hint, const std::string& value)
296 return mWindowBase->AddAuxiliaryHint(hint, value);
299 bool GlWindow::RemoveAuxiliaryHint(unsigned int id)
301 return mWindowBase->RemoveAuxiliaryHint(id);
304 bool GlWindow::SetAuxiliaryHintValue(unsigned int id, const std::string& value)
306 return mWindowBase->SetAuxiliaryHintValue(id, value);
309 std::string GlWindow::GetAuxiliaryHintValue(unsigned int id) const
311 return mWindowBase->GetAuxiliaryHintValue(id);
314 unsigned int GlWindow::GetAuxiliaryHintId(const std::string& hint) const
316 return mWindowBase->GetAuxiliaryHintId(hint);
319 void GlWindow::SetInputRegion(const Rect<int>& inputRegion)
321 mWindowBase->SetInputRegion(inputRegion);
323 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);
326 void GlWindow::SetOpaqueState(bool opaque)
328 mOpaqueState = opaque;
330 mWindowBase->SetOpaqueState(opaque);
332 DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "GlWindow::SetOpaqueState: opaque = %d\n", opaque);
335 bool GlWindow::IsOpaqueState() const
340 void GlWindow::SetPositionSize(PositionSize positionSize)
344 AddAuxiliaryHint("wm.policy.win.user.geometry", "1");
345 mResizeEnabled = true;
348 bool needToMove = false;
349 bool needToResize = false;
352 if((fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
353 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE))
359 if((fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
360 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE))
369 mWindowBase->MoveResize(positionSize);
373 mWindowBase->Resize(positionSize);
375 mPositionSize = positionSize;
381 mWindowBase->Move(positionSize);
382 mPositionSize = positionSize;
386 // If window's size or position is changed, the signal will be emitted to user.
387 if(needToMove || needToResize)
389 Uint16Pair newSize(mPositionSize.width, mPositionSize.height);
390 Dali::GlWindow handle(this);
391 mResizeSignal.Emit(newSize);
393 if(mGlWindowRenderThread)
395 mGlWindowRenderThread->RequestWindowResize(mPositionSize.width, mPositionSize.height);
400 PositionSize GlWindow::GetPositionSize() const
402 PositionSize positionSize(mPositionSize);
403 if(mTotalRotationAngle == 90 || mTotalRotationAngle == 270)
405 positionSize.height = mPositionSize.width;
406 positionSize.width = mPositionSize.height;
412 void GlWindow::OnIconifyChanged(bool iconified)
420 Dali::GlWindow handle(this);
421 mVisibilityChangedSignal.Emit(handle, false);
426 mEventHandler->Pause();
429 if(mGlWindowRenderThread)
431 mGlWindowRenderThread->Pause();
434 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Iconified: visible = %d\n", this, mNativeWindowId, mVisible);
442 Dali::GlWindow handle(this);
443 mVisibilityChangedSignal.Emit(handle, true);
448 mEventHandler->Resume();
451 if(mGlWindowRenderThread)
453 mGlWindowRenderThread->Resume();
456 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Deiconified: visible = %d\n", this, mNativeWindowId, mVisible);
460 void GlWindow::OnFocusChanged(bool focusIn)
462 Dali::GlWindow handle(this);
463 mFocusChangeSignal.Emit(handle, focusIn);
466 void GlWindow::OnOutputTransformed()
468 int newScreenRotationAngle = mWindowBase->GetScreenRotationAngle();
469 DALI_LOG_RELEASE_INFO("GlWindow::OnOutputTransformed(), screen rotation occurs, old[%d], new[%d\n", mScreenRotationAngle, newScreenRotationAngle);
471 if(newScreenRotationAngle != mScreenRotationAngle)
473 UpdateScreenRotation(newScreenRotationAngle);
477 void GlWindow::OnTouchPoint(Dali::Integration::Point& point, int timeStamp)
479 PointState::Type state = point.GetState();
481 if(state == PointState::DOWN)
486 if(state == PointState::UP)
491 if(!mIsTouched && state == PointState::MOTION)
496 RecalculateTouchPosition(point);
497 Dali::TouchEvent touchEvent = Dali::Integration::NewTouchEvent(timeStamp, point);
498 Dali::GlWindow handle(this);
499 mTouchedSignal.Emit(touchEvent);
502 void GlWindow::OnWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
505 //FeedWheelEvent( wheelEvent );
508 void GlWindow::OnKeyEvent(Dali::Integration::KeyEvent& keyEvent)
510 Dali::KeyEvent event = Dali::DevelKeyEvent::New(keyEvent.keyName, keyEvent.logicalKey, keyEvent.keyString, keyEvent.keyCode, keyEvent.keyModifier, keyEvent.time, static_cast<Dali::KeyEvent::State>(keyEvent.state), keyEvent.compose, keyEvent.deviceName, keyEvent.deviceClass, keyEvent.deviceSubclass);
511 Dali::GlWindow handle(this);
512 mKeyEventSignal.Emit(event);
515 void GlWindow::OnRotation(const RotationEvent& rotation)
517 mWindowRotationAngle = rotation.angle;
518 mTotalRotationAngle = (mWindowRotationAngle + mScreenRotationAngle) % 360;
519 if(mTotalRotationAngle == 90 || mTotalRotationAngle == 270)
521 mWindowWidth = mPositionSize.height;
522 mWindowHeight = mPositionSize.width;
526 mWindowWidth = mPositionSize.width;
527 mWindowHeight = mPositionSize.height;
530 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), OnRotation(): resize signal emit [%d x %d]\n", this, mNativeWindowId, mWindowWidth, mWindowHeight);
532 // Emit Resize signal
533 Dali::GlWindow handle(this);
534 mResizeSignal.Emit(Dali::Uint16Pair(mWindowWidth, mWindowHeight));
536 if(mGlWindowRenderThread)
538 mGlWindowRenderThread->RequestWindowRotate(mWindowRotationAngle);
542 void GlWindow::RecalculateTouchPosition(Integration::Point& point)
544 Vector2 position = point.GetScreenPosition();
545 Vector2 convertedPosition;
547 switch(mTotalRotationAngle)
551 convertedPosition.x = static_cast<float>(mWindowWidth) - position.y;
552 convertedPosition.y = position.x;
557 convertedPosition.x = static_cast<float>(mWindowWidth) - position.x;
558 convertedPosition.y = static_cast<float>(mWindowHeight) - position.y;
563 convertedPosition.x = position.y;
564 convertedPosition.y = static_cast<float>(mWindowHeight) - position.x;
569 convertedPosition = position;
574 point.SetScreenPosition(convertedPosition);
577 void GlWindow::SetAvailableAnlges(const std::vector<int>& angles)
579 if(angles.size() > 4)
581 DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window::SetAvailableAnlges: Invalid vector size! [%d]\n", angles.size());
585 mWindowBase->SetAvailableAnlges(angles);
588 bool GlWindow::IsOrientationAvailable(WindowOrientation orientation) const
590 if(orientation <= WindowOrientation::NO_ORIENTATION_PREFERENCE || orientation > WindowOrientation::LANDSCAPE_INVERSE)
592 DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window::IsOrientationAvailable: Invalid input orientation [%d]\n", orientation);
598 int GlWindow::ConvertToAngle(WindowOrientation orientation)
600 int convertAngle = 0;
601 if(mOrientationMode == 0)
603 convertAngle = static_cast<int>(orientation);
605 else if(mOrientationMode == 1)
609 case WindowOrientation::LANDSCAPE:
614 case WindowOrientation::PORTRAIT:
619 case WindowOrientation::LANDSCAPE_INVERSE:
624 case WindowOrientation::PORTRAIT_INVERSE:
629 case WindowOrientation::NO_ORIENTATION_PREFERENCE:
639 WindowOrientation GlWindow::ConvertToOrientation(int angle) const
641 WindowOrientation orientation = WindowOrientation::NO_ORIENTATION_PREFERENCE;
642 if(mOrientationMode == 0) // Portrate mode
644 orientation = static_cast<WindowOrientation>(angle);
646 else if(mOrientationMode == 1) // Landscape mode
652 orientation = WindowOrientation::LANDSCAPE;
657 orientation = WindowOrientation::PORTRAIT;
662 orientation = WindowOrientation::LANDSCAPE_INVERSE;
667 orientation = WindowOrientation::PORTRAIT_INVERSE;
672 orientation = WindowOrientation::NO_ORIENTATION_PREFERENCE;
680 WindowOrientation GlWindow::GetCurrentOrientation() const
682 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), GetCurrentOrientation(): %d\n", this, mNativeWindowId, mTotalRotationAngle);
683 return ConvertToOrientation(mTotalRotationAngle);
686 void GlWindow::SetAvailableOrientations(const Dali::Vector<WindowOrientation>& orientations)
688 Dali::Vector<float>::SizeType count = orientations.Count();
689 for(Dali::Vector<float>::SizeType index = 0; index < count; ++index)
691 if(IsOrientationAvailable(orientations[index]) == false)
693 DALI_LOG_ERROR("Window::SetAvailableRotationAngles, invalid angle: %d\n", orientations[index]);
698 int angle = ConvertToAngle(orientations[index]);
700 for(std::size_t i = 0; i < mAvailableAngles.size(); i++)
702 if(mAvailableAngles[i] == angle)
711 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), SetAvailableOrientations: %d\n", this, mNativeWindowId, angle);
712 mAvailableAngles.push_back(angle);
715 SetAvailableAnlges(mAvailableAngles);
718 void GlWindow::SetPreferredOrientation(WindowOrientation orientation)
720 if(IsOrientationAvailable(orientation) == false)
722 DALI_LOG_ERROR("Window::SetPreferredOrientation, invalid orientation: %d\n", orientation);
725 mPreferredAngle = ConvertToAngle(orientation);
726 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), SetPreferredOrientation: %d\n", this, mNativeWindowId, mPreferredAngle);
727 mWindowBase->SetPreferredAngle(mPreferredAngle);
730 void GlWindow::SetChild(Dali::Window& child)
732 if(DALI_UNLIKELY(child))
734 mChildWindow = child;
735 Internal::Adaptor::Window& windowImpl = Dali::GetImplementation(mChildWindow);
736 WindowRenderSurface* renderSurface = static_cast<WindowRenderSurface*>(windowImpl.GetSurface());
739 WindowBase* childWindowBase = renderSurface->GetWindowBase();
742 childWindowBase->SetParent(mWindowBase.get());
748 void GlWindow::RegisterGlCallback(CallbackBase* initCallback, CallbackBase* renderFrameCallback, CallbackBase* terminateCallback)
750 if(mIsEGLInitialized == false)
752 InitializeGraphics();
754 mGlWindowRenderThread->RegisterGlCallback(initCallback, renderFrameCallback, terminateCallback);
755 mGlWindowRenderThread->Start();
758 void GlWindow::RenderOnce()
760 if(mGlWindowRenderThread)
762 mGlWindowRenderThread->RenderOnce();
766 void GlWindow::SetRenderingMode(Dali::GlWindow::RenderingMode mode)
768 mRenderingMode = mode;
769 if(mGlWindowRenderThread)
771 bool onDemand = false;
772 if(mRenderingMode == Dali::GlWindow::RenderingMode::ON_DEMAND)
776 mGlWindowRenderThread->SetOnDemandRenderMode(onDemand);
780 Dali::GlWindow::RenderingMode GlWindow::GetRenderingMode() const
782 return mRenderingMode;
785 void GlWindow::InitializeGraphics()
787 if(!mIsEGLInitialized)
790 std::unique_ptr<GraphicsFactory> graphicsFactoryPtr = Utils::MakeUnique<GraphicsFactory>(mEnvironmentOptions);
791 auto graphicsFactory = *graphicsFactoryPtr.get();
793 mGraphics = std::unique_ptr<GraphicsInterface>(&graphicsFactory.Create());
794 GraphicsInterface* graphics = mGraphics.get();
795 EglGraphics* eglGraphics = static_cast<EglGraphics*>(graphics);
796 eglGraphics->Initialize(mDepth, mStencil, false, mMSAA);
798 mDisplayConnection = std::unique_ptr<Dali::DisplayConnection>(Dali::DisplayConnection::New(*graphics, Dali::RenderSurfaceInterface::Type::WINDOW_RENDER_SURFACE));
799 mDisplayConnection->Initialize();
801 // Create Render Thread
802 mGlWindowRenderThread = std::unique_ptr<Dali::Internal::Adaptor::GlWindowRenderThread>(new GlWindowRenderThread(mPositionSize, mColorDepth));
803 if(!mGlWindowRenderThread)
805 DALI_LOG_ERROR("Fail to create GlWindow Render Thread!!!!\n");
809 mGlWindowRenderThread->SetGraphicsInterface(graphics);
810 mGlWindowRenderThread->SetWindowBase(mWindowBase.get());
811 bool onDemand = false;
812 if(mRenderingMode == Dali::GlWindow::RenderingMode::ON_DEMAND)
816 mGlWindowRenderThread->SetOnDemandRenderMode(onDemand);
818 mIsEGLInitialized = true;
820 // Check screen rotation
821 int newScreenRotationAngle = mWindowBase->GetScreenRotationAngle();
822 DALI_LOG_RELEASE_INFO("GlWindow::InitializeGraphics(), GetScreenRotationAngle(): %d\n", mScreenRotationAngle);
823 if(newScreenRotationAngle != 0)
825 UpdateScreenRotation(newScreenRotationAngle);
830 void GlWindow::OnDamaged(const DamageArea& area)
834 void GlWindow::UpdateScreenRotation(int newAngle)
836 mScreenRotationAngle = newAngle;
837 mTotalRotationAngle = (mWindowRotationAngle + mScreenRotationAngle) % 360;
839 if(mTotalRotationAngle == 90 || mTotalRotationAngle == 270)
841 mWindowWidth = mPositionSize.height;
842 mWindowHeight = mPositionSize.width;
846 mWindowWidth = mPositionSize.width;
847 mWindowHeight = mPositionSize.height;
850 // Emit Resize signal
851 Dali::GlWindow handle(this);
852 mResizeSignal.Emit(Dali::Uint16Pair(mWindowWidth, mWindowHeight));
854 if(mGlWindowRenderThread)
856 DALI_LOG_RELEASE_INFO("GlWindow::UpdateScreenRotation(), RequestScreenRotatem(), mScreenRotationAngle: %d\n", mScreenRotationAngle);
857 mGlWindowRenderThread->RequestScreenRotate(mScreenRotationAngle);
861 } // namespace Adaptor
863 } // namespace Internal