Michael Wright | 5d22d4f | 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 | |
| 17 | #pragma once |
| 18 | |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 19 | #include <atomic> |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 20 | #include <chrono> |
Peiyong Lin | 74ca2f4 | 2019-01-14 19:36:57 -0800 | [diff] [blame] | 21 | #include <unordered_set> |
| 22 | |
Dan Stoza | 2095000 | 2020-06-18 14:56:58 -0700 | [diff] [blame] | 23 | #include <utils/Mutex.h> |
| 24 | |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 25 | #include <android/hardware/power/IPower.h> |
Alec Mouri | ff79387 | 2022-01-13 17:45:06 -0800 | [diff] [blame] | 26 | #include <ui/DisplayIdentification.h> |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 27 | #include "../Scheduler/OneShotTimer.h" |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 28 | |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 29 | using namespace std::chrono_literals; |
| 30 | |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 31 | namespace android { |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 32 | |
| 33 | class SurfaceFlinger; |
| 34 | |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 35 | namespace Hwc2 { |
| 36 | |
| 37 | class PowerAdvisor { |
| 38 | public: |
| 39 | virtual ~PowerAdvisor(); |
| 40 | |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 41 | // Initializes resources that cannot be initialized on construction |
| 42 | virtual void init() = 0; |
Dan Stoza | 29e7bdf | 2020-03-23 14:43:09 -0700 | [diff] [blame] | 43 | virtual void onBootFinished() = 0; |
Peiyong Lin | 74ca2f4 | 2019-01-14 19:36:57 -0800 | [diff] [blame] | 44 | virtual void setExpensiveRenderingExpected(DisplayId displayId, bool expected) = 0; |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 45 | virtual bool isUsingExpensiveRendering() = 0; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 46 | virtual void notifyDisplayUpdateImminent() = 0; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 47 | virtual bool usePowerHintSession() = 0; |
| 48 | virtual bool supportsPowerHintSession() = 0; |
| 49 | virtual bool isPowerHintSessionRunning() = 0; |
| 50 | virtual void setTargetWorkDuration(int64_t targetDurationNanos) = 0; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 51 | virtual void sendActualWorkDuration(int64_t actualDurationNanos, nsecs_t timestamp) = 0; |
| 52 | virtual void enablePowerHint(bool enabled) = 0; |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 53 | virtual bool startPowerHintSession(const std::vector<int32_t>& threadIds) = 0; |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | namespace impl { |
| 57 | |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 58 | // PowerAdvisor is a wrapper around IPower HAL which takes into account the |
| 59 | // full state of the system when sending out power hints to things like the GPU. |
| 60 | class PowerAdvisor final : public Hwc2::PowerAdvisor { |
| 61 | public: |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 62 | class HalWrapper { |
| 63 | public: |
| 64 | virtual ~HalWrapper() = default; |
| 65 | |
| 66 | virtual bool setExpensiveRendering(bool enabled) = 0; |
| 67 | virtual bool notifyDisplayUpdateImminent() = 0; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 68 | virtual bool supportsPowerHintSession() = 0; |
| 69 | virtual bool isPowerHintSessionRunning() = 0; |
| 70 | virtual void restartPowerHintSession() = 0; |
| 71 | virtual void setPowerHintSessionThreadIds(const std::vector<int32_t>& threadIds) = 0; |
| 72 | virtual bool startPowerHintSession() = 0; |
| 73 | virtual void setTargetWorkDuration(int64_t targetDurationNanos) = 0; |
| 74 | virtual void sendActualWorkDuration(int64_t actualDurationNanos, |
| 75 | nsecs_t timeStampNanos) = 0; |
| 76 | virtual bool shouldReconnectHAL() = 0; |
| 77 | virtual std::vector<int32_t> getPowerHintSessionThreadIds() = 0; |
| 78 | virtual std::optional<int64_t> getTargetWorkDuration() = 0; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 81 | PowerAdvisor(SurfaceFlinger& flinger); |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 82 | ~PowerAdvisor() override; |
| 83 | |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 84 | void init() override; |
Dan Stoza | 29e7bdf | 2020-03-23 14:43:09 -0700 | [diff] [blame] | 85 | void onBootFinished() override; |
Peiyong Lin | 74ca2f4 | 2019-01-14 19:36:57 -0800 | [diff] [blame] | 86 | void setExpensiveRenderingExpected(DisplayId displayId, bool expected) override; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 87 | bool isUsingExpensiveRendering() override { return mNotifiedExpensiveRendering; }; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 88 | void notifyDisplayUpdateImminent() override; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 89 | bool usePowerHintSession() override; |
| 90 | bool supportsPowerHintSession() override; |
| 91 | bool isPowerHintSessionRunning() override; |
| 92 | void setTargetWorkDuration(int64_t targetDurationNanos) override; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 93 | void sendActualWorkDuration(int64_t actualDurationNanos, nsecs_t timestamp) override; |
| 94 | void enablePowerHint(bool enabled) override; |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 95 | bool startPowerHintSession(const std::vector<int32_t>& threadIds) override; |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 96 | |
| 97 | private: |
Dan Stoza | 2095000 | 2020-06-18 14:56:58 -0700 | [diff] [blame] | 98 | HalWrapper* getPowerHal() REQUIRES(mPowerHalMutex); |
| 99 | bool mReconnectPowerHal GUARDED_BY(mPowerHalMutex) = false; |
| 100 | std::mutex mPowerHalMutex; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 101 | |
Dan Stoza | 29e7bdf | 2020-03-23 14:43:09 -0700 | [diff] [blame] | 102 | std::atomic_bool mBootFinished = false; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 103 | std::optional<bool> mPowerHintEnabled; |
| 104 | std::optional<bool> mSupportsPowerHint; |
| 105 | bool mPowerHintSessionRunning = false; |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 106 | |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 107 | // An adjustable safety margin which moves the "target" earlier to allow flinger to |
| 108 | // go a bit over without dropping a frame, especially since we can't measure |
| 109 | // the exact time HWC finishes composition so "actual" durations are measured |
| 110 | // from the end of present() instead, which is a bit later. |
| 111 | static constexpr const std::chrono::nanoseconds kTargetSafetyMargin = 2ms; |
| 112 | |
Peiyong Lin | 74ca2f4 | 2019-01-14 19:36:57 -0800 | [diff] [blame] | 113 | std::unordered_set<DisplayId> mExpensiveDisplays; |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 114 | bool mNotifiedExpensiveRendering = false; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 115 | |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 116 | SurfaceFlinger& mFlinger; |
| 117 | const bool mUseScreenUpdateTimer; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 118 | std::atomic_bool mSendUpdateImminent = true; |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 119 | scheduler::OneShotTimer mScreenUpdateTimer; |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 122 | class AidlPowerHalWrapper : public PowerAdvisor::HalWrapper { |
| 123 | public: |
| 124 | explicit AidlPowerHalWrapper(sp<hardware::power::IPower> powerHal); |
| 125 | ~AidlPowerHalWrapper() override; |
| 126 | |
| 127 | static std::unique_ptr<HalWrapper> connect(); |
| 128 | |
| 129 | bool setExpensiveRendering(bool enabled) override; |
| 130 | bool notifyDisplayUpdateImminent() override; |
| 131 | bool supportsPowerHintSession() override; |
| 132 | bool isPowerHintSessionRunning() override; |
| 133 | void restartPowerHintSession() override; |
| 134 | void setPowerHintSessionThreadIds(const std::vector<int32_t>& threadIds) override; |
| 135 | bool startPowerHintSession() override; |
| 136 | void setTargetWorkDuration(int64_t targetDurationNanos) override; |
| 137 | void sendActualWorkDuration(int64_t actualDurationNanos, nsecs_t timeStampNanos) override; |
| 138 | bool shouldReconnectHAL() override; |
| 139 | std::vector<int32_t> getPowerHintSessionThreadIds() override; |
| 140 | std::optional<int64_t> getTargetWorkDuration() override; |
| 141 | |
| 142 | private: |
| 143 | bool checkPowerHintSessionSupported(); |
| 144 | void closePowerHintSession(); |
| 145 | bool shouldReportActualDurationsNow(); |
| 146 | bool shouldSetTargetDuration(int64_t targetDurationNanos); |
| 147 | |
| 148 | const sp<hardware::power::IPower> mPowerHal = nullptr; |
| 149 | bool mHasExpensiveRendering = false; |
| 150 | bool mHasDisplayUpdateImminent = false; |
| 151 | // Used to indicate an error state and need for reconstruction |
| 152 | bool mShouldReconnectHal = false; |
| 153 | // This is not thread safe, but is currently protected by mPowerHalMutex so it needs no lock |
| 154 | sp<hardware::power::IPowerHintSession> mPowerHintSession = nullptr; |
| 155 | // Queue of actual durations saved to report |
| 156 | std::vector<hardware::power::WorkDuration> mPowerHintQueue; |
| 157 | // The latest un-normalized values we have received for target and actual |
| 158 | int64_t mTargetDuration = kDefaultTarget.count(); |
| 159 | std::optional<int64_t> mActualDuration; |
| 160 | // The list of thread ids, stored so we can restart the session from this class if needed |
| 161 | std::vector<int32_t> mPowerHintThreadIds; |
| 162 | bool mSupportsPowerHint; |
| 163 | // Keep track of the last messages sent for rate limiter change detection |
| 164 | std::optional<int64_t> mLastActualDurationSent; |
| 165 | // timestamp of the last report we sent, used to avoid stale sessions |
| 166 | int64_t mLastActualReportTimestamp = 0; |
| 167 | int64_t mLastTargetDurationSent = kDefaultTarget.count(); |
| 168 | // Whether to normalize all the actual values as error terms relative to a constant target |
| 169 | // This saves a binder call by not setting the target, and should not affect the pid values |
| 170 | static const bool sNormalizeTarget; |
| 171 | // Whether we should emit ATRACE_INT data for hint sessions |
| 172 | static const bool sTraceHintSessionData; |
| 173 | |
| 174 | // Max percent the actual duration can vary without causing a report (eg: 0.1 = 10%) |
| 175 | static constexpr double kAllowedActualDeviationPercent = 0.1; |
Xiang Wang | 0aba49e | 2022-04-06 16:13:59 +0000 | [diff] [blame] | 176 | // Max percent the target duration can vary without causing a report (eg: 0.1 = 10%) |
| 177 | static constexpr double kAllowedTargetDeviationPercent = 0.1; |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 178 | // Target used for init and normalization, the actual value does not really matter |
| 179 | static constexpr const std::chrono::nanoseconds kDefaultTarget = 50ms; |
| 180 | // Amount of time after the last message was sent before the session goes stale |
| 181 | // actually 100ms but we use 80 here to ideally avoid going stale |
| 182 | static constexpr const std::chrono::nanoseconds kStaleTimeout = 80ms; |
| 183 | }; |
| 184 | |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 185 | } // namespace impl |
| 186 | } // namespace Hwc2 |
| 187 | } // namespace android |