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 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 19 | #undef LOG_TAG |
| 20 | #define LOG_TAG "PowerAdvisor" |
| 21 | |
| 22 | #include <cinttypes> |
| 23 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame^] | 24 | #include <android-base/properties.h> |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | #include <utils/Mutex.h> |
| 27 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame^] | 28 | #include <android/hardware/power/1.3/IPower.h> |
| 29 | #include <android/hardware/power/IPower.h> |
| 30 | #include <binder/IServiceManager.h> |
| 31 | |
| 32 | #include "../SurfaceFlingerProperties.h" |
| 33 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 34 | #include "PowerAdvisor.h" |
| 35 | |
| 36 | namespace android { |
| 37 | namespace Hwc2 { |
| 38 | |
| 39 | PowerAdvisor::~PowerAdvisor() = default; |
| 40 | |
| 41 | namespace impl { |
| 42 | |
| 43 | namespace V1_0 = android::hardware::power::V1_0; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame^] | 44 | namespace V1_3 = android::hardware::power::V1_3; |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 45 | using V1_3::PowerHint; |
| 46 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame^] | 47 | using android::hardware::power::Boost; |
| 48 | using android::hardware::power::IPower; |
| 49 | using android::hardware::power::Mode; |
| 50 | using base::GetIntProperty; |
| 51 | using scheduler::OneShotTimer; |
| 52 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 53 | PowerAdvisor::~PowerAdvisor() = default; |
| 54 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame^] | 55 | namespace { |
| 56 | int32_t getUpdateTimeout() { |
| 57 | // Default to a timeout of 80ms if nothing else is specified |
| 58 | static int32_t timeout = sysprop::display_update_imminent_timeout_ms(80); |
| 59 | return timeout; |
| 60 | } |
| 61 | |
| 62 | } // namespace |
| 63 | |
| 64 | PowerAdvisor::PowerAdvisor() |
| 65 | : mUseUpdateImminentTimer(getUpdateTimeout() > 0), |
| 66 | mUpdateImminentTimer( |
| 67 | OneShotTimer::Interval(getUpdateTimeout()), |
| 68 | /* resetCallback */ [this] { mSendUpdateImminent.store(false); }, |
| 69 | /* timeoutCallback */ [this] { mSendUpdateImminent.store(true); }) { |
| 70 | if (mUseUpdateImminentTimer) { |
| 71 | mUpdateImminentTimer.start(); |
| 72 | } |
| 73 | } |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 74 | |
Peiyong Lin | 74ca2f4 | 2019-01-14 19:36:57 -0800 | [diff] [blame] | 75 | void PowerAdvisor::setExpensiveRenderingExpected(DisplayId displayId, bool expected) { |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 76 | if (expected) { |
| 77 | mExpensiveDisplays.insert(displayId); |
| 78 | } else { |
| 79 | mExpensiveDisplays.erase(displayId); |
| 80 | } |
| 81 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 82 | const bool expectsExpensiveRendering = !mExpensiveDisplays.empty(); |
| 83 | if (mNotifiedExpensiveRendering != expectsExpensiveRendering) { |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame^] | 84 | HalWrapper* const halWrapper = getPowerHal(); |
| 85 | if (halWrapper == nullptr) { |
Peiyong Lin | 8193497 | 2018-07-02 11:00:54 -0700 | [diff] [blame] | 86 | return; |
| 87 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame^] | 88 | |
| 89 | if (!halWrapper->setExpensiveRendering(expectsExpensiveRendering)) { |
| 90 | // The HAL has become unavailable; attempt to reconnect later |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 91 | mReconnectPowerHal = true; |
| 92 | return; |
| 93 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame^] | 94 | |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 95 | mNotifiedExpensiveRendering = expectsExpensiveRendering; |
| 96 | } |
| 97 | } |
| 98 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame^] | 99 | void PowerAdvisor::notifyDisplayUpdateImminent() { |
| 100 | if (mSendUpdateImminent.load()) { |
| 101 | HalWrapper* const halWrapper = getPowerHal(); |
| 102 | if (halWrapper == nullptr) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | if (!halWrapper->notifyDisplayUpdateImminent()) { |
| 107 | // The HAL has become unavailable; attempt to reconnect later |
| 108 | mReconnectPowerHal = true; |
| 109 | return; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (mUseUpdateImminentTimer) { |
| 114 | mUpdateImminentTimer.reset(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | class HidlPowerHalWrapper : public PowerAdvisor::HalWrapper { |
| 119 | public: |
| 120 | HidlPowerHalWrapper(sp<V1_3::IPower> powerHal) : mPowerHal(std::move(powerHal)) {} |
| 121 | |
| 122 | ~HidlPowerHalWrapper() override = default; |
| 123 | |
| 124 | static std::unique_ptr<HalWrapper> connect() { |
| 125 | // Power HAL 1.3 is not guaranteed to be available, thus we need to query |
| 126 | // Power HAL 1.0 first and try to cast it to Power HAL 1.3. |
| 127 | // Power HAL 1.0 is always available, thus if we fail to query it, it means |
| 128 | // Power HAL is not available temporarily and we should retry later. However, |
| 129 | // if Power HAL 1.0 is available and we can't cast it to Power HAL 1.3, |
| 130 | // it means Power HAL 1.3 is not available at all, so we should stop trying. |
| 131 | sp<V1_3::IPower> powerHal = nullptr; |
| 132 | if (sHasPowerHal_1_3) { |
| 133 | sp<V1_0::IPower> powerHal_1_0 = V1_0::IPower::getService(); |
| 134 | if (powerHal_1_0 != nullptr) { |
| 135 | // Try to cast to Power HAL 1.3 |
| 136 | powerHal = V1_3::IPower::castFrom(powerHal_1_0); |
| 137 | if (powerHal == nullptr) { |
| 138 | ALOGW("No Power HAL 1.3 service in system"); |
| 139 | sHasPowerHal_1_3 = false; |
| 140 | } else { |
| 141 | ALOGI("Loaded Power HAL 1.3 service"); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | if (powerHal == nullptr) { |
| 146 | return nullptr; |
| 147 | } |
| 148 | |
| 149 | return std::make_unique<HidlPowerHalWrapper>(std::move(powerHal)); |
| 150 | } |
| 151 | |
| 152 | bool setExpensiveRendering(bool enabled) override { |
| 153 | ALOGV("HIDL setExpensiveRendering %s", enabled ? "T" : "F"); |
| 154 | auto ret = mPowerHal->powerHintAsync_1_3(PowerHint::EXPENSIVE_RENDERING, enabled); |
| 155 | return ret.isOk(); |
| 156 | } |
| 157 | |
| 158 | bool notifyDisplayUpdateImminent() override { |
| 159 | // Power HAL 1.x doesn't have a notification for this |
| 160 | ALOGV("HIDL notifyUpdateImminent received but can't send"); |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | private: |
| 165 | static bool sHasPowerHal_1_3; |
| 166 | const sp<V1_3::IPower> mPowerHal = nullptr; |
| 167 | }; |
| 168 | |
| 169 | bool HidlPowerHalWrapper::sHasPowerHal_1_3 = true; |
| 170 | |
| 171 | class AidlPowerHalWrapper : public PowerAdvisor::HalWrapper { |
| 172 | public: |
| 173 | AidlPowerHalWrapper(sp<IPower> powerHal) : mPowerHal(std::move(powerHal)) { |
| 174 | auto ret = mPowerHal->isModeSupported(Mode::EXPENSIVE_RENDERING, &mHasExpensiveRendering); |
| 175 | if (!ret.isOk()) { |
| 176 | mHasExpensiveRendering = false; |
| 177 | } |
| 178 | |
| 179 | ret = mPowerHal->isBoostSupported(Boost::DISPLAY_UPDATE_IMMINENT, |
| 180 | &mHasDisplayUpdateImminent); |
| 181 | if (!ret.isOk()) { |
| 182 | mHasDisplayUpdateImminent = false; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | ~AidlPowerHalWrapper() override = default; |
| 187 | |
| 188 | static std::unique_ptr<HalWrapper> connect() { |
| 189 | // This only waits if the service is actually declared |
| 190 | sp<IPower> powerHal = waitForVintfService<IPower>(); |
| 191 | if (powerHal == nullptr) { |
| 192 | return nullptr; |
| 193 | } |
| 194 | ALOGI("Loaded AIDL Power HAL service"); |
| 195 | |
| 196 | return std::make_unique<AidlPowerHalWrapper>(std::move(powerHal)); |
| 197 | } |
| 198 | |
| 199 | bool setExpensiveRendering(bool enabled) override { |
| 200 | ALOGV("AIDL setExpensiveRendering %s", enabled ? "T" : "F"); |
| 201 | if (!mHasExpensiveRendering) { |
| 202 | ALOGV("Skipped sending EXPENSIVE_RENDERING because HAL doesn't support it"); |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | auto ret = mPowerHal->setMode(Mode::EXPENSIVE_RENDERING, enabled); |
| 207 | return ret.isOk(); |
| 208 | } |
| 209 | |
| 210 | bool notifyDisplayUpdateImminent() override { |
| 211 | ALOGV("AIDL notifyDisplayUpdateImminent"); |
| 212 | if (!mHasDisplayUpdateImminent) { |
| 213 | ALOGV("Skipped sending DISPLAY_UPDATE_IMMINENT because HAL doesn't support it"); |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | auto ret = mPowerHal->setBoost(Boost::DISPLAY_UPDATE_IMMINENT, 0); |
| 218 | return ret.isOk(); |
| 219 | } |
| 220 | |
| 221 | private: |
| 222 | const sp<IPower> mPowerHal = nullptr; |
| 223 | bool mHasExpensiveRendering = false; |
| 224 | bool mHasDisplayUpdateImminent = false; |
| 225 | }; |
| 226 | |
| 227 | PowerAdvisor::HalWrapper* PowerAdvisor::getPowerHal() { |
| 228 | static std::unique_ptr<HalWrapper> sHalWrapper = nullptr; |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 229 | |
| 230 | if (mReconnectPowerHal) { |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame^] | 231 | sHalWrapper = nullptr; |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 232 | mReconnectPowerHal = false; |
| 233 | } |
| 234 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame^] | 235 | if (sHalWrapper != nullptr) { |
| 236 | return sHalWrapper.get(); |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 237 | } |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame^] | 238 | |
| 239 | // First attempt to connect to the AIDL Power HAL |
| 240 | sHalWrapper = AidlPowerHalWrapper::connect(); |
| 241 | |
| 242 | // If that didn't succeed, attempt to connect to the HIDL Power HAL |
| 243 | if (sHalWrapper == nullptr) { |
| 244 | sHalWrapper = HidlPowerHalWrapper::connect(); |
| 245 | } |
| 246 | |
| 247 | return sHalWrapper.get(); |
Michael Wright | 1509a23 | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | } // namespace impl |
| 251 | } // namespace Hwc2 |
| 252 | } // namespace android |