From 3a6e73f1f0536cf2b7e329e40796832ead1da612 Mon Sep 17 00:00:00 2001 From: xpeng Date: Tue, 5 Jul 2022 19:23:52 +0800 Subject: [PATCH] optimize orientation switch logic Signed-off-by: xpeng Change-Id: Ib1f1f5e5a76584c0f5784241366088f1ee741564 --- dmserver/include/screen_rotation_controller.h | 10 ++- dmserver/src/abstract_screen_controller.cpp | 13 +-- dmserver/src/screen_rotation_controller.cpp | 79 +++++++++++-------- 3 files changed, 58 insertions(+), 44 deletions(-) diff --git a/dmserver/include/screen_rotation_controller.h b/dmserver/include/screen_rotation_controller.h index 1d9071e8..22e72c80 100644 --- a/dmserver/include/screen_rotation_controller.h +++ b/dmserver/include/screen_rotation_controller.h @@ -52,6 +52,7 @@ public: static void SetScreenRotationLocked(bool isLocked); static void SetDefaultDeviceRotationOffset(uint32_t defaultDeviceRotationOffset); static bool IsGravitySensorEnabled(); + static void ProcessOrientationSwitch(Orientation orientation); private: static void HandleGravitySensorEventCallback(SensorEvent *event); static bool CheckCallbackTimeInterval(); @@ -75,12 +76,12 @@ private: static bool IsSensorRelatedOrientation(Orientation orientation); static void ProcessRotationMapping(); - static bool ProcessOrientationSwitch(Orientation orientation, DeviceRotation deviceRotation); - static bool ProcessSwitchToAutoRotationPortrait(DeviceRotation rotation); - static bool ProcessSwitchToAutoRotationLandscape(DeviceRotation rotation); - static bool ProcessSwitchToAutoRotation(DeviceRotation rotation); + static void ProcessSwitchToAutoRotationPortrait(DeviceRotation rotation); + static void ProcessSwitchToAutoRotationLandscape(DeviceRotation rotation); + static void ProcessSwitchToAutoRotation(DeviceRotation rotation); static void ProcessSwitchToAutoRotationPortraitRestricted(); static void ProcessSwitchToAutoRotationLandscapeRestricted(); + static void ProcessSwitchToSensorRelatedOrientation(Orientation orientation, DeviceRotation deviceRotation); static void ProcessSwitchToSensorUnrelatedOrientation(Orientation orientation); static Rotation ProcessAutoRotationPortraitOrientation(DeviceRotation sensorRotationConveted); static Rotation ProcessAutoRotationLandscapeOrientation(DeviceRotation sensorRotationConveted); @@ -98,6 +99,7 @@ private: static Rotation rotationLockedRotation_; static bool isGravitySensorSubscribed_; static bool isScreenRotationLocked_; + static DeviceRotation lastSensorRotationConverted_; }; } // Rosen } // OHOS diff --git a/dmserver/src/abstract_screen_controller.cpp b/dmserver/src/abstract_screen_controller.cpp index 8283556f..b064ab6a 100644 --- a/dmserver/src/abstract_screen_controller.cpp +++ b/dmserver/src/abstract_screen_controller.cpp @@ -611,22 +611,17 @@ bool AbstractScreenController::SetOrientation(ScreenId screenId, Orientation new return false; } if (isFromWindow) { - if (newOrientation == Orientation::UNSPECIFIED) { - newOrientation = screen->screenRequestedOrientation_; - } + ScreenRotationController::ProcessOrientationSwitch(newOrientation); } else { screen->screenRequestedOrientation_ = newOrientation; + Rotation rotationAfter = screen->CalcRotation(newOrientation); + SetRotation(screenId, rotationAfter, false); + screen->rotation_ = rotationAfter; } if (screen->orientation_ == newOrientation) { WLOGI("skip setting orientation. screen %{public}" PRIu64" orientation %{public}u", screenId, newOrientation); return true; } - if ((newOrientation >= Orientation::VERTICAL && newOrientation <= Orientation::REVERSE_HORIZONTAL) || - (newOrientation == Orientation::UNSPECIFIED && !ScreenRotationController::IsGravitySensorEnabled())) { - Rotation rotationAfter = screen->CalcRotation(newOrientation); - SetRotation(screenId, rotationAfter, false); - screen->rotation_ = rotationAfter; - } if (!screen->SetOrientation(newOrientation)) { WLOGE("fail to set rotation, screen %{public}" PRIu64"", screenId); diff --git a/dmserver/src/screen_rotation_controller.cpp b/dmserver/src/screen_rotation_controller.cpp index 3e363c54..cb09fa43 100644 --- a/dmserver/src/screen_rotation_controller.cpp +++ b/dmserver/src/screen_rotation_controller.cpp @@ -43,6 +43,7 @@ Rotation ScreenRotationController::rotationLockedRotation_; uint32_t ScreenRotationController::defaultDeviceRotation_ = 0; std::map ScreenRotationController::sensorToDeviceRotationMap_; std::map ScreenRotationController::deviceToDisplayRotationMap_; +DeviceRotation ScreenRotationController::lastSensorRotationConverted_ = DeviceRotation::INVALID; void ScreenRotationController::SubscribeGravitySensor() { @@ -124,16 +125,13 @@ void ScreenRotationController::HandleGravitySensorEventCallback(SensorEvent *eve return; } Orientation orientation = GetPreferredOrientation(); - if (!IsSensorRelatedOrientation(orientation)) { - ProcessSwitchToSensorUnrelatedOrientation(orientation); - return; - } currentDisplayRotation_ = GetCurrentDisplayRotation(); GravityData* gravityData = reinterpret_cast(event->data); int sensorDegree = CalcRotationDegree(gravityData); DeviceRotation sensorRotationConverted = ConvertSensorToDeviceRotation(CalcSensorRotation(sensorDegree)); - if (!ProcessOrientationSwitch(orientation, sensorRotationConverted)) { + lastSensorRotationConverted_ = sensorRotationConverted; + if (!IsSensorRelatedOrientation(orientation)) { return; } if (sensorRotationConverted == DeviceRotation::INVALID) { @@ -163,14 +161,23 @@ int ScreenRotationController::CalcRotationDegree(GravityData* gravityData) Rotation ScreenRotationController::GetCurrentDisplayRotation() { - return DisplayManagerServiceInner::GetInstance().GetDisplayById(defaultDisplayId_)->GetRotation(); + sptr defaultDisplayInfo = DisplayManagerServiceInner::GetInstance().GetDefaultDisplay(); + if (defaultDisplayInfo == nullptr) { + WLOGFE("Cannot get default display info"); + return defaultDeviceRotation_ == 0 ? ConvertDeviceToDisplayRotation(DeviceRotation::ROTATION_PORTRAIT) : + ConvertDeviceToDisplayRotation(DeviceRotation::ROTATION_LANDSCAPE); + } + return defaultDisplayInfo->GetRotation(); } Orientation ScreenRotationController::GetPreferredOrientation() { - Orientation orientation = Orientation::UNSPECIFIED; - DisplayManagerServiceInner::GetInstance().GetWindowPreferredOrientation(defaultDisplayId_, orientation); - return orientation; + sptr screenInfo = DisplayManagerServiceInner::GetInstance().GetScreenInfoByDisplayId(defaultDisplayId_); + if (screenInfo == nullptr) { + WLOGFE("Cannot get default screen info"); + return Orientation::UNSPECIFIED; + } + return screenInfo->GetOrientation(); } Rotation ScreenRotationController::CalcTargetDisplayRotation( @@ -281,10 +288,11 @@ bool ScreenRotationController::IsSensorRelatedOrientation(Orientation orientatio return true; } -bool ScreenRotationController::ProcessOrientationSwitch(Orientation orientation, DeviceRotation sensorRotationConverted) +void ScreenRotationController::ProcessSwitchToSensorRelatedOrientation( + Orientation orientation, DeviceRotation sensorRotationConverted) { if (lastOrientationType_ == orientation) { - return true; + return; } lastOrientationType_ = orientation; switch (orientation) { @@ -292,70 +300,70 @@ bool ScreenRotationController::ProcessOrientationSwitch(Orientation orientation, case Orientation::AUTO_ROTATION_RESTRICTED: { if (isScreenRotationLocked_) { SetScreenRotation(rotationLockedRotation_); - return false; + return; } [[fallthrough]]; } case Orientation::SENSOR: { - return ProcessSwitchToAutoRotation(sensorRotationConverted); + ProcessSwitchToAutoRotation(sensorRotationConverted); + return; } case Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED: { if (isScreenRotationLocked_) { ProcessSwitchToAutoRotationPortraitRestricted(); - return false; + return; } [[fallthrough]]; } case Orientation::SENSOR_VERTICAL: { - return ProcessSwitchToAutoRotationPortrait(sensorRotationConverted); + ProcessSwitchToAutoRotationPortrait(sensorRotationConverted); + return; } case Orientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED: { if (isScreenRotationLocked_) { ProcessSwitchToAutoRotationLandscapeRestricted(); - return false; + return; } [[fallthrough]]; } case Orientation::SENSOR_HORIZONTAL: { - return ProcessSwitchToAutoRotationLandscape(sensorRotationConverted); + ProcessSwitchToAutoRotationLandscape(sensorRotationConverted); + return; } default: { - return true; + return; } } } -bool ScreenRotationController::ProcessSwitchToAutoRotation(DeviceRotation rotation) +void ScreenRotationController::ProcessSwitchToAutoRotation(DeviceRotation rotation) { if (rotation != DeviceRotation::INVALID) { - return true; + return; } SetScreenRotation(lastSensorDecidedRotation_); - return false; } -bool ScreenRotationController::ProcessSwitchToAutoRotationPortrait(DeviceRotation rotation) +void ScreenRotationController::ProcessSwitchToAutoRotationPortrait(DeviceRotation rotation) { if (IsCurrentDisplayVertical()) { - return true; + return; } if (IsDeviceRotationVertical(rotation)) { - return true; + return; } SetScreenRotation(ConvertDeviceToDisplayRotation(DeviceRotation::ROTATION_PORTRAIT)); - return false; } -bool ScreenRotationController::ProcessSwitchToAutoRotationLandscape(DeviceRotation rotation) +void ScreenRotationController::ProcessSwitchToAutoRotationLandscape(DeviceRotation rotation) { if (IsCurrentDisplayHorizontal()) { - return true; + return; } if (IsDeviceRotationHorizontal(rotation)) { - return true; + return; } SetScreenRotation(ConvertDeviceToDisplayRotation(DeviceRotation::ROTATION_LANDSCAPE)); - return false; } void ScreenRotationController::ProcessSwitchToAutoRotationPortraitRestricted() @@ -454,12 +462,12 @@ bool ScreenRotationController::IsDeviceRotationHorizontal(DeviceRotation deviceR bool ScreenRotationController::IsCurrentDisplayVertical() { - return IsDisplayRotationVertical(currentDisplayRotation_); + return IsDisplayRotationVertical(GetCurrentDisplayRotation()); } bool ScreenRotationController::IsCurrentDisplayHorizontal() { - return IsDisplayRotationHorizontal(currentDisplayRotation_); + return IsDisplayRotationHorizontal(GetCurrentDisplayRotation()); } bool ScreenRotationController::IsDisplayRotationVertical(Rotation rotation) @@ -507,5 +515,14 @@ void ScreenRotationController::ProcessSwitchToSensorUnrelatedOrientation(Orienta } } } + +void ScreenRotationController::ProcessOrientationSwitch(Orientation orientation) +{ + if (!IsSensorRelatedOrientation(orientation)) { + ProcessSwitchToSensorUnrelatedOrientation(orientation); + } else { + ProcessSwitchToSensorRelatedOrientation(orientation, lastSensorRotationConverted_); + } +} } // Rosen } // OHOS \ No newline at end of file