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 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 19 | #include <mutex> |
| 20 | #include <unordered_map> |
| 21 | #include <vector> |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 22 | |
| 23 | #include <android-base/thread_annotations.h> |
| 24 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 25 | #include "VSyncTracker.h" |
| 26 | |
| 27 | namespace android::scheduler { |
| 28 | |
| 29 | class VSyncPredictor : public VSyncTracker { |
| 30 | public: |
| 31 | /* |
| 32 | * \param [in] idealPeriod The initial ideal period to use. |
| 33 | * \param [in] historySize The internal amount of entries to store in the model. |
| 34 | * \param [in] minimumSamplesForPrediction The minimum number of samples to collect before |
| 35 | * predicting. \param [in] outlierTolerancePercent a number 0 to 100 that will be used to filter |
| 36 | * samples that fall outlierTolerancePercent from an anticipated vsync event. |
| 37 | */ |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame^] | 38 | VSyncPredictor(nsecs_t idealPeriod, size_t historySize, size_t minimumSamplesForPrediction, |
| 39 | uint32_t outlierTolerancePercent); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 40 | ~VSyncPredictor(); |
| 41 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 42 | bool addVsyncTimestamp(nsecs_t timestamp) final EXCLUDES(mMutex); |
| 43 | nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const final EXCLUDES(mMutex); |
| 44 | nsecs_t currentPeriod() const final EXCLUDES(mMutex); |
| 45 | void resetModel() final EXCLUDES(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Inform the model that the period is anticipated to change to a new value. |
| 49 | * model will use the period parameter to predict vsync events until enough |
| 50 | * timestamps with the new period have been collected. |
| 51 | * |
| 52 | * \param [in] period The new period that should be used. |
| 53 | */ |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 54 | void setPeriod(nsecs_t period) final EXCLUDES(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 55 | |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 56 | /* 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] | 57 | * \return True, if model would benefit from more samples, False if not. |
| 58 | */ |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 59 | bool needsMoreSamples() const final EXCLUDES(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 60 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 61 | struct Model { |
| 62 | nsecs_t slope; |
| 63 | nsecs_t intercept; |
| 64 | }; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 65 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 66 | VSyncPredictor::Model getVSyncPredictionModel() const EXCLUDES(mMutex); |
| 67 | |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 68 | bool isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const final EXCLUDES(mMutex); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 69 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 70 | void setDivisor(unsigned divisor) final EXCLUDES(mMutex); |
| 71 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 72 | void dump(std::string& result) const final EXCLUDES(mMutex); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 73 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 74 | private: |
| 75 | VSyncPredictor(VSyncPredictor const&) = delete; |
| 76 | VSyncPredictor& operator=(VSyncPredictor const&) = delete; |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 77 | void clearTimestamps() REQUIRES(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 78 | |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 79 | inline void traceInt64If(const char* name, int64_t value) const; |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 80 | inline void traceInt64(const char* name, int64_t value) const; |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 81 | bool const mTraceOn; |
| 82 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 83 | size_t const kHistorySize; |
| 84 | size_t const kMinimumSamplesForPrediction; |
| 85 | size_t const kOutlierTolerancePercent; |
| 86 | |
| 87 | std::mutex mutable mMutex; |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 88 | size_t next(size_t i) const REQUIRES(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 89 | bool validate(nsecs_t timestamp) const REQUIRES(mMutex); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 90 | |
| 91 | Model getVSyncPredictionModelLocked() const REQUIRES(mMutex); |
| 92 | |
| 93 | nsecs_t nextAnticipatedVSyncTimeFromLocked(nsecs_t timePoint) const REQUIRES(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 94 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 95 | bool isVSyncInPhaseLocked(nsecs_t timePoint, unsigned divisor) const REQUIRES(mMutex); |
| 96 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 97 | nsecs_t mIdealPeriod GUARDED_BY(mMutex); |
| 98 | std::optional<nsecs_t> mKnownTimestamp GUARDED_BY(mMutex); |
| 99 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 100 | // Map between ideal vsync period and the calculated model |
| 101 | std::unordered_map<nsecs_t, Model> mutable mRateMap GUARDED_BY(mMutex); |
| 102 | |
| 103 | // Map between the divided vsync period and the last known vsync timestamp |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 104 | std::unordered_map<nsecs_t, nsecs_t> mutable mRateDivisorKnownTimestampMap GUARDED_BY(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 105 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 106 | size_t mLastTimestampIndex GUARDED_BY(mMutex) = 0; |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 107 | std::vector<nsecs_t> mTimestamps GUARDED_BY(mMutex); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 108 | |
| 109 | unsigned mDivisor GUARDED_BY(mMutex) = 1; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | } // namespace android::scheduler |