• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *            http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_POWERHALWRAPPER_H
18 #define ANDROID_POWERHALWRAPPER_H
19 
20 #include <android-base/thread_annotations.h>
21 #include <android/hardware/power/1.1/IPower.h>
22 #include <android/hardware/power/Boost.h>
23 #include <android/hardware/power/IPower.h>
24 #include <android/hardware/power/IPowerHintSession.h>
25 #include <android/hardware/power/Mode.h>
26 
27 namespace android {
28 
29 namespace power {
30 
31 // State of Power HAL support for individual apis.
32 enum class HalSupport {
33     UNKNOWN = 0,
34     ON = 1,
35     OFF = 2,
36 };
37 
38 // Result of a call to the Power HAL wrapper, holding data if successful.
39 template <typename T>
40 class HalResult {
41 public:
ok(T value)42     static HalResult<T> ok(T value) { return HalResult(value); }
failed(std::string msg)43     static HalResult<T> failed(std::string msg) {
44         return HalResult(std::move(msg), /* unsupported= */ false);
45     }
unsupported()46     static HalResult<T> unsupported() { return HalResult("", /* unsupported= */ true); }
47 
fromStatus(binder::Status status,T data)48     static HalResult<T> fromStatus(binder::Status status, T data) {
49         if (status.exceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) {
50             return HalResult<T>::unsupported();
51         }
52         if (status.isOk()) {
53             return HalResult<T>::ok(data);
54         }
55         return HalResult<T>::failed(std::string(status.toString8().c_str()));
56     }
57     static HalResult<T> fromStatus(hardware::power::V1_0::Status status, T data);
58 
59     template <typename R>
60     static HalResult<T> fromReturn(hardware::Return<R>& ret, T data);
61 
62     template <typename R>
63     static HalResult<T> fromReturn(hardware::Return<R>& ret, hardware::power::V1_0::Status status,
64                                    T data);
65 
66     // This will throw std::bad_optional_access if this result is not ok.
value()67     const T& value() const { return mValue.value(); }
isOk()68     bool isOk() const { return !mUnsupported && mValue.has_value(); }
isFailed()69     bool isFailed() const { return !mUnsupported && !mValue.has_value(); }
isUnsupported()70     bool isUnsupported() const { return mUnsupported; }
errorMessage()71     const char* errorMessage() const { return mErrorMessage.c_str(); }
72 
73 private:
74     std::optional<T> mValue;
75     std::string mErrorMessage;
76     bool mUnsupported;
77 
HalResult(T value)78     explicit HalResult(T value)
79           : mValue(std::make_optional(value)), mErrorMessage(), mUnsupported(false) {}
HalResult(std::string errorMessage,bool unsupported)80     explicit HalResult(std::string errorMessage, bool unsupported)
81           : mValue(), mErrorMessage(std::move(errorMessage)), mUnsupported(unsupported) {}
82 };
83 
84 // Empty result of a call to the Power HAL wrapper.
85 template <>
86 class HalResult<void> {
87 public:
ok()88     static HalResult<void> ok() { return HalResult(); }
failed(std::string msg)89     static HalResult<void> failed(std::string msg) { return HalResult(std::move(msg)); }
unsupported()90     static HalResult<void> unsupported() { return HalResult(/* unsupported= */ true); }
91 
92     static HalResult<void> fromStatus(status_t status);
93     static HalResult<void> fromStatus(binder::Status status);
94     static HalResult<void> fromStatus(hardware::power::V1_0::Status status);
95 
96     template <typename R>
97     static HalResult<void> fromReturn(hardware::Return<R>& ret);
98 
isOk()99     bool isOk() const { return !mUnsupported && !mFailed; }
isFailed()100     bool isFailed() const { return !mUnsupported && mFailed; }
isUnsupported()101     bool isUnsupported() const { return mUnsupported; }
errorMessage()102     const char* errorMessage() const { return mErrorMessage.c_str(); }
103 
104 private:
105     std::string mErrorMessage;
106     bool mFailed;
107     bool mUnsupported;
108 
109     explicit HalResult(bool unsupported = false)
mErrorMessage()110           : mErrorMessage(), mFailed(false), mUnsupported(unsupported) {}
HalResult(std::string errorMessage)111     explicit HalResult(std::string errorMessage)
112           : mErrorMessage(std::move(errorMessage)), mFailed(true), mUnsupported(false) {}
113 };
114 
115 // Wrapper for Power HAL handlers.
116 class HalWrapper {
117 public:
118     virtual ~HalWrapper() = default;
119 
120     virtual HalResult<void> setBoost(hardware::power::Boost boost, int32_t durationMs) = 0;
121     virtual HalResult<void> setMode(hardware::power::Mode mode, bool enabled) = 0;
122     virtual HalResult<sp<hardware::power::IPowerHintSession>> createHintSession(
123             int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds,
124             int64_t durationNanos) = 0;
125     virtual HalResult<int64_t> getHintSessionPreferredRate() = 0;
126 };
127 
128 // Empty Power HAL wrapper that ignores all api calls.
129 class EmptyHalWrapper : public HalWrapper {
130 public:
131     EmptyHalWrapper() = default;
132     ~EmptyHalWrapper() = default;
133 
134     virtual HalResult<void> setBoost(hardware::power::Boost boost, int32_t durationMs) override;
135     virtual HalResult<void> setMode(hardware::power::Mode mode, bool enabled) override;
136     virtual HalResult<sp<hardware::power::IPowerHintSession>> createHintSession(
137             int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds,
138             int64_t durationNanos) override;
139     virtual HalResult<int64_t> getHintSessionPreferredRate() override;
140 };
141 
142 // Wrapper for the HIDL Power HAL v1.0.
143 class HidlHalWrapperV1_0 : public HalWrapper {
144 public:
HidlHalWrapperV1_0(sp<hardware::power::V1_0::IPower> Hal)145     explicit HidlHalWrapperV1_0(sp<hardware::power::V1_0::IPower> Hal)
146           : mHandleV1_0(std::move(Hal)) {}
147     virtual ~HidlHalWrapperV1_0() = default;
148 
149     virtual HalResult<void> setBoost(hardware::power::Boost boost, int32_t durationMs) override;
150     virtual HalResult<void> setMode(hardware::power::Mode mode, bool enabled) override;
151     virtual HalResult<sp<hardware::power::IPowerHintSession>> createHintSession(
152             int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds,
153             int64_t durationNanos) override;
154     virtual HalResult<int64_t> getHintSessionPreferredRate() override;
155 
156 protected:
157     virtual HalResult<void> sendPowerHint(hardware::power::V1_0::PowerHint hintId, uint32_t data);
158 
159 private:
160     sp<hardware::power::V1_0::IPower> mHandleV1_0;
161     HalResult<void> setInteractive(bool enabled);
162     HalResult<void> setFeature(hardware::power::V1_0::Feature feature, bool enabled);
163 };
164 
165 // Wrapper for the HIDL Power HAL v1.1.
166 class HidlHalWrapperV1_1 : public HidlHalWrapperV1_0 {
167 public:
HidlHalWrapperV1_1(sp<hardware::power::V1_0::IPower> handleV1_0,sp<hardware::power::V1_1::IPower> handleV1_1)168     HidlHalWrapperV1_1(sp<hardware::power::V1_0::IPower> handleV1_0,
169                        sp<hardware::power::V1_1::IPower> handleV1_1)
170           : HidlHalWrapperV1_0(std::move(handleV1_0)), mHandleV1_1(std::move(handleV1_1)) {}
171     virtual ~HidlHalWrapperV1_1() = default;
172 
173 protected:
174     virtual HalResult<void> sendPowerHint(hardware::power::V1_0::PowerHint hintId,
175                                           uint32_t data) override;
176 
177 private:
178     sp<hardware::power::V1_1::IPower> mHandleV1_1;
179 };
180 
181 // Wrapper for the AIDL Power HAL.
182 class AidlHalWrapper : public HalWrapper {
183 public:
AidlHalWrapper(sp<hardware::power::IPower> handle)184     explicit AidlHalWrapper(sp<hardware::power::IPower> handle) : mHandle(std::move(handle)) {}
185     virtual ~AidlHalWrapper() = default;
186 
187     virtual HalResult<void> setBoost(hardware::power::Boost boost, int32_t durationMs) override;
188     virtual HalResult<void> setMode(hardware::power::Mode mode, bool enabled) override;
189     virtual HalResult<sp<hardware::power::IPowerHintSession>> createHintSession(
190             int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds,
191             int64_t durationNanos) override;
192     virtual HalResult<int64_t> getHintSessionPreferredRate() override;
193 
194 private:
195     // Control access to the boost and mode supported arrays.
196     std::mutex mBoostMutex;
197     std::mutex mModeMutex;
198     sp<hardware::power::IPower> mHandle;
199     // Android framework only sends boost upto DISPLAY_UPDATE_IMMINENT.
200     // Need to increase the array size if more boost supported.
201     std::array<std::atomic<HalSupport>,
202                static_cast<int32_t>(hardware::power::Boost::DISPLAY_UPDATE_IMMINENT) + 1>
203             mBoostSupportedArray GUARDED_BY(mBoostMutex) = {HalSupport::UNKNOWN};
204     // Android framework only sends mode upto DISPLAY_INACTIVE.
205     // Need to increase the array if more mode supported.
206     std::array<std::atomic<HalSupport>,
207                static_cast<int32_t>(hardware::power::Mode::DISPLAY_INACTIVE) + 1>
208             mModeSupportedArray GUARDED_BY(mModeMutex) = {HalSupport::UNKNOWN};
209 };
210 
211 }; // namespace power
212 
213 }; // namespace android
214 
215 #endif // ANDROID_POWERHALWRAPPER_H
216