blob: 919100363fd83603664388b161ecab8e53860b22 [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>
Leon Scroggins III67388622023-02-06 20:36:20 -050025#include <ui/DisplayId.h>
Dominik Laskowski62eff352021-12-06 09:59:41 -080026
Kevin DuBois1678e2c2019-08-22 12:26:24 -070027#include "VSyncTracker.h"
28
29namespace android::scheduler {
30
31class VSyncPredictor : public VSyncTracker {
32public:
33 /*
Leon Scroggins III67388622023-02-06 20:36:20 -050034 * \param [in] PhysicalDisplayid The display this corresponds to.
Ady Abrahamc585dba2023-11-15 18:41:35 -080035 * \param [in] modePtr The initial display mode
Kevin DuBois1678e2c2019-08-22 12:26:24 -070036 * \param [in] historySize The internal amount of entries to store in the model.
37 * \param [in] minimumSamplesForPrediction The minimum number of samples to collect before
38 * predicting. \param [in] outlierTolerancePercent a number 0 to 100 that will be used to filter
39 * samples that fall outlierTolerancePercent from an anticipated vsync event.
ramindanid4354a92023-10-02 15:11:09 -070040 * \param [in] IVsyncTrackerCallback The callback for the VSyncTracker.
Kevin DuBois1678e2c2019-08-22 12:26:24 -070041 */
Ady Abrahamc585dba2023-11-15 18:41:35 -080042 VSyncPredictor(ftl::NonNull<DisplayModePtr> modePtr, size_t historySize,
ramindanid4354a92023-10-02 15:11:09 -070043 size_t minimumSamplesForPrediction, uint32_t outlierTolerancePercent,
44 IVsyncTrackerCallback&);
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,
49 std::optional<nsecs_t> lastVsyncOpt = {}) const final
50 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 Abraham5cc2e262021-03-25 13:09:17 -070067 bool isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const 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
71 void setRenderRate(Fps) final EXCLUDES(mMutex);
Ady Abrahamace3d052022-11-17 16:25:05 -080072
Ady Abrahame9883032023-11-20 17:54:54 -080073 void onFrameBegin(TimePoint expectedPresentTime, TimePoint lastConfirmedPresentTime) final
74 EXCLUDES(mMutex);
75 void onFrameMissed(TimePoint expectedPresentTime) final EXCLUDES(mMutex);
76
Ady Abraham0bb6a472020-10-12 10:22:13 -070077 void dump(std::string& result) const final EXCLUDES(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -070078
Kevin DuBois1678e2c2019-08-22 12:26:24 -070079private:
80 VSyncPredictor(VSyncPredictor const&) = delete;
81 VSyncPredictor& operator=(VSyncPredictor const&) = delete;
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080082 void clearTimestamps() REQUIRES(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070083
Leon Scroggins III67388622023-02-06 20:36:20 -050084 const PhysicalDisplayId mId;
85
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080086 inline void traceInt64If(const char* name, int64_t value) const;
Ady Abrahamd9b9a042023-01-13 11:30:58 -080087 inline void traceInt64(const char* name, int64_t value) const;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080088
Ady Abrahamf34a8132023-02-13 20:49:48 -080089 size_t next(size_t i) const REQUIRES(mMutex);
90 bool validate(nsecs_t timestamp) const REQUIRES(mMutex);
91 Model getVSyncPredictionModelLocked() const REQUIRES(mMutex);
Ady Abraham4335afd2023-12-18 19:10:47 -080092 nsecs_t snapToVsync(nsecs_t timePoint) const REQUIRES(mMutex);
93 nsecs_t snapToVsyncAlignedWithRenderRate(nsecs_t timePoint) const REQUIRES(mMutex);
Ady Abrahamf34a8132023-02-13 20:49:48 -080094 bool isVSyncInPhaseLocked(nsecs_t timePoint, unsigned divisor) const REQUIRES(mMutex);
Ady Abrahame9883032023-11-20 17:54:54 -080095 Period minFramePeriodLocked() const REQUIRES(mMutex);
96 void ensureMinFrameDurationIsKept(TimePoint, TimePoint) REQUIRES(mMutex);
Ady Abrahamf34a8132023-02-13 20:49:48 -080097
98 struct VsyncSequence {
99 nsecs_t vsyncTime;
100 int64_t seq;
101 };
102 VsyncSequence getVsyncSequenceLocked(nsecs_t timestamp) const REQUIRES(mMutex);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800103 nsecs_t idealPeriod() const REQUIRES(mMutex);
Ady Abrahamf34a8132023-02-13 20:49:48 -0800104
105 bool const mTraceOn;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700106 size_t const kHistorySize;
107 size_t const kMinimumSamplesForPrediction;
108 size_t const kOutlierTolerancePercent;
ramindanid4354a92023-10-02 15:11:09 -0700109 IVsyncTrackerCallback& mVsyncTrackerCallback;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700110 std::mutex mutable mMutex;
Ady Abrahamace3d052022-11-17 16:25:05 -0800111
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700112 std::optional<nsecs_t> mKnownTimestamp GUARDED_BY(mMutex);
113
Ady Abraham0bb6a472020-10-12 10:22:13 -0700114 // Map between ideal vsync period and the calculated model
115 std::unordered_map<nsecs_t, Model> mutable mRateMap GUARDED_BY(mMutex);
116
Ady Abraham9c53ee72020-07-22 21:16:18 -0700117 size_t mLastTimestampIndex GUARDED_BY(mMutex) = 0;
Ady Abraham92fa2f42020-02-11 15:33:56 -0800118 std::vector<nsecs_t> mTimestamps GUARDED_BY(mMutex);
Ady Abrahamace3d052022-11-17 16:25:05 -0800119
Ady Abrahamc585dba2023-11-15 18:41:35 -0800120 ftl::NonNull<DisplayModePtr> mDisplayModePtr GUARDED_BY(mMutex);
121 std::optional<Fps> mRenderRateOpt GUARDED_BY(mMutex);
Ady Abrahamf34a8132023-02-13 20:49:48 -0800122
123 mutable std::optional<VsyncSequence> mLastVsyncSequence GUARDED_BY(mMutex);
Ady Abrahame9883032023-11-20 17:54:54 -0800124
125 std::deque<TimePoint> mPastExpectedPresentTimes GUARDED_BY(mMutex);
Ady Abraham4335afd2023-12-18 19:10:47 -0800126
127 TimePoint mLastMissedVsync GUARDED_BY(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700128};
129
130} // namespace android::scheduler