From 25fd34343917ee2f34adc9b5adc7a561f9c6b222 Mon Sep 17 00:00:00 2001 From: zblue Date: Mon, 1 Aug 2022 22:51:19 +0800 Subject: [PATCH 1/4] feat: decrease max screen brightness Signed-off-by: zblue Change-Id: I781ea54a855d38447ae807f3500bc9e135bed46a --- .../native/display_power_mgr_client.cpp | 10 +- .../native/include/display_power_mgr_client.h | 1 + .../native/include/idisplay_power_mgr.h | 2 + service/BUILD.gn | 2 + .../include/display_accessibility_observer.h | 39 +++ .../include/display_power_mgr_service.h | 4 + .../native/include/display_system_ability.h | 2 + service/native/include/screen_controller.h | 7 +- .../src/display_accessibility_observer.cpp | 59 ++++ .../native/src/display_power_mgr_service.cpp | 28 ++ service/native/src/display_system_ability.cpp | 25 ++ service/native/src/screen_controller.cpp | 51 ++- .../zidl/include/display_power_mgr_proxy.h | 1 + service/zidl/include/display_power_mgr_stub.h | 1 + service/zidl/src/display_power_mgr_proxy.cpp | 34 ++ service/zidl/src/display_power_mgr_stub.cpp | 19 ++ .../src/display_power_mgr_brightness_test.cpp | 309 ++++++++++++++++++ 17 files changed, 580 insertions(+), 14 deletions(-) create mode 100644 service/native/include/display_accessibility_observer.h create mode 100644 service/native/src/display_accessibility_observer.cpp diff --git a/frameworks/native/display_power_mgr_client.cpp b/frameworks/native/display_power_mgr_client.cpp index 973f940..fd57b1d 100644 --- a/frameworks/native/display_power_mgr_client.cpp +++ b/frameworks/native/display_power_mgr_client.cpp @@ -126,6 +126,15 @@ bool DisplayPowerMgrClient::SetBrightness(uint32_t value, uint32_t displayId) return proxy->SetBrightness(value, displayId); } +bool DisplayPowerMgrClient::DiscountBrightness(double discount, uint32_t displayId) +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + return false; + } + return proxy->DiscountBrightness(discount, displayId); +} + bool DisplayPowerMgrClient::OverrideBrightness(uint32_t value, uint32_t displayId) { auto proxy = GetProxy(); @@ -180,7 +189,6 @@ uint32_t DisplayPowerMgrClient::GetMinBrightness() return proxy->GetMinBrightness(); } - bool DisplayPowerMgrClient::AdjustBrightness(uint32_t value, uint32_t duration, uint32_t id) { auto proxy = GetProxy(); diff --git a/interfaces/innerkits/native/include/display_power_mgr_client.h b/interfaces/innerkits/native/include/display_power_mgr_client.h index 087dbe0..aecb575 100644 --- a/interfaces/innerkits/native/include/display_power_mgr_client.h +++ b/interfaces/innerkits/native/include/display_power_mgr_client.h @@ -38,6 +38,7 @@ public: std::vector GetDisplayIds(); int32_t GetMainDisplayId(); bool SetBrightness(uint32_t value, uint32_t displayId = 0); + bool DiscountBrightness(double discount, uint32_t displayId = 0); bool OverrideBrightness(uint32_t value, uint32_t displayId = 0); bool RestoreBrightness(uint32_t displayId = 0); uint32_t GetBrightness(uint32_t displayId = 0); diff --git a/interfaces/innerkits/native/include/idisplay_power_mgr.h b/interfaces/innerkits/native/include/idisplay_power_mgr.h index f7cda6d..ff4304d 100644 --- a/interfaces/innerkits/native/include/idisplay_power_mgr.h +++ b/interfaces/innerkits/native/include/idisplay_power_mgr.h @@ -32,6 +32,7 @@ public: GET_DISPLAY_IDS, GET_MAIN_DISPLAY_ID, SET_BRIGHTNESS, + DISCOUNT_BRIGHTNESS, OVERRIDE_BRIGHTNESS, RESTORE_BRIGHTNESS, GET_BRIGHTNESS, @@ -51,6 +52,7 @@ public: virtual std::vector GetDisplayIds() = 0; virtual uint32_t GetMainDisplayId() = 0; virtual bool SetBrightness(uint32_t value, uint32_t displayId) = 0; + virtual bool DiscountBrightness(double discount, uint32_t displayId) = 0; virtual bool OverrideBrightness(uint32_t value, uint32_t displayId) = 0; virtual bool RestoreBrightness(uint32_t displayId) = 0; virtual uint32_t GetBrightness(uint32_t displayId) = 0; diff --git a/service/BUILD.gn b/service/BUILD.gn index 81e67ef..352273c 100644 --- a/service/BUILD.gn +++ b/service/BUILD.gn @@ -31,6 +31,7 @@ config("displaymgr_public_config") { ohos_shared_library("displaymgrservice") { sources = [ + "native/src/display_accessibility_observer.cpp", "native/src/display_event_handler.cpp", "native/src/display_param_helper.cpp", "native/src/display_power_mgr_service.cpp", @@ -60,6 +61,7 @@ ohos_shared_library("displaymgrservice") { "ability_base:want", "ability_base:zuri", "ability_runtime:ability_manager", + "accessibility:accessibilityconfig", "bundle_framework:appexecfwk_base", "c_utils:utils", "eventhandler:libeventhandler", diff --git a/service/native/include/display_accessibility_observer.h b/service/native/include/display_accessibility_observer.h new file mode 100644 index 0000000..20faa54 --- /dev/null +++ b/service/native/include/display_accessibility_observer.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISPLAYMGR_DISPLAY_ACCESSIBILITY_OBSERVER_H +#define DISPLAYMGR_DISPLAY_ACCESSIBILITY_OBSERVER_H + +#include "accessibility_config.h" + +namespace OHOS { +namespace DisplayPowerMgr { +using namespace OHOS::AccessibilityConfig; + +class DisplayAccessibilityObserver : public AccessibilityConfigObserver { +public: + DisplayAccessibilityObserver() = default; + ~DisplayAccessibilityObserver() override = default; + void OnConfigChanged(CONFIG_ID id, const ConfigValue& value) override; + + void RegisterObserver(); + void UnregisterObserver(); + +private: + std::shared_ptr observer_ = nullptr; +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif //DISPLAYMGR_DISPLAY_ACCESSIBILITY_OBSERVER_H diff --git a/service/native/include/display_power_mgr_service.h b/service/native/include/display_power_mgr_service.h index 2be435c..4f57045 100644 --- a/service/native/include/display_power_mgr_service.h +++ b/service/native/include/display_power_mgr_service.h @@ -45,6 +45,7 @@ public: virtual std::vector GetDisplayIds() override; virtual uint32_t GetMainDisplayId() override; virtual bool SetBrightness(uint32_t value, uint32_t displayId) override; + virtual bool DiscountBrightness(double discount, uint32_t displayId) override; virtual bool OverrideBrightness(uint32_t value, uint32_t displayId) override; virtual bool RestoreBrightness(uint32_t displayId) override; virtual uint32_t GetBrightness(uint32_t displayId) override; @@ -83,6 +84,8 @@ private: static const uint32_t BRIGHTNESS_MIN; static const uint32_t BRIGHTNESS_DEFAULT; static const uint32_t BRIGHTNESS_MAX; + static constexpr const double DISCOUNT_MIN = 0.01; + static constexpr const double DISCOUNT_MAX = 1.00; static void AmbientLightCallback(SensorEvent *event); friend DelayedSpSingleton; @@ -91,6 +94,7 @@ private: void InitSensors(); bool IsChangedLux(float scalar); static uint32_t GetSafeBrightness(uint32_t value); + static double GetSafeDiscount(double discount); bool CalculateBrightness(float scalar, int32_t& brightness); int32_t GetBrightnessFromLightScalar(float scalar); void ActivateAmbientSensor(); diff --git a/service/native/include/display_system_ability.h b/service/native/include/display_system_ability.h index ca90799..153b482 100644 --- a/service/native/include/display_system_ability.h +++ b/service/native/include/display_system_ability.h @@ -34,6 +34,8 @@ public: void OnStart() override; void OnStop() override; + void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; private: sptr service_; diff --git a/service/native/include/screen_controller.h b/service/native/include/screen_controller.h index 53207f4..48eed37 100644 --- a/service/native/include/screen_controller.h +++ b/service/native/include/screen_controller.h @@ -52,6 +52,7 @@ public: bool SetBrightness(uint32_t value, uint32_t gradualDuration = 0); uint32_t GetBrightness(); + bool DiscountBrightness(double discount, uint32_t gradualDuration = 0); bool OverrideBrightness(uint32_t value, uint32_t gradualDuration = 0); bool RestoreBrightness(uint32_t gradualDuration = 0); bool IsBrightnessOverridden() const; @@ -63,14 +64,17 @@ public: uint32_t GetSettingBrightness(const std::string& key = SETTING_BRIGHTNESS_KEY) const; void RegisterSettingBrightnessObserver(); void UnregisterSettingBrightnessObserver(); + double GetDiscount() const; private: void OnStateChanged(DisplayState state); bool CanSetBrightness(); + bool CanDiscountBrightness(); bool CanOverrideBrightness(); bool CanBoostBrightness(); bool UpdateBrightness(uint32_t value, uint32_t gradualDuration = 0); + uint32_t GetOriginalBrightness(); void SetSettingBrightness(); uint32_t GetScreenOnBrightness() const; void BrightnessSettingUpdateFunc(const std::string& key); @@ -79,10 +83,11 @@ private: DisplayState state_; std::mutex mutexState_; uint32_t stateChangeReason_ {0}; + double discount_ {1.0}; std::atomic isBrightnessOverridden_ {false}; std::atomic isBrightnessBoosted_ {false}; - uint32_t cachedBrightness_ {102}; + uint32_t cachedSettingBrightness_ {102}; uint32_t overriddenBrightness_ {102}; std::shared_ptr action_ {nullptr}; std::shared_ptr animator_; diff --git a/service/native/src/display_accessibility_observer.cpp b/service/native/src/display_accessibility_observer.cpp new file mode 100644 index 0000000..7fa19fe --- /dev/null +++ b/service/native/src/display_accessibility_observer.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "display_accessibility_observer.h" + +#include "display_power_mgr_service.h" +#include "display_log.h" + +namespace OHOS { +namespace DisplayPowerMgr { +void DisplayAccessibilityObserver::OnConfigChanged(CONFIG_ID id, const ConfigValue& value) +{ + if (id != CONFIG_BRIGHTNESS_DISCOUNT) { + return; + } + float discount = value.brightnessDiscount; + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Brightness discount config changed, discount=%{public}f", discount); + + auto service = DelayedSpSingleton::GetInstance(); + auto displayIds = service->GetDisplayIds(); + for (const auto& displayId: displayIds) { + service->DiscountBrightness(static_cast(discount), displayId); + } + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Brightness discount config changed finish"); +} + +void DisplayAccessibilityObserver::RegisterObserver() +{ + if (!observer_) { + observer_ = std::make_shared(); + } + auto& config = Singleton::GetInstance(); + config.SubscribeConfigObserver(OHOS::AccessibilityConfig::CONFIG_BRIGHTNESS_DISCOUNT, observer_); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Brightness discount accessibility observer registered"); +} + +void DisplayAccessibilityObserver::UnregisterObserver() +{ + if (!observer_) { + return; + } + auto& config = Singleton::GetInstance(); + config.UnsubscribeConfigObserver(OHOS::AccessibilityConfig::CONFIG_BRIGHTNESS_DISCOUNT, observer_); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Brightness discount accessibility observer unregistered"); +} +} // namespace DisplayPowerMgr +} // namespace OHOS \ No newline at end of file diff --git a/service/native/src/display_power_mgr_service.cpp b/service/native/src/display_power_mgr_service.cpp index 327242f..730a018 100644 --- a/service/native/src/display_power_mgr_service.cpp +++ b/service/native/src/display_power_mgr_service.cpp @@ -136,6 +136,18 @@ bool DisplayPowerMgrService::SetBrightness(uint32_t value, uint32_t displayId) return iter->second->SetBrightness(brightness); } +bool DisplayPowerMgrService::DiscountBrightness(double discount, uint32_t displayId) +{ + auto safeDiscount = GetSafeDiscount(discount); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "DiscountBrightness displayId=%{public}u, discount-%{public}lf", + displayId, safeDiscount); + auto iter = controllerMap_.find(displayId); + if (iter == controllerMap_.end()) { + return false; + } + return iter->second->DiscountBrightness(safeDiscount); +} + bool DisplayPowerMgrService::OverrideBrightness(uint32_t value, uint32_t displayId) { auto brightness = GetSafeBrightness(value); @@ -316,6 +328,8 @@ int32_t DisplayPowerMgrService::Dump(int32_t fd, const std::vector g_observer = nullptr; } void DisplaySystemAbility::OnStart() { DISPLAY_HILOGI(COMP_SVC, "Start service"); auto service = DelayedSpSingleton::GetInstance(); service->Init(); + AddSystemAbilityListener(ACCESSIBILITY_MANAGER_SERVICE_ID); if (!Publish(service)) { DISPLAY_HILOGE(COMP_SVC, "Failed to publish service"); } @@ -38,6 +41,28 @@ void DisplaySystemAbility::OnStart() void DisplaySystemAbility::OnStop() { DISPLAY_HILOGW(COMP_SVC, "Stop service"); + RemoveSystemAbilityListener(ACCESSIBILITY_MANAGER_SERVICE_ID); +} + +void DisplaySystemAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) +{ + if (systemAbilityId == ACCESSIBILITY_MANAGER_SERVICE_ID) { + DISPLAY_HILOGI(COMP_SVC, "Accessibility service added, deviceId=%{private}s", deviceId.c_str()); + if (!g_observer) { + g_observer = std::make_shared(); + } + g_observer->RegisterObserver(); + } +} + +void DisplaySystemAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) +{ + if (systemAbilityId == ACCESSIBILITY_MANAGER_SERVICE_ID) { + DISPLAY_HILOGI(COMP_SVC, "Accessibility service removed, deviceId=%{private}s", deviceId.c_str()); + if (!g_observer) { + g_observer->UnregisterObserver(); + } + } } } // OHOS } // DisplayPowerMgr \ No newline at end of file diff --git a/service/native/src/screen_controller.cpp b/service/native/src/screen_controller.cpp index 10f0066..698ea6e 100644 --- a/service/native/src/screen_controller.cpp +++ b/service/native/src/screen_controller.cpp @@ -130,7 +130,7 @@ bool ScreenController::SetBrightness(uint32_t value, uint32_t gradualDuration) return false; } DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Set brightness, value=%{public}u", value); - cachedBrightness_ = action_->GetBrightness(); + cachedSettingBrightness_ = GetOriginalBrightness(); bool ret = UpdateBrightness(value, gradualDuration); if (ret) { handler_->SendEvent(DisplayEventHandler::Event::EVENT_SET_SETTING_BRIGHTNESS); @@ -143,6 +143,17 @@ uint32_t ScreenController::GetBrightness() return action_->GetBrightness(); } +bool ScreenController::DiscountBrightness(double discount, uint32_t gradualDuration) +{ + if (!CanDiscountBrightness()) { + DISPLAY_HILOGW(FEAT_BRIGHTNESS, "Cannot discount brightness, ignore the change"); + return false; + } + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Discount brightness, discount=%{public}lf", discount); + discount_ = discount; + return UpdateBrightness(GetBrightness(), gradualDuration); +} + bool ScreenController::OverrideBrightness(uint32_t value, uint32_t gradualDuration) { if (!CanOverrideBrightness()) { @@ -152,7 +163,7 @@ bool ScreenController::OverrideBrightness(uint32_t value, uint32_t gradualDurati DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Override brightness, value=%{public}u", value); if (!isBrightnessOverridden_) { isBrightnessOverridden_ = true; - cachedBrightness_ = action_->GetBrightness(); + cachedSettingBrightness_ = GetOriginalBrightness(); } overriddenBrightness_ = value; return UpdateBrightness(value, gradualDuration); @@ -185,7 +196,7 @@ bool ScreenController::BoostBrightness(uint32_t timeoutMs, uint32_t gradualDurat uint32_t maxBrightness = DisplayParamHelper::GetInstance().GetMaxBrightness(); DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Boost brightness, maxBrightness: %{public}d", maxBrightness); isBrightnessBoosted_ = true; - cachedBrightness_ = action_->GetBrightness(); + cachedSettingBrightness_ = GetOriginalBrightness(); ret = UpdateBrightness(maxBrightness, gradualDuration); } @@ -239,6 +250,11 @@ bool ScreenController::CanSetBrightness() return IsScreenOn() && !IsBrightnessOverridden() && !IsBrightnessBoosted(); } +bool ScreenController::CanDiscountBrightness() +{ + return IsScreenOn(); +} + bool ScreenController::CanOverrideBrightness() { return IsScreenOn() && !IsBrightnessBoosted(); @@ -251,20 +267,21 @@ bool ScreenController::CanBoostBrightness() bool ScreenController::UpdateBrightness(uint32_t value, uint32_t gradualDuration) { - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Update brightness, value=%{public}u, duration=%{public}u", - value, gradualDuration); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Update brightness, value=%{public}u, discount=%{public}lf,"\ + " duration=%{public}u", value, discount_, gradualDuration); + auto brightness = static_cast(value * discount_); if (animator_->IsAnimating()) { animator_->StopAnimation(); } if (gradualDuration > 0) { DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Update brightness gradually"); - animator_->StartAnimation(action_->GetBrightness(), value, gradualDuration); + animator_->StartAnimation(GetBrightness(), brightness, gradualDuration); return true; } - bool isSucc = action_->SetBrightness(value); + bool isSucc = action_->SetBrightness(brightness); DISPLAY_HILOGD(FEAT_BRIGHTNESS, "Updated brightness is %{public}s, brightness: %{public}u", - isSucc ? "succ" : "failed", value); + isSucc ? "succ" : "failed", brightness); return isSucc; } @@ -275,15 +292,15 @@ uint32_t ScreenController::GetSettingBrightness(const std::string& key) const ErrCode ret = helper.GetIntValue(key, value); if (ret != ERR_OK) { DISPLAY_HILOGW(FEAT_BRIGHTNESS, "get setting brightness failed, return cachedBrightness_=%{public}u,"\ - " key=%{public}s, ret=%{public}d", cachedBrightness_, key.c_str(), ret); - return cachedBrightness_; + " key=%{public}s, ret=%{public}d", cachedSettingBrightness_, key.c_str(), ret); + return cachedSettingBrightness_; } return static_cast(value); } void ScreenController::SetSettingBrightness() { - uint32_t brightness = action_->GetBrightness(); + uint32_t brightness = GetOriginalBrightness(); uint32_t settingBrightness = GetSettingBrightness(); if (brightness == static_cast(settingBrightness)) { DISPLAY_HILOGD(FEAT_BRIGHTNESS, "no need to set setting brightness"); @@ -329,7 +346,7 @@ void ScreenController::RegisterSettingBrightnessObserver() void ScreenController::BrightnessSettingUpdateFunc(const string& key) { - uint32_t brightness = action_->GetBrightness(); + uint32_t brightness = GetOriginalBrightness(); uint32_t settingBrightness = GetSettingBrightness(key); if (brightness == static_cast(settingBrightness)) { DISPLAY_HILOGD(FEAT_BRIGHTNESS, "no need to set setting brightness"); @@ -353,5 +370,15 @@ void ScreenController::UnregisterSettingBrightnessObserver() } g_brightnessObserver = nullptr; } + +double ScreenController::GetDiscount() const +{ + return discount_; +} + +uint32_t ScreenController::GetOriginalBrightness() +{ + return static_cast(GetBrightness() / discount_); +} } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/zidl/include/display_power_mgr_proxy.h b/service/zidl/include/display_power_mgr_proxy.h index 64f13c8..fcc727a 100644 --- a/service/zidl/include/display_power_mgr_proxy.h +++ b/service/zidl/include/display_power_mgr_proxy.h @@ -41,6 +41,7 @@ public: virtual uint32_t GetMainDisplayId() override; virtual bool SetBrightness(uint32_t value, uint32_t displayId) override; + virtual bool DiscountBrightness(double value, uint32_t displayId) override; virtual bool OverrideBrightness(uint32_t value, uint32_t displayId) override; virtual bool RestoreBrightness(uint32_t displayId) override; virtual uint32_t GetBrightness(uint32_t displayId) override; diff --git a/service/zidl/include/display_power_mgr_stub.h b/service/zidl/include/display_power_mgr_stub.h index 3b8bfd4..257aa9b 100644 --- a/service/zidl/include/display_power_mgr_stub.h +++ b/service/zidl/include/display_power_mgr_stub.h @@ -35,6 +35,7 @@ private: int32_t GetDisplayIdsStub(MessageParcel& data, MessageParcel& reply); int32_t GetMainDisplayIdStub(MessageParcel& data, MessageParcel& reply); int32_t SetBrightnessStub(MessageParcel& data, MessageParcel& reply); + int32_t DiscountBrightnessStub(MessageParcel& data, MessageParcel& reply); int32_t OverrideBrightnessStub(MessageParcel& data, MessageParcel& reply); int32_t RestoreBrightnessStub(MessageParcel& data, MessageParcel& reply); int32_t GetBrightnessStub(MessageParcel& data, MessageParcel& reply); diff --git a/service/zidl/src/display_power_mgr_proxy.cpp b/service/zidl/src/display_power_mgr_proxy.cpp index cafff63..c7c6d10 100644 --- a/service/zidl/src/display_power_mgr_proxy.cpp +++ b/service/zidl/src/display_power_mgr_proxy.cpp @@ -196,6 +196,40 @@ bool DisplayPowerMgrProxy::SetBrightness(uint32_t value, uint32_t displayId) return result; } +bool DisplayPowerMgrProxy::DiscountBrightness(double discount, uint32_t displayId) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, false); + + bool result = false; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { + DISPLAY_HILOGE(COMP_FWK, "DisplayPowerMgrProxy::%{public}s write descriptor failed!", __func__); + return result; + } + + WRITE_PARCEL_WITH_RET(data, Double, discount, false); + WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false); + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::DISCOUNT_BRIGHTNESS), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(COMP_FWK, "DisplayPowerMgrProxy::%{public}s SendRequest is failed: %{public}d", __func__, + ret); + return false; + } + + if(!reply.ReadBool(result)) { + DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); + return false; + } + + return result; +} + bool DisplayPowerMgrProxy::OverrideBrightness(uint32_t value, uint32_t displayId) { sptr remote = Remote(); diff --git a/service/zidl/src/display_power_mgr_stub.cpp b/service/zidl/src/display_power_mgr_stub.cpp index f051252..5fc84f2 100644 --- a/service/zidl/src/display_power_mgr_stub.cpp +++ b/service/zidl/src/display_power_mgr_stub.cpp @@ -62,6 +62,9 @@ int32_t DisplayPowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, case static_cast(IDisplayPowerMgr::SET_BRIGHTNESS): ret = SetBrightnessStub(data, reply); break; + case static_cast(IDisplayPowerMgr::DISCOUNT_BRIGHTNESS): + ret = DiscountBrightnessStub(data, reply); + break; case static_cast(IDisplayPowerMgr::OVERRIDE_BRIGHTNESS): ret = OverrideBrightnessStub(data, reply); break; @@ -177,6 +180,22 @@ int32_t DisplayPowerMgrStub::SetBrightnessStub(MessageParcel& data, MessageParce return ERR_OK; } +int32_t DisplayPowerMgrStub::DiscountBrightnessStub(MessageParcel& data, MessageParcel& reply) +{ + double discount = 0; + uint32_t displayId = 0; + + READ_PARCEL_WITH_RET(data, Double, discount, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR); + + bool ret = DiscountBrightness(discount, displayId); + if (!reply.WriteBool(ret)) { + DISPLAY_HILOGE(COMP_SVC, "Failed to wirte DiscountBrightness return value"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} + int32_t DisplayPowerMgrStub::OverrideBrightnessStub(MessageParcel& data, MessageParcel& reply) { uint32_t value = 0; diff --git a/test/unittest/common/native/src/display_power_mgr_brightness_test.cpp b/test/unittest/common/native/src/display_power_mgr_brightness_test.cpp index 7281c64..876735a 100644 --- a/test/unittest/common/native/src/display_power_mgr_brightness_test.cpp +++ b/test/unittest/common/native/src/display_power_mgr_brightness_test.cpp @@ -104,6 +104,315 @@ HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrSettingBrightness002, Tes DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrSettingBrightness002: fun is end"); } +/** + * @tc.name: DisplayPowerMgrDiscountBrightness001 + * @tc.desc: Test DiscountBrightness after OverrideBrightness + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrDiscountBrightness001, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness001: fun is start"); + bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(100); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().OverrideBrightness(200); + EXPECT_TRUE(ret); + sleep(1); + uint32_t value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 200); + ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(0.8); + EXPECT_TRUE(ret); + sleep(1); + value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 160); + DisplayPowerMgrClient::GetInstance().RestoreBrightness(); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness001: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrDiscountBrightness002 + * @tc.desc: Test OverrideBrightness after DiscountBrightness + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrDiscountBrightness002, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness002: fun is start"); + bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(100); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(0.8); + EXPECT_TRUE(ret); + sleep(1); + uint32_t value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 80); + ret = DisplayPowerMgrClient::GetInstance().OverrideBrightness(200); + EXPECT_TRUE(ret); + sleep(1); + value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 160); + DisplayPowerMgrClient::GetInstance().RestoreBrightness(); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness002: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrDiscountBrightness003 + * @tc.desc: Test OverrideBrightness after DiscountBrightness, and continue RestoreBrightness + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrDiscountBrightness003, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness003: fun is start"); + bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(100); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(0.8); + EXPECT_TRUE(ret); + sleep(1); + uint32_t value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 80); + ret = DisplayPowerMgrClient::GetInstance().OverrideBrightness(200); + EXPECT_TRUE(ret); + sleep(1); + value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 160); + ret = DisplayPowerMgrClient::GetInstance().RestoreBrightness(); + EXPECT_TRUE(ret); + sleep(1); + value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 80); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness003: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrDiscountBrightness004 + * @tc.desc: Test DiscountBrightness after OverrideBrightness, and continue RestoreBrightness + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrDiscountBrightness004, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness004: fun is start"); + bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(100); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().OverrideBrightness(200); + EXPECT_TRUE(ret); + sleep(1); + uint32_t value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 200); + ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(0.8); + EXPECT_TRUE(ret); + sleep(1); + value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 160); + ret = DisplayPowerMgrClient::GetInstance().RestoreBrightness(); + EXPECT_TRUE(ret); + sleep(1); + value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 80); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness004: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrDiscountBrightness005 + * @tc.desc: Test BoostBrightness after DisconutBrightness + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrDiscountBrightness005, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness005: fun is start"); + bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(100); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(0.8); + EXPECT_TRUE(ret); + sleep(1); + uint32_t value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 80); + ret = DisplayPowerMgrClient::GetInstance().BoostBrightness(10000); + EXPECT_TRUE(ret); + sleep(1); + uint32_t maxBrightness = DisplayPowerMgrClient::GetInstance().GetMaxBrightness(); + value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, static_cast(maxBrightness * 0.8)); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness005: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrDiscountBrightness006 + * @tc.desc: Test DisconutBrightness after BoostBrightness + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrDiscountBrightness006, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness006: fun is start"); + bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(100); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().BoostBrightness(10000); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(0.8); + EXPECT_TRUE(ret); + sleep(1); + uint32_t maxBrightness = DisplayPowerMgrClient::GetInstance().GetMaxBrightness(); + uint32_t value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, static_cast(maxBrightness * 0.8)); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness006: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrDiscountBrightness007 + * @tc.desc: Test DisconutBrightness after BoostBrightness and CancelBoostBrightness + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrDiscountBrightness007, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness007: fun is start"); + bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(100); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().BoostBrightness(10000); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().CancelBoostBrightness(); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(0.8); + EXPECT_TRUE(ret); + sleep(1); + uint32_t value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 80); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness007: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrDiscountBrightness008 + * @tc.desc: Test DisconutBrightness after SetDisplayState(DisplayState::DISPLAY_OFF) + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrDiscountBrightness008, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness008: fun is start"); + bool ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_OFF); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(0.8); + EXPECT_FALSE(ret); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightness008: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrDiscountBrightnessNormal + * @tc.desc: Test DiscountBrightness the normal test + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrDiscountBrightnessNormal, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightnessNormal: fun is start"); + bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(100); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(0.8); + EXPECT_TRUE(ret); + sleep(1); + uint32_t value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 80); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightnessNormal: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrDiscountBrightnessAbnormal001 + * @tc.desc: Test DiscountBrightness the normal test + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrDiscountBrightnessAbnormal001, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightnessAbnormal001: fun is start"); + bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(100); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(0); + EXPECT_TRUE(ret); + sleep(1); + uint32_t value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 4); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightnessAbnormal001: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrDiscountBrightnessAbnormal002 + * @tc.desc: Test DiscountBrightness the abnormal test + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrDiscountBrightnessAbnormal002, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightnessAbnormal002: fun is start"); + bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(100); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(0.01); + EXPECT_TRUE(ret); + sleep(1); + uint32_t value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 4); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightnessAbnormal002: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrDiscountBrightnessAbnormal003 + * @tc.desc: Test DiscountBrightness the abnormal test + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrDiscountBrightnessAbnormal003, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightnessAbnormal03: fun is start"); + bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(100); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(1); + EXPECT_TRUE(ret); + sleep(1); + uint32_t value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 100); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightnessAbnormal003: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrDiscountBrightnessAbnormal004 + * @tc.desc: Test DiscountBrightness the abnormal test + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrDiscountBrightnessAbnormal004, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightnessAbnormal004: fun is start"); + bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(100); + EXPECT_TRUE(ret); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(2); + EXPECT_TRUE(ret); + sleep(1); + uint32_t value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 100); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightnessAbnormal004: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrDiscountBrightnessAbnormal005 + * @tc.desc: Test DiscountBrightness the abnormal test + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrDiscountBrightnessAbnormal005, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightnessAbnormal005: fun is start"); + sleep(1); + ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(-1); + EXPECT_TRUE(ret); + sleep(1); + uint32_t value = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(value, 4); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDiscountBrightnessAbnormal005: fun is end"); +} + /** * @tc.name: DisplayPowerMgrOverrideBrightness001 * @tc.desc: Test OverrideBrightness -- Gitee From e2423b017f3455ec156655dea127fc065ec3417f Mon Sep 17 00:00:00 2001 From: aqxyjay Date: Tue, 2 Aug 2022 10:31:03 +0800 Subject: [PATCH 2/4] feat: optimize brightness discount logic Signed-off-by: aqxyjay --- .../include/display_accessibility_observer.h | 3 -- .../include/display_power_mgr_service.h | 4 +-- .../native/include/display_system_ability.h | 2 -- service/native/include/screen_controller.h | 3 +- .../src/display_accessibility_observer.cpp | 20 ------------ .../native/src/display_power_mgr_service.cpp | 8 ++--- service/native/src/display_system_ability.cpp | 31 ++++++------------- service/native/src/screen_controller.cpp | 23 +++++++------- 8 files changed, 28 insertions(+), 66 deletions(-) diff --git a/service/native/include/display_accessibility_observer.h b/service/native/include/display_accessibility_observer.h index 20faa54..9be78a1 100644 --- a/service/native/include/display_accessibility_observer.h +++ b/service/native/include/display_accessibility_observer.h @@ -28,9 +28,6 @@ public: ~DisplayAccessibilityObserver() override = default; void OnConfigChanged(CONFIG_ID id, const ConfigValue& value) override; - void RegisterObserver(); - void UnregisterObserver(); - private: std::shared_ptr observer_ = nullptr; }; diff --git a/service/native/include/display_power_mgr_service.h b/service/native/include/display_power_mgr_service.h index 4f57045..0c7cbd1 100644 --- a/service/native/include/display_power_mgr_service.h +++ b/service/native/include/display_power_mgr_service.h @@ -63,6 +63,8 @@ public: void Init(); [[maybe_unused]] void RegisterSettings(); [[maybe_unused]] void UnregisterSettings(); + static uint32_t GetSafeBrightness(uint32_t value); + static double GetSafeDiscount(double discount); private: class CallbackDeathRecipient : public IRemoteObject::DeathRecipient { @@ -93,8 +95,6 @@ private: DisplayPowerMgrService(); void InitSensors(); bool IsChangedLux(float scalar); - static uint32_t GetSafeBrightness(uint32_t value); - static double GetSafeDiscount(double discount); bool CalculateBrightness(float scalar, int32_t& brightness); int32_t GetBrightnessFromLightScalar(float scalar); void ActivateAmbientSensor(); diff --git a/service/native/include/display_system_ability.h b/service/native/include/display_system_ability.h index 153b482..ca90799 100644 --- a/service/native/include/display_system_ability.h +++ b/service/native/include/display_system_ability.h @@ -34,8 +34,6 @@ public: void OnStart() override; void OnStop() override; - void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; - void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; private: sptr service_; diff --git a/service/native/include/screen_controller.h b/service/native/include/screen_controller.h index 48eed37..f4e1888 100644 --- a/service/native/include/screen_controller.h +++ b/service/native/include/screen_controller.h @@ -46,7 +46,6 @@ public: DisplayState GetState(); bool UpdateState(DisplayState state, uint32_t reason); - bool UpdateStateConfig(DisplayState state, uint32_t value); bool IsScreenOn(); bool SetBrightness(uint32_t value, uint32_t gradualDuration = 0); @@ -74,7 +73,7 @@ private: bool CanOverrideBrightness(); bool CanBoostBrightness(); bool UpdateBrightness(uint32_t value, uint32_t gradualDuration = 0); - uint32_t GetOriginalBrightness(); + uint32_t GetDeviceBrightness(); void SetSettingBrightness(); uint32_t GetScreenOnBrightness() const; void BrightnessSettingUpdateFunc(const std::string& key); diff --git a/service/native/src/display_accessibility_observer.cpp b/service/native/src/display_accessibility_observer.cpp index 7fa19fe..14be68d 100644 --- a/service/native/src/display_accessibility_observer.cpp +++ b/service/native/src/display_accessibility_observer.cpp @@ -35,25 +35,5 @@ void DisplayAccessibilityObserver::OnConfigChanged(CONFIG_ID id, const ConfigVal } DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Brightness discount config changed finish"); } - -void DisplayAccessibilityObserver::RegisterObserver() -{ - if (!observer_) { - observer_ = std::make_shared(); - } - auto& config = Singleton::GetInstance(); - config.SubscribeConfigObserver(OHOS::AccessibilityConfig::CONFIG_BRIGHTNESS_DISCOUNT, observer_); - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Brightness discount accessibility observer registered"); -} - -void DisplayAccessibilityObserver::UnregisterObserver() -{ - if (!observer_) { - return; - } - auto& config = Singleton::GetInstance(); - config.UnsubscribeConfigObserver(OHOS::AccessibilityConfig::CONFIG_BRIGHTNESS_DISCOUNT, observer_); - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Brightness discount accessibility observer unregistered"); -} } // namespace DisplayPowerMgr } // namespace OHOS \ No newline at end of file diff --git a/service/native/src/display_power_mgr_service.cpp b/service/native/src/display_power_mgr_service.cpp index 730a018..872c133 100644 --- a/service/native/src/display_power_mgr_service.cpp +++ b/service/native/src/display_power_mgr_service.cpp @@ -479,11 +479,11 @@ uint32_t DisplayPowerMgrService::GetSafeBrightness(uint32_t value) { auto brightnessValue = value; if (brightnessValue > BRIGHTNESS_MAX) { - DISPLAY_HILOGW(FEAT_BRIGHTNESS, "brightness value is greater than max, value=%{public}u", value); + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "brightness value is greater than max, value=%{public}u", value); brightnessValue = BRIGHTNESS_MAX; } if (brightnessValue < BRIGHTNESS_MIN) { - DISPLAY_HILOGW(FEAT_BRIGHTNESS, "brightness value is less than min, value=%{public}u", value); + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "brightness value is less than min, value=%{public}u", value); brightnessValue = BRIGHTNESS_MIN; } return brightnessValue; @@ -493,11 +493,11 @@ double DisplayPowerMgrService::GetSafeDiscount(double discount) { auto safeDiscount = discount; if (safeDiscount > DISCOUNT_MAX) { - DISPLAY_HILOGW(COMP_SVC, "discount value is greater than max, discount=%{public}lf", discount); + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "discount value is greater than max, discount=%{public}lf", discount); safeDiscount = DISCOUNT_MAX; } if (safeDiscount < DISCOUNT_MIN) { - DISPLAY_HILOGW(COMP_SVC, "discount value is less than min, discount=%{public}lf", discount); + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "discount value is less than min, discount=%{public}lf", discount); safeDiscount = DISCOUNT_MIN; } return safeDiscount; diff --git a/service/native/src/display_system_ability.cpp b/service/native/src/display_system_ability.cpp index 8f05823..a4b8286 100644 --- a/service/native/src/display_system_ability.cpp +++ b/service/native/src/display_system_ability.cpp @@ -32,7 +32,11 @@ void DisplaySystemAbility::OnStart() DISPLAY_HILOGI(COMP_SVC, "Start service"); auto service = DelayedSpSingleton::GetInstance(); service->Init(); - AddSystemAbilityListener(ACCESSIBILITY_MANAGER_SERVICE_ID); + g_observer = std::make_shared(); + auto& config = Singleton::GetInstance(); + config.InitializeContext(); + config.SubscribeConfigObserver(OHOS::AccessibilityConfig::CONFIG_BRIGHTNESS_DISCOUNT, g_observer); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Brightness discount accessibility observer registered"); if (!Publish(service)) { DISPLAY_HILOGE(COMP_SVC, "Failed to publish service"); } @@ -41,27 +45,10 @@ void DisplaySystemAbility::OnStart() void DisplaySystemAbility::OnStop() { DISPLAY_HILOGW(COMP_SVC, "Stop service"); - RemoveSystemAbilityListener(ACCESSIBILITY_MANAGER_SERVICE_ID); -} - -void DisplaySystemAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) -{ - if (systemAbilityId == ACCESSIBILITY_MANAGER_SERVICE_ID) { - DISPLAY_HILOGI(COMP_SVC, "Accessibility service added, deviceId=%{private}s", deviceId.c_str()); - if (!g_observer) { - g_observer = std::make_shared(); - } - g_observer->RegisterObserver(); - } -} - -void DisplaySystemAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) -{ - if (systemAbilityId == ACCESSIBILITY_MANAGER_SERVICE_ID) { - DISPLAY_HILOGI(COMP_SVC, "Accessibility service removed, deviceId=%{private}s", deviceId.c_str()); - if (!g_observer) { - g_observer->UnregisterObserver(); - } + if (!g_observer) { + auto& config = Singleton::GetInstance(); + config.UnsubscribeConfigObserver(OHOS::AccessibilityConfig::CONFIG_BRIGHTNESS_DISCOUNT, g_observer); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Brightness discount accessibility observer unregistered"); } } } // OHOS diff --git a/service/native/src/screen_controller.cpp b/service/native/src/screen_controller.cpp index 698ea6e..39b328b 100644 --- a/service/native/src/screen_controller.cpp +++ b/service/native/src/screen_controller.cpp @@ -130,7 +130,7 @@ bool ScreenController::SetBrightness(uint32_t value, uint32_t gradualDuration) return false; } DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Set brightness, value=%{public}u", value); - cachedSettingBrightness_ = GetOriginalBrightness(); + cachedSettingBrightness_ = GetBrightness(); bool ret = UpdateBrightness(value, gradualDuration); if (ret) { handler_->SendEvent(DisplayEventHandler::Event::EVENT_SET_SETTING_BRIGHTNESS); @@ -140,7 +140,7 @@ bool ScreenController::SetBrightness(uint32_t value, uint32_t gradualDuration) uint32_t ScreenController::GetBrightness() { - return action_->GetBrightness(); + return static_cast(GetDeviceBrightness() / discount_); } bool ScreenController::DiscountBrightness(double discount, uint32_t gradualDuration) @@ -149,9 +149,10 @@ bool ScreenController::DiscountBrightness(double discount, uint32_t gradualDurat DISPLAY_HILOGW(FEAT_BRIGHTNESS, "Cannot discount brightness, ignore the change"); return false; } + uint32_t originalBrightness = GetBrightness(); DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Discount brightness, discount=%{public}lf", discount); discount_ = discount; - return UpdateBrightness(GetBrightness(), gradualDuration); + return UpdateBrightness(originalBrightness, gradualDuration); } bool ScreenController::OverrideBrightness(uint32_t value, uint32_t gradualDuration) @@ -163,7 +164,7 @@ bool ScreenController::OverrideBrightness(uint32_t value, uint32_t gradualDurati DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Override brightness, value=%{public}u", value); if (!isBrightnessOverridden_) { isBrightnessOverridden_ = true; - cachedSettingBrightness_ = GetOriginalBrightness(); + cachedSettingBrightness_ = GetBrightness(); } overriddenBrightness_ = value; return UpdateBrightness(value, gradualDuration); @@ -196,7 +197,7 @@ bool ScreenController::BoostBrightness(uint32_t timeoutMs, uint32_t gradualDurat uint32_t maxBrightness = DisplayParamHelper::GetInstance().GetMaxBrightness(); DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Boost brightness, maxBrightness: %{public}d", maxBrightness); isBrightnessBoosted_ = true; - cachedSettingBrightness_ = GetOriginalBrightness(); + cachedSettingBrightness_ = GetBrightness(); ret = UpdateBrightness(maxBrightness, gradualDuration); } @@ -269,14 +270,14 @@ bool ScreenController::UpdateBrightness(uint32_t value, uint32_t gradualDuration { DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Update brightness, value=%{public}u, discount=%{public}lf,"\ " duration=%{public}u", value, discount_, gradualDuration); - auto brightness = static_cast(value * discount_); + auto brightness = DisplayPowerMgrService::GetSafeBrightness(static_cast(value * discount_)); if (animator_->IsAnimating()) { animator_->StopAnimation(); } if (gradualDuration > 0) { DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Update brightness gradually"); - animator_->StartAnimation(GetBrightness(), brightness, gradualDuration); + animator_->StartAnimation(GetDeviceBrightness(), brightness, gradualDuration); return true; } bool isSucc = action_->SetBrightness(brightness); @@ -300,7 +301,7 @@ uint32_t ScreenController::GetSettingBrightness(const std::string& key) const void ScreenController::SetSettingBrightness() { - uint32_t brightness = GetOriginalBrightness(); + uint32_t brightness = GetBrightness(); uint32_t settingBrightness = GetSettingBrightness(); if (brightness == static_cast(settingBrightness)) { DISPLAY_HILOGD(FEAT_BRIGHTNESS, "no need to set setting brightness"); @@ -346,7 +347,7 @@ void ScreenController::RegisterSettingBrightnessObserver() void ScreenController::BrightnessSettingUpdateFunc(const string& key) { - uint32_t brightness = GetOriginalBrightness(); + uint32_t brightness = GetBrightness(); uint32_t settingBrightness = GetSettingBrightness(key); if (brightness == static_cast(settingBrightness)) { DISPLAY_HILOGD(FEAT_BRIGHTNESS, "no need to set setting brightness"); @@ -376,9 +377,9 @@ double ScreenController::GetDiscount() const return discount_; } -uint32_t ScreenController::GetOriginalBrightness() +uint32_t ScreenController::GetDeviceBrightness() { - return static_cast(GetBrightness() / discount_); + return action_->GetBrightness(); } } // namespace DisplayPowerMgr } // namespace OHOS -- Gitee From 3348775887a51a0ca53c2c4ceb5a239eed73e458 Mon Sep 17 00:00:00 2001 From: aqxyjay Date: Wed, 3 Aug 2022 16:47:58 +0800 Subject: [PATCH 3/4] feat: optimize brightness discount logic2 Signed-off-by: aqxyjay --- service/native/include/display_power_mgr_service.h | 3 +++ service/native/src/display_power_mgr_service.cpp | 13 ++++++++++++- service/native/src/display_system_ability.cpp | 12 ------------ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/service/native/include/display_power_mgr_service.h b/service/native/include/display_power_mgr_service.h index 0c7cbd1..245d7f7 100644 --- a/service/native/include/display_power_mgr_service.h +++ b/service/native/include/display_power_mgr_service.h @@ -28,6 +28,7 @@ #include "event_runner.h" #include "iremote_object.h" #include "sensor_agent_type.h" +#include "display_accessibility_observer.h" #include "idisplay_power_callback.h" #include "display_power_info.h" #include "display_event_handler.h" @@ -99,6 +100,7 @@ private: int32_t GetBrightnessFromLightScalar(float scalar); void ActivateAmbientSensor(); void DeactivateAmbientSensor(); + void InitAccessibilityObserver(); std::map> controllerMap_; bool supportLightSensor_ {false}; @@ -109,6 +111,7 @@ private: sptr cbDeathRecipient_; std::shared_ptr eventRunner_ { nullptr }; std::shared_ptr handler_ { nullptr }; + std::shared_ptr observer_ { nullptr }; time_t lastLuxTime_ {0}; float lastLux_ {0}; diff --git a/service/native/src/display_power_mgr_service.cpp b/service/native/src/display_power_mgr_service.cpp index 872c133..c8a605a 100644 --- a/service/native/src/display_power_mgr_service.cpp +++ b/service/native/src/display_power_mgr_service.cpp @@ -59,6 +59,8 @@ void DisplayPowerMgrService::Init() DISPLAY_HILOGI(COMP_SVC, "find display, id=%{public}u", id); controllerMap_.emplace(id, std::make_shared(id, handler_)); } + + InitAccessibilityObserver(); callback_ = nullptr; cbDeathRecipient_ = nullptr; @@ -139,7 +141,7 @@ bool DisplayPowerMgrService::SetBrightness(uint32_t value, uint32_t displayId) bool DisplayPowerMgrService::DiscountBrightness(double discount, uint32_t displayId) { auto safeDiscount = GetSafeDiscount(discount); - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "DiscountBrightness displayId=%{public}u, discount-%{public}lf", + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "DiscountBrightness displayId=%{public}u, discount=%{public}lf", displayId, safeDiscount); auto iter = controllerMap_.find(displayId); if (iter == controllerMap_.end()) { @@ -542,6 +544,15 @@ int32_t DisplayPowerMgrService::GetBrightnessFromLightScalar(float scalar) return brightness; } +void DisplayPowerMgrService::InitAccessibilityObserver() +{ + observer_ = std::make_shared(); + auto& config = Singleton::GetInstance(); + bool ret = config.InitializeContext(); + config.SubscribeConfigObserver(OHOS::AccessibilityConfig::CONFIG_BRIGHTNESS_DISCOUNT, observer_); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Brightness discount accessibility observer registered, ret=%{public}d", ret); +} + void DisplayPowerMgrService::CallbackDeathRecipient::OnRemoteDied(const wptr& remote) { DISPLAY_HILOGI(COMP_SVC, "CallbackDeathRecipient OnRemoteDied"); diff --git a/service/native/src/display_system_ability.cpp b/service/native/src/display_system_ability.cpp index a4b8286..144c1af 100644 --- a/service/native/src/display_system_ability.cpp +++ b/service/native/src/display_system_ability.cpp @@ -18,25 +18,18 @@ #include "iremote_object.h" #include "new" #include "system_ability_definition.h" -#include "display_accessibility_observer.h" #include "display_log.h" namespace OHOS { namespace DisplayPowerMgr { namespace { REGISTER_SYSTEM_ABILITY_BY_ID(DisplaySystemAbility, DISPLAY_MANAGER_SERVICE_ID, true); -std::shared_ptr g_observer = nullptr; } void DisplaySystemAbility::OnStart() { DISPLAY_HILOGI(COMP_SVC, "Start service"); auto service = DelayedSpSingleton::GetInstance(); service->Init(); - g_observer = std::make_shared(); - auto& config = Singleton::GetInstance(); - config.InitializeContext(); - config.SubscribeConfigObserver(OHOS::AccessibilityConfig::CONFIG_BRIGHTNESS_DISCOUNT, g_observer); - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Brightness discount accessibility observer registered"); if (!Publish(service)) { DISPLAY_HILOGE(COMP_SVC, "Failed to publish service"); } @@ -45,11 +38,6 @@ void DisplaySystemAbility::OnStart() void DisplaySystemAbility::OnStop() { DISPLAY_HILOGW(COMP_SVC, "Stop service"); - if (!g_observer) { - auto& config = Singleton::GetInstance(); - config.UnsubscribeConfigObserver(OHOS::AccessibilityConfig::CONFIG_BRIGHTNESS_DISCOUNT, g_observer); - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Brightness discount accessibility observer unregistered"); - } } } // OHOS } // DisplayPowerMgr \ No newline at end of file -- Gitee From 16a031c83352fb330486d1b68fc69e354cc45448 Mon Sep 17 00:00:00 2001 From: aqxyjay Date: Thu, 4 Aug 2022 16:56:35 +0800 Subject: [PATCH 4/4] feat: remove accessibility deps Signed-off-by: aqxyjay --- service/BUILD.gn | 2 - .../include/display_accessibility_observer.h | 36 ----------------- .../include/display_power_mgr_service.h | 2 - .../src/display_accessibility_observer.cpp | 39 ------------------- .../native/src/display_power_mgr_service.cpp | 10 ----- 5 files changed, 89 deletions(-) delete mode 100644 service/native/include/display_accessibility_observer.h delete mode 100644 service/native/src/display_accessibility_observer.cpp diff --git a/service/BUILD.gn b/service/BUILD.gn index 352273c..81e67ef 100644 --- a/service/BUILD.gn +++ b/service/BUILD.gn @@ -31,7 +31,6 @@ config("displaymgr_public_config") { ohos_shared_library("displaymgrservice") { sources = [ - "native/src/display_accessibility_observer.cpp", "native/src/display_event_handler.cpp", "native/src/display_param_helper.cpp", "native/src/display_power_mgr_service.cpp", @@ -61,7 +60,6 @@ ohos_shared_library("displaymgrservice") { "ability_base:want", "ability_base:zuri", "ability_runtime:ability_manager", - "accessibility:accessibilityconfig", "bundle_framework:appexecfwk_base", "c_utils:utils", "eventhandler:libeventhandler", diff --git a/service/native/include/display_accessibility_observer.h b/service/native/include/display_accessibility_observer.h deleted file mode 100644 index 9be78a1..0000000 --- a/service/native/include/display_accessibility_observer.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef DISPLAYMGR_DISPLAY_ACCESSIBILITY_OBSERVER_H -#define DISPLAYMGR_DISPLAY_ACCESSIBILITY_OBSERVER_H - -#include "accessibility_config.h" - -namespace OHOS { -namespace DisplayPowerMgr { -using namespace OHOS::AccessibilityConfig; - -class DisplayAccessibilityObserver : public AccessibilityConfigObserver { -public: - DisplayAccessibilityObserver() = default; - ~DisplayAccessibilityObserver() override = default; - void OnConfigChanged(CONFIG_ID id, const ConfigValue& value) override; - -private: - std::shared_ptr observer_ = nullptr; -}; -} // namespace DisplayPowerMgr -} // namespace OHOS -#endif //DISPLAYMGR_DISPLAY_ACCESSIBILITY_OBSERVER_H diff --git a/service/native/include/display_power_mgr_service.h b/service/native/include/display_power_mgr_service.h index 245d7f7..92e7d82 100644 --- a/service/native/include/display_power_mgr_service.h +++ b/service/native/include/display_power_mgr_service.h @@ -28,7 +28,6 @@ #include "event_runner.h" #include "iremote_object.h" #include "sensor_agent_type.h" -#include "display_accessibility_observer.h" #include "idisplay_power_callback.h" #include "display_power_info.h" #include "display_event_handler.h" @@ -100,7 +99,6 @@ private: int32_t GetBrightnessFromLightScalar(float scalar); void ActivateAmbientSensor(); void DeactivateAmbientSensor(); - void InitAccessibilityObserver(); std::map> controllerMap_; bool supportLightSensor_ {false}; diff --git a/service/native/src/display_accessibility_observer.cpp b/service/native/src/display_accessibility_observer.cpp deleted file mode 100644 index 14be68d..0000000 --- a/service/native/src/display_accessibility_observer.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "display_accessibility_observer.h" - -#include "display_power_mgr_service.h" -#include "display_log.h" - -namespace OHOS { -namespace DisplayPowerMgr { -void DisplayAccessibilityObserver::OnConfigChanged(CONFIG_ID id, const ConfigValue& value) -{ - if (id != CONFIG_BRIGHTNESS_DISCOUNT) { - return; - } - float discount = value.brightnessDiscount; - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Brightness discount config changed, discount=%{public}f", discount); - - auto service = DelayedSpSingleton::GetInstance(); - auto displayIds = service->GetDisplayIds(); - for (const auto& displayId: displayIds) { - service->DiscountBrightness(static_cast(discount), displayId); - } - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Brightness discount config changed finish"); -} -} // namespace DisplayPowerMgr -} // namespace OHOS \ No newline at end of file diff --git a/service/native/src/display_power_mgr_service.cpp b/service/native/src/display_power_mgr_service.cpp index c8a605a..196f1ed 100644 --- a/service/native/src/display_power_mgr_service.cpp +++ b/service/native/src/display_power_mgr_service.cpp @@ -60,7 +60,6 @@ void DisplayPowerMgrService::Init() controllerMap_.emplace(id, std::make_shared(id, handler_)); } - InitAccessibilityObserver(); callback_ = nullptr; cbDeathRecipient_ = nullptr; @@ -544,15 +543,6 @@ int32_t DisplayPowerMgrService::GetBrightnessFromLightScalar(float scalar) return brightness; } -void DisplayPowerMgrService::InitAccessibilityObserver() -{ - observer_ = std::make_shared(); - auto& config = Singleton::GetInstance(); - bool ret = config.InitializeContext(); - config.SubscribeConfigObserver(OHOS::AccessibilityConfig::CONFIG_BRIGHTNESS_DISCOUNT, observer_); - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Brightness discount accessibility observer registered, ret=%{public}d", ret); -} - void DisplayPowerMgrService::CallbackDeathRecipient::OnRemoteDied(const wptr& remote) { DISPLAY_HILOGI(COMP_SVC, "CallbackDeathRecipient OnRemoteDied"); -- Gitee