diff --git a/frameworks/native/display_power_mgr_client.cpp b/frameworks/native/display_power_mgr_client.cpp index cd18d42f5249aed16de02dd083336d400cd76f2e..4b2f261a8104e2e05ae85b6e0cf293b0bd859c1e 100644 --- a/frameworks/native/display_power_mgr_client.cpp +++ b/frameworks/native/display_power_mgr_client.cpp @@ -72,13 +72,14 @@ void DisplayPowerMgrClient::OnRemoteDied(const wptr& remote) } } -bool DisplayPowerMgrClient::SetDisplayState(DisplayState state, uint32_t id) +bool DisplayPowerMgrClient::SetDisplayState(DisplayState state, + PowerMgr::StateChangeReason reason, uint32_t id) { auto proxy = GetProxy(); if (proxy == nullptr) { return false; } - return proxy->SetDisplayState(id, state); + return proxy->SetDisplayState(id, state, static_cast(reason)); } DisplayState DisplayPowerMgrClient::GetDisplayState(uint32_t id) diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index 07652fe947e4e728de021b5498a3f36c1e2bdeaf..ce29a338ca2dc5dd37c13a6cd02f7de0a9fd1cd3 100644 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -16,6 +16,7 @@ import("//base/powermgr/display_manager/displaymgr.gni") config("displaymgr_private_config") { include_dirs = [ "${displaymgr_service_path}/zidl/include", + "//base/powermgr/power_manager/interfaces/innerkits/native/include", "//utils/system/safwk/native/include", ] } @@ -27,6 +28,7 @@ config("displaymgr_public_config") { ohos_shared_library("displaymgr") { sources = [ "${displaymgr_framework_path}/native/display_power_mgr_client.cpp", + "${displaymgr_service_path}/zidl/src/display_power_callback_stub.cpp", "${displaymgr_service_path}/zidl/src/display_power_mgr_proxy.cpp", ] diff --git a/interfaces/innerkits/native/include/display_power_callback_stub.h b/interfaces/innerkits/native/include/display_power_callback_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..ebe640c043d6dfd60aeb18248c0b9a467b3176fb --- /dev/null +++ b/interfaces/innerkits/native/include/display_power_callback_stub.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 DISPLAYMGR_DISPLAY_POWER_CALLBACK_STUB_H +#define DISPLAYMGR_DISPLAY_POWER_CALLBACK_STUB_H + +#include + +#include "idisplay_power_mgr.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class DisplayPowerCallbackStub : public IRemoteStub { +public: + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + int32_t OnDisplayStateChangedStub(MessageParcel& data, MessageParcel& reply); +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // DISPLAYMGR_DISPLAY_POWER_CALLBACK_STUB_H diff --git a/interfaces/innerkits/native/include/display_info.h b/interfaces/innerkits/native/include/display_power_info.h similarity index 87% rename from interfaces/innerkits/native/include/display_info.h rename to interfaces/innerkits/native/include/display_power_info.h index 5117781d72160754d113997022efd27998a480c9..a0ef566f0ffd029b78ae849285feb4d2a0197a30 100644 --- a/interfaces/innerkits/native/include/display_info.h +++ b/interfaces/innerkits/native/include/display_power_info.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef DISPLAYMGR_DISPLAY_INFO_H -#define DISPLAYMGR_DISPLAY_INFO_H +#ifndef DISPLAYMGR_DISPLAY_POWER_INFO_H +#define DISPLAYMGR_DISPLAY_POWER_INFO_H namespace OHOS { namespace DisplayPowerMgr { @@ -30,4 +30,4 @@ enum class DisplayState : uint32_t { }; } // namespace DisplayPowerMgr } // namespace OHOS -#endif // DISPLAYMGR_DISPLAY_INFO_H +#endif // DISPLAYMGR_DISPLAY_POWER_INFO_H diff --git a/interfaces/innerkits/native/include/display_power_mgr_client.h b/interfaces/innerkits/native/include/display_power_mgr_client.h index 9e434809a8f6fc868889d736c3321f45a5d3e40f..e16fa0441b42aab3e53e5e04ef426b31c69d0f7b 100644 --- a/interfaces/innerkits/native/include/display_power_mgr_client.h +++ b/interfaces/innerkits/native/include/display_power_mgr_client.h @@ -20,9 +20,10 @@ #include #include -#include "display_info.h" +#include "display_power_info.h" #include "idisplay_power_callback.h" #include "idisplay_power_mgr.h" +#include "power_state_machine_info.h" namespace OHOS { namespace DisplayPowerMgr { @@ -30,7 +31,9 @@ class DisplayPowerMgrClient : public DelayedRefSingleton DECLARE_DELAYED_REF_SINGLETON(DisplayPowerMgrClient); public: - bool SetDisplayState(DisplayState state, uint32_t id = 0); + bool SetDisplayState(DisplayState state, + PowerMgr::StateChangeReason reason = PowerMgr::StateChangeReason::STATE_CHANGE_REASON_UNKNOWN, + uint32_t id = 0); DisplayState GetDisplayState(uint32_t id = 0); std::vector GetDisplayIds(); int32_t GetMainDisplayId(); diff --git a/interfaces/innerkits/native/include/idisplay_power_callback.h b/interfaces/innerkits/native/include/idisplay_power_callback.h index ff1ac202815fb93f71ebce209fc105be9f2e8896..dd9fc4ef64ef1214a4c36fce22b23387683e2f76 100644 --- a/interfaces/innerkits/native/include/idisplay_power_callback.h +++ b/interfaces/innerkits/native/include/idisplay_power_callback.h @@ -19,13 +19,16 @@ #include #include -#include "display_info.h" +#include "display_power_info.h" namespace OHOS { namespace DisplayPowerMgr { class IDisplayPowerCallback : public IRemoteBroker { public: - virtual void OnDisplayStateChanged(DisplayState state) = 0; + enum { + ON_DISPLAY_STATE_CHANGED = 0, + }; + virtual void OnDisplayStateChanged(uint32_t displayId, DisplayState state) = 0; DECLARE_INTERFACE_DESCRIPTOR(u"ohos.powermgr.IDisplayPowerCallback"); }; diff --git a/interfaces/innerkits/native/include/idisplay_power_mgr.h b/interfaces/innerkits/native/include/idisplay_power_mgr.h index 1bae7cde7f22dc9bb9163b1588d960b5a2e6c4aa..f093dec6dbb610ab17afe6b98dcaf89e9cf93e14 100644 --- a/interfaces/innerkits/native/include/idisplay_power_mgr.h +++ b/interfaces/innerkits/native/include/idisplay_power_mgr.h @@ -19,7 +19,7 @@ #include #include -#include "display_info.h" +#include "display_power_info.h" #include "idisplay_power_callback.h" namespace OHOS { @@ -38,7 +38,7 @@ public: REGISTER_CALLBACK, }; - virtual bool SetDisplayState(uint32_t id, DisplayState state) = 0; + virtual bool SetDisplayState(uint32_t id, DisplayState state, uint32_t reason) = 0; virtual DisplayState GetDisplayState(uint32_t id) = 0; virtual std::vector GetDisplayIds() = 0; virtual uint32_t GetMainDisplayId() = 0; diff --git a/interfaces/kits/js/napi/BUILD.gn b/interfaces/kits/js/napi/BUILD.gn index 2636e269b132c113647bc02b3e7abbe5df922e23..24566a145b3810a84cb1886920aea6fe8080d7c2 100644 --- a/interfaces/kits/js/napi/BUILD.gn +++ b/interfaces/kits/js/napi/BUILD.gn @@ -17,6 +17,7 @@ ohos_shared_library("brightness") { sources = [ "${displaymgr_framework_path}/napi/brightness.cpp" ] include_dirs = [ + "//base/powermgr/power_manager/interfaces/innerkits/native/include", "//foundation/ace/napi/native_engine", "//foundation/ace/napi/interfaces/kits", "//third_party/node/src", diff --git a/ohos.build b/ohos.build index 8a5963263eaa293c7389151a21cf3d7c04c161fc..9a4169c47a00fcad904ddb0b9964408be309500b 100644 --- a/ohos.build +++ b/ohos.build @@ -13,7 +13,7 @@ "name": "//base/powermgr/display_manager/interfaces/innerkits:displaymgr", "header": { "header_files": [ - "display_info.h", + "display_power_info.h", "display_power_mgr_client.h" ], "header_base": "//base/powermgr/display_manager/interfaces/innerkits/native/include" diff --git a/service/BUILD.gn b/service/BUILD.gn index a08cb4438820ff9d059b82d59d9a32afb602d9fc..751f202323c683fc838d9d8bcd1571fc06e13849 100644 --- a/service/BUILD.gn +++ b/service/BUILD.gn @@ -16,7 +16,9 @@ import("//base/powermgr/display_manager/displaymgr.gni") config("displaymgr_private_config") { include_dirs = [ "//utils/system/safwk/native/include", + "//base/powermgr/power_manager/interfaces/innerkits/native/include", "//base/sensors/sensor/interfaces/native/include", + "//foundation/multimedia/image_standard/interfaces/innerkits/include", ] } @@ -34,6 +36,7 @@ ohos_shared_library("displaymgrservice") { "native/src/gradual_animator.cpp", "native/src/screen_action.cpp", "native/src/screen_controller.cpp", + "zidl/src/display_power_callback_proxy.cpp", "zidl/src/display_power_mgr_stub.cpp", ] @@ -47,7 +50,10 @@ ohos_shared_library("displaymgrservice") { deps = [ "${displaymgr_native_innerkits_path}:displaymgr", "//drivers/peripheral/display/hal:hdi_display_device", + "//foundation/graphic/standard:libsurface", "//foundation/graphic/standard:libwmservice", + "//foundation/windowmanager/utils:libwmutil", + "//foundation/windowmanager/wm:libwm", "//utils/native/base:utils", ] diff --git a/service/native/include/display_power_mgr_service.h b/service/native/include/display_power_mgr_service.h index e9615f9e41ad31aa3806752f14d528c43adbea0c..dcdcf417dbb493c92b22f97b8483bde14a0fe05f 100644 --- a/service/native/include/display_power_mgr_service.h +++ b/service/native/include/display_power_mgr_service.h @@ -31,7 +31,7 @@ namespace DisplayPowerMgr { class DisplayPowerMgrService : public DisplayPowerMgrStub { public: virtual ~DisplayPowerMgrService(); - virtual bool SetDisplayState(uint32_t id, DisplayState state) override; + virtual bool SetDisplayState(uint32_t id, DisplayState state, uint32_t reason) override; virtual DisplayState GetDisplayState(uint32_t id) override; virtual std::vector GetDisplayIds() override; virtual uint32_t GetMainDisplayId() override; @@ -41,8 +41,15 @@ public: 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; + void NotifyStateChangeCallback(uint32_t displayId, DisplayState state); private: + class CallbackDeathRecipient : public IRemoteObject::DeathRecipient { + public: + CallbackDeathRecipient() = default; + virtual ~CallbackDeathRecipient() = default; + virtual void OnRemoteDied(const wptr& remote); + }; static const uint32_t AUTO_ADJUST_BRIGHTNESS_DURATION = 1000; static const uint32_t SAMPLING_RATE = 100000000; static const int32_t BRIGHTNESS_CHANGE_MIN = 5; @@ -64,11 +71,14 @@ private: bool CalculateBrightness(float scalar, int32_t& brightness); int32_t GetBrightnessFromLightScalar(float scalar); - std::map> controllerMap_; + std::shared_ptr action_; + + std::map> controllerMap_; bool supportLightSensor_ {false}; bool autoBrightness_ {false}; SensorUser user_; sptr callback_; + sptr cbDeathRecipient_; time_t lastLuxTime_ {0}; float lastLux_ {0}; diff --git a/service/native/include/screen_action.h b/service/native/include/screen_action.h index e9682becf59b3fdc62e23925dae6e89a1d15c0ae..6439ef9e29945aea2ceee8c905057f3490f7198a 100644 --- a/service/native/include/screen_action.h +++ b/service/native/include/screen_action.h @@ -22,7 +22,7 @@ #include -#include "display_info.h" +#include "display_power_info.h" namespace OHOS { namespace DisplayPowerMgr { @@ -30,11 +30,14 @@ class ScreenAction { public: ScreenAction(); ~ScreenAction() = default; - std::vector GetDisplayIds(); - DisplayState GetPowerState(uint32_t devId); - bool SetPowerState(uint32_t devId, DisplayState state); - uint32_t GetBrightness(uint32_t devId); - bool SetBrightness(uint32_t devId, uint32_t value); + uint64_t GetDefaultDisplayId(); + std::vector GetDisplayIds(); + DisplayState GetPowerState(uint64_t devId); + bool SetDisplayState(uint64_t devId, DisplayState state, + std::function callback); + bool SetDisplayPower(uint64_t devId, DisplayState state, uint32_t reason); + uint32_t GetBrightness(uint64_t devId); + bool SetBrightness(uint64_t devId, uint32_t value); private: struct DeviceFuncCloser { @@ -53,7 +56,7 @@ private: static constexpr int32_t MAX_BRIGHTNESS = 255; static constexpr int32_t MIN_BRIGHTNESS = 6; - std::vector devIds_; + std::vector devIds_; DeviceFuncPtr hdiFuncs_; }; } // namespace DisplayPowerMgr diff --git a/service/native/include/screen_controller.h b/service/native/include/screen_controller.h index 1d4653708780ac52152032db32f0aec43e72d98d..a8e9d7ddeff617feeb9b485ff586c8bc4812a765 100644 --- a/service/native/include/screen_controller.h +++ b/service/native/include/screen_controller.h @@ -19,7 +19,7 @@ #include #include -#include "display_info.h" +#include "display_power_info.h" #include "gradual_animator.h" #include "screen_action.h" @@ -29,14 +29,14 @@ class ScreenController : public AnimateCallback, public std::enable_shared_from_this { public: - ScreenController(uint32_t devId, std::shared_ptr action); + ScreenController(uint64_t devId, std::shared_ptr action); virtual ~ScreenController() = default; DisplayState GetState() { return state_; }; - bool UpdateState(DisplayState state); + bool UpdateState(DisplayState state, uint32_t reason); bool UpdateStateConfig(DisplayState state, uint32_t value); bool UpdateBrightness(uint32_t value, uint32_t duration = 0); bool IsScreenOn(); @@ -46,12 +46,14 @@ public: virtual void OnEnd() override; private: static const uint32_t SCREEN_BRIGHTNESS_UPDATE_DURATION = 200; + void OnStateChanged(DisplayState state); std::mutex mutex_; - const uint32_t devId_; + const uint64_t devId_; DisplayState state_; std::map stateValues_; uint32_t brightness_ {0}; + uint32_t stateChangeReason_ {0}; std::shared_ptr action_; std::shared_ptr animator_; }; diff --git a/service/native/src/display_power_mgr_service.cpp b/service/native/src/display_power_mgr_service.cpp index aade314db8088976d28977ba7cf409933376d884..66a1dd58cf694da16444b1489589d43e5434f8d2 100644 --- a/service/native/src/display_power_mgr_service.cpp +++ b/service/native/src/display_power_mgr_service.cpp @@ -26,13 +26,15 @@ namespace DisplayPowerMgr { DisplayPowerMgrService::DisplayPowerMgrService() { DISPLAY_HILOGI(MODULE_SERVICE, "DisplayPowerMgrService Create"); - std::shared_ptr screenAction = std::make_shared(); - std::vector devIds = screenAction->GetDisplayIds(); + action_ = std::make_shared(); + std::vector devIds = action_->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)); + DISPLAY_HILOGI(MODULE_SERVICE, "find display: %{public}d", static_cast(devIds[i])); + controllerMap_.emplace(devIds[i], std::make_shared(devIds[i], action_)); } + callback_ = nullptr; + cbDeathRecipient_ = nullptr; InitSensors(); } @@ -40,14 +42,14 @@ DisplayPowerMgrService::~DisplayPowerMgrService() { } -bool DisplayPowerMgrService::SetDisplayState(uint32_t id, DisplayState state) +bool DisplayPowerMgrService::SetDisplayState(uint32_t id, DisplayState state, uint32_t reason) { DISPLAY_HILOGI(MODULE_SERVICE, "SetDisplayState %{public}d, %{public}d", id, state); auto iterater = controllerMap_.find(id); if (iterater == controllerMap_.end()) { return false; } - return iterater->second->UpdateState(state); + return iterater->second->UpdateState(state, reason); } DisplayState DisplayPowerMgrService::GetDisplayState(uint32_t id) @@ -73,8 +75,8 @@ std::vector DisplayPowerMgrService::GetDisplayIds() 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; + uint64_t id = action_->GetDefaultDisplayId(); + return static_cast(id); } bool DisplayPowerMgrService::SetBrightness(uint32_t id, int32_t value) @@ -144,10 +146,28 @@ bool DisplayPowerMgrService::SetStateConfig(uint32_t id, DisplayState state, int bool DisplayPowerMgrService::RegisterCallback(sptr callback) { DISPLAY_HILOGI(MODULE_SERVICE, "RegisterCallback"); + if (callback_ != nullptr) { + DISPLAY_HILOGI(MODULE_SERVICE, "Callback function exist"); + return false; + } callback_ = callback; + sptr remote = callback_->AsObject(); + if (!remote->IsProxyObject()) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Callback is not proxy"); + return false; + } + if (cbDeathRecipient_ == nullptr) { + cbDeathRecipient_ = new CallbackDeathRecipient(); + } + remote->AddDeathRecipient(cbDeathRecipient_); return true; } +void DisplayPowerMgrService::NotifyStateChangeCallback(uint32_t displayId, DisplayState state) +{ + callback_->OnDisplayStateChanged(displayId, state); +} + int32_t DisplayPowerMgrService::Dump(int32_t fd, const std::vector& args) { std::string result("DISPLAY POWER MANAGER DUMP:\n"); @@ -314,5 +334,17 @@ int32_t DisplayPowerMgrService::GetBrightnessFromLightScalar(float scalar) return brightness; } + +void DisplayPowerMgrService::CallbackDeathRecipient::OnRemoteDied(const wptr& remote) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "CallbackDeathRecipient OnRemoteDied"); + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + DISPLAY_HILOGI(MODULE_SERVICE, "OnRemoteDied no service"); + return; + } + + pms->callback_ = nullptr; +} } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/src/screen_action.cpp b/service/native/src/screen_action.cpp index 7c5305f4f80d34384f09f4a6425f58e99e88964a..c52bb69829e74254ff9cf66b6bb46c66a1ab79d7 100644 --- a/service/native/src/screen_action.cpp +++ b/service/native/src/screen_action.cpp @@ -16,142 +16,141 @@ #include "screen_action.h" #include "display_common.h" +#include "display_manager.h" #include "display_type.h" #include "hilog_wrapper.h" #include "window_manager_service_client.h" +#include "display_power_info.h" namespace OHOS { namespace DisplayPowerMgr { ScreenAction::ScreenAction() { - DeviceFuncs *f = NULL; - - int32_t ret = DeviceInitialize(&f); - if (ret != DISPLAY_SUCCESS) { - DISPLAY_HILOGE(MODULE_SERVICE, "Failed to init device"); - return; - } - devIds_.push_back(0); - hdiFuncs_ = DeviceFuncPtr(f); DISPLAY_HILOGI(MODULE_SERVICE, "Succeed to init"); } -std::vector ScreenAction::GetDisplayIds() +uint64_t ScreenAction::GetDefaultDisplayId() +{ + DISPLAY_HILOGI(MODULE_SERVICE, "GetDefaultDisplayId"); + return Rosen::DisplayManager::GetInstance().GetDefaultDisplayId(); +} + +std::vector ScreenAction::GetDisplayIds() { + DISPLAY_HILOGI(MODULE_SERVICE, "GetDisplayIds"); + devIds_ = Rosen::DisplayManager::GetInstance().GetAllDisplayIds(); + if (devIds_.empty()) { + devIds_.push_back(0); + } return devIds_; } -DisplayState ScreenAction::GetPowerState(uint32_t devId) +DisplayState ScreenAction::GetPowerState(uint64_t devId) { - DisplayState ret = DisplayState::DISPLAY_UNKNOWN; + DISPLAY_HILOGI(MODULE_SERVICE, "GetPowerState: %{public}d", static_cast(devId)); - auto wmsc = WindowManagerServiceClient::GetInstance(); - wmsc->Init(); - sptr wms = wmsc->GetService(); - if (wms == nullptr) { - DISPLAY_HILOGE(MODULE_SERVICE, "FAILED to get service from WindowManager Client"); - return DisplayState::DISPLAY_UNKNOWN; + DisplayState ret = DisplayState::DISPLAY_UNKNOWN; + Rosen::DisplayPowerState state = Rosen::DisplayManager::GetInstance() + .GetScreenPower(devId); + DISPLAY_HILOGI(MODULE_SERVICE, "GetPowerState: %{public}d", static_cast(state)); + switch (state) { + case Rosen::DisplayPowerState::POWER_ON: + ret = DisplayState::DISPLAY_ON; + break; + case Rosen::DisplayPowerState::POWER_STAND_BY: + ret = DisplayState::DISPLAY_DIM; + break; + case Rosen::DisplayPowerState::POWER_SUSPEND: + ret = DisplayState::DISPLAY_SUSPEND; + break; + case Rosen::DisplayPowerState::POWER_OFF: + ret = DisplayState::DISPLAY_OFF; + break; + default: + break; } - // auto promise = wms->GetDisplayPower(devId)->Await(); - // if (promise.wret != WM_OK) { - // DISPLAY_HILOGE(MODULE_SERVICE, "GetPowerState failed: %{public}d", promise.wret); - // return ret; - // } - - // switch (promise.status) { - // case POWER_STATUS_ON: - // ret = DisplayState::DISPLAY_ON; - // break; - // case POWER_STATUS_STANDBY: - // ret = DisplayState::DISPLAY_DIM; - // break; - // case POWER_STATUS_SUSPEND: - // ret = DisplayState::DISPLAY_SUSPEND; - // break; - // case POWER_STATUS_OFF: - // ret = DisplayState::DISPLAY_OFF; - // break; - // default: - // break; - // } return ret; } -bool ScreenAction::SetPowerState(uint32_t devId, DisplayState state) +bool ScreenAction::SetDisplayState(uint64_t devId, DisplayState state, + std::function callback) { - DISPLAY_HILOGI(MODULE_SERVICE, "SetDisplayPower: devId=%{public}d, state=%{public}d", - devId, static_cast(state)); - auto wmsc = WindowManagerServiceClient::GetInstance(); - wmsc->Init(); - sptr wms = wmsc->GetService(); - if (wms == nullptr) { - DISPLAY_HILOGE(MODULE_SERVICE, "FAILED to get service from WindowManager Client"); - return false; + DISPLAY_HILOGI(MODULE_SERVICE, "SetDisplayState: devId=%{public}d, state=%{public}d", + static_cast(devId), static_cast(state)); + + Rosen::DisplayState rds = Rosen::DisplayState::UNKNOWN; + switch (state) { + case DisplayState::DISPLAY_ON: + rds = Rosen::DisplayState::ON; + break; + case DisplayState::DISPLAY_OFF: + rds = Rosen::DisplayState::OFF; + break; + default: + break; } + bool ret = Rosen::DisplayManager::GetInstance().SetDisplayState(rds, + [callback](Rosen::DisplayState rosenState) { + DISPLAY_HILOGE(MODULE_SERVICE, "SetDisplayState Callback:%{public}d", + static_cast(rosenState)); + DisplayState state = DisplayState::DISPLAY_UNKNOWN; + switch (rosenState) { + case Rosen::DisplayState::ON: + state = DisplayState::DISPLAY_ON; + break; + case Rosen::DisplayState::OFF: + state = DisplayState::DISPLAY_OFF; + break; + default: + return; + } + callback(state); + }); + DISPLAY_HILOGE(MODULE_SERVICE, "SetDisplayState:%{public}d", ret); + return ret; +} - DispPowerStatus status = POWER_STATUS_BUTT; +bool ScreenAction::SetDisplayPower(uint64_t devId, DisplayState state, uint32_t reason) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "SetDisplayPower: devId=%{public}d, state=%{public}d, state=%{public}d", + static_cast(devId), static_cast(state), reason); + Rosen::DisplayPowerState status = Rosen::DisplayPowerState::INVALID_STATE; switch (state) { case DisplayState::DISPLAY_ON: - status = POWER_STATUS_ON; + status = Rosen::DisplayPowerState::POWER_ON; break; case DisplayState::DISPLAY_DIM: - status = POWER_STATUS_STANDBY; + status = Rosen::DisplayPowerState::POWER_STAND_BY; break; case DisplayState::DISPLAY_SUSPEND: - status = POWER_STATUS_SUSPEND; + status = Rosen::DisplayPowerState::POWER_SUSPEND; break; case DisplayState::DISPLAY_OFF: - status = POWER_STATUS_OFF; + status = Rosen::DisplayPowerState::POWER_OFF; break; default: break; } - - // auto wret = wms->SetDisplayPower(devId, status)->Await(); - // if (wret != WM_OK) { - // DISPLAY_HILOGE(MODULE_SERVICE, "SetDisplayPower failed: %{public}d", wret); - // return false; - // } - + bool ret = Rosen::DisplayManager::GetInstance().SetScreenPowerForAll(status, + Rosen::PowerStateChangeReason::POWER_BUTTON); + DISPLAY_HILOGE(MODULE_SERVICE, "SetScreenPowerForAll:%{public}d", ret); return true; } -uint32_t ScreenAction::GetBrightness(uint32_t devId) +uint32_t ScreenAction::GetBrightness(uint64_t devId) { - auto wmsc = WindowManagerServiceClient::GetInstance(); - wmsc->Init(); - sptr wms = wmsc->GetService(); - if (wms == nullptr) { - DISPLAY_HILOGE(MODULE_SERVICE, "FAILED to get service from WindowManager Client"); - return 0; - } - // auto promise = wms->GetDisplayBacklight(devId)->Await(); - // if (promise.wret != WM_OK) { - // DISPLAY_HILOGE(MODULE_SERVICE, "GetBrightness failed: %{public}d", promise.wret); - // return 0; - // } - - // return promise.level; - return 0; + DISPLAY_HILOGI(MODULE_SERVICE, "GetBrightness: %{public}d", static_cast(devId)); + return Rosen::DisplayManager::GetInstance() + .GetScreenBrightness(static_cast(devId)); } -bool ScreenAction::SetBrightness(uint32_t devId, uint32_t value) +bool ScreenAction::SetBrightness(uint64_t devId, uint32_t value) { - auto wmsc = WindowManagerServiceClient::GetInstance(); - wmsc->Init(); - sptr wms = wmsc->GetService(); - if (wms == nullptr) { - DISPLAY_HILOGE(MODULE_SERVICE, "FAILED to get service from WindowManager Client"); - return false; - } - // auto wret = wms->SetDisplayBacklight(devId, value)->Await(); - // if (wret != WM_OK) { - // DISPLAY_HILOGE(MODULE_SERVICE, "SetBrightness failed: %{public}d", wret); - // return false; - // } - - return true; + DISPLAY_HILOGI(MODULE_SERVICE, "SetBrightness: %{public}d, %{public}d", + static_cast(devId), value); + return Rosen::DisplayManager::GetInstance() + .SetScreenBrightness(static_cast(devId), value); } } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/src/screen_controller.cpp b/service/native/src/screen_controller.cpp index ca77d8919902b387c31d2492e90974333d764c5c..743bdde770ed6479aa3f27e6721268e8a5f73ce0 100644 --- a/service/native/src/screen_controller.cpp +++ b/service/native/src/screen_controller.cpp @@ -16,6 +16,7 @@ #include "screen_controller.h" #include "display_common.h" +#include "display_power_mgr_service.h" #include "hilog_wrapper.h" namespace OHOS { @@ -25,10 +26,10 @@ const int DISPLAY_DIM_BRIGHTNESS = 50; const int DISPLAY_OFF_BRIGHTNESS = 0; const int DISPLAY_SUSPEND_BRIGHTNESS = 50; -ScreenController::ScreenController(uint32_t devId, std::shared_ptr action) +ScreenController::ScreenController(uint64_t devId, std::shared_ptr action) : devId_(devId), state_(DisplayState::DISPLAY_UNKNOWN), action_(action) { - DISPLAY_HILOGI(MODULE_SERVICE, "ScreenController created: %{public}d", devId_); + DISPLAY_HILOGI(MODULE_SERVICE, "ScreenController created: %{public}d", static_cast(devId_)); stateValues_.emplace(DisplayState::DISPLAY_ON, DISPLAY_FULL_BRIGHTNESS); stateValues_.emplace(DisplayState::DISPLAY_DIM, DISPLAY_DIM_BRIGHTNESS); stateValues_.emplace(DisplayState::DISPLAY_OFF, DISPLAY_OFF_BRIGHTNESS); @@ -36,29 +37,43 @@ ScreenController::ScreenController(uint32_t devId, std::shared_ptr animator_ = nullptr; } -bool ScreenController::UpdateState(DisplayState state) +bool ScreenController::UpdateState(DisplayState state, uint32_t reason) { std::lock_guard lock(mutex_); DISPLAY_HILOGI(MODULE_SERVICE, "ScreenController UpdateState: %{public}d, %{public}d", - devId_, static_cast(state)); + static_cast(devId_), static_cast(state)); if (state == state_) { return true; } - bool ret = action_->SetPowerState(devId_, state); - if (!ret) { - DISPLAY_HILOGW(MODULE_SERVICE, "SetPowerState failed state=%{public}d", state); - } - state_ = state; - auto iterator = stateValues_.find(state); - if (iterator != stateValues_.end()) { - ret = action_->SetBrightness(devId_, iterator->second); - if (ret) { - brightness_ = iterator->second; - } else { - DISPLAY_HILOGI(MODULE_SERVICE, "set brightness falied! %{public}d", iterator->second); + switch (state) { + case DisplayState::DISPLAY_ON: // fall through + case DisplayState::DISPLAY_OFF: { + std::function callback = + std::bind(&ScreenController::OnStateChanged, this, std::placeholders::_1); + bool ret = action_->SetDisplayState(devId_, state, callback); + if (!ret) { + DISPLAY_HILOGW(MODULE_SERVICE, "SetDisplayState failed state=%{public}d", state); + return ret; + } + break; + } + case DisplayState::DISPLAY_DIM: // fall through + case DisplayState::DISPLAY_SUSPEND: { + bool ret = action_->SetDisplayPower(devId_, state, stateChangeReason_); + if (!ret) { + DISPLAY_HILOGW(MODULE_SERVICE, "SetDisplayPower failed state=%{public}d", state); + return ret; + } + break; } + default: + break; } + + state_ = state; + stateChangeReason_ = reason; + DISPLAY_HILOGI(MODULE_SERVICE, "Update screen state to %{public}u", state); return true; } @@ -67,7 +82,7 @@ bool ScreenController::UpdateBrightness(uint32_t value, uint32_t duraion) { std::lock_guard lock(mutex_); DISPLAY_HILOGI(MODULE_SERVICE, "ScreenController UpdateBrightness: %{public}d, %{public}d, %{public}d", - devId_, value, duraion); + static_cast(devId_), value, duraion); if (animator_ == nullptr) { std::string name = "ScreenController_" + std::to_string(devId_); std::shared_ptr callback = shared_from_this(); @@ -96,7 +111,7 @@ bool ScreenController::UpdateStateConfig(DisplayState state, uint32_t value) std::lock_guard lock(mutex_); DISPLAY_HILOGI(MODULE_SERVICE, "ScreenController UpdateStateConfig: Id=%{public}d, State=%{public}d, Value=%{public}d", - devId_, static_cast(state), value); + static_cast(devId_), static_cast(state), value); auto iterator = stateValues_.find(state); if (iterator == stateValues_.end()) { DISPLAY_HILOGI(MODULE_SERVICE, "UpdateStateConfig No such state"); @@ -141,5 +156,20 @@ void ScreenController::OnEnd() { DISPLAY_HILOGD(MODULE_SERVICE, "ScreenAnimatorCallback OnEnd"); } + +void ScreenController::OnStateChanged(DisplayState state) +{ + DISPLAY_HILOGD(MODULE_SERVICE, "OnStateChanged %{public}d", state); + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + DISPLAY_HILOGI(MODULE_SERVICE, "OnRemoteDied no service"); + return; + } + + bool ret = action_->SetDisplayPower(devId_, state, stateChangeReason_); + if (ret) { + pms->NotifyStateChangeCallback(devId_, state); + } +} } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/zidl/include/display_power_callback_proxy.h b/service/zidl/include/display_power_callback_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..69f626f1c44713f965182dc47214ee3f3b370baf --- /dev/null +++ b/service/zidl/include/display_power_callback_proxy.h @@ -0,0 +1,38 @@ +/* + * 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 DISPLAYMGR_DISPLAY_POWER_CALLBACK_PROXY_H +#define DISPLAYMGR_DISPLAY_POWER_CALLBACK_PROXY_H + +#include + +#include "idisplay_power_callback.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class DisplayPowerCallbackProxy : public IRemoteProxy { +public: + explicit DisplayPowerCallbackProxy(const sptr& impl) + : IRemoteProxy(impl) {} + ~DisplayPowerCallbackProxy() override = default; + virtual void OnDisplayStateChanged(uint32_t displayId, DisplayState state) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace DisplayPowerMgr +} // namespace OHOS + +#endif // DISPLAYMGR_DISPLAY_POWER_CALLBACK_PROXY_H \ No newline at end of file diff --git a/service/zidl/include/display_power_mgr_proxy.h b/service/zidl/include/display_power_mgr_proxy.h index 93da3c83da858c93bdeeadb41b505f8d022ffede..8d8e5bfb631809aefd915928c06a93c5af7c9565 100644 --- a/service/zidl/include/display_power_mgr_proxy.h +++ b/service/zidl/include/display_power_mgr_proxy.h @@ -28,7 +28,7 @@ public: : IRemoteProxy(impl) {} ~DisplayPowerMgrProxy() override = default; - virtual bool SetDisplayState(uint32_t id, DisplayState state) override; + virtual bool SetDisplayState(uint32_t id, DisplayState state, uint32_t reason) override; virtual DisplayState GetDisplayState(uint32_t id) override; virtual std::vector GetDisplayIds() override; virtual uint32_t GetMainDisplayId() override; diff --git a/service/zidl/src/display_power_callback_proxy.cpp b/service/zidl/src/display_power_callback_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1efb1e1a654b7829a46ae66ed0ed839d27cee8c4 --- /dev/null +++ b/service/zidl/src/display_power_callback_proxy.cpp @@ -0,0 +1,51 @@ +/* + * 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. + */ + +#include "display_power_callback_proxy.h" + +#include "display_common.h" + +namespace OHOS { +namespace DisplayPowerMgr { +void DisplayPowerCallbackProxy::OnDisplayStateChanged(uint32_t displayId, DisplayState state) +{ + sptr remote = Remote(); + RETURN_IF(remote == nullptr); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(DisplayPowerCallbackProxy::GetDescriptor())) { + DISPLAY_HILOGE(MODULE_INNERKIT, + "DisplayPowerCallbackProxy::%{public}s write descriptor failed!", __func__); + return; + } + + WRITE_PARCEL_NO_RET(data, Uint32, displayId); + WRITE_PARCEL_NO_RET(data, Uint32, static_cast(state)); + + int ret = remote->SendRequest(static_cast(IDisplayPowerCallback::ON_DISPLAY_STATE_CHANGED), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(MODULE_INNERKIT, + "PowerMgrProxy::%{public}s SendRequest is failed, error code: %d", __func__, ret); + return; + } + + return; +} +} // namespace DisplayPowerMgr +} // namespace OHOS \ No newline at end of file diff --git a/service/zidl/src/display_power_callback_stub.cpp b/service/zidl/src/display_power_callback_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..563051438c3f7a846b26456f5500eed02e5db042 --- /dev/null +++ b/service/zidl/src/display_power_callback_stub.cpp @@ -0,0 +1,57 @@ +/* + * 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. + */ + +#include "display_power_callback_stub.h" + +#include + +#include "display_common.h" + +namespace OHOS { +namespace DisplayPowerMgr { +int32_t DisplayPowerCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DISPLAY_HILOGD(MODULE_SERVICE, "DisplayPowerCallbackStub::OnRemoteRequest, cmd = %d, flags= %d", + code, option.GetFlags()); + std::u16string descripter = DisplayPowerCallbackStub::GetDescriptor(); + std::u16string remoteDescripter = data.ReadInterfaceToken(); + if (descripter != remoteDescripter) { + DISPLAY_HILOGE(MODULE_SERVICE, + "DisplayPowerCallbackStub::OnRemoteRequest failed, descriptor is not matched!"); + return E_GET_POWER_SERVICE_FAILED; + } + + switch (code) { + case static_cast(IDisplayPowerCallback::ON_DISPLAY_STATE_CHANGED): + return OnDisplayStateChangedStub(data, reply); + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } +} + +int32_t DisplayPowerCallbackStub::OnDisplayStateChangedStub(MessageParcel& data, MessageParcel& reply) +{ + uint32_t id = 0; + uint32_t state = 0; + + READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint32, state, E_READ_PARCEL_ERROR); + + OnDisplayStateChanged(id, static_cast(state)); + return ERR_OK; +} +} // namespace DisplayPowerMgr +} // namespace OHOS \ No newline at end of file diff --git a/service/zidl/src/display_power_mgr_proxy.cpp b/service/zidl/src/display_power_mgr_proxy.cpp index f9d2beeba09644611de5174772ac461641a29d50..5783118525e6c4b0360b2df50307e30e9192c0b1 100644 --- a/service/zidl/src/display_power_mgr_proxy.cpp +++ b/service/zidl/src/display_power_mgr_proxy.cpp @@ -19,7 +19,7 @@ namespace OHOS { namespace DisplayPowerMgr { -bool DisplayPowerMgrProxy::SetDisplayState(uint32_t id, DisplayState state) +bool DisplayPowerMgrProxy::SetDisplayState(uint32_t id, DisplayState state, uint32_t reason) { sptr remote = Remote(); RETURN_IF_WITH_RET(remote == nullptr, false); @@ -36,6 +36,7 @@ bool DisplayPowerMgrProxy::SetDisplayState(uint32_t id, DisplayState state) WRITE_PARCEL_WITH_RET(data, Uint32, id, false); WRITE_PARCEL_WITH_RET(data, Uint32, static_cast(state), false); + WRITE_PARCEL_WITH_RET(data, Uint32, reason, false); int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::SET_DISPLAY_STATE), data, reply, option); diff --git a/service/zidl/src/display_power_mgr_stub.cpp b/service/zidl/src/display_power_mgr_stub.cpp index b709150f143438d8627a753162b0632480ab13e7..77948d46b609e78d65e84d7971f79b3cdce0308a 100644 --- a/service/zidl/src/display_power_mgr_stub.cpp +++ b/service/zidl/src/display_power_mgr_stub.cpp @@ -59,11 +59,13 @@ int32_t DisplayPowerMgrStub::SetDisplayStateStub(MessageParcel& data, MessagePar { uint32_t id = 0; uint32_t state = 0; + uint32_t reason = 0; READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR); READ_PARCEL_WITH_RET(data, Uint32, state, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint32, reason, E_READ_PARCEL_ERROR); - bool ret = SetDisplayState(id, static_cast(state)); + bool ret = SetDisplayState(id, static_cast(state), reason); if (!reply.WriteBool(ret)) { DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetDisplayStateStub return value"); return E_WRITE_PARCEL_ERROR; diff --git a/test/native/unittest/BUILD.gn b/test/native/unittest/BUILD.gn index a5f28fe38b73eeef71efeef78b382df907ac8a0f..7ab4d05089bd8bd323e8f5df512ff99c92884577 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/powermgr/power_manager/interfaces/innerkits/native/include", "//base/sensors/sensor/interfaces/native/include", "//utils/system/safwk/native/include", "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include",