blob: 6e347a9ce9074f3dee4843fde4d3c9671e7d3c09 [file] [log] [blame]
Lais Andrade4d51f6c2020-03-25 10:58:31 +00001/*
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 Buckleydb4192a2023-12-21 20:00:32 +000017#pragma once
Lais Andrade4d51f6c2020-03-25 10:58:31 +000018
Xiang Wang99f6f3c2023-05-22 13:12:16 -070019#include <aidl/android/hardware/power/Boost.h>
Matt Buckleydb4192a2023-12-21 20:00:32 +000020#include <aidl/android/hardware/power/ChannelConfig.h>
Xiang Wang99f6f3c2023-05-22 13:12:16 -070021#include <aidl/android/hardware/power/IPower.h>
22#include <aidl/android/hardware/power/IPowerHintSession.h>
23#include <aidl/android/hardware/power/Mode.h>
Matt Buckleydb4192a2023-12-21 20:00:32 +000024#include <aidl/android/hardware/power/SessionConfig.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000025#include <android-base/thread_annotations.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000026#include <android/hardware/power/1.1/IPower.h>
Matt Buckleyc3894a42022-09-01 21:17:15 +000027#include <android/hardware/power/1.2/IPower.h>
28#include <android/hardware/power/1.3/IPower.h>
Matt Buckley6c18e6d2024-02-07 23:39:50 +000029#include <powermanager/HalResult.h>
30#include <powermanager/PowerHintSessionWrapper.h>
31
Xiang Wang99f6f3c2023-05-22 13:12:16 -070032#include <binder/Status.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000033
Matt Buckleydb4192a2023-12-21 20:00:32 +000034#include <utility>
35
Lais Andrade4d51f6c2020-03-25 10:58:31 +000036namespace android {
37
Lais Andradeb59a9b52020-05-07 17:23:42 +010038namespace power {
39
Lais Andrade4d51f6c2020-03-25 10:58:31 +000040// State of Power HAL support for individual apis.
Lais Andradeb59a9b52020-05-07 17:23:42 +010041enum class HalSupport {
Lais Andrade4d51f6c2020-03-25 10:58:31 +000042 UNKNOWN = 0,
43 ON = 1,
44 OFF = 2,
45};
46
Lais Andrade4d51f6c2020-03-25 10:58:31 +000047// Wrapper for Power HAL handlers.
Lais Andradeb59a9b52020-05-07 17:23:42 +010048class HalWrapper {
Lais Andrade4d51f6c2020-03-25 10:58:31 +000049public:
Lais Andradeb59a9b52020-05-07 17:23:42 +010050 virtual ~HalWrapper() = default;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000051
Xiang Wang99f6f3c2023-05-22 13:12:16 -070052 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 Buckley6c18e6d2024-02-07 23:39:50 +000055 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 Shiu0b264bb2021-03-03 00:30:50 +080062 virtual HalResult<int64_t> getHintSessionPreferredRate() = 0;
Matt Buckleydb4192a2023-12-21 20:00:32 +000063 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 Andrade4d51f6c2020-03-25 10:58:31 +000066};
67
68// Empty Power HAL wrapper that ignores all api calls.
Lais Andradeb59a9b52020-05-07 17:23:42 +010069class EmptyHalWrapper : public HalWrapper {
Lais Andrade4d51f6c2020-03-25 10:58:31 +000070public:
Lais Andradeb59a9b52020-05-07 17:23:42 +010071 EmptyHalWrapper() = default;
Xiang Wang99f6f3c2023-05-22 13:12:16 -070072 ~EmptyHalWrapper() override = default;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000073
Xiang Wang99f6f3c2023-05-22 13:12:16 -070074 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 Buckley6c18e6d2024-02-07 23:39:50 +000077 HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSession(
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080078 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds,
79 int64_t durationNanos) override;
Matt Buckley6c18e6d2024-02-07 23:39:50 +000080 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 Wang99f6f3c2023-05-22 13:12:16 -070084 HalResult<int64_t> getHintSessionPreferredRate() override;
Matt Buckleydb4192a2023-12-21 20:00:32 +000085 HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid,
86 int uid) override;
87 HalResult<void> closeSessionChannel(int tgid, int uid) override;
88
89protected:
90 virtual const char* getUnsupportedMessage();
Lais Andrade4d51f6c2020-03-25 10:58:31 +000091};
92
93// Wrapper for the HIDL Power HAL v1.0.
Matt Buckleydb4192a2023-12-21 20:00:32 +000094class HidlHalWrapperV1_0 : public EmptyHalWrapper {
Lais Andrade4d51f6c2020-03-25 10:58:31 +000095public:
Matt Buckleyc3894a42022-09-01 21:17:15 +000096 explicit HidlHalWrapperV1_0(sp<hardware::power::V1_0::IPower> handleV1_0)
97 : mHandleV1_0(std::move(handleV1_0)) {}
Xiang Wang99f6f3c2023-05-22 13:12:16 -070098 ~HidlHalWrapperV1_0() override = default;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000099
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700100 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 Andrade4d51f6c2020-03-25 10:58:31 +0000103
104protected:
Matt Buckleyc3894a42022-09-01 21:17:15 +0000105 const sp<hardware::power::V1_0::IPower> mHandleV1_0;
106 virtual HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data);
Matt Buckleydb4192a2023-12-21 20:00:32 +0000107 const char* getUnsupportedMessage();
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000108
109private:
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800110 HalResult<void> setInteractive(bool enabled);
111 HalResult<void> setFeature(hardware::power::V1_0::Feature feature, bool enabled);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000112};
113
114// Wrapper for the HIDL Power HAL v1.1.
Lais Andradeb59a9b52020-05-07 17:23:42 +0100115class HidlHalWrapperV1_1 : public HidlHalWrapperV1_0 {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000116public:
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700117 explicit HidlHalWrapperV1_1(sp<hardware::power::V1_1::IPower> handleV1_1)
Matt Buckleyc3894a42022-09-01 21:17:15 +0000118 : HidlHalWrapperV1_0(std::move(handleV1_1)) {}
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700119 ~HidlHalWrapperV1_1() override = default;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000120
121protected:
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700122 HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override;
Matt Buckleyc3894a42022-09-01 21:17:15 +0000123};
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000124
Matt Buckleyc3894a42022-09-01 21:17:15 +0000125// Wrapper for the HIDL Power HAL v1.2.
126class HidlHalWrapperV1_2 : public HidlHalWrapperV1_1 {
127public:
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700128 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 Buckleyc3894a42022-09-01 21:17:15 +0000132 : HidlHalWrapperV1_1(std::move(handleV1_2)) {}
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700133 ~HidlHalWrapperV1_2() override = default;
Matt Buckleyc3894a42022-09-01 21:17:15 +0000134
135protected:
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700136 HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override;
Matt Buckleyc3894a42022-09-01 21:17:15 +0000137};
138
139// Wrapper for the HIDL Power HAL v1.3.
140class HidlHalWrapperV1_3 : public HidlHalWrapperV1_2 {
141public:
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700142 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 Buckleyc3894a42022-09-01 21:17:15 +0000144 : HidlHalWrapperV1_2(std::move(handleV1_3)) {}
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700145 ~HidlHalWrapperV1_3() override = default;
Matt Buckleyc3894a42022-09-01 21:17:15 +0000146
147protected:
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700148 HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000149};
150
151// Wrapper for the AIDL Power HAL.
Matt Buckleydb4192a2023-12-21 20:00:32 +0000152class AidlHalWrapper : public EmptyHalWrapper {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000153public:
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700154 explicit AidlHalWrapper(std::shared_ptr<aidl::android::hardware::power::IPower> handle)
155 : mHandle(std::move(handle)) {}
156 ~AidlHalWrapper() override = default;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000157
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700158 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 Buckley6c18e6d2024-02-07 23:39:50 +0000161 HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSession(
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800162 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds,
163 int64_t durationNanos) override;
Matt Buckley6c18e6d2024-02-07 23:39:50 +0000164 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 Buckleydb4192a2023-12-21 20:00:32 +0000168
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700169 HalResult<int64_t> getHintSessionPreferredRate() override;
Matt Buckleydb4192a2023-12-21 20:00:32 +0000170 HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid,
171 int uid) override;
172 HalResult<void> closeSessionChannel(int tgid, int uid) override;
173
174protected:
175 const char* getUnsupportedMessage() override;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000176
177private:
178 // Control access to the boost and mode supported arrays.
179 std::mutex mBoostMutex;
180 std::mutex mModeMutex;
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700181 std::shared_ptr<aidl::android::hardware::power::IPower> mHandle;
Matt Buckley6c18e6d2024-02-07 23:39:50 +0000182 std::array<HalSupport,
183 static_cast<int32_t>(
184 *(ndk::enum_range<aidl::android::hardware::power::Boost>().end() - 1)) +
185 1>
Lais Andradeb59a9b52020-05-07 17:23:42 +0100186 mBoostSupportedArray GUARDED_BY(mBoostMutex) = {HalSupport::UNKNOWN};
Matt Buckley6c18e6d2024-02-07 23:39:50 +0000187 std::array<HalSupport,
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700188 static_cast<int32_t>(
189 *(ndk::enum_range<aidl::android::hardware::power::Mode>().end() - 1)) +
190 1>
Lais Andradeb59a9b52020-05-07 17:23:42 +0100191 mModeSupportedArray GUARDED_BY(mModeMutex) = {HalSupport::UNKNOWN};
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000192};
193
Lais Andradeb59a9b52020-05-07 17:23:42 +0100194}; // namespace power
195
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000196}; // namespace android