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> |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 21 | #include <unordered_map> |
Peiyong Lin | 74ca2f4 | 2019-01-14 19:36:57 -0800 | [diff] [blame] | 22 | #include <unordered_set> |
| 23 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 24 | #include <ui/DisplayId.h> |
| 25 | #include <ui/FenceTime.h> |
Dan Stoza | 2095000 | 2020-06-18 14:56:58 -0700 | [diff] [blame] | 26 | #include <utils/Mutex.h> |
| 27 | |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 28 | #include <android/hardware/power/IPower.h> |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 29 | #include <compositionengine/impl/OutputCompositionState.h> |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 30 | #include <scheduler/Time.h> |
Alec Mouri | ff79387 | 2022-01-13 17:45:06 -0800 | [diff] [blame] | 31 | #include <ui/DisplayIdentification.h> |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 32 | #include "../Scheduler/OneShotTimer.h" |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 33 | |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 34 | using namespace std::chrono_literals; |
| 35 | |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 36 | namespace android { |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 37 | |
| 38 | class SurfaceFlinger; |
| 39 | |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 40 | namespace Hwc2 { |
| 41 | |
| 42 | class PowerAdvisor { |
| 43 | public: |
| 44 | virtual ~PowerAdvisor(); |
| 45 | |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 46 | // Initializes resources that cannot be initialized on construction |
| 47 | virtual void init() = 0; |
Dan Stoza | 29e7bdf | 2020-03-23 14:43:09 -0700 | [diff] [blame] | 48 | virtual void onBootFinished() = 0; |
Peiyong Lin | 74ca2f4 | 2019-01-14 19:36:57 -0800 | [diff] [blame] | 49 | virtual void setExpensiveRenderingExpected(DisplayId displayId, bool expected) = 0; |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 50 | virtual bool isUsingExpensiveRendering() = 0; |
Matt Buckley | 15ecd1c | 2022-11-01 21:57:16 +0000 | [diff] [blame] | 51 | virtual void notifyDisplayUpdateImminentAndCpuReset() = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 52 | // Checks both if it supports and if it's enabled |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 53 | virtual bool usePowerHintSession() = 0; |
| 54 | virtual bool supportsPowerHintSession() = 0; |
| 55 | virtual bool isPowerHintSessionRunning() = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 56 | // Sends a power hint that updates to the target work duration for the frame |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 57 | virtual void setTargetWorkDuration(Duration targetDuration) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 58 | // Sends a power hint for the actual known work duration at the end of the frame |
| 59 | virtual void sendActualWorkDuration() = 0; |
| 60 | // Sends a power hint for the upcoming frame predicted from previous frame timing |
| 61 | virtual void sendPredictedWorkDuration() = 0; |
| 62 | // Sets whether the power hint session is enabled |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 63 | virtual void enablePowerHint(bool enabled) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 64 | // Initializes the power hint session |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 65 | virtual bool startPowerHintSession(const std::vector<int32_t>& threadIds) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 66 | // Provides PowerAdvisor with a copy of the gpu fence so it can determine the gpu end time |
| 67 | virtual void setGpuFenceTime(DisplayId displayId, std::unique_ptr<FenceTime>&& fenceTime) = 0; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 68 | // Reports the start and end times of a hwc validate call this frame for a given display |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 69 | virtual void setHwcValidateTiming(DisplayId displayId, TimePoint validateStartTime, |
| 70 | TimePoint validateEndTime) = 0; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 71 | // Reports the start and end times of a hwc present call this frame for a given display |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 72 | virtual void setHwcPresentTiming(DisplayId displayId, TimePoint presentStartTime, |
| 73 | TimePoint presentEndTime) = 0; |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame] | 74 | // Reports the expected time that the current frame will present to the display |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 75 | virtual void setExpectedPresentTime(TimePoint expectedPresentTime) = 0; |
Matt Buckley | 1809d90 | 2022-08-05 06:51:43 +0000 | [diff] [blame] | 76 | // Reports the most recent present fence time and end time once known |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 77 | virtual void setSfPresentTiming(TimePoint presentFenceTime, TimePoint presentEndTime) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 78 | // Reports whether a display used client composition this frame |
| 79 | virtual void setRequiresClientComposition(DisplayId displayId, |
| 80 | bool requiresClientComposition) = 0; |
| 81 | // Reports whether a given display skipped validation this frame |
| 82 | virtual void setSkippedValidate(DisplayId displayId, bool skipped) = 0; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 83 | // Reports when a hwc present is delayed, and the time that it will resume |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 84 | virtual void setHwcPresentDelayedTime(DisplayId displayId, |
| 85 | TimePoint earliestFrameStartTime) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 86 | // Reports the start delay for SurfaceFlinger this frame |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 87 | virtual void setFrameDelay(Duration frameDelayDuration) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 88 | // Reports the SurfaceFlinger commit start time this frame |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 89 | virtual void setCommitStart(TimePoint commitStartTime) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 90 | // Reports the SurfaceFlinger composite end time this frame |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 91 | virtual void setCompositeEnd(TimePoint compositeEndTime) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 92 | // Reports the list of the currently active displays |
| 93 | virtual void setDisplays(std::vector<DisplayId>& displayIds) = 0; |
| 94 | // Sets the target duration for the entire pipeline including the gpu |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 95 | virtual void setTotalFrameTargetWorkDuration(Duration targetDuration) = 0; |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | namespace impl { |
| 99 | |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 100 | // PowerAdvisor is a wrapper around IPower HAL which takes into account the |
| 101 | // full state of the system when sending out power hints to things like the GPU. |
| 102 | class PowerAdvisor final : public Hwc2::PowerAdvisor { |
| 103 | public: |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 104 | class HalWrapper { |
| 105 | public: |
| 106 | virtual ~HalWrapper() = default; |
| 107 | |
| 108 | virtual bool setExpensiveRendering(bool enabled) = 0; |
Matt Buckley | 15ecd1c | 2022-11-01 21:57:16 +0000 | [diff] [blame] | 109 | virtual bool notifyDisplayUpdateImminentAndCpuReset() = 0; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 110 | virtual bool supportsPowerHintSession() = 0; |
| 111 | virtual bool isPowerHintSessionRunning() = 0; |
| 112 | virtual void restartPowerHintSession() = 0; |
| 113 | virtual void setPowerHintSessionThreadIds(const std::vector<int32_t>& threadIds) = 0; |
| 114 | virtual bool startPowerHintSession() = 0; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 115 | virtual void setTargetWorkDuration(Duration targetDuration) = 0; |
| 116 | virtual void sendActualWorkDuration(Duration actualDuration, TimePoint timestamp) = 0; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 117 | virtual bool shouldReconnectHAL() = 0; |
| 118 | virtual std::vector<int32_t> getPowerHintSessionThreadIds() = 0; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 119 | virtual std::optional<Duration> getTargetWorkDuration() = 0; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 120 | }; |
| 121 | |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 122 | PowerAdvisor(SurfaceFlinger& flinger); |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 123 | ~PowerAdvisor() override; |
| 124 | |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 125 | void init() override; |
Dan Stoza | 29e7bdf | 2020-03-23 14:43:09 -0700 | [diff] [blame] | 126 | void onBootFinished() override; |
Peiyong Lin | 74ca2f4 | 2019-01-14 19:36:57 -0800 | [diff] [blame] | 127 | void setExpensiveRenderingExpected(DisplayId displayId, bool expected) override; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 128 | bool isUsingExpensiveRendering() override { return mNotifiedExpensiveRendering; }; |
Matt Buckley | 15ecd1c | 2022-11-01 21:57:16 +0000 | [diff] [blame] | 129 | void notifyDisplayUpdateImminentAndCpuReset() override; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 130 | bool usePowerHintSession() override; |
| 131 | bool supportsPowerHintSession() override; |
| 132 | bool isPowerHintSessionRunning() override; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 133 | void setTargetWorkDuration(Duration targetDuration) override; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 134 | void sendActualWorkDuration() override; |
| 135 | void sendPredictedWorkDuration() override; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 136 | void enablePowerHint(bool enabled) override; |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 137 | bool startPowerHintSession(const std::vector<int32_t>& threadIds) override; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 138 | void setGpuFenceTime(DisplayId displayId, std::unique_ptr<FenceTime>&& fenceTime); |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 139 | void setHwcValidateTiming(DisplayId displayId, TimePoint validateStartTime, |
| 140 | TimePoint validateEndTime) override; |
| 141 | void setHwcPresentTiming(DisplayId displayId, TimePoint presentStartTime, |
| 142 | TimePoint presentEndTime) override; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 143 | void setSkippedValidate(DisplayId displayId, bool skipped) override; |
| 144 | void setRequiresClientComposition(DisplayId displayId, bool requiresClientComposition) override; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 145 | void setExpectedPresentTime(TimePoint expectedPresentTime) override; |
| 146 | void setSfPresentTiming(TimePoint presentFenceTime, TimePoint presentEndTime) override; |
| 147 | void setHwcPresentDelayedTime(DisplayId displayId, TimePoint earliestFrameStartTime) override; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 148 | |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 149 | void setFrameDelay(Duration frameDelayDuration) override; |
| 150 | void setCommitStart(TimePoint commitStartTime) override; |
| 151 | void setCompositeEnd(TimePoint compositeEndTime) override; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 152 | void setDisplays(std::vector<DisplayId>& displayIds) override; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 153 | void setTotalFrameTargetWorkDuration(Duration targetDuration) override; |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 154 | |
| 155 | private: |
Matt Buckley | 5727405 | 2022-08-12 21:54:23 +0000 | [diff] [blame] | 156 | friend class PowerAdvisorTest; |
| 157 | |
| 158 | // Tracks if powerhal exists |
| 159 | bool mHasHal = true; |
| 160 | // Holds the hal wrapper for getPowerHal |
| 161 | std::unique_ptr<HalWrapper> mHalWrapper GUARDED_BY(mPowerHalMutex) = nullptr; |
| 162 | |
Dan Stoza | 2095000 | 2020-06-18 14:56:58 -0700 | [diff] [blame] | 163 | HalWrapper* getPowerHal() REQUIRES(mPowerHalMutex); |
| 164 | bool mReconnectPowerHal GUARDED_BY(mPowerHalMutex) = false; |
| 165 | std::mutex mPowerHalMutex; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 166 | |
Dan Stoza | 29e7bdf | 2020-03-23 14:43:09 -0700 | [diff] [blame] | 167 | std::atomic_bool mBootFinished = false; |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 168 | |
Peiyong Lin | 74ca2f4 | 2019-01-14 19:36:57 -0800 | [diff] [blame] | 169 | std::unordered_set<DisplayId> mExpensiveDisplays; |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 170 | bool mNotifiedExpensiveRendering = false; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 171 | |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 172 | SurfaceFlinger& mFlinger; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 173 | std::atomic_bool mSendUpdateImminent = true; |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 174 | std::atomic<nsecs_t> mLastScreenUpdatedTime = 0; |
| 175 | std::optional<scheduler::OneShotTimer> mScreenUpdateTimer; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 176 | |
| 177 | // Higher-level timing data used for estimation |
| 178 | struct DisplayTimeline { |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 179 | // The start of hwc present, or the start of validate if it happened there instead |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 180 | TimePoint hwcPresentStartTime; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 181 | // The end of hwc present or validate, whichever one actually presented |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 182 | TimePoint hwcPresentEndTime; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 183 | // How long the actual hwc present was delayed after hwcPresentStartTime |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 184 | Duration hwcPresentDelayDuration{0ns}; |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame] | 185 | // When we think we started waiting for the present fence after calling into hwc present and |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 186 | // after potentially waiting for the earliest present time |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 187 | TimePoint presentFenceWaitStartTime; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 188 | // How long we ran after we finished waiting for the fence but before hwc present finished |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 189 | Duration postPresentFenceHwcPresentDuration{0ns}; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 190 | // Are we likely to have waited for the present fence during composition |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame] | 191 | bool probablyWaitsForPresentFence = false; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 192 | // Estimate one frame's timeline from that of a previous frame |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 193 | DisplayTimeline estimateTimelineFromReference(TimePoint fenceTime, |
| 194 | TimePoint displayStartTime); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | struct GpuTimeline { |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 198 | Duration duration{0ns}; |
| 199 | TimePoint startTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | // Power hint session data recorded from the pipeline |
| 203 | struct DisplayTimingData { |
| 204 | std::unique_ptr<FenceTime> gpuEndFenceTime; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 205 | std::optional<TimePoint> gpuStartTime; |
| 206 | std::optional<TimePoint> lastValidGpuEndTime; |
| 207 | std::optional<TimePoint> lastValidGpuStartTime; |
| 208 | std::optional<TimePoint> hwcPresentStartTime; |
| 209 | std::optional<TimePoint> hwcPresentEndTime; |
| 210 | std::optional<TimePoint> hwcValidateStartTime; |
| 211 | std::optional<TimePoint> hwcValidateEndTime; |
| 212 | std::optional<TimePoint> hwcPresentDelayedTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 213 | bool usedClientComposition = false; |
| 214 | bool skippedValidate = false; |
| 215 | // Calculate high-level timing milestones from more granular display timing data |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 216 | DisplayTimeline calculateDisplayTimeline(TimePoint fenceTime); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 217 | // Estimate the gpu duration for a given display from previous gpu timing data |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 218 | std::optional<GpuTimeline> estimateGpuTiming(std::optional<TimePoint> previousEndTime); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | template <class T, size_t N> |
| 222 | class RingBuffer { |
| 223 | std::array<T, N> elements = {}; |
| 224 | size_t mIndex = 0; |
| 225 | size_t numElements = 0; |
| 226 | |
| 227 | public: |
| 228 | void append(T item) { |
| 229 | mIndex = (mIndex + 1) % N; |
| 230 | numElements = std::min(N, numElements + 1); |
| 231 | elements[mIndex] = item; |
| 232 | } |
| 233 | bool isFull() const { return numElements == N; } |
| 234 | // Allows access like [0] == current, [-1] = previous, etc.. |
| 235 | T& operator[](int offset) { |
| 236 | size_t positiveOffset = |
| 237 | static_cast<size_t>((offset % static_cast<int>(N)) + static_cast<int>(N)); |
| 238 | return elements[(mIndex + positiveOffset) % N]; |
| 239 | } |
| 240 | }; |
| 241 | |
| 242 | // Filter and sort the display ids by a given property |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 243 | std::vector<DisplayId> getOrderedDisplayIds( |
| 244 | std::optional<TimePoint> DisplayTimingData::*sortBy); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 245 | // Estimates a frame's total work duration including gpu time. |
| 246 | // Runs either at the beginning or end of a frame, using the most recent data available |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 247 | std::optional<Duration> estimateWorkDuration(bool earlyHint); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 248 | // There are two different targets and actual work durations we care about, |
| 249 | // this normalizes them together and takes the max of the two |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 250 | Duration combineTimingEstimates(Duration totalDuration, Duration flingerDuration); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 251 | |
| 252 | std::unordered_map<DisplayId, DisplayTimingData> mDisplayTimingData; |
| 253 | |
| 254 | // Current frame's delay |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 255 | Duration mFrameDelayDuration{0ns}; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 256 | // Last frame's post-composition duration |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 257 | Duration mLastPostcompDuration{0ns}; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 258 | // Buffer of recent commit start times |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 259 | RingBuffer<TimePoint, 2> mCommitStartTimes; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 260 | // Buffer of recent expected present times |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 261 | RingBuffer<TimePoint, 2> mExpectedPresentTimes; |
Matt Buckley | a2ad1dc | 2022-08-16 20:07:26 +0000 | [diff] [blame] | 262 | // Most recent present fence time, provided by SF after composition engine finishes presenting |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 263 | TimePoint mLastPresentFenceTime; |
Matt Buckley | a2ad1dc | 2022-08-16 20:07:26 +0000 | [diff] [blame] | 264 | // Most recent composition engine present end time, returned with the present fence from SF |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 265 | TimePoint mLastSfPresentEndTime; |
Matt Buckley | a2ad1dc | 2022-08-16 20:07:26 +0000 | [diff] [blame] | 266 | // Target duration for the entire pipeline including gpu |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 267 | std::optional<Duration> mTotalFrameTargetDuration; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 268 | // Updated list of display IDs |
| 269 | std::vector<DisplayId> mDisplayIds; |
| 270 | |
| 271 | std::optional<bool> mPowerHintEnabled; |
| 272 | std::optional<bool> mSupportsPowerHint; |
| 273 | bool mPowerHintSessionRunning = false; |
| 274 | |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame] | 275 | // An adjustable safety margin which pads the "actual" value sent to PowerHAL, |
| 276 | // encouraging more aggressive boosting to give SurfaceFlinger a larger margin for error |
Matt Buckley | ac15a1b | 2023-02-28 06:51:28 +0000 | [diff] [blame^] | 277 | static const Duration sTargetSafetyMargin; |
| 278 | static constexpr const Duration kDefaultTargetSafetyMargin{1ms}; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 279 | |
| 280 | // How long we expect hwc to run after the present call until it waits for the fence |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 281 | static constexpr const Duration kFenceWaitStartDelayValidated{150us}; |
| 282 | static constexpr const Duration kFenceWaitStartDelaySkippedValidate{250us}; |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 283 | }; |
| 284 | |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 285 | class AidlPowerHalWrapper : public PowerAdvisor::HalWrapper { |
| 286 | public: |
| 287 | explicit AidlPowerHalWrapper(sp<hardware::power::IPower> powerHal); |
| 288 | ~AidlPowerHalWrapper() override; |
| 289 | |
| 290 | static std::unique_ptr<HalWrapper> connect(); |
| 291 | |
| 292 | bool setExpensiveRendering(bool enabled) override; |
Matt Buckley | 15ecd1c | 2022-11-01 21:57:16 +0000 | [diff] [blame] | 293 | bool notifyDisplayUpdateImminentAndCpuReset() override; |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 294 | bool supportsPowerHintSession() override; |
| 295 | bool isPowerHintSessionRunning() override; |
| 296 | void restartPowerHintSession() override; |
| 297 | void setPowerHintSessionThreadIds(const std::vector<int32_t>& threadIds) override; |
| 298 | bool startPowerHintSession() override; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 299 | void setTargetWorkDuration(Duration targetDuration) override; |
| 300 | void sendActualWorkDuration(Duration actualDuration, TimePoint timestamp) override; |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 301 | bool shouldReconnectHAL() override; |
| 302 | std::vector<int32_t> getPowerHintSessionThreadIds() override; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 303 | std::optional<Duration> getTargetWorkDuration() override; |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 304 | |
| 305 | private: |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 306 | friend class AidlPowerHalWrapperTest; |
| 307 | |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 308 | bool checkPowerHintSessionSupported(); |
| 309 | void closePowerHintSession(); |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 310 | |
| 311 | const sp<hardware::power::IPower> mPowerHal = nullptr; |
| 312 | bool mHasExpensiveRendering = false; |
| 313 | bool mHasDisplayUpdateImminent = false; |
| 314 | // Used to indicate an error state and need for reconstruction |
| 315 | bool mShouldReconnectHal = false; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 316 | |
| 317 | // Power hint session data |
| 318 | |
| 319 | // Concurrent access for this is protected by mPowerHalMutex |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 320 | sp<hardware::power::IPowerHintSession> mPowerHintSession = nullptr; |
| 321 | // Queue of actual durations saved to report |
| 322 | std::vector<hardware::power::WorkDuration> mPowerHintQueue; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 323 | // The latest values we have received for target and actual |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 324 | Duration mTargetDuration = kDefaultTargetDuration; |
| 325 | std::optional<Duration> mActualDuration; |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 326 | // The list of thread ids, stored so we can restart the session from this class if needed |
| 327 | std::vector<int32_t> mPowerHintThreadIds; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 328 | bool mSupportsPowerHint = false; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 329 | Duration mLastTargetDurationSent = kDefaultTargetDuration; |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 330 | // Whether we should emit ATRACE_INT data for hint sessions |
| 331 | static const bool sTraceHintSessionData; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 332 | static constexpr Duration kDefaultTargetDuration{16ms}; |
Xiang Wang | e12b4fa | 2022-03-25 23:48:40 +0000 | [diff] [blame] | 333 | }; |
| 334 | |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 335 | } // namespace impl |
| 336 | } // namespace Hwc2 |
| 337 | } // namespace android |