From abb8687d96ed272b38e2df291d18ee07c5b338b3 Mon Sep 17 00:00:00 2001 From: yangziyong Date: Wed, 1 Dec 2021 16:43:18 +0800 Subject: [PATCH 1/9] Fix code check issues Change-Id: I1b3741ee0c0108210ad9afb7f60fabd6c78c1acc Signed-off-by: Du Jiang --- service/native/include/gradual_animator.h | 2 +- service/native/include/screen_controller.h | 2 +- service/native/src/gradual_animator.cpp | 2 +- service/native/src/screen_controller.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/service/native/include/gradual_animator.h b/service/native/include/gradual_animator.h index f592fac..8c1888e 100644 --- a/service/native/include/gradual_animator.h +++ b/service/native/include/gradual_animator.h @@ -53,7 +53,7 @@ private: std::string name_; std::shared_ptr callback_; std::shared_ptr eventRunner_; - std::shared_ptr handler_; + std::unique_ptr handler_; bool animating_ = false; int32_t from_; int32_t to_; diff --git a/service/native/include/screen_controller.h b/service/native/include/screen_controller.h index 3ad2c07..193ac2d 100644 --- a/service/native/include/screen_controller.h +++ b/service/native/include/screen_controller.h @@ -52,7 +52,7 @@ private: uint32_t brightness_ {0}; std::shared_ptr action_; - std::shared_ptr animator_; + std::unique_ptr animator_; }; } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/src/gradual_animator.cpp b/service/native/src/gradual_animator.cpp index 84b88ce..a794a25 100644 --- a/service/native/src/gradual_animator.cpp +++ b/service/native/src/gradual_animator.cpp @@ -60,7 +60,7 @@ void GradualAnimator::StartAnimation(int32_t from, int32_t to, uint32_t duration stride_ = (to_ - from_) / steps_; currentStep_ = 0; if (handler_ == nullptr) { - handler_ = std::make_shared(eventRunner_, shared_from_this()); + handler_ = std::make_unique(eventRunner_, shared_from_this()); } animating_ = true; handler_->SendEvent(EVENT_STEP, 0, updateTime_); diff --git a/service/native/src/screen_controller.cpp b/service/native/src/screen_controller.cpp index 93f34fe..d2cde6c 100644 --- a/service/native/src/screen_controller.cpp +++ b/service/native/src/screen_controller.cpp @@ -71,7 +71,7 @@ bool ScreenController::UpdateBrightness(uint32_t value, uint32_t duraion) if (animator_ == nullptr) { std::string name = "ScreenController_" + std::to_string(devId_); std::shared_ptr callback = shared_from_this(); - animator_ = std::make_shared(name, callback); + animator_ = std::make_unique(name, callback); } if (animator_->IsAnimating()) { animator_->StopAnimation(); -- Gitee From 04cfddbfb84fdee1a2da39b1ba879ef5ba04a596 Mon Sep 17 00:00:00 2001 From: Du Jiang Date: Fri, 17 Dec 2021 14:04:57 +0800 Subject: [PATCH 2/9] Support to adjust brightness automatically Change-Id: Ifde2f6643287831fad81f16349e217f2b2e24041 Signed-off-by: Du Jiang --- .../native/display_power_mgr_client.cpp | 36 +++++ .../native/include/display_power_mgr_client.h | 6 + .../native/include/idisplay_power_callback.h | 34 +++++ .../native/include/idisplay_power_mgr.h | 10 ++ service/BUILD.gn | 6 +- .../include/display_power_mgr_service.h | 28 +++- service/native/include/screen_controller.h | 1 + .../native/src/display_power_mgr_service.cpp | 142 ++++++++++++++++++ service/native/src/display_system_ability.cpp | 9 +- service/native/src/screen_controller.cpp | 9 ++ .../zidl/include/display_power_mgr_proxy.h | 5 + service/zidl/include/display_power_mgr_stub.h | 4 + service/zidl/src/display_power_mgr_proxy.cpp | 136 +++++++++++++++++ service/zidl/src/display_power_mgr_stub.cpp | 62 +++++++- test/native/unittest/BUILD.gn | 2 + .../src/display_power_mgr_service_test.cpp | 47 ++++++ utils/native/include/delayed_sp_singleton.h | 68 +++++++++ 17 files changed, 592 insertions(+), 13 deletions(-) create mode 100644 interfaces/innerkits/native/include/idisplay_power_callback.h create mode 100644 utils/native/include/delayed_sp_singleton.h diff --git a/frameworks/native/display_power_mgr_client.cpp b/frameworks/native/display_power_mgr_client.cpp index 94e59c8..382f951 100644 --- a/frameworks/native/display_power_mgr_client.cpp +++ b/frameworks/native/display_power_mgr_client.cpp @@ -90,6 +90,24 @@ DisplayState DisplayPowerMgrClient::GetDisplayState(uint32_t id) return proxy->GetDisplayState(id); } +std::vector DisplayPowerMgrClient::GetDisplayIds() +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + return std::vector(); + } + return proxy->GetDisplayIds(); +} + +uint32_t DisplayPowerMgrClient::GetMainDisplayId() +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + return 0; + } + return proxy->GetMainDisplayId(); +} + bool DisplayPowerMgrClient::SetBrightness(uint32_t value, uint32_t id) { auto proxy = GetProxy(); @@ -108,6 +126,15 @@ bool DisplayPowerMgrClient::AdjustBrightness(uint32_t value, uint32_t duration, return proxy->AdjustBrightness(id, value, duration); } +bool DisplayPowerMgrClient::AutoAdjustBrightness(bool enable) +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + return false; + } + return proxy->AutoAdjustBrightness(enable); +} + bool DisplayPowerMgrClient::SetStateConfig(DisplayState state, uint32_t value, uint32_t id) { auto proxy = GetProxy(); @@ -116,5 +143,14 @@ bool DisplayPowerMgrClient::SetStateConfig(DisplayState state, uint32_t value, u } return proxy->SetStateConfig(id, state, value); } + +bool DisplayPowerMgrClient::RegisterCallback(sptr callback) +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + return false; + } + return proxy->RegisterCallback(callback); +} } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/interfaces/innerkits/native/include/display_power_mgr_client.h b/interfaces/innerkits/native/include/display_power_mgr_client.h index 3478eb3..0e8efc6 100644 --- a/interfaces/innerkits/native/include/display_power_mgr_client.h +++ b/interfaces/innerkits/native/include/display_power_mgr_client.h @@ -18,8 +18,10 @@ #include #include +#include #include "display_info.h" +#include "idisplay_power_callback.h" #include "idisplay_power_mgr.h" namespace OHOS { @@ -30,9 +32,13 @@ class DisplayPowerMgrClient : public DelayedRefSingleton public: bool SetDisplayState(DisplayState state, uint32_t id = 0); DisplayState GetDisplayState(uint32_t id = 0); + std::vector GetDisplayIds(); + uint32_t GetMainDisplayId(); bool SetBrightness(uint32_t value, uint32_t id = 0); bool AdjustBrightness(uint32_t value, uint32_t duration, uint32_t id = 0); bool SetStateConfig(DisplayState state, uint32_t value, uint32_t id = 0); + bool AutoAdjustBrightness(bool enable); + bool RegisterCallback(sptr callback); private: class DisplayDeathRecipient : public IRemoteObject::DeathRecipient { diff --git a/interfaces/innerkits/native/include/idisplay_power_callback.h b/interfaces/innerkits/native/include/idisplay_power_callback.h new file mode 100644 index 0000000..ff1ac20 --- /dev/null +++ b/interfaces/innerkits/native/include/idisplay_power_callback.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 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 POWERMGR_IDISPLAY_POWER_CALLBACK_H +#define POWERMGR_IDISPLAY_POWER_CALLBACK_H + +#include +#include + +#include "display_info.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class IDisplayPowerCallback : public IRemoteBroker { +public: + virtual void OnDisplayStateChanged(DisplayState state) = 0; + + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.powermgr.IDisplayPowerCallback"); +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // POWERMGR_IDISPLAY_POWER_CALLBACK_H \ No newline at end of file diff --git a/interfaces/innerkits/native/include/idisplay_power_mgr.h b/interfaces/innerkits/native/include/idisplay_power_mgr.h index cf2f5e9..1bae7cd 100644 --- a/interfaces/innerkits/native/include/idisplay_power_mgr.h +++ b/interfaces/innerkits/native/include/idisplay_power_mgr.h @@ -17,8 +17,10 @@ #define DISPLAYMGR_IDISPLAY_MGR_H #include +#include #include "display_info.h" +#include "idisplay_power_callback.h" namespace OHOS { namespace DisplayPowerMgr { @@ -27,16 +29,24 @@ public: enum { SET_DISPLAY_STATE = 0, GET_DISPLAY_STATE, + GET_DISPLAY_IDS, + GET_MAIN_DISPLAY_ID, SET_BRIGHTNESS, ADJUST_BRIGHTNESS, + AUTO_ADJUST_BRIGHTNESS, SET_STATE_CONFIG, + REGISTER_CALLBACK, }; virtual bool SetDisplayState(uint32_t id, DisplayState state) = 0; virtual DisplayState GetDisplayState(uint32_t id) = 0; + virtual std::vector GetDisplayIds() = 0; + virtual uint32_t GetMainDisplayId() = 0; virtual bool SetBrightness(uint32_t id, int32_t value) = 0; virtual bool AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) = 0; + virtual bool AutoAdjustBrightness(bool enable) = 0; virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) = 0; + virtual bool RegisterCallback(sptr callback) = 0; DECLARE_INTERFACE_DESCRIPTOR(u"ohos.displaypowermgr.IDisplayPowerMgr"); }; diff --git a/service/BUILD.gn b/service/BUILD.gn index abdaac4..a08cb44 100644 --- a/service/BUILD.gn +++ b/service/BUILD.gn @@ -14,7 +14,10 @@ import("//base/powermgr/display_manager/displaymgr.gni") config("displaymgr_private_config") { - include_dirs = [ "//utils/system/safwk/native/include" ] + include_dirs = [ + "//utils/system/safwk/native/include", + "//base/sensors/sensor/interfaces/native/include", + ] } config("displaymgr_public_config") { @@ -55,6 +58,7 @@ ohos_shared_library("displaymgrservice") { "ipc:ipc_core", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", + "sensor:sensor_interface_native", ] part_name = "${displaymgr_native_part_name}" diff --git a/service/native/include/display_power_mgr_service.h b/service/native/include/display_power_mgr_service.h index 96a5fc5..96e714b 100644 --- a/service/native/include/display_power_mgr_service.h +++ b/service/native/include/display_power_mgr_service.h @@ -20,21 +20,26 @@ #include #include +#include "delayed_sp_singleton.h" +#include "display_common.h" #include "display_power_mgr_stub.h" #include "screen_controller.h" -#include "display_common.h" +#include "sensor_agent.h" namespace OHOS { namespace DisplayPowerMgr { class DisplayPowerMgrService : public DisplayPowerMgrStub { public: - DisplayPowerMgrService(); - ~DisplayPowerMgrService() = default; + virtual ~DisplayPowerMgrService(); virtual bool SetDisplayState(uint32_t id, DisplayState state) override; virtual DisplayState GetDisplayState(uint32_t id) override; + virtual std::vector GetDisplayIds() override; + virtual uint32_t GetMainDisplayId() override; virtual bool SetBrightness(uint32_t id, int32_t value) override; virtual bool AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) override; + virtual bool AutoAdjustBrightness(bool enable) override; virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) override; + virtual bool RegisterCallback(sptr callback) override; virtual int32_t Dump(int32_t fd, const std::vector& args) override; class DisplaySystemAbility : public SystemAbility { @@ -64,7 +69,24 @@ public: REGISTER_SYSTEM_ABILITY_BY_ID(DisplaySystemAbility, DISPLAY_MANAGER_SERVICE_ID, true); private: + static const uint32_t AUTO_ADJUST_BRIGHTNESS_DURATION = 1500; + static const int32_t BRIGHTNESS_MORE_THEN_AMBIENT = 50; + static const int32_t BRIGHTNESS_CHANGE_MIN = 10; + static void AmbientLightCallback(SensorEvent *event); + + friend DelayedSpSingleton; + + DisplayPowerMgrService(); + void InitSensors(); + bool CalculateBrightness(int32_t scalar, int32_t& brightness); + int32_t GetBrightnessFromLightScalar(int32_t scalar); + std::map> controllerMap_; + bool supportLightSensor_ {false}; + bool autoBrightness_ {false}; + SensorUser user_; + uint32_t brightness_; + sptr callback_; }; } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/include/screen_controller.h b/service/native/include/screen_controller.h index 193ac2d..21f3c38 100644 --- a/service/native/include/screen_controller.h +++ b/service/native/include/screen_controller.h @@ -40,6 +40,7 @@ public: bool UpdateStateConfig(DisplayState state, uint32_t value); bool UpdateBrightness(uint32_t value, uint32_t duration = 0); bool IsScreenOn(); + uint32_t GetBrightness(); virtual void onStart() override; virtual void onChanged(int32_t currentValue) override; virtual void onEnd() override; diff --git a/service/native/src/display_power_mgr_service.cpp b/service/native/src/display_power_mgr_service.cpp index ef3c967..1e87742 100644 --- a/service/native/src/display_power_mgr_service.cpp +++ b/service/native/src/display_power_mgr_service.cpp @@ -16,22 +16,32 @@ #include "display_power_mgr_service.h" #include +#include #include +#include "hilog_wrapper.h" + namespace OHOS { namespace DisplayPowerMgr { DisplayPowerMgrService::DisplayPowerMgrService() { + DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService Create"); std::shared_ptr screenAction = std::make_shared(); std::vector devIds = screenAction->GetDisplayIds(); int count = devIds.size(); for (int i = 0; i < count; i++) { controllerMap_.emplace(devIds[i], std::make_shared(devIds[i], screenAction)); } + InitSensors(); +} + +DisplayPowerMgrService::~DisplayPowerMgrService() +{ } bool DisplayPowerMgrService::SetDisplayState(uint32_t id, DisplayState state) { + DISPLAY_HILOGI(MODULE_SERVICE, "SetDisplayState %{public}d, %{public}d", id, state); auto iterater = controllerMap_.find(id); if (iterater == controllerMap_.end()) { return false; @@ -41,6 +51,7 @@ bool DisplayPowerMgrService::SetDisplayState(uint32_t id, DisplayState state) DisplayState DisplayPowerMgrService::GetDisplayState(uint32_t id) { + DISPLAY_HILOGI(MODULE_SERVICE, "GetDisplayState %{public}d", id); auto iterater = controllerMap_.find(id); if (iterater == controllerMap_.end()) { return DisplayState::DISPLAY_UNKNOWN; @@ -48,8 +59,26 @@ DisplayState DisplayPowerMgrService::GetDisplayState(uint32_t id) return iterater->second->GetState(); } +std::vector DisplayPowerMgrService::GetDisplayIds() +{ + DISPLAY_HILOGI(MODULE_SERVICE, "GetDisplayIds"); + std::vector ids; + for (auto iter = controllerMap_.begin(); iter != controllerMap_.end(); iter++) { + ids.push_back(iter->first); + } + return ids; +} + +uint32_t DisplayPowerMgrService::GetMainDisplayId() +{ + DISPLAY_HILOGI(MODULE_SERVICE, "GetMainDisplayId"); + // To add modified when window manager can tell us which is the main display + return 0; +} + bool DisplayPowerMgrService::SetBrightness(uint32_t id, int32_t value) { + DISPLAY_HILOGI(MODULE_SERVICE, "SetDisplayState %{public}d, %{public}d", id, value); auto iterater = controllerMap_.find(id); if (iterater == controllerMap_.end()) { return false; @@ -59,6 +88,8 @@ bool DisplayPowerMgrService::SetBrightness(uint32_t id, int32_t value) bool DisplayPowerMgrService::AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) { + DISPLAY_HILOGI(MODULE_SERVICE, "SetDisplayState %{public}d, %{public}d, %{public}d", + id, value, duration); auto iterater = controllerMap_.find(id); if (iterater == controllerMap_.end()) { return false; @@ -66,8 +97,41 @@ bool DisplayPowerMgrService::AdjustBrightness(uint32_t id, int32_t value, uint32 return iterater->second->UpdateBrightness(value, duration); } +bool DisplayPowerMgrService::AutoAdjustBrightness(bool enable) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "AutoAdjustBrightness start"); + if (!supportLightSensor_) { + DISPLAY_HILOGI(MODULE_SERVICE, "AutoAdjustBrightness not support"); + return false; + } + if (enable) { + DISPLAY_HILOGI(MODULE_SERVICE, "AutoAdjustBrightness enable"); + if (autoBrightness_) { + DISPLAY_HILOGW(MODULE_SERVICE, "AutoAdjustBrightness is already enabled"); + return true; + } + strcpy_s(user_.name, sizeof(user_.name), "DisplayPowerMgrService"); + user_.userData = nullptr; + user_.callback = &AmbientLightCallback; + SubscribeSensor(SENSOR_TYPE_ID_AMBIENT_LIGHT, &user_); + ActivateSensor(SENSOR_TYPE_ID_AMBIENT_LIGHT, &user_); + SetMode(SENSOR_TYPE_ID_AMBIENT_LIGHT, &user_, SENSOR_ON_CHANGE); + } else { + DISPLAY_HILOGI(MODULE_SERVICE, "AutoAdjustBrightness disable"); + if (!autoBrightness_) { + DISPLAY_HILOGW(MODULE_SERVICE, "AutoAdjustBrightness is already disabled"); + return true; + } + DeactivateSensor(SENSOR_TYPE_ID_AMBIENT_LIGHT, &user_); + UnsubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &user_); + } + return true; +} + bool DisplayPowerMgrService::SetStateConfig(uint32_t id, DisplayState state, int32_t value) { + DISPLAY_HILOGI(MODULE_SERVICE, "SetStateConfig %{public}d, %{public}d, %{public}d", + id, state, value); auto iterater = controllerMap_.find(id); if (iterater == controllerMap_.end()) { return false; @@ -75,6 +139,13 @@ bool DisplayPowerMgrService::SetStateConfig(uint32_t id, DisplayState state, int return iterater->second->UpdateStateConfig(state, value); } +bool DisplayPowerMgrService::RegisterCallback(sptr callback) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "RegisterCallback"); + callback_ = callback; + return true; +} + int32_t DisplayPowerMgrService::Dump(int32_t fd, const std::vector& args) { std::string result("Empty dump info"); @@ -83,5 +154,76 @@ int32_t DisplayPowerMgrService::Dump(int32_t fd, const std::vectorsensorTypeId != SENSOR_TYPE_ID_AMBIENT_LIGHT) { + DISPLAY_HILOGI(MODULE_SERVICE, "Sensor Callback is not AMBIENT_LIGHT"); + return; + } + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + DISPLAY_HILOGI(MODULE_SERVICE, "Sensor Callback no service"); + return; + } + uint32_t mainDispId = pms->GetMainDisplayId(); + auto mainDisp = pms->controllerMap_.find(mainDispId); + if (mainDisp == pms->controllerMap_.end()) { + return; + } + AmbientLightData* data = (AmbientLightData*)event->data; + DISPLAY_HILOGI(MODULE_SERVICE, "AmbientLightCallback: %{public}d", data->scalar); + int32_t brightness = mainDisp->second->GetBrightness(); + if (pms->CalculateBrightness(data->scalar, brightness)) { + pms->AdjustBrightness(mainDispId, brightness, AUTO_ADJUST_BRIGHTNESS_DURATION); + } +} + +bool DisplayPowerMgrService::CalculateBrightness(int32_t scalar, int32_t& brightness) +{ + if (brightness <= BRIGHTNESS_MORE_THEN_AMBIENT) { + DISPLAY_HILOGI(MODULE_SERVICE, "Already in minimum brightness"); + return false; + } + int32_t change = GetBrightnessFromLightScalar(scalar); + if (abs(change - brightness) < BRIGHTNESS_CHANGE_MIN) { + DISPLAY_HILOGI(MODULE_SERVICE, "Small change, don't need to adjust brightness"); + return false; + } + brightness = change; + return true; +} + +int32_t DisplayPowerMgrService::GetBrightnessFromLightScalar(int32_t scalar) +{ + // temp solution + return scalar + BRIGHTNESS_MORE_THEN_AMBIENT; +} } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/src/display_system_ability.cpp b/service/native/src/display_system_ability.cpp index 496b357..b689b2e 100644 --- a/service/native/src/display_system_ability.cpp +++ b/service/native/src/display_system_ability.cpp @@ -23,11 +23,10 @@ REGISTER_SYSTEM_ABILITY_BY_ID(DisplaySystemAbility, DISPLAY_MANAGER_SERVICE_ID, } void DisplaySystemAbility::OnStart() { - DISPLAY_HILOGI(MODULE_SERVICE, "Start service"); - service_ = new DisplayPowerMgrService(); - if (!Publish(service_)) { - DISPLAY_HILOGE(MODULE_SERVICE, "Failed to publish service"); - } + DISPLAY_HILOGI(MODULE_SERVICE, "Start service"); + if (!Publish(DelayedSpSingleton::GetInstance())) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to publish service"); + } } void DisplaySystemAbility::OnStop() diff --git a/service/native/src/screen_controller.cpp b/service/native/src/screen_controller.cpp index d2cde6c..e38b88a 100644 --- a/service/native/src/screen_controller.cpp +++ b/service/native/src/screen_controller.cpp @@ -113,6 +113,15 @@ bool ScreenController::IsScreenOn() return (state_ == DisplayState::DISPLAY_ON || state_ == DisplayState::DISPLAY_DIM); } +uint32_t ScreenController::GetBrightness() +{ + std::lock_guard lock(mutex_); + if (brightness_ == 0) { + brightness_ = action_->GetBrightness(devId_); + } + return brightness_; +} + void ScreenController::onStart() { DISPLAY_HILOGD(MODULE_SERVICE, "ScreenAnimatorCallback onStart"); diff --git a/service/zidl/include/display_power_mgr_proxy.h b/service/zidl/include/display_power_mgr_proxy.h index e27b0d0..93da3c8 100644 --- a/service/zidl/include/display_power_mgr_proxy.h +++ b/service/zidl/include/display_power_mgr_proxy.h @@ -30,9 +30,14 @@ public: virtual bool SetDisplayState(uint32_t id, DisplayState state) override; virtual DisplayState GetDisplayState(uint32_t id) override; + virtual std::vector GetDisplayIds() override; + virtual uint32_t GetMainDisplayId() override; + virtual bool SetBrightness(uint32_t id, int32_t value) override; virtual bool AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) override; + virtual bool AutoAdjustBrightness(bool enable) override; virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) override; + virtual bool RegisterCallback(sptr callback) override; private: static inline BrokerDelegator delegator_; diff --git a/service/zidl/include/display_power_mgr_stub.h b/service/zidl/include/display_power_mgr_stub.h index 2a35edd..7268339 100644 --- a/service/zidl/include/display_power_mgr_stub.h +++ b/service/zidl/include/display_power_mgr_stub.h @@ -29,9 +29,13 @@ public: private: int32_t SetDisplayStateStub(MessageParcel& data, MessageParcel& reply); int32_t GetDisplayStateStub(MessageParcel& data, MessageParcel& reply); + int32_t GetDisplayIdsStub(MessageParcel& data, MessageParcel& reply); + int32_t GetMainDisplayIdStub(MessageParcel& data, MessageParcel& reply); int32_t SetBrightnessStub(MessageParcel& data, MessageParcel& reply); int32_t AdjustBrightnessStub(MessageParcel& data, MessageParcel& reply); + int32_t AutoAdjustBrightnessStub(MessageParcel& data, MessageParcel& reply); int32_t SetStateConfigStub(MessageParcel& data, MessageParcel& reply); + int32_t RegisterCallbackStub(MessageParcel& data, MessageParcel& reply); }; } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/zidl/src/display_power_mgr_proxy.cpp b/service/zidl/src/display_power_mgr_proxy.cpp index f0538f7..f9d2bee 100644 --- a/service/zidl/src/display_power_mgr_proxy.cpp +++ b/service/zidl/src/display_power_mgr_proxy.cpp @@ -85,6 +85,78 @@ DisplayState DisplayPowerMgrProxy::GetDisplayState(uint32_t id) return static_cast(result); } +std::vector DisplayPowerMgrProxy::GetDisplayIds() +{ + sptr remote = Remote(); + std::vector result; + + RETURN_IF_WITH_RET(remote == nullptr, result); + + uint32_t count = 0; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrClient::%{public}s write descriptor failed!", __func__); + return result; + } + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::GET_DISPLAY_IDS), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s SendRequest is failed,%d", __func__, ret); + return result; + } + + if (!reply.ReadUint32(count)) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return result; + } + + for (uint32_t i = 0; i < count; i++) { + uint32_t value; + if (reply.ReadUint32(value)) { + result.push_back(value); + } else { + DISPLAY_HILOGE(MODULE_INNERKIT, "read value fail: %{public}d", i); + } + } + + return result; +} + +uint32_t DisplayPowerMgrProxy::GetMainDisplayId() +{ + sptr remote = Remote(); + uint32_t result = 0; + + RETURN_IF_WITH_RET(remote == nullptr, result); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrClient::%{public}s write descriptor failed!", __func__); + return result; + } + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::GET_MAIN_DISPLAY_ID), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s SendRequest is failed,%d", __func__, ret); + return result; + } + + if (!reply.ReadUint32(result)) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return result; + } + + return result; +} + bool DisplayPowerMgrProxy::SetBrightness(uint32_t id, int32_t value) { sptr remote = Remote(); @@ -152,6 +224,38 @@ bool DisplayPowerMgrProxy::AdjustBrightness(uint32_t id, int32_t value, uint32_t return result; } +bool DisplayPowerMgrProxy::AutoAdjustBrightness(bool enable) +{ + 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(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s write descriptor failed!", __func__); + return result; + } + + WRITE_PARCEL_WITH_RET(data, Bool, enable, false); + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::AUTO_ADJUST_BRIGHTNESS), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s SendRequest is failed: %d", __func__, ret); + return result; + } + + if (!reply.ReadBool(result)) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return result; + } + + return result; +} + bool DisplayPowerMgrProxy::SetStateConfig(uint32_t id, DisplayState state, int32_t value) { sptr remote = Remote(); @@ -186,5 +290,37 @@ bool DisplayPowerMgrProxy::SetStateConfig(uint32_t id, DisplayState state, int32 return result; } + +bool DisplayPowerMgrProxy::RegisterCallback(sptr callback) +{ + 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(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s write descriptor failed!", __func__); + return result; + } + + WRITE_PARCEL_WITH_RET(data, RemoteObject, callback->AsObject(), false); + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::REGISTER_CALLBACK), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s SendRequest is failed: %d", __func__, ret); + return result; + } + + if (!reply.ReadBool(result)) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return result; + } + + return result; +} } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/zidl/src/display_power_mgr_stub.cpp b/service/zidl/src/display_power_mgr_stub.cpp index 8fe9b53..b709150 100644 --- a/service/zidl/src/display_power_mgr_stub.cpp +++ b/service/zidl/src/display_power_mgr_stub.cpp @@ -38,10 +38,16 @@ int32_t DisplayPowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, return SetDisplayStateStub(data, reply); case static_cast(IDisplayPowerMgr::GET_DISPLAY_STATE): return GetDisplayStateStub(data, reply); + case static_cast(IDisplayPowerMgr::GET_DISPLAY_IDS): + return GetDisplayIdsStub(data, reply); + case static_cast(IDisplayPowerMgr::GET_MAIN_DISPLAY_ID): + return GetMainDisplayIdStub(data, reply); case static_cast(IDisplayPowerMgr::SET_BRIGHTNESS): return SetBrightnessStub(data, reply); case static_cast(IDisplayPowerMgr::ADJUST_BRIGHTNESS): return AdjustBrightnessStub(data, reply); + case static_cast(IDisplayPowerMgr::AUTO_ADJUST_BRIGHTNESS): + return AutoAdjustBrightnessStub(data, reply); case static_cast(IDisplayPowerMgr::SET_STATE_CONFIG): return SetStateConfigStub(data, reply); default: @@ -59,7 +65,7 @@ int32_t DisplayPowerMgrStub::SetDisplayStateStub(MessageParcel& data, MessagePar bool ret = SetDisplayState(id, static_cast(state)); if (!reply.WriteBool(ret)) { - DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetScreenState return value"); + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetDisplayStateStub return value"); return E_WRITE_PARCEL_ERROR; } return ERR_OK; @@ -73,12 +79,36 @@ int32_t DisplayPowerMgrStub::GetDisplayStateStub(MessageParcel& data, MessagePar DisplayState ret = GetDisplayState(id); if (!reply.WriteUint32(static_cast(ret))) { - DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetScreenState return value"); + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write GetDisplayStateStub return value"); return E_WRITE_PARCEL_ERROR; } return ERR_OK; } +int32_t DisplayPowerMgrStub::GetDisplayIdsStub(MessageParcel& data, MessageParcel& reply) +{ + std::vector result = GetDisplayIds(); + if (!reply.WriteUint32(static_cast(result.size()))) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write GetDisplayIdsStub return value"); + return E_WRITE_PARCEL_ERROR; + } + for (uint32_t i = 0; i < result.size(); i++) { + if (!reply.WriteUint32(static_cast(result[i]))) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write GetDisplayIdsStub"); + } + } + return ERR_OK; +} + +int32_t DisplayPowerMgrStub::GetMainDisplayIdStub(MessageParcel& data, MessageParcel& reply) +{ + uint32_t result = GetMainDisplayId(); + if (!reply.WriteUint32(result)) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write GetMainDisplayIdStub return value"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} int32_t DisplayPowerMgrStub::SetBrightnessStub(MessageParcel& data, MessageParcel& reply) { @@ -108,7 +138,21 @@ int32_t DisplayPowerMgrStub::AdjustBrightnessStub(MessageParcel& data, MessagePa bool ret = AdjustBrightness(id, value, duration); if (!reply.WriteBool(ret)) { - DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetBrightness return value"); + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write AdjustBrightnessStub return value"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} + +int32_t DisplayPowerMgrStub::AutoAdjustBrightnessStub(MessageParcel& data, MessageParcel& reply) +{ + bool enable = 0; + + READ_PARCEL_WITH_RET(data, Bool, enable, E_READ_PARCEL_ERROR); + + bool ret = AutoAdjustBrightness(enable); + if (!reply.WriteBool(ret)) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write AutoAdjustBrightnessStub return value"); return E_WRITE_PARCEL_ERROR; } return ERR_OK; @@ -126,10 +170,20 @@ int32_t DisplayPowerMgrStub::SetStateConfigStub(MessageParcel& data, MessageParc bool ret = SetStateConfig(id, static_cast(state), value); if (!reply.WriteBool(ret)) { - DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetBrightness return value"); + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetStateConfigStub return value"); return E_WRITE_PARCEL_ERROR; } return ERR_OK; } + +int32_t DisplayPowerMgrStub::RegisterCallbackStub(MessageParcel& data, MessageParcel& reply) +{ + sptr obj = data.ReadRemoteObject(); + RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR); + sptr callback = iface_cast(obj); + RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR); + RegisterCallback(callback); + return ERR_OK; +} } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/test/native/unittest/BUILD.gn b/test/native/unittest/BUILD.gn index fd2098e..a5f28fe 100644 --- a/test/native/unittest/BUILD.gn +++ b/test/native/unittest/BUILD.gn @@ -22,6 +22,7 @@ config("module_private_config") { include_dirs = [ "include", + "//base/sensors/sensor/interfaces/native/include", "//utils/system/safwk/native/include", "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", ] @@ -51,5 +52,6 @@ ohos_unittest("unittest_display_mgr_service") { "ipc:ipc_core", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", + "sensor:sensor_interface_native", ] } diff --git a/test/native/unittest/src/display_power_mgr_service_test.cpp b/test/native/unittest/src/display_power_mgr_service_test.cpp index ff3cc9e..41024c4 100644 --- a/test/native/unittest/src/display_power_mgr_service_test.cpp +++ b/test/native/unittest/src/display_power_mgr_service_test.cpp @@ -17,6 +17,7 @@ #include #include +#include #include "display_power_mgr_client.h" #include "display_power_mgr_service.h" @@ -200,4 +201,50 @@ HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService012, TestSize.Level0) DisplayState state = DisplayPowerMgrClient::GetInstance().GetDisplayState(); EXPECT_TRUE(state == DisplayState::DISPLAY_UNKNOWN); } + +/** + * @tc.name: DisplayPowerMgrService013 + * @tc.desc: Test GetDisplayIds + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService013, TestSize.Level0) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService013: fun is start"); + std::vector ret = DisplayPowerMgrClient::GetInstance().GetDisplayIds(); + sleep(5); + EXPECT_TRUE(ret.size() != 0); +} + +/** + * @tc.name: DisplayPowerMgrService014 + * @tc.desc: Test GetDisplayIds + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService014, TestSize.Level0) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService014: fun is start"); + uint32_t ret = DisplayPowerMgrClient::GetInstance().GetMainDisplayId(); + sleep(5); + EXPECT_TRUE(ret == 0); +} + +/** + * @tc.name: DisplayPowerMgrService015 + * @tc.desc: Test GetDisplayIds + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService015, TestSize.Level0) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService015: fun is start"); + uint32_t ret = DisplayPowerMgrClient::GetInstance().AutoAdjustBrightness(true); + sleep(5); + if (ret) { + DISPLAY_HILOGI(MODULE_SERVICE, "AutoAdjustBrightness: is supported"); + ret = DisplayPowerMgrClient::GetInstance().AutoAdjustBrightness(false); + EXPECT_TRUE(ret); + } else { + DISPLAY_HILOGI(MODULE_SERVICE, "AutoAdjustBrightness: is not supported"); + EXPECT_FALSE(ret); + } +} } \ No newline at end of file diff --git a/utils/native/include/delayed_sp_singleton.h b/utils/native/include/delayed_sp_singleton.h new file mode 100644 index 0000000..3bd17a2 --- /dev/null +++ b/utils/native/include/delayed_sp_singleton.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2021 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 POWERMGR_SP_SINGLETON_H +#define POWERMGR_SP_SINGLETON_H + +#include +#include +#include + +#include "nocopyable.h" + +namespace OHOS { +namespace DisplayPowerMgr { +template +class DelayedSpSingleton : public NoCopyable { +public: + static sptr GetInstance(); + static void DestroyInstance(); + +private: + static sptr instance_; + static std::mutex mutex_; +}; + +template +sptr DelayedSpSingleton::instance_ = nullptr; + +template +std::mutex DelayedSpSingleton::mutex_; + +template +sptr DelayedSpSingleton::GetInstance() +{ + if (!instance_) { + std::lock_guard lock(mutex_); + if (instance_ == nullptr) { + instance_ = new T(); + } + } + + return instance_; +} + +template +void DelayedSpSingleton::DestroyInstance() +{ + std::lock_guard lock(mutex_); + if (instance_) { + instance_.clear(); + instance_ = nullptr; + } +} +} // namespace PowerMgr +} // namespace OHOS +#endif -- Gitee From d87114cab60d1bc151d9e79672d7a37783f76d2d Mon Sep 17 00:00:00 2001 From: Du Jiang Date: Mon, 20 Dec 2021 16:29:36 +0800 Subject: [PATCH 3/9] Add DisplayPowerMgr dump Signed-off-by: Du Jiang Change-Id: I89e59ed6077caea23c0a9586032a3dc9ba735443 --- .../include/display_power_mgr_service.h | 1 - .../native/src/display_power_mgr_service.cpp | 29 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/service/native/include/display_power_mgr_service.h b/service/native/include/display_power_mgr_service.h index 96e714b..36d1117 100644 --- a/service/native/include/display_power_mgr_service.h +++ b/service/native/include/display_power_mgr_service.h @@ -85,7 +85,6 @@ private: bool supportLightSensor_ {false}; bool autoBrightness_ {false}; SensorUser user_; - uint32_t brightness_; sptr callback_; }; } // namespace DisplayPowerMgr diff --git a/service/native/src/display_power_mgr_service.cpp b/service/native/src/display_power_mgr_service.cpp index 1e87742..a2c68df 100644 --- a/service/native/src/display_power_mgr_service.cpp +++ b/service/native/src/display_power_mgr_service.cpp @@ -30,6 +30,7 @@ DisplayPowerMgrService::DisplayPowerMgrService() std::vector devIds = screenAction->GetDisplayIds(); int count = devIds.size(); for (int i = 0; i < count; i++) { + DISPLAY_HILOGI(MODULE_SERVICE, "find display: %{public}d", devIds[i]); controllerMap_.emplace(devIds[i], std::make_shared(devIds[i], screenAction)); } InitSensors(); @@ -148,7 +149,33 @@ bool DisplayPowerMgrService::RegisterCallback(sptr callba int32_t DisplayPowerMgrService::Dump(int32_t fd, const std::vector& args) { - std::string result("Empty dump info"); + std::string result("DISPLAY POWER MANAGER DUMP:\n"); + for (auto iter = controllerMap_.begin(); iter != controllerMap_.end(); iter++) { + result.append("Display Id="); + result.append(std::to_string(iter->first)); + result.append(" State="); + result.append(std::to_string(static_cast(iter->second->GetState()))); + result.append(" Brightness="); + result.append(std::to_string(iter->second->GetBrightness())); + result.append("\n"); + } + + result.append("Support Ambient Light: "); + if (supportLightSensor_) { + result.append("TRUE"); + } else { + result.append("FALSE"); + } + result.append("\n"); + + result.append("Auto Adjust Brightness: "); + if (autoBrightness_) { + result.append("ON"); + } else { + result.append("OFF"); + } + result.append("\n"); + if (!SaveStringToFd(fd, result)) { DISPLAY_HILOGE(MODULE_SERVICE, "Failed to save dump info to fd"); } -- Gitee From 6e073d08b667c96a3f67758cb8f2c6d0e0c92038 Mon Sep 17 00:00:00 2001 From: Du Jiang Date: Tue, 21 Dec 2021 11:15:10 +0800 Subject: [PATCH 4/9] Add abnormal return when get main display failed Change-Id: I3845ac36be2863afd4059918de001f7c7939ade5 Signed-off-by: Du Jiang --- frameworks/native/display_power_mgr_client.cpp | 4 ++-- .../innerkits/native/include/display_power_mgr_client.h | 2 +- test/native/unittest/src/display_power_mgr_service_test.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/frameworks/native/display_power_mgr_client.cpp b/frameworks/native/display_power_mgr_client.cpp index 382f951..cd18d42 100644 --- a/frameworks/native/display_power_mgr_client.cpp +++ b/frameworks/native/display_power_mgr_client.cpp @@ -99,11 +99,11 @@ std::vector DisplayPowerMgrClient::GetDisplayIds() return proxy->GetDisplayIds(); } -uint32_t DisplayPowerMgrClient::GetMainDisplayId() +int32_t DisplayPowerMgrClient::GetMainDisplayId() { auto proxy = GetProxy(); if (proxy == nullptr) { - return 0; + return -1; } return proxy->GetMainDisplayId(); } diff --git a/interfaces/innerkits/native/include/display_power_mgr_client.h b/interfaces/innerkits/native/include/display_power_mgr_client.h index 0e8efc6..9e43480 100644 --- a/interfaces/innerkits/native/include/display_power_mgr_client.h +++ b/interfaces/innerkits/native/include/display_power_mgr_client.h @@ -33,7 +33,7 @@ public: bool SetDisplayState(DisplayState state, uint32_t id = 0); DisplayState GetDisplayState(uint32_t id = 0); std::vector GetDisplayIds(); - uint32_t GetMainDisplayId(); + int32_t GetMainDisplayId(); bool SetBrightness(uint32_t value, uint32_t id = 0); bool AdjustBrightness(uint32_t value, uint32_t duration, uint32_t id = 0); bool SetStateConfig(DisplayState state, uint32_t value, uint32_t id = 0); diff --git a/test/native/unittest/src/display_power_mgr_service_test.cpp b/test/native/unittest/src/display_power_mgr_service_test.cpp index 41024c4..d9c94a5 100644 --- a/test/native/unittest/src/display_power_mgr_service_test.cpp +++ b/test/native/unittest/src/display_power_mgr_service_test.cpp @@ -223,7 +223,7 @@ HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService013, TestSize.Level0) HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService014, TestSize.Level0) { DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService014: fun is start"); - uint32_t ret = DisplayPowerMgrClient::GetInstance().GetMainDisplayId(); + int32_t ret = DisplayPowerMgrClient::GetInstance().GetMainDisplayId(); sleep(5); EXPECT_TRUE(ret == 0); } -- Gitee From a5dc8095f66984f5052f996bbba13b4c447c1350 Mon Sep 17 00:00:00 2001 From: Du Jiang Date: Tue, 21 Dec 2021 16:55:54 +0800 Subject: [PATCH 5/9] Fix error when gradually adjust brightness Change-Id: Ie420906fdd431c377d5a5f4754409b062cfb8a89 Signed-off-by: Du Jiang --- service/native/include/gradual_animator.h | 2 +- service/native/include/screen_controller.h | 2 +- service/native/src/gradual_animator.cpp | 22 ++++++++++++------- service/native/src/screen_controller.cpp | 2 +- .../src/display_power_mgr_service_test.cpp | 15 ++++++++++++- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/service/native/include/gradual_animator.h b/service/native/include/gradual_animator.h index 8c1888e..4cdc131 100644 --- a/service/native/include/gradual_animator.h +++ b/service/native/include/gradual_animator.h @@ -51,7 +51,7 @@ private: }; void NextStep(); std::string name_; - std::shared_ptr callback_; + std::weak_ptr callback_; std::shared_ptr eventRunner_; std::unique_ptr handler_; bool animating_ = false; diff --git a/service/native/include/screen_controller.h b/service/native/include/screen_controller.h index 21f3c38..fa1bfae 100644 --- a/service/native/include/screen_controller.h +++ b/service/native/include/screen_controller.h @@ -53,7 +53,7 @@ private: uint32_t brightness_ {0}; std::shared_ptr action_; - std::unique_ptr animator_; + std::shared_ptr animator_; }; } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/src/gradual_animator.cpp b/service/native/src/gradual_animator.cpp index a794a25..8ec3538 100644 --- a/service/native/src/gradual_animator.cpp +++ b/service/native/src/gradual_animator.cpp @@ -45,7 +45,7 @@ void GradualAnimator::StartAnimation(int32_t from, int32_t to, uint32_t duration DISPLAY_HILOGD(MODULE_SERVICE, "StartAnimation from=%{public}d, to=%{public}d, duration=%{public}d", from, to, duration); - if (callback_ == nullptr) { + if (callback_.lock() == nullptr) { DISPLAY_HILOGW(MODULE_SERVICE, "Callback is NULL"); return; } @@ -72,11 +72,12 @@ void GradualAnimator::StopAnimation() DISPLAY_HILOGD(MODULE_SERVICE, "GradualAnimator StopAnimation start"); animating_ = false; handler_->RemoveEvent(EVENT_STEP); - if (callback_ == nullptr) { + if (callback_.lock() == nullptr) { DISPLAY_HILOGW(MODULE_SERVICE, "Callback is NULL"); return; } - callback_->onEnd(); + std::shared_ptr callback = callback_.lock(); + callback->onEnd(); DISPLAY_HILOGD(MODULE_SERVICE, "GradualAnimator StopAnimation end"); } @@ -91,22 +92,23 @@ void GradualAnimator::NextStep() DISPLAY_HILOGW(MODULE_SERVICE, "NextStep, not animating"); return; } - if (callback_ == nullptr) { + if (callback_.lock() == nullptr) { DISPLAY_HILOGW(MODULE_SERVICE, "Callback is NULL"); return; } + std::shared_ptr callback = callback_.lock(); currentStep_++; if (currentStep_ == 1) { - callback_->onStart(); + callback->onStart(); } if (currentStep_ < steps_) { current_ = current_ + stride_; - callback_->onChanged(current_); + callback->onChanged(current_); handler_->SendEvent(EVENT_STEP, 0, updateTime_); } else { current_ = to_; - callback_->onChanged(current_); - callback_->onEnd(); + callback->onChanged(current_); + callback->onEnd(); animating_ = false; } } @@ -124,6 +126,10 @@ void GradualAnimator::AnimatorHandler::ProcessEvent(const AppExecFwk::InnerEvent DISPLAY_HILOGD(MODULE_SERVICE, "AnimatorHandler::%{public}s ,eventid = %d", __func__, event->GetInnerEventId()); std::shared_ptr animator = owner_.lock(); + if (animator == nullptr) { + DISPLAY_HILOGD(MODULE_SERVICE, "AnimatorHandler no object"); + return; + } switch (event->GetInnerEventId()) { case EVENT_STEP: { animator->NextStep(); diff --git a/service/native/src/screen_controller.cpp b/service/native/src/screen_controller.cpp index e38b88a..16b2ce9 100644 --- a/service/native/src/screen_controller.cpp +++ b/service/native/src/screen_controller.cpp @@ -71,7 +71,7 @@ bool ScreenController::UpdateBrightness(uint32_t value, uint32_t duraion) if (animator_ == nullptr) { std::string name = "ScreenController_" + std::to_string(devId_); std::shared_ptr callback = shared_from_this(); - animator_ = std::make_unique(name, callback); + animator_ = std::make_shared(name, callback); } if (animator_->IsAnimating()) { animator_->StopAnimation(); diff --git a/test/native/unittest/src/display_power_mgr_service_test.cpp b/test/native/unittest/src/display_power_mgr_service_test.cpp index d9c94a5..9627991 100644 --- a/test/native/unittest/src/display_power_mgr_service_test.cpp +++ b/test/native/unittest/src/display_power_mgr_service_test.cpp @@ -247,4 +247,17 @@ HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService015, TestSize.Level0) EXPECT_FALSE(ret); } } -} \ No newline at end of file + +/** + * @tc.name: DisplayPowerMgrService016 + * @tc.desc: Test GetDisplayIds + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService016, TestSize.Level0) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService016: fun is start"); + int32_t ret = DisplayPowerMgrClient::GetInstance().AdjustBrightness(0, 1); + sleep(5); + EXPECT_TRUE(ret == 0); +} +} -- Gitee From c62645ca1244dab59577267a4fe0fba0ec33bc56 Mon Sep 17 00:00:00 2001 From: Du Jiang Date: Thu, 23 Dec 2021 10:09:06 +0800 Subject: [PATCH 6/9] Fix send event issue Change-Id: I7148d966e8abf119a69ab9b065278210474bd5fa Signed-off-by: Du Jiang --- service/native/include/gradual_animator.h | 6 +++--- service/native/include/screen_controller.h | 6 +++--- service/native/src/gradual_animator.cpp | 19 ++++++++++--------- service/native/src/screen_controller.cpp | 12 ++++++------ 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/service/native/include/gradual_animator.h b/service/native/include/gradual_animator.h index 4cdc131..4af2c1e 100644 --- a/service/native/include/gradual_animator.h +++ b/service/native/include/gradual_animator.h @@ -25,9 +25,9 @@ namespace OHOS { namespace DisplayPowerMgr { class AnimateCallback { public: - virtual void onStart() = 0; - virtual void onChanged(int32_t currentValue) = 0; - virtual void onEnd() = 0; + virtual void OnStart() = 0; + virtual void OnChanged(int32_t currentValue) = 0; + virtual void OnEnd() = 0; }; class GradualAnimator : public std::enable_shared_from_this { diff --git a/service/native/include/screen_controller.h b/service/native/include/screen_controller.h index fa1bfae..1d46537 100644 --- a/service/native/include/screen_controller.h +++ b/service/native/include/screen_controller.h @@ -41,9 +41,9 @@ public: bool UpdateBrightness(uint32_t value, uint32_t duration = 0); bool IsScreenOn(); uint32_t GetBrightness(); - virtual void onStart() override; - virtual void onChanged(int32_t currentValue) override; - virtual void onEnd() override; + virtual void OnStart() override; + virtual void OnChanged(int32_t currentValue) override; + virtual void OnEnd() override; private: static const uint32_t SCREEN_BRIGHTNESS_UPDATE_DURATION = 200; std::mutex mutex_; diff --git a/service/native/src/gradual_animator.cpp b/service/native/src/gradual_animator.cpp index 8ec3538..0289c34 100644 --- a/service/native/src/gradual_animator.cpp +++ b/service/native/src/gradual_animator.cpp @@ -25,10 +25,6 @@ GradualAnimator::GradualAnimator(const std::string& name, DISPLAY_HILOGD(MODULE_SERVICE, "GradualAnimator construct start"); name_ = name; callback_ = callback; - eventRunner_ = AppExecFwk::EventRunner::Create(name_); - if (eventRunner_ == nullptr) { - DISPLAY_HILOGW(MODULE_SERVICE, "GradualAnimator failed due to create EventRunner"); - } from_ = 0; to_ = 0; current_ = 0; @@ -60,6 +56,11 @@ void GradualAnimator::StartAnimation(int32_t from, int32_t to, uint32_t duration stride_ = (to_ - from_) / steps_; currentStep_ = 0; if (handler_ == nullptr) { + eventRunner_ = AppExecFwk::EventRunner::Create(name_); + if (eventRunner_ == nullptr) { + DISPLAY_HILOGW(MODULE_SERVICE, "GradualAnimator failed due to create EventRunner"); + return; + } handler_ = std::make_unique(eventRunner_, shared_from_this()); } animating_ = true; @@ -77,7 +78,7 @@ void GradualAnimator::StopAnimation() return; } std::shared_ptr callback = callback_.lock(); - callback->onEnd(); + callback->OnEnd(); DISPLAY_HILOGD(MODULE_SERVICE, "GradualAnimator StopAnimation end"); } @@ -99,16 +100,16 @@ void GradualAnimator::NextStep() std::shared_ptr callback = callback_.lock(); currentStep_++; if (currentStep_ == 1) { - callback->onStart(); + callback->OnStart(); } if (currentStep_ < steps_) { current_ = current_ + stride_; - callback->onChanged(current_); + callback->OnChanged(current_); handler_->SendEvent(EVENT_STEP, 0, updateTime_); } else { current_ = to_; - callback->onChanged(current_); - callback->onEnd(); + callback->OnChanged(current_); + callback->OnEnd(); animating_ = false; } } diff --git a/service/native/src/screen_controller.cpp b/service/native/src/screen_controller.cpp index 16b2ce9..286fa2f 100644 --- a/service/native/src/screen_controller.cpp +++ b/service/native/src/screen_controller.cpp @@ -66,8 +66,8 @@ bool ScreenController::UpdateState(DisplayState state) bool ScreenController::UpdateBrightness(uint32_t value, uint32_t duraion) { std::lock_guard lock(mutex_); - DISPLAY_HILOGI(MODULE_SERVICE, "ScreenController UpdateState: %{public}d, %{public}d", - devId_, value); + DISPLAY_HILOGI(MODULE_SERVICE, "ScreenController UpdateState: %{public}d, %{public}d, %{public}d", + devId_, value, duraion); if (animator_ == nullptr) { std::string name = "ScreenController_" + std::to_string(devId_); std::shared_ptr callback = shared_from_this(); @@ -88,7 +88,7 @@ bool ScreenController::UpdateBrightness(uint32_t value, uint32_t duraion) } else { DISPLAY_HILOGI(MODULE_SERVICE, "Update brightness falied! %{public}d", value); } - + return ret; } @@ -122,12 +122,12 @@ uint32_t ScreenController::GetBrightness() return brightness_; } -void ScreenController::onStart() +void ScreenController::OnStart() { DISPLAY_HILOGD(MODULE_SERVICE, "ScreenAnimatorCallback onStart"); } -void ScreenController::onChanged(int32_t currentValue) +void ScreenController::OnChanged(int32_t currentValue) { brightness_ = currentValue; bool ret = action_->SetBrightness(devId_, currentValue); @@ -138,7 +138,7 @@ void ScreenController::onChanged(int32_t currentValue) } } -void ScreenController::onEnd() +void ScreenController::OnEnd() { DISPLAY_HILOGD(MODULE_SERVICE, "ScreenAnimatorCallback OnEnd"); } -- Gitee From 03297e9676f9f2a7b687259e48fc6094d74e1e0a Mon Sep 17 00:00:00 2001 From: Du Jiang Date: Fri, 24 Dec 2021 14:19:38 +0800 Subject: [PATCH 7/9] Fix wrong duration and test case for adjust brightness Change-Id: I102c38d0783ed682b58978a9e418d8d74a732293 Signed-off-by: Du Jiang --- service/native/include/gradual_animator.h | 2 +- service/native/src/gradual_animator.cpp | 2 +- service/native/src/screen_controller.cpp | 4 ++-- .../native/unittest/src/display_power_mgr_service_test.cpp | 7 ++++--- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/service/native/include/gradual_animator.h b/service/native/include/gradual_animator.h index 4af2c1e..8f1d54c 100644 --- a/service/native/include/gradual_animator.h +++ b/service/native/include/gradual_animator.h @@ -53,7 +53,7 @@ private: std::string name_; std::weak_ptr callback_; std::shared_ptr eventRunner_; - std::unique_ptr handler_; + std::shared_ptr handler_; bool animating_ = false; int32_t from_; int32_t to_; diff --git a/service/native/src/gradual_animator.cpp b/service/native/src/gradual_animator.cpp index 0289c34..9f16ac1 100644 --- a/service/native/src/gradual_animator.cpp +++ b/service/native/src/gradual_animator.cpp @@ -61,7 +61,7 @@ void GradualAnimator::StartAnimation(int32_t from, int32_t to, uint32_t duration DISPLAY_HILOGW(MODULE_SERVICE, "GradualAnimator failed due to create EventRunner"); return; } - handler_ = std::make_unique(eventRunner_, shared_from_this()); + handler_ = std::make_shared(eventRunner_, shared_from_this()); } animating_ = true; handler_->SendEvent(EVENT_STEP, 0, updateTime_); diff --git a/service/native/src/screen_controller.cpp b/service/native/src/screen_controller.cpp index 286fa2f..3e536e8 100644 --- a/service/native/src/screen_controller.cpp +++ b/service/native/src/screen_controller.cpp @@ -66,7 +66,7 @@ bool ScreenController::UpdateState(DisplayState state) bool ScreenController::UpdateBrightness(uint32_t value, uint32_t duraion) { std::lock_guard lock(mutex_); - DISPLAY_HILOGI(MODULE_SERVICE, "ScreenController UpdateState: %{public}d, %{public}d, %{public}d", + DISPLAY_HILOGI(MODULE_SERVICE, "ScreenController UpdateBrightness: %{public}d, %{public}d, %{public}d", devId_, value, duraion); if (animator_ == nullptr) { std::string name = "ScreenController_" + std::to_string(devId_); @@ -78,7 +78,7 @@ bool ScreenController::UpdateBrightness(uint32_t value, uint32_t duraion) } if (duraion > 0) { DISPLAY_HILOGI(MODULE_SERVICE, "UpdateState gradually"); - animator_->StartAnimation(brightness_, value, SCREEN_BRIGHTNESS_UPDATE_DURATION); + animator_->StartAnimation(brightness_, value, duraion); return true; } bool ret = action_->SetBrightness(devId_, value); diff --git a/test/native/unittest/src/display_power_mgr_service_test.cpp b/test/native/unittest/src/display_power_mgr_service_test.cpp index 9627991..20461d0 100644 --- a/test/native/unittest/src/display_power_mgr_service_test.cpp +++ b/test/native/unittest/src/display_power_mgr_service_test.cpp @@ -236,7 +236,7 @@ HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService014, TestSize.Level0) HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService015, TestSize.Level0) { DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService015: fun is start"); - uint32_t ret = DisplayPowerMgrClient::GetInstance().AutoAdjustBrightness(true); + bool ret = DisplayPowerMgrClient::GetInstance().AutoAdjustBrightness(true); sleep(5); if (ret) { DISPLAY_HILOGI(MODULE_SERVICE, "AutoAdjustBrightness: is supported"); @@ -256,8 +256,9 @@ HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService015, TestSize.Level0) HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService016, TestSize.Level0) { DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService016: fun is start"); - int32_t ret = DisplayPowerMgrClient::GetInstance().AdjustBrightness(0, 1); + bool ret = DisplayPowerMgrClient::GetInstance().AdjustBrightness(0, 3000); sleep(5); - EXPECT_TRUE(ret == 0); + EXPECT_TRUE(ret); + DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService016: fun is end"); } } -- Gitee From 69c679f824008fb24874466abc421e2436afcf9f Mon Sep 17 00:00:00 2001 From: Du Jiang Date: Mon, 27 Dec 2021 10:25:55 +0800 Subject: [PATCH 8/9] Fix wrong stride when change brightness gradually Signed-off-by: Du Jiang Change-Id: I160e79c0482cadca7f5f738df8fba453bafaf3b6 --- service/native/src/gradual_animator.cpp | 4 +++- service/native/src/screen_controller.cpp | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/service/native/src/gradual_animator.cpp b/service/native/src/gradual_animator.cpp index 9f16ac1..32614d1 100644 --- a/service/native/src/gradual_animator.cpp +++ b/service/native/src/gradual_animator.cpp @@ -53,7 +53,7 @@ void GradualAnimator::StartAnimation(int32_t from, int32_t to, uint32_t duration if (steps_ < 1) { steps_ = 1; } - stride_ = (to_ - from_) / steps_; + stride_ = (to_ - from_) / static_cast(steps_); currentStep_ = 0; if (handler_ == nullptr) { eventRunner_ = AppExecFwk::EventRunner::Create(name_); @@ -112,6 +112,8 @@ void GradualAnimator::NextStep() callback->OnEnd(); animating_ = false; } + DISPLAY_HILOGD(MODULE_SERVICE, "NextStep: Step=%{public}d, current=%{public}d, stride=%{public}d", + currentStep_, current_, stride_); } GradualAnimator::AnimatorHandler::AnimatorHandler( diff --git a/service/native/src/screen_controller.cpp b/service/native/src/screen_controller.cpp index 3e536e8..39631b1 100644 --- a/service/native/src/screen_controller.cpp +++ b/service/native/src/screen_controller.cpp @@ -129,12 +129,12 @@ void ScreenController::OnStart() void ScreenController::OnChanged(int32_t currentValue) { - brightness_ = currentValue; - bool ret = action_->SetBrightness(devId_, currentValue); + brightness_ = static_cast(currentValue); + bool ret = action_->SetBrightness(devId_, brightness_); if (ret) { - DISPLAY_HILOGD(MODULE_SERVICE, "Update brightness to %{public}d", currentValue); + DISPLAY_HILOGD(MODULE_SERVICE, "Update brightness to %{public}d", brightness_); } else { - DISPLAY_HILOGD(MODULE_SERVICE, "Update brightness falied! %{public}d", currentValue); + DISPLAY_HILOGD(MODULE_SERVICE, "Update brightness falied! %{public}d", brightness_); } } -- Gitee From 9566fa6a7f48b8f8c219cfb38de381ec9fd0ff4b Mon Sep 17 00:00:00 2001 From: Du Jiang Date: Thu, 30 Dec 2021 17:49:58 +0800 Subject: [PATCH 9/9] Add algorithm to calculate brightness from lux Signed-off-by: Du Jiang Change-Id: I0255eda6b99af86219fcd452be1ddc4355e78e00 --- .../include/display_power_mgr_service.h | 22 +++-- .../native/src/display_power_mgr_service.cpp | 80 +++++++++++++++++-- 2 files changed, 89 insertions(+), 13 deletions(-) diff --git a/service/native/include/display_power_mgr_service.h b/service/native/include/display_power_mgr_service.h index 36d1117..15483de 100644 --- a/service/native/include/display_power_mgr_service.h +++ b/service/native/include/display_power_mgr_service.h @@ -69,23 +69,35 @@ public: REGISTER_SYSTEM_ABILITY_BY_ID(DisplaySystemAbility, DISPLAY_MANAGER_SERVICE_ID, true); private: - static const uint32_t AUTO_ADJUST_BRIGHTNESS_DURATION = 1500; - static const int32_t BRIGHTNESS_MORE_THEN_AMBIENT = 50; - static const int32_t BRIGHTNESS_CHANGE_MIN = 10; + static const uint32_t AUTO_ADJUST_BRIGHTNESS_DURATION = 1000; + static const int32_t BRIGHTNESS_CHANGE_MIN = 5; + static const int32_t LUX_TO_NIT_SQRT_RADIO = 5; + static const time_t LUX_STABLE_TIME = 1100 ; + static constexpr float LUX_CHANGE_RATE_THRESHOLD = 10.0; + static constexpr float LUX_CHANGE_STABLE_MIN = 100.0; + static const int32_t NIT_MIN = 2; + static const int32_t NIT_MAX = 450; + static const int32_t BRIGHTNESS_MIN = 1; + static const int32_t BRIGHTNESS_MAX = 255; static void AmbientLightCallback(SensorEvent *event); friend DelayedSpSingleton; DisplayPowerMgrService(); void InitSensors(); - bool CalculateBrightness(int32_t scalar, int32_t& brightness); - int32_t GetBrightnessFromLightScalar(int32_t scalar); + bool IsChangedLux(float scalar); + bool CalculateBrightness(float scalar, int32_t& brightness); + int32_t GetBrightnessFromLightScalar(float scalar); std::map> controllerMap_; bool supportLightSensor_ {false}; bool autoBrightness_ {false}; SensorUser user_; sptr callback_; + + time_t lastLuxTime_ {0}; + float lastLux_ {0}; + bool luxChanged_ {false}; }; } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/src/display_power_mgr_service.cpp b/service/native/src/display_power_mgr_service.cpp index a2c68df..72df371 100644 --- a/service/native/src/display_power_mgr_service.cpp +++ b/service/native/src/display_power_mgr_service.cpp @@ -225,17 +225,68 @@ void DisplayPowerMgrService::AmbientLightCallback(SensorEvent *event) return; } AmbientLightData* data = (AmbientLightData*)event->data; - DISPLAY_HILOGI(MODULE_SERVICE, "AmbientLightCallback: %{public}d", data->scalar); + DISPLAY_HILOGI(MODULE_SERVICE, "AmbientLightCallback: %{public}f", data->intensity); int32_t brightness = mainDisp->second->GetBrightness(); - if (pms->CalculateBrightness(data->scalar, brightness)) { + if (pms->CalculateBrightness(data->intensity, brightness)) { pms->AdjustBrightness(mainDispId, brightness, AUTO_ADJUST_BRIGHTNESS_DURATION); } } -bool DisplayPowerMgrService::CalculateBrightness(int32_t scalar, int32_t& brightness) +bool DisplayPowerMgrService::IsChangedLux(float scalar) { - if (brightness <= BRIGHTNESS_MORE_THEN_AMBIENT) { - DISPLAY_HILOGI(MODULE_SERVICE, "Already in minimum brightness"); + DISPLAY_HILOGI(MODULE_SERVICE, "IsChangedLux: (%{public}d), %{public}f vs %{public}f", + luxChanged_, lastLux_, scalar); + + if (lastLuxTime_ <= 0) { + DISPLAY_HILOGI(MODULE_SERVICE, "IsChangedLux: receive lux at first time"); + lastLuxTime_ = time(0); + lastLux_ = scalar; + luxChanged_ = true; + return false; + } + + if (!luxChanged_) { + float luxChangeMin = (lastLux_ / LUX_CHANGE_RATE_THRESHOLD) + 1; + if (abs(scalar - lastLux_) < luxChangeMin) { + DISPLAY_HILOGI(MODULE_SERVICE, "IsChangedLux: Too little change"); + return false; + } else { + DISPLAY_HILOGI(MODULE_SERVICE, "IsChangedLux: First time to change, wait for stable"); + lastLuxTime_ = time(0); + lastLux_ = scalar; + luxChanged_ = true; + return false; + } + } else { + float luxChangeMin = (lastLux_ / LUX_CHANGE_RATE_THRESHOLD) + 1; + if (luxChangeMin < LUX_CHANGE_STABLE_MIN) { + luxChangeMin = LUX_CHANGE_STABLE_MIN; + } + if (abs(scalar - lastLux_) <= luxChangeMin) { + DISPLAY_HILOGI(MODULE_SERVICE, "IsChangedLux: stable lux"); + if (time(0) - lastLuxTime_ >= LUX_STABLE_TIME) { + DISPLAY_HILOGI(MODULE_SERVICE, "IsChangedLux: stable enought to change"); + lastLuxTime_ = time(0); + lastLux_ = scalar; + luxChanged_ = false; + return true; + } + } else { + DISPLAY_HILOGI(MODULE_SERVICE, "IsChangedLux: unstable lux, wait for stable"); + lastLuxTime_ = time(0); + lastLux_ = scalar; + luxChanged_ = true; + return false; + } + } + + return false; +} + +bool DisplayPowerMgrService::CalculateBrightness(float scalar, int32_t& brightness) +{ + if (!IsChangedLux(scalar)) { + DISPLAY_HILOGI(MODULE_SERVICE, "Lux is not change"); return false; } int32_t change = GetBrightnessFromLightScalar(scalar); @@ -247,10 +298,23 @@ bool DisplayPowerMgrService::CalculateBrightness(int32_t scalar, int32_t& bright return true; } -int32_t DisplayPowerMgrService::GetBrightnessFromLightScalar(int32_t scalar) +int32_t DisplayPowerMgrService::GetBrightnessFromLightScalar(float scalar) { - // temp solution - return scalar + BRIGHTNESS_MORE_THEN_AMBIENT; + DISPLAY_HILOGI(MODULE_SERVICE, "GetBrightnessFromLightScalar: %{public}f", scalar); + // use simple quadratic equation (lux = (nit / 5) ^ 2) to calculate nit + int32_t nit = static_cast(5 * sqrt(scalar)); + if (nit < NIT_MIN) { + nit = NIT_MIN; + } else if (nit > NIT_MAX) { + nit = NIT_MAX; + } + DISPLAY_HILOGI(MODULE_SERVICE, "nit: %{public}d", nit); + + int32_t brightness = BRIGHTNESS_MIN + + ((BRIGHTNESS_MAX - BRIGHTNESS_MIN) * (nit - NIT_MIN) / (NIT_MAX - NIT_MIN)); + DISPLAY_HILOGI(MODULE_SERVICE, "brightness: %{public}d", brightness); + + return brightness; } } // namespace DisplayPowerMgr } // namespace OHOS -- Gitee