diff --git a/frameworks/native/display_power_mgr_client.cpp b/frameworks/native/display_power_mgr_client.cpp index 024b58264720dec4da44bb97a182b26672d02e18..b4f142b98a863487f3096d04683684be9bfe0f72 100644 --- a/frameworks/native/display_power_mgr_client.cpp +++ b/frameworks/native/display_power_mgr_client.cpp @@ -217,5 +217,19 @@ bool DisplayPowerMgrClient::RegisterCallback(sptr callbac } return proxy->RegisterCallback(callback); } + +bool DisplayPowerMgrClient::BoostBrightness(int32_t timeoutMs, uint32_t displayId) +{ + auto proxy = GetProxy(); + RETURN_IF_WITH_RET(proxy == nullptr, false); + return proxy->BoostBrightness(timeoutMs, displayId); +} + +bool DisplayPowerMgrClient::CancelBoostBrightness(uint32_t displayId) +{ + auto proxy = GetProxy(); + RETURN_IF_WITH_RET(proxy == nullptr, false); + return proxy->CancelBoostBrightness(displayId); +} } // 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 976045e6eac5c4decec279417f4fac1eca34a8e1..58b3980fe37edbaa82cb32df9997c466b7f22069 100644 --- a/interfaces/innerkits/native/include/display_power_mgr_client.h +++ b/interfaces/innerkits/native/include/display_power_mgr_client.h @@ -49,6 +49,8 @@ public: bool AutoAdjustBrightness(bool enable); bool IsAutoAdjustBrightness(); bool RegisterCallback(sptr callback); + bool BoostBrightness(int32_t timeoutMs, uint32_t displayId = 0); + bool CancelBoostBrightness(uint32_t displayId = 0); private: class DisplayDeathRecipient : public IRemoteObject::DeathRecipient { diff --git a/interfaces/innerkits/native/include/idisplay_power_mgr.h b/interfaces/innerkits/native/include/idisplay_power_mgr.h index bba82fe9d3efccbf1cb03774e10a9c397ee070c9..2f9fc45f14b78ac56ef9a07adc46e1731b90e2f3 100644 --- a/interfaces/innerkits/native/include/idisplay_power_mgr.h +++ b/interfaces/innerkits/native/include/idisplay_power_mgr.h @@ -43,6 +43,8 @@ public: IS_AUTO_ADJUST_BRIGHTNESS, SET_STATE_CONFIG, REGISTER_CALLBACK, + BOOST_BRIGHTNESS, + CANCEL_BOOST_BRIGHTNESS }; virtual bool SetDisplayState(uint32_t id, DisplayState state, uint32_t reason) = 0; @@ -61,6 +63,8 @@ public: virtual bool IsAutoAdjustBrightness() = 0; virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) = 0; virtual bool RegisterCallback(sptr callback) = 0; + virtual bool BoostBrightness(int32_t timeoutMs, uint32_t displayId) = 0; + virtual bool CancelBoostBrightness(uint32_t displayId) = 0; DECLARE_INTERFACE_DESCRIPTOR(u"ohos.displaypowermgr.IDisplayPowerMgr"); }; diff --git a/service/BUILD.gn b/service/BUILD.gn index f44b1822442ba3cc9e0c20149c9abcf81001c03e..d8ce8e9174f53479586e356795c14769c86d7d28 100644 --- a/service/BUILD.gn +++ b/service/BUILD.gn @@ -32,6 +32,7 @@ config("displaymgr_public_config") { ohos_shared_library("displaymgrservice") { sources = [ + "native/src/display_event_handler.cpp", "native/src/display_param_helper.cpp", "native/src/display_power_mgr_service.cpp", "native/src/display_system_ability.cpp", diff --git a/service/native/include/display_event_handler.h b/service/native/include/display_event_handler.h new file mode 100644 index 0000000000000000000000000000000000000000..b280d1532aec20f410320c5db9e9fe56f729c211 --- /dev/null +++ b/service/native/include/display_event_handler.h @@ -0,0 +1,45 @@ +/* + * 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_EVENT_HANDLER_H +#define DISPLAYMGR_DISPLAY_EVENT_HANDLER_H + +#include +#include +#include +#include + +namespace OHOS { +namespace DisplayPowerMgr { +class DisplayPowerMgrService; +class DisplayEventHandler : public AppExecFwk::EventHandler { +public: + typedef std::function EventCallback; + enum Event { + EVENT_CANCEL_BOOST_BRIGHTNESS + }; + DisplayEventHandler(const std::shared_ptr& runner, + const wptr& parent); + ~DisplayEventHandler() = default; + void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override; + void EmplaceCallBack(DisplayEventHandler::Event event, EventCallback& callback); + +private: + wptr parent_; + std::map eventCallbackMap_; +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // DISPLAYMGR_DISPLAY_EVENT_HANDLER_H \ No newline at end of file diff --git a/service/native/include/display_power_mgr_service.h b/service/native/include/display_power_mgr_service.h index 9bf6c174eceece1b5326dbe52dd8d38c7bc99b0d..b71f3cc18d78efd1f0c144639bd5ce858dc8b00b 100644 --- a/service/native/include/display_power_mgr_service.h +++ b/service/native/include/display_power_mgr_service.h @@ -21,6 +21,7 @@ #include #include "delayed_sp_singleton.h" +#include "display_event_handler.h" #include "display_common.h" #include "display_power_mgr_stub.h" #include "screen_controller.h" @@ -30,7 +31,7 @@ namespace OHOS { namespace DisplayPowerMgr { class DisplayPowerMgrService : public DisplayPowerMgrStub { public: - virtual ~DisplayPowerMgrService(); + virtual ~DisplayPowerMgrService() = default; virtual bool SetDisplayState(uint32_t id, DisplayState state, uint32_t reason) override; virtual DisplayState GetDisplayState(uint32_t id) override; virtual std::vector GetDisplayIds() override; @@ -47,8 +48,11 @@ public: virtual bool IsAutoAdjustBrightness() override; virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) override; virtual bool RegisterCallback(sptr callback) override; + virtual bool BoostBrightness(int32_t timeoutMs, uint32_t displayId) override; + virtual bool CancelBoostBrightness(uint32_t displayId) override; virtual int32_t Dump(int32_t fd, const std::vector& args) override; void NotifyStateChangeCallback(uint32_t displayId, DisplayState state); + void Init(); private: class CallbackDeathRecipient : public IRemoteObject::DeathRecipient { @@ -82,8 +86,6 @@ private: void ActivateAmbientSensor(); void DeactivateAmbientSensor(); - std::shared_ptr action_; - std::map> controllerMap_; bool supportLightSensor_ {false}; bool autoBrightness_ {false}; @@ -91,6 +93,8 @@ private: SensorUser user_; sptr callback_; sptr cbDeathRecipient_; + std::shared_ptr eventRunner_ { nullptr }; + std::shared_ptr handler_ { nullptr }; time_t lastLuxTime_ {0}; float lastLux_ {0}; diff --git a/service/native/include/gradual_animator.h b/service/native/include/gradual_animator.h index 9300cb1944ce0e64d6bcf0b9ef9ed40ffafc036b..7f9a680a250bbd42476a007970b804d28d3a7619 100644 --- a/service/native/include/gradual_animator.h +++ b/service/native/include/gradual_animator.h @@ -25,6 +25,7 @@ namespace OHOS { namespace DisplayPowerMgr { class AnimateCallback { public: + virtual ~AnimateCallback() = default; virtual void OnStart() = 0; virtual void OnChanged(uint32_t currentValue) = 0; virtual void OnEnd() = 0; @@ -32,8 +33,8 @@ public: class GradualAnimator : public std::enable_shared_from_this { public: - GradualAnimator(const std::string& name, const std::shared_ptr& callback); - ~GradualAnimator() = default; + GradualAnimator(const std::string& name, std::shared_ptr& callback); + virtual ~GradualAnimator() = default; void StartAnimation(uint32_t from, uint32_t to, uint32_t duration); void StopAnimation(); bool IsAnimating() const; @@ -51,9 +52,9 @@ private: }; void NextStep(); std::string name_; - std::weak_ptr callback_; std::shared_ptr eventRunner_; std::shared_ptr handler_; + std::shared_ptr callback_; bool animating_ = false; uint32_t fromBrightness_; uint32_t toBrightness_; diff --git a/service/native/include/screen_action.h b/service/native/include/screen_action.h index dfa93bf0d8ed6e4649f571d27d806b033b9396ba..03876cdd10e12ea18220a1cf67147aa1ce9d5041 100644 --- a/service/native/include/screen_action.h +++ b/service/native/include/screen_action.h @@ -28,19 +28,23 @@ namespace OHOS { namespace DisplayPowerMgr { class ScreenAction { public: - ScreenAction(); + ScreenAction(uint32_t displayId); ~ScreenAction() = default; - uint32_t GetDefaultDisplayId(); - std::vector GetDisplayIds(); - DisplayState GetPowerState(uint32_t displayId); - bool SetDisplayState(uint32_t displayId, DisplayState state, const std::function &callback); - bool SetDisplayPower(uint32_t displayId, DisplayState state, uint32_t reason); - uint32_t GetBrightness(uint32_t displayId); - bool SetBrightness(uint32_t displayId, uint32_t value); + + static uint32_t GetDefaultDisplayId(); + static std::vector GetAllDisplayId(); + + uint32_t GetDisplayId(); + DisplayState GetPowerState(); + bool SetDisplayState(DisplayState state, const std::function& callback); + bool SetDisplayPower(DisplayState state, uint32_t reason); + uint32_t GetBrightness(); + bool SetBrightness(uint32_t value); private: static constexpr uint32_t DEFAULT_DISPLAY_ID = 0; - std::vector displayIds_; + uint32_t brightness_ { 0 }; + uint32_t displayId_ { DEFAULT_DISPLAY_ID }; }; } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/include/screen_controller.h b/service/native/include/screen_controller.h index c05c18d7c8cb7d1c0de7e43ef60bfae9841c1a27..02c6ad808aff7357cd915dfb855ecacdadcc333a 100644 --- a/service/native/include/screen_controller.h +++ b/service/native/include/screen_controller.h @@ -16,56 +16,116 @@ #ifndef DISPLAYMGR_SCREEN_CONTROLLER_H #define DISPLAYMGR_SCREEN_CONTROLLER_H +#include #include +#include #include +#include "display_event_handler.h" #include "display_power_info.h" #include "gradual_animator.h" #include "screen_action.h" namespace OHOS { namespace DisplayPowerMgr { -class ScreenController : - public AnimateCallback, - public std::enable_shared_from_this { +class ScreenController { public: - ScreenController(uint32_t displayId, std::shared_ptr action); + ScreenController(uint32_t displayId, const std::shared_ptr& handler); virtual ~ScreenController() = default; - DisplayState GetState() - { - return state_; + class AnimateController : public AnimateCallback { + public: + AnimateController (const std::shared_ptr& action); + virtual ~AnimateController () = default; + virtual void OnStart() override; + virtual void OnChanged(uint32_t currentValue) override; + virtual void OnEnd() override; + private: + const std::shared_ptr& action_; + }; + + class SharedController { + public: + SharedController(const uint32_t displayId); + bool SetBrightness(uint32_t value, uint32_t gradualDuration = 0); + uint32_t GetBrightness(); + void AllowAdjustBrightness(bool allow); + std::shared_ptr& GetAction(); + bool UpdateBrightness(uint32_t value, uint32_t gradualDuration); + + private: + std::mutex mutex_; + std::atomic isAllow_ { true }; + std::shared_ptr action_ { nullptr }; + std::shared_ptr animator_; }; - bool UpdateState(DisplayState state, uint32_t reason); - bool UpdateStateConfig(DisplayState state, uint32_t value); - bool SetBrightness(uint32_t value, uint32_t gradualDuration = 0); - bool OverrideBrightness(uint32_t value, uint32_t gradualDuration = SCREEN_BRIGHTNESS_UPDATE_DURATION); - bool RestoreBrightness(uint32_t gradualDuration = SCREEN_BRIGHTNESS_UPDATE_DURATION); - uint32_t GetBrightness(); - bool IsScreenOn(); - bool IsBrightnessOverride() const; - uint32_t GetBeforeOverrideBrightness() const; - virtual void OnStart() override; - virtual void OnChanged(uint32_t currentValue) override; - virtual void OnEnd() override; + + class DisplayStateController { + public: + DisplayStateController(const std::shared_ptr& sharedControl); + DisplayState GetState() + { + return state_; + } + bool UpdateState(DisplayState state, uint32_t reason); + bool UpdateStateConfig(DisplayState state, uint32_t value); + bool IsScreenOn(); + void UpdateBeforeOffBrightness(uint32_t brightness); + + private: + void BeforeScreenOff(DisplayState state); + void AfterScreenOn(DisplayState state); + void OnStateChanged(DisplayState state); + uint32_t beforeOffBrightness_ { 0 }; + DisplayState state_; + uint32_t stateChangeReason_ { 0 }; + std::mutex mutexState_; + std::map stateValues_; + const std::shared_ptr& sharedControl_; + }; + + class OverrideController { + public: + OverrideController(const std::shared_ptr& sharedControl, + const std::shared_ptr& stateControl, + const std::shared_ptr& handler); + bool OverrideBrightness(uint32_t value, uint32_t gradualDuration = SCREEN_BRIGHTNESS_UPDATE_DURATION); + bool RestoreBrightness(uint32_t gradualDuration = SCREEN_BRIGHTNESS_UPDATE_DURATION); + bool IsBrightnessOverride() const; + uint32_t GetBeforeBrightness() const; + bool BoostBrightness(uint32_t timeoutMs, uint32_t gradualDuration = SCREEN_BRIGHTNESS_UPDATE_DURATION); + bool CancelBoostBrightness(); + bool IsBoostBrightness() const; + private: + void SaveBeforeBrightness(std::atomic& isOverride); + bool BrightnessBeforeRestore(std::atomic& isOverride, uint32_t gradualDuration); + uint32_t beforeBrightness_ { 0 }; + std::mutex mutexOverride_; + std::atomic isBrightnessOverride_ { false }; + std::atomic isBoostBrightness_ { false }; + const std::shared_ptr& sharedControl_; + const std::shared_ptr& stateControl_; + const std::shared_ptr& handler_; + }; + + std::shared_ptr& SharedControl() + { + return sharedControl_; + } + std::unique_ptr& OverrideControl() + { + return overrideControl_; + } + std::shared_ptr& DisplayStateControl() + { + return stateControl_; + } + private: static const uint32_t SCREEN_BRIGHTNESS_UPDATE_DURATION = 200; - void OnStateChanged(DisplayState state); - void BeforeUpdateState(DisplayState state); - void AfterUpdateState(DisplayState state); - bool UpdateBrightness(uint32_t value, uint32_t gradualDuration); - std::mutex mutex_; - const uint32_t displayId_; - DisplayState state_; - std::map stateValues_; - - uint32_t brightness_ {0}; - bool isBrightnessOverride_ {false}; - uint32_t beforeOverrideBrightness_ {0}; - uint32_t beforeOffBrightness_ {0}; - uint32_t stateChangeReason_ {0}; - std::shared_ptr action_; - std::shared_ptr animator_; + std::shared_ptr sharedControl_ { nullptr }; + std::unique_ptr overrideControl_; + std::shared_ptr stateControl_; }; } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/src/display_event_handler.cpp b/service/native/src/display_event_handler.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95b616d323e18de84b43f47a2dae60de9e95aa9e --- /dev/null +++ b/service/native/src/display_event_handler.cpp @@ -0,0 +1,51 @@ +/* + * 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_event_handler.h" + +#include "display_log.h" + +namespace OHOS { +namespace DisplayPowerMgr { +DisplayEventHandler::DisplayEventHandler(const std::shared_ptr& runner, + const wptr& parent) : AppExecFwk::EventHandler(runner), parent_(parent) +{ + DISPLAY_HILOGD(COMP_SVC, "Instance created"); +} + +void DisplayEventHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) +{ + uint32_t eventId = event->GetInnerEventId(); + auto callback = eventCallbackMap_.find(eventId); + if (callback != eventCallbackMap_.end()) { + DISPLAY_HILOGD(COMP_SVC, "Perform the display handler event callback, eventId: %{public}d", eventId); + callback->second(); + return; + } + DISPLAY_HILOGI(COMP_SVC, "Start to process, eventId: %{public}d", eventId); +} + +void DisplayEventHandler::EmplaceCallBack(DisplayEventHandler::Event event, EventCallback& callback) +{ + uint32_t eventId = static_cast(event); + DISPLAY_HILOGI(COMP_SVC, "Register handler events, eventId: %{public}d", eventId); + if (eventCallbackMap_.find(eventId) != eventCallbackMap_.end()) { + DISPLAY_HILOGW(COMP_SVC, "Already registered, ignore registration eventId: %{public}d", eventId); + return; + } + eventCallbackMap_.emplace(eventId, callback); +} +} // 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 6f591ef51951dccf5dd8c866bf14e1193626a17e..117dae9da5b9b86922ec3d60f4ef6fef12727bd3 100644 --- a/service/native/src/display_power_mgr_service.cpp +++ b/service/native/src/display_power_mgr_service.cpp @@ -19,28 +19,49 @@ #include #include #include -#include "display_param_helper.h" + +#include "watchdog.h" + #include "display_log.h" +#include "display_param_helper.h" namespace OHOS { namespace DisplayPowerMgr { +namespace { +const std::string DISPLAY_SERVICE_NAME = "DisplayPowerManagerService"; +constexpr int32_t WATCH_DOG_DELAY_S = 60; +} DisplayPowerMgrService::DisplayPowerMgrService() +{ +} + +void DisplayPowerMgrService::Init() { DISPLAY_HILOGI(COMP_SVC, "DisplayPowerMgrService Create"); - action_ = std::make_shared(); - std::vector displayIds = action_->GetDisplayIds(); - uint32_t count = displayIds.size(); - for (uint32_t i = 0; i < count; i++) { - DISPLAY_HILOGI(COMP_SVC, "find display, id=%{public}u", displayIds[i]); - controllerMap_.emplace(displayIds[i], std::make_shared(displayIds[i], action_)); + if (!eventRunner_) { + eventRunner_ = AppExecFwk::EventRunner::Create(DISPLAY_SERVICE_NAME); + if (eventRunner_ == nullptr) { + DISPLAY_HILOGE(COMP_SVC, "Init failed due to create EventRunner"); + return; + } + } + + if (!handler_) { + handler_ = std::make_shared(eventRunner_, + DelayedSpSingleton::GetInstance()); + std::string handlerName("DisplayPowerEventHandler"); + HiviewDFX::Watchdog::GetInstance().AddThread(handlerName, handler_, WATCH_DOG_DELAY_S); + } + + std::vector displayIds = ScreenAction::GetAllDisplayId(); + for (const auto& id : displayIds) { + DISPLAY_HILOGI(COMP_SVC, "find display, id=%{public}u", id); + controllerMap_.emplace(id, std::make_shared(id, handler_)); } callback_ = nullptr; cbDeathRecipient_ = nullptr; - InitSensors(); -} -DisplayPowerMgrService::~DisplayPowerMgrService() -{ + InitSensors(); } bool DisplayPowerMgrService::SetDisplayState(uint32_t id, DisplayState state, uint32_t reason) @@ -58,7 +79,7 @@ bool DisplayPowerMgrService::SetDisplayState(uint32_t id, DisplayState state, ui DeactivateAmbientSensor(); } } - return iterater->second->UpdateState(state, reason); + return iterater->second->DisplayStateControl()->UpdateState(state, reason); } DisplayState DisplayPowerMgrService::GetDisplayState(uint32_t id) @@ -68,7 +89,7 @@ DisplayState DisplayPowerMgrService::GetDisplayState(uint32_t id) if (iterater == controllerMap_.end()) { return DisplayState::DISPLAY_UNKNOWN; } - return iterater->second->GetState(); + return iterater->second->DisplayStateControl()->GetState(); } std::vector DisplayPowerMgrService::GetDisplayIds() @@ -82,7 +103,7 @@ std::vector DisplayPowerMgrService::GetDisplayIds() uint32_t DisplayPowerMgrService::GetMainDisplayId() { - uint32_t id = action_->GetDefaultDisplayId(); + uint32_t id = ScreenAction::GetDefaultDisplayId(); DISPLAY_HILOGI(COMP_SVC, "GetMainDisplayId %{public}d", id); return id; } @@ -95,7 +116,7 @@ bool DisplayPowerMgrService::SetBrightness(uint32_t value, uint32_t displayId) if (iter == controllerMap_.end()) { return false; } - return iter->second->SetBrightness(brightness); + return iter->second->SharedControl()->SetBrightness(brightness); } bool DisplayPowerMgrService::OverrideBrightness(uint32_t value, uint32_t displayId) @@ -106,7 +127,7 @@ bool DisplayPowerMgrService::OverrideBrightness(uint32_t value, uint32_t display if (iter == controllerMap_.end()) { return false; } - return iter->second->OverrideBrightness(brightness); + return iter->second->OverrideControl()->OverrideBrightness(brightness); } bool DisplayPowerMgrService::RestoreBrightness(uint32_t displayId) @@ -116,7 +137,7 @@ bool DisplayPowerMgrService::RestoreBrightness(uint32_t displayId) if (iter == controllerMap_.end()) { return false; } - return iter->second->RestoreBrightness(); + return iter->second->OverrideControl()->RestoreBrightness(); } uint32_t DisplayPowerMgrService::GetBrightness(uint32_t displayId) @@ -126,7 +147,7 @@ uint32_t DisplayPowerMgrService::GetBrightness(uint32_t displayId) if (iter == controllerMap_.end()) { return BRIGHTNESS_OFF; } - return iter->second->GetBrightness(); + return iter->second->SharedControl()->GetBrightness(); } uint32_t DisplayPowerMgrService::GetDefaultBrightness() @@ -152,7 +173,7 @@ bool DisplayPowerMgrService::AdjustBrightness(uint32_t id, int32_t value, uint32 if (iterater == controllerMap_.end()) { return false; } - return iterater->second->SetBrightness(value, duration); + return iterater->second->SharedControl()->SetBrightness(value, duration); } bool DisplayPowerMgrService::AutoAdjustBrightness(bool enable) @@ -235,7 +256,7 @@ bool DisplayPowerMgrService::SetStateConfig(uint32_t id, DisplayState state, int if (iterater == controllerMap_.end()) { return false; } - return iterater->second->UpdateStateConfig(state, value); + return iterater->second->DisplayStateControl()->UpdateStateConfig(state, value); } bool DisplayPowerMgrService::RegisterCallback(sptr callback) @@ -258,6 +279,23 @@ bool DisplayPowerMgrService::RegisterCallback(sptr callba return true; } +bool DisplayPowerMgrService::BoostBrightness(int32_t timeoutMs, uint32_t displayId) +{ + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "Timing max brightness: %{public}d, id: %{public}d", timeoutMs, displayId); + RETURN_IF_WITH_RET(timeoutMs <= 0, false); + auto iter = controllerMap_.find(displayId); + RETURN_IF_WITH_RET(iter == controllerMap_.end(), false); + return iter->second->OverrideControl()->BoostBrightness(timeoutMs); +} + +bool DisplayPowerMgrService::CancelBoostBrightness(uint32_t displayId) +{ + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "Cancel boost brightness, id: %{public}d", displayId); + auto iter = controllerMap_.find(displayId); + RETURN_IF_WITH_RET(iter == controllerMap_.end(), false); + return iter->second->OverrideControl()->CancelBoostBrightness(); +} + void DisplayPowerMgrService::NotifyStateChangeCallback(uint32_t displayId, DisplayState state) { if (callback_ != nullptr) { @@ -269,18 +307,24 @@ int32_t DisplayPowerMgrService::Dump(int32_t fd, const std::vectordata; DISPLAY_HILOGI(FEAT_BRIGHTNESS, "AmbientLightCallback: %{public}f", data->intensity); - int32_t brightness = static_cast(mainDisp->second->GetBrightness()); + int32_t brightness = static_cast(mainDisp->second->SharedControl()->GetBrightness()); if (pms->CalculateBrightness(data->intensity, brightness)) { pms->AdjustBrightness(mainDispId, brightness, AUTO_ADJUST_BRIGHTNESS_DURATION); } diff --git a/service/native/src/display_system_ability.cpp b/service/native/src/display_system_ability.cpp index 8515896b6ebb3f8dc05d4a7cc49d628a24062364..89f72923fe3fd4cdd64c570c288fc95815674c1c 100644 --- a/service/native/src/display_system_ability.cpp +++ b/service/native/src/display_system_ability.cpp @@ -24,7 +24,9 @@ REGISTER_SYSTEM_ABILITY_BY_ID(DisplaySystemAbility, DISPLAY_MANAGER_SERVICE_ID, void DisplaySystemAbility::OnStart() { DISPLAY_HILOGI(COMP_SVC, "Start service"); - if (!Publish(DelayedSpSingleton::GetInstance())) { + auto service = DelayedSpSingleton::GetInstance(); + service->Init(); + if (!Publish(service)) { DISPLAY_HILOGE(COMP_SVC, "Failed to publish service"); } } diff --git a/service/native/src/gradual_animator.cpp b/service/native/src/gradual_animator.cpp index 9e21389d6f30ef5841ef19c9d81c2f5cafe2772f..f1cb23c00d12f948f527374d1ab32e774371b57c 100644 --- a/service/native/src/gradual_animator.cpp +++ b/service/native/src/gradual_animator.cpp @@ -20,11 +20,9 @@ namespace OHOS { namespace DisplayPowerMgr { -GradualAnimator::GradualAnimator(const std::string& name, - const std::shared_ptr& callback) +GradualAnimator::GradualAnimator(const std::string& name, std::shared_ptr& callback) + : name_(name), callback_(callback) { - name_ = name; - callback_ = callback; fromBrightness_ = 0; toBrightness_ = 0; currentBrightness_ = 0; @@ -43,7 +41,7 @@ void GradualAnimator::StartAnimation(uint32_t from, uint32_t to, uint32_t durati } DISPLAY_HILOGD(FEAT_BRIGHTNESS, "animation from=%{public}u, to=%{public}u, duration=%{public}u", from, to, duration); - if (callback_.lock() == nullptr) { + if (callback_ == nullptr) { DISPLAY_HILOGW(FEAT_BRIGHTNESS, "callback_ is nullptr"); return; } @@ -77,12 +75,11 @@ void GradualAnimator::StopAnimation() { animating_ = false; handler_->RemoveEvent(EVENT_STEP); - if (callback_.lock() == nullptr) { + if (callback_ == nullptr) { DISPLAY_HILOGW(FEAT_BRIGHTNESS, "Callback is nullptr"); return; } - std::shared_ptr callback = callback_.lock(); - callback->OnEnd(); + callback_->OnEnd(); DISPLAY_HILOGD(FEAT_BRIGHTNESS, "animation stopped"); } @@ -97,23 +94,22 @@ void GradualAnimator::NextStep() DISPLAY_HILOGW(FEAT_BRIGHTNESS, "is not animating, return"); return; } - if (callback_.lock() == nullptr) { + if (callback_ == nullptr) { DISPLAY_HILOGW(FEAT_BRIGHTNESS, "Callback is nullptr"); return; } - std::shared_ptr callback = callback_.lock(); currentStep_++; if (currentStep_ == 1) { - callback->OnStart(); + callback_->OnStart(); } if (currentStep_ < totalSteps_) { currentBrightness_ = currentBrightness_ + stride_; - callback->OnChanged(currentBrightness_); + callback_->OnChanged(currentBrightness_); handler_->SendEvent(EVENT_STEP, 0, updateTime_); } else { currentBrightness_ = toBrightness_; - callback->OnChanged(currentBrightness_); - callback->OnEnd(); + callback_->OnChanged(currentBrightness_); + callback_->OnEnd(); animating_ = false; } DISPLAY_HILOGD(FEAT_BRIGHTNESS, "animating next step, step=%{public}u, brightness=%{public}u, stride=%{public}d", diff --git a/service/native/src/screen_action.cpp b/service/native/src/screen_action.cpp index a28cbdb3fdb98816f9431e65215b923b0e546460..4f47f632cbfdf56a1c8c2be01fdf43cc5e8031d2 100644 --- a/service/native/src/screen_action.cpp +++ b/service/native/src/screen_action.cpp @@ -24,7 +24,7 @@ namespace OHOS { namespace DisplayPowerMgr { -ScreenAction::ScreenAction() +ScreenAction::ScreenAction(uint32_t displayId) : displayId_(displayId) { DISPLAY_HILOGI(COMP_SVC, "Succeed to init"); } @@ -35,24 +35,30 @@ uint32_t ScreenAction::GetDefaultDisplayId() return static_cast(defaultId); } -std::vector ScreenAction::GetDisplayIds() +std::vector ScreenAction::GetAllDisplayId() { std::vector allIds = Rosen::DisplayManager::GetInstance().GetAllDisplayIds(); + std::vector displayIds; if (allIds.empty()) { - displayIds_.push_back(DEFAULT_DISPLAY_ID); - return displayIds_; + displayIds.push_back(DEFAULT_DISPLAY_ID); + return displayIds; } for (const auto& id: allIds) { - displayIds_.push_back(static_cast(id)); + displayIds.push_back(static_cast(id)); } - return displayIds_; + return displayIds; } -DisplayState ScreenAction::GetPowerState(uint32_t displayId) +uint32_t ScreenAction::GetDisplayId() { - DISPLAY_HILOGI(COMP_SVC, "GetPowerState of displayId=%{public}u", displayId); + return displayId_; +} + +DisplayState ScreenAction::GetPowerState() +{ + DISPLAY_HILOGI(COMP_SVC, "GetPowerState of displayId=%{public}u", displayId_); DisplayState ret = DisplayState::DISPLAY_UNKNOWN; - Rosen::ScreenPowerState state = Rosen::ScreenManager::GetInstance().GetScreenPower(displayId); + Rosen::ScreenPowerState state = Rosen::ScreenManager::GetInstance().GetScreenPower(displayId_); DISPLAY_HILOGI(COMP_SVC, "GetPowerState: %{public}d", static_cast(state)); switch (state) { case Rosen::ScreenPowerState::POWER_ON: @@ -74,11 +80,10 @@ DisplayState ScreenAction::GetPowerState(uint32_t displayId) return ret; } -bool ScreenAction::SetDisplayState(uint32_t displayId, DisplayState state, - const std::function& callback) +bool ScreenAction::SetDisplayState(DisplayState state, const std::function& callback) { DISPLAY_HILOGI(COMP_SVC, "SetDisplayState: displayId=%{public}u, state=%{public}u", - displayId, static_cast(state)); + displayId_, static_cast(state)); Rosen::DisplayState rds = Rosen::DisplayState::UNKNOWN; switch (state) { @@ -115,10 +120,10 @@ bool ScreenAction::SetDisplayState(uint32_t displayId, DisplayState state, return ret; } -bool ScreenAction::SetDisplayPower(uint32_t displayId, DisplayState state, uint32_t reason) +bool ScreenAction::SetDisplayPower(DisplayState state, uint32_t reason) { DISPLAY_HILOGI(COMP_SVC, "SetDisplayPower: displayId=%{public}u, state=%{public}u, state=%{public}u", - displayId, static_cast(state), reason); + displayId_, static_cast(state), reason); Rosen::ScreenPowerState status = Rosen::ScreenPowerState::INVALID_STATE; switch (state) { case DisplayState::DISPLAY_ON: @@ -142,20 +147,24 @@ bool ScreenAction::SetDisplayPower(uint32_t displayId, DisplayState state, uint3 return true; } -uint32_t ScreenAction::GetBrightness(uint32_t displayId) +uint32_t ScreenAction::GetBrightness() { - auto brightness = Rosen::DisplayManager::GetInstance().GetScreenBrightness(displayId); - DISPLAY_HILOGD(FEAT_BRIGHTNESS, "displayId=%{public}u, brightness=%{public}u", displayId, brightness); - return brightness; + if (brightness_ == 0) { + brightness_ = Rosen::DisplayManager::GetInstance().GetScreenBrightness(displayId_); + } + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "displayId=%{public}u, brightness=%{public}u", displayId_, brightness_); + return brightness_; } -bool ScreenAction::SetBrightness(uint32_t displayId, uint32_t value) +bool ScreenAction::SetBrightness(uint32_t value) { - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "displayId=%{public}u, brightness=%{public}u", displayId, value); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "displayId=%{public}u, brightness=%{public}u", displayId_, value); // Notify screen brightness change event to battery statistics HiviewDFX::HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::POWERMGR, "BRIGHTNESS_NIT", HiviewDFX::HiSysEvent::EventType::STATISTIC, "brightness", value); - return Rosen::DisplayManager::GetInstance().SetScreenBrightness(displayId, value); + bool isSucc = Rosen::DisplayManager::GetInstance().SetScreenBrightness(displayId_, value); + brightness_ = isSucc ? value : brightness_; + return isSucc; } } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/src/screen_controller.cpp b/service/native/src/screen_controller.cpp index 39365eb0f662358f02f0f0dc7356cd373b9a601e..ddde60bea29ec1706bc7bda531dda8fdc5d70c5c 100644 --- a/service/native/src/screen_controller.cpp +++ b/service/native/src/screen_controller.cpp @@ -15,215 +15,314 @@ #include "screen_controller.h" -#include "display_power_mgr_service.h" #include "display_log.h" +#include "display_param_helper.h" +#include "display_power_mgr_service.h" +using namespace std; namespace OHOS { namespace DisplayPowerMgr { -const int DISPLAY_FULL_BRIGHTNESS = 100; -const int DISPLAY_DIM_BRIGHTNESS = 50; -const int DISPLAY_OFF_BRIGHTNESS = 0; -const int DISPLAY_SUSPEND_BRIGHTNESS = 50; +constexpr uint32_t DISPLAY_FULL_BRIGHTNESS = 255; +constexpr uint32_t DISPLAY_DIM_BRIGHTNESS = 50; +constexpr uint32_t DISPLAY_OFF_BRIGHTNESS = 0; +constexpr uint32_t DISPLAY_SUSPEND_BRIGHTNESS = 50; -ScreenController::ScreenController(uint32_t displayId, std::shared_ptr action) - : displayId_(displayId), state_(DisplayState::DISPLAY_UNKNOWN), action_(std::move(action)) +ScreenController::ScreenController(uint32_t displayId, const shared_ptr& handler) { - DISPLAY_HILOGI(COMP_SVC, "ScreenController created for displayId=%{public}u", displayId_); - stateValues_.emplace(DisplayState::DISPLAY_ON, DISPLAY_FULL_BRIGHTNESS); - stateValues_.emplace(DisplayState::DISPLAY_DIM, DISPLAY_DIM_BRIGHTNESS); - stateValues_.emplace(DisplayState::DISPLAY_OFF, DISPLAY_OFF_BRIGHTNESS); - stateValues_.emplace(DisplayState::DISPLAY_SUSPEND, DISPLAY_SUSPEND_BRIGHTNESS); - animator_ = nullptr; + DISPLAY_HILOGI(COMP_SVC, "ScreenController created for displayId=%{public}u", displayId); + sharedControl_ = make_shared(displayId); + stateControl_ = make_shared(sharedControl_); + overrideControl_ = make_unique(sharedControl_, stateControl_, handler); } -bool ScreenController::UpdateState(DisplayState state, uint32_t reason) +ScreenController::AnimateController ::AnimateController (const shared_ptr& action) + : action_(action) { - std::lock_guard lock(mutex_); - DISPLAY_HILOGI(COMP_SVC, - "ScreenController UpdateState, displayId=%{public}u, state=%{public}u, state_=%{public}u", - displayId_, static_cast(state), static_cast(state_)); - if (state == state_) { - return true; - } +} - switch (state) { - case DisplayState::DISPLAY_ON: // fall through - case DisplayState::DISPLAY_OFF: { - BeforeUpdateState(state); - std::function callback = - std::bind(&ScreenController::OnStateChanged, this, std::placeholders::_1); - bool ret = action_->SetDisplayState(displayId_, state, callback); - if (!ret) { - DISPLAY_HILOGW(COMP_SVC, "SetDisplayState failed state=%{public}d", state); - return ret; - } - AfterUpdateState(state); - break; - } - case DisplayState::DISPLAY_DIM: // fall through - case DisplayState::DISPLAY_SUSPEND: { - bool ret = action_->SetDisplayPower(displayId_, state, stateChangeReason_); - if (!ret) { - DISPLAY_HILOGW(COMP_SVC, "SetDisplayPower failed state=%{public}d", state); - return ret; - } - break; - } - default: - break; +void ScreenController::AnimateController ::OnStart() +{ + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "ScreenAnimatorCallback onStart"); +} + +void ScreenController::AnimateController ::OnChanged(uint32_t currentValue) +{ + bool ret = action_->SetBrightness(currentValue); + if (ret) { + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "Update brightness, brightness=%{public}u", currentValue); + } else { + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "Update brightness failed, brightness=%{public}d", currentValue); } +} - state_ = state; - stateChangeReason_ = reason; +void ScreenController::AnimateController ::OnEnd() +{ + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "ScreenAnimatorCallback OnEnd"); +} - DISPLAY_HILOGI(COMP_SVC, "Update screen state to %{public}u", state); - return true; +ScreenController::SharedController::SharedController(const uint32_t displayId) +{ + action_ = make_shared(displayId); + string name = "BrightnessController_" + to_string(displayId); + shared_ptr callback = make_shared(action_); + animator_ = make_shared(name, callback); } -void ScreenController::BeforeUpdateState(DisplayState state) +bool ScreenController::SharedController::SetBrightness(uint32_t value, uint32_t gradualDuration) { - if (state == DisplayState::DISPLAY_OFF) { - beforeOffBrightness_ = action_->GetBrightness(displayId_); - beforeOffBrightness_ = (beforeOffBrightness_ <= DISPLAY_OFF_BRIGHTNESS || - beforeOffBrightness_ > DISPLAY_FULL_BRIGHTNESS) ? - DISPLAY_FULL_BRIGHTNESS : beforeOffBrightness_; - DISPLAY_HILOGI(COMP_SVC, "Screen brightness before screen off %{public}d", - beforeOffBrightness_); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Set brightness, value=%{public}u", value); + if (!isAllow_) { + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "brightness is not allowed, ignore the change"); + return false; } + return UpdateBrightness(value, gradualDuration); } -void ScreenController::AfterUpdateState(DisplayState state) +uint32_t ScreenController::SharedController::GetBrightness() { - if (state == DisplayState::DISPLAY_ON) { - bool ret = action_->SetBrightness(displayId_, beforeOffBrightness_); - DISPLAY_HILOGI(COMP_SVC, "Is SetBrightness %{public}d, \ - Update brightness to %{public}u", ret, beforeOffBrightness_); - } + lock_guard lock(mutex_); + return action_->GetBrightness(); } -bool ScreenController::UpdateBrightness(uint32_t value, uint32_t gradualDuration) +void ScreenController::SharedController::AllowAdjustBrightness(bool allow) { - std::lock_guard lock(mutex_); - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Update brightness, displayId=%{public}u, value=%{public}u, duration=%{public}u", - displayId_, value, gradualDuration); - if (animator_ == nullptr) { - std::string name = "ScreenController_" + std::to_string(displayId_); - std::shared_ptr callback = shared_from_this(); - animator_ = std::make_shared(name, callback); - } + isAllow_ = allow; +} + +shared_ptr& ScreenController::SharedController::GetAction() +{ + return action_; +} + +bool ScreenController::SharedController::UpdateBrightness(uint32_t value, uint32_t gradualDuration) +{ + lock_guard lock(mutex_); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Update brightness, value=%{public}u, duration=%{public}u", + value, gradualDuration); + if (animator_->IsAnimating()) { animator_->StopAnimation(); } if (gradualDuration > 0) { DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Update brightness gradually"); - animator_->StartAnimation(brightness_, value, gradualDuration); + animator_->StartAnimation(action_->GetBrightness(), value, gradualDuration); return true; } - bool ret = action_->SetBrightness(displayId_, value); - if (ret) { - brightness_ = value; - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Updated brightness, value=%{public}u", value); - } else { - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Updated brightness failed, value=%{public}u", value); - } - return ret; + bool isSucc = action_->SetBrightness(value); + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "Updated brightness is %{public}s, brightness: %{public}u", + isSucc ? "succ" : "failed", value); + return isSucc; } -bool ScreenController::SetBrightness(uint32_t value, uint32_t gradualDuration) +ScreenController::OverrideController::OverrideController(const shared_ptr& sharedControl, + const std::shared_ptr& stateControl, + const shared_ptr& handler) + : sharedControl_(sharedControl), stateControl_(stateControl), handler_(handler) { - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Set brightness, value=%{public}u", value); - if (isBrightnessOverride_) { - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "brightness is override, ignore the change"); - return false; - } - return UpdateBrightness(value, gradualDuration); + DisplayEventHandler::EventCallback callback = bind([&]() { CancelBoostBrightness(); }); + handler_->EmplaceCallBack(DisplayEventHandler::Event::EVENT_CANCEL_BOOST_BRIGHTNESS, callback); } -bool ScreenController::OverrideBrightness(uint32_t value, uint32_t gradualDuration) +bool ScreenController::OverrideController::OverrideBrightness(uint32_t value, uint32_t gradualDuration) { + lock_guard lock(mutexOverride_); + bool screenOn = stateControl_->IsScreenOn(); + // Cannot be called under overwrite or off screen + if (isBoostBrightness_ || !screenOn) { + DISPLAY_HILOGW(FEAT_BRIGHTNESS, "Cannot override brightness. isBoost: %{public}d, screenOn: %{public}d", + isBoostBrightness_.load(), screenOn); + return false; + } DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Override brightness, value=%{public}u", value); if (!isBrightnessOverride_) { - isBrightnessOverride_ = true; - beforeOverrideBrightness_ = GetBrightness(); - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Confirm override brightness, beforeOverrideBrightness_=%{public}u", - beforeOverrideBrightness_); + SaveBeforeBrightness(isBrightnessOverride_); } - return UpdateBrightness(value, gradualDuration); + return sharedControl_->UpdateBrightness(value, gradualDuration); } -bool ScreenController::RestoreBrightness(uint32_t gradualDuration) +bool ScreenController::OverrideController::RestoreBrightness(uint32_t gradualDuration) { - if (!isBrightnessOverride_) { + lock_guard lock(mutexOverride_); + if (!isBrightnessOverride_ || isBoostBrightness_) { DISPLAY_HILOGD(FEAT_BRIGHTNESS, "Brightness is not override, no need to restore"); return false; } - isBrightnessOverride_ = false; - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Restore brightness to value=%{public}u", beforeOverrideBrightness_); - return UpdateBrightness(beforeOverrideBrightness_, gradualDuration); + return BrightnessBeforeRestore(isBrightnessOverride_, gradualDuration); } -uint32_t ScreenController::GetBrightness() +bool ScreenController::OverrideController::IsBrightnessOverride() const { - std::lock_guard lock(mutex_); - if (brightness_ == DISPLAY_OFF_BRIGHTNESS) { - brightness_ = action_->GetBrightness(displayId_); - } - return brightness_; + return isBrightnessOverride_; } -bool ScreenController::UpdateStateConfig(DisplayState state, uint32_t value) +uint32_t ScreenController::OverrideController::GetBeforeBrightness() const { - std::lock_guard lock(mutex_); - DISPLAY_HILOGI(COMP_SVC, - "ScreenController UpdateStateConfig, displayId=%{public}u, state=%{public}u, value=%{public}u", - displayId_, static_cast(state), value); - auto iterator = stateValues_.find(state); - if (iterator == stateValues_.end()) { - DISPLAY_HILOGI(COMP_SVC, "UpdateStateConfig no such state"); + return beforeBrightness_; +} + +bool ScreenController::OverrideController::BoostBrightness(uint32_t timeoutMs, uint32_t gradualDuration) +{ + lock_guard lock(mutexOverride_); + bool screenOn = stateControl_->IsScreenOn(); + // Calls are not allowed under Boost or off screen + if (isBrightnessOverride_ || !screenOn) { + DISPLAY_HILOGW(FEAT_BRIGHTNESS, "Cannot boost brightness, isOverride: %{public}d, screenOn: %{public}d", + isBrightnessOverride_.load(), screenOn); return false; } - iterator->second = value; + if (!isBoostBrightness_) { + uint32_t maxBrightness = DisplayParamHelper::GetInstance().GetMaxBrightness(); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Boost brightness, maxBrightness: %{public}d", maxBrightness); + SaveBeforeBrightness(isBoostBrightness_); + sharedControl_->UpdateBrightness(maxBrightness, gradualDuration); + } + + handler_->RemoveEvent(DisplayEventHandler::Event::EVENT_CANCEL_BOOST_BRIGHTNESS); + handler_->SendEvent(DisplayEventHandler::Event::EVENT_CANCEL_BOOST_BRIGHTNESS, timeoutMs); + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "BoostBrightness update timeout"); + return true; } -bool ScreenController::IsScreenOn() +bool ScreenController::OverrideController::CancelBoostBrightness() { - std::lock_guard lock(mutex_); - return (state_ == DisplayState::DISPLAY_ON || state_ == DisplayState::DISPLAY_DIM); + lock_guard lock(mutexOverride_); + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "Cancel boost brightness"); + if (!isBoostBrightness_ || isBrightnessOverride_) { + DISPLAY_HILOGD(FEAT_BRIGHTNESS, "Brightness is not boost, no need to restore"); + return false; + } + handler_->RemoveEvent(DisplayEventHandler::Event::EVENT_CANCEL_BOOST_BRIGHTNESS); + return BrightnessBeforeRestore(isBoostBrightness_, SCREEN_BRIGHTNESS_UPDATE_DURATION); } -bool ScreenController::IsBrightnessOverride() const +bool ScreenController::OverrideController::IsBoostBrightness() const { - return isBrightnessOverride_; + return isBoostBrightness_; } -uint32_t ScreenController::GetBeforeOverrideBrightness() const +void ScreenController::OverrideController::SaveBeforeBrightness(atomic& isOverride) { - return beforeOverrideBrightness_; + isOverride = true; + // Disable external calls to brightness adjustment + sharedControl_->AllowAdjustBrightness(false); + beforeBrightness_ = sharedControl_->GetBrightness(); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Gets the current system brightness, beforeBrightness_=%{public}u", + beforeBrightness_); } -void ScreenController::OnStart() +bool ScreenController::OverrideController::BrightnessBeforeRestore(atomic& isOverride, + uint32_t gradualDuration) { - DISPLAY_HILOGD(FEAT_BRIGHTNESS, "ScreenAnimatorCallback onStart"); + isOverride = false; + // Allows external calls to brightness adjustment + sharedControl_->AllowAdjustBrightness(true); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Restore system Brightness. value=%{public}u", beforeBrightness_); + // After the screen is off, the system brightness is restored + stateControl_->UpdateBeforeOffBrightness(beforeBrightness_); + return sharedControl_->UpdateBrightness(beforeBrightness_, gradualDuration); } -void ScreenController::OnChanged(uint32_t currentValue) +ScreenController::DisplayStateController::DisplayStateController(const shared_ptr& sharedControl) + : state_(DisplayState::DISPLAY_UNKNOWN), sharedControl_(sharedControl) { - brightness_ = static_cast(currentValue); - bool ret = action_->SetBrightness(displayId_, brightness_); - if (ret) { - DISPLAY_HILOGD(FEAT_BRIGHTNESS, "Update brightness, brightness_=%{public}u", brightness_); - } else { - DISPLAY_HILOGD(FEAT_BRIGHTNESS, "Update brightness failed, brightness_=%{public}d", brightness_); + stateValues_.emplace(DisplayState::DISPLAY_ON, DisplayParamHelper::GetInstance().GetMaxBrightness()); + stateValues_.emplace(DisplayState::DISPLAY_DIM, DISPLAY_DIM_BRIGHTNESS); + stateValues_.emplace(DisplayState::DISPLAY_OFF, DisplayParamHelper::GetInstance().GetMinBrightness()); + stateValues_.emplace(DisplayState::DISPLAY_SUSPEND, DISPLAY_SUSPEND_BRIGHTNESS); +} + +bool ScreenController::DisplayStateController::UpdateState(DisplayState state, uint32_t reason) +{ + lock_guard lock(mutexState_); + DISPLAY_HILOGI(COMP_SVC, "UpdateState, state=%{public}u, current state=%{public}u", + static_cast(state), static_cast(state_)); + RETURN_IF_WITH_RET(state == state_, true); + + switch (state) { + case DisplayState::DISPLAY_ON: + case DisplayState::DISPLAY_OFF: { + BeforeScreenOff(state); + function callback = + bind(&ScreenController::DisplayStateController::OnStateChanged, this, placeholders::_1); + bool ret = sharedControl_->GetAction()->SetDisplayState(state, callback); + if (!ret) { + DISPLAY_HILOGW(COMP_SVC, "SetDisplayState failed state=%{public}d", state); + return ret; + } + AfterScreenOn(state); + break; + } + case DisplayState::DISPLAY_DIM: + case DisplayState::DISPLAY_SUSPEND: { + bool ret = sharedControl_->GetAction()->SetDisplayPower(state, stateChangeReason_); + if (!ret) { + DISPLAY_HILOGW(COMP_SVC, "SetDisplayPower failed state=%{public}d", state); + return ret; + } + break; + } + default: + break; } + + state_ = state; + stateChangeReason_ = reason; + + DISPLAY_HILOGI(COMP_SVC, "Update screen state to %{public}u", state); + return true; } -void ScreenController::OnEnd() +bool ScreenController::DisplayStateController::UpdateStateConfig(DisplayState state, uint32_t value) { - DISPLAY_HILOGD(FEAT_BRIGHTNESS, "ScreenAnimatorCallback OnEnd"); + lock_guard lock(mutexState_); + DISPLAY_HILOGI(COMP_SVC, "UpdateStateConfig, state=%{public}u, value=%{public}u", + static_cast(state), value); + auto iterator = stateValues_.find(state); + if (iterator == stateValues_.end()) { + DISPLAY_HILOGI(COMP_SVC, "UpdateStateConfig no such state"); + return false; + } + iterator->second = value; + return true; +} + +bool ScreenController::DisplayStateController::IsScreenOn() +{ + lock_guard lock(mutexState_); + return (state_ == DisplayState::DISPLAY_ON || state_ == DisplayState::DISPLAY_DIM); +} + +void ScreenController::DisplayStateController::UpdateBeforeOffBrightness(uint32_t brightness) +{ + lock_guard lock(mutexState_); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "After the screen is off, the system brightness is restored." \ + "brightness: %{public}d", brightness); + beforeOffBrightness_ = brightness; +} + +void ScreenController::DisplayStateController::BeforeScreenOff(DisplayState state) +{ + if (state == DisplayState::DISPLAY_OFF) { + beforeOffBrightness_ = sharedControl_->GetBrightness(); + beforeOffBrightness_ = (beforeOffBrightness_ <= DISPLAY_OFF_BRIGHTNESS || + beforeOffBrightness_ > DISPLAY_FULL_BRIGHTNESS) ? + DISPLAY_FULL_BRIGHTNESS : beforeOffBrightness_; + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Screen brightness before screen off %{public}d", + beforeOffBrightness_); + } +} + +void ScreenController::DisplayStateController::AfterScreenOn(DisplayState state) +{ + if (state == DisplayState::DISPLAY_ON) { + bool ret = sharedControl_->UpdateBrightness(beforeOffBrightness_, 0); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Is SetBrightness %{public}d, \ + Update brightness to %{public}u", ret, beforeOffBrightness_); + } } -void ScreenController::OnStateChanged(DisplayState state) +void ScreenController::DisplayStateController::OnStateChanged(DisplayState state) { DISPLAY_HILOGD(COMP_SVC, "OnStateChanged %{public}u", static_cast(state)); auto pms = DelayedSpSingleton::GetInstance(); @@ -232,9 +331,9 @@ void ScreenController::OnStateChanged(DisplayState state) return; } - bool ret = action_->SetDisplayPower(displayId_, state, stateChangeReason_); + bool ret = sharedControl_->GetAction()->SetDisplayPower(state, stateChangeReason_); if (ret) { - pms->NotifyStateChangeCallback(displayId_, state); + pms->NotifyStateChangeCallback(sharedControl_->GetAction()->GetDisplayId(), state); } } } // namespace DisplayPowerMgr diff --git a/service/zidl/include/display_power_mgr_proxy.h b/service/zidl/include/display_power_mgr_proxy.h index 22711cc1faff9d58c1f32f668302d61362159c97..406ee128258aefe2961a020f60f765172793af44 100644 --- a/service/zidl/include/display_power_mgr_proxy.h +++ b/service/zidl/include/display_power_mgr_proxy.h @@ -45,6 +45,8 @@ public: virtual bool IsAutoAdjustBrightness() override; virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) override; virtual bool RegisterCallback(sptr callback) override; + virtual bool BoostBrightness(int32_t timeoutMs, uint32_t displayId) override; + virtual bool CancelBoostBrightness(uint32_t displayId) 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 38b33c9ba068d4da24fa6b63f1db9a2baeae45ae..629ec77fcd99677e5d3e1652ed28dce69f57b944 100644 --- a/service/zidl/include/display_power_mgr_stub.h +++ b/service/zidl/include/display_power_mgr_stub.h @@ -43,6 +43,8 @@ private: int32_t IsAutoAdjustBrightnessStub(MessageParcel& data, MessageParcel& reply); int32_t SetStateConfigStub(MessageParcel& data, MessageParcel& reply); int32_t RegisterCallbackStub(MessageParcel& data, MessageParcel& reply); + int32_t BoostBrightnessStub(MessageParcel& data, MessageParcel& reply); + int32_t CancelBoostBrightnessStub(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 059a09df210371ab94f54806e7a9c4abfad24dcb..6a5ed8c99c87fdd992b14e48651b83589426b72a 100644 --- a/service/zidl/src/display_power_mgr_proxy.cpp +++ b/service/zidl/src/display_power_mgr_proxy.cpp @@ -546,5 +546,69 @@ bool DisplayPowerMgrProxy::RegisterCallback(sptr callback return result; } + +bool DisplayPowerMgrProxy::BoostBrightness(int32_t timeoutMs, 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, "write descriptor failed!"); + return result; + } + + WRITE_PARCEL_WITH_RET(data, Int32, timeoutMs, false); + WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false); + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::BOOST_BRIGHTNESS), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed: %{public}d", ret); + return result; + } + + if (!reply.ReadBool(result)) { + DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); + return result; + } + + return result; +} + +bool DisplayPowerMgrProxy::CancelBoostBrightness(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, "write descriptor failed!"); + return result; + } + WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false); + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::CANCEL_BOOST_BRIGHTNESS), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed: %{public}d", ret); + return result; + } + + if (!reply.ReadBool(result)) { + DISPLAY_HILOGE(COMP_FWK, "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 86e3f9afba9c74ea390027b409f79cfd41144034..4a47f1428ae30618b4f6ecd7620fa9072e275841 100644 --- a/service/zidl/src/display_power_mgr_stub.cpp +++ b/service/zidl/src/display_power_mgr_stub.cpp @@ -84,6 +84,12 @@ int32_t DisplayPowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, case static_cast(IDisplayPowerMgr::SET_STATE_CONFIG): ret = SetStateConfigStub(data, reply); break; + case static_cast(IDisplayPowerMgr::BOOST_BRIGHTNESS): + ret = BoostBrightnessStub(data, reply); + break; + case static_cast(IDisplayPowerMgr::CANCEL_BOOST_BRIGHTNESS): + ret = CancelBoostBrightnessStub(data, reply); + break; default: ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option); break; @@ -309,5 +315,34 @@ int32_t DisplayPowerMgrStub::RegisterCallbackStub(MessageParcel& data, MessagePa RegisterCallback(callback); return ERR_OK; } + +int32_t DisplayPowerMgrStub::BoostBrightnessStub(MessageParcel& data, MessageParcel& reply) +{ + int32_t timeoutMs = -1; + uint32_t id = 0; + READ_PARCEL_WITH_RET(data, Int32, timeoutMs, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR); + + bool isScuu = BoostBrightness(timeoutMs, id); + if (!reply.WriteBool(isScuu)) { + DISPLAY_HILOGW(COMP_SVC, "Failed to write BoostBrightness return value"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} + +int32_t DisplayPowerMgrStub::CancelBoostBrightnessStub(MessageParcel& data, MessageParcel& reply) +{ + uint32_t displayId = 0; + + READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR); + + bool isScuu = CancelBoostBrightness(displayId); + if (!reply.WriteBool(isScuu)) { + DISPLAY_HILOGW(COMP_SVC, "Failed to write CancelBoostBrightness return value"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/test/unittest/common/display_fuzzer/display_fuzzer.cpp b/test/unittest/common/display_fuzzer/display_fuzzer.cpp index 3fbf6bfaea07210b05d5bddc150a7120abdcc81e..15ab5c3a686aee27973d024982e1f9a78f4e20d6 100644 --- a/test/unittest/common/display_fuzzer/display_fuzzer.cpp +++ b/test/unittest/common/display_fuzzer/display_fuzzer.cpp @@ -140,6 +140,46 @@ static void RegisterCallback(const uint8_t* data) g_displayMgrClient.RegisterCallback(callback); } +static void OverrideBrightness(const uint8_t* data) +{ + uint32_t type[1]; + int32_t idSize = 4; + if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { + return; + } + g_displayMgrClient.OverrideBrightness(type[0]); +} + +static void RestoreBrightness(const uint8_t* data) +{ + uint32_t type[1]; + int32_t idSize = 4; + if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { + return; + } + g_displayMgrClient.RestoreBrightness(type[0]); +} + +static void BoostBrightness(const uint8_t* data) +{ + uint32_t type[1]; + int32_t idSize = 4; + if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { + return; + } + g_displayMgrClient.BoostBrightness(type[0]); +} + +static void CancelBoostBrightness(const uint8_t* data) +{ + uint32_t type[1]; + int32_t idSize = 4; + if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { + return; + } + g_displayMgrClient.CancelBoostBrightness(type[0]); +} + namespace OHOS { bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) { @@ -182,11 +222,31 @@ bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) case ApiNumber::NUM_EIGHT: RegisterCallback(data); break; + case ApiNumber::NUM_NINE: + OverrideBrightness(data); + break; + case ApiNumber::NUM_TEN: + RestoreBrightness(data); + break; + case ApiNumber::NUM_ELEVEN: + BoostBrightness(data); + break; + case ApiNumber::NUM_TWELVE: + CancelBoostBrightness(data); + break; + case ApiNumber::NUM_THIRTEEN: + g_displayMgrClient.GetDefaultBrightness(); + break; + case ApiNumber::NUM_FOURTEEN: + g_displayMgrClient.GetMaxBrightness(); + break; + case ApiNumber::NUM_FIFTEEN: + g_displayMgrClient.GetMinBrightness(); + break; default: break; } } - return true; } } diff --git a/test/unittest/common/display_fuzzer/display_fuzzer.h b/test/unittest/common/display_fuzzer/display_fuzzer.h index fd6ac3abc5ef7099e2b4816d937222b6d9c3ec71..5320dd533bab93ef9377e39c83c6881dc0fdf642 100644 --- a/test/unittest/common/display_fuzzer/display_fuzzer.h +++ b/test/unittest/common/display_fuzzer/display_fuzzer.h @@ -28,6 +28,13 @@ enum class ApiNumber { NUM_SIX, NUM_SEVEN, NUM_EIGHT, + NUM_NINE, + NUM_TEN, + NUM_ELEVEN, + NUM_TWELVE, + NUM_THIRTEEN, + NUM_FOURTEEN, + NUM_FIFTEEN }; #endif 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 5cecc61ae43cfc14f835995e29791edaa336a6b5..e00a6875891183dbf6cfc4c5479c24913f54c2bd 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 @@ -33,6 +33,8 @@ public: { DisplayPowerMgrClient::GetInstance().RestoreBrightness(); sleep(1); // wait for gradual animation + DisplayPowerMgrClient::GetInstance().CancelBoostBrightness(); + sleep(1); // wait for gradual animation } }; @@ -128,6 +130,22 @@ HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrBrightness005, TestSize.L DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBrightness005: fun is end"); } +/** + * @tc.name: DisplayPowerMgrBrightness006 + * @tc.desc: Test OverrideBrightness off Disable the call + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrBrightness006, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBrightness006: fun is start"); + DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_OFF); + bool isBoost = DisplayPowerMgrClient::GetInstance().OverrideBrightness(100); + EXPECT_FALSE(isBoost); + bool isRestore = DisplayPowerMgrClient::GetInstance().RestoreBrightness(); + EXPECT_FALSE(isRestore); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBrightness006: fun is end"); +} + /** * @tc.name: DisplayPowerMgrMaxBrightness001 * @tc.desc: Test GetMaxBrightness less equals 255 @@ -167,4 +185,219 @@ HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrDefaultBrightness001, Tes EXPECT_LE(value, 255); DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrDefaultBrightness001: fun is end"); } + +/** + * @tc.name: DisplayPowerMgrBoostBrightnessNormal + * @tc.desc: Test BoostBrightness the normal test + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrBoostBrightnessNormal, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessNormal: fun is start"); + bool isSucc = DisplayPowerMgrClient::GetInstance().BoostBrightness(1000); + EXPECT_TRUE(isSucc); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessNormal: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrBoostBrightnessAbnormal + * @tc.desc: Test BoostBrightness the abnormal value + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrBoostBrightnessAbnormal, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessAbnormal: fun is start"); + bool isSucc = DisplayPowerMgrClient::GetInstance().BoostBrightness(-1); + EXPECT_FALSE(isSucc); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessAbnormal: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrCancelBoostBrightnessNormal + * @tc.desc: Test CancelBoostBrightness the normal test + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrCancelBoostBrightnessNormal, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrCancelBoostBrightnessNormal: fun is start"); + bool isSucc = DisplayPowerMgrClient::GetInstance().BoostBrightness(50000); + EXPECT_TRUE(isSucc); + sleep(1); + bool isCancel = DisplayPowerMgrClient::GetInstance().CancelBoostBrightness(); + EXPECT_TRUE(isCancel); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrCancelBoostBrightnessNormal: fun is end"); +} + +/** + * @tc.name: BoostAndOverrideMutuallyExclusive1 + * @tc.desc: Test BoostBrightness and OverrideBrightness are mutually exclusive + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, BoostAndOverrideMutuallyExclusive1, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "BoostAndOverrideMutuallyExclusive: fun is start"); + bool isBoost = DisplayPowerMgrClient::GetInstance().BoostBrightness(50000); + EXPECT_TRUE(isBoost); + sleep(1); + bool isOverride = DisplayPowerMgrClient::GetInstance().OverrideBrightness(100); + EXPECT_FALSE(isOverride); + bool isRestore = DisplayPowerMgrClient::GetInstance().RestoreBrightness(); + EXPECT_FALSE(isRestore); + DISPLAY_HILOGI(LABEL_TEST, "BoostAndOverrideMutuallyExclusive: fun is end"); +} + +/** + * @tc.name: BoostAndOverrideMutuallyExclusive2 + * @tc.desc: Test BoostBrightness and OverrideBrightness are mutually exclusive + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, BoostAndOverrideMutuallyExclusive2, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "BoostAndOverrideMutuallyExclusive2: fun is start"); + bool isOverride = DisplayPowerMgrClient::GetInstance().OverrideBrightness(255); + EXPECT_TRUE(isOverride); + sleep(1); + bool isBoost = DisplayPowerMgrClient::GetInstance().BoostBrightness(50000); + EXPECT_FALSE(isBoost); + bool isCancel = DisplayPowerMgrClient::GetInstance().CancelBoostBrightness(); + EXPECT_FALSE(isCancel); + DISPLAY_HILOGI(LABEL_TEST, "BoostAndOverrideMutuallyExclusive2: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrBoostBrightnessTimeout1 + * @tc.desc: Test BoostBrightness timeout restore system brightness + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrBoostBrightnessTimeout1, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessTimeout1: fun is start"); + uint32_t beforeValue = DisplayPowerMgrClient::GetInstance().GetBrightness(); + bool isBoost = DisplayPowerMgrClient::GetInstance().BoostBrightness(1000); + EXPECT_TRUE(isBoost); + sleep(2); + uint32_t currentValue = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(beforeValue, currentValue); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessTimeout1: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrBoostBrightnessTimeout2 + * @tc.desc: Test BoostBrightness timeout brightness adjustment can be called + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrBoostBrightnessTimeout2, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessTimeout2: fun is start"); + bool isBoost = DisplayPowerMgrClient::GetInstance().BoostBrightness(1000); + EXPECT_TRUE(isBoost); + sleep(2); + bool isSet = DisplayPowerMgrClient::GetInstance().SetBrightness(100); + EXPECT_TRUE(isSet); + uint32_t currentValue = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(100, currentValue); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessTimeout2: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrBoostBrightnessCancel1 + * @tc.desc: Test BoostBrightness Cancel restore system brightness + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrBoostBrightnessCancel1, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessCancel1: fun is start"); + uint32_t beforeValue = DisplayPowerMgrClient::GetInstance().GetBrightness(); + bool isBoost = DisplayPowerMgrClient::GetInstance().BoostBrightness(10000); + EXPECT_TRUE(isBoost); + sleep(1); + bool isCancel = DisplayPowerMgrClient::GetInstance().CancelBoostBrightness(); + EXPECT_TRUE(isCancel); + sleep(1); + uint32_t currentValue = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(beforeValue, currentValue) << "beforeValue: " << beforeValue << " currentVal: " << currentValue; + DISPLAY_HILOGI(LABEL_TEST, "BoostBrightnessCancel1: beforeValue: %{public}d, currentVal: %{public}d", + beforeValue, currentValue); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessCancel1: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrBoostBrightnessCancel2 + * @tc.desc: Test BoostBrightness Cancel brightness adjustment can be called + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrBoostBrightnessCancel2, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessCancel2: fun is start"); + bool isBoost = DisplayPowerMgrClient::GetInstance().BoostBrightness(10000); + EXPECT_TRUE(isBoost); + sleep(1); + bool isCancel = DisplayPowerMgrClient::GetInstance().CancelBoostBrightness(); + EXPECT_TRUE(isCancel); + bool isSet = DisplayPowerMgrClient::GetInstance().SetBrightness(111); + EXPECT_TRUE(isSet); + uint32_t currentValue = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(111, currentValue); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessCancel2: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrBoostBrightnessNotAdjust + * @tc.desc: Test BoostBrightness do not adjust brightness + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrBoostBrightnessNotAdjust, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessNotAdjust: fun is start"); + bool isBoost = DisplayPowerMgrClient::GetInstance().BoostBrightness(10000); + EXPECT_TRUE(isBoost); + sleep(1); + bool isSet = DisplayPowerMgrClient::GetInstance().SetBrightness(100); + EXPECT_FALSE(isSet); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessNotAdjust: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrBoostBrightnessScreenOff + * @tc.desc: Test BoostBrightness off Disable the call + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrBoostBrightnessScreenOff, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessScreenOff: fun is start"); + DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_OFF); + bool isBoost = DisplayPowerMgrClient::GetInstance().BoostBrightness(10000); + EXPECT_FALSE(isBoost); + bool isCancel = DisplayPowerMgrClient::GetInstance().CancelBoostBrightness(); + EXPECT_FALSE(isCancel); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessScreenOff: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrBoostBrightnessMultipleCalls + * @tc.desc: Test BoostBrightness multiple calls + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrBrightnessTest, DisplayPowerMgrBoostBrightnessMultipleCalls, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessMultipleCalls: fun is start"); + const uint32_t COUNT = 3; + const uint32_t TIMEOUT = 1000; + for (uint32_t i = 0; i < COUNT; ++i) { + bool isBoost = DisplayPowerMgrClient::GetInstance().BoostBrightness(TIMEOUT); + EXPECT_TRUE(isBoost); + } + // Brightness adjustment is not allowed + bool isSet = DisplayPowerMgrClient::GetInstance().SetBrightness(123); + EXPECT_FALSE(isSet); + // Wait for a timeout + sleep(COUNT + 1); + bool isCancel = DisplayPowerMgrClient::GetInstance().CancelBoostBrightness(); + EXPECT_FALSE(isCancel); + isSet = DisplayPowerMgrClient::GetInstance().SetBrightness(222); + EXPECT_TRUE(isSet); + uint32_t currentVal = DisplayPowerMgrClient::GetInstance().GetBrightness(); + EXPECT_EQ(222, currentVal); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrBoostBrightnessMultipleCalls: fun is end"); +} } \ No newline at end of file diff --git a/test/unittest/common/native/src/display_power_mgr_service_test.cpp b/test/unittest/common/native/src/display_power_mgr_service_test.cpp index 827f9f96f0691fbbe5803b961878f9c692093dcb..c075d0f9b460ead365d7cd36f5a99e8b7f6c1cce 100644 --- a/test/unittest/common/native/src/display_power_mgr_service_test.cpp +++ b/test/unittest/common/native/src/display_power_mgr_service_test.cpp @@ -105,7 +105,6 @@ HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService006, TestSize.Level0) * @tc.desc: Test set brightness * @tc.type: FUNC */ -#ifdef SHIELDING HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService007, TestSize.Level0) { DISPLAY_HILOGI(LABEL_TEST, "SetBrightness: fun is start"); @@ -122,8 +121,6 @@ HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService007, TestSize.Level0) sleep(1); EXPECT_TRUE(ret); } -#endif - /** * @tc.name: DisplayPowerMgrService008