blob: 7eedc312e2dffe3152e5b73160426a7552f49133 [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>
23
Kevin DuBois305bef12019-10-09 13:23:27 -070024#include "VSyncDispatch.h"
25
26namespace android::scheduler {
ramindanid4354a92023-10-02 15:11:09 -070027
28struct DisplayModeData {
29 Fps renderRate;
30 std::optional<Period> notifyExpectedPresentTimeoutOpt;
31
32 bool operator==(const DisplayModeData& other) const {
33 return isApproxEqual(renderRate, other.renderRate) &&
34 notifyExpectedPresentTimeoutOpt == other.notifyExpectedPresentTimeoutOpt;
35 }
36};
37
38struct IVsyncTrackerCallback {
39 virtual ~IVsyncTrackerCallback() = default;
40 virtual void onVsyncGenerated(PhysicalDisplayId, TimePoint expectedPresentTime,
41 const DisplayModeData&, Period vsyncPeriod) = 0;
42};
43
Kevin DuBois305bef12019-10-09 13:23:27 -070044/*
45 * VSyncTracker is an interface for providing estimates on future Vsync signal times based on
46 * historical vsync timing data.
47 */
48class VSyncTracker {
49public:
50 virtual ~VSyncTracker();
51
52 /*
53 * Adds a known timestamp from a vsync timing source (HWVsync signal, present fence)
54 * to the model.
55 *
56 * \param [in] timestamp The timestamp when the vsync signal was.
Kevin DuBois02d5ed92020-01-27 11:05:46 -080057 * \return True if the timestamp was consistent with the internal model,
58 * False otherwise
Kevin DuBois305bef12019-10-09 13:23:27 -070059 */
Kevin DuBois02d5ed92020-01-27 11:05:46 -080060 virtual bool addVsyncTimestamp(nsecs_t timestamp) = 0;
Kevin DuBois305bef12019-10-09 13:23:27 -070061
62 /*
63 * Access the next anticipated vsync time such that the anticipated time >= timePoint.
64 * This will always give the best accurate at the time of calling; multiple
65 * calls with the same timePoint might give differing values if the internal model
66 * is updated.
67 *
68 * \param [in] timePoint The point in time after which to estimate a vsync event.
69 * \return A prediction of the timestamp of a vsync event.
70 */
71 virtual nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const = 0;
72
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080073 /*
74 * The current period of the vsync signal.
75 *
76 * \return The current period of the vsync signal
77 */
78 virtual nsecs_t currentPeriod() const = 0;
79
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080080 /*
81 * Inform the tracker that the period is changing and the tracker needs to recalibrate itself.
82 *
83 * \param [in] period The period that the system is changing into.
84 */
85 virtual void setPeriod(nsecs_t period) = 0;
86
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080087 /* Inform the tracker that the samples it has are not accurate for prediction. */
88 virtual void resetModel() = 0;
89
Kevin DuBoisb818bfa2020-07-10 14:29:36 -070090 virtual bool needsMoreSamples() const = 0;
91
Ady Abraham0bb6a472020-10-12 10:22:13 -070092 /*
Ady Abraham5cc2e262021-03-25 13:09:17 -070093 * Checks if a vsync timestamp is in phase for a frame rate
Ady Abraham0bb6a472020-10-12 10:22:13 -070094 *
95 * \param [in] timePoint A vsync timestamp
Ady Abraham5cc2e262021-03-25 13:09:17 -070096 * \param [in] frameRate The frame rate to check for
Ady Abraham0bb6a472020-10-12 10:22:13 -070097 */
Ady Abraham5cc2e262021-03-25 13:09:17 -070098 virtual bool isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const = 0;
Ady Abraham0bb6a472020-10-12 10:22:13 -070099
Ady Abrahamace3d052022-11-17 16:25:05 -0800100 /*
ramindanid4354a92023-10-02 15:11:09 -0700101 * Sets the metadata about the currently active display mode such as VRR
102 * timeout period, vsyncPeriod and framework property such as render rate.
103 * If the render rate is not a divisor of the period, the render rate is
104 * 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.
ramindanid4354a92023-10-02 15:11:09 -0700109 * When IVsyncTrackerCallback::onVsyncGenerated callback is made we will pass along
110 * the vsyncPeriod, render rate and timeoutNs.
Ady Abrahamace3d052022-11-17 16:25:05 -0800111 *
ramindanid4354a92023-10-02 15:11:09 -0700112 * \param [in] DisplayModeData The DisplayModeData the tracker will use.
Ady Abrahamace3d052022-11-17 16:25:05 -0800113 */
ramindanid4354a92023-10-02 15:11:09 -0700114 virtual void setDisplayModeData(const DisplayModeData&) = 0;
Ady Abrahamace3d052022-11-17 16:25:05 -0800115
Ady Abraham5e7371c2020-03-24 14:47:24 -0700116 virtual void dump(std::string& result) const = 0;
117
Kevin DuBois305bef12019-10-09 13:23:27 -0700118protected:
119 VSyncTracker(VSyncTracker const&) = delete;
120 VSyncTracker& operator=(VSyncTracker const&) = delete;
121 VSyncTracker() = default;
122};
123
124} // namespace android::scheduler