• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <mutex>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include <android-base/thread_annotations.h>
24 
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      */
38     VSyncPredictor(nsecs_t idealPeriod, size_t historySize, size_t minimumSamplesForPrediction,
39                    uint32_t outlierTolerancePercent);
40     ~VSyncPredictor();
41 
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);
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      */
54     void setPeriod(nsecs_t period) final EXCLUDES(mMutex);
55 
56     /* Query if the model is in need of more samples to make a prediction.
57      * \return  True, if model would benefit from more samples, False if not.
58      */
59     bool needsMoreSamples() const final EXCLUDES(mMutex);
60 
61     struct Model {
62         nsecs_t slope;
63         nsecs_t intercept;
64     };
65 
66     VSyncPredictor::Model getVSyncPredictionModel() const EXCLUDES(mMutex);
67 
68     bool isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const final EXCLUDES(mMutex);
69 
70     void dump(std::string& result) const final EXCLUDES(mMutex);
71 
72 private:
73     VSyncPredictor(VSyncPredictor const&) = delete;
74     VSyncPredictor& operator=(VSyncPredictor const&) = delete;
75     void clearTimestamps() REQUIRES(mMutex);
76 
77     inline void traceInt64If(const char* name, int64_t value) const;
78     bool const mTraceOn;
79 
80     size_t const kHistorySize;
81     size_t const kMinimumSamplesForPrediction;
82     size_t const kOutlierTolerancePercent;
83 
84     std::mutex mutable mMutex;
85     size_t next(size_t i) const REQUIRES(mMutex);
86     bool validate(nsecs_t timestamp) const REQUIRES(mMutex);
87 
88     Model getVSyncPredictionModelLocked() const REQUIRES(mMutex);
89 
90     nsecs_t nextAnticipatedVSyncTimeFromLocked(nsecs_t timePoint) const REQUIRES(mMutex);
91 
92     nsecs_t mIdealPeriod GUARDED_BY(mMutex);
93     std::optional<nsecs_t> mKnownTimestamp GUARDED_BY(mMutex);
94 
95     // Map between ideal vsync period and the calculated model
96     std::unordered_map<nsecs_t, Model> mutable mRateMap GUARDED_BY(mMutex);
97 
98     // Map between the divided vsync period and the last known vsync timestamp
99     std::unordered_map<nsecs_t, nsecs_t> mutable mRateDivisorKnownTimestampMap GUARDED_BY(mMutex);
100 
101     size_t mLastTimestampIndex GUARDED_BY(mMutex) = 0;
102     std::vector<nsecs_t> mTimestamps GUARDED_BY(mMutex);
103 };
104 
105 } // namespace android::scheduler
106