blob: 417163f4e18f35ff0e264733f3aa4ebed0934d27 [file] [log] [blame]
Kevin DuBois305bef12019-10-09 13:23:27 -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
ramindanid4354a92023-10-02 15:11:09 -070019#include <ui/DisplayId.h>
Kevin DuBois305bef12019-10-09 13:23:27 -070020#include <utils/Timers.h>
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080021
22#include <scheduler/Fps.h>
Ady Abrahamc585dba2023-11-15 18:41:35 -080023#include <scheduler/FrameRateMode.h>
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080024
Kevin DuBois305bef12019-10-09 13:23:27 -070025#include "VSyncDispatch.h"
26
27namespace android::scheduler {
ramindanid4354a92023-10-02 15:11:09 -070028
ramindanid4354a92023-10-02 15:11:09 -070029struct IVsyncTrackerCallback {
30 virtual ~IVsyncTrackerCallback() = default;
Ady Abrahamc585dba2023-11-15 18:41:35 -080031 virtual void onVsyncGenerated(TimePoint expectedPresentTime, ftl::NonNull<DisplayModePtr>,
32 Fps renderRate) = 0;
ramindanid4354a92023-10-02 15:11:09 -070033};
34
Kevin DuBois305bef12019-10-09 13:23:27 -070035/*
36 * VSyncTracker is an interface for providing estimates on future Vsync signal times based on
37 * historical vsync timing data.
38 */
39class VSyncTracker {
40public:
41 virtual ~VSyncTracker();
42
43 /*
44 * Adds a known timestamp from a vsync timing source (HWVsync signal, present fence)
45 * to the model.
46 *
47 * \param [in] timestamp The timestamp when the vsync signal was.
Kevin DuBois02d5ed92020-01-27 11:05:46 -080048 * \return True if the timestamp was consistent with the internal model,
49 * False otherwise
Kevin DuBois305bef12019-10-09 13:23:27 -070050 */
Kevin DuBois02d5ed92020-01-27 11:05:46 -080051 virtual bool addVsyncTimestamp(nsecs_t timestamp) = 0;
Kevin DuBois305bef12019-10-09 13:23:27 -070052
53 /*
54 * Access the next anticipated vsync time such that the anticipated time >= timePoint.
55 * This will always give the best accurate at the time of calling; multiple
56 * calls with the same timePoint might give differing values if the internal model
57 * is updated.
58 *
59 * \param [in] timePoint The point in time after which to estimate a vsync event.
Ady Abraham4335afd2023-12-18 19:10:47 -080060 * \param [in] lastVsyncOpt The last vsync time used by the client. If provided, the tracker
61 * should use that as a reference point when generating the new vsync
62 * and avoid crossing the minimal frame period of a VRR display.
Kevin DuBois305bef12019-10-09 13:23:27 -070063 * \return A prediction of the timestamp of a vsync event.
64 */
Ady Abraham4335afd2023-12-18 19:10:47 -080065 virtual nsecs_t nextAnticipatedVSyncTimeFrom(
66 nsecs_t timePoint, std::optional<nsecs_t> lastVsyncOpt = {}) const = 0;
Kevin DuBois305bef12019-10-09 13:23:27 -070067
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080068 /*
69 * The current period of the vsync signal.
70 *
71 * \return The current period of the vsync signal
72 */
73 virtual nsecs_t currentPeriod() const = 0;
74
Ady Abraham3db8a3c2023-11-20 17:53:47 -080075 /*
76 * The minimal period frames can be displayed.
77 */
78 virtual Period minFramePeriod() const = 0;
79
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080080 /* Inform the tracker that the samples it has are not accurate for prediction. */
81 virtual void resetModel() = 0;
82
Kevin DuBoisb818bfa2020-07-10 14:29:36 -070083 virtual bool needsMoreSamples() const = 0;
84
Ady Abraham0bb6a472020-10-12 10:22:13 -070085 /*
Ady Abraham5cc2e262021-03-25 13:09:17 -070086 * Checks if a vsync timestamp is in phase for a frame rate
Ady Abraham0bb6a472020-10-12 10:22:13 -070087 *
88 * \param [in] timePoint A vsync timestamp
Ady Abraham5cc2e262021-03-25 13:09:17 -070089 * \param [in] frameRate The frame rate to check for
Ady Abraham0bb6a472020-10-12 10:22:13 -070090 */
Ady Abraham5cc2e262021-03-25 13:09:17 -070091 virtual bool isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const = 0;
Ady Abraham0bb6a472020-10-12 10:22:13 -070092
Ady Abrahamace3d052022-11-17 16:25:05 -080093 /*
Ady Abrahamc585dba2023-11-15 18:41:35 -080094 * 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.
Ady Abrahamace3d052022-11-17 16:25:05 -0800105 * The tracker will continue to track the vsync timeline and expect it
106 * to match the current period, however, nextAnticipatedVSyncTimeFrom will
Ady Abrahamfdc049c2023-02-17 14:52:05 +0000107 * return vsyncs according to the render rate set. Setting a render rate is useful
Ady Abrahamace3d052022-11-17 16:25:05 -0800108 * when a display is running at 120Hz but the render frame rate is 60Hz.
109 *
Ady Abrahamc585dba2023-11-15 18:41:35 -0800110 * \param [in] Fps The render rate the tracker should operate at.
Ady Abrahamace3d052022-11-17 16:25:05 -0800111 */
Ady Abrahamc585dba2023-11-15 18:41:35 -0800112 virtual void setRenderRate(Fps) = 0;
Ady Abrahamace3d052022-11-17 16:25:05 -0800113
Ady Abrahame9883032023-11-20 17:54:54 -0800114 virtual void onFrameBegin(TimePoint expectedPresentTime,
115 TimePoint lastConfirmedPresentTime) = 0;
116
117 virtual void onFrameMissed(TimePoint expectedPresentTime) = 0;
118
Ady Abraham5e7371c2020-03-24 14:47:24 -0700119 virtual void dump(std::string& result) const = 0;
120
Kevin DuBois305bef12019-10-09 13:23:27 -0700121protected:
122 VSyncTracker(VSyncTracker const&) = delete;
123 VSyncTracker& operator=(VSyncTracker const&) = delete;
124 VSyncTracker() = default;
125};
126
127} // namespace android::scheduler