blob: 06e0cbb0445cbcb81da1fbb7546fd806fa6afda8 [file] [log] [blame]
Michael Wright1509a232018-06-21 02:50:34 +01001/*
2 * Copyright 2018 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
Dan Stoza030fbc12020-02-19 15:32:01 -080017//#define LOG_NDEBUG 0
18
Michael Wright1509a232018-06-21 02:50:34 +010019#undef LOG_TAG
20#define LOG_TAG "PowerAdvisor"
21
22#include <cinttypes>
23
Dan Stoza030fbc12020-02-19 15:32:01 -080024#include <android-base/properties.h>
Michael Wright1509a232018-06-21 02:50:34 +010025#include <utils/Log.h>
26#include <utils/Mutex.h>
27
Dan Stoza030fbc12020-02-19 15:32:01 -080028#include <android/hardware/power/1.3/IPower.h>
29#include <android/hardware/power/IPower.h>
30#include <binder/IServiceManager.h>
31
32#include "../SurfaceFlingerProperties.h"
33
Michael Wright1509a232018-06-21 02:50:34 +010034#include "PowerAdvisor.h"
35
36namespace android {
37namespace Hwc2 {
38
39PowerAdvisor::~PowerAdvisor() = default;
40
41namespace impl {
42
43namespace V1_0 = android::hardware::power::V1_0;
Dan Stoza030fbc12020-02-19 15:32:01 -080044namespace V1_3 = android::hardware::power::V1_3;
Michael Wright1509a232018-06-21 02:50:34 +010045using V1_3::PowerHint;
46
Dan Stoza030fbc12020-02-19 15:32:01 -080047using android::hardware::power::Boost;
48using android::hardware::power::IPower;
49using android::hardware::power::Mode;
50using base::GetIntProperty;
51using scheduler::OneShotTimer;
52
Michael Wright1509a232018-06-21 02:50:34 +010053PowerAdvisor::~PowerAdvisor() = default;
54
Dan Stoza030fbc12020-02-19 15:32:01 -080055namespace {
56int32_t getUpdateTimeout() {
57 // Default to a timeout of 80ms if nothing else is specified
58 static int32_t timeout = sysprop::display_update_imminent_timeout_ms(80);
59 return timeout;
60}
61
62} // namespace
63
64PowerAdvisor::PowerAdvisor()
65 : mUseUpdateImminentTimer(getUpdateTimeout() > 0),
66 mUpdateImminentTimer(
67 OneShotTimer::Interval(getUpdateTimeout()),
68 /* resetCallback */ [this] { mSendUpdateImminent.store(false); },
69 /* timeoutCallback */ [this] { mSendUpdateImminent.store(true); }) {
70 if (mUseUpdateImminentTimer) {
71 mUpdateImminentTimer.start();
72 }
73}
Michael Wright1509a232018-06-21 02:50:34 +010074
Peiyong Lin74ca2f42019-01-14 19:36:57 -080075void PowerAdvisor::setExpensiveRenderingExpected(DisplayId displayId, bool expected) {
Michael Wright1509a232018-06-21 02:50:34 +010076 if (expected) {
77 mExpensiveDisplays.insert(displayId);
78 } else {
79 mExpensiveDisplays.erase(displayId);
80 }
81
Michael Wright1509a232018-06-21 02:50:34 +010082 const bool expectsExpensiveRendering = !mExpensiveDisplays.empty();
83 if (mNotifiedExpensiveRendering != expectsExpensiveRendering) {
Dan Stoza030fbc12020-02-19 15:32:01 -080084 HalWrapper* const halWrapper = getPowerHal();
85 if (halWrapper == nullptr) {
Peiyong Lin81934972018-07-02 11:00:54 -070086 return;
87 }
Dan Stoza030fbc12020-02-19 15:32:01 -080088
89 if (!halWrapper->setExpensiveRendering(expectsExpensiveRendering)) {
90 // The HAL has become unavailable; attempt to reconnect later
Michael Wright1509a232018-06-21 02:50:34 +010091 mReconnectPowerHal = true;
92 return;
93 }
Dan Stoza030fbc12020-02-19 15:32:01 -080094
Michael Wright1509a232018-06-21 02:50:34 +010095 mNotifiedExpensiveRendering = expectsExpensiveRendering;
96 }
97}
98
Dan Stoza030fbc12020-02-19 15:32:01 -080099void PowerAdvisor::notifyDisplayUpdateImminent() {
100 if (mSendUpdateImminent.load()) {
101 HalWrapper* const halWrapper = getPowerHal();
102 if (halWrapper == nullptr) {
103 return;
104 }
105
106 if (!halWrapper->notifyDisplayUpdateImminent()) {
107 // The HAL has become unavailable; attempt to reconnect later
108 mReconnectPowerHal = true;
109 return;
110 }
111 }
112
113 if (mUseUpdateImminentTimer) {
114 mUpdateImminentTimer.reset();
115 }
116}
117
118class HidlPowerHalWrapper : public PowerAdvisor::HalWrapper {
119public:
120 HidlPowerHalWrapper(sp<V1_3::IPower> powerHal) : mPowerHal(std::move(powerHal)) {}
121
122 ~HidlPowerHalWrapper() override = default;
123
124 static std::unique_ptr<HalWrapper> connect() {
125 // Power HAL 1.3 is not guaranteed to be available, thus we need to query
126 // Power HAL 1.0 first and try to cast it to Power HAL 1.3.
Dan Stoza030fbc12020-02-19 15:32:01 -0800127 sp<V1_3::IPower> powerHal = nullptr;
Dan Stoza9c051c02020-02-28 10:19:07 -0800128 sp<V1_0::IPower> powerHal_1_0 = V1_0::IPower::getService();
129 if (powerHal_1_0 != nullptr) {
130 // Try to cast to Power HAL 1.3
131 powerHal = V1_3::IPower::castFrom(powerHal_1_0);
132 if (powerHal == nullptr) {
133 ALOGW("No Power HAL 1.3 service in system, disabling PowerAdvisor");
134 } else {
135 ALOGI("Loaded Power HAL 1.3 service");
Dan Stoza030fbc12020-02-19 15:32:01 -0800136 }
Dan Stoza9c051c02020-02-28 10:19:07 -0800137 } else {
138 ALOGW("No Power HAL found, disabling PowerAdvisor");
Dan Stoza030fbc12020-02-19 15:32:01 -0800139 }
Dan Stoza9c051c02020-02-28 10:19:07 -0800140
Dan Stoza030fbc12020-02-19 15:32:01 -0800141 if (powerHal == nullptr) {
142 return nullptr;
143 }
144
145 return std::make_unique<HidlPowerHalWrapper>(std::move(powerHal));
146 }
147
148 bool setExpensiveRendering(bool enabled) override {
149 ALOGV("HIDL setExpensiveRendering %s", enabled ? "T" : "F");
150 auto ret = mPowerHal->powerHintAsync_1_3(PowerHint::EXPENSIVE_RENDERING, enabled);
151 return ret.isOk();
152 }
153
154 bool notifyDisplayUpdateImminent() override {
155 // Power HAL 1.x doesn't have a notification for this
156 ALOGV("HIDL notifyUpdateImminent received but can't send");
157 return true;
158 }
159
160private:
Dan Stoza030fbc12020-02-19 15:32:01 -0800161 const sp<V1_3::IPower> mPowerHal = nullptr;
162};
163
Dan Stoza030fbc12020-02-19 15:32:01 -0800164class AidlPowerHalWrapper : public PowerAdvisor::HalWrapper {
165public:
166 AidlPowerHalWrapper(sp<IPower> powerHal) : mPowerHal(std::move(powerHal)) {
167 auto ret = mPowerHal->isModeSupported(Mode::EXPENSIVE_RENDERING, &mHasExpensiveRendering);
168 if (!ret.isOk()) {
169 mHasExpensiveRendering = false;
170 }
171
172 ret = mPowerHal->isBoostSupported(Boost::DISPLAY_UPDATE_IMMINENT,
173 &mHasDisplayUpdateImminent);
174 if (!ret.isOk()) {
175 mHasDisplayUpdateImminent = false;
176 }
177 }
178
179 ~AidlPowerHalWrapper() override = default;
180
181 static std::unique_ptr<HalWrapper> connect() {
182 // This only waits if the service is actually declared
183 sp<IPower> powerHal = waitForVintfService<IPower>();
184 if (powerHal == nullptr) {
185 return nullptr;
186 }
187 ALOGI("Loaded AIDL Power HAL service");
188
189 return std::make_unique<AidlPowerHalWrapper>(std::move(powerHal));
190 }
191
192 bool setExpensiveRendering(bool enabled) override {
193 ALOGV("AIDL setExpensiveRendering %s", enabled ? "T" : "F");
194 if (!mHasExpensiveRendering) {
195 ALOGV("Skipped sending EXPENSIVE_RENDERING because HAL doesn't support it");
196 return true;
197 }
198
199 auto ret = mPowerHal->setMode(Mode::EXPENSIVE_RENDERING, enabled);
200 return ret.isOk();
201 }
202
203 bool notifyDisplayUpdateImminent() override {
204 ALOGV("AIDL notifyDisplayUpdateImminent");
205 if (!mHasDisplayUpdateImminent) {
206 ALOGV("Skipped sending DISPLAY_UPDATE_IMMINENT because HAL doesn't support it");
207 return true;
208 }
209
210 auto ret = mPowerHal->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 0);
211 return ret.isOk();
212 }
213
214private:
215 const sp<IPower> mPowerHal = nullptr;
216 bool mHasExpensiveRendering = false;
217 bool mHasDisplayUpdateImminent = false;
218};
219
220PowerAdvisor::HalWrapper* PowerAdvisor::getPowerHal() {
221 static std::unique_ptr<HalWrapper> sHalWrapper = nullptr;
Dan Stoza9c051c02020-02-28 10:19:07 -0800222 static bool sHasHal = true;
Michael Wright1509a232018-06-21 02:50:34 +0100223
Dan Stoza9c051c02020-02-28 10:19:07 -0800224 if (!sHasHal) {
225 return nullptr;
226 }
227
228 // If we used to have a HAL, but it stopped responding, attempt to reconnect
Michael Wright1509a232018-06-21 02:50:34 +0100229 if (mReconnectPowerHal) {
Dan Stoza030fbc12020-02-19 15:32:01 -0800230 sHalWrapper = nullptr;
Michael Wright1509a232018-06-21 02:50:34 +0100231 mReconnectPowerHal = false;
232 }
233
Dan Stoza030fbc12020-02-19 15:32:01 -0800234 if (sHalWrapper != nullptr) {
235 return sHalWrapper.get();
Michael Wright1509a232018-06-21 02:50:34 +0100236 }
Dan Stoza030fbc12020-02-19 15:32:01 -0800237
238 // First attempt to connect to the AIDL Power HAL
239 sHalWrapper = AidlPowerHalWrapper::connect();
240
241 // If that didn't succeed, attempt to connect to the HIDL Power HAL
242 if (sHalWrapper == nullptr) {
243 sHalWrapper = HidlPowerHalWrapper::connect();
244 }
245
Dan Stoza9c051c02020-02-28 10:19:07 -0800246 // If we make it to this point and still don't have a HAL, it's unlikely we
247 // will, so stop trying
248 if (sHalWrapper == nullptr) {
249 sHasHal = false;
250 }
251
Dan Stoza030fbc12020-02-19 15:32:01 -0800252 return sHalWrapper.get();
Michael Wright1509a232018-06-21 02:50:34 +0100253}
254
255} // namespace impl
256} // namespace Hwc2
257} // namespace android