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 mIsWindowRotated(false),
80 mIsEGLInitialized(false),
85 mColorDepth(COLOR_DEPTH_24),
86 mRenderingMode(Dali::GlWindow::RenderingMode::CONTINUOUS),
88 mTotalRotationAngle(0),
89 mWindowRotationAngle(0),
90 mScreenRotationAngle(0),
100 mVisibilityChangedSignal()
104 GlWindow::~GlWindow()
108 mEventHandler->RemoveObserver(*this);
111 if(mGlWindowRenderThread)
113 mGlWindowRenderThread->Stop();
114 mGlWindowRenderThread->Join();
117 if(mIsEGLInitialized)
119 mGraphics->Destroy();
123 void GlWindow::Initialize(const PositionSize& positionSize, const std::string& name, const std::string& className)
125 int screenWidth, screenHeight;
127 mPositionSize = positionSize;
128 WindowSystem::GetScreenSize(screenWidth, screenHeight);
129 if((mPositionSize.width == 0) || (mPositionSize.height == 0))
133 mPositionSize.width = screenWidth;
134 mPositionSize.height = screenHeight;
137 if(screenWidth > screenHeight)
139 mOrientationMode = 1; // Default mode is landscape
143 mOrientationMode = 0; // Default mode is portrate
146 // Create a window base
147 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
149 mWindowBase = windowFactory->CreateWindowBase(mPositionSize, surface, (mIsTransparent ? true : false));
150 mWindowBase->IconifyChangedSignal().Connect(this, &GlWindow::OnIconifyChanged);
151 mWindowBase->FocusChangedSignal().Connect(this, &GlWindow::OnFocusChanged);
153 if(Dali::Adaptor::IsAvailable())
158 if(!mPositionSize.IsEmpty())
160 AddAuxiliaryHint("wm.policy.win.user.geometry", "1");
161 mResizeEnabled = true;
168 mColorDepth = COLOR_DEPTH_32;
172 mColorDepth = COLOR_DEPTH_24;
175 SetClass(name, className);
178 mNativeWindowId = mWindowBase->GetNativeWindowId();
181 void GlWindow::SetEventHandler()
183 mEventHandler = EventHandlerPtr(new EventHandler(mWindowBase.get(), *this));
184 mEventHandler->AddObserver(*this);
187 void GlWindow::SetClass(const std::string& name, const std::string className)
190 mClassName = className;
191 mWindowBase->SetClass(name, className);
194 void GlWindow::SetEglConfig(bool depth, bool stencil, int msaa, Dali::GlWindow::GlesVersion version)
201 InitializeGraphics();
205 if(version == Dali::GlWindow::GlesVersion::VERSION_2_0)
209 else if(version == Dali::GlWindow::GlesVersion::VERSION_3_0)
214 mGlWindowRenderThread->SetEglConfig(depth, stencil, msaa, rVersion);
217 void GlWindow::Raise()
219 mWindowBase->Raise();
220 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Raise() \n", this, mNativeWindowId);
223 void GlWindow::Lower()
225 mWindowBase->Lower();
226 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Lower() \n", this, mNativeWindowId);
229 void GlWindow::Activate()
231 mWindowBase->Activate();
232 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Activate() \n", this, mNativeWindowId);
235 void GlWindow::Show()
243 Dali::GlWindow handle(this);
244 mVisibilityChangedSignal.Emit(handle, true);
249 mEventHandler->Resume();
252 if(mGlWindowRenderThread)
254 mGlWindowRenderThread->Resume();
257 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Show(): iconified = %d, visible = %d\n", this, mNativeWindowId, mIconified, mVisible);
260 void GlWindow::Hide()
268 Dali::GlWindow handle(this);
269 mVisibilityChangedSignal.Emit(handle, false);
274 mEventHandler->Pause();
277 if(mGlWindowRenderThread)
279 mGlWindowRenderThread->Pause();
282 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Hide(): iconified = %d, visible = %d\n", this, mNativeWindowId, mIconified, mVisible);
285 unsigned int GlWindow::GetSupportedAuxiliaryHintCount() const
287 return mWindowBase->GetSupportedAuxiliaryHintCount();
290 std::string GlWindow::GetSupportedAuxiliaryHint(unsigned int index) const
292 return mWindowBase->GetSupportedAuxiliaryHint(index);
295 unsigned int GlWindow::AddAuxiliaryHint(const std::string& hint, const std::string& value)
297 return mWindowBase->AddAuxiliaryHint(hint, value);
300 bool GlWindow::RemoveAuxiliaryHint(unsigned int id)
302 return mWindowBase->RemoveAuxiliaryHint(id);
305 bool GlWindow::SetAuxiliaryHintValue(unsigned int id, const std::string& value)
307 return mWindowBase->SetAuxiliaryHintValue(id, value);
310 std::string GlWindow::GetAuxiliaryHintValue(unsigned int id) const
312 return mWindowBase->GetAuxiliaryHintValue(id);
315 unsigned int GlWindow::GetAuxiliaryHintId(const std::string& hint) const
317 return mWindowBase->GetAuxiliaryHintId(hint);
320 void GlWindow::SetInputRegion(const Rect<int>& inputRegion)
322 mWindowBase->SetInputRegion(inputRegion);
324 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);
327 void GlWindow::SetOpaqueState(bool opaque)
329 mOpaqueState = opaque;
331 mWindowBase->SetOpaqueState(opaque);
333 DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "GlWindow::SetOpaqueState: opaque = %d\n", opaque);
336 bool GlWindow::IsOpaqueState() const
341 void GlWindow::SetPositionSize(PositionSize positionSize)
345 AddAuxiliaryHint("wm.policy.win.user.geometry", "1");
346 mResizeEnabled = true;
349 bool needToMove = false;
350 bool needToResize = false;
353 if((fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
354 (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE))
360 if((fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
361 (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE))
370 mWindowBase->MoveResize(positionSize);
374 mWindowBase->Resize(positionSize);
376 mPositionSize = positionSize;
382 mWindowBase->Move(positionSize);
383 mPositionSize = positionSize;
387 // If window's size or position is changed, the signal will be emitted to user.
388 if((needToMove) || (needToResize))
390 Uint16Pair newSize(mPositionSize.width, mPositionSize.height);
391 Dali::GlWindow handle(this);
392 mResizeSignal.Emit(newSize);
396 PositionSize GlWindow::GetPositionSize() const
398 PositionSize positionSize(mPositionSize);
399 if(mTotalRotationAngle == 90 || mTotalRotationAngle == 270)
401 positionSize.height = mPositionSize.width;
402 positionSize.width = mPositionSize.height;
408 void GlWindow::OnIconifyChanged(bool iconified)
416 Dali::GlWindow handle(this);
417 mVisibilityChangedSignal.Emit(handle, false);
422 mEventHandler->Pause();
425 if(mGlWindowRenderThread)
427 mGlWindowRenderThread->Pause();
430 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Iconified: visible = %d\n", this, mNativeWindowId, mVisible);
438 Dali::GlWindow handle(this);
439 mVisibilityChangedSignal.Emit(handle, true);
444 mEventHandler->Resume();
447 if(mGlWindowRenderThread)
449 mGlWindowRenderThread->Resume();
452 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Deiconified: visible = %d\n", this, mNativeWindowId, mVisible);
456 void GlWindow::OnFocusChanged(bool focusIn)
458 Dali::GlWindow handle(this);
459 mFocusChangeSignal.Emit(handle, focusIn);
462 void GlWindow::OnOutputTransformed()
464 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
465 if(screenRotationAngle != mScreenRotationAngle)
467 mScreenRotationAngle = screenRotationAngle;
468 mTotalRotationAngle = (mWindowRotationAngle + mScreenRotationAngle) % 360;
470 if(mTotalRotationAngle == 90 || mTotalRotationAngle == 270)
472 mWindowWidth = mPositionSize.height;
473 mWindowHeight = mPositionSize.width;
477 mWindowWidth = mPositionSize.width;
478 mWindowHeight = mPositionSize.height;
481 // Emit Resize signal
482 Dali::GlWindow handle(this);
483 mResizeSignal.Emit(Dali::Uint16Pair(mWindowWidth, mWindowHeight));
487 void GlWindow::OnTouchPoint(Dali::Integration::Point& point, int timeStamp)
489 PointState::Type state = point.GetState();
491 if(state == PointState::DOWN)
496 if(state == PointState::UP)
501 if(!mIsTouched && state == PointState::MOTION)
506 RecalculateTouchPosition(point);
507 Dali::TouchEvent touchEvent = Dali::Integration::NewTouchEvent(timeStamp, point);
508 Dali::GlWindow handle(this);
509 mTouchedSignal.Emit(touchEvent);
512 void GlWindow::OnWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
515 //FeedWheelEvent( wheelEvent );
518 void GlWindow::OnKeyEvent(Dali::Integration::KeyEvent& keyEvent)
520 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);
521 Dali::GlWindow handle(this);
522 mKeyEventSignal.Emit(event);
525 void GlWindow::OnRotation(const RotationEvent& rotation)
527 mWindowRotationAngle = rotation.angle;
528 mTotalRotationAngle = (mWindowRotationAngle + mScreenRotationAngle) % 360;
529 if(mTotalRotationAngle == 90 || mTotalRotationAngle == 270)
531 mWindowWidth = mPositionSize.height;
532 mWindowHeight = mPositionSize.width;
536 mWindowWidth = mPositionSize.width;
537 mWindowHeight = mPositionSize.height;
541 mIsWindowRotated = true;
542 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), OnRotation(): resize signal emit [%d x %d]\n", this, mNativeWindowId, mWindowWidth, mWindowHeight);
544 // Emit Resize signal
545 Dali::GlWindow handle(this);
546 mResizeSignal.Emit(Dali::Uint16Pair(mWindowWidth, mWindowHeight));
549 void GlWindow::RecalculateTouchPosition(Integration::Point& point)
551 Vector2 position = point.GetScreenPosition();
552 Vector2 convertedPosition;
554 switch(mTotalRotationAngle)
558 convertedPosition.x = static_cast<float>(mWindowWidth) - position.y;
559 convertedPosition.y = position.x;
564 convertedPosition.x = static_cast<float>(mWindowWidth) - position.x;
565 convertedPosition.y = static_cast<float>(mWindowHeight) - position.y;
570 convertedPosition.x = position.y;
571 convertedPosition.y = static_cast<float>(mWindowHeight) - position.x;
576 convertedPosition = position;
581 point.SetScreenPosition(convertedPosition);
584 void GlWindow::SetAvailableAnlges(const std::vector<int>& angles)
586 if(angles.size() > 4)
588 DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window::SetAvailableAnlges: Invalid vector size! [%d]\n", angles.size());
592 mWindowBase->SetAvailableAnlges(angles);
595 bool GlWindow::IsOrientationAvailable(WindowOrientation orientation) const
597 if(orientation <= WindowOrientation::NO_ORIENTATION_PREFERENCE || orientation > WindowOrientation::LANDSCAPE_INVERSE)
599 DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window::IsOrientationAvailable: Invalid input orientation [%d]\n", orientation);
605 int GlWindow::ConvertToAngle(WindowOrientation orientation)
607 int convertAngle = 0;
608 if(mOrientationMode == 0)
610 convertAngle = static_cast<int>(orientation);
612 else if(mOrientationMode == 1)
616 case WindowOrientation::LANDSCAPE:
621 case WindowOrientation::PORTRAIT:
626 case WindowOrientation::LANDSCAPE_INVERSE:
631 case WindowOrientation::PORTRAIT_INVERSE:
636 case WindowOrientation::NO_ORIENTATION_PREFERENCE:
646 WindowOrientation GlWindow::ConvertToOrientation(int angle) const
648 WindowOrientation orientation = WindowOrientation::NO_ORIENTATION_PREFERENCE;
649 if(mOrientationMode == 0) // Portrate mode
651 orientation = static_cast<WindowOrientation>(angle);
653 else if(mOrientationMode == 1) // Landscape mode
659 orientation = WindowOrientation::LANDSCAPE;
664 orientation = WindowOrientation::PORTRAIT;
669 orientation = WindowOrientation::LANDSCAPE_INVERSE;
674 orientation = WindowOrientation::PORTRAIT_INVERSE;
679 orientation = WindowOrientation::NO_ORIENTATION_PREFERENCE;
687 WindowOrientation GlWindow::GetCurrentOrientation() const
689 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), GetCurrentOrientation(): %d\n", this, mNativeWindowId, mTotalRotationAngle);
690 return ConvertToOrientation(mTotalRotationAngle);
693 void GlWindow::SetAvailableOrientations(const Dali::Vector<WindowOrientation>& orientations)
695 Dali::Vector<float>::SizeType count = orientations.Count();
696 for(Dali::Vector<float>::SizeType index = 0; index < count; ++index)
698 if(IsOrientationAvailable(orientations[index]) == false)
700 DALI_LOG_ERROR("Window::SetAvailableRotationAngles, invalid angle: %d\n", orientations[index]);
705 int angle = ConvertToAngle(orientations[index]);
707 for(std::size_t i = 0; i < mAvailableAngles.size(); i++)
709 if(mAvailableAngles[i] == angle)
718 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), SetAvailableOrientations: %d\n", this, mNativeWindowId, angle);
719 mAvailableAngles.push_back(angle);
722 SetAvailableAnlges(mAvailableAngles);
725 void GlWindow::SetPreferredOrientation(WindowOrientation orientation)
727 if(IsOrientationAvailable(orientation) == false)
729 DALI_LOG_ERROR("Window::SetPreferredOrientation, invalid orientation: %d\n", orientation);
732 mPreferredAngle = ConvertToAngle(orientation);
733 DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), SetPreferredOrientation: %d\n", this, mNativeWindowId, mPreferredAngle);
734 mWindowBase->SetPreferredAngle(mPreferredAngle);
737 void GlWindow::SetChild(Dali::Window& child)
739 if(DALI_UNLIKELY(child))
741 mChildWindow = child;
742 Internal::Adaptor::Window& windowImpl = Dali::GetImplementation(mChildWindow);
743 WindowRenderSurface* renderSurface = static_cast<WindowRenderSurface*>(windowImpl.GetSurface());
746 WindowBase* childWindowBase = renderSurface->GetWindowBase();
749 childWindowBase->SetParent(mWindowBase.get());
755 void GlWindow::RegisterGlCallback(CallbackBase* initCallback, CallbackBase* renderFrameCallback, CallbackBase* terminateCallback)
757 if(mIsEGLInitialized == false)
759 InitializeGraphics();
761 mGlWindowRenderThread->RegisterGlCallback(initCallback, renderFrameCallback, terminateCallback);
762 mGlWindowRenderThread->Start();
765 void GlWindow::RenderOnce()
767 if(mGlWindowRenderThread)
769 mGlWindowRenderThread->RenderOnce();
773 void GlWindow::SetRenderingMode(Dali::GlWindow::RenderingMode mode)
775 mRenderingMode = mode;
776 if(mGlWindowRenderThread)
778 bool onDemand = false;
779 if(mRenderingMode == Dali::GlWindow::RenderingMode::ON_DEMAND)
783 mGlWindowRenderThread->SetOnDemandRenderMode(onDemand);
787 Dali::GlWindow::RenderingMode GlWindow::GetRenderingMode() const
789 return mRenderingMode;
792 void GlWindow::InitializeGraphics()
794 if(!mIsEGLInitialized)
797 std::unique_ptr<GraphicsFactory> graphicsFactoryPtr = Utils::MakeUnique<GraphicsFactory>(mEnvironmentOptions);
798 auto graphicsFactory = *graphicsFactoryPtr.get();
800 mGraphics = std::unique_ptr<GraphicsInterface>(&graphicsFactory.Create());
801 GraphicsInterface* graphics = mGraphics.get();
802 EglGraphics* eglGraphics = static_cast<EglGraphics*>(graphics);
803 eglGraphics->Initialize(mDepth, mStencil, false, mMSAA);
805 mDisplayConnection = std::unique_ptr<Dali::DisplayConnection>(Dali::DisplayConnection::New(*graphics, Dali::RenderSurfaceInterface::Type::WINDOW_RENDER_SURFACE));
806 mDisplayConnection->Initialize();
808 // Create Render Thread
809 mGlWindowRenderThread = std::unique_ptr<Dali::Internal::Adaptor::GlWindowRenderThread>(new GlWindowRenderThread(mPositionSize, mColorDepth));
810 if(!mGlWindowRenderThread)
812 DALI_LOG_ERROR("Fail to create GlWindow Render Thread!!!!\n");
816 mGlWindowRenderThread->SetGraphicsInterface(graphics);
817 mGlWindowRenderThread->SetWindowBase(mWindowBase.get());
818 bool onDemand = false;
819 if(mRenderingMode == Dali::GlWindow::RenderingMode::ON_DEMAND)
823 mGlWindowRenderThread->SetOnDemandRenderMode(onDemand);
825 mIsEGLInitialized = true;
829 void GlWindow::OnDamaged(const DamageArea& area)
833 } // namespace Adaptor
835 } // namespace Internal