Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -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 | |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 19 | #include <deque> |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 20 | #include <mutex> |
| 21 | #include <unordered_map> |
| 22 | #include <vector> |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 23 | |
| 24 | #include <android-base/thread_annotations.h> |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 25 | #include <ui/DisplayId.h> |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 26 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 27 | #include "VSyncTracker.h" |
| 28 | |
| 29 | namespace android::scheduler { |
| 30 | |
| 31 | class VSyncPredictor : public VSyncTracker { |
| 32 | public: |
| 33 | /* |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 34 | * \param [in] PhysicalDisplayid The display this corresponds to. |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 35 | * \param [in] modePtr The initial display mode |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 36 | * \param [in] historySize The internal amount of entries to store in the model. |
| 37 | * \param [in] minimumSamplesForPrediction The minimum number of samples to collect before |
| 38 | * predicting. \param [in] outlierTolerancePercent a number 0 to 100 that will be used to filter |
| 39 | * samples that fall outlierTolerancePercent from an anticipated vsync event. |
| 40 | */ |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 41 | VSyncPredictor(ftl::NonNull<DisplayModePtr> modePtr, size_t historySize, |
ramindani | ae64582 | 2024-01-11 10:57:29 -0800 | [diff] [blame^] | 42 | size_t minimumSamplesForPrediction, uint32_t outlierTolerancePercent); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 43 | ~VSyncPredictor(); |
| 44 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 45 | bool addVsyncTimestamp(nsecs_t timestamp) final EXCLUDES(mMutex); |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 46 | nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint, |
| 47 | std::optional<nsecs_t> lastVsyncOpt = {}) const final |
| 48 | EXCLUDES(mMutex); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 49 | nsecs_t currentPeriod() const final EXCLUDES(mMutex); |
Ady Abraham | 3db8a3c | 2023-11-20 17:53:47 -0800 | [diff] [blame] | 50 | Period minFramePeriod() const final EXCLUDES(mMutex); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 51 | void resetModel() final EXCLUDES(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 52 | |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 53 | /* Query if the model is in need of more samples to make a prediction. |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 54 | * \return True, if model would benefit from more samples, False if not. |
| 55 | */ |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 56 | bool needsMoreSamples() const final EXCLUDES(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 57 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 58 | struct Model { |
| 59 | nsecs_t slope; |
| 60 | nsecs_t intercept; |
| 61 | }; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 62 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 63 | VSyncPredictor::Model getVSyncPredictionModel() const EXCLUDES(mMutex); |
| 64 | |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 65 | bool isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const final EXCLUDES(mMutex); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 66 | |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 67 | void setDisplayModePtr(ftl::NonNull<DisplayModePtr>) final EXCLUDES(mMutex); |
| 68 | |
| 69 | void setRenderRate(Fps) final EXCLUDES(mMutex); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 70 | |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 71 | void onFrameBegin(TimePoint expectedPresentTime, TimePoint lastConfirmedPresentTime) final |
| 72 | EXCLUDES(mMutex); |
| 73 | void onFrameMissed(TimePoint expectedPresentTime) final EXCLUDES(mMutex); |
| 74 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 75 | void dump(std::string& result) const final EXCLUDES(mMutex); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 76 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 77 | private: |
| 78 | VSyncPredictor(VSyncPredictor const&) = delete; |
| 79 | VSyncPredictor& operator=(VSyncPredictor const&) = delete; |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 80 | void clearTimestamps() REQUIRES(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 81 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 82 | const PhysicalDisplayId mId; |
| 83 | |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 84 | inline void traceInt64If(const char* name, int64_t value) const; |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 85 | inline void traceInt64(const char* name, int64_t value) const; |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 86 | |
Ady Abraham | f34a813 | 2023-02-13 20:49:48 -0800 | [diff] [blame] | 87 | size_t next(size_t i) const REQUIRES(mMutex); |
| 88 | bool validate(nsecs_t timestamp) const REQUIRES(mMutex); |
| 89 | Model getVSyncPredictionModelLocked() const REQUIRES(mMutex); |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 90 | nsecs_t snapToVsync(nsecs_t timePoint) const REQUIRES(mMutex); |
| 91 | nsecs_t snapToVsyncAlignedWithRenderRate(nsecs_t timePoint) const REQUIRES(mMutex); |
Ady Abraham | f34a813 | 2023-02-13 20:49:48 -0800 | [diff] [blame] | 92 | bool isVSyncInPhaseLocked(nsecs_t timePoint, unsigned divisor) const REQUIRES(mMutex); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 93 | Period minFramePeriodLocked() const REQUIRES(mMutex); |
| 94 | void ensureMinFrameDurationIsKept(TimePoint, TimePoint) REQUIRES(mMutex); |
Ady Abraham | f34a813 | 2023-02-13 20:49:48 -0800 | [diff] [blame] | 95 | |
| 96 | struct VsyncSequence { |
| 97 | nsecs_t vsyncTime; |
| 98 | int64_t seq; |
| 99 | }; |
| 100 | VsyncSequence getVsyncSequenceLocked(nsecs_t timestamp) const REQUIRES(mMutex); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 101 | nsecs_t idealPeriod() const REQUIRES(mMutex); |
Ady Abraham | f34a813 | 2023-02-13 20:49:48 -0800 | [diff] [blame] | 102 | |
| 103 | bool const mTraceOn; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 104 | size_t const kHistorySize; |
| 105 | size_t const kMinimumSamplesForPrediction; |
| 106 | size_t const kOutlierTolerancePercent; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 107 | std::mutex mutable mMutex; |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 108 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 109 | std::optional<nsecs_t> mKnownTimestamp GUARDED_BY(mMutex); |
| 110 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 111 | // Map between ideal vsync period and the calculated model |
| 112 | std::unordered_map<nsecs_t, Model> mutable mRateMap GUARDED_BY(mMutex); |
| 113 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 114 | size_t mLastTimestampIndex GUARDED_BY(mMutex) = 0; |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 115 | std::vector<nsecs_t> mTimestamps GUARDED_BY(mMutex); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 116 | |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 117 | ftl::NonNull<DisplayModePtr> mDisplayModePtr GUARDED_BY(mMutex); |
| 118 | std::optional<Fps> mRenderRateOpt GUARDED_BY(mMutex); |
Ady Abraham | f34a813 | 2023-02-13 20:49:48 -0800 | [diff] [blame] | 119 | |
| 120 | mutable std::optional<VsyncSequence> mLastVsyncSequence GUARDED_BY(mMutex); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 121 | |
| 122 | std::deque<TimePoint> mPastExpectedPresentTimes GUARDED_BY(mMutex); |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 123 | |
| 124 | TimePoint mLastMissedVsync GUARDED_BY(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | } // namespace android::scheduler |