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 | |
Matt Buckley | 104f53a | 2023-12-14 00:19:20 +0000 | [diff] [blame] | 28 | // FMQ library in IPower does questionable conversions |
| 29 | #pragma clang diagnostic push |
| 30 | #pragma clang diagnostic ignored "-Wconversion" |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 31 | #include <aidl/android/hardware/power/IPower.h> |
Xiang Wang | 154fc04 | 2024-04-02 14:17:53 -0700 | [diff] [blame] | 32 | #include <fmq/AidlMessageQueue.h> |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 33 | #include <powermanager/PowerHalController.h> |
Matt Buckley | 104f53a | 2023-12-14 00:19:20 +0000 | [diff] [blame] | 34 | #pragma clang diagnostic pop |
| 35 | |
| 36 | #include <compositionengine/impl/OutputCompositionState.h> |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 37 | #include <scheduler/Time.h> |
Alec Mouri | ff79387 | 2022-01-13 17:45:06 -0800 | [diff] [blame] | 38 | #include <ui/DisplayIdentification.h> |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 39 | #include "../Scheduler/OneShotTimer.h" |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 40 | |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 41 | using namespace std::chrono_literals; |
| 42 | |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 43 | namespace android { |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 44 | |
| 45 | class SurfaceFlinger; |
| 46 | |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 47 | namespace Hwc2 { |
| 48 | |
| 49 | class PowerAdvisor { |
| 50 | public: |
| 51 | virtual ~PowerAdvisor(); |
| 52 | |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 53 | // Initializes resources that cannot be initialized on construction |
| 54 | virtual void init() = 0; |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 55 | // Used to indicate that power hints can now be reported |
Dan Stoza | 29e7bdf | 2020-03-23 14:43:09 -0700 | [diff] [blame] | 56 | virtual void onBootFinished() = 0; |
Peiyong Lin | 74ca2f4 | 2019-01-14 19:36:57 -0800 | [diff] [blame] | 57 | virtual void setExpensiveRenderingExpected(DisplayId displayId, bool expected) = 0; |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 58 | virtual bool isUsingExpensiveRendering() = 0; |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 59 | // Checks both if it's supported and if it's enabled; this is thread-safe since its values are |
| 60 | // set before onBootFinished, which gates all methods that run on threads other than SF main |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 61 | virtual bool usePowerHintSession() = 0; |
| 62 | virtual bool supportsPowerHintSession() = 0; |
Xiang Wang | cb50bbd | 2024-04-18 16:57:54 -0700 | [diff] [blame] | 63 | virtual bool supportsGpuReporting() = 0; |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 64 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 65 | // Sends a power hint that updates to the target work duration for the frame |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 66 | virtual void updateTargetWorkDuration(Duration targetDuration) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 67 | // Sends a power hint for the actual known work duration at the end of the frame |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 68 | virtual void reportActualWorkDuration() = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 69 | // Sets whether the power hint session is enabled |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 70 | virtual void enablePowerHintSession(bool enabled) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 71 | // Initializes the power hint session |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 72 | virtual bool startPowerHintSession(std::vector<int32_t>&& threadIds) = 0; |
Xiang Wang | aab3116 | 2024-03-12 19:48:08 -0700 | [diff] [blame] | 73 | // Provides PowerAdvisor with gpu start time |
| 74 | virtual void setGpuStartTime(DisplayId displayId, TimePoint startTime) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 75 | // Provides PowerAdvisor with a copy of the gpu fence so it can determine the gpu end time |
| 76 | virtual void setGpuFenceTime(DisplayId displayId, std::unique_ptr<FenceTime>&& fenceTime) = 0; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 77 | // 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] | 78 | virtual void setHwcValidateTiming(DisplayId displayId, TimePoint validateStartTime, |
| 79 | TimePoint validateEndTime) = 0; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 80 | // 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] | 81 | virtual void setHwcPresentTiming(DisplayId displayId, TimePoint presentStartTime, |
| 82 | TimePoint presentEndTime) = 0; |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame] | 83 | // 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] | 84 | virtual void setExpectedPresentTime(TimePoint expectedPresentTime) = 0; |
Matt Buckley | 1809d90 | 2022-08-05 06:51:43 +0000 | [diff] [blame] | 85 | // Reports the most recent present fence time and end time once known |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 86 | virtual void setSfPresentTiming(TimePoint presentFenceTime, TimePoint presentEndTime) = 0; |
Xiang Wang | aab3116 | 2024-03-12 19:48:08 -0700 | [diff] [blame] | 87 | // Reports whether a display requires RenderEngine to draw |
| 88 | virtual void setRequiresRenderEngine(DisplayId displayId, bool requiresRenderEngine) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 89 | // Reports whether a given display skipped validation this frame |
| 90 | virtual void setSkippedValidate(DisplayId displayId, bool skipped) = 0; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 91 | // 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] | 92 | virtual void setHwcPresentDelayedTime(DisplayId displayId, |
| 93 | TimePoint earliestFrameStartTime) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 94 | // Reports the start delay for SurfaceFlinger this frame |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 95 | virtual void setFrameDelay(Duration frameDelayDuration) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 96 | // Reports the SurfaceFlinger commit start time this frame |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 97 | virtual void setCommitStart(TimePoint commitStartTime) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 98 | // Reports the SurfaceFlinger composite end time this frame |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 99 | virtual void setCompositeEnd(TimePoint compositeEndTime) = 0; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 100 | // Reports the list of the currently active displays |
| 101 | virtual void setDisplays(std::vector<DisplayId>& displayIds) = 0; |
| 102 | // Sets the target duration for the entire pipeline including the gpu |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 103 | virtual void setTotalFrameTargetWorkDuration(Duration targetDuration) = 0; |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 104 | |
| 105 | // --- The following methods may run on threads besides SF main --- |
| 106 | // Send a hint about an upcoming increase in the CPU workload |
| 107 | virtual void notifyCpuLoadUp() = 0; |
| 108 | // Send a hint about the imminent start of a new CPU workload |
| 109 | virtual void notifyDisplayUpdateImminentAndCpuReset() = 0; |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | namespace impl { |
| 113 | |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 114 | // PowerAdvisor is a wrapper around IPower HAL which takes into account the |
| 115 | // full state of the system when sending out power hints to things like the GPU. |
| 116 | class PowerAdvisor final : public Hwc2::PowerAdvisor { |
| 117 | public: |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 118 | PowerAdvisor(SurfaceFlinger& flinger); |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 119 | ~PowerAdvisor() override; |
| 120 | |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 121 | void init() override; |
Dan Stoza | 29e7bdf | 2020-03-23 14:43:09 -0700 | [diff] [blame] | 122 | void onBootFinished() override; |
Peiyong Lin | 74ca2f4 | 2019-01-14 19:36:57 -0800 | [diff] [blame] | 123 | void setExpensiveRenderingExpected(DisplayId displayId, bool expected) override; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 124 | bool isUsingExpensiveRendering() override { return mNotifiedExpensiveRendering; }; |
Matt Buckley | 06f299a | 2021-09-24 19:43:51 +0000 | [diff] [blame] | 125 | bool usePowerHintSession() override; |
| 126 | bool supportsPowerHintSession() override; |
Xiang Wang | cb50bbd | 2024-04-18 16:57:54 -0700 | [diff] [blame] | 127 | bool supportsGpuReporting() override; |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 128 | void updateTargetWorkDuration(Duration targetDuration) override; |
| 129 | void reportActualWorkDuration() override; |
| 130 | void enablePowerHintSession(bool enabled) override; |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 131 | bool startPowerHintSession(std::vector<int32_t>&& threadIds) override; |
Xiang Wang | aab3116 | 2024-03-12 19:48:08 -0700 | [diff] [blame] | 132 | void setGpuStartTime(DisplayId displayId, TimePoint startTime) override; |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 133 | void setGpuFenceTime(DisplayId displayId, std::unique_ptr<FenceTime>&& fenceTime) override; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 134 | void setHwcValidateTiming(DisplayId displayId, TimePoint validateStartTime, |
| 135 | TimePoint validateEndTime) override; |
| 136 | void setHwcPresentTiming(DisplayId displayId, TimePoint presentStartTime, |
| 137 | TimePoint presentEndTime) override; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 138 | void setSkippedValidate(DisplayId displayId, bool skipped) override; |
Xiang Wang | aab3116 | 2024-03-12 19:48:08 -0700 | [diff] [blame] | 139 | void setRequiresRenderEngine(DisplayId displayId, bool requiresRenderEngine); |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 140 | void setExpectedPresentTime(TimePoint expectedPresentTime) override; |
| 141 | void setSfPresentTiming(TimePoint presentFenceTime, TimePoint presentEndTime) override; |
| 142 | void setHwcPresentDelayedTime(DisplayId displayId, TimePoint earliestFrameStartTime) override; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 143 | void setFrameDelay(Duration frameDelayDuration) override; |
| 144 | void setCommitStart(TimePoint commitStartTime) override; |
| 145 | void setCompositeEnd(TimePoint compositeEndTime) override; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 146 | void setDisplays(std::vector<DisplayId>& displayIds) override; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 147 | void setTotalFrameTargetWorkDuration(Duration targetDuration) override; |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 148 | |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 149 | // --- The following methods may run on threads besides SF main --- |
| 150 | void notifyCpuLoadUp() override; |
| 151 | void notifyDisplayUpdateImminentAndCpuReset() override; |
| 152 | |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 153 | private: |
Matt Buckley | 5727405 | 2022-08-12 21:54:23 +0000 | [diff] [blame] | 154 | friend class PowerAdvisorTest; |
| 155 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 156 | std::unique_ptr<power::PowerHalController> mPowerHal; |
Dan Stoza | 29e7bdf | 2020-03-23 14:43:09 -0700 | [diff] [blame] | 157 | std::atomic_bool mBootFinished = false; |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 158 | |
Peiyong Lin | 74ca2f4 | 2019-01-14 19:36:57 -0800 | [diff] [blame] | 159 | std::unordered_set<DisplayId> mExpensiveDisplays; |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 160 | bool mNotifiedExpensiveRendering = false; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 161 | |
Alec Mouri | dea1ac5 | 2021-06-23 18:12:18 -0700 | [diff] [blame] | 162 | SurfaceFlinger& mFlinger; |
Dan Stoza | 030fbc1 | 2020-02-19 15:32:01 -0800 | [diff] [blame] | 163 | std::atomic_bool mSendUpdateImminent = true; |
Alec Mouri | c059dcf | 2022-05-05 23:40:07 +0000 | [diff] [blame] | 164 | std::atomic<nsecs_t> mLastScreenUpdatedTime = 0; |
| 165 | std::optional<scheduler::OneShotTimer> mScreenUpdateTimer; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 166 | |
| 167 | // Higher-level timing data used for estimation |
| 168 | struct DisplayTimeline { |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 169 | // 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] | 170 | TimePoint hwcPresentStartTime; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 171 | // The end of hwc present or validate, whichever one actually presented |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 172 | TimePoint hwcPresentEndTime; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 173 | // How long the actual hwc present was delayed after hwcPresentStartTime |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 174 | Duration hwcPresentDelayDuration{0ns}; |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame] | 175 | // 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] | 176 | // after potentially waiting for the earliest present time |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 177 | TimePoint presentFenceWaitStartTime; |
Matt Buckley | 16dec1f | 2022-06-07 21:46:20 +0000 | [diff] [blame] | 178 | // 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] | 179 | Duration postPresentFenceHwcPresentDuration{0ns}; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 180 | // Are we likely to have waited for the present fence during composition |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame] | 181 | bool probablyWaitsForPresentFence = false; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | struct GpuTimeline { |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 185 | Duration duration{0ns}; |
| 186 | TimePoint startTime; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | // Power hint session data recorded from the pipeline |
| 190 | struct DisplayTimingData { |
| 191 | std::unique_ptr<FenceTime> gpuEndFenceTime; |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 192 | std::optional<TimePoint> gpuStartTime; |
| 193 | std::optional<TimePoint> lastValidGpuEndTime; |
| 194 | std::optional<TimePoint> lastValidGpuStartTime; |
| 195 | std::optional<TimePoint> hwcPresentStartTime; |
| 196 | std::optional<TimePoint> hwcPresentEndTime; |
| 197 | std::optional<TimePoint> hwcValidateStartTime; |
| 198 | std::optional<TimePoint> hwcValidateEndTime; |
| 199 | std::optional<TimePoint> hwcPresentDelayedTime; |
Xiang Wang | aab3116 | 2024-03-12 19:48:08 -0700 | [diff] [blame] | 200 | bool requiresRenderEngine = false; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 201 | bool skippedValidate = false; |
| 202 | // Calculate high-level timing milestones from more granular display timing data |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 203 | DisplayTimeline calculateDisplayTimeline(TimePoint fenceTime); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 204 | // 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] | 205 | std::optional<GpuTimeline> estimateGpuTiming(std::optional<TimePoint> previousEndTime); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | template <class T, size_t N> |
| 209 | class RingBuffer { |
| 210 | std::array<T, N> elements = {}; |
| 211 | size_t mIndex = 0; |
| 212 | size_t numElements = 0; |
| 213 | |
| 214 | public: |
| 215 | void append(T item) { |
| 216 | mIndex = (mIndex + 1) % N; |
| 217 | numElements = std::min(N, numElements + 1); |
| 218 | elements[mIndex] = item; |
| 219 | } |
| 220 | bool isFull() const { return numElements == N; } |
| 221 | // Allows access like [0] == current, [-1] = previous, etc.. |
| 222 | T& operator[](int offset) { |
| 223 | size_t positiveOffset = |
| 224 | static_cast<size_t>((offset % static_cast<int>(N)) + static_cast<int>(N)); |
| 225 | return elements[(mIndex + positiveOffset) % N]; |
| 226 | } |
| 227 | }; |
| 228 | |
| 229 | // Filter and sort the display ids by a given property |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 230 | std::vector<DisplayId> getOrderedDisplayIds( |
| 231 | std::optional<TimePoint> DisplayTimingData::*sortBy); |
Xiang Wang | aab3116 | 2024-03-12 19:48:08 -0700 | [diff] [blame] | 232 | // Estimates a frame's total work duration including gpu and gpu time. |
| 233 | std::optional<aidl::android::hardware::power::WorkDuration> estimateWorkDuration(); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 234 | // There are two different targets and actual work durations we care about, |
| 235 | // this normalizes them together and takes the max of the two |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 236 | Duration combineTimingEstimates(Duration totalDuration, Duration flingerDuration); |
Matt Buckley | 52dfaad | 2024-03-07 20:20:07 +0000 | [diff] [blame] | 237 | // Whether to use the new "createHintSessionWithConfig" method |
| 238 | bool shouldCreateSessionWithConfig() REQUIRES(mHintSessionMutex); |
| 239 | |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 240 | bool ensurePowerHintSessionRunning() REQUIRES(mHintSessionMutex); |
Xiang Wang | 154fc04 | 2024-04-02 14:17:53 -0700 | [diff] [blame] | 241 | void setUpFmq() REQUIRES(mHintSessionMutex); |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 242 | std::unordered_map<DisplayId, DisplayTimingData> mDisplayTimingData; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 243 | // Current frame's delay |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 244 | Duration mFrameDelayDuration{0ns}; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 245 | // Last frame's post-composition duration |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 246 | Duration mLastPostcompDuration{0ns}; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 247 | // Buffer of recent commit start times |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 248 | RingBuffer<TimePoint, 2> mCommitStartTimes; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 249 | // Buffer of recent expected present times |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 250 | RingBuffer<TimePoint, 2> mExpectedPresentTimes; |
Matt Buckley | a2ad1dc | 2022-08-16 20:07:26 +0000 | [diff] [blame] | 251 | // 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] | 252 | TimePoint mLastPresentFenceTime; |
Matt Buckley | a2ad1dc | 2022-08-16 20:07:26 +0000 | [diff] [blame] | 253 | // 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] | 254 | TimePoint mLastSfPresentEndTime; |
Matt Buckley | a2ad1dc | 2022-08-16 20:07:26 +0000 | [diff] [blame] | 255 | // Target duration for the entire pipeline including gpu |
Matt Buckley | 2fa8501 | 2022-08-30 22:38:45 +0000 | [diff] [blame] | 256 | std::optional<Duration> mTotalFrameTargetDuration; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 257 | // Updated list of display IDs |
| 258 | std::vector<DisplayId> mDisplayIds; |
| 259 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 260 | // Ensure powerhal connection is initialized |
| 261 | power::PowerHalController& getPowerHal(); |
| 262 | |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 263 | // These variables are set before mBootFinished and never mutated after, so it's safe to access |
| 264 | // from threaded methods. |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 265 | std::optional<bool> mHintSessionEnabled; |
| 266 | std::optional<bool> mSupportsHintSession; |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 267 | |
| 268 | std::mutex mHintSessionMutex; |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame] | 269 | std::shared_ptr<power::PowerHintSessionWrapper> mHintSession GUARDED_BY(mHintSessionMutex) = |
| 270 | nullptr; |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 271 | |
| 272 | // Initialize to true so we try to call, to check if it's supported |
| 273 | bool mHasExpensiveRendering = true; |
| 274 | bool mHasDisplayUpdateImminent = true; |
| 275 | // Queue of actual durations saved to report |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 276 | std::vector<aidl::android::hardware::power::WorkDuration> mHintSessionQueue; |
Xiang Wang | 154fc04 | 2024-04-02 14:17:53 -0700 | [diff] [blame] | 277 | std::unique_ptr<::android::AidlMessageQueue< |
| 278 | aidl::android::hardware::power::ChannelMessage, |
| 279 | ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>> |
| 280 | mMsgQueue GUARDED_BY(mHintSessionMutex); |
| 281 | std::unique_ptr<::android::AidlMessageQueue< |
| 282 | int8_t, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>> |
| 283 | mFlagQueue GUARDED_BY(mHintSessionMutex); |
| 284 | android::hardware::EventFlag* mEventFlag; |
| 285 | uint32_t mFmqWriteMask; |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 286 | // The latest values we have received for target and actual |
| 287 | Duration mTargetDuration = kDefaultTargetDuration; |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 288 | // The list of thread ids, stored so we can restart the session from this class if needed |
| 289 | std::vector<int32_t> mHintSessionThreadIds; |
| 290 | Duration mLastTargetDurationSent = kDefaultTargetDuration; |
Matt Buckley | 547cc0c | 2023-10-27 22:22:36 +0000 | [diff] [blame] | 291 | |
| 292 | // Used to manage the execution ordering of reportActualWorkDuration for concurrency testing |
| 293 | std::promise<bool> mDelayReportActualMutexAcquisitonPromise; |
| 294 | bool mTimingTestingMode = false; |
| 295 | |
Matt Buckley | 52dfaad | 2024-03-07 20:20:07 +0000 | [diff] [blame] | 296 | // Hint session configuration data |
| 297 | aidl::android::hardware::power::SessionConfig mSessionConfig; |
| 298 | |
| 299 | // Whether createHintSessionWithConfig is supported, assume true until it fails |
| 300 | bool mSessionConfigSupported = true; |
| 301 | bool mFirstConfigSupportCheck = true; |
| 302 | |
Matt Buckley | 0538cae | 2022-11-08 23:12:04 +0000 | [diff] [blame] | 303 | // Whether we should emit ATRACE_INT data for hint sessions |
| 304 | static const bool sTraceHintSessionData; |
| 305 | |
| 306 | // Default target duration for the hint session |
| 307 | static constexpr const Duration kDefaultTargetDuration{16ms}; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 308 | |
Matt Buckley | c6b9d38 | 2022-06-17 15:28:07 -0700 | [diff] [blame] | 309 | // An adjustable safety margin which pads the "actual" value sent to PowerHAL, |
| 310 | // encouraging more aggressive boosting to give SurfaceFlinger a larger margin for error |
Matt Buckley | ac15a1b | 2023-02-28 06:51:28 +0000 | [diff] [blame] | 311 | static const Duration sTargetSafetyMargin; |
| 312 | static constexpr const Duration kDefaultTargetSafetyMargin{1ms}; |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 313 | |
Matt Buckley | 676e439 | 2023-05-25 22:09:26 +0000 | [diff] [blame] | 314 | // Whether we should send reportActualWorkDuration calls |
| 315 | static const bool sUseReportActualDuration; |
| 316 | |
Matt Buckley | 50c4406 | 2022-01-17 20:48:10 +0000 | [diff] [blame] | 317 | // 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] | 318 | static constexpr const Duration kFenceWaitStartDelayValidated{150us}; |
| 319 | static constexpr const Duration kFenceWaitStartDelaySkippedValidate{250us}; |
Xiang Wang | 154fc04 | 2024-04-02 14:17:53 -0700 | [diff] [blame] | 320 | |
| 321 | void sendHintSessionHint(aidl::android::hardware::power::SessionHint hint); |
| 322 | |
| 323 | template <aidl::android::hardware::power::ChannelMessage::ChannelMessageContents::Tag T, |
| 324 | class In> |
| 325 | bool writeHintSessionMessage(In* elements, size_t count) REQUIRES(mHintSessionMutex); |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 326 | }; |
| 327 | |
Michael Wright | 5d22d4f | 2018-06-21 02:50:34 +0100 | [diff] [blame] | 328 | } // namespace impl |
| 329 | } // namespace Hwc2 |
| 330 | } // namespace android |