blob: bbe51cc09d0adfd3cd6d7205ea4791aa28719863 [file] [log] [blame]
Michael Wright5d22d4f2018-06-21 02:50:34 +01001/*
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 Stoza030fbc12020-02-19 15:32:01 -080019#include <atomic>
Matt Buckleyef51fba2021-10-12 19:30:12 +000020#include <chrono>
Matt Buckley50c44062022-01-17 20:48:10 +000021#include <unordered_map>
Peiyong Lin74ca2f42019-01-14 19:36:57 -080022#include <unordered_set>
23
Matt Buckley50c44062022-01-17 20:48:10 +000024#include <ui/DisplayId.h>
25#include <ui/FenceTime.h>
Dan Stoza20950002020-06-18 14:56:58 -070026#include <utils/Mutex.h>
27
Matt Buckley104f53a2023-12-14 00:19:20 +000028// FMQ library in IPower does questionable conversions
29#pragma clang diagnostic push
30#pragma clang diagnostic ignored "-Wconversion"
Xiang Wang99f6f3c2023-05-22 13:12:16 -070031#include <aidl/android/hardware/power/IPower.h>
Matt Buckley0538cae2022-11-08 23:12:04 +000032#include <powermanager/PowerHalController.h>
Matt Buckley104f53a2023-12-14 00:19:20 +000033#pragma clang diagnostic pop
34
35#include <compositionengine/impl/OutputCompositionState.h>
Matt Buckley2fa85012022-08-30 22:38:45 +000036#include <scheduler/Time.h>
Alec Mouriff793872022-01-13 17:45:06 -080037#include <ui/DisplayIdentification.h>
Dan Stoza030fbc12020-02-19 15:32:01 -080038#include "../Scheduler/OneShotTimer.h"
Michael Wright5d22d4f2018-06-21 02:50:34 +010039
Matt Buckleyef51fba2021-10-12 19:30:12 +000040using namespace std::chrono_literals;
41
Michael Wright5d22d4f2018-06-21 02:50:34 +010042namespace android {
Alec Mouridea1ac52021-06-23 18:12:18 -070043
44class SurfaceFlinger;
45
Michael Wright5d22d4f2018-06-21 02:50:34 +010046namespace Hwc2 {
47
48class PowerAdvisor {
49public:
50 virtual ~PowerAdvisor();
51
Alec Mouridea1ac52021-06-23 18:12:18 -070052 // Initializes resources that cannot be initialized on construction
53 virtual void init() = 0;
Matt Buckley547cc0c2023-10-27 22:22:36 +000054 // Used to indicate that power hints can now be reported
Dan Stoza29e7bdf2020-03-23 14:43:09 -070055 virtual void onBootFinished() = 0;
Peiyong Lin74ca2f42019-01-14 19:36:57 -080056 virtual void setExpensiveRenderingExpected(DisplayId displayId, bool expected) = 0;
Alec Mouridea1ac52021-06-23 18:12:18 -070057 virtual bool isUsingExpensiveRendering() = 0;
Matt Buckley547cc0c2023-10-27 22:22:36 +000058 // Checks both if it's supported and if it's enabled; this is thread-safe since its values are
59 // set before onBootFinished, which gates all methods that run on threads other than SF main
Matt Buckley06f299a2021-09-24 19:43:51 +000060 virtual bool usePowerHintSession() = 0;
61 virtual bool supportsPowerHintSession() = 0;
Matt Buckley0538cae2022-11-08 23:12:04 +000062
Matt Buckley50c44062022-01-17 20:48:10 +000063 // Sends a power hint that updates to the target work duration for the frame
Matt Buckley0538cae2022-11-08 23:12:04 +000064 virtual void updateTargetWorkDuration(Duration targetDuration) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000065 // Sends a power hint for the actual known work duration at the end of the frame
Matt Buckley0538cae2022-11-08 23:12:04 +000066 virtual void reportActualWorkDuration() = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000067 // Sets whether the power hint session is enabled
Matt Buckley0538cae2022-11-08 23:12:04 +000068 virtual void enablePowerHintSession(bool enabled) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000069 // Initializes the power hint session
Matt Buckley547cc0c2023-10-27 22:22:36 +000070 virtual bool startPowerHintSession(std::vector<int32_t>&& threadIds) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000071 // Provides PowerAdvisor with a copy of the gpu fence so it can determine the gpu end time
72 virtual void setGpuFenceTime(DisplayId displayId, std::unique_ptr<FenceTime>&& fenceTime) = 0;
Matt Buckley16dec1f2022-06-07 21:46:20 +000073 // Reports the start and end times of a hwc validate call this frame for a given display
Matt Buckley2fa85012022-08-30 22:38:45 +000074 virtual void setHwcValidateTiming(DisplayId displayId, TimePoint validateStartTime,
75 TimePoint validateEndTime) = 0;
Matt Buckley16dec1f2022-06-07 21:46:20 +000076 // Reports the start and end times of a hwc present call this frame for a given display
Matt Buckley2fa85012022-08-30 22:38:45 +000077 virtual void setHwcPresentTiming(DisplayId displayId, TimePoint presentStartTime,
78 TimePoint presentEndTime) = 0;
Matt Buckleyc6b9d382022-06-17 15:28:07 -070079 // Reports the expected time that the current frame will present to the display
Matt Buckley2fa85012022-08-30 22:38:45 +000080 virtual void setExpectedPresentTime(TimePoint expectedPresentTime) = 0;
Matt Buckley1809d902022-08-05 06:51:43 +000081 // Reports the most recent present fence time and end time once known
Matt Buckley2fa85012022-08-30 22:38:45 +000082 virtual void setSfPresentTiming(TimePoint presentFenceTime, TimePoint presentEndTime) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000083 // Reports whether a display used client composition this frame
84 virtual void setRequiresClientComposition(DisplayId displayId,
85 bool requiresClientComposition) = 0;
86 // Reports whether a given display skipped validation this frame
87 virtual void setSkippedValidate(DisplayId displayId, bool skipped) = 0;
Matt Buckley16dec1f2022-06-07 21:46:20 +000088 // Reports when a hwc present is delayed, and the time that it will resume
Matt Buckley2fa85012022-08-30 22:38:45 +000089 virtual void setHwcPresentDelayedTime(DisplayId displayId,
90 TimePoint earliestFrameStartTime) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000091 // Reports the start delay for SurfaceFlinger this frame
Matt Buckley2fa85012022-08-30 22:38:45 +000092 virtual void setFrameDelay(Duration frameDelayDuration) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000093 // Reports the SurfaceFlinger commit start time this frame
Matt Buckley2fa85012022-08-30 22:38:45 +000094 virtual void setCommitStart(TimePoint commitStartTime) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000095 // Reports the SurfaceFlinger composite end time this frame
Matt Buckley2fa85012022-08-30 22:38:45 +000096 virtual void setCompositeEnd(TimePoint compositeEndTime) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000097 // Reports the list of the currently active displays
98 virtual void setDisplays(std::vector<DisplayId>& displayIds) = 0;
99 // Sets the target duration for the entire pipeline including the gpu
Matt Buckley2fa85012022-08-30 22:38:45 +0000100 virtual void setTotalFrameTargetWorkDuration(Duration targetDuration) = 0;
Matt Buckley547cc0c2023-10-27 22:22:36 +0000101
102 // --- The following methods may run on threads besides SF main ---
103 // Send a hint about an upcoming increase in the CPU workload
104 virtual void notifyCpuLoadUp() = 0;
105 // Send a hint about the imminent start of a new CPU workload
106 virtual void notifyDisplayUpdateImminentAndCpuReset() = 0;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100107};
108
109namespace impl {
110
Michael Wright5d22d4f2018-06-21 02:50:34 +0100111// PowerAdvisor is a wrapper around IPower HAL which takes into account the
112// full state of the system when sending out power hints to things like the GPU.
113class PowerAdvisor final : public Hwc2::PowerAdvisor {
114public:
Alec Mouridea1ac52021-06-23 18:12:18 -0700115 PowerAdvisor(SurfaceFlinger& flinger);
Michael Wright5d22d4f2018-06-21 02:50:34 +0100116 ~PowerAdvisor() override;
117
Alec Mouridea1ac52021-06-23 18:12:18 -0700118 void init() override;
Dan Stoza29e7bdf2020-03-23 14:43:09 -0700119 void onBootFinished() override;
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800120 void setExpensiveRenderingExpected(DisplayId displayId, bool expected) override;
Matt Buckley06f299a2021-09-24 19:43:51 +0000121 bool isUsingExpensiveRendering() override { return mNotifiedExpensiveRendering; };
Matt Buckley06f299a2021-09-24 19:43:51 +0000122 bool usePowerHintSession() override;
123 bool supportsPowerHintSession() override;
Matt Buckley0538cae2022-11-08 23:12:04 +0000124 void updateTargetWorkDuration(Duration targetDuration) override;
125 void reportActualWorkDuration() override;
126 void enablePowerHintSession(bool enabled) override;
Matt Buckley547cc0c2023-10-27 22:22:36 +0000127 bool startPowerHintSession(std::vector<int32_t>&& threadIds) override;
128 void setGpuFenceTime(DisplayId displayId, std::unique_ptr<FenceTime>&& fenceTime) override;
Matt Buckley2fa85012022-08-30 22:38:45 +0000129 void setHwcValidateTiming(DisplayId displayId, TimePoint validateStartTime,
130 TimePoint validateEndTime) override;
131 void setHwcPresentTiming(DisplayId displayId, TimePoint presentStartTime,
132 TimePoint presentEndTime) override;
Matt Buckley50c44062022-01-17 20:48:10 +0000133 void setSkippedValidate(DisplayId displayId, bool skipped) override;
134 void setRequiresClientComposition(DisplayId displayId, bool requiresClientComposition) override;
Matt Buckley2fa85012022-08-30 22:38:45 +0000135 void setExpectedPresentTime(TimePoint expectedPresentTime) override;
136 void setSfPresentTiming(TimePoint presentFenceTime, TimePoint presentEndTime) override;
137 void setHwcPresentDelayedTime(DisplayId displayId, TimePoint earliestFrameStartTime) override;
Matt Buckley2fa85012022-08-30 22:38:45 +0000138 void setFrameDelay(Duration frameDelayDuration) override;
139 void setCommitStart(TimePoint commitStartTime) override;
140 void setCompositeEnd(TimePoint compositeEndTime) override;
Matt Buckley50c44062022-01-17 20:48:10 +0000141 void setDisplays(std::vector<DisplayId>& displayIds) override;
Matt Buckley2fa85012022-08-30 22:38:45 +0000142 void setTotalFrameTargetWorkDuration(Duration targetDuration) override;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100143
Matt Buckley547cc0c2023-10-27 22:22:36 +0000144 // --- The following methods may run on threads besides SF main ---
145 void notifyCpuLoadUp() override;
146 void notifyDisplayUpdateImminentAndCpuReset() override;
147
Michael Wright5d22d4f2018-06-21 02:50:34 +0100148private:
Matt Buckley57274052022-08-12 21:54:23 +0000149 friend class PowerAdvisorTest;
150
Matt Buckley0538cae2022-11-08 23:12:04 +0000151 std::unique_ptr<power::PowerHalController> mPowerHal;
Dan Stoza29e7bdf2020-03-23 14:43:09 -0700152 std::atomic_bool mBootFinished = false;
Matt Buckleyef51fba2021-10-12 19:30:12 +0000153
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800154 std::unordered_set<DisplayId> mExpensiveDisplays;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100155 bool mNotifiedExpensiveRendering = false;
Dan Stoza030fbc12020-02-19 15:32:01 -0800156
Alec Mouridea1ac52021-06-23 18:12:18 -0700157 SurfaceFlinger& mFlinger;
Dan Stoza030fbc12020-02-19 15:32:01 -0800158 std::atomic_bool mSendUpdateImminent = true;
Alec Mouric059dcf2022-05-05 23:40:07 +0000159 std::atomic<nsecs_t> mLastScreenUpdatedTime = 0;
160 std::optional<scheduler::OneShotTimer> mScreenUpdateTimer;
Matt Buckley50c44062022-01-17 20:48:10 +0000161
162 // Higher-level timing data used for estimation
163 struct DisplayTimeline {
Matt Buckley16dec1f2022-06-07 21:46:20 +0000164 // The start of hwc present, or the start of validate if it happened there instead
Matt Buckley2fa85012022-08-30 22:38:45 +0000165 TimePoint hwcPresentStartTime;
Matt Buckley16dec1f2022-06-07 21:46:20 +0000166 // The end of hwc present or validate, whichever one actually presented
Matt Buckley2fa85012022-08-30 22:38:45 +0000167 TimePoint hwcPresentEndTime;
Matt Buckley16dec1f2022-06-07 21:46:20 +0000168 // How long the actual hwc present was delayed after hwcPresentStartTime
Matt Buckley2fa85012022-08-30 22:38:45 +0000169 Duration hwcPresentDelayDuration{0ns};
Matt Buckleyc6b9d382022-06-17 15:28:07 -0700170 // When we think we started waiting for the present fence after calling into hwc present and
Matt Buckley50c44062022-01-17 20:48:10 +0000171 // after potentially waiting for the earliest present time
Matt Buckley2fa85012022-08-30 22:38:45 +0000172 TimePoint presentFenceWaitStartTime;
Matt Buckley16dec1f2022-06-07 21:46:20 +0000173 // How long we ran after we finished waiting for the fence but before hwc present finished
Matt Buckley2fa85012022-08-30 22:38:45 +0000174 Duration postPresentFenceHwcPresentDuration{0ns};
Matt Buckley50c44062022-01-17 20:48:10 +0000175 // Are we likely to have waited for the present fence during composition
Matt Buckleyc6b9d382022-06-17 15:28:07 -0700176 bool probablyWaitsForPresentFence = false;
Matt Buckley50c44062022-01-17 20:48:10 +0000177 };
178
179 struct GpuTimeline {
Matt Buckley2fa85012022-08-30 22:38:45 +0000180 Duration duration{0ns};
181 TimePoint startTime;
Matt Buckley50c44062022-01-17 20:48:10 +0000182 };
183
184 // Power hint session data recorded from the pipeline
185 struct DisplayTimingData {
186 std::unique_ptr<FenceTime> gpuEndFenceTime;
Matt Buckley2fa85012022-08-30 22:38:45 +0000187 std::optional<TimePoint> gpuStartTime;
188 std::optional<TimePoint> lastValidGpuEndTime;
189 std::optional<TimePoint> lastValidGpuStartTime;
190 std::optional<TimePoint> hwcPresentStartTime;
191 std::optional<TimePoint> hwcPresentEndTime;
192 std::optional<TimePoint> hwcValidateStartTime;
193 std::optional<TimePoint> hwcValidateEndTime;
194 std::optional<TimePoint> hwcPresentDelayedTime;
Matt Buckley50c44062022-01-17 20:48:10 +0000195 bool usedClientComposition = false;
196 bool skippedValidate = false;
197 // Calculate high-level timing milestones from more granular display timing data
Matt Buckley2fa85012022-08-30 22:38:45 +0000198 DisplayTimeline calculateDisplayTimeline(TimePoint fenceTime);
Matt Buckley50c44062022-01-17 20:48:10 +0000199 // Estimate the gpu duration for a given display from previous gpu timing data
Matt Buckley2fa85012022-08-30 22:38:45 +0000200 std::optional<GpuTimeline> estimateGpuTiming(std::optional<TimePoint> previousEndTime);
Matt Buckley50c44062022-01-17 20:48:10 +0000201 };
202
203 template <class T, size_t N>
204 class RingBuffer {
205 std::array<T, N> elements = {};
206 size_t mIndex = 0;
207 size_t numElements = 0;
208
209 public:
210 void append(T item) {
211 mIndex = (mIndex + 1) % N;
212 numElements = std::min(N, numElements + 1);
213 elements[mIndex] = item;
214 }
215 bool isFull() const { return numElements == N; }
216 // Allows access like [0] == current, [-1] = previous, etc..
217 T& operator[](int offset) {
218 size_t positiveOffset =
219 static_cast<size_t>((offset % static_cast<int>(N)) + static_cast<int>(N));
220 return elements[(mIndex + positiveOffset) % N];
221 }
222 };
223
224 // Filter and sort the display ids by a given property
Matt Buckley2fa85012022-08-30 22:38:45 +0000225 std::vector<DisplayId> getOrderedDisplayIds(
226 std::optional<TimePoint> DisplayTimingData::*sortBy);
Matt Buckley50c44062022-01-17 20:48:10 +0000227 // Estimates a frame's total work duration including gpu time.
Matt Buckley0538cae2022-11-08 23:12:04 +0000228 std::optional<Duration> estimateWorkDuration();
Matt Buckley50c44062022-01-17 20:48:10 +0000229 // There are two different targets and actual work durations we care about,
230 // this normalizes them together and takes the max of the two
Matt Buckley2fa85012022-08-30 22:38:45 +0000231 Duration combineTimingEstimates(Duration totalDuration, Duration flingerDuration);
Matt Buckley50c44062022-01-17 20:48:10 +0000232
Matt Buckley547cc0c2023-10-27 22:22:36 +0000233 bool ensurePowerHintSessionRunning() REQUIRES(mHintSessionMutex);
Matt Buckley50c44062022-01-17 20:48:10 +0000234 std::unordered_map<DisplayId, DisplayTimingData> mDisplayTimingData;
235
236 // Current frame's delay
Matt Buckley2fa85012022-08-30 22:38:45 +0000237 Duration mFrameDelayDuration{0ns};
Matt Buckley50c44062022-01-17 20:48:10 +0000238 // Last frame's post-composition duration
Matt Buckley2fa85012022-08-30 22:38:45 +0000239 Duration mLastPostcompDuration{0ns};
Matt Buckley50c44062022-01-17 20:48:10 +0000240 // Buffer of recent commit start times
Matt Buckley2fa85012022-08-30 22:38:45 +0000241 RingBuffer<TimePoint, 2> mCommitStartTimes;
Matt Buckley50c44062022-01-17 20:48:10 +0000242 // Buffer of recent expected present times
Matt Buckley2fa85012022-08-30 22:38:45 +0000243 RingBuffer<TimePoint, 2> mExpectedPresentTimes;
Matt Buckleya2ad1dc2022-08-16 20:07:26 +0000244 // Most recent present fence time, provided by SF after composition engine finishes presenting
Matt Buckley2fa85012022-08-30 22:38:45 +0000245 TimePoint mLastPresentFenceTime;
Matt Buckleya2ad1dc2022-08-16 20:07:26 +0000246 // Most recent composition engine present end time, returned with the present fence from SF
Matt Buckley2fa85012022-08-30 22:38:45 +0000247 TimePoint mLastSfPresentEndTime;
Matt Buckleya2ad1dc2022-08-16 20:07:26 +0000248 // Target duration for the entire pipeline including gpu
Matt Buckley2fa85012022-08-30 22:38:45 +0000249 std::optional<Duration> mTotalFrameTargetDuration;
Matt Buckley50c44062022-01-17 20:48:10 +0000250 // Updated list of display IDs
251 std::vector<DisplayId> mDisplayIds;
252
Matt Buckley0538cae2022-11-08 23:12:04 +0000253 // Ensure powerhal connection is initialized
254 power::PowerHalController& getPowerHal();
255
Matt Buckley547cc0c2023-10-27 22:22:36 +0000256 // These variables are set before mBootFinished and never mutated after, so it's safe to access
257 // from threaded methods.
Matt Buckley0538cae2022-11-08 23:12:04 +0000258 std::optional<bool> mHintSessionEnabled;
259 std::optional<bool> mSupportsHintSession;
Matt Buckley0538cae2022-11-08 23:12:04 +0000260
261 std::mutex mHintSessionMutex;
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700262 std::shared_ptr<aidl::android::hardware::power::IPowerHintSession> mHintSession
263 GUARDED_BY(mHintSessionMutex) = nullptr;
Matt Buckley0538cae2022-11-08 23:12:04 +0000264
265 // Initialize to true so we try to call, to check if it's supported
266 bool mHasExpensiveRendering = true;
267 bool mHasDisplayUpdateImminent = true;
268 // Queue of actual durations saved to report
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700269 std::vector<aidl::android::hardware::power::WorkDuration> mHintSessionQueue;
Matt Buckley0538cae2022-11-08 23:12:04 +0000270 // The latest values we have received for target and actual
271 Duration mTargetDuration = kDefaultTargetDuration;
272 std::optional<Duration> mActualDuration;
273 // The list of thread ids, stored so we can restart the session from this class if needed
274 std::vector<int32_t> mHintSessionThreadIds;
275 Duration mLastTargetDurationSent = kDefaultTargetDuration;
Matt Buckley547cc0c2023-10-27 22:22:36 +0000276
277 // Used to manage the execution ordering of reportActualWorkDuration for concurrency testing
278 std::promise<bool> mDelayReportActualMutexAcquisitonPromise;
279 bool mTimingTestingMode = false;
280
Matt Buckley0538cae2022-11-08 23:12:04 +0000281 // Whether we should emit ATRACE_INT data for hint sessions
282 static const bool sTraceHintSessionData;
283
284 // Default target duration for the hint session
285 static constexpr const Duration kDefaultTargetDuration{16ms};
Matt Buckley50c44062022-01-17 20:48:10 +0000286
Matt Buckleyc6b9d382022-06-17 15:28:07 -0700287 // An adjustable safety margin which pads the "actual" value sent to PowerHAL,
288 // encouraging more aggressive boosting to give SurfaceFlinger a larger margin for error
Matt Buckleyac15a1b2023-02-28 06:51:28 +0000289 static const Duration sTargetSafetyMargin;
290 static constexpr const Duration kDefaultTargetSafetyMargin{1ms};
Matt Buckley50c44062022-01-17 20:48:10 +0000291
Matt Buckley676e4392023-05-25 22:09:26 +0000292 // Whether we should send reportActualWorkDuration calls
293 static const bool sUseReportActualDuration;
294
Matt Buckley50c44062022-01-17 20:48:10 +0000295 // How long we expect hwc to run after the present call until it waits for the fence
Matt Buckley2fa85012022-08-30 22:38:45 +0000296 static constexpr const Duration kFenceWaitStartDelayValidated{150us};
297 static constexpr const Duration kFenceWaitStartDelaySkippedValidate{250us};
Michael Wright5d22d4f2018-06-21 02:50:34 +0100298};
299
Michael Wright5d22d4f2018-06-21 02:50:34 +0100300} // namespace impl
301} // namespace Hwc2
302} // namespace android