blob: 859cf3d3696643efcc159bdeea14148e0f6804d5 [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
Xiang Wange12b4fa2022-03-25 23:48:40 +000028#include <android/hardware/power/IPower.h>
Matt Buckley50c44062022-01-17 20:48:10 +000029#include <compositionengine/impl/OutputCompositionState.h>
Alec Mouriff793872022-01-13 17:45:06 -080030#include <ui/DisplayIdentification.h>
Dan Stoza030fbc12020-02-19 15:32:01 -080031#include "../Scheduler/OneShotTimer.h"
Michael Wright5d22d4f2018-06-21 02:50:34 +010032
Matt Buckleyef51fba2021-10-12 19:30:12 +000033using namespace std::chrono_literals;
34
Michael Wright5d22d4f2018-06-21 02:50:34 +010035namespace android {
Alec Mouridea1ac52021-06-23 18:12:18 -070036
37class SurfaceFlinger;
38
Michael Wright5d22d4f2018-06-21 02:50:34 +010039namespace Hwc2 {
40
41class PowerAdvisor {
42public:
43 virtual ~PowerAdvisor();
44
Alec Mouridea1ac52021-06-23 18:12:18 -070045 // Initializes resources that cannot be initialized on construction
46 virtual void init() = 0;
Dan Stoza29e7bdf2020-03-23 14:43:09 -070047 virtual void onBootFinished() = 0;
Peiyong Lin74ca2f42019-01-14 19:36:57 -080048 virtual void setExpensiveRenderingExpected(DisplayId displayId, bool expected) = 0;
Alec Mouridea1ac52021-06-23 18:12:18 -070049 virtual bool isUsingExpensiveRendering() = 0;
Dan Stoza030fbc12020-02-19 15:32:01 -080050 virtual void notifyDisplayUpdateImminent() = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000051 // Checks both if it supports and if it's enabled
Matt Buckley06f299a2021-09-24 19:43:51 +000052 virtual bool usePowerHintSession() = 0;
53 virtual bool supportsPowerHintSession() = 0;
54 virtual bool isPowerHintSessionRunning() = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000055 // Sends a power hint that updates to the target work duration for the frame
56 virtual void setTargetWorkDuration(nsecs_t targetDuration) = 0;
57 // Sends a power hint for the actual known work duration at the end of the frame
58 virtual void sendActualWorkDuration() = 0;
59 // Sends a power hint for the upcoming frame predicted from previous frame timing
60 virtual void sendPredictedWorkDuration() = 0;
61 // Sets whether the power hint session is enabled
Matt Buckley06f299a2021-09-24 19:43:51 +000062 virtual void enablePowerHint(bool enabled) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000063 // Initializes the power hint session
Matt Buckleyef51fba2021-10-12 19:30:12 +000064 virtual bool startPowerHintSession(const std::vector<int32_t>& threadIds) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000065 // Provides PowerAdvisor with a copy of the gpu fence so it can determine the gpu end time
66 virtual void setGpuFenceTime(DisplayId displayId, std::unique_ptr<FenceTime>&& fenceTime) = 0;
Matt Buckley16dec1f2022-06-07 21:46:20 +000067 // Reports the start and end times of a hwc validate call this frame for a given display
68 virtual void setHwcValidateTiming(DisplayId displayId, nsecs_t validateStartTime,
69 nsecs_t validateEndTime) = 0;
70 // Reports the start and end times of a hwc present call this frame for a given display
71 virtual void setHwcPresentTiming(DisplayId displayId, nsecs_t presentStartTime,
72 nsecs_t presentEndTime) = 0;
Matt Buckleyc6b9d382022-06-17 15:28:07 -070073 // Reports the expected time that the current frame will present to the display
Matt Buckley50c44062022-01-17 20:48:10 +000074 virtual void setExpectedPresentTime(nsecs_t expectedPresentTime) = 0;
Matt Buckley1809d902022-08-05 06:51:43 +000075 // Reports the most recent present fence time and end time once known
76 virtual void setSfPresentTiming(nsecs_t presentFenceTime, nsecs_t presentEndTime) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000077 // Reports whether a display used client composition this frame
78 virtual void setRequiresClientComposition(DisplayId displayId,
79 bool requiresClientComposition) = 0;
80 // Reports whether a given display skipped validation this frame
81 virtual void setSkippedValidate(DisplayId displayId, bool skipped) = 0;
Matt Buckley16dec1f2022-06-07 21:46:20 +000082 // Reports when a hwc present is delayed, and the time that it will resume
83 virtual void setHwcPresentDelayedTime(
Matt Buckley50c44062022-01-17 20:48:10 +000084 DisplayId displayId, std::chrono::steady_clock::time_point earliestFrameStartTime) = 0;
85 // Reports the start delay for SurfaceFlinger this frame
86 virtual void setFrameDelay(nsecs_t frameDelayDuration) = 0;
87 // Reports the SurfaceFlinger commit start time this frame
88 virtual void setCommitStart(nsecs_t commitStartTime) = 0;
89 // Reports the SurfaceFlinger composite end time this frame
90 virtual void setCompositeEnd(nsecs_t compositeEndTime) = 0;
91 // Reports the list of the currently active displays
92 virtual void setDisplays(std::vector<DisplayId>& displayIds) = 0;
93 // Sets the target duration for the entire pipeline including the gpu
94 virtual void setTotalFrameTargetWorkDuration(nsecs_t targetDuration) = 0;
Michael Wright5d22d4f2018-06-21 02:50:34 +010095};
96
97namespace impl {
98
Michael Wright5d22d4f2018-06-21 02:50:34 +010099// PowerAdvisor is a wrapper around IPower HAL which takes into account the
100// full state of the system when sending out power hints to things like the GPU.
101class PowerAdvisor final : public Hwc2::PowerAdvisor {
102public:
Dan Stoza030fbc12020-02-19 15:32:01 -0800103 class HalWrapper {
104 public:
105 virtual ~HalWrapper() = default;
106
107 virtual bool setExpensiveRendering(bool enabled) = 0;
108 virtual bool notifyDisplayUpdateImminent() = 0;
Matt Buckley06f299a2021-09-24 19:43:51 +0000109 virtual bool supportsPowerHintSession() = 0;
110 virtual bool isPowerHintSessionRunning() = 0;
111 virtual void restartPowerHintSession() = 0;
112 virtual void setPowerHintSessionThreadIds(const std::vector<int32_t>& threadIds) = 0;
113 virtual bool startPowerHintSession() = 0;
Matt Buckley50c44062022-01-17 20:48:10 +0000114 virtual void setTargetWorkDuration(nsecs_t targetDuration) = 0;
115 virtual void sendActualWorkDuration(nsecs_t actualDuration, nsecs_t timestamp) = 0;
Matt Buckley06f299a2021-09-24 19:43:51 +0000116 virtual bool shouldReconnectHAL() = 0;
117 virtual std::vector<int32_t> getPowerHintSessionThreadIds() = 0;
Matt Buckley50c44062022-01-17 20:48:10 +0000118 virtual std::optional<nsecs_t> getTargetWorkDuration() = 0;
Dan Stoza030fbc12020-02-19 15:32:01 -0800119 };
120
Alec Mouridea1ac52021-06-23 18:12:18 -0700121 PowerAdvisor(SurfaceFlinger& flinger);
Michael Wright5d22d4f2018-06-21 02:50:34 +0100122 ~PowerAdvisor() override;
123
Alec Mouridea1ac52021-06-23 18:12:18 -0700124 void init() override;
Dan Stoza29e7bdf2020-03-23 14:43:09 -0700125 void onBootFinished() override;
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800126 void setExpensiveRenderingExpected(DisplayId displayId, bool expected) override;
Matt Buckley06f299a2021-09-24 19:43:51 +0000127 bool isUsingExpensiveRendering() override { return mNotifiedExpensiveRendering; };
Dan Stoza030fbc12020-02-19 15:32:01 -0800128 void notifyDisplayUpdateImminent() override;
Matt Buckley06f299a2021-09-24 19:43:51 +0000129 bool usePowerHintSession() override;
130 bool supportsPowerHintSession() override;
131 bool isPowerHintSessionRunning() override;
Matt Buckley50c44062022-01-17 20:48:10 +0000132 void setTargetWorkDuration(nsecs_t targetDuration) override;
133 void sendActualWorkDuration() override;
134 void sendPredictedWorkDuration() override;
Matt Buckley06f299a2021-09-24 19:43:51 +0000135 void enablePowerHint(bool enabled) override;
Matt Buckleyef51fba2021-10-12 19:30:12 +0000136 bool startPowerHintSession(const std::vector<int32_t>& threadIds) override;
Matt Buckley50c44062022-01-17 20:48:10 +0000137 void setGpuFenceTime(DisplayId displayId, std::unique_ptr<FenceTime>&& fenceTime);
Matt Buckley16dec1f2022-06-07 21:46:20 +0000138 void setHwcValidateTiming(DisplayId displayId, nsecs_t valiateStartTime,
139 nsecs_t validateEndTime) override;
140 void setHwcPresentTiming(DisplayId displayId, nsecs_t presentStartTime,
141 nsecs_t presentEndTime) override;
Matt Buckley50c44062022-01-17 20:48:10 +0000142 void setSkippedValidate(DisplayId displayId, bool skipped) override;
143 void setRequiresClientComposition(DisplayId displayId, bool requiresClientComposition) override;
144 void setExpectedPresentTime(nsecs_t expectedPresentTime) override;
Matt Buckley1809d902022-08-05 06:51:43 +0000145 void setSfPresentTiming(nsecs_t presentFenceTime, nsecs_t presentEndTime) override;
Matt Buckley16dec1f2022-06-07 21:46:20 +0000146 void setHwcPresentDelayedTime(
Matt Buckley50c44062022-01-17 20:48:10 +0000147 DisplayId displayId,
148 std::chrono::steady_clock::time_point earliestFrameStartTime) override;
149
150 void setFrameDelay(nsecs_t frameDelayDuration) override;
151 void setCommitStart(nsecs_t commitStartTime) override;
152 void setCompositeEnd(nsecs_t compositeEndTime) override;
153 void setDisplays(std::vector<DisplayId>& displayIds) override;
154 void setTotalFrameTargetWorkDuration(nsecs_t targetDuration) override;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100155
156private:
Dan Stoza20950002020-06-18 14:56:58 -0700157 HalWrapper* getPowerHal() REQUIRES(mPowerHalMutex);
158 bool mReconnectPowerHal GUARDED_BY(mPowerHalMutex) = false;
159 std::mutex mPowerHalMutex;
Dan Stoza030fbc12020-02-19 15:32:01 -0800160
Dan Stoza29e7bdf2020-03-23 14:43:09 -0700161 std::atomic_bool mBootFinished = false;
Matt Buckleyef51fba2021-10-12 19:30:12 +0000162
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800163 std::unordered_set<DisplayId> mExpensiveDisplays;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100164 bool mNotifiedExpensiveRendering = false;
Dan Stoza030fbc12020-02-19 15:32:01 -0800165
Alec Mouridea1ac52021-06-23 18:12:18 -0700166 SurfaceFlinger& mFlinger;
Dan Stoza030fbc12020-02-19 15:32:01 -0800167 std::atomic_bool mSendUpdateImminent = true;
Alec Mouric059dcf2022-05-05 23:40:07 +0000168 std::atomic<nsecs_t> mLastScreenUpdatedTime = 0;
169 std::optional<scheduler::OneShotTimer> mScreenUpdateTimer;
Matt Buckley50c44062022-01-17 20:48:10 +0000170
171 // Higher-level timing data used for estimation
172 struct DisplayTimeline {
Matt Buckley16dec1f2022-06-07 21:46:20 +0000173 // The start of hwc present, or the start of validate if it happened there instead
174 nsecs_t hwcPresentStartTime = -1;
175 // The end of hwc present or validate, whichever one actually presented
176 nsecs_t hwcPresentEndTime = -1;
177 // How long the actual hwc present was delayed after hwcPresentStartTime
178 nsecs_t hwcPresentDelayDuration = 0;
Matt Buckleyc6b9d382022-06-17 15:28:07 -0700179 // When we think we started waiting for the present fence after calling into hwc present and
Matt Buckley50c44062022-01-17 20:48:10 +0000180 // after potentially waiting for the earliest present time
Matt Buckleyc6b9d382022-06-17 15:28:07 -0700181 nsecs_t presentFenceWaitStartTime = -1;
Matt Buckley16dec1f2022-06-07 21:46:20 +0000182 // How long we ran after we finished waiting for the fence but before hwc present finished
Matt Buckleyc6b9d382022-06-17 15:28:07 -0700183 nsecs_t postPresentFenceHwcPresentDuration = 0;
Matt Buckley50c44062022-01-17 20:48:10 +0000184 // Are we likely to have waited for the present fence during composition
Matt Buckleyc6b9d382022-06-17 15:28:07 -0700185 bool probablyWaitsForPresentFence = false;
Matt Buckley50c44062022-01-17 20:48:10 +0000186 // Estimate one frame's timeline from that of a previous frame
187 DisplayTimeline estimateTimelineFromReference(nsecs_t fenceTime, nsecs_t displayStartTime);
188 };
189
190 struct GpuTimeline {
191 nsecs_t duration = 0;
192 nsecs_t startTime = -1;
193 };
194
195 // Power hint session data recorded from the pipeline
196 struct DisplayTimingData {
197 std::unique_ptr<FenceTime> gpuEndFenceTime;
198 std::optional<nsecs_t> gpuStartTime;
199 std::optional<nsecs_t> lastValidGpuEndTime;
200 std::optional<nsecs_t> lastValidGpuStartTime;
Matt Buckley16dec1f2022-06-07 21:46:20 +0000201 std::optional<nsecs_t> hwcPresentStartTime;
202 std::optional<nsecs_t> hwcPresentEndTime;
203 std::optional<nsecs_t> hwcValidateStartTime;
204 std::optional<nsecs_t> hwcValidateEndTime;
205 std::optional<nsecs_t> hwcPresentDelayedTime;
Matt Buckley50c44062022-01-17 20:48:10 +0000206 bool usedClientComposition = false;
207 bool skippedValidate = false;
208 // Calculate high-level timing milestones from more granular display timing data
209 DisplayTimeline calculateDisplayTimeline(nsecs_t fenceTime);
210 // Estimate the gpu duration for a given display from previous gpu timing data
211 std::optional<GpuTimeline> estimateGpuTiming(std::optional<nsecs_t> previousEnd);
212 };
213
214 template <class T, size_t N>
215 class RingBuffer {
216 std::array<T, N> elements = {};
217 size_t mIndex = 0;
218 size_t numElements = 0;
219
220 public:
221 void append(T item) {
222 mIndex = (mIndex + 1) % N;
223 numElements = std::min(N, numElements + 1);
224 elements[mIndex] = item;
225 }
226 bool isFull() const { return numElements == N; }
227 // Allows access like [0] == current, [-1] = previous, etc..
228 T& operator[](int offset) {
229 size_t positiveOffset =
230 static_cast<size_t>((offset % static_cast<int>(N)) + static_cast<int>(N));
231 return elements[(mIndex + positiveOffset) % N];
232 }
233 };
234
235 // Filter and sort the display ids by a given property
236 std::vector<DisplayId> getOrderedDisplayIds(std::optional<nsecs_t> DisplayTimingData::*sortBy);
237 // Estimates a frame's total work duration including gpu time.
238 // Runs either at the beginning or end of a frame, using the most recent data available
239 std::optional<nsecs_t> estimateWorkDuration(bool earlyHint);
240 // There are two different targets and actual work durations we care about,
241 // this normalizes them together and takes the max of the two
242 nsecs_t combineTimingEstimates(nsecs_t totalDuration, nsecs_t flingerDuration);
243
244 std::unordered_map<DisplayId, DisplayTimingData> mDisplayTimingData;
245
246 // Current frame's delay
247 nsecs_t mFrameDelayDuration = 0;
Matt Buckley50c44062022-01-17 20:48:10 +0000248 // Last frame's post-composition duration
249 nsecs_t mLastPostcompDuration = 0;
250 // Buffer of recent commit start times
251 RingBuffer<nsecs_t, 2> mCommitStartTimes;
252 // Buffer of recent expected present times
Matt Buckleyc6b9d382022-06-17 15:28:07 -0700253 RingBuffer<nsecs_t, 2> mExpectedPresentTimes;
254 // Most recent present fence time, set at the end of the frame once known
255 nsecs_t mLastPresentFenceTime = -1;
Matt Buckley1809d902022-08-05 06:51:43 +0000256 // Most recent present fence time, set at the end of the frame once known
257 nsecs_t mLastSfPresentEndTime = -1;
Matt Buckley50c44062022-01-17 20:48:10 +0000258 // Target for the entire pipeline including gpu
259 std::optional<nsecs_t> mTotalFrameTargetDuration;
260 // Updated list of display IDs
261 std::vector<DisplayId> mDisplayIds;
262
263 std::optional<bool> mPowerHintEnabled;
264 std::optional<bool> mSupportsPowerHint;
265 bool mPowerHintSessionRunning = false;
266
Matt Buckleyc6b9d382022-06-17 15:28:07 -0700267 // An adjustable safety margin which pads the "actual" value sent to PowerHAL,
268 // encouraging more aggressive boosting to give SurfaceFlinger a larger margin for error
Matt Buckley50c44062022-01-17 20:48:10 +0000269 static constexpr const std::chrono::nanoseconds kTargetSafetyMargin = 1ms;
270
271 // How long we expect hwc to run after the present call until it waits for the fence
Matt Buckley16dec1f2022-06-07 21:46:20 +0000272 static constexpr const std::chrono::nanoseconds kFenceWaitStartDelayValidated = 150us;
273 static constexpr const std::chrono::nanoseconds kFenceWaitStartDelaySkippedValidate = 250us;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100274};
275
Xiang Wange12b4fa2022-03-25 23:48:40 +0000276class AidlPowerHalWrapper : public PowerAdvisor::HalWrapper {
277public:
278 explicit AidlPowerHalWrapper(sp<hardware::power::IPower> powerHal);
279 ~AidlPowerHalWrapper() override;
280
281 static std::unique_ptr<HalWrapper> connect();
282
283 bool setExpensiveRendering(bool enabled) override;
284 bool notifyDisplayUpdateImminent() override;
285 bool supportsPowerHintSession() override;
286 bool isPowerHintSessionRunning() override;
287 void restartPowerHintSession() override;
288 void setPowerHintSessionThreadIds(const std::vector<int32_t>& threadIds) override;
289 bool startPowerHintSession() override;
Matt Buckley50c44062022-01-17 20:48:10 +0000290 void setTargetWorkDuration(nsecs_t targetDuration) override;
291 void sendActualWorkDuration(nsecs_t actualDuration, nsecs_t timestamp) override;
Xiang Wange12b4fa2022-03-25 23:48:40 +0000292 bool shouldReconnectHAL() override;
293 std::vector<int32_t> getPowerHintSessionThreadIds() override;
Matt Buckley50c44062022-01-17 20:48:10 +0000294 std::optional<nsecs_t> getTargetWorkDuration() override;
Xiang Wange12b4fa2022-03-25 23:48:40 +0000295
296private:
Matt Buckley50c44062022-01-17 20:48:10 +0000297 friend class AidlPowerHalWrapperTest;
298
Xiang Wange12b4fa2022-03-25 23:48:40 +0000299 bool checkPowerHintSessionSupported();
300 void closePowerHintSession();
Matt Buckley50c44062022-01-17 20:48:10 +0000301 bool shouldReportActualDurations();
302
303 // Used for testing
304 void setAllowedActualDeviation(nsecs_t);
Xiang Wange12b4fa2022-03-25 23:48:40 +0000305
306 const sp<hardware::power::IPower> mPowerHal = nullptr;
307 bool mHasExpensiveRendering = false;
308 bool mHasDisplayUpdateImminent = false;
309 // Used to indicate an error state and need for reconstruction
310 bool mShouldReconnectHal = false;
Matt Buckley50c44062022-01-17 20:48:10 +0000311
312 // Power hint session data
313
314 // Concurrent access for this is protected by mPowerHalMutex
Xiang Wange12b4fa2022-03-25 23:48:40 +0000315 sp<hardware::power::IPowerHintSession> mPowerHintSession = nullptr;
316 // Queue of actual durations saved to report
317 std::vector<hardware::power::WorkDuration> mPowerHintQueue;
Matt Buckley50c44062022-01-17 20:48:10 +0000318 // The latest values we have received for target and actual
319 nsecs_t mTargetDuration = kDefaultTarget.count();
320 std::optional<nsecs_t> mActualDuration;
Xiang Wange12b4fa2022-03-25 23:48:40 +0000321 // The list of thread ids, stored so we can restart the session from this class if needed
322 std::vector<int32_t> mPowerHintThreadIds;
Matt Buckley50c44062022-01-17 20:48:10 +0000323 bool mSupportsPowerHint = false;
Xiang Wange12b4fa2022-03-25 23:48:40 +0000324 // Keep track of the last messages sent for rate limiter change detection
Matt Buckley50c44062022-01-17 20:48:10 +0000325 std::optional<nsecs_t> mLastActualDurationSent;
326 // Timestamp of the last report we sent, used to avoid stale sessions
327 nsecs_t mLastActualReportTimestamp = 0;
328 nsecs_t mLastTargetDurationSent = kDefaultTarget.count();
329 // Max amount the error term can vary without causing an actual value report
330 nsecs_t mAllowedActualDeviation = -1;
Xiang Wange12b4fa2022-03-25 23:48:40 +0000331 // Whether we should emit ATRACE_INT data for hint sessions
332 static const bool sTraceHintSessionData;
Matt Buckley50c44062022-01-17 20:48:10 +0000333 static constexpr const std::chrono::nanoseconds kDefaultTarget = 16ms;
Xiang Wange12b4fa2022-03-25 23:48:40 +0000334 // Amount of time after the last message was sent before the session goes stale
Matt Buckleya8a45972022-07-27 22:01:27 +0000335 // actually 100ms but we use 80 here to give some slack
Xiang Wange12b4fa2022-03-25 23:48:40 +0000336 static constexpr const std::chrono::nanoseconds kStaleTimeout = 80ms;
337};
338
Michael Wright5d22d4f2018-06-21 02:50:34 +0100339} // namespace impl
340} // namespace Hwc2
341} // namespace android