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 | |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 17 | #pragma once |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 18 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 19 | #include <aidl/android/hardware/power/Boost.h> |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 20 | #include <aidl/android/hardware/power/ChannelConfig.h> |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 21 | #include <aidl/android/hardware/power/IPower.h> |
| 22 | #include <aidl/android/hardware/power/IPowerHintSession.h> |
| 23 | #include <aidl/android/hardware/power/Mode.h> |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 24 | #include <aidl/android/hardware/power/SessionConfig.h> |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 25 | #include <android-base/thread_annotations.h> |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 26 | #include <android/hardware/power/1.1/IPower.h> |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 27 | #include <android/hardware/power/1.2/IPower.h> |
| 28 | #include <android/hardware/power/1.3/IPower.h> |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 29 | #include <powermanager/HalResult.h> |
| 30 | #include <powermanager/PowerHintSessionWrapper.h> |
| 31 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 32 | #include <binder/Status.h> |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 33 | |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 34 | #include <utility> |
| 35 | |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 36 | namespace android { |
| 37 | |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 38 | namespace power { |
| 39 | |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 40 | // State of Power HAL support for individual apis. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 41 | enum class HalSupport { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 42 | UNKNOWN = 0, |
| 43 | ON = 1, |
| 44 | OFF = 2, |
| 45 | }; |
| 46 | |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 47 | // Wrapper for Power HAL handlers. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 48 | class HalWrapper { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 49 | public: |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 50 | virtual ~HalWrapper() = default; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 51 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 52 | virtual HalResult<void> setBoost(aidl::android::hardware::power::Boost boost, |
| 53 | int32_t durationMs) = 0; |
| 54 | virtual HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) = 0; |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 55 | virtual HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSession( |
| 56 | int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, |
| 57 | int64_t durationNanos) = 0; |
| 58 | virtual HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSessionWithConfig( |
| 59 | int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos, |
| 60 | aidl::android::hardware::power::SessionTag tag, |
| 61 | aidl::android::hardware::power::SessionConfig* config) = 0; |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 62 | virtual HalResult<int64_t> getHintSessionPreferredRate() = 0; |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 63 | virtual HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid, |
| 64 | int uid) = 0; |
| 65 | virtual HalResult<void> closeSessionChannel(int tgid, int uid) = 0; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | // Empty Power HAL wrapper that ignores all api calls. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 69 | class EmptyHalWrapper : public HalWrapper { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 70 | public: |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 71 | EmptyHalWrapper() = default; |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 72 | ~EmptyHalWrapper() override = default; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 73 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 74 | HalResult<void> setBoost(aidl::android::hardware::power::Boost boost, |
| 75 | int32_t durationMs) override; |
| 76 | HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 77 | HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSession( |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 78 | int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, |
| 79 | int64_t durationNanos) override; |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 80 | HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSessionWithConfig( |
| 81 | int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos, |
| 82 | aidl::android::hardware::power::SessionTag tag, |
| 83 | aidl::android::hardware::power::SessionConfig* config) override; |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 84 | HalResult<int64_t> getHintSessionPreferredRate() override; |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 85 | HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid, |
| 86 | int uid) override; |
| 87 | HalResult<void> closeSessionChannel(int tgid, int uid) override; |
| 88 | |
| 89 | protected: |
| 90 | virtual const char* getUnsupportedMessage(); |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | // Wrapper for the HIDL Power HAL v1.0. |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 94 | class HidlHalWrapperV1_0 : public EmptyHalWrapper { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 95 | public: |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 96 | explicit HidlHalWrapperV1_0(sp<hardware::power::V1_0::IPower> handleV1_0) |
| 97 | : mHandleV1_0(std::move(handleV1_0)) {} |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 98 | ~HidlHalWrapperV1_0() override = default; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 99 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 100 | HalResult<void> setBoost(aidl::android::hardware::power::Boost boost, |
| 101 | int32_t durationMs) override; |
| 102 | HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 103 | |
| 104 | protected: |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 105 | const sp<hardware::power::V1_0::IPower> mHandleV1_0; |
| 106 | virtual HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data); |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 107 | const char* getUnsupportedMessage(); |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 108 | |
| 109 | private: |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 110 | HalResult<void> setInteractive(bool enabled); |
| 111 | HalResult<void> setFeature(hardware::power::V1_0::Feature feature, bool enabled); |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | // Wrapper for the HIDL Power HAL v1.1. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 115 | class HidlHalWrapperV1_1 : public HidlHalWrapperV1_0 { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 116 | public: |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 117 | explicit HidlHalWrapperV1_1(sp<hardware::power::V1_1::IPower> handleV1_1) |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 118 | : HidlHalWrapperV1_0(std::move(handleV1_1)) {} |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 119 | ~HidlHalWrapperV1_1() override = default; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 120 | |
| 121 | protected: |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 122 | HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override; |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 123 | }; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 124 | |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 125 | // Wrapper for the HIDL Power HAL v1.2. |
| 126 | class HidlHalWrapperV1_2 : public HidlHalWrapperV1_1 { |
| 127 | public: |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 128 | HalResult<void> setBoost(aidl::android::hardware::power::Boost boost, |
| 129 | int32_t durationMs) override; |
| 130 | HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; |
| 131 | explicit HidlHalWrapperV1_2(sp<hardware::power::V1_2::IPower> handleV1_2) |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 132 | : HidlHalWrapperV1_1(std::move(handleV1_2)) {} |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 133 | ~HidlHalWrapperV1_2() override = default; |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 134 | |
| 135 | protected: |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 136 | HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override; |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | // Wrapper for the HIDL Power HAL v1.3. |
| 140 | class HidlHalWrapperV1_3 : public HidlHalWrapperV1_2 { |
| 141 | public: |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 142 | HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; |
| 143 | explicit HidlHalWrapperV1_3(sp<hardware::power::V1_3::IPower> handleV1_3) |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 144 | : HidlHalWrapperV1_2(std::move(handleV1_3)) {} |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 145 | ~HidlHalWrapperV1_3() override = default; |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 146 | |
| 147 | protected: |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 148 | HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | // Wrapper for the AIDL Power HAL. |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 152 | class AidlHalWrapper : public EmptyHalWrapper { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 153 | public: |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 154 | explicit AidlHalWrapper(std::shared_ptr<aidl::android::hardware::power::IPower> handle) |
| 155 | : mHandle(std::move(handle)) {} |
| 156 | ~AidlHalWrapper() override = default; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 157 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 158 | HalResult<void> setBoost(aidl::android::hardware::power::Boost boost, |
| 159 | int32_t durationMs) override; |
| 160 | HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 161 | HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSession( |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 162 | int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, |
| 163 | int64_t durationNanos) override; |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 164 | HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSessionWithConfig( |
| 165 | int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos, |
| 166 | aidl::android::hardware::power::SessionTag tag, |
| 167 | aidl::android::hardware::power::SessionConfig* config) override; |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 168 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 169 | HalResult<int64_t> getHintSessionPreferredRate() override; |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 170 | HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid, |
| 171 | int uid) override; |
| 172 | HalResult<void> closeSessionChannel(int tgid, int uid) override; |
| 173 | |
| 174 | protected: |
| 175 | const char* getUnsupportedMessage() override; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 176 | |
| 177 | private: |
| 178 | // Control access to the boost and mode supported arrays. |
| 179 | std::mutex mBoostMutex; |
| 180 | std::mutex mModeMutex; |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 181 | std::shared_ptr<aidl::android::hardware::power::IPower> mHandle; |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 182 | std::array<HalSupport, |
| 183 | static_cast<int32_t>( |
| 184 | *(ndk::enum_range<aidl::android::hardware::power::Boost>().end() - 1)) + |
| 185 | 1> |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 186 | mBoostSupportedArray GUARDED_BY(mBoostMutex) = {HalSupport::UNKNOWN}; |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 187 | std::array<HalSupport, |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 188 | static_cast<int32_t>( |
| 189 | *(ndk::enum_range<aidl::android::hardware::power::Mode>().end() - 1)) + |
| 190 | 1> |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 191 | mModeSupportedArray GUARDED_BY(mModeMutex) = {HalSupport::UNKNOWN}; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 194 | }; // namespace power |
| 195 | |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 196 | }; // namespace android |