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