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