Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [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 | #include "Power.h" |
Peiyong Lin | 5851c3a | 2022-10-18 20:47:56 +0000 | [diff] [blame] | 18 | #include "PowerHintSession.h" |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 19 | |
| 20 | #include <android-base/logging.h> |
Matt Buckley | caac147 | 2023-12-12 03:55:50 +0000 | [diff] [blame] | 21 | #include <fmq/AidlMessageQueue.h> |
| 22 | #include <fmq/EventFlag.h> |
Matt Buckley | f7c36d4 | 2024-02-27 01:31:43 +0000 | [diff] [blame] | 23 | #include <thread> |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 24 | |
| 25 | namespace aidl { |
| 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | namespace power { |
| 29 | namespace impl { |
| 30 | namespace example { |
| 31 | |
Peiyong Lin | 5851c3a | 2022-10-18 20:47:56 +0000 | [diff] [blame] | 32 | using namespace std::chrono_literals; |
Matt Buckley | caac147 | 2023-12-12 03:55:50 +0000 | [diff] [blame] | 33 | using ::aidl::android::hardware::common::fmq::MQDescriptor; |
| 34 | using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite; |
| 35 | using ::aidl::android::hardware::power::ChannelMessage; |
Matt Buckley | 0efc748 | 2024-10-29 18:39:20 +0000 | [diff] [blame] | 36 | using ::aidl::android::hardware::power::CompositionData; |
| 37 | using ::aidl::android::hardware::power::CompositionUpdate; |
Matt Buckley | caac147 | 2023-12-12 03:55:50 +0000 | [diff] [blame] | 38 | using ::android::AidlMessageQueue; |
Peiyong Lin | 5851c3a | 2022-10-18 20:47:56 +0000 | [diff] [blame] | 39 | |
| 40 | using ndk::ScopedAStatus; |
| 41 | |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 42 | const std::vector<Boost> BOOST_RANGE{ndk::enum_range<Boost>().begin(), |
| 43 | ndk::enum_range<Boost>().end()}; |
| 44 | const std::vector<Mode> MODE_RANGE{ndk::enum_range<Mode>().begin(), ndk::enum_range<Mode>().end()}; |
| 45 | |
Matt Buckley | 607720e | 2024-10-28 19:21:37 +0000 | [diff] [blame] | 46 | template <class T> |
| 47 | constexpr size_t enum_size() { |
| 48 | return static_cast<size_t>(*(ndk::enum_range<T>().end() - 1)) + 1; |
| 49 | } |
| 50 | |
Peiyong Lin | 5851c3a | 2022-10-18 20:47:56 +0000 | [diff] [blame] | 51 | ScopedAStatus Power::setMode(Mode type, bool enabled) { |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 52 | LOG(VERBOSE) << "Power setMode: " << static_cast<int32_t>(type) << " to: " << enabled; |
Peiyong Lin | 5851c3a | 2022-10-18 20:47:56 +0000 | [diff] [blame] | 53 | return ScopedAStatus::ok(); |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Peiyong Lin | 5851c3a | 2022-10-18 20:47:56 +0000 | [diff] [blame] | 56 | ScopedAStatus Power::isModeSupported(Mode type, bool* _aidl_return) { |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 57 | LOG(INFO) << "Power isModeSupported: " << static_cast<int32_t>(type); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 58 | *_aidl_return = type >= MODE_RANGE.front() && type <= MODE_RANGE.back(); |
Peiyong Lin | 5851c3a | 2022-10-18 20:47:56 +0000 | [diff] [blame] | 59 | return ScopedAStatus::ok(); |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Peiyong Lin | 5851c3a | 2022-10-18 20:47:56 +0000 | [diff] [blame] | 62 | ScopedAStatus Power::setBoost(Boost type, int32_t durationMs) { |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 63 | LOG(VERBOSE) << "Power setBoost: " << static_cast<int32_t>(type) |
| 64 | << ", duration: " << durationMs; |
Peiyong Lin | 5851c3a | 2022-10-18 20:47:56 +0000 | [diff] [blame] | 65 | return ScopedAStatus::ok(); |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Peiyong Lin | 5851c3a | 2022-10-18 20:47:56 +0000 | [diff] [blame] | 68 | ScopedAStatus Power::isBoostSupported(Boost type, bool* _aidl_return) { |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 69 | LOG(INFO) << "Power isBoostSupported: " << static_cast<int32_t>(type); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 70 | *_aidl_return = type >= BOOST_RANGE.front() && type <= BOOST_RANGE.back(); |
Peiyong Lin | 5851c3a | 2022-10-18 20:47:56 +0000 | [diff] [blame] | 71 | return ScopedAStatus::ok(); |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Xiang Wang | af05282 | 2024-10-21 23:18:29 +0000 | [diff] [blame] | 74 | ndk::ScopedAStatus Power::getCpuHeadroom(const CpuHeadroomParams& _, |
| 75 | std::vector<float>* _aidl_return) { |
| 76 | *_aidl_return = {0.5f}; |
| 77 | return ndk::ScopedAStatus::ok(); |
| 78 | } |
| 79 | |
| 80 | ndk::ScopedAStatus Power::getGpuHeadroom(const GpuHeadroomParams& _, float* _aidl_return) { |
| 81 | *_aidl_return = 0.5f; |
| 82 | return ndk::ScopedAStatus::ok(); |
| 83 | } |
| 84 | |
| 85 | ndk::ScopedAStatus Power::getCpuHeadroomMinIntervalMillis(int64_t* _aidl_return) { |
| 86 | *_aidl_return = 1000; |
| 87 | return ndk::ScopedAStatus::ok(); |
| 88 | } |
| 89 | |
| 90 | ndk::ScopedAStatus Power::getGpuHeadroomMinIntervalMillis(int64_t* _aidl_return) { |
| 91 | *_aidl_return = 1000; |
| 92 | return ndk::ScopedAStatus::ok(); |
| 93 | } |
| 94 | |
Peiyong Lin | 5851c3a | 2022-10-18 20:47:56 +0000 | [diff] [blame] | 95 | ScopedAStatus Power::createHintSession(int32_t, int32_t, const std::vector<int32_t>& tids, int64_t, |
| 96 | std::shared_ptr<IPowerHintSession>* _aidl_return) { |
| 97 | if (tids.size() == 0) { |
| 98 | *_aidl_return = nullptr; |
| 99 | return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 100 | } |
| 101 | std::shared_ptr<IPowerHintSession> powerHintSession = |
| 102 | ndk::SharedRefBase::make<PowerHintSession>(); |
| 103 | mPowerHintSessions.push_back(powerHintSession); |
| 104 | *_aidl_return = powerHintSession; |
| 105 | return ScopedAStatus::ok(); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Matt Buckley | caac147 | 2023-12-12 03:55:50 +0000 | [diff] [blame] | 108 | ndk::ScopedAStatus Power::createHintSessionWithConfig( |
| 109 | int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos, |
| 110 | SessionTag, SessionConfig* config, std::shared_ptr<IPowerHintSession>* _aidl_return) { |
| 111 | auto out = createHintSession(tgid, uid, threadIds, durationNanos, _aidl_return); |
| 112 | static_cast<PowerHintSession*>(_aidl_return->get())->getSessionConfig(config); |
| 113 | return out; |
| 114 | } |
| 115 | |
| 116 | ndk::ScopedAStatus Power::getSessionChannel(int32_t, int32_t, ChannelConfig* _aidl_return) { |
Matt Buckley | f7c36d4 | 2024-02-27 01:31:43 +0000 | [diff] [blame] | 117 | static AidlMessageQueue<ChannelMessage, SynchronizedReadWrite> stubQueue{20, true}; |
| 118 | static std::thread stubThread([&] { |
| 119 | ChannelMessage data; |
| 120 | // This loop will only run while there is data waiting |
| 121 | // to be processed, and blocks on a futex all other times |
| 122 | while (stubQueue.readBlocking(&data, 1, 0)) { |
| 123 | } |
| 124 | }); |
Matt Buckley | caac147 | 2023-12-12 03:55:50 +0000 | [diff] [blame] | 125 | _aidl_return->channelDescriptor = stubQueue.dupeDesc(); |
Matt Buckley | f7c36d4 | 2024-02-27 01:31:43 +0000 | [diff] [blame] | 126 | _aidl_return->readFlagBitmask = 0x01; |
| 127 | _aidl_return->writeFlagBitmask = 0x02; |
Matt Buckley | caac147 | 2023-12-12 03:55:50 +0000 | [diff] [blame] | 128 | _aidl_return->eventFlagDescriptor = std::nullopt; |
| 129 | return ndk::ScopedAStatus::ok(); |
| 130 | } |
| 131 | |
| 132 | ndk::ScopedAStatus Power::closeSessionChannel(int32_t, int32_t) { |
| 133 | return ndk::ScopedAStatus::ok(); |
| 134 | } |
| 135 | |
Matt Buckley | 607720e | 2024-10-28 19:21:37 +0000 | [diff] [blame] | 136 | ndk::ScopedAStatus Power::getHintSessionPreferredRate(int64_t* outNanoseconds) { |
Peiyong Lin | 5851c3a | 2022-10-18 20:47:56 +0000 | [diff] [blame] | 137 | *outNanoseconds = std::chrono::nanoseconds(1ms).count(); |
| 138 | return ScopedAStatus::ok(); |
Wei Wang | 0500341 | 2021-04-01 10:44:29 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Matt Buckley | 607720e | 2024-10-28 19:21:37 +0000 | [diff] [blame] | 141 | template <class E> |
| 142 | int64_t bitsForEnum() { |
| 143 | return static_cast<int64_t>(std::bitset<enum_size<E>()>().set().to_ullong()); |
| 144 | } |
| 145 | |
| 146 | ndk::ScopedAStatus Power::getSupportInfo(SupportInfo* _aidl_return) { |
Matt Buckley | 0efc748 | 2024-10-29 18:39:20 +0000 | [diff] [blame] | 147 | static SupportInfo supportInfo = {.usesSessions = false, |
| 148 | .modes = bitsForEnum<Mode>(), |
| 149 | .boosts = bitsForEnum<Boost>(), |
| 150 | .sessionHints = 0, |
| 151 | .sessionModes = 0, |
| 152 | .sessionTags = 0, |
| 153 | .compositionData = { |
| 154 | .isSupported = false, |
| 155 | .disableGpuFences = false, |
| 156 | .maxBatchSize = 1, |
| 157 | .alwaysBatch = false, |
| 158 | }}; |
Matt Buckley | 607720e | 2024-10-28 19:21:37 +0000 | [diff] [blame] | 159 | // Copy the support object into the binder |
| 160 | *_aidl_return = supportInfo; |
| 161 | return ndk::ScopedAStatus::ok(); |
| 162 | } |
| 163 | |
Matt Buckley | 0efc748 | 2024-10-29 18:39:20 +0000 | [diff] [blame] | 164 | ndk::ScopedAStatus Power::sendCompositionData(const std::vector<CompositionData>&) { |
| 165 | LOG(INFO) << "Composition data received!"; |
| 166 | return ndk::ScopedAStatus::ok(); |
| 167 | } |
| 168 | |
| 169 | ndk::ScopedAStatus Power::sendCompositionUpdate(const CompositionUpdate&) { |
| 170 | LOG(INFO) << "Composition update received!"; |
| 171 | return ndk::ScopedAStatus::ok(); |
| 172 | } |
| 173 | |
Wei Wang | 61c2a33 | 2020-01-08 16:51:47 -0800 | [diff] [blame] | 174 | } // namespace example |
| 175 | } // namespace impl |
| 176 | } // namespace power |
| 177 | } // namespace hardware |
| 178 | } // namespace android |
| 179 | } // namespace aidl |