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