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