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