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 { |
Alec Mouri | 29382ad | 2022-05-11 18:38:38 +0000 | [diff] [blame] | 67 | std::chrono::milliseconds getUpdateTimeout() { |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 68 | // Default to a timeout of 80ms if nothing else is specified |
Alec Mouri | 29382ad | 2022-05-11 18:38:38 +0000 | [diff] [blame] | 69 | static std::chrono::milliseconds timeout = |
| 70 | std::chrono::milliseconds(sysprop::display_update_imminent_timeout_ms(80)); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 71 | return timeout; |
| 72 | } |
| 73 | |
Ady Abraham | abce165 | 2022-02-24 10:51:19 -0800 | [diff] [blame] | 74 | void traceExpensiveRendering(bool enabled) { |
| 75 | if (enabled) { |
| 76 | ATRACE_ASYNC_BEGIN("ExpensiveRendering", 0); |
| 77 | } else { |
| 78 | ATRACE_ASYNC_END("ExpensiveRendering", 0); |
| 79 | } |
| 80 | } |
| 81 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 82 | } // namespace |
| 83 | |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 84 | PowerAdvisor::PowerAdvisor(SurfaceFlinger& flinger) : mFlinger(flinger) { |
Alec Mouri | 29382ad | 2022-05-11 18:38:38 +0000 | [diff] [blame] | 85 | if (getUpdateTimeout() > 0ms) { |
| 86 | mScreenUpdateTimer.emplace("UpdateImminentTimer", getUpdateTimeout(), |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 87 | /* resetCallback */ nullptr, |
| 88 | /* timeoutCallback */ |
| 89 | [this] { |
Alec Mouri | 29382ad | 2022-05-11 18:38:38 +0000 | [diff] [blame] | 90 | while (true) { |
| 91 | auto timeSinceLastUpdate = std::chrono::nanoseconds( |
| 92 | systemTime() - mLastScreenUpdatedTime.load()); |
| 93 | if (timeSinceLastUpdate >= getUpdateTimeout()) { |
| 94 | break; |
| 95 | } |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 96 | // We may try to disable expensive rendering and allow |
| 97 | // for sending DISPLAY_UPDATE_IMMINENT hints too early if |
| 98 | // we idled very shortly after updating the screen, so |
| 99 | // make sure we wait enough time. |
Alec Mouri | 29382ad | 2022-05-11 18:38:38 +0000 | [diff] [blame] | 100 | std::this_thread::sleep_for(getUpdateTimeout() - |
| 101 | timeSinceLastUpdate); |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 102 | } |
| 103 | mSendUpdateImminent.store(true); |
| 104 | mFlinger.disableExpensiveRendering(); |
| 105 | }); |
| 106 | } |
| 107 | } |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 108 | |
| 109 | void PowerAdvisor::init() { |
| 110 | // Defer starting the screen update timer until SurfaceFlinger finishes construction. |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 111 | if (mScreenUpdateTimer) { |
| 112 | mScreenUpdateTimer->start(); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 115 | |
Dan Stoza | 29e7bdf | 2020-03-23 14:43:09 -0700 | [diff] [blame] | 116 | void PowerAdvisor::onBootFinished() { |
| 117 | mBootFinished.store(true); |
| 118 | } |
| 119 | |
Peiyong Lin | 74ca2f4 | 2019-01-14 19:36:57 -0800 | [diff] [blame] | 120 | void PowerAdvisor::setExpensiveRenderingExpected(DisplayId displayId, bool expected) { |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 121 | if (expected) { |
| 122 | mExpensiveDisplays.insert(displayId); |
| 123 | } else { |
| 124 | mExpensiveDisplays.erase(displayId); |
| 125 | } |
| 126 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 127 | const bool expectsExpensiveRendering = !mExpensiveDisplays.empty(); |
| 128 | if (mNotifiedExpensiveRendering != expectsExpensiveRendering) { |
Dan Stoza | 2095000 | 2020-06-18 14:56:58 -0700 | [diff] [blame] | 129 | std::lock_guard lock(mPowerHalMutex); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 130 | HalWrapper* const halWrapper = getPowerHal(); |
| 131 | if (halWrapper == nullptr) { |
Peiyong Lin | 8193497 | 2018-07-02 11:00:54 -0700 | [diff] [blame] | 132 | return; |
| 133 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 134 | |
| 135 | if (!halWrapper->setExpensiveRendering(expectsExpensiveRendering)) { |
| 136 | // The HAL has become unavailable; attempt to reconnect later |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 137 | mReconnectPowerHal = true; |
| 138 | return; |
| 139 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 140 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 141 | mNotifiedExpensiveRendering = expectsExpensiveRendering; |
| 142 | } |
| 143 | } |
| 144 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 145 | void PowerAdvisor::notifyDisplayUpdateImminent() { |
Dan Stoza | 29e7bdf | 2020-03-23 14:43:09 -0700 | [diff] [blame] | 146 | // Only start sending this notification once the system has booted so we don't introduce an |
| 147 | // early-boot dependency on Power HAL |
| 148 | if (!mBootFinished.load()) { |
| 149 | return; |
| 150 | } |
| 151 | |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 152 | if (mSendUpdateImminent.exchange(false)) { |
Dan Stoza | 2095000 | 2020-06-18 14:56:58 -0700 | [diff] [blame] | 153 | std::lock_guard lock(mPowerHalMutex); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 154 | HalWrapper* const halWrapper = getPowerHal(); |
| 155 | if (halWrapper == nullptr) { |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | if (!halWrapper->notifyDisplayUpdateImminent()) { |
| 160 | // The HAL has become unavailable; attempt to reconnect later |
| 161 | mReconnectPowerHal = true; |
| 162 | return; |
| 163 | } |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 164 | |
| 165 | if (mScreenUpdateTimer) { |
| 166 | mScreenUpdateTimer->reset(); |
| 167 | } else { |
| 168 | // If we don't have a screen update timer, then we don't throttle power hal calls so |
| 169 | // flip this bit back to allow for calling into power hal again. |
| 170 | mSendUpdateImminent.store(true); |
| 171 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 174 | if (mScreenUpdateTimer) { |
| 175 | mLastScreenUpdatedTime.store(systemTime()); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 179 | // checks both if it supports and if it's enabled |
| 180 | bool PowerAdvisor::usePowerHintSession() { |
| 181 | // 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] | 182 | return mPowerHintEnabled.value_or(false) && supportsPowerHintSession(); |
| 183 | } |
| 184 | |
| 185 | bool PowerAdvisor::supportsPowerHintSession() { |
| 186 | // cache to avoid needing lock every time |
| 187 | if (!mSupportsPowerHint.has_value()) { |
| 188 | std::lock_guard lock(mPowerHalMutex); |
| 189 | HalWrapper* const halWrapper = getPowerHal(); |
| 190 | mSupportsPowerHint = halWrapper->supportsPowerHintSession(); |
| 191 | } |
| 192 | return *mSupportsPowerHint; |
| 193 | } |
| 194 | |
| 195 | bool PowerAdvisor::isPowerHintSessionRunning() { |
| 196 | return mPowerHintSessionRunning; |
| 197 | } |
| 198 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 199 | void PowerAdvisor::setTargetWorkDuration(int64_t targetDuration) { |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 200 | if (!usePowerHintSession()) { |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 201 | ALOGV("Power hint session target duration cannot be set, skipping"); |
| 202 | return; |
| 203 | } |
| 204 | { |
| 205 | std::lock_guard lock(mPowerHalMutex); |
| 206 | HalWrapper* const halWrapper = getPowerHal(); |
| 207 | if (halWrapper != nullptr) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 208 | halWrapper->setTargetWorkDuration(targetDuration); |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 213 | void PowerAdvisor::sendActualWorkDuration() { |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 214 | if (!mBootFinished || !usePowerHintSession()) { |
| 215 | ALOGV("Actual work duration power hint cannot be sent, skipping"); |
| 216 | return; |
| 217 | } |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 218 | const std::optional<nsecs_t> actualDuration = estimateWorkDuration(false); |
| 219 | if (actualDuration.has_value()) { |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 220 | std::lock_guard lock(mPowerHalMutex); |
| 221 | HalWrapper* const halWrapper = getPowerHal(); |
| 222 | if (halWrapper != nullptr) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 223 | halWrapper->sendActualWorkDuration(*actualDuration + kTargetSafetyMargin.count(), |
| 224 | systemTime()); |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 229 | void PowerAdvisor::sendPredictedWorkDuration() { |
| 230 | if (!mBootFinished || !usePowerHintSession()) { |
| 231 | ALOGV("Actual work duration power hint cannot be sent, skipping"); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | const std::optional<nsecs_t> predictedDuration = estimateWorkDuration(true); |
| 236 | |
| 237 | if (predictedDuration.has_value()) { |
| 238 | std::lock_guard lock(mPowerHalMutex); |
| 239 | HalWrapper* const halWrapper = getPowerHal(); |
| 240 | if (halWrapper != nullptr) { |
Matt Buckley | ffabce9 | 2022-06-17 13:52:46 -0700 | [diff] [blame] | 241 | halWrapper->sendActualWorkDuration(*predictedDuration + kTargetSafetyMargin.count(), |
| 242 | systemTime()); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 247 | void PowerAdvisor::enablePowerHint(bool enabled) { |
| 248 | mPowerHintEnabled = enabled; |
| 249 | } |
| 250 | |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 251 | bool PowerAdvisor::startPowerHintSession(const std::vector<int32_t>& threadIds) { |
| 252 | if (!usePowerHintSession()) { |
| 253 | ALOGI("Power hint session cannot be started, skipping"); |
| 254 | } |
| 255 | { |
| 256 | std::lock_guard lock(mPowerHalMutex); |
| 257 | HalWrapper* halWrapper = getPowerHal(); |
| 258 | if (halWrapper != nullptr && usePowerHintSession()) { |
| 259 | halWrapper->setPowerHintSessionThreadIds(threadIds); |
| 260 | mPowerHintSessionRunning = halWrapper->startPowerHintSession(); |
| 261 | } |
| 262 | } |
| 263 | return mPowerHintSessionRunning; |
| 264 | } |
| 265 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 266 | void PowerAdvisor::setGpuFenceTime(DisplayId displayId, std::unique_ptr<FenceTime>&& fenceTime) { |
| 267 | DisplayTimingData& displayData = mDisplayTimingData[displayId]; |
| 268 | if (displayData.gpuEndFenceTime) { |
| 269 | nsecs_t signalTime = displayData.gpuEndFenceTime->getSignalTime(); |
| 270 | if (signalTime != Fence::SIGNAL_TIME_INVALID && signalTime != Fence::SIGNAL_TIME_PENDING) { |
| 271 | for (auto&& [_, otherDisplayData] : mDisplayTimingData) { |
| 272 | // If the previous display started before us but ended after we should have |
| 273 | // started, then it likely delayed our start time and we must compensate for that. |
| 274 | // Displays finishing earlier should have already made their way through this call |
| 275 | // and swapped their timing into "lastValid" from "latest", so we check that here. |
| 276 | if (!otherDisplayData.lastValidGpuStartTime.has_value()) continue; |
| 277 | if ((*otherDisplayData.lastValidGpuStartTime < *displayData.gpuStartTime) && |
| 278 | (*otherDisplayData.lastValidGpuEndTime > *displayData.gpuStartTime)) { |
| 279 | displayData.lastValidGpuStartTime = *otherDisplayData.lastValidGpuEndTime; |
| 280 | break; |
| 281 | } |
| 282 | } |
| 283 | displayData.lastValidGpuStartTime = displayData.gpuStartTime; |
| 284 | displayData.lastValidGpuEndTime = signalTime; |
| 285 | } |
| 286 | } |
| 287 | displayData.gpuEndFenceTime = std::move(fenceTime); |
| 288 | displayData.gpuStartTime = systemTime(); |
| 289 | } |
| 290 | |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 291 | void PowerAdvisor::setHwcValidateTiming(DisplayId displayId, nsecs_t validateStartTime, |
| 292 | nsecs_t validateEndTime) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 293 | DisplayTimingData& displayData = mDisplayTimingData[displayId]; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 294 | displayData.hwcValidateStartTime = validateStartTime; |
| 295 | displayData.hwcValidateEndTime = validateEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 298 | void PowerAdvisor::setHwcPresentTiming(DisplayId displayId, nsecs_t presentStartTime, |
| 299 | nsecs_t presentEndTime) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 300 | DisplayTimingData& displayData = mDisplayTimingData[displayId]; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 301 | displayData.hwcPresentStartTime = presentStartTime; |
| 302 | displayData.hwcPresentEndTime = presentEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void PowerAdvisor::setSkippedValidate(DisplayId displayId, bool skipped) { |
| 306 | mDisplayTimingData[displayId].skippedValidate = skipped; |
| 307 | } |
| 308 | |
| 309 | void PowerAdvisor::setRequiresClientComposition(DisplayId displayId, |
| 310 | bool requiresClientComposition) { |
| 311 | mDisplayTimingData[displayId].usedClientComposition = requiresClientComposition; |
| 312 | } |
| 313 | |
| 314 | void PowerAdvisor::setExpectedPresentTime(nsecs_t expectedPresentTime) { |
| 315 | mExpectedPresentTimes.append(expectedPresentTime); |
| 316 | } |
| 317 | |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame^] | 318 | void PowerAdvisor::setPresentFenceTime(nsecs_t presentFenceTime) { |
| 319 | mLastPresentFenceTime = presentFenceTime; |
| 320 | } |
| 321 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 322 | void PowerAdvisor::setFrameDelay(nsecs_t frameDelayDuration) { |
| 323 | mFrameDelayDuration = frameDelayDuration; |
| 324 | } |
| 325 | |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 326 | void PowerAdvisor::setHwcPresentDelayedTime( |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 327 | DisplayId displayId, std::chrono::steady_clock::time_point earliestFrameStartTime) { |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 328 | mDisplayTimingData[displayId].hwcPresentDelayedTime = |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 329 | (earliestFrameStartTime - std::chrono::steady_clock::now()).count() + systemTime(); |
| 330 | } |
| 331 | |
| 332 | void PowerAdvisor::setCommitStart(nsecs_t commitStartTime) { |
| 333 | mCommitStartTimes.append(commitStartTime); |
| 334 | } |
| 335 | |
| 336 | void PowerAdvisor::setCompositeEnd(nsecs_t compositeEnd) { |
| 337 | mLastCompositeEndTime = compositeEnd; |
| 338 | // calculate the postcomp time here as well |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 339 | std::vector<DisplayId>&& displays = getOrderedDisplayIds(&DisplayTimingData::hwcPresentEndTime); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 340 | DisplayTimingData& timingData = mDisplayTimingData[displays.back()]; |
| 341 | mLastPostcompDuration = compositeEnd - |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 342 | (timingData.skippedValidate ? *timingData.hwcValidateEndTime |
| 343 | : *timingData.hwcPresentEndTime); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void PowerAdvisor::setDisplays(std::vector<DisplayId>& displayIds) { |
| 347 | mDisplayIds = displayIds; |
| 348 | } |
| 349 | |
| 350 | void PowerAdvisor::setTotalFrameTargetWorkDuration(nsecs_t targetDuration) { |
| 351 | mTotalFrameTargetDuration = targetDuration; |
| 352 | } |
| 353 | |
| 354 | std::vector<DisplayId> PowerAdvisor::getOrderedDisplayIds( |
| 355 | std::optional<nsecs_t> DisplayTimingData::*sortBy) { |
| 356 | std::vector<DisplayId> sortedDisplays; |
| 357 | std::copy_if(mDisplayIds.begin(), mDisplayIds.end(), std::back_inserter(sortedDisplays), |
| 358 | [&](DisplayId id) { |
| 359 | return mDisplayTimingData.count(id) && |
| 360 | (mDisplayTimingData[id].*sortBy).has_value(); |
| 361 | }); |
| 362 | std::sort(sortedDisplays.begin(), sortedDisplays.end(), [&](DisplayId idA, DisplayId idB) { |
| 363 | return *(mDisplayTimingData[idA].*sortBy) < *(mDisplayTimingData[idB].*sortBy); |
| 364 | }); |
| 365 | return sortedDisplays; |
| 366 | } |
| 367 | |
| 368 | std::optional<nsecs_t> PowerAdvisor::estimateWorkDuration(bool earlyHint) { |
| 369 | if (earlyHint && (!mExpectedPresentTimes.isFull() || !mCommitStartTimes.isFull())) { |
| 370 | return std::nullopt; |
| 371 | } |
| 372 | |
| 373 | // Tracks when we finish presenting to hwc |
| 374 | nsecs_t estimatedEndTime = mCommitStartTimes[0]; |
| 375 | |
| 376 | // How long we spent this frame not doing anything, waiting for fences or vsync |
| 377 | nsecs_t idleDuration = 0; |
| 378 | |
| 379 | // Most recent previous gpu end time in the current frame, probably from a prior display, used |
| 380 | // as the start time for the next gpu operation if it ran over time since it probably blocked |
| 381 | std::optional<nsecs_t> previousValidGpuEndTime; |
| 382 | |
| 383 | // The currently estimated gpu end time for the frame, |
| 384 | // used to accumulate gpu time as we iterate over the active displays |
| 385 | std::optional<nsecs_t> estimatedGpuEndTime; |
| 386 | |
| 387 | // If we're predicting at the start of the frame, we use last frame as our reference point |
| 388 | // If we're predicting at the end of the frame, we use the current frame as a reference point |
| 389 | nsecs_t referenceFrameStartTime = (earlyHint ? mCommitStartTimes[-1] : mCommitStartTimes[0]); |
| 390 | |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame^] | 391 | // When the prior frame should be presenting to the display |
| 392 | // If we're predicting at the start of the frame, we use last frame's expected present time |
| 393 | // If we're predicting at the end of the frame, the present fence time is already known |
| 394 | nsecs_t lastFramePresentTime = (earlyHint ? mExpectedPresentTimes[-1] : mLastPresentFenceTime); |
| 395 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 396 | // The timing info for the previously calculated display, if there was one |
| 397 | std::optional<DisplayTimeline> previousDisplayReferenceTiming; |
| 398 | std::vector<DisplayId>&& displayIds = |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 399 | getOrderedDisplayIds(&DisplayTimingData::hwcPresentStartTime); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 400 | DisplayTimeline referenceTiming, estimatedTiming; |
| 401 | |
| 402 | // Iterate over the displays in the same order they are presented |
| 403 | for (DisplayId displayId : displayIds) { |
| 404 | if (mDisplayTimingData.count(displayId) == 0) { |
| 405 | continue; |
| 406 | } |
| 407 | |
| 408 | auto& displayData = mDisplayTimingData.at(displayId); |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame^] | 409 | |
| 410 | // mLastPresentFenceTime should always be the time of the reference frame, since it will be |
| 411 | // the previous frame's present fence if called at the start, and current frame's if called |
| 412 | // at the end |
| 413 | referenceTiming = displayData.calculateDisplayTimeline(mLastPresentFenceTime); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 414 | |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 415 | // If this is the first display, include the duration before hwc present starts |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 416 | if (!previousDisplayReferenceTiming.has_value()) { |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 417 | estimatedEndTime += referenceTiming.hwcPresentStartTime - referenceFrameStartTime; |
| 418 | } else { // Otherwise add the time since last display's hwc present finished |
| 419 | estimatedEndTime += referenceTiming.hwcPresentStartTime - |
| 420 | previousDisplayReferenceTiming->hwcPresentEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame^] | 423 | estimatedTiming = referenceTiming.estimateTimelineFromReference(lastFramePresentTime, |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 424 | estimatedEndTime); |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame^] | 425 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 426 | // Update predicted present finish time with this display's present time |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 427 | estimatedEndTime = estimatedTiming.hwcPresentEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 428 | |
| 429 | // Track how long we spent waiting for the fence, can be excluded from the timing estimate |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame^] | 430 | idleDuration += estimatedTiming.probablyWaitsForPresentFence |
| 431 | ? lastFramePresentTime - estimatedTiming.presentFenceWaitStartTime |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 432 | : 0; |
| 433 | |
| 434 | // Track how long we spent waiting to present, can be excluded from the timing estimate |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 435 | idleDuration += earlyHint ? 0 : referenceTiming.hwcPresentDelayDuration; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 436 | |
| 437 | // Estimate the reference frame's gpu timing |
| 438 | auto gpuTiming = displayData.estimateGpuTiming(previousValidGpuEndTime); |
| 439 | if (gpuTiming.has_value()) { |
| 440 | previousValidGpuEndTime = gpuTiming->startTime + gpuTiming->duration; |
| 441 | |
| 442 | // Estimate the prediction frame's gpu end time from the reference frame |
| 443 | estimatedGpuEndTime = |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 444 | std::max(estimatedTiming.hwcPresentStartTime, estimatedGpuEndTime.value_or(0)) + |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 445 | gpuTiming->duration; |
| 446 | } |
| 447 | previousDisplayReferenceTiming = referenceTiming; |
| 448 | } |
| 449 | ATRACE_INT64("Idle duration", idleDuration); |
| 450 | |
| 451 | // Don't count time spent idly waiting in the estimate as we could do more work in that time |
| 452 | estimatedEndTime -= idleDuration; |
| 453 | |
| 454 | // We finish the frame when both present and the gpu are done, so wait for the later of the two |
| 455 | // Also add the frame delay duration since the target did not move while we were delayed |
| 456 | nsecs_t totalDuration = mFrameDelayDuration + |
| 457 | std::max(estimatedEndTime, estimatedGpuEndTime.value_or(0)) - mCommitStartTimes[0]; |
| 458 | |
| 459 | // We finish SurfaceFlinger when post-composition finishes, so add that in here |
| 460 | nsecs_t flingerDuration = estimatedEndTime + mLastPostcompDuration - mCommitStartTimes[0]; |
| 461 | nsecs_t combinedDuration = combineTimingEstimates(totalDuration, flingerDuration); |
| 462 | |
| 463 | return std::make_optional(combinedDuration); |
| 464 | } |
| 465 | |
| 466 | nsecs_t PowerAdvisor::combineTimingEstimates(nsecs_t totalDuration, nsecs_t flingerDuration) { |
| 467 | nsecs_t targetDuration; |
| 468 | { |
| 469 | std::lock_guard lock(mPowerHalMutex); |
| 470 | targetDuration = *getPowerHal()->getTargetWorkDuration(); |
| 471 | } |
| 472 | if (!mTotalFrameTargetDuration.has_value()) return flingerDuration; |
| 473 | |
| 474 | // Normalize total to the flinger target (vsync period) since that's how often we actually send |
| 475 | // hints |
| 476 | nsecs_t normalizedTotalDuration = (targetDuration * totalDuration) / *mTotalFrameTargetDuration; |
| 477 | return std::max(flingerDuration, normalizedTotalDuration); |
| 478 | } |
| 479 | |
| 480 | PowerAdvisor::DisplayTimeline PowerAdvisor::DisplayTimeline::estimateTimelineFromReference( |
| 481 | nsecs_t fenceTime, nsecs_t displayStartTime) { |
| 482 | DisplayTimeline estimated; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 483 | estimated.hwcPresentStartTime = displayStartTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 484 | |
| 485 | // We don't predict waiting for vsync alignment yet |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 486 | estimated.hwcPresentDelayDuration = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 487 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 488 | // How long we expect to run before we start waiting for the fence |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame^] | 489 | // For now just re-use last frame's post-present duration and assume it will not change much |
| 490 | // Excludes time spent waiting for vsync since that's not going to be consistent |
| 491 | estimated.presentFenceWaitStartTime = estimated.hwcPresentStartTime + |
| 492 | (presentFenceWaitStartTime - (hwcPresentStartTime + hwcPresentDelayDuration)); |
| 493 | estimated.probablyWaitsForPresentFence = fenceTime > estimated.presentFenceWaitStartTime; |
| 494 | estimated.hwcPresentEndTime = postPresentFenceHwcPresentDuration + |
| 495 | (estimated.probablyWaitsForPresentFence ? fenceTime |
| 496 | : estimated.presentFenceWaitStartTime); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 497 | return estimated; |
| 498 | } |
| 499 | |
| 500 | PowerAdvisor::DisplayTimeline PowerAdvisor::DisplayTimingData::calculateDisplayTimeline( |
| 501 | nsecs_t fenceTime) { |
| 502 | DisplayTimeline timeline; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 503 | // How long between calling hwc present and trying to wait on the fence |
| 504 | const nsecs_t fenceWaitStartDelay = |
| 505 | (skippedValidate ? kFenceWaitStartDelaySkippedValidate : kFenceWaitStartDelayValidated) |
| 506 | .count(); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 507 | |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 508 | // Did our reference frame wait for an appropriate vsync before calling into hwc |
| 509 | const bool waitedOnHwcPresentTime = hwcPresentDelayedTime.has_value() && |
| 510 | *hwcPresentDelayedTime > *hwcPresentStartTime && |
| 511 | *hwcPresentDelayedTime < *hwcPresentEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 512 | |
| 513 | // Use validate start here if we skipped it because we did validate + present together |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 514 | timeline.hwcPresentStartTime = skippedValidate ? *hwcValidateStartTime : *hwcPresentStartTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 515 | |
| 516 | // Use validate end here if we skipped it because we did validate + present together |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 517 | timeline.hwcPresentEndTime = skippedValidate ? *hwcValidateEndTime : *hwcPresentEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 518 | |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 519 | // How long hwc present was delayed waiting for the next appropriate vsync |
| 520 | timeline.hwcPresentDelayDuration = |
| 521 | (waitedOnHwcPresentTime ? *hwcPresentDelayedTime - *hwcPresentStartTime : 0); |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame^] | 522 | // When we started waiting for the present fence after calling into hwc present |
| 523 | timeline.presentFenceWaitStartTime = |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 524 | timeline.hwcPresentStartTime + timeline.hwcPresentDelayDuration + fenceWaitStartDelay; |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame^] | 525 | timeline.probablyWaitsForPresentFence = fenceTime > timeline.presentFenceWaitStartTime && |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 526 | fenceTime < timeline.hwcPresentEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 527 | |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 528 | // How long we ran after we finished waiting for the fence but before hwc present finished |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame^] | 529 | timeline.postPresentFenceHwcPresentDuration = timeline.hwcPresentEndTime - |
| 530 | (timeline.probablyWaitsForPresentFence ? fenceTime |
| 531 | : timeline.presentFenceWaitStartTime); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 532 | return timeline; |
| 533 | } |
| 534 | |
| 535 | std::optional<PowerAdvisor::GpuTimeline> PowerAdvisor::DisplayTimingData::estimateGpuTiming( |
| 536 | std::optional<nsecs_t> previousEnd) { |
| 537 | if (!(usedClientComposition && lastValidGpuStartTime.has_value() && gpuEndFenceTime)) { |
| 538 | return std::nullopt; |
| 539 | } |
| 540 | const nsecs_t latestGpuStartTime = std::max(previousEnd.value_or(0), *gpuStartTime); |
| 541 | const nsecs_t latestGpuEndTime = gpuEndFenceTime->getSignalTime(); |
| 542 | nsecs_t gpuDuration = 0; |
| 543 | if (latestGpuEndTime != Fence::SIGNAL_TIME_INVALID && |
| 544 | latestGpuEndTime != Fence::SIGNAL_TIME_PENDING) { |
| 545 | // If we know how long the most recent gpu duration was, use that |
| 546 | gpuDuration = latestGpuEndTime - latestGpuStartTime; |
| 547 | } else if (lastValidGpuEndTime.has_value()) { |
| 548 | // If we don't have the fence data, use the most recent information we do have |
| 549 | gpuDuration = *lastValidGpuEndTime - *lastValidGpuStartTime; |
| 550 | if (latestGpuEndTime == Fence::SIGNAL_TIME_PENDING) { |
| 551 | // If pending but went over the previous duration, use current time as the end |
| 552 | gpuDuration = std::max(gpuDuration, systemTime() - latestGpuStartTime); |
| 553 | } |
| 554 | } |
| 555 | return GpuTimeline{.duration = gpuDuration, .startTime = latestGpuStartTime}; |
| 556 | } |
| 557 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 558 | class HidlPowerHalWrapper : public PowerAdvisor::HalWrapper { |
| 559 | public: |
| 560 | HidlPowerHalWrapper(sp<V1_3::IPower> powerHal) : mPowerHal(std::move(powerHal)) {} |
| 561 | |
| 562 | ~HidlPowerHalWrapper() override = default; |
| 563 | |
| 564 | static std::unique_ptr<HalWrapper> connect() { |
| 565 | // Power HAL 1.3 is not guaranteed to be available, thus we need to query |
| 566 | // 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] | 567 | sp<V1_3::IPower> powerHal = nullptr; |
Dan Stoza | 9c051c0 | 2020-02-28 10:19:07 -0800 | [diff] [blame] | 568 | sp<V1_0::IPower> powerHal_1_0 = V1_0::IPower::getService(); |
| 569 | if (powerHal_1_0 != nullptr) { |
| 570 | // Try to cast to Power HAL 1.3 |
| 571 | powerHal = V1_3::IPower::castFrom(powerHal_1_0); |
| 572 | if (powerHal == nullptr) { |
| 573 | ALOGW("No Power HAL 1.3 service in system, disabling PowerAdvisor"); |
| 574 | } else { |
| 575 | ALOGI("Loaded Power HAL 1.3 service"); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 576 | } |
Dan Stoza | 9c051c0 | 2020-02-28 10:19:07 -0800 | [diff] [blame] | 577 | } else { |
| 578 | ALOGW("No Power HAL found, disabling PowerAdvisor"); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 579 | } |
Dan Stoza | 9c051c0 | 2020-02-28 10:19:07 -0800 | [diff] [blame] | 580 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 581 | if (powerHal == nullptr) { |
| 582 | return nullptr; |
| 583 | } |
| 584 | |
| 585 | return std::make_unique<HidlPowerHalWrapper>(std::move(powerHal)); |
| 586 | } |
| 587 | |
| 588 | bool setExpensiveRendering(bool enabled) override { |
| 589 | ALOGV("HIDL setExpensiveRendering %s", enabled ? "T" : "F"); |
| 590 | auto ret = mPowerHal->powerHintAsync_1_3(PowerHint::EXPENSIVE_RENDERING, enabled); |
Ady Abraham | abce165 | 2022-02-24 10:51:19 -0800 | [diff] [blame] | 591 | if (ret.isOk()) { |
| 592 | traceExpensiveRendering(enabled); |
| 593 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 594 | return ret.isOk(); |
| 595 | } |
| 596 | |
| 597 | bool notifyDisplayUpdateImminent() override { |
| 598 | // Power HAL 1.x doesn't have a notification for this |
| 599 | ALOGV("HIDL notifyUpdateImminent received but can't send"); |
| 600 | return true; |
| 601 | } |
| 602 | |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 603 | bool supportsPowerHintSession() override { return false; } |
| 604 | |
| 605 | bool isPowerHintSessionRunning() override { return false; } |
| 606 | |
| 607 | void restartPowerHintSession() override {} |
| 608 | |
| 609 | void setPowerHintSessionThreadIds(const std::vector<int32_t>&) override {} |
| 610 | |
| 611 | bool startPowerHintSession() override { return false; } |
| 612 | |
| 613 | void setTargetWorkDuration(int64_t) override {} |
| 614 | |
| 615 | void sendActualWorkDuration(int64_t, nsecs_t) override {} |
| 616 | |
| 617 | bool shouldReconnectHAL() override { return false; } |
| 618 | |
| 619 | std::vector<int32_t> getPowerHintSessionThreadIds() override { return std::vector<int32_t>{}; } |
| 620 | |
| 621 | std::optional<int64_t> getTargetWorkDuration() override { return std::nullopt; } |
| 622 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 623 | private: |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 624 | const sp<V1_3::IPower> mPowerHal = nullptr; |
| 625 | }; |
| 626 | |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 627 | AidlPowerHalWrapper::AidlPowerHalWrapper(sp<IPower> powerHal) : mPowerHal(std::move(powerHal)) { |
| 628 | auto ret = mPowerHal->isModeSupported(Mode::EXPENSIVE_RENDERING, &mHasExpensiveRendering); |
| 629 | if (!ret.isOk()) { |
| 630 | mHasExpensiveRendering = false; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 633 | ret = mPowerHal->isBoostSupported(Boost::DISPLAY_UPDATE_IMMINENT, &mHasDisplayUpdateImminent); |
| 634 | if (!ret.isOk()) { |
| 635 | mHasDisplayUpdateImminent = false; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 638 | mSupportsPowerHint = checkPowerHintSessionSupported(); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 639 | |
| 640 | mAllowedActualDeviation = |
| 641 | base::GetIntProperty<nsecs_t>("debug.sf.allowed_actual_deviation", |
| 642 | std::chrono::nanoseconds(250us).count()); |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 643 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 644 | |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 645 | AidlPowerHalWrapper::~AidlPowerHalWrapper() { |
| 646 | if (mPowerHintSession != nullptr) { |
| 647 | mPowerHintSession->close(); |
| 648 | mPowerHintSession = nullptr; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 649 | } |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 650 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 651 | |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 652 | std::unique_ptr<PowerAdvisor::HalWrapper> AidlPowerHalWrapper::connect() { |
| 653 | // This only waits if the service is actually declared |
| 654 | sp<IPower> powerHal = waitForVintfService<IPower>(); |
| 655 | if (powerHal == nullptr) { |
| 656 | return nullptr; |
| 657 | } |
| 658 | ALOGI("Loaded AIDL Power HAL service"); |
| 659 | |
| 660 | return std::make_unique<AidlPowerHalWrapper>(std::move(powerHal)); |
| 661 | } |
| 662 | |
| 663 | bool AidlPowerHalWrapper::setExpensiveRendering(bool enabled) { |
| 664 | ALOGV("AIDL setExpensiveRendering %s", enabled ? "T" : "F"); |
| 665 | if (!mHasExpensiveRendering) { |
| 666 | ALOGV("Skipped sending EXPENSIVE_RENDERING because HAL doesn't support it"); |
| 667 | return true; |
| 668 | } |
| 669 | |
| 670 | auto ret = mPowerHal->setMode(Mode::EXPENSIVE_RENDERING, enabled); |
| 671 | if (ret.isOk()) { |
| 672 | traceExpensiveRendering(enabled); |
| 673 | } |
| 674 | return ret.isOk(); |
| 675 | } |
| 676 | |
| 677 | bool AidlPowerHalWrapper::notifyDisplayUpdateImminent() { |
| 678 | ALOGV("AIDL notifyDisplayUpdateImminent"); |
| 679 | if (!mHasDisplayUpdateImminent) { |
| 680 | ALOGV("Skipped sending DISPLAY_UPDATE_IMMINENT because HAL doesn't support it"); |
| 681 | return true; |
| 682 | } |
| 683 | |
| 684 | auto ret = mPowerHal->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 0); |
| 685 | return ret.isOk(); |
| 686 | } |
| 687 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 688 | // Only version 2+ of the aidl supports power hint sessions, hidl has no support |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 689 | bool AidlPowerHalWrapper::supportsPowerHintSession() { |
| 690 | return mSupportsPowerHint; |
| 691 | } |
| 692 | |
| 693 | bool AidlPowerHalWrapper::checkPowerHintSessionSupported() { |
| 694 | int64_t unused; |
| 695 | // Try to get preferred rate to determine if hint sessions are supported |
| 696 | // We check for isOk not EX_UNSUPPORTED_OPERATION to lump together errors |
| 697 | return mPowerHal->getHintSessionPreferredRate(&unused).isOk(); |
| 698 | } |
| 699 | |
| 700 | bool AidlPowerHalWrapper::isPowerHintSessionRunning() { |
| 701 | return mPowerHintSession != nullptr; |
| 702 | } |
| 703 | |
| 704 | void AidlPowerHalWrapper::closePowerHintSession() { |
| 705 | if (mPowerHintSession != nullptr) { |
| 706 | mPowerHintSession->close(); |
| 707 | mPowerHintSession = nullptr; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | void AidlPowerHalWrapper::restartPowerHintSession() { |
| 712 | closePowerHintSession(); |
| 713 | startPowerHintSession(); |
| 714 | } |
| 715 | |
| 716 | void AidlPowerHalWrapper::setPowerHintSessionThreadIds(const std::vector<int32_t>& threadIds) { |
| 717 | if (threadIds != mPowerHintThreadIds) { |
| 718 | mPowerHintThreadIds = threadIds; |
| 719 | if (isPowerHintSessionRunning()) { |
| 720 | restartPowerHintSession(); |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | bool AidlPowerHalWrapper::startPowerHintSession() { |
| 726 | if (mPowerHintSession != nullptr || mPowerHintThreadIds.empty()) { |
| 727 | ALOGV("Cannot start power hint session, skipping"); |
| 728 | return false; |
| 729 | } |
| 730 | auto ret = |
| 731 | mPowerHal->createHintSession(getpid(), static_cast<int32_t>(getuid()), |
| 732 | mPowerHintThreadIds, mTargetDuration, &mPowerHintSession); |
| 733 | if (!ret.isOk()) { |
| 734 | ALOGW("Failed to start power hint session with error: %s", |
| 735 | ret.exceptionToString(ret.exceptionCode()).c_str()); |
| 736 | } else { |
| 737 | mLastTargetDurationSent = mTargetDuration; |
| 738 | } |
| 739 | return isPowerHintSessionRunning(); |
| 740 | } |
| 741 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 742 | void AidlPowerHalWrapper::setTargetWorkDuration(int64_t targetDuration) { |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 743 | ATRACE_CALL(); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 744 | mTargetDuration = targetDuration; |
| 745 | if (sTraceHintSessionData) ATRACE_INT64("Time target", targetDuration); |
| 746 | if (isPowerHintSessionRunning() && (targetDuration != mLastTargetDurationSent)) { |
| 747 | ALOGV("Sending target time: %" PRId64 "ns", targetDuration); |
| 748 | mLastTargetDurationSent = targetDuration; |
| 749 | auto ret = mPowerHintSession->updateTargetWorkDuration(targetDuration); |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 750 | if (!ret.isOk()) { |
| 751 | ALOGW("Failed to set power hint target work duration with error: %s", |
| 752 | ret.exceptionMessage().c_str()); |
| 753 | mShouldReconnectHal = true; |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 758 | bool AidlPowerHalWrapper::shouldReportActualDurations() { |
| 759 | // Report if we have never reported before or are approaching a stale session |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 760 | if (!mLastActualDurationSent.has_value() || |
| 761 | (systemTime() - mLastActualReportTimestamp) > kStaleTimeout.count()) { |
| 762 | return true; |
| 763 | } |
| 764 | |
| 765 | if (!mActualDuration.has_value()) { |
| 766 | return false; |
| 767 | } |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 768 | // Report if the change in actual duration exceeds the threshold |
| 769 | return abs(*mActualDuration - *mLastActualDurationSent) > mAllowedActualDeviation; |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 772 | void AidlPowerHalWrapper::sendActualWorkDuration(int64_t actualDuration, nsecs_t timestamp) { |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 773 | ATRACE_CALL(); |
| 774 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 775 | if (actualDuration < 0 || !isPowerHintSessionRunning()) { |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 776 | ALOGV("Failed to send actual work duration, skipping"); |
| 777 | return; |
| 778 | } |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 779 | const nsecs_t reportedDuration = actualDuration; |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 780 | |
Xiang Wang | 0aba49e | 2022-04-06 16:13:59 +0000 | [diff] [blame] | 781 | mActualDuration = reportedDuration; |
| 782 | WorkDuration duration; |
| 783 | duration.durationNanos = reportedDuration; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 784 | duration.timeStampNanos = timestamp; |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 785 | mPowerHintQueue.push_back(duration); |
| 786 | |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 787 | if (sTraceHintSessionData) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 788 | ATRACE_INT64("Measured duration", actualDuration); |
| 789 | ATRACE_INT64("Target error term", actualDuration - mTargetDuration); |
Xiang Wang | 0aba49e | 2022-04-06 16:13:59 +0000 | [diff] [blame] | 790 | |
| 791 | ATRACE_INT64("Reported duration", reportedDuration); |
| 792 | ATRACE_INT64("Reported target", mLastTargetDurationSent); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 793 | ATRACE_INT64("Reported target error term", reportedDuration - mLastTargetDurationSent); |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Xiang Wang | 0aba49e | 2022-04-06 16:13:59 +0000 | [diff] [blame] | 796 | ALOGV("Sending actual work duration of: %" PRId64 " on reported target: %" PRId64 |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 797 | " with error: %" PRId64, |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 798 | reportedDuration, mLastTargetDurationSent, reportedDuration - mLastTargetDurationSent); |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 799 | |
| 800 | // This rate limiter queues similar duration reports to the powerhal into |
| 801 | // batches to avoid excessive binder calls. The criteria to send a given batch |
| 802 | // are outlined in shouldReportActualDurationsNow() |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 803 | if (shouldReportActualDurations()) { |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 804 | ALOGV("Sending hint update batch"); |
| 805 | mLastActualReportTimestamp = systemTime(); |
| 806 | auto ret = mPowerHintSession->reportActualWorkDuration(mPowerHintQueue); |
| 807 | if (!ret.isOk()) { |
| 808 | ALOGW("Failed to report actual work durations with error: %s", |
| 809 | ret.exceptionMessage().c_str()); |
| 810 | mShouldReconnectHal = true; |
| 811 | } |
| 812 | mPowerHintQueue.clear(); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 813 | // We save the actual duration here for rate limiting |
| 814 | mLastActualDurationSent = actualDuration; |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
| 818 | bool AidlPowerHalWrapper::shouldReconnectHAL() { |
| 819 | return mShouldReconnectHal; |
| 820 | } |
| 821 | |
| 822 | std::vector<int32_t> AidlPowerHalWrapper::getPowerHintSessionThreadIds() { |
| 823 | return mPowerHintThreadIds; |
| 824 | } |
| 825 | |
| 826 | std::optional<int64_t> AidlPowerHalWrapper::getTargetWorkDuration() { |
| 827 | return mTargetDuration; |
| 828 | } |
| 829 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 830 | void AidlPowerHalWrapper::setAllowedActualDeviation(nsecs_t allowedDeviation) { |
| 831 | mAllowedActualDeviation = allowedDeviation; |
| 832 | } |
| 833 | |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 834 | const bool AidlPowerHalWrapper::sTraceHintSessionData = |
| 835 | base::GetBoolProperty(std::string("debug.sf.trace_hint_sessions"), false); |
| 836 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 837 | PowerAdvisor::HalWrapper* PowerAdvisor::getPowerHal() { |
| 838 | static std::unique_ptr<HalWrapper> sHalWrapper = nullptr; |
Dan Stoza | 9c051c0 | 2020-02-28 10:19:07 -0800 | [diff] [blame] | 839 | static bool sHasHal = true; |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 840 | |
Dan Stoza | 9c051c0 | 2020-02-28 10:19:07 -0800 | [diff] [blame] | 841 | if (!sHasHal) { |
| 842 | return nullptr; |
| 843 | } |
| 844 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 845 | // Grab old hint session values before we destroy any existing wrapper |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 846 | std::vector<int32_t> oldPowerHintSessionThreadIds; |
| 847 | std::optional<int64_t> oldTargetWorkDuration; |
| 848 | |
| 849 | if (sHalWrapper != nullptr) { |
| 850 | oldPowerHintSessionThreadIds = sHalWrapper->getPowerHintSessionThreadIds(); |
| 851 | oldTargetWorkDuration = sHalWrapper->getTargetWorkDuration(); |
| 852 | } |
| 853 | |
Dan Stoza | 9c051c0 | 2020-02-28 10:19:07 -0800 | [diff] [blame] | 854 | // 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] | 855 | if (mReconnectPowerHal) { |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 856 | sHalWrapper = nullptr; |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 857 | mReconnectPowerHal = false; |
| 858 | } |
| 859 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 860 | if (sHalWrapper != nullptr) { |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 861 | auto wrapper = sHalWrapper.get(); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 862 | // If the wrapper is fine, return it, but if it indicates a reconnect, remake it |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 863 | if (!wrapper->shouldReconnectHAL()) { |
| 864 | return wrapper; |
| 865 | } |
Xiang Wang | 65a2e6f | 2022-04-18 21:19:17 +0000 | [diff] [blame] | 866 | ALOGD("Reconnecting Power HAL"); |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 867 | sHalWrapper = nullptr; |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 868 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 869 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 870 | // At this point, we know for sure there is no running session |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 871 | mPowerHintSessionRunning = false; |
| 872 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 873 | // First attempt to connect to the AIDL Power HAL |
| 874 | sHalWrapper = AidlPowerHalWrapper::connect(); |
| 875 | |
| 876 | // If that didn't succeed, attempt to connect to the HIDL Power HAL |
| 877 | if (sHalWrapper == nullptr) { |
| 878 | sHalWrapper = HidlPowerHalWrapper::connect(); |
Xiang Wang | 65a2e6f | 2022-04-18 21:19:17 +0000 | [diff] [blame] | 879 | } else { |
| 880 | ALOGD("Successfully connecting AIDL Power HAL"); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 881 | // If AIDL, pass on any existing hint session values |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 882 | sHalWrapper->setPowerHintSessionThreadIds(oldPowerHintSessionThreadIds); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 883 | // Only set duration and start if duration is defined |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 884 | if (oldTargetWorkDuration.has_value()) { |
| 885 | sHalWrapper->setTargetWorkDuration(*oldTargetWorkDuration); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 886 | // Only start if possible to run and both threadids and duration are defined |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 887 | if (usePowerHintSession() && !oldPowerHintSessionThreadIds.empty()) { |
| 888 | mPowerHintSessionRunning = sHalWrapper->startPowerHintSession(); |
| 889 | } |
| 890 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 891 | } |
| 892 | |
Dan Stoza | 9c051c0 | 2020-02-28 10:19:07 -0800 | [diff] [blame] | 893 | // If we make it to this point and still don't have a HAL, it's unlikely we |
| 894 | // will, so stop trying |
| 895 | if (sHalWrapper == nullptr) { |
| 896 | sHasHal = false; |
| 897 | } |
| 898 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 899 | return sHalWrapper.get(); |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | } // namespace impl |
| 903 | } // namespace Hwc2 |
| 904 | } // namespace android |