1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "brightness_action.h"
17 #ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
18 #include <hisysevent.h>
19 #endif
20 #include <ipc_skeleton.h>
21 
22 #include "display_log.h"
23 #include "display_manager_lite.h"
24 #include "dm_common.h"
25 #include "screen_manager_lite.h"
26 
27 namespace OHOS {
28 namespace DisplayPowerMgr {
BrightnessAction(uint32_t displayId)29 BrightnessAction::BrightnessAction(uint32_t displayId) : mDisplayId(displayId)
30 {}
31 
GetDisplayId()32 uint32_t BrightnessAction::GetDisplayId()
33 {
34     return mDisplayId;
35 }
36 
SetDisplayId(uint32_t displayId)37 void BrightnessAction::SetDisplayId(uint32_t displayId)
38 {
39     mDisplayId = displayId;
40 }
41 
GetDisplayState()42 DisplayState BrightnessAction::GetDisplayState()
43 {
44     DisplayState state = DisplayState::DISPLAY_UNKNOWN;
45     Rosen::ScreenPowerState powerState = Rosen::ScreenManagerLite::GetInstance().GetScreenPower(mDisplayId);
46     DISPLAY_HILOGI(FEAT_STATE, "ScreenPowerState=%{public}d", static_cast<uint32_t>(powerState));
47     switch (powerState) {
48         case Rosen::ScreenPowerState::POWER_ON:
49             state = DisplayState::DISPLAY_ON;
50             break;
51         case Rosen::ScreenPowerState::POWER_STAND_BY:
52             state = DisplayState::DISPLAY_DIM;
53             break;
54         case Rosen::ScreenPowerState::POWER_SUSPEND:
55             state = DisplayState::DISPLAY_SUSPEND;
56             break;
57         case Rosen::ScreenPowerState::POWER_OFF:
58             state = DisplayState::DISPLAY_OFF;
59             break;
60         default:
61             break;
62     }
63     DISPLAY_HILOGI(FEAT_STATE, "state=%{public}u displayId=%{public}u", static_cast<uint32_t>(state), mDisplayId);
64     return state;
65 }
66 
GetBrightness()67 uint32_t BrightnessAction::GetBrightness()
68 {
69     std::lock_guard lock(mMutexBrightness);
70     std::string identity = IPCSkeleton::ResetCallingIdentity();
71     mBrightness = Rosen::DisplayManagerLite::GetInstance().GetScreenBrightness(mDisplayId);
72     IPCSkeleton::SetCallingIdentity(identity);
73     DISPLAY_HILOGD(FEAT_BRIGHTNESS, "displayId=%{public}u, brightness=%{public}u", mDisplayId, mBrightness);
74     return mBrightness;
75 }
76 
SetBrightness(uint32_t value)77 bool BrightnessAction::SetBrightness(uint32_t value)
78 {
79     return SetBrightness(mDisplayId, value);
80 }
81 
SetBrightness(uint32_t displayId, uint32_t value)82 bool BrightnessAction::SetBrightness(uint32_t displayId, uint32_t value)
83 {
84     DISPLAY_HILOGI(FEAT_BRIGHTNESS, "SetBrightness mDisplayId=%{public}u, displayId=%{public}u ,brightness=%{public}u",
85         mDisplayId, displayId, value);
86     DISPLAY_HILOGD(FEAT_BRIGHTNESS, "displayId=%{public}u, brightness=%{public}u", displayId, value);
87     std::string identity = IPCSkeleton::ResetCallingIdentity();
88     bool isSucc = Rosen::DisplayManagerLite::GetInstance().SetScreenBrightness(displayId, value);
89     IPCSkeleton::SetCallingIdentity(identity);
90     std::lock_guard lock(mMutexBrightness);
91     mBrightness = isSucc ? value : mBrightness;
92     return isSucc;
93 }
94 } // namespace DisplayPowerMgr
95 } // namespace OHOS
96