Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 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> |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 21 | #include <android/hardware/power/1.1/IPower.h> |
| 22 | #include <android/hardware/power/Boost.h> |
| 23 | #include <android/hardware/power/IPower.h> |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame^] | 24 | #include <android/hardware/power/IPowerHintSession.h> |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 25 | #include <android/hardware/power/Mode.h> |
| 26 | |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 27 | namespace android { |
| 28 | |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 29 | namespace power { |
| 30 | |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 31 | // State of Power HAL support for individual apis. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 32 | enum class HalSupport { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 33 | UNKNOWN = 0, |
| 34 | ON = 1, |
| 35 | OFF = 2, |
| 36 | }; |
| 37 | |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame^] | 38 | // Result of a call to the Power HAL wrapper, holding data if successful. |
| 39 | template <typename T> |
| 40 | class HalResult { |
| 41 | public: |
| 42 | static HalResult<T> ok(T value) { return HalResult(value); } |
| 43 | static HalResult<T> failed(std::string msg) { |
| 44 | return HalResult(std::move(msg), /* unsupported= */ false); |
| 45 | } |
| 46 | static HalResult<T> unsupported() { return HalResult("", /* unsupported= */ true); } |
| 47 | |
| 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. |
| 67 | const T& value() const { return mValue.value(); } |
| 68 | bool isOk() const { return !mUnsupported && mValue.has_value(); } |
| 69 | bool isFailed() const { return !mUnsupported && !mValue.has_value(); } |
| 70 | bool isUnsupported() const { return mUnsupported; } |
| 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 | |
| 78 | explicit HalResult(T value) |
| 79 | : mValue(std::make_optional(value)), mErrorMessage(), mUnsupported(false) {} |
| 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: |
| 88 | static HalResult<void> ok() { return HalResult(); } |
| 89 | static HalResult<void> failed(std::string msg) { return HalResult(std::move(msg)); } |
| 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 | |
| 99 | bool isOk() const { return !mUnsupported && !mFailed; } |
| 100 | bool isFailed() const { return !mUnsupported && mFailed; } |
| 101 | bool isUnsupported() const { return mUnsupported; } |
| 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) |
| 110 | : mErrorMessage(), mFailed(false), mUnsupported(unsupported) {} |
| 111 | explicit HalResult(std::string errorMessage) |
| 112 | : mErrorMessage(std::move(errorMessage)), mFailed(true), mUnsupported(false) {} |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | // Wrapper for Power HAL handlers. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 116 | class HalWrapper { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 117 | public: |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 118 | virtual ~HalWrapper() = default; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 119 | |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame^] | 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; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | // Empty Power HAL wrapper that ignores all api calls. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 129 | class EmptyHalWrapper : public HalWrapper { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 130 | public: |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 131 | EmptyHalWrapper() = default; |
| 132 | ~EmptyHalWrapper() = default; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 133 | |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame^] | 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; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | // Wrapper for the HIDL Power HAL v1.0. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 143 | class HidlHalWrapperV1_0 : public HalWrapper { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 144 | public: |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 145 | explicit HidlHalWrapperV1_0(sp<hardware::power::V1_0::IPower> Hal) |
| 146 | : mHandleV1_0(std::move(Hal)) {} |
| 147 | virtual ~HidlHalWrapperV1_0() = default; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 148 | |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame^] | 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; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 155 | |
| 156 | protected: |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame^] | 157 | virtual HalResult<void> sendPowerHint(hardware::power::V1_0::PowerHint hintId, uint32_t data); |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 158 | |
| 159 | private: |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 160 | sp<hardware::power::V1_0::IPower> mHandleV1_0; |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame^] | 161 | HalResult<void> setInteractive(bool enabled); |
| 162 | HalResult<void> setFeature(hardware::power::V1_0::Feature feature, bool enabled); |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | // Wrapper for the HIDL Power HAL v1.1. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 166 | class HidlHalWrapperV1_1 : public HidlHalWrapperV1_0 { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 167 | public: |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 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; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 172 | |
| 173 | protected: |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame^] | 174 | virtual HalResult<void> sendPowerHint(hardware::power::V1_0::PowerHint hintId, |
| 175 | uint32_t data) override; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 176 | |
| 177 | private: |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 178 | sp<hardware::power::V1_1::IPower> mHandleV1_1; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | // Wrapper for the AIDL Power HAL. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 182 | class AidlHalWrapper : public HalWrapper { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 183 | public: |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 184 | explicit AidlHalWrapper(sp<hardware::power::IPower> handle) : mHandle(std::move(handle)) {} |
| 185 | virtual ~AidlHalWrapper() = default; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 186 | |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame^] | 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; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 193 | |
| 194 | private: |
| 195 | // Control access to the boost and mode supported arrays. |
| 196 | std::mutex mBoostMutex; |
| 197 | std::mutex mModeMutex; |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 198 | sp<hardware::power::IPower> mHandle; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 199 | // Android framework only sends boost upto DISPLAY_UPDATE_IMMINENT. |
| 200 | // Need to increase the array size if more boost supported. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 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}; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 204 | // Android framework only sends mode upto DISPLAY_INACTIVE. |
| 205 | // Need to increase the array if more mode supported. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 206 | std::array<std::atomic<HalSupport>, |
| 207 | static_cast<int32_t>(hardware::power::Mode::DISPLAY_INACTIVE) + 1> |
| 208 | mModeSupportedArray GUARDED_BY(mModeMutex) = {HalSupport::UNKNOWN}; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 211 | }; // namespace power |
| 212 | |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 213 | }; // namespace android |
| 214 | |
| 215 | #endif // ANDROID_POWERHALWRAPPER_H |