Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 1 | /* |
| 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 Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | |
Ady Abraham | abce165 | 2022-02-24 10:51:19 -0800 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 21 | #undef LOG_TAG |
| 22 | #define LOG_TAG "PowerAdvisor" |
| 23 | |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 25 | #include <cinttypes> |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 26 | #include <cstdint> |
| 27 | #include <optional> |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 28 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 29 | #include <android-base/properties.h> |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 30 | #include <utils/Log.h> |
| 31 | #include <utils/Mutex.h> |
Ady Abraham | abce165 | 2022-02-24 10:51:19 -0800 | [diff] [blame] | 32 | #include <utils/Trace.h> |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 33 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 34 | #include <android/hardware/power/1.3/IPower.h> |
| 35 | #include <android/hardware/power/IPower.h> |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 36 | #include <android/hardware/power/IPowerHintSession.h> |
| 37 | #include <android/hardware/power/WorkDuration.h> |
| 38 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 39 | #include <binder/IServiceManager.h> |
| 40 | |
| 41 | #include "../SurfaceFlingerProperties.h" |
| 42 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 43 | #include "PowerAdvisor.h" |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 44 | #include "SurfaceFlinger.h" |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 45 | |
| 46 | namespace android { |
| 47 | namespace Hwc2 { |
| 48 | |
| 49 | PowerAdvisor::~PowerAdvisor() = default; |
| 50 | |
| 51 | namespace impl { |
| 52 | |
| 53 | namespace V1_0 = android::hardware::power::V1_0; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 54 | namespace V1_3 = android::hardware::power::V1_3; |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 55 | using V1_3::PowerHint; |
| 56 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 57 | using android::hardware::power::Boost; |
| 58 | using android::hardware::power::IPower; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 59 | using android::hardware::power::IPowerHintSession; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 60 | using android::hardware::power::Mode; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 61 | using android::hardware::power::WorkDuration; |
| 62 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 63 | using scheduler::OneShotTimer; |
| 64 | |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 65 | class AidlPowerHalWrapper; |
| 66 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 67 | PowerAdvisor::~PowerAdvisor() = default; |
| 68 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 69 | namespace { |
| 70 | int32_t getUpdateTimeout() { |
| 71 | // Default to a timeout of 80ms if nothing else is specified |
| 72 | static int32_t timeout = sysprop::display_update_imminent_timeout_ms(80); |
| 73 | return timeout; |
| 74 | } |
| 75 | |
Ady Abraham | abce165 | 2022-02-24 10:51:19 -0800 | [diff] [blame] | 76 | void traceExpensiveRendering(bool enabled) { |
| 77 | if (enabled) { |
| 78 | ATRACE_ASYNC_BEGIN("ExpensiveRendering", 0); |
| 79 | } else { |
| 80 | ATRACE_ASYNC_END("ExpensiveRendering", 0); |
| 81 | } |
| 82 | } |
| 83 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 84 | } // namespace |
| 85 | |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 86 | PowerAdvisor::PowerAdvisor(SurfaceFlinger& flinger) |
| 87 | : mFlinger(flinger), |
| 88 | mUseScreenUpdateTimer(getUpdateTimeout() > 0), |
| 89 | mScreenUpdateTimer( |
Ady Abraham | db3dfee | 2020-11-17 17:07:12 -0800 | [diff] [blame] | 90 | "UpdateImminentTimer", OneShotTimer::Interval(getUpdateTimeout()), |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 91 | /* resetCallback */ [this] { mSendUpdateImminent.store(false); }, |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 92 | /* timeoutCallback */ |
| 93 | [this] { |
| 94 | mSendUpdateImminent.store(true); |
| 95 | mFlinger.disableExpensiveRendering(); |
| 96 | }) {} |
| 97 | |
| 98 | void PowerAdvisor::init() { |
| 99 | // Defer starting the screen update timer until SurfaceFlinger finishes construction. |
| 100 | if (mUseScreenUpdateTimer) { |
| 101 | mScreenUpdateTimer.start(); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 104 | |
Dan Stoza | 29e7bdf | 2020-03-23 14:43:09 -0700 | [diff] [blame] | 105 | void PowerAdvisor::onBootFinished() { |
| 106 | mBootFinished.store(true); |
| 107 | } |
| 108 | |
Peiyong Lin | 74ca2f4 | 2019-01-14 19:36:57 -0800 | [diff] [blame] | 109 | void PowerAdvisor::setExpensiveRenderingExpected(DisplayId displayId, bool expected) { |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 110 | if (expected) { |
| 111 | mExpensiveDisplays.insert(displayId); |
| 112 | } else { |
| 113 | mExpensiveDisplays.erase(displayId); |
| 114 | } |
| 115 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 116 | const bool expectsExpensiveRendering = !mExpensiveDisplays.empty(); |
| 117 | if (mNotifiedExpensiveRendering != expectsExpensiveRendering) { |
Dan Stoza | 2095000 | 2020-06-18 14:56:58 -0700 | [diff] [blame] | 118 | std::lock_guard lock(mPowerHalMutex); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 119 | HalWrapper* const halWrapper = getPowerHal(); |
| 120 | if (halWrapper == nullptr) { |
Peiyong Lin | 8193497 | 2018-07-02 11:00:54 -0700 | [diff] [blame] | 121 | return; |
| 122 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 123 | |
| 124 | if (!halWrapper->setExpensiveRendering(expectsExpensiveRendering)) { |
| 125 | // The HAL has become unavailable; attempt to reconnect later |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 126 | mReconnectPowerHal = true; |
| 127 | return; |
| 128 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 129 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 130 | mNotifiedExpensiveRendering = expectsExpensiveRendering; |
| 131 | } |
| 132 | } |
| 133 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 134 | void PowerAdvisor::notifyDisplayUpdateImminent() { |
Dan Stoza | 29e7bdf | 2020-03-23 14:43:09 -0700 | [diff] [blame] | 135 | // Only start sending this notification once the system has booted so we don't introduce an |
| 136 | // early-boot dependency on Power HAL |
| 137 | if (!mBootFinished.load()) { |
| 138 | return; |
| 139 | } |
| 140 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 141 | if (mSendUpdateImminent.load()) { |
Dan Stoza | 2095000 | 2020-06-18 14:56:58 -0700 | [diff] [blame] | 142 | std::lock_guard lock(mPowerHalMutex); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 143 | HalWrapper* const halWrapper = getPowerHal(); |
| 144 | if (halWrapper == nullptr) { |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | if (!halWrapper->notifyDisplayUpdateImminent()) { |
| 149 | // The HAL has become unavailable; attempt to reconnect later |
| 150 | mReconnectPowerHal = true; |
| 151 | return; |
| 152 | } |
| 153 | } |
| 154 | |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 155 | if (mUseScreenUpdateTimer) { |
| 156 | mScreenUpdateTimer.reset(); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 160 | // checks both if it supports and if it's enabled |
| 161 | bool PowerAdvisor::usePowerHintSession() { |
| 162 | // uses cached value since the underlying support and flag are unlikely to change at runtime |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 163 | return mPowerHintEnabled.value_or(false) && supportsPowerHintSession(); |
| 164 | } |
| 165 | |
| 166 | bool PowerAdvisor::supportsPowerHintSession() { |
| 167 | // cache to avoid needing lock every time |
| 168 | if (!mSupportsPowerHint.has_value()) { |
| 169 | std::lock_guard lock(mPowerHalMutex); |
| 170 | HalWrapper* const halWrapper = getPowerHal(); |
| 171 | mSupportsPowerHint = halWrapper->supportsPowerHintSession(); |
| 172 | } |
| 173 | return *mSupportsPowerHint; |
| 174 | } |
| 175 | |
| 176 | bool PowerAdvisor::isPowerHintSessionRunning() { |
| 177 | return mPowerHintSessionRunning; |
| 178 | } |
| 179 | |
| 180 | void PowerAdvisor::setTargetWorkDuration(int64_t targetDurationNanos) { |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 181 | if (!usePowerHintSession()) { |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 182 | ALOGV("Power hint session target duration cannot be set, skipping"); |
| 183 | return; |
| 184 | } |
| 185 | { |
| 186 | std::lock_guard lock(mPowerHalMutex); |
| 187 | HalWrapper* const halWrapper = getPowerHal(); |
| 188 | if (halWrapper != nullptr) { |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 189 | halWrapper->setTargetWorkDuration(targetDurationNanos - kTargetSafetyMargin.count()); |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void PowerAdvisor::sendActualWorkDuration(int64_t actualDurationNanos, nsecs_t timeStampNanos) { |
| 195 | if (!mBootFinished || !usePowerHintSession()) { |
| 196 | ALOGV("Actual work duration power hint cannot be sent, skipping"); |
| 197 | return; |
| 198 | } |
| 199 | { |
| 200 | std::lock_guard lock(mPowerHalMutex); |
| 201 | HalWrapper* const halWrapper = getPowerHal(); |
| 202 | if (halWrapper != nullptr) { |
| 203 | halWrapper->sendActualWorkDuration(actualDurationNanos, timeStampNanos); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // needs to be set after the flag is known but before PowerAdvisor enters onBootFinished |
| 209 | void PowerAdvisor::enablePowerHint(bool enabled) { |
| 210 | mPowerHintEnabled = enabled; |
| 211 | } |
| 212 | |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 213 | bool PowerAdvisor::startPowerHintSession(const std::vector<int32_t>& threadIds) { |
| 214 | if (!usePowerHintSession()) { |
| 215 | ALOGI("Power hint session cannot be started, skipping"); |
| 216 | } |
| 217 | { |
| 218 | std::lock_guard lock(mPowerHalMutex); |
| 219 | HalWrapper* halWrapper = getPowerHal(); |
| 220 | if (halWrapper != nullptr && usePowerHintSession()) { |
| 221 | halWrapper->setPowerHintSessionThreadIds(threadIds); |
| 222 | mPowerHintSessionRunning = halWrapper->startPowerHintSession(); |
| 223 | } |
| 224 | } |
| 225 | return mPowerHintSessionRunning; |
| 226 | } |
| 227 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 228 | class HidlPowerHalWrapper : public PowerAdvisor::HalWrapper { |
| 229 | public: |
| 230 | HidlPowerHalWrapper(sp<V1_3::IPower> powerHal) : mPowerHal(std::move(powerHal)) {} |
| 231 | |
| 232 | ~HidlPowerHalWrapper() override = default; |
| 233 | |
| 234 | static std::unique_ptr<HalWrapper> connect() { |
| 235 | // Power HAL 1.3 is not guaranteed to be available, thus we need to query |
| 236 | // Power HAL 1.0 first and try to cast it to Power HAL 1.3. |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 237 | sp<V1_3::IPower> powerHal = nullptr; |
Dan Stoza | 9c051c0 | 2020-02-28 10:19:07 -0800 | [diff] [blame] | 238 | sp<V1_0::IPower> powerHal_1_0 = V1_0::IPower::getService(); |
| 239 | if (powerHal_1_0 != nullptr) { |
| 240 | // Try to cast to Power HAL 1.3 |
| 241 | powerHal = V1_3::IPower::castFrom(powerHal_1_0); |
| 242 | if (powerHal == nullptr) { |
| 243 | ALOGW("No Power HAL 1.3 service in system, disabling PowerAdvisor"); |
| 244 | } else { |
| 245 | ALOGI("Loaded Power HAL 1.3 service"); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 246 | } |
Dan Stoza | 9c051c0 | 2020-02-28 10:19:07 -0800 | [diff] [blame] | 247 | } else { |
| 248 | ALOGW("No Power HAL found, disabling PowerAdvisor"); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 249 | } |
Dan Stoza | 9c051c0 | 2020-02-28 10:19:07 -0800 | [diff] [blame] | 250 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 251 | if (powerHal == nullptr) { |
| 252 | return nullptr; |
| 253 | } |
| 254 | |
| 255 | return std::make_unique<HidlPowerHalWrapper>(std::move(powerHal)); |
| 256 | } |
| 257 | |
| 258 | bool setExpensiveRendering(bool enabled) override { |
| 259 | ALOGV("HIDL setExpensiveRendering %s", enabled ? "T" : "F"); |
| 260 | auto ret = mPowerHal->powerHintAsync_1_3(PowerHint::EXPENSIVE_RENDERING, enabled); |
Ady Abraham | abce165 | 2022-02-24 10:51:19 -0800 | [diff] [blame] | 261 | if (ret.isOk()) { |
| 262 | traceExpensiveRendering(enabled); |
| 263 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 264 | return ret.isOk(); |
| 265 | } |
| 266 | |
| 267 | bool notifyDisplayUpdateImminent() override { |
| 268 | // Power HAL 1.x doesn't have a notification for this |
| 269 | ALOGV("HIDL notifyUpdateImminent received but can't send"); |
| 270 | return true; |
| 271 | } |
| 272 | |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 273 | bool supportsPowerHintSession() override { return false; } |
| 274 | |
| 275 | bool isPowerHintSessionRunning() override { return false; } |
| 276 | |
| 277 | void restartPowerHintSession() override {} |
| 278 | |
| 279 | void setPowerHintSessionThreadIds(const std::vector<int32_t>&) override {} |
| 280 | |
| 281 | bool startPowerHintSession() override { return false; } |
| 282 | |
| 283 | void setTargetWorkDuration(int64_t) override {} |
| 284 | |
| 285 | void sendActualWorkDuration(int64_t, nsecs_t) override {} |
| 286 | |
| 287 | bool shouldReconnectHAL() override { return false; } |
| 288 | |
| 289 | std::vector<int32_t> getPowerHintSessionThreadIds() override { return std::vector<int32_t>{}; } |
| 290 | |
| 291 | std::optional<int64_t> getTargetWorkDuration() override { return std::nullopt; } |
| 292 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 293 | private: |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 294 | const sp<V1_3::IPower> mPowerHal = nullptr; |
| 295 | }; |
| 296 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 297 | class AidlPowerHalWrapper : public PowerAdvisor::HalWrapper { |
| 298 | public: |
| 299 | AidlPowerHalWrapper(sp<IPower> powerHal) : mPowerHal(std::move(powerHal)) { |
| 300 | auto ret = mPowerHal->isModeSupported(Mode::EXPENSIVE_RENDERING, &mHasExpensiveRendering); |
| 301 | if (!ret.isOk()) { |
| 302 | mHasExpensiveRendering = false; |
| 303 | } |
| 304 | |
| 305 | ret = mPowerHal->isBoostSupported(Boost::DISPLAY_UPDATE_IMMINENT, |
| 306 | &mHasDisplayUpdateImminent); |
| 307 | if (!ret.isOk()) { |
| 308 | mHasDisplayUpdateImminent = false; |
| 309 | } |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 310 | |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 311 | mSupportsPowerHint = checkPowerHintSessionSupported(); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 314 | ~AidlPowerHalWrapper() override { |
| 315 | if (mPowerHintSession != nullptr) { |
| 316 | mPowerHintSession->close(); |
| 317 | mPowerHintSession = nullptr; |
| 318 | } |
| 319 | }; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 320 | |
| 321 | static std::unique_ptr<HalWrapper> connect() { |
| 322 | // This only waits if the service is actually declared |
| 323 | sp<IPower> powerHal = waitForVintfService<IPower>(); |
| 324 | if (powerHal == nullptr) { |
| 325 | return nullptr; |
| 326 | } |
| 327 | ALOGI("Loaded AIDL Power HAL service"); |
| 328 | |
| 329 | return std::make_unique<AidlPowerHalWrapper>(std::move(powerHal)); |
| 330 | } |
| 331 | |
| 332 | bool setExpensiveRendering(bool enabled) override { |
| 333 | ALOGV("AIDL setExpensiveRendering %s", enabled ? "T" : "F"); |
| 334 | if (!mHasExpensiveRendering) { |
| 335 | ALOGV("Skipped sending EXPENSIVE_RENDERING because HAL doesn't support it"); |
| 336 | return true; |
| 337 | } |
| 338 | |
| 339 | auto ret = mPowerHal->setMode(Mode::EXPENSIVE_RENDERING, enabled); |
Ady Abraham | abce165 | 2022-02-24 10:51:19 -0800 | [diff] [blame] | 340 | if (ret.isOk()) { |
| 341 | traceExpensiveRendering(enabled); |
| 342 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 343 | return ret.isOk(); |
| 344 | } |
| 345 | |
| 346 | bool notifyDisplayUpdateImminent() override { |
| 347 | ALOGV("AIDL notifyDisplayUpdateImminent"); |
| 348 | if (!mHasDisplayUpdateImminent) { |
| 349 | ALOGV("Skipped sending DISPLAY_UPDATE_IMMINENT because HAL doesn't support it"); |
| 350 | return true; |
| 351 | } |
| 352 | |
| 353 | auto ret = mPowerHal->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 0); |
| 354 | return ret.isOk(); |
| 355 | } |
| 356 | |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 357 | // only version 2+ of the aidl supports power hint sessions, hidl has no support |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 358 | bool supportsPowerHintSession() override { return mSupportsPowerHint; } |
| 359 | |
| 360 | bool checkPowerHintSessionSupported() { |
| 361 | int64_t unused; |
| 362 | // Try to get preferred rate to determine if hint sessions are supported |
| 363 | // We check for isOk not EX_UNSUPPORTED_OPERATION to lump other errors |
| 364 | return mPowerHal->getHintSessionPreferredRate(&unused).isOk(); |
| 365 | } |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 366 | |
| 367 | bool isPowerHintSessionRunning() override { return mPowerHintSession != nullptr; } |
| 368 | |
| 369 | void closePowerHintSession() { |
| 370 | if (mPowerHintSession != nullptr) { |
| 371 | mPowerHintSession->close(); |
| 372 | mPowerHintSession = nullptr; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | void restartPowerHintSession() { |
| 377 | closePowerHintSession(); |
| 378 | startPowerHintSession(); |
| 379 | } |
| 380 | |
| 381 | void setPowerHintSessionThreadIds(const std::vector<int32_t>& threadIds) override { |
| 382 | if (threadIds != mPowerHintThreadIds) { |
| 383 | mPowerHintThreadIds = threadIds; |
| 384 | if (isPowerHintSessionRunning()) { |
| 385 | restartPowerHintSession(); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | bool startPowerHintSession() override { |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 391 | if (mPowerHintSession != nullptr || mPowerHintThreadIds.empty()) { |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 392 | ALOGV("Cannot start power hint session, skipping"); |
| 393 | return false; |
| 394 | } |
| 395 | auto ret = mPowerHal->createHintSession(getpid(), static_cast<int32_t>(getuid()), |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 396 | mPowerHintThreadIds, mTargetDuration, |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 397 | &mPowerHintSession); |
| 398 | if (!ret.isOk()) { |
| 399 | ALOGW("Failed to start power hint session with error: %s", |
| 400 | ret.exceptionToString(ret.exceptionCode()).c_str()); |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 401 | } else { |
| 402 | mLastTargetDurationSent = mTargetDuration; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 403 | } |
| 404 | return isPowerHintSessionRunning(); |
| 405 | } |
| 406 | |
| 407 | bool shouldSetTargetDuration(int64_t targetDurationNanos) { |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 408 | // report if the change in target from our last submission to now exceeds the threshold |
| 409 | return abs(1.0 - |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 410 | static_cast<double>(mLastTargetDurationSent) / |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 411 | static_cast<double>(targetDurationNanos)) >= |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 412 | kAllowedTargetDeviationPercent; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | void setTargetWorkDuration(int64_t targetDurationNanos) override { |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 416 | ATRACE_CALL(); |
| 417 | mTargetDuration = targetDurationNanos; |
| 418 | if (sTraceHintSessionData) ATRACE_INT64("Time target", targetDurationNanos); |
| 419 | if (!sNormalizeTarget && shouldSetTargetDuration(targetDurationNanos) && |
| 420 | isPowerHintSessionRunning()) { |
| 421 | if (mLastActualDurationSent.has_value()) { |
| 422 | // update the error term here since we are actually sending an update to powerhal |
| 423 | if (sTraceHintSessionData) |
| 424 | ATRACE_INT64("Target error term", |
| 425 | targetDurationNanos - *mLastActualDurationSent); |
| 426 | } |
| 427 | ALOGV("Sending target time: %lld ns", static_cast<long long>(targetDurationNanos)); |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 428 | mLastTargetDurationSent = targetDurationNanos; |
| 429 | auto ret = mPowerHintSession->updateTargetWorkDuration(targetDurationNanos); |
| 430 | if (!ret.isOk()) { |
| 431 | ALOGW("Failed to set power hint target work duration with error: %s", |
| 432 | ret.exceptionMessage().c_str()); |
| 433 | mShouldReconnectHal = true; |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | bool shouldReportActualDurationsNow() { |
Matt Buckley | 5793e16 | 2022-01-11 20:35:26 +0000 | [diff] [blame] | 439 | // report if we have never reported before or are approaching a stale session |
| 440 | if (!mLastActualDurationSent.has_value() || |
| 441 | (systemTime() - mLastActualReportTimestamp) > kStaleTimeout.count()) { |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 442 | return true; |
| 443 | } |
| 444 | |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 445 | if (!mActualDuration.has_value()) { |
| 446 | return false; |
| 447 | } |
| 448 | |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 449 | // duration of most recent timing |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 450 | const double mostRecentActualDuration = static_cast<double>(*mActualDuration); |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 451 | // duration of the last timing actually reported to the powerhal |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 452 | const double lastReportedActualDuration = static_cast<double>(*mLastActualDurationSent); |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 453 | |
| 454 | // report if the change in duration from then to now exceeds the threshold |
| 455 | return abs(1.0 - mostRecentActualDuration / lastReportedActualDuration) >= |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 456 | kAllowedActualDeviationPercent; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | void sendActualWorkDuration(int64_t actualDurationNanos, nsecs_t timeStampNanos) override { |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 460 | ATRACE_CALL(); |
| 461 | |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 462 | if (actualDurationNanos < 0 || !isPowerHintSessionRunning()) { |
| 463 | ALOGV("Failed to send actual work duration, skipping"); |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | WorkDuration duration; |
| 468 | duration.durationNanos = actualDurationNanos; |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 469 | mActualDuration = actualDurationNanos; |
| 470 | |
| 471 | // normalize the sent values to a pre-set target |
| 472 | if (sNormalizeTarget) { |
| 473 | duration.durationNanos += mLastTargetDurationSent - mTargetDuration; |
| 474 | } |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 475 | duration.timeStampNanos = timeStampNanos; |
| 476 | mPowerHintQueue.push_back(duration); |
| 477 | |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 478 | long long targetNsec = mTargetDuration; |
| 479 | long long durationNsec = actualDurationNanos; |
| 480 | |
| 481 | if (sTraceHintSessionData) { |
| 482 | ATRACE_INT64("Measured duration", durationNsec); |
| 483 | ATRACE_INT64("Target error term", targetNsec - durationNsec); |
| 484 | } |
| 485 | |
| 486 | ALOGV("Sending actual work duration of: %lld on target: %lld with error: %lld", |
| 487 | durationNsec, targetNsec, targetNsec - durationNsec); |
| 488 | |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 489 | // This rate limiter queues similar duration reports to the powerhal into |
| 490 | // batches to avoid excessive binder calls. The criteria to send a given batch |
| 491 | // are outlined in shouldReportActualDurationsNow() |
| 492 | if (shouldReportActualDurationsNow()) { |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 493 | ALOGV("Sending hint update batch"); |
Matt Buckley | 5793e16 | 2022-01-11 20:35:26 +0000 | [diff] [blame] | 494 | mLastActualReportTimestamp = systemTime(); |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 495 | auto ret = mPowerHintSession->reportActualWorkDuration(mPowerHintQueue); |
| 496 | if (!ret.isOk()) { |
| 497 | ALOGW("Failed to report actual work durations with error: %s", |
| 498 | ret.exceptionMessage().c_str()); |
| 499 | mShouldReconnectHal = true; |
| 500 | } |
| 501 | mPowerHintQueue.clear(); |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 502 | // we save the non-normalized value here to detect % changes |
| 503 | mLastActualDurationSent = actualDurationNanos; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
| 507 | bool shouldReconnectHAL() override { return mShouldReconnectHal; } |
| 508 | |
| 509 | std::vector<int32_t> getPowerHintSessionThreadIds() override { return mPowerHintThreadIds; } |
| 510 | |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 511 | std::optional<int64_t> getTargetWorkDuration() override { return mTargetDuration; } |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 512 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 513 | private: |
| 514 | const sp<IPower> mPowerHal = nullptr; |
| 515 | bool mHasExpensiveRendering = false; |
| 516 | bool mHasDisplayUpdateImminent = false; |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 517 | // Used to indicate an error state and need for reconstruction |
| 518 | bool mShouldReconnectHal = false; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 519 | // This is not thread safe, but is currently protected by mPowerHalMutex so it needs no lock |
| 520 | sp<IPowerHintSession> mPowerHintSession = nullptr; |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 521 | // Queue of actual durations saved to report |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 522 | std::vector<WorkDuration> mPowerHintQueue; |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 523 | // The latest un-normalized values we have received for target and actual |
Matt Buckley | 5793e16 | 2022-01-11 20:35:26 +0000 | [diff] [blame] | 524 | int64_t mTargetDuration = kDefaultTarget.count(); |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 525 | std::optional<int64_t> mActualDuration; |
| 526 | // The list of thread ids, stored so we can restart the session from this class if needed |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 527 | std::vector<int32_t> mPowerHintThreadIds; |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 528 | bool mSupportsPowerHint; |
| 529 | // Keep track of the last messages sent for rate limiter change detection |
| 530 | std::optional<int64_t> mLastActualDurationSent; |
Matt Buckley | 5793e16 | 2022-01-11 20:35:26 +0000 | [diff] [blame] | 531 | // timestamp of the last report we sent, used to avoid stale sessions |
| 532 | int64_t mLastActualReportTimestamp = 0; |
| 533 | int64_t mLastTargetDurationSent = kDefaultTarget.count(); |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 534 | // Whether to normalize all the actual values as error terms relative to a constant target |
| 535 | // This saves a binder call by not setting the target, and should not affect the pid values |
| 536 | static const bool sNormalizeTarget; |
| 537 | // Whether we should emit ATRACE_INT data for hint sessions |
| 538 | static const bool sTraceHintSessionData; |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 539 | // Max percent the actual duration can vary without causing a report (eg: 0.1 = 10%) |
| 540 | static constexpr double kAllowedActualDeviationPercent = 0.1; |
| 541 | // Max percent the target duration can vary without causing a report (eg: 0.05 = 5%) |
| 542 | static constexpr double kAllowedTargetDeviationPercent = 0.05; |
| 543 | // Target used for init and normalization, the actual value does not really matter |
Matt Buckley | 5793e16 | 2022-01-11 20:35:26 +0000 | [diff] [blame] | 544 | static constexpr const std::chrono::nanoseconds kDefaultTarget = 50ms; |
Xiang Wang | 76236e1 | 2022-03-22 17:25:30 +0000 | [diff] [blame^] | 545 | // Amount of time after the last message was sent before the session goes stale |
Matt Buckley | 5793e16 | 2022-01-11 20:35:26 +0000 | [diff] [blame] | 546 | // actually 100ms but we use 80 here to ideally avoid going stale |
| 547 | static constexpr const std::chrono::nanoseconds kStaleTimeout = 80ms; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 548 | }; |
| 549 | |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 550 | const bool AidlPowerHalWrapper::sTraceHintSessionData = |
| 551 | base::GetBoolProperty(std::string("debug.sf.trace_hint_sessions"), false); |
| 552 | |
| 553 | const bool AidlPowerHalWrapper::sNormalizeTarget = |
Xiang Wang | 76236e1 | 2022-03-22 17:25:30 +0000 | [diff] [blame^] | 554 | base::GetBoolProperty(std::string("debug.sf.normalize_hint_session_durations"), false); |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 555 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 556 | PowerAdvisor::HalWrapper* PowerAdvisor::getPowerHal() { |
| 557 | static std::unique_ptr<HalWrapper> sHalWrapper = nullptr; |
Dan Stoza | 9c051c0 | 2020-02-28 10:19:07 -0800 | [diff] [blame] | 558 | static bool sHasHal = true; |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 559 | |
Dan Stoza | 9c051c0 | 2020-02-28 10:19:07 -0800 | [diff] [blame] | 560 | if (!sHasHal) { |
| 561 | return nullptr; |
| 562 | } |
| 563 | |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 564 | // grab old hint session values before we destroy any existing wrapper |
| 565 | std::vector<int32_t> oldPowerHintSessionThreadIds; |
| 566 | std::optional<int64_t> oldTargetWorkDuration; |
| 567 | |
| 568 | if (sHalWrapper != nullptr) { |
| 569 | oldPowerHintSessionThreadIds = sHalWrapper->getPowerHintSessionThreadIds(); |
| 570 | oldTargetWorkDuration = sHalWrapper->getTargetWorkDuration(); |
| 571 | } |
| 572 | |
Dan Stoza | 9c051c0 | 2020-02-28 10:19:07 -0800 | [diff] [blame] | 573 | // If we used to have a HAL, but it stopped responding, attempt to reconnect |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 574 | if (mReconnectPowerHal) { |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 575 | sHalWrapper = nullptr; |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 576 | mReconnectPowerHal = false; |
| 577 | } |
| 578 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 579 | if (sHalWrapper != nullptr) { |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 580 | auto wrapper = sHalWrapper.get(); |
| 581 | // if the wrapper is fine, return it, but if it indicates a reconnect, remake it |
| 582 | if (!wrapper->shouldReconnectHAL()) { |
| 583 | return wrapper; |
| 584 | } |
| 585 | sHalWrapper = nullptr; |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 586 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 587 | |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 588 | // at this point, we know for sure there is no running session |
| 589 | mPowerHintSessionRunning = false; |
| 590 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 591 | // First attempt to connect to the AIDL Power HAL |
| 592 | sHalWrapper = AidlPowerHalWrapper::connect(); |
| 593 | |
| 594 | // If that didn't succeed, attempt to connect to the HIDL Power HAL |
| 595 | if (sHalWrapper == nullptr) { |
| 596 | sHalWrapper = HidlPowerHalWrapper::connect(); |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 597 | } else { // if AIDL, pass on any existing hint session values |
| 598 | // thread ids always safe to set |
| 599 | sHalWrapper->setPowerHintSessionThreadIds(oldPowerHintSessionThreadIds); |
| 600 | // only set duration and start if duration is defined |
| 601 | if (oldTargetWorkDuration.has_value()) { |
| 602 | sHalWrapper->setTargetWorkDuration(*oldTargetWorkDuration); |
| 603 | // only start if possible to run and both threadids and duration are defined |
| 604 | if (usePowerHintSession() && !oldPowerHintSessionThreadIds.empty()) { |
| 605 | mPowerHintSessionRunning = sHalWrapper->startPowerHintSession(); |
| 606 | } |
| 607 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Dan Stoza | 9c051c0 | 2020-02-28 10:19:07 -0800 | [diff] [blame] | 610 | // If we make it to this point and still don't have a HAL, it's unlikely we |
| 611 | // will, so stop trying |
| 612 | if (sHalWrapper == nullptr) { |
| 613 | sHasHal = false; |
| 614 | } |
| 615 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 616 | return sHalWrapper.get(); |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | } // namespace impl |
| 620 | } // namespace Hwc2 |
| 621 | } // namespace android |