| 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 |  | 
|  | 19 | #include <utils/Timers.h> | 
| Dominik Laskowski | f6b4ba6 | 2021-11-09 12:46:10 -0800 | [diff] [blame] | 20 |  | 
|  | 21 | #include <scheduler/Fps.h> | 
|  | 22 |  | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 23 | #include "VSyncDispatch.h" | 
|  | 24 |  | 
|  | 25 | namespace android::scheduler { | 
|  | 26 | /* | 
|  | 27 | * VSyncTracker is an interface for providing estimates on future Vsync signal times based on | 
|  | 28 | * historical vsync timing data. | 
|  | 29 | */ | 
|  | 30 | class VSyncTracker { | 
|  | 31 | public: | 
|  | 32 | virtual ~VSyncTracker(); | 
|  | 33 |  | 
|  | 34 | /* | 
|  | 35 | * Adds a known timestamp from a vsync timing source (HWVsync signal, present fence) | 
|  | 36 | * to the model. | 
|  | 37 | * | 
|  | 38 | * \param [in] timestamp    The timestamp when the vsync signal was. | 
| Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 39 | * \return                  True if the timestamp was consistent with the internal model, | 
|  | 40 | *                          False otherwise | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 41 | */ | 
| Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 42 | virtual bool addVsyncTimestamp(nsecs_t timestamp) = 0; | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 43 |  | 
|  | 44 | /* | 
|  | 45 | * Access the next anticipated vsync time such that the anticipated time >= timePoint. | 
|  | 46 | * This will always give the best accurate at the time of calling; multiple | 
|  | 47 | * calls with the same timePoint might give differing values if the internal model | 
|  | 48 | * is updated. | 
|  | 49 | * | 
|  | 50 | * \param [in] timePoint    The point in time after which to estimate a vsync event. | 
|  | 51 | * \return                  A prediction of the timestamp of a vsync event. | 
|  | 52 | */ | 
|  | 53 | virtual nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const = 0; | 
|  | 54 |  | 
| Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 55 | /* | 
|  | 56 | * The current period of the vsync signal. | 
|  | 57 | * | 
|  | 58 | * \return  The current period of the vsync signal | 
|  | 59 | */ | 
|  | 60 | virtual nsecs_t currentPeriod() const = 0; | 
|  | 61 |  | 
| Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 62 | /* | 
|  | 63 | * Inform the tracker that the period is changing and the tracker needs to recalibrate itself. | 
|  | 64 | * | 
|  | 65 | * \param [in] period   The period that the system is changing into. | 
|  | 66 | */ | 
|  | 67 | virtual void setPeriod(nsecs_t period) = 0; | 
|  | 68 |  | 
| Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 69 | /* Inform the tracker that the samples it has are not accurate for prediction. */ | 
|  | 70 | virtual void resetModel() = 0; | 
|  | 71 |  | 
| Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 72 | virtual bool needsMoreSamples() const = 0; | 
|  | 73 |  | 
| Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 74 | /* | 
| Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 75 | * Checks if a vsync timestamp is in phase for a frame rate | 
| Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 76 | * | 
|  | 77 | * \param [in] timePoint  A vsync timestamp | 
| Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 78 | * \param [in] frameRate  The frame rate to check for | 
| Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 79 | */ | 
| Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 80 | virtual bool isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const = 0; | 
| Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 81 |  | 
| Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 82 | /* | 
|  | 83 | * Sets a divisor on the rate (which is a multiplier of the period). | 
|  | 84 | * The tracker will continue to track the vsync timeline and expect it | 
|  | 85 | * to match the current period, however, nextAnticipatedVSyncTimeFrom will | 
|  | 86 | * return vsyncs according to the divisor set. Setting a divisor is useful | 
|  | 87 | * when a display is running at 120Hz but the render frame rate is 60Hz. | 
|  | 88 | * | 
|  | 89 | * \param [in] divisor   The rate divisor the tracker should operate at. | 
|  | 90 | */ | 
|  | 91 | virtual void setDivisor(unsigned divisor) = 0; | 
|  | 92 |  | 
| Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 93 | virtual void dump(std::string& result) const = 0; | 
|  | 94 |  | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 95 | protected: | 
|  | 96 | VSyncTracker(VSyncTracker const&) = delete; | 
|  | 97 | VSyncTracker& operator=(VSyncTracker const&) = delete; | 
|  | 98 | VSyncTracker() = default; | 
|  | 99 | }; | 
|  | 100 |  | 
|  | 101 | } // namespace android::scheduler |