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