blob: 66a7d71435969398a0076bf69c1d125273f0074c [file] [log] [blame]
Kevin DuBois1678e2c2019-08-22 12:26:24 -07001/*
2 * Copyright 2019 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
Ady Abrahame9883032023-11-20 17:54:54 -080019#include <deque>
Kevin DuBois1678e2c2019-08-22 12:26:24 -070020#include <mutex>
21#include <unordered_map>
22#include <vector>
Dominik Laskowski62eff352021-12-06 09:59:41 -080023
24#include <android-base/thread_annotations.h>
Ady Abraham20024aa2024-03-05 01:32:49 +000025#include <scheduler/TimeKeeper.h>
Leon Scroggins III67388622023-02-06 20:36:20 -050026#include <ui/DisplayId.h>
Dominik Laskowski62eff352021-12-06 09:59:41 -080027
Kevin DuBois1678e2c2019-08-22 12:26:24 -070028#include "VSyncTracker.h"
29
30namespace android::scheduler {
31
32class VSyncPredictor : public VSyncTracker {
33public:
34 /*
Ady Abraham20024aa2024-03-05 01:32:49 +000035 * \param [in] Clock The clock abstraction. Useful for unit tests.
Leon Scroggins III67388622023-02-06 20:36:20 -050036 * \param [in] PhysicalDisplayid The display this corresponds to.
Ady Abrahamc585dba2023-11-15 18:41:35 -080037 * \param [in] modePtr The initial display mode
Kevin DuBois1678e2c2019-08-22 12:26:24 -070038 * \param [in] historySize The internal amount of entries to store in the model.
39 * \param [in] minimumSamplesForPrediction The minimum number of samples to collect before
40 * predicting. \param [in] outlierTolerancePercent a number 0 to 100 that will be used to filter
41 * samples that fall outlierTolerancePercent from an anticipated vsync event.
42 */
Ady Abraham20024aa2024-03-05 01:32:49 +000043 VSyncPredictor(std::unique_ptr<Clock>, ftl::NonNull<DisplayModePtr> modePtr, size_t historySize,
ramindaniae645822024-01-11 10:57:29 -080044 size_t minimumSamplesForPrediction, uint32_t outlierTolerancePercent);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070045 ~VSyncPredictor();
46
Ady Abraham0bb6a472020-10-12 10:22:13 -070047 bool addVsyncTimestamp(nsecs_t timestamp) final EXCLUDES(mMutex);
Ady Abraham4335afd2023-12-18 19:10:47 -080048 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint,
Ady Abraham20024aa2024-03-05 01:32:49 +000049 std::optional<nsecs_t> lastVsyncOpt = {}) final
Ady Abraham4335afd2023-12-18 19:10:47 -080050 EXCLUDES(mMutex);
Ady Abraham0bb6a472020-10-12 10:22:13 -070051 nsecs_t currentPeriod() const final EXCLUDES(mMutex);
Ady Abraham3db8a3c2023-11-20 17:53:47 -080052 Period minFramePeriod() const final EXCLUDES(mMutex);
Ady Abraham0bb6a472020-10-12 10:22:13 -070053 void resetModel() final EXCLUDES(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070054
Kevin DuBoisb818bfa2020-07-10 14:29:36 -070055 /* Query if the model is in need of more samples to make a prediction.
Kevin DuBois1678e2c2019-08-22 12:26:24 -070056 * \return True, if model would benefit from more samples, False if not.
57 */
Ady Abraham0bb6a472020-10-12 10:22:13 -070058 bool needsMoreSamples() const final EXCLUDES(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070059
Ady Abraham0bb6a472020-10-12 10:22:13 -070060 struct Model {
61 nsecs_t slope;
62 nsecs_t intercept;
63 };
Kevin DuBois1678e2c2019-08-22 12:26:24 -070064
Ady Abraham0bb6a472020-10-12 10:22:13 -070065 VSyncPredictor::Model getVSyncPredictionModel() const EXCLUDES(mMutex);
66
Ady Abraham20024aa2024-03-05 01:32:49 +000067 bool isVSyncInPhase(nsecs_t timePoint, Fps frameRate) final EXCLUDES(mMutex);
Ady Abraham0bb6a472020-10-12 10:22:13 -070068
Ady Abrahamc585dba2023-11-15 18:41:35 -080069 void setDisplayModePtr(ftl::NonNull<DisplayModePtr>) final EXCLUDES(mMutex);
70
ramindanid17261e2024-03-27 17:50:25 -070071 bool isCurrentMode(const ftl::NonNull<DisplayModePtr>& modePtr) const EXCLUDES(mMutex) {
72 std::lock_guard lock(mMutex);
73 return mDisplayModePtr->getId() == modePtr->getId() &&
74 mDisplayModePtr->getVsyncRate().getPeriodNsecs() ==
75 mRateMap.find(idealPeriod())->second.slope;
76 }
77
Ady Abrahamee6365b2024-03-06 14:31:45 -080078 void setRenderRate(Fps, bool applyImmediately) final EXCLUDES(mMutex);
Ady Abrahamace3d052022-11-17 16:25:05 -080079
Ady Abrahame9883032023-11-20 17:54:54 -080080 void onFrameBegin(TimePoint expectedPresentTime, TimePoint lastConfirmedPresentTime) final
81 EXCLUDES(mMutex);
82 void onFrameMissed(TimePoint expectedPresentTime) final EXCLUDES(mMutex);
83
Ady Abraham0bb6a472020-10-12 10:22:13 -070084 void dump(std::string& result) const final EXCLUDES(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -070085
Kevin DuBois1678e2c2019-08-22 12:26:24 -070086private:
Ady Abraham20024aa2024-03-05 01:32:49 +000087 struct VsyncSequence {
88 nsecs_t vsyncTime;
89 int64_t seq;
90 };
91
92 struct MissedVsync {
Ady Abrahamc5d72462024-03-23 23:56:33 +000093 TimePoint vsync = TimePoint::fromNs(0);
Ady Abraham20024aa2024-03-05 01:32:49 +000094 Duration fixup = Duration::fromNs(0);
95 };
96
97 class VsyncTimeline {
98 public:
Ady Abraham77b4fb12024-03-05 17:51:53 -080099 VsyncTimeline(TimePoint knownVsync, Period idealPeriod, std::optional<Fps> renderRateOpt);
Ady Abraham20024aa2024-03-05 01:32:49 +0000100 std::optional<TimePoint> nextAnticipatedVSyncTimeFrom(
Ady Abraham940b7a62024-03-07 10:04:27 -0800101 Model model, std::optional<Period> minFramePeriodOpt, nsecs_t vsyncTime,
102 MissedVsync lastMissedVsync, std::optional<nsecs_t> lastVsyncOpt = {});
Ady Abraham20024aa2024-03-05 01:32:49 +0000103 void freeze(TimePoint lastVsync);
104 std::optional<TimePoint> validUntil() const { return mValidUntil; }
105 bool isVSyncInPhase(Model, nsecs_t vsync, Fps frameRate);
106 void shiftVsyncSequence(Duration phase);
Ady Abrahamc5d72462024-03-23 23:56:33 +0000107 void setRenderRate(std::optional<Fps> renderRateOpt) { mRenderRateOpt = renderRateOpt; }
Ady Abraham20024aa2024-03-05 01:32:49 +0000108
ramindani548f4492024-06-13 10:29:04 -0700109 enum class VsyncOnTimeline {
110 Unique, // Within timeline, not shared with next timeline.
111 Shared, // Within timeline, shared with next timeline.
112 Outside, // Outside of the timeline.
113 };
114 VsyncOnTimeline isWithin(TimePoint vsync) {
115 const auto threshold = mIdealPeriod.ns() / 2;
116 if (!mValidUntil || vsync.ns() < mValidUntil->ns() - threshold) {
117 // if mValidUntil is absent then timeline is not frozen and
118 // vsync should be unique to that timeline.
119 return VsyncOnTimeline::Unique;
120 }
121 if (vsync.ns() > mValidUntil->ns() + threshold) {
122 return VsyncOnTimeline::Outside;
123 }
124 return VsyncOnTimeline::Shared;
125 }
126
Ady Abraham20024aa2024-03-05 01:32:49 +0000127 private:
128 nsecs_t snapToVsyncAlignedWithRenderRate(Model model, nsecs_t vsync);
129 VsyncSequence getVsyncSequenceLocked(Model, nsecs_t vsync);
Ady Abraham77b4fb12024-03-05 17:51:53 -0800130 std::optional<VsyncSequence> makeVsyncSequence(TimePoint knownVsync);
Ady Abraham20024aa2024-03-05 01:32:49 +0000131
132 const Period mIdealPeriod = Duration::fromNs(0);
Ady Abraham4fc2fce2024-03-08 06:43:44 +0000133 std::optional<Fps> mRenderRateOpt;
Ady Abraham20024aa2024-03-05 01:32:49 +0000134 std::optional<TimePoint> mValidUntil;
135 std::optional<VsyncSequence> mLastVsyncSequence;
136 };
137
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700138 VSyncPredictor(VSyncPredictor const&) = delete;
139 VSyncPredictor& operator=(VSyncPredictor const&) = delete;
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800140 void clearTimestamps() REQUIRES(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700141
Ady Abraham20024aa2024-03-05 01:32:49 +0000142 const std::unique_ptr<Clock> mClock;
Leon Scroggins III67388622023-02-06 20:36:20 -0500143 const PhysicalDisplayId mId;
144
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800145 inline void traceInt64If(const char* name, int64_t value) const;
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800146 inline void traceInt64(const char* name, int64_t value) const;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800147
Ady Abrahamf34a8132023-02-13 20:49:48 -0800148 size_t next(size_t i) const REQUIRES(mMutex);
149 bool validate(nsecs_t timestamp) const REQUIRES(mMutex);
150 Model getVSyncPredictionModelLocked() const REQUIRES(mMutex);
Ady Abraham4335afd2023-12-18 19:10:47 -0800151 nsecs_t snapToVsync(nsecs_t timePoint) const REQUIRES(mMutex);
Ady Abrahame9883032023-11-20 17:54:54 -0800152 Period minFramePeriodLocked() const REQUIRES(mMutex);
Ady Abraham20024aa2024-03-05 01:32:49 +0000153 Duration ensureMinFrameDurationIsKept(TimePoint, TimePoint) REQUIRES(mMutex);
154 void purgeTimelines(android::TimePoint now) REQUIRES(mMutex);
Ady Abrahamf34a8132023-02-13 20:49:48 -0800155
Ady Abrahamc585dba2023-11-15 18:41:35 -0800156 nsecs_t idealPeriod() const REQUIRES(mMutex);
Ady Abrahamf34a8132023-02-13 20:49:48 -0800157
158 bool const mTraceOn;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700159 size_t const kHistorySize;
160 size_t const kMinimumSamplesForPrediction;
161 size_t const kOutlierTolerancePercent;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700162 std::mutex mutable mMutex;
Ady Abrahamace3d052022-11-17 16:25:05 -0800163
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700164 std::optional<nsecs_t> mKnownTimestamp GUARDED_BY(mMutex);
165
Ady Abraham0bb6a472020-10-12 10:22:13 -0700166 // Map between ideal vsync period and the calculated model
167 std::unordered_map<nsecs_t, Model> mutable mRateMap GUARDED_BY(mMutex);
168
Ady Abraham9c53ee72020-07-22 21:16:18 -0700169 size_t mLastTimestampIndex GUARDED_BY(mMutex) = 0;
Ady Abraham92fa2f42020-02-11 15:33:56 -0800170 std::vector<nsecs_t> mTimestamps GUARDED_BY(mMutex);
Ady Abrahamace3d052022-11-17 16:25:05 -0800171
Ady Abrahamc585dba2023-11-15 18:41:35 -0800172 ftl::NonNull<DisplayModePtr> mDisplayModePtr GUARDED_BY(mMutex);
Ady Abraham940b7a62024-03-07 10:04:27 -0800173 int mNumVsyncsForFrame GUARDED_BY(mMutex);
Ady Abrahame9883032023-11-20 17:54:54 -0800174
175 std::deque<TimePoint> mPastExpectedPresentTimes GUARDED_BY(mMutex);
Ady Abraham4335afd2023-12-18 19:10:47 -0800176
Ady Abraham20024aa2024-03-05 01:32:49 +0000177 MissedVsync mMissedVsync GUARDED_BY(mMutex);
178
179 std::deque<VsyncTimeline> mTimelines GUARDED_BY(mMutex);
180 TimePoint mLastCommittedVsync GUARDED_BY(mMutex) = TimePoint::fromNs(0);
181 Period mIdealPeriod GUARDED_BY(mMutex) = Duration::fromNs(0);
182 std::optional<Fps> mRenderRateOpt GUARDED_BY(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700183};
184
185} // namespace android::scheduler