| 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 |  | 
|  | 19 | #include <android-base/thread_annotations.h> | 
|  | 20 | #include <mutex> | 
|  | 21 | #include <unordered_map> | 
|  | 22 | #include <vector> | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 23 | #include "SchedulerUtils.h" | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 24 | #include "VSyncTracker.h" | 
|  | 25 |  | 
|  | 26 | namespace android::scheduler { | 
|  | 27 |  | 
|  | 28 | class VSyncPredictor : public VSyncTracker { | 
|  | 29 | public: | 
|  | 30 | /* | 
|  | 31 | * \param [in] idealPeriod  The initial ideal period to use. | 
|  | 32 | * \param [in] historySize  The internal amount of entries to store in the model. | 
|  | 33 | * \param [in] minimumSamplesForPrediction The minimum number of samples to collect before | 
|  | 34 | * predicting. \param [in] outlierTolerancePercent a number 0 to 100 that will be used to filter | 
|  | 35 | * samples that fall outlierTolerancePercent from an anticipated vsync event. | 
|  | 36 | */ | 
|  | 37 | VSyncPredictor(nsecs_t idealPeriod, size_t historySize, size_t minimumSamplesForPrediction, | 
|  | 38 | uint32_t outlierTolerancePercent); | 
|  | 39 | ~VSyncPredictor(); | 
|  | 40 |  | 
| Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 41 | bool addVsyncTimestamp(nsecs_t timestamp) final; | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 42 | nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const final; | 
| Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 43 | nsecs_t currentPeriod() const final; | 
| Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 44 | void resetModel() final; | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 45 |  | 
|  | 46 | /* | 
|  | 47 | * Inform the model that the period is anticipated to change to a new value. | 
|  | 48 | * model will use the period parameter to predict vsync events until enough | 
|  | 49 | * timestamps with the new period have been collected. | 
|  | 50 | * | 
|  | 51 | * \param [in] period   The new period that should be used. | 
|  | 52 | */ | 
| Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 53 | void setPeriod(nsecs_t period) final; | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 54 |  | 
|  | 55 | /* Query if the model is in need of more samples to make a prediction at timePoint. | 
|  | 56 | * \param [in] timePoint    The timePoint to inquire of. | 
|  | 57 | * \return  True, if model would benefit from more samples, False if not. | 
|  | 58 | */ | 
|  | 59 | bool needsMoreSamples(nsecs_t timePoint) const; | 
|  | 60 |  | 
|  | 61 | std::tuple<nsecs_t /* slope */, nsecs_t /* intercept */> getVSyncPredictionModel() const; | 
|  | 62 |  | 
|  | 63 | private: | 
|  | 64 | VSyncPredictor(VSyncPredictor const&) = delete; | 
|  | 65 | VSyncPredictor& operator=(VSyncPredictor const&) = delete; | 
| Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 66 | void clearTimestamps() REQUIRES(mMutex); | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 67 |  | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 68 | inline void traceInt64If(const char* name, int64_t value) const; | 
|  | 69 | bool const mTraceOn; | 
|  | 70 |  | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 71 | size_t const kHistorySize; | 
|  | 72 | size_t const kMinimumSamplesForPrediction; | 
|  | 73 | size_t const kOutlierTolerancePercent; | 
|  | 74 |  | 
|  | 75 | std::mutex mutable mMutex; | 
|  | 76 | size_t next(int i) const REQUIRES(mMutex); | 
|  | 77 | bool validate(nsecs_t timestamp) const REQUIRES(mMutex); | 
|  | 78 | std::tuple<nsecs_t, nsecs_t> getVSyncPredictionModel(std::lock_guard<std::mutex> const&) const | 
|  | 79 | REQUIRES(mMutex); | 
|  | 80 |  | 
|  | 81 | nsecs_t mIdealPeriod GUARDED_BY(mMutex); | 
|  | 82 | std::optional<nsecs_t> mKnownTimestamp GUARDED_BY(mMutex); | 
|  | 83 |  | 
|  | 84 | std::unordered_map<nsecs_t, std::tuple<nsecs_t, nsecs_t>> mutable mRateMap GUARDED_BY(mMutex); | 
|  | 85 |  | 
|  | 86 | int lastTimestampIndex GUARDED_BY(mMutex) = 0; | 
|  | 87 | std::vector<nsecs_t> timestamps GUARDED_BY(mMutex); | 
|  | 88 | }; | 
|  | 89 |  | 
|  | 90 | } // namespace android::scheduler |