blob: 1fc0a0a8a735d3169f8d2b3cc6b016b2ca91ebb5 [file] [log] [blame]
Wei Wang61c2a332020-01-08 16:51:47 -08001/*
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 Lin5851c3a2022-10-18 20:47:56 +000018#include "PowerHintSession.h"
Wei Wang61c2a332020-01-08 16:51:47 -080019
20#include <android-base/logging.h>
Matt Buckleycaac1472023-12-12 03:55:50 +000021#include <fmq/AidlMessageQueue.h>
22#include <fmq/EventFlag.h>
Matt Buckleyf7c36d42024-02-27 01:31:43 +000023#include <thread>
Wei Wang61c2a332020-01-08 16:51:47 -080024
25namespace aidl {
26namespace android {
27namespace hardware {
28namespace power {
29namespace impl {
30namespace example {
31
Peiyong Lin5851c3a2022-10-18 20:47:56 +000032using namespace std::chrono_literals;
Matt Buckleycaac1472023-12-12 03:55:50 +000033using ::aidl::android::hardware::common::fmq::MQDescriptor;
34using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
35using ::aidl::android::hardware::power::ChannelMessage;
36using ::android::AidlMessageQueue;
Peiyong Lin5851c3a2022-10-18 20:47:56 +000037
38using ndk::ScopedAStatus;
39
Wei Wang05003412021-04-01 10:44:29 -070040const std::vector<Boost> BOOST_RANGE{ndk::enum_range<Boost>().begin(),
41 ndk::enum_range<Boost>().end()};
42const std::vector<Mode> MODE_RANGE{ndk::enum_range<Mode>().begin(), ndk::enum_range<Mode>().end()};
43
Matt Buckley607720e2024-10-28 19:21:37 +000044template <class T>
45constexpr size_t enum_size() {
46 return static_cast<size_t>(*(ndk::enum_range<T>().end() - 1)) + 1;
47}
48
Peiyong Lin5851c3a2022-10-18 20:47:56 +000049ScopedAStatus Power::setMode(Mode type, bool enabled) {
Wei Wang61c2a332020-01-08 16:51:47 -080050 LOG(VERBOSE) << "Power setMode: " << static_cast<int32_t>(type) << " to: " << enabled;
Peiyong Lin5851c3a2022-10-18 20:47:56 +000051 return ScopedAStatus::ok();
Wei Wang61c2a332020-01-08 16:51:47 -080052}
53
Peiyong Lin5851c3a2022-10-18 20:47:56 +000054ScopedAStatus Power::isModeSupported(Mode type, bool* _aidl_return) {
Wei Wang61c2a332020-01-08 16:51:47 -080055 LOG(INFO) << "Power isModeSupported: " << static_cast<int32_t>(type);
Wei Wang05003412021-04-01 10:44:29 -070056 *_aidl_return = type >= MODE_RANGE.front() && type <= MODE_RANGE.back();
Peiyong Lin5851c3a2022-10-18 20:47:56 +000057 return ScopedAStatus::ok();
Wei Wang61c2a332020-01-08 16:51:47 -080058}
59
Peiyong Lin5851c3a2022-10-18 20:47:56 +000060ScopedAStatus Power::setBoost(Boost type, int32_t durationMs) {
Wei Wang61c2a332020-01-08 16:51:47 -080061 LOG(VERBOSE) << "Power setBoost: " << static_cast<int32_t>(type)
62 << ", duration: " << durationMs;
Peiyong Lin5851c3a2022-10-18 20:47:56 +000063 return ScopedAStatus::ok();
Wei Wang61c2a332020-01-08 16:51:47 -080064}
65
Peiyong Lin5851c3a2022-10-18 20:47:56 +000066ScopedAStatus Power::isBoostSupported(Boost type, bool* _aidl_return) {
Wei Wang61c2a332020-01-08 16:51:47 -080067 LOG(INFO) << "Power isBoostSupported: " << static_cast<int32_t>(type);
Wei Wang05003412021-04-01 10:44:29 -070068 *_aidl_return = type >= BOOST_RANGE.front() && type <= BOOST_RANGE.back();
Peiyong Lin5851c3a2022-10-18 20:47:56 +000069 return ScopedAStatus::ok();
Wei Wang61c2a332020-01-08 16:51:47 -080070}
71
Xiang Wangaf052822024-10-21 23:18:29 +000072ndk::ScopedAStatus Power::getCpuHeadroom(const CpuHeadroomParams& _,
73 std::vector<float>* _aidl_return) {
74 *_aidl_return = {0.5f};
75 return ndk::ScopedAStatus::ok();
76}
77
78ndk::ScopedAStatus Power::getGpuHeadroom(const GpuHeadroomParams& _, float* _aidl_return) {
79 *_aidl_return = 0.5f;
80 return ndk::ScopedAStatus::ok();
81}
82
83ndk::ScopedAStatus Power::getCpuHeadroomMinIntervalMillis(int64_t* _aidl_return) {
84 *_aidl_return = 1000;
85 return ndk::ScopedAStatus::ok();
86}
87
88ndk::ScopedAStatus Power::getGpuHeadroomMinIntervalMillis(int64_t* _aidl_return) {
89 *_aidl_return = 1000;
90 return ndk::ScopedAStatus::ok();
91}
92
Peiyong Lin5851c3a2022-10-18 20:47:56 +000093ScopedAStatus Power::createHintSession(int32_t, int32_t, const std::vector<int32_t>& tids, int64_t,
94 std::shared_ptr<IPowerHintSession>* _aidl_return) {
95 if (tids.size() == 0) {
96 *_aidl_return = nullptr;
97 return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
98 }
99 std::shared_ptr<IPowerHintSession> powerHintSession =
100 ndk::SharedRefBase::make<PowerHintSession>();
101 mPowerHintSessions.push_back(powerHintSession);
102 *_aidl_return = powerHintSession;
103 return ScopedAStatus::ok();
Wei Wang05003412021-04-01 10:44:29 -0700104}
105
Matt Buckleycaac1472023-12-12 03:55:50 +0000106ndk::ScopedAStatus Power::createHintSessionWithConfig(
107 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos,
108 SessionTag, SessionConfig* config, std::shared_ptr<IPowerHintSession>* _aidl_return) {
109 auto out = createHintSession(tgid, uid, threadIds, durationNanos, _aidl_return);
110 static_cast<PowerHintSession*>(_aidl_return->get())->getSessionConfig(config);
111 return out;
112}
113
114ndk::ScopedAStatus Power::getSessionChannel(int32_t, int32_t, ChannelConfig* _aidl_return) {
Matt Buckleyf7c36d42024-02-27 01:31:43 +0000115 static AidlMessageQueue<ChannelMessage, SynchronizedReadWrite> stubQueue{20, true};
116 static std::thread stubThread([&] {
117 ChannelMessage data;
118 // This loop will only run while there is data waiting
119 // to be processed, and blocks on a futex all other times
120 while (stubQueue.readBlocking(&data, 1, 0)) {
121 }
122 });
Matt Buckleycaac1472023-12-12 03:55:50 +0000123 _aidl_return->channelDescriptor = stubQueue.dupeDesc();
Matt Buckleyf7c36d42024-02-27 01:31:43 +0000124 _aidl_return->readFlagBitmask = 0x01;
125 _aidl_return->writeFlagBitmask = 0x02;
Matt Buckleycaac1472023-12-12 03:55:50 +0000126 _aidl_return->eventFlagDescriptor = std::nullopt;
127 return ndk::ScopedAStatus::ok();
128}
129
130ndk::ScopedAStatus Power::closeSessionChannel(int32_t, int32_t) {
131 return ndk::ScopedAStatus::ok();
132}
133
Matt Buckley607720e2024-10-28 19:21:37 +0000134ndk::ScopedAStatus Power::getHintSessionPreferredRate(int64_t* outNanoseconds) {
Peiyong Lin5851c3a2022-10-18 20:47:56 +0000135 *outNanoseconds = std::chrono::nanoseconds(1ms).count();
136 return ScopedAStatus::ok();
Wei Wang05003412021-04-01 10:44:29 -0700137}
138
Matt Buckley607720e2024-10-28 19:21:37 +0000139template <class E>
140int64_t bitsForEnum() {
141 return static_cast<int64_t>(std::bitset<enum_size<E>()>().set().to_ullong());
142}
143
144ndk::ScopedAStatus Power::getSupportInfo(SupportInfo* _aidl_return) {
145 static SupportInfo supportInfo = {
146 .usesSessions = false,
147 .modes = bitsForEnum<Mode>(),
148 .boosts = bitsForEnum<Boost>(),
149 .sessionHints = 0,
150 .sessionModes = 0,
151 .sessionTags = 0,
152 };
153 // Copy the support object into the binder
154 *_aidl_return = supportInfo;
155 return ndk::ScopedAStatus::ok();
156}
157
Wei Wang61c2a332020-01-08 16:51:47 -0800158} // namespace example
159} // namespace impl
160} // namespace power
161} // namespace hardware
162} // namespace android
163} // namespace aidl