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