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