xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/Scheduler/VSyncTracker.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
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 
19 #include <ui/DisplayId.h>
20 #include <utils/Timers.h>
21 
22 #include <scheduler/Fps.h>
23 #include <scheduler/FrameRateMode.h>
24 #include <scheduler/FrameTime.h>
25 
26 #include "VSyncDispatch.h"
27 
28 namespace android::scheduler {
29 
30 /*
31  * VSyncTracker is an interface for providing estimates on future Vsync signal times based on
32  * historical vsync timing data.
33  */
34 class VSyncTracker {
35 public:
36     virtual ~VSyncTracker();
37 
38     /*
39      * Adds a known timestamp from a vsync timing source (HWVsync signal, present fence)
40      * to the model.
41      *
42      * \param [in] timestamp    The timestamp when the vsync signal was.
43      * \return                  True if the timestamp was consistent with the internal model,
44      *                          False otherwise
45      */
46     virtual bool addVsyncTimestamp(nsecs_t timestamp) = 0;
47 
48     /*
49      * Access the next anticipated vsync time such that the anticipated time >= timePoint.
50      * This will always give the best accurate at the time of calling; multiple
51      * calls with the same timePoint might give differing values if the internal model
52      * is updated.
53      *
54      * \param [in] timePoint    The point in time after which to estimate a vsync event.
55      * \param [in] lastVsyncOpt The last vsync time used by the client. If provided, the tracker
56      *                          should use that as a reference point when generating the new vsync
57      *                          and avoid crossing the minimal frame period of a VRR display.
58      * \return                  A prediction of the timestamp of a vsync event.
59      */
60     virtual nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint,
61                                                  std::optional<nsecs_t> lastVsyncOpt = {}) = 0;
62 
63     /*
64      * The current period of the vsync signal.
65      *
66      * \return  The current period of the vsync signal
67      */
68     virtual nsecs_t currentPeriod() const = 0;
69 
70     /*
71      * The minimal period frames can be displayed.
72      */
73     virtual Period minFramePeriod() const = 0;
74 
75     /**
76      * Checks if the sourced mode is equal to the mode in the tracker.
77      */
78     virtual bool isCurrentMode(const ftl::NonNull<DisplayModePtr>& modePtr) const = 0;
79 
80     /* Inform the tracker that the samples it has are not accurate for prediction. */
81     virtual void resetModel() = 0;
82 
83     virtual bool needsMoreSamples() const = 0;
84 
85     /*
86      * Checks if a vsync timestamp is in phase for a frame rate
87      *
88      * \param [in] timePoint  A vsync timestamp
89      * \param [in] frameRate  The frame rate to check for
90      */
91     virtual bool isVSyncInPhase(nsecs_t timePoint, Fps frameRate) = 0;
92 
93     /*
94      * Sets the active mode of the display which includes the vsync period and other VRR attributes.
95      * This will inform the tracker that the period is changing and the tracker needs to recalibrate
96      * itself.
97      *
98      * \param [in] DisplayModePtr The display mode the tracker will use.
99      */
100     virtual void setDisplayModePtr(ftl::NonNull<DisplayModePtr>) = 0;
101 
102     /*
103      * Sets a render rate on the tracker. If the render rate is not a divisor
104      * of the period, the render rate is ignored until the period changes.
105      * The tracker will continue to track the vsync timeline and expect it
106      * to match the current period, however, nextAnticipatedVSyncTimeFrom will
107      * return vsyncs according to the render rate set. Setting a render rate is useful
108      * when a display is running at 120Hz but the render frame rate is 60Hz.
109      *
110      * \param [in] Fps   The render rate the tracker should operate at.
111      * \param [in] applyImmediately Whether to apply the new render rate immediately regardless of
112      *                              already committed vsyncs.
113      */
114     virtual void setRenderRate(Fps, bool applyImmediately) = 0;
115 
116     virtual void onFrameBegin(TimePoint expectedPresentTime, FrameTime lastSignaledFrameTime) = 0;
117 
118     virtual void onFrameMissed(TimePoint expectedPresentTime) = 0;
119 
120     virtual void dump(std::string& result) const = 0;
121 
122 protected:
123     VSyncTracker(VSyncTracker const&) = delete;
124     VSyncTracker& operator=(VSyncTracker const&) = delete;
125     VSyncTracker() = default;
126 };
127 
128 } // namespace android::scheduler
129