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 <binder/IServiceManager.h> |
| 35 | |
| 36 | #include "../SurfaceFlingerProperties.h" |
| 37 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 38 | #include "PowerAdvisor.h" |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 39 | #include "SurfaceFlinger.h" |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 40 | |
| 41 | namespace android { |
| 42 | namespace Hwc2 { |
| 43 | |
| 44 | PowerAdvisor::~PowerAdvisor() = default; |
| 45 | |
| 46 | namespace impl { |
| 47 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 48 | using aidl::android::hardware::power::Boost; |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 49 | using aidl::android::hardware::power::Mode; |
| 50 | using aidl::android::hardware::power::SessionHint; |
| 51 | using aidl::android::hardware::power::WorkDuration; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 52 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 53 | PowerAdvisor::~PowerAdvisor() = default; |
| 54 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 55 | namespace { |
Alec Mouri | 29382ad | 2022-05-11 18:38:38 +0000 | [diff] [blame] | 56 | std::chrono::milliseconds getUpdateTimeout() { |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 57 | // Default to a timeout of 80ms if nothing else is specified |
Alec Mouri | 29382ad | 2022-05-11 18:38:38 +0000 | [diff] [blame] | 58 | static std::chrono::milliseconds timeout = |
| 59 | std::chrono::milliseconds(sysprop::display_update_imminent_timeout_ms(80)); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 60 | return timeout; |
| 61 | } |
| 62 | |
Ady Abraham | abce165 | 2022-02-24 10:51:19 -0800 | [diff] [blame] | 63 | void traceExpensiveRendering(bool enabled) { |
| 64 | if (enabled) { |
| 65 | ATRACE_ASYNC_BEGIN("ExpensiveRendering", 0); |
| 66 | } else { |
| 67 | ATRACE_ASYNC_END("ExpensiveRendering", 0); |
| 68 | } |
| 69 | } |
| 70 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 71 | } // namespace |
| 72 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 73 | PowerAdvisor::PowerAdvisor(SurfaceFlinger& flinger) |
| 74 | : mPowerHal(std::make_unique<power::PowerHalController>()), mFlinger(flinger) { |
Alec Mouri | 29382ad | 2022-05-11 18:38:38 +0000 | [diff] [blame] | 75 | if (getUpdateTimeout() > 0ms) { |
| 76 | mScreenUpdateTimer.emplace("UpdateImminentTimer", getUpdateTimeout(), |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 77 | /* resetCallback */ nullptr, |
| 78 | /* timeoutCallback */ |
| 79 | [this] { |
Alec Mouri | 29382ad | 2022-05-11 18:38:38 +0000 | [diff] [blame] | 80 | while (true) { |
| 81 | auto timeSinceLastUpdate = std::chrono::nanoseconds( |
| 82 | systemTime() - mLastScreenUpdatedTime.load()); |
| 83 | if (timeSinceLastUpdate >= getUpdateTimeout()) { |
| 84 | break; |
| 85 | } |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 86 | // We may try to disable expensive rendering and allow |
| 87 | // for sending DISPLAY_UPDATE_IMMINENT hints too early if |
| 88 | // we idled very shortly after updating the screen, so |
| 89 | // make sure we wait enough time. |
Alec Mouri | 29382ad | 2022-05-11 18:38:38 +0000 | [diff] [blame] | 90 | std::this_thread::sleep_for(getUpdateTimeout() - |
| 91 | timeSinceLastUpdate); |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 92 | } |
| 93 | mSendUpdateImminent.store(true); |
| 94 | mFlinger.disableExpensiveRendering(); |
| 95 | }); |
| 96 | } |
| 97 | } |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 98 | |
| 99 | void PowerAdvisor::init() { |
| 100 | // Defer starting the screen update timer until SurfaceFlinger finishes construction. |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 101 | if (mScreenUpdateTimer) { |
| 102 | mScreenUpdateTimer->start(); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 105 | |
Dan Stoza | 29e7bdf | 2020-03-23 14:43:09 -0700 | [diff] [blame] | 106 | void PowerAdvisor::onBootFinished() { |
| 107 | mBootFinished.store(true); |
| 108 | } |
| 109 | |
Peiyong Lin | 74ca2f4 | 2019-01-14 19:36:57 -0800 | [diff] [blame] | 110 | void PowerAdvisor::setExpensiveRenderingExpected(DisplayId displayId, bool expected) { |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 111 | if (!mHasExpensiveRendering) { |
| 112 | ALOGV("Skipped sending EXPENSIVE_RENDERING because HAL doesn't support it"); |
| 113 | return; |
| 114 | } |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 115 | if (expected) { |
| 116 | mExpensiveDisplays.insert(displayId); |
| 117 | } else { |
| 118 | mExpensiveDisplays.erase(displayId); |
| 119 | } |
| 120 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 121 | const bool expectsExpensiveRendering = !mExpensiveDisplays.empty(); |
| 122 | if (mNotifiedExpensiveRendering != expectsExpensiveRendering) { |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 123 | auto ret = getPowerHal().setMode(Mode::EXPENSIVE_RENDERING, expectsExpensiveRendering); |
| 124 | if (!ret.isOk()) { |
| 125 | if (ret.isUnsupported()) { |
| 126 | mHasExpensiveRendering = false; |
| 127 | } |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 128 | return; |
| 129 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 130 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 131 | mNotifiedExpensiveRendering = expectsExpensiveRendering; |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 132 | traceExpensiveRendering(mNotifiedExpensiveRendering); |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
jimmyshiu | 4e21177 | 2023-06-15 15:18:38 +0000 | [diff] [blame] | 136 | void PowerAdvisor::notifyCpuLoadUp() { |
| 137 | // Only start sending this notification once the system has booted so we don't introduce an |
| 138 | // early-boot dependency on Power HAL |
| 139 | if (!mBootFinished.load()) { |
| 140 | return; |
| 141 | } |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 142 | if (usePowerHintSession()) { |
jimmyshiu | 4e21177 | 2023-06-15 15:18:38 +0000 | [diff] [blame] | 143 | std::lock_guard lock(mHintSessionMutex); |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 144 | if (ensurePowerHintSessionRunning()) { |
| 145 | auto ret = mHintSession->sendHint(SessionHint::CPU_LOAD_UP); |
| 146 | if (!ret.isOk()) { |
| 147 | mHintSession = nullptr; |
| 148 | } |
jimmyshiu | 4e21177 | 2023-06-15 15:18:38 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
Matt Buckley | 15ecd1c | 2022-11-01 21:57:16 +0000 | [diff] [blame] | 153 | void PowerAdvisor::notifyDisplayUpdateImminentAndCpuReset() { |
Dan Stoza | 29e7bdf | 2020-03-23 14:43:09 -0700 | [diff] [blame] | 154 | // Only start sending this notification once the system has booted so we don't introduce an |
| 155 | // early-boot dependency on Power HAL |
| 156 | if (!mBootFinished.load()) { |
| 157 | return; |
| 158 | } |
| 159 | |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 160 | if (mSendUpdateImminent.exchange(false)) { |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 161 | ALOGV("AIDL notifyDisplayUpdateImminentAndCpuReset"); |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 162 | if (usePowerHintSession()) { |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 163 | std::lock_guard lock(mHintSessionMutex); |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 164 | if (ensurePowerHintSessionRunning()) { |
| 165 | auto ret = mHintSession->sendHint(SessionHint::CPU_LOAD_RESET); |
| 166 | if (!ret.isOk()) { |
| 167 | mHintSession = nullptr; |
| 168 | } |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 169 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 172 | if (!mHasDisplayUpdateImminent) { |
| 173 | ALOGV("Skipped sending DISPLAY_UPDATE_IMMINENT because HAL doesn't support it"); |
| 174 | } else { |
| 175 | auto ret = getPowerHal().setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 0); |
| 176 | if (ret.isUnsupported()) { |
| 177 | mHasDisplayUpdateImminent = false; |
| 178 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 179 | } |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 180 | |
| 181 | if (mScreenUpdateTimer) { |
| 182 | mScreenUpdateTimer->reset(); |
| 183 | } else { |
| 184 | // If we don't have a screen update timer, then we don't throttle power hal calls so |
| 185 | // flip this bit back to allow for calling into power hal again. |
| 186 | mSendUpdateImminent.store(true); |
| 187 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 190 | if (mScreenUpdateTimer) { |
| 191 | mLastScreenUpdatedTime.store(systemTime()); |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 195 | bool PowerAdvisor::usePowerHintSession() { |
| 196 | // uses cached value since the underlying support and flag are unlikely to change at runtime |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 197 | return mHintSessionEnabled.value_or(false) && supportsPowerHintSession(); |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | bool PowerAdvisor::supportsPowerHintSession() { |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 201 | if (!mSupportsHintSession.has_value()) { |
| 202 | mSupportsHintSession = getPowerHal().getHintSessionPreferredRate().isOk(); |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 203 | } |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 204 | return *mSupportsHintSession; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 207 | bool PowerAdvisor::ensurePowerHintSessionRunning() { |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 208 | if (mHintSession == nullptr && !mHintSessionThreadIds.empty() && usePowerHintSession()) { |
Peter Kalauskas | 48cca92 | 2024-02-08 18:24:51 +0000 | [diff] [blame] | 209 | auto ret = getPowerHal().createHintSession(getpid(), static_cast<int32_t>(getuid()), |
| 210 | mHintSessionThreadIds, mTargetDuration.ns()); |
| 211 | |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 212 | if (ret.isOk()) { |
| 213 | mHintSession = ret.value(); |
| 214 | } |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 215 | } |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 216 | return mHintSession != nullptr; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 219 | void PowerAdvisor::updateTargetWorkDuration(Duration targetDuration) { |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 220 | if (!usePowerHintSession()) { |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 221 | ALOGV("Power hint session target duration cannot be set, skipping"); |
| 222 | return; |
| 223 | } |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 224 | ATRACE_CALL(); |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 225 | { |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 226 | mTargetDuration = targetDuration; |
| 227 | if (sTraceHintSessionData) ATRACE_INT64("Time target", targetDuration.ns()); |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 228 | if (targetDuration == mLastTargetDurationSent) return; |
| 229 | std::lock_guard lock(mHintSessionMutex); |
| 230 | if (ensurePowerHintSessionRunning()) { |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 231 | ALOGV("Sending target time: %" PRId64 "ns", targetDuration.ns()); |
| 232 | mLastTargetDurationSent = targetDuration; |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 233 | auto ret = mHintSession->updateTargetWorkDuration(targetDuration.ns()); |
| 234 | if (!ret.isOk()) { |
| 235 | ALOGW("Failed to set power hint target work duration with error: %s", |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame] | 236 | ret.errorMessage()); |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 237 | mHintSession = nullptr; |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 238 | } |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 243 | void PowerAdvisor::reportActualWorkDuration() { |
Matt Buckley | 676e439 | 2023-05-25 22:09:26 +0000 | [diff] [blame] | 244 | if (!mBootFinished || !sUseReportActualDuration || !usePowerHintSession()) { |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 245 | ALOGV("Actual work duration power hint cannot be sent, skipping"); |
| 246 | return; |
| 247 | } |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 248 | ATRACE_CALL(); |
| 249 | std::optional<Duration> actualDuration = estimateWorkDuration(); |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 250 | if (!actualDuration.has_value() || actualDuration < 0ns) { |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 251 | ALOGV("Failed to send actual work duration, skipping"); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 252 | return; |
| 253 | } |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 254 | actualDuration = std::make_optional(*actualDuration + sTargetSafetyMargin); |
| 255 | mActualDuration = actualDuration; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 256 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 257 | if (sTraceHintSessionData) { |
| 258 | ATRACE_INT64("Measured duration", actualDuration->ns()); |
| 259 | ATRACE_INT64("Target error term", Duration{*actualDuration - mTargetDuration}.ns()); |
| 260 | ATRACE_INT64("Reported duration", actualDuration->ns()); |
| 261 | ATRACE_INT64("Reported target", mLastTargetDurationSent.ns()); |
| 262 | ATRACE_INT64("Reported target error term", |
| 263 | Duration{*actualDuration - mLastTargetDurationSent}.ns()); |
| 264 | } |
| 265 | |
| 266 | ALOGV("Sending actual work duration of: %" PRId64 " on reported target: %" PRId64 |
| 267 | " with error: %" PRId64, |
| 268 | actualDuration->ns(), mLastTargetDurationSent.ns(), |
| 269 | Duration{*actualDuration - mLastTargetDurationSent}.ns()); |
| 270 | |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 271 | if (mTimingTestingMode) { |
| 272 | mDelayReportActualMutexAcquisitonPromise.get_future().wait(); |
| 273 | mDelayReportActualMutexAcquisitonPromise = std::promise<bool>{}; |
| 274 | } |
| 275 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 276 | { |
| 277 | std::lock_guard lock(mHintSessionMutex); |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 278 | if (!ensurePowerHintSessionRunning()) { |
| 279 | ALOGV("Hint session not running and could not be started, skipping"); |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | WorkDuration duration{ |
| 284 | .timeStampNanos = TimePoint::now().ns(), |
| 285 | // TODO(b/284324521): Correctly calculate total duration. |
| 286 | .durationNanos = actualDuration->ns(), |
| 287 | .workPeriodStartTimestampNanos = mCommitStartTimes[0].ns(), |
| 288 | .cpuDurationNanos = actualDuration->ns(), |
| 289 | // TODO(b/284324521): Calculate RenderEngine GPU time. |
| 290 | .gpuDurationNanos = 0, |
| 291 | }; |
| 292 | mHintSessionQueue.push_back(duration); |
| 293 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 294 | auto ret = mHintSession->reportActualWorkDuration(mHintSessionQueue); |
| 295 | if (!ret.isOk()) { |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame] | 296 | ALOGW("Failed to report actual work durations with error: %s", ret.errorMessage()); |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 297 | mHintSession = nullptr; |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 298 | return; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 301 | mHintSessionQueue.clear(); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 304 | void PowerAdvisor::enablePowerHintSession(bool enabled) { |
| 305 | mHintSessionEnabled = enabled; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 308 | bool PowerAdvisor::startPowerHintSession(std::vector<int32_t>&& threadIds) { |
| 309 | mHintSessionThreadIds = threadIds; |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 310 | if (!mBootFinished.load()) { |
| 311 | return false; |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 312 | } |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 313 | if (!usePowerHintSession()) { |
| 314 | ALOGI("Cannot start power hint session: disabled or unsupported"); |
| 315 | return false; |
| 316 | } |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 317 | LOG_ALWAYS_FATAL_IF(mHintSessionThreadIds.empty(), |
| 318 | "No thread IDs provided to power hint session!"); |
| 319 | std::lock_guard lock(mHintSessionMutex); |
| 320 | if (mHintSession != nullptr) { |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 321 | ALOGE("Cannot start power hint session: already running"); |
| 322 | return false; |
| 323 | } |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 324 | return ensurePowerHintSessionRunning(); |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 327 | void PowerAdvisor::setGpuFenceTime(DisplayId displayId, std::unique_ptr<FenceTime>&& fenceTime) { |
| 328 | DisplayTimingData& displayData = mDisplayTimingData[displayId]; |
| 329 | if (displayData.gpuEndFenceTime) { |
| 330 | nsecs_t signalTime = displayData.gpuEndFenceTime->getSignalTime(); |
| 331 | if (signalTime != Fence::SIGNAL_TIME_INVALID && signalTime != Fence::SIGNAL_TIME_PENDING) { |
| 332 | for (auto&& [_, otherDisplayData] : mDisplayTimingData) { |
| 333 | // If the previous display started before us but ended after we should have |
| 334 | // started, then it likely delayed our start time and we must compensate for that. |
| 335 | // Displays finishing earlier should have already made their way through this call |
| 336 | // and swapped their timing into "lastValid" from "latest", so we check that here. |
| 337 | if (!otherDisplayData.lastValidGpuStartTime.has_value()) continue; |
| 338 | if ((*otherDisplayData.lastValidGpuStartTime < *displayData.gpuStartTime) && |
| 339 | (*otherDisplayData.lastValidGpuEndTime > *displayData.gpuStartTime)) { |
| 340 | displayData.lastValidGpuStartTime = *otherDisplayData.lastValidGpuEndTime; |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | displayData.lastValidGpuStartTime = displayData.gpuStartTime; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 345 | displayData.lastValidGpuEndTime = TimePoint::fromNs(signalTime); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | displayData.gpuEndFenceTime = std::move(fenceTime); |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 349 | displayData.gpuStartTime = TimePoint::now(); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 352 | void PowerAdvisor::setHwcValidateTiming(DisplayId displayId, TimePoint validateStartTime, |
| 353 | TimePoint validateEndTime) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 354 | DisplayTimingData& displayData = mDisplayTimingData[displayId]; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 355 | displayData.hwcValidateStartTime = validateStartTime; |
| 356 | displayData.hwcValidateEndTime = validateEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 359 | void PowerAdvisor::setHwcPresentTiming(DisplayId displayId, TimePoint presentStartTime, |
| 360 | TimePoint presentEndTime) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 361 | DisplayTimingData& displayData = mDisplayTimingData[displayId]; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 362 | displayData.hwcPresentStartTime = presentStartTime; |
| 363 | displayData.hwcPresentEndTime = presentEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void PowerAdvisor::setSkippedValidate(DisplayId displayId, bool skipped) { |
| 367 | mDisplayTimingData[displayId].skippedValidate = skipped; |
| 368 | } |
| 369 | |
| 370 | void PowerAdvisor::setRequiresClientComposition(DisplayId displayId, |
| 371 | bool requiresClientComposition) { |
| 372 | mDisplayTimingData[displayId].usedClientComposition = requiresClientComposition; |
| 373 | } |
| 374 | |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 375 | void PowerAdvisor::setExpectedPresentTime(TimePoint expectedPresentTime) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 376 | mExpectedPresentTimes.append(expectedPresentTime); |
| 377 | } |
| 378 | |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 379 | void PowerAdvisor::setSfPresentTiming(TimePoint presentFenceTime, TimePoint presentEndTime) { |
Matt Buckley | 1809d90 | 2022-08-05 06:51:43 +0000 | [diff] [blame] | 380 | mLastSfPresentEndTime = presentEndTime; |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame] | 381 | mLastPresentFenceTime = presentFenceTime; |
| 382 | } |
| 383 | |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 384 | void PowerAdvisor::setFrameDelay(Duration frameDelayDuration) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 385 | mFrameDelayDuration = frameDelayDuration; |
| 386 | } |
| 387 | |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 388 | void PowerAdvisor::setHwcPresentDelayedTime(DisplayId displayId, TimePoint earliestFrameStartTime) { |
| 389 | mDisplayTimingData[displayId].hwcPresentDelayedTime = earliestFrameStartTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 392 | void PowerAdvisor::setCommitStart(TimePoint commitStartTime) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 393 | mCommitStartTimes.append(commitStartTime); |
| 394 | } |
| 395 | |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 396 | void PowerAdvisor::setCompositeEnd(TimePoint compositeEndTime) { |
| 397 | mLastPostcompDuration = compositeEndTime - mLastSfPresentEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void PowerAdvisor::setDisplays(std::vector<DisplayId>& displayIds) { |
| 401 | mDisplayIds = displayIds; |
| 402 | } |
| 403 | |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 404 | void PowerAdvisor::setTotalFrameTargetWorkDuration(Duration targetDuration) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 405 | mTotalFrameTargetDuration = targetDuration; |
| 406 | } |
| 407 | |
| 408 | std::vector<DisplayId> PowerAdvisor::getOrderedDisplayIds( |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 409 | std::optional<TimePoint> DisplayTimingData::*sortBy) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 410 | std::vector<DisplayId> sortedDisplays; |
| 411 | std::copy_if(mDisplayIds.begin(), mDisplayIds.end(), std::back_inserter(sortedDisplays), |
| 412 | [&](DisplayId id) { |
| 413 | return mDisplayTimingData.count(id) && |
| 414 | (mDisplayTimingData[id].*sortBy).has_value(); |
| 415 | }); |
| 416 | std::sort(sortedDisplays.begin(), sortedDisplays.end(), [&](DisplayId idA, DisplayId idB) { |
| 417 | return *(mDisplayTimingData[idA].*sortBy) < *(mDisplayTimingData[idB].*sortBy); |
| 418 | }); |
| 419 | return sortedDisplays; |
| 420 | } |
| 421 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 422 | std::optional<Duration> PowerAdvisor::estimateWorkDuration() { |
| 423 | if (!mExpectedPresentTimes.isFull() || !mCommitStartTimes.isFull()) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 424 | return std::nullopt; |
| 425 | } |
| 426 | |
| 427 | // Tracks when we finish presenting to hwc |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 428 | TimePoint estimatedHwcEndTime = mCommitStartTimes[0]; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 429 | |
| 430 | // How long we spent this frame not doing anything, waiting for fences or vsync |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 431 | Duration idleDuration = 0ns; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 432 | |
| 433 | // Most recent previous gpu end time in the current frame, probably from a prior display, used |
| 434 | // as the start time for the next gpu operation if it ran over time since it probably blocked |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 435 | std::optional<TimePoint> previousValidGpuEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 436 | |
| 437 | // The currently estimated gpu end time for the frame, |
| 438 | // used to accumulate gpu time as we iterate over the active displays |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 439 | std::optional<TimePoint> estimatedGpuEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 440 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 441 | // The timing info for the previously calculated display, if there was one |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 442 | std::optional<DisplayTimeline> previousDisplayTiming; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 443 | std::vector<DisplayId>&& displayIds = |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 444 | getOrderedDisplayIds(&DisplayTimingData::hwcPresentStartTime); |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 445 | DisplayTimeline displayTiming; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 446 | |
Matt Buckley | 1809d90 | 2022-08-05 06:51:43 +0000 | [diff] [blame] | 447 | // Iterate over the displays that use hwc in the same order they are presented |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 448 | for (DisplayId displayId : displayIds) { |
| 449 | if (mDisplayTimingData.count(displayId) == 0) { |
| 450 | continue; |
| 451 | } |
| 452 | |
| 453 | auto& displayData = mDisplayTimingData.at(displayId); |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame] | 454 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 455 | displayTiming = displayData.calculateDisplayTimeline(mLastPresentFenceTime); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 456 | |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 457 | // If this is the first display, include the duration before hwc present starts |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 458 | if (!previousDisplayTiming.has_value()) { |
| 459 | estimatedHwcEndTime += displayTiming.hwcPresentStartTime - mCommitStartTimes[0]; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 460 | } else { // Otherwise add the time since last display's hwc present finished |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 461 | estimatedHwcEndTime += |
| 462 | displayTiming.hwcPresentStartTime - previousDisplayTiming->hwcPresentEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 465 | // Update predicted present finish time with this display's present time |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 466 | estimatedHwcEndTime = displayTiming.hwcPresentEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 467 | |
| 468 | // Track how long we spent waiting for the fence, can be excluded from the timing estimate |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 469 | idleDuration += displayTiming.probablyWaitsForPresentFence |
| 470 | ? mLastPresentFenceTime - displayTiming.presentFenceWaitStartTime |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 471 | : 0ns; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 472 | |
| 473 | // Track how long we spent waiting to present, can be excluded from the timing estimate |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 474 | idleDuration += displayTiming.hwcPresentDelayDuration; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 475 | |
| 476 | // Estimate the reference frame's gpu timing |
| 477 | auto gpuTiming = displayData.estimateGpuTiming(previousValidGpuEndTime); |
| 478 | if (gpuTiming.has_value()) { |
| 479 | previousValidGpuEndTime = gpuTiming->startTime + gpuTiming->duration; |
| 480 | |
| 481 | // Estimate the prediction frame's gpu end time from the reference frame |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 482 | estimatedGpuEndTime = std::max(displayTiming.hwcPresentStartTime, |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 483 | estimatedGpuEndTime.value_or(TimePoint{0ns})) + |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 484 | gpuTiming->duration; |
| 485 | } |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 486 | previousDisplayTiming = displayTiming; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 487 | } |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 488 | ATRACE_INT64("Idle duration", idleDuration.ns()); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 489 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 490 | TimePoint estimatedFlingerEndTime = mLastSfPresentEndTime; |
Matt Buckley | 1809d90 | 2022-08-05 06:51:43 +0000 | [diff] [blame] | 491 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 492 | // Don't count time spent idly waiting in the estimate as we could do more work in that time |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 493 | estimatedHwcEndTime -= idleDuration; |
Matt Buckley | 1809d90 | 2022-08-05 06:51:43 +0000 | [diff] [blame] | 494 | estimatedFlingerEndTime -= idleDuration; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 495 | |
| 496 | // We finish the frame when both present and the gpu are done, so wait for the later of the two |
| 497 | // Also add the frame delay duration since the target did not move while we were delayed |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 498 | Duration totalDuration = mFrameDelayDuration + |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 499 | std::max(estimatedHwcEndTime, estimatedGpuEndTime.value_or(TimePoint{0ns})) - |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 500 | mCommitStartTimes[0]; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 501 | |
| 502 | // We finish SurfaceFlinger when post-composition finishes, so add that in here |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 503 | Duration flingerDuration = |
Matt Buckley | 1809d90 | 2022-08-05 06:51:43 +0000 | [diff] [blame] | 504 | estimatedFlingerEndTime + mLastPostcompDuration - mCommitStartTimes[0]; |
| 505 | |
| 506 | // Combine the two timings into a single normalized one |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 507 | Duration combinedDuration = combineTimingEstimates(totalDuration, flingerDuration); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 508 | |
| 509 | return std::make_optional(combinedDuration); |
| 510 | } |
| 511 | |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 512 | Duration PowerAdvisor::combineTimingEstimates(Duration totalDuration, Duration flingerDuration) { |
| 513 | Duration targetDuration{0ns}; |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 514 | targetDuration = mTargetDuration; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 515 | if (!mTotalFrameTargetDuration.has_value()) return flingerDuration; |
| 516 | |
| 517 | // Normalize total to the flinger target (vsync period) since that's how often we actually send |
| 518 | // hints |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 519 | Duration normalizedTotalDuration = Duration::fromNs((targetDuration.ns() * totalDuration.ns()) / |
| 520 | mTotalFrameTargetDuration->ns()); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 521 | return std::max(flingerDuration, normalizedTotalDuration); |
| 522 | } |
| 523 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 524 | PowerAdvisor::DisplayTimeline PowerAdvisor::DisplayTimingData::calculateDisplayTimeline( |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 525 | TimePoint fenceTime) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 526 | DisplayTimeline timeline; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 527 | // How long between calling hwc present and trying to wait on the fence |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 528 | const Duration fenceWaitStartDelay = |
| 529 | (skippedValidate ? kFenceWaitStartDelaySkippedValidate : kFenceWaitStartDelayValidated); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 530 | |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 531 | // Did our reference frame wait for an appropriate vsync before calling into hwc |
| 532 | const bool waitedOnHwcPresentTime = hwcPresentDelayedTime.has_value() && |
| 533 | *hwcPresentDelayedTime > *hwcPresentStartTime && |
| 534 | *hwcPresentDelayedTime < *hwcPresentEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 535 | |
| 536 | // 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] | 537 | timeline.hwcPresentStartTime = skippedValidate ? *hwcValidateStartTime : *hwcPresentStartTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 538 | |
| 539 | // 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] | 540 | timeline.hwcPresentEndTime = skippedValidate ? *hwcValidateEndTime : *hwcPresentEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 541 | |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 542 | // How long hwc present was delayed waiting for the next appropriate vsync |
| 543 | timeline.hwcPresentDelayDuration = |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 544 | (waitedOnHwcPresentTime ? *hwcPresentDelayedTime - *hwcPresentStartTime : 0ns); |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame] | 545 | // When we started waiting for the present fence after calling into hwc present |
| 546 | timeline.presentFenceWaitStartTime = |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 547 | timeline.hwcPresentStartTime + timeline.hwcPresentDelayDuration + fenceWaitStartDelay; |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame] | 548 | timeline.probablyWaitsForPresentFence = fenceTime > timeline.presentFenceWaitStartTime && |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 549 | fenceTime < timeline.hwcPresentEndTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 550 | |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 551 | // 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] | 552 | timeline.postPresentFenceHwcPresentDuration = timeline.hwcPresentEndTime - |
| 553 | (timeline.probablyWaitsForPresentFence ? fenceTime |
| 554 | : timeline.presentFenceWaitStartTime); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 555 | return timeline; |
| 556 | } |
| 557 | |
| 558 | std::optional<PowerAdvisor::GpuTimeline> PowerAdvisor::DisplayTimingData::estimateGpuTiming( |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 559 | std::optional<TimePoint> previousEndTime) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 560 | if (!(usedClientComposition && lastValidGpuStartTime.has_value() && gpuEndFenceTime)) { |
| 561 | return std::nullopt; |
| 562 | } |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 563 | const TimePoint latestGpuStartTime = |
| 564 | std::max(previousEndTime.value_or(TimePoint{0ns}), *gpuStartTime); |
| 565 | const nsecs_t gpuEndFenceSignal = gpuEndFenceTime->getSignalTime(); |
| 566 | Duration gpuDuration{0ns}; |
| 567 | if (gpuEndFenceSignal != Fence::SIGNAL_TIME_INVALID && |
| 568 | gpuEndFenceSignal != Fence::SIGNAL_TIME_PENDING) { |
| 569 | const TimePoint latestGpuEndTime = TimePoint::fromNs(gpuEndFenceSignal); |
| 570 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 571 | // If we know how long the most recent gpu duration was, use that |
| 572 | gpuDuration = latestGpuEndTime - latestGpuStartTime; |
| 573 | } else if (lastValidGpuEndTime.has_value()) { |
| 574 | // If we don't have the fence data, use the most recent information we do have |
| 575 | gpuDuration = *lastValidGpuEndTime - *lastValidGpuStartTime; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 576 | if (gpuEndFenceSignal == Fence::SIGNAL_TIME_PENDING) { |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 577 | // If pending but went over the previous duration, use current time as the end |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 578 | gpuDuration = std::max(gpuDuration, Duration{TimePoint::now() - latestGpuStartTime}); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | return GpuTimeline{.duration = gpuDuration, .startTime = latestGpuStartTime}; |
| 582 | } |
| 583 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 584 | const bool PowerAdvisor::sTraceHintSessionData = |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 585 | base::GetBoolProperty(std::string("debug.sf.trace_hint_sessions"), false); |
| 586 | |
Matt Buckley | ac15a1b | 2023-02-28 06:51:28 +0000 | [diff] [blame] | 587 | const Duration PowerAdvisor::sTargetSafetyMargin = std::chrono::microseconds( |
| 588 | base::GetIntProperty<int64_t>("debug.sf.hint_margin_us", |
| 589 | ticks<std::micro>(PowerAdvisor::kDefaultTargetSafetyMargin))); |
| 590 | |
Matt Buckley | 676e439 | 2023-05-25 22:09:26 +0000 | [diff] [blame] | 591 | const bool PowerAdvisor::sUseReportActualDuration = |
| 592 | base::GetBoolProperty(std::string("debug.adpf.use_report_actual_duration"), true); |
| 593 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 594 | power::PowerHalController& PowerAdvisor::getPowerHal() { |
| 595 | static std::once_flag halFlag; |
| 596 | std::call_once(halFlag, [this] { mPowerHal->init(); }); |
| 597 | return *mPowerHal; |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | } // namespace impl |
| 601 | } // namespace Hwc2 |
| 602 | } // namespace android |