blob: 05e4c8b9307bb655f71bc1c3c571b5271134def7 [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 Wang99f6f3c2023-05-22 13:12:16 -070028#include <aidl/android/hardware/power/IPower.h>
Matt Buckley50c44062022-01-17 20:48:10 +000029#include <compositionengine/impl/OutputCompositionState.h>
Matt Buckley0538cae2022-11-08 23:12:04 +000030#include <powermanager/PowerHalController.h>
Matt Buckley2fa85012022-08-30 22:38:45 +000031#include <scheduler/Time.h>
Alec Mouriff793872022-01-13 17:45:06 -080032#include <ui/DisplayIdentification.h>
Dan Stoza030fbc12020-02-19 15:32:01 -080033#include "../Scheduler/OneShotTimer.h"
Michael Wright5d22d4f2018-06-21 02:50:34 +010034
Matt Buckleyef51fba2021-10-12 19:30:12 +000035using namespace std::chrono_literals;
36
Michael Wright5d22d4f2018-06-21 02:50:34 +010037namespace android {
Alec Mouridea1ac52021-06-23 18:12:18 -070038
39class SurfaceFlinger;
40
Michael Wright5d22d4f2018-06-21 02:50:34 +010041namespace Hwc2 {
42
43class PowerAdvisor {
44public:
45 virtual ~PowerAdvisor();
46
Alec Mouridea1ac52021-06-23 18:12:18 -070047 // Initializes resources that cannot be initialized on construction
48 virtual void init() = 0;
Dan Stoza29e7bdf2020-03-23 14:43:09 -070049 virtual void onBootFinished() = 0;
Peiyong Lin74ca2f42019-01-14 19:36:57 -080050 virtual void setExpensiveRenderingExpected(DisplayId displayId, bool expected) = 0;
Alec Mouridea1ac52021-06-23 18:12:18 -070051 virtual bool isUsingExpensiveRendering() = 0;
jimmyshiu4e211772023-06-15 15:18:38 +000052 virtual void notifyCpuLoadUp() = 0;
Matt Buckley15ecd1c2022-11-01 21:57:16 +000053 virtual void notifyDisplayUpdateImminentAndCpuReset() = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000054 // Checks both if it supports and if it's enabled
Matt Buckley06f299a2021-09-24 19:43:51 +000055 virtual bool usePowerHintSession() = 0;
56 virtual bool supportsPowerHintSession() = 0;
Matt Buckley0538cae2022-11-08 23:12:04 +000057
58 virtual bool ensurePowerHintSessionRunning() = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000059 // Sends a power hint that updates to the target work duration for the frame
Matt Buckley0538cae2022-11-08 23:12:04 +000060 virtual void updateTargetWorkDuration(Duration targetDuration) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000061 // Sends a power hint for the actual known work duration at the end of the frame
Matt Buckley0538cae2022-11-08 23:12:04 +000062 virtual void reportActualWorkDuration() = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000063 // Sets whether the power hint session is enabled
Matt Buckley0538cae2022-11-08 23:12:04 +000064 virtual void enablePowerHintSession(bool enabled) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000065 // Initializes the power hint session
Matt Buckleyef51fba2021-10-12 19:30:12 +000066 virtual bool startPowerHintSession(const std::vector<int32_t>& threadIds) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000067 // Provides PowerAdvisor with a copy of the gpu fence so it can determine the gpu end time
68 virtual void setGpuFenceTime(DisplayId displayId, std::unique_ptr<FenceTime>&& fenceTime) = 0;
Matt Buckley16dec1f2022-06-07 21:46:20 +000069 // Reports the start and end times of a hwc validate call this frame for a given display
Matt Buckley2fa85012022-08-30 22:38:45 +000070 virtual void setHwcValidateTiming(DisplayId displayId, TimePoint validateStartTime,
71 TimePoint validateEndTime) = 0;
Matt Buckley16dec1f2022-06-07 21:46:20 +000072 // Reports the start and end times of a hwc present call this frame for a given display
Matt Buckley2fa85012022-08-30 22:38:45 +000073 virtual void setHwcPresentTiming(DisplayId displayId, TimePoint presentStartTime,
74 TimePoint presentEndTime) = 0;
Matt Buckleyc6b9d382022-06-17 15:28:07 -070075 // Reports the expected time that the current frame will present to the display
Matt Buckley2fa85012022-08-30 22:38:45 +000076 virtual void setExpectedPresentTime(TimePoint expectedPresentTime) = 0;
Matt Buckley1809d902022-08-05 06:51:43 +000077 // Reports the most recent present fence time and end time once known
Matt Buckley2fa85012022-08-30 22:38:45 +000078 virtual void setSfPresentTiming(TimePoint presentFenceTime, TimePoint presentEndTime) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000079 // Reports whether a display used client composition this frame
80 virtual void setRequiresClientComposition(DisplayId displayId,
81 bool requiresClientComposition) = 0;
82 // Reports whether a given display skipped validation this frame
83 virtual void setSkippedValidate(DisplayId displayId, bool skipped) = 0;
Matt Buckley16dec1f2022-06-07 21:46:20 +000084 // Reports when a hwc present is delayed, and the time that it will resume
Matt Buckley2fa85012022-08-30 22:38:45 +000085 virtual void setHwcPresentDelayedTime(DisplayId displayId,
86 TimePoint earliestFrameStartTime) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000087 // Reports the start delay for SurfaceFlinger this frame
Matt Buckley2fa85012022-08-30 22:38:45 +000088 virtual void setFrameDelay(Duration frameDelayDuration) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000089 // Reports the SurfaceFlinger commit start time this frame
Matt Buckley2fa85012022-08-30 22:38:45 +000090 virtual void setCommitStart(TimePoint commitStartTime) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000091 // Reports the SurfaceFlinger composite end time this frame
Matt Buckley2fa85012022-08-30 22:38:45 +000092 virtual void setCompositeEnd(TimePoint compositeEndTime) = 0;
Matt Buckley50c44062022-01-17 20:48:10 +000093 // Reports the list of the currently active displays
94 virtual void setDisplays(std::vector<DisplayId>& displayIds) = 0;
95 // Sets the target duration for the entire pipeline including the gpu
Matt Buckley2fa85012022-08-30 22:38:45 +000096 virtual void setTotalFrameTargetWorkDuration(Duration targetDuration) = 0;
Michael Wright5d22d4f2018-06-21 02:50:34 +010097};
98
99namespace impl {
100
Michael Wright5d22d4f2018-06-21 02:50:34 +0100101// PowerAdvisor is a wrapper around IPower HAL which takes into account the
102// full state of the system when sending out power hints to things like the GPU.
103class PowerAdvisor final : public Hwc2::PowerAdvisor {
104public:
Alec Mouridea1ac52021-06-23 18:12:18 -0700105 PowerAdvisor(SurfaceFlinger& flinger);
Michael Wright5d22d4f2018-06-21 02:50:34 +0100106 ~PowerAdvisor() override;
107
Alec Mouridea1ac52021-06-23 18:12:18 -0700108 void init() override;
Dan Stoza29e7bdf2020-03-23 14:43:09 -0700109 void onBootFinished() override;
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800110 void setExpensiveRenderingExpected(DisplayId displayId, bool expected) override;
Matt Buckley06f299a2021-09-24 19:43:51 +0000111 bool isUsingExpensiveRendering() override { return mNotifiedExpensiveRendering; };
jimmyshiu4e211772023-06-15 15:18:38 +0000112 void notifyCpuLoadUp() override;
Matt Buckley15ecd1c2022-11-01 21:57:16 +0000113 void notifyDisplayUpdateImminentAndCpuReset() override;
Matt Buckley06f299a2021-09-24 19:43:51 +0000114 bool usePowerHintSession() override;
115 bool supportsPowerHintSession() override;
Matt Buckley0538cae2022-11-08 23:12:04 +0000116 bool ensurePowerHintSessionRunning() override;
117 void updateTargetWorkDuration(Duration targetDuration) override;
118 void reportActualWorkDuration() override;
119 void enablePowerHintSession(bool enabled) override;
Matt Buckleyef51fba2021-10-12 19:30:12 +0000120 bool startPowerHintSession(const std::vector<int32_t>& threadIds) override;
Matt Buckley50c44062022-01-17 20:48:10 +0000121 void setGpuFenceTime(DisplayId displayId, std::unique_ptr<FenceTime>&& fenceTime);
Matt Buckley2fa85012022-08-30 22:38:45 +0000122 void setHwcValidateTiming(DisplayId displayId, TimePoint validateStartTime,
123 TimePoint validateEndTime) override;
124 void setHwcPresentTiming(DisplayId displayId, TimePoint presentStartTime,
125 TimePoint presentEndTime) override;
Matt Buckley50c44062022-01-17 20:48:10 +0000126 void setSkippedValidate(DisplayId displayId, bool skipped) override;
127 void setRequiresClientComposition(DisplayId displayId, bool requiresClientComposition) override;
Matt Buckley2fa85012022-08-30 22:38:45 +0000128 void setExpectedPresentTime(TimePoint expectedPresentTime) override;
129 void setSfPresentTiming(TimePoint presentFenceTime, TimePoint presentEndTime) override;
130 void setHwcPresentDelayedTime(DisplayId displayId, TimePoint earliestFrameStartTime) override;
Matt Buckley50c44062022-01-17 20:48:10 +0000131
Matt Buckley2fa85012022-08-30 22:38:45 +0000132 void setFrameDelay(Duration frameDelayDuration) override;
133 void setCommitStart(TimePoint commitStartTime) override;
134 void setCompositeEnd(TimePoint compositeEndTime) override;
Matt Buckley50c44062022-01-17 20:48:10 +0000135 void setDisplays(std::vector<DisplayId>& displayIds) override;
Matt Buckley2fa85012022-08-30 22:38:45 +0000136 void setTotalFrameTargetWorkDuration(Duration targetDuration) override;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100137
138private:
Matt Buckley57274052022-08-12 21:54:23 +0000139 friend class PowerAdvisorTest;
140
Matt Buckley0538cae2022-11-08 23:12:04 +0000141 std::unique_ptr<power::PowerHalController> mPowerHal;
Dan Stoza29e7bdf2020-03-23 14:43:09 -0700142 std::atomic_bool mBootFinished = false;
Matt Buckleyef51fba2021-10-12 19:30:12 +0000143
Peiyong Lin74ca2f42019-01-14 19:36:57 -0800144 std::unordered_set<DisplayId> mExpensiveDisplays;
Michael Wright5d22d4f2018-06-21 02:50:34 +0100145 bool mNotifiedExpensiveRendering = false;
Dan Stoza030fbc12020-02-19 15:32:01 -0800146
Alec Mouridea1ac52021-06-23 18:12:18 -0700147 SurfaceFlinger& mFlinger;
Dan Stoza030fbc12020-02-19 15:32:01 -0800148 std::atomic_bool mSendUpdateImminent = true;
Alec Mouric059dcf2022-05-05 23:40:07 +0000149 std::atomic<nsecs_t> mLastScreenUpdatedTime = 0;
150 std::optional<scheduler::OneShotTimer> mScreenUpdateTimer;
Matt Buckley50c44062022-01-17 20:48:10 +0000151
152 // Higher-level timing data used for estimation
153 struct DisplayTimeline {
Matt Buckley16dec1f2022-06-07 21:46:20 +0000154 // The start of hwc present, or the start of validate if it happened there instead
Matt Buckley2fa85012022-08-30 22:38:45 +0000155 TimePoint hwcPresentStartTime;
Matt Buckley16dec1f2022-06-07 21:46:20 +0000156 // The end of hwc present or validate, whichever one actually presented
Matt Buckley2fa85012022-08-30 22:38:45 +0000157 TimePoint hwcPresentEndTime;
Matt Buckley16dec1f2022-06-07 21:46:20 +0000158 // How long the actual hwc present was delayed after hwcPresentStartTime
Matt Buckley2fa85012022-08-30 22:38:45 +0000159 Duration hwcPresentDelayDuration{0ns};
Matt Buckleyc6b9d382022-06-17 15:28:07 -0700160 // When we think we started waiting for the present fence after calling into hwc present and
Matt Buckley50c44062022-01-17 20:48:10 +0000161 // after potentially waiting for the earliest present time
Matt Buckley2fa85012022-08-30 22:38:45 +0000162 TimePoint presentFenceWaitStartTime;
Matt Buckley16dec1f2022-06-07 21:46:20 +0000163 // How long we ran after we finished waiting for the fence but before hwc present finished
Matt Buckley2fa85012022-08-30 22:38:45 +0000164 Duration postPresentFenceHwcPresentDuration{0ns};
Matt Buckley50c44062022-01-17 20:48:10 +0000165 // Are we likely to have waited for the present fence during composition
Matt Buckleyc6b9d382022-06-17 15:28:07 -0700166 bool probablyWaitsForPresentFence = false;
Matt Buckley50c44062022-01-17 20:48:10 +0000167 };
168
169 struct GpuTimeline {
Matt Buckley2fa85012022-08-30 22:38:45 +0000170 Duration duration{0ns};
171 TimePoint startTime;
Matt Buckley50c44062022-01-17 20:48:10 +0000172 };
173
174 // Power hint session data recorded from the pipeline
175 struct DisplayTimingData {
176 std::unique_ptr<FenceTime> gpuEndFenceTime;
Matt Buckley2fa85012022-08-30 22:38:45 +0000177 std::optional<TimePoint> gpuStartTime;
178 std::optional<TimePoint> lastValidGpuEndTime;
179 std::optional<TimePoint> lastValidGpuStartTime;
180 std::optional<TimePoint> hwcPresentStartTime;
181 std::optional<TimePoint> hwcPresentEndTime;
182 std::optional<TimePoint> hwcValidateStartTime;
183 std::optional<TimePoint> hwcValidateEndTime;
184 std::optional<TimePoint> hwcPresentDelayedTime;
Matt Buckley50c44062022-01-17 20:48:10 +0000185 bool usedClientComposition = false;
186 bool skippedValidate = false;
187 // Calculate high-level timing milestones from more granular display timing data
Matt Buckley2fa85012022-08-30 22:38:45 +0000188 DisplayTimeline calculateDisplayTimeline(TimePoint fenceTime);
Matt Buckley50c44062022-01-17 20:48:10 +0000189 // Estimate the gpu duration for a given display from previous gpu timing data
Matt Buckley2fa85012022-08-30 22:38:45 +0000190 std::optional<GpuTimeline> estimateGpuTiming(std::optional<TimePoint> previousEndTime);
Matt Buckley50c44062022-01-17 20:48:10 +0000191 };
192
193 template <class T, size_t N>
194 class RingBuffer {
195 std::array<T, N> elements = {};
196 size_t mIndex = 0;
197 size_t numElements = 0;
198
199 public:
200 void append(T item) {
201 mIndex = (mIndex + 1) % N;
202 numElements = std::min(N, numElements + 1);
203 elements[mIndex] = item;
204 }
205 bool isFull() const { return numElements == N; }
206 // Allows access like [0] == current, [-1] = previous, etc..
207 T& operator[](int offset) {
208 size_t positiveOffset =
209 static_cast<size_t>((offset % static_cast<int>(N)) + static_cast<int>(N));
210 return elements[(mIndex + positiveOffset) % N];
211 }
212 };
213
214 // Filter and sort the display ids by a given property
Matt Buckley2fa85012022-08-30 22:38:45 +0000215 std::vector<DisplayId> getOrderedDisplayIds(
216 std::optional<TimePoint> DisplayTimingData::*sortBy);
Matt Buckley50c44062022-01-17 20:48:10 +0000217 // Estimates a frame's total work duration including gpu time.
Matt Buckley0538cae2022-11-08 23:12:04 +0000218 std::optional<Duration> estimateWorkDuration();
Matt Buckley50c44062022-01-17 20:48:10 +0000219 // There are two different targets and actual work durations we care about,
220 // this normalizes them together and takes the max of the two
Matt Buckley2fa85012022-08-30 22:38:45 +0000221 Duration combineTimingEstimates(Duration totalDuration, Duration flingerDuration);
Matt Buckley50c44062022-01-17 20:48:10 +0000222
223 std::unordered_map<DisplayId, DisplayTimingData> mDisplayTimingData;
224
225 // Current frame's delay
Matt Buckley2fa85012022-08-30 22:38:45 +0000226 Duration mFrameDelayDuration{0ns};
Matt Buckley50c44062022-01-17 20:48:10 +0000227 // Last frame's post-composition duration
Matt Buckley2fa85012022-08-30 22:38:45 +0000228 Duration mLastPostcompDuration{0ns};
Matt Buckley50c44062022-01-17 20:48:10 +0000229 // Buffer of recent commit start times
Matt Buckley2fa85012022-08-30 22:38:45 +0000230 RingBuffer<TimePoint, 2> mCommitStartTimes;
Matt Buckley50c44062022-01-17 20:48:10 +0000231 // Buffer of recent expected present times
Matt Buckley2fa85012022-08-30 22:38:45 +0000232 RingBuffer<TimePoint, 2> mExpectedPresentTimes;
Matt Buckleya2ad1dc2022-08-16 20:07:26 +0000233 // Most recent present fence time, provided by SF after composition engine finishes presenting
Matt Buckley2fa85012022-08-30 22:38:45 +0000234 TimePoint mLastPresentFenceTime;
Matt Buckleya2ad1dc2022-08-16 20:07:26 +0000235 // Most recent composition engine present end time, returned with the present fence from SF
Matt Buckley2fa85012022-08-30 22:38:45 +0000236 TimePoint mLastSfPresentEndTime;
Matt Buckleya2ad1dc2022-08-16 20:07:26 +0000237 // Target duration for the entire pipeline including gpu
Matt Buckley2fa85012022-08-30 22:38:45 +0000238 std::optional<Duration> mTotalFrameTargetDuration;
Matt Buckley50c44062022-01-17 20:48:10 +0000239 // Updated list of display IDs
240 std::vector<DisplayId> mDisplayIds;
241
Matt Buckley0538cae2022-11-08 23:12:04 +0000242 // Ensure powerhal connection is initialized
243 power::PowerHalController& getPowerHal();
244
245 std::optional<bool> mHintSessionEnabled;
246 std::optional<bool> mSupportsHintSession;
247 bool mHintSessionRunning = false;
248
249 std::mutex mHintSessionMutex;
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700250 std::shared_ptr<aidl::android::hardware::power::IPowerHintSession> mHintSession
251 GUARDED_BY(mHintSessionMutex) = nullptr;
Matt Buckley0538cae2022-11-08 23:12:04 +0000252
253 // Initialize to true so we try to call, to check if it's supported
254 bool mHasExpensiveRendering = true;
255 bool mHasDisplayUpdateImminent = true;
256 // Queue of actual durations saved to report
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700257 std::vector<aidl::android::hardware::power::WorkDuration> mHintSessionQueue;
Matt Buckley0538cae2022-11-08 23:12:04 +0000258 // The latest values we have received for target and actual
259 Duration mTargetDuration = kDefaultTargetDuration;
260 std::optional<Duration> mActualDuration;
261 // The list of thread ids, stored so we can restart the session from this class if needed
262 std::vector<int32_t> mHintSessionThreadIds;
263 Duration mLastTargetDurationSent = kDefaultTargetDuration;
264 // Whether we should emit ATRACE_INT data for hint sessions
265 static const bool sTraceHintSessionData;
266
267 // Default target duration for the hint session
268 static constexpr const Duration kDefaultTargetDuration{16ms};
Matt Buckley50c44062022-01-17 20:48:10 +0000269
Matt Buckleyc6b9d382022-06-17 15:28:07 -0700270 // An adjustable safety margin which pads the "actual" value sent to PowerHAL,
271 // encouraging more aggressive boosting to give SurfaceFlinger a larger margin for error
Matt Buckleyac15a1b2023-02-28 06:51:28 +0000272 static const Duration sTargetSafetyMargin;
273 static constexpr const Duration kDefaultTargetSafetyMargin{1ms};
Matt Buckley50c44062022-01-17 20:48:10 +0000274
Matt Buckley676e4392023-05-25 22:09:26 +0000275 // Whether we should send reportActualWorkDuration calls
276 static const bool sUseReportActualDuration;
277
Matt Buckley50c44062022-01-17 20:48:10 +0000278 // How long we expect hwc to run after the present call until it waits for the fence
Matt Buckley2fa85012022-08-30 22:38:45 +0000279 static constexpr const Duration kFenceWaitStartDelayValidated{150us};
280 static constexpr const Duration kFenceWaitStartDelaySkippedValidate{250us};
Michael Wright5d22d4f2018-06-21 02:50:34 +0100281};
282
Michael Wright5d22d4f2018-06-21 02:50:34 +0100283} // namespace impl
284} // namespace Hwc2
285} // namespace android