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