optimize orientation switch logic

Signed-off-by: xpeng <pengxin33@huawei.com>
Change-Id: Ib1f1f5e5a76584c0f5784241366088f1ee741564
This commit is contained in:
xpeng
2022-07-05 19:23:52 +08:00
parent 7b41f80eda
commit 3a6e73f1f0
3 changed files with 58 additions and 44 deletions
@@ -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
+4 -9
View File
@@ -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);
+48 -31
View File
@@ -43,6 +43,7 @@ Rotation ScreenRotationController::rotationLockedRotation_;
uint32_t ScreenRotationController::defaultDeviceRotation_ = 0;
std::map<SensorRotation, DeviceRotation> ScreenRotationController::sensorToDeviceRotationMap_;
std::map<DeviceRotation, Rotation> 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<GravityData*>(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<DisplayInfo> 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> 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