• 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 #include <ui/DisplayId.h>
25 
26 #include "VSyncTracker.h"
27 
28 namespace android::scheduler {
29 
30 class VSyncPredictor : public VSyncTracker {
31 public:
32     /*
33      * \param [in] PhysicalDisplayid The display this corresponds to.
34      * \param [in] idealPeriod  The initial ideal period to use.
35      * \param [in] historySize  The internal amount of entries to store in the model.
36      * \param [in] minimumSamplesForPrediction The minimum number of samples to collect before
37      * predicting. \param [in] outlierTolerancePercent a number 0 to 100 that will be used to filter
38      * samples that fall outlierTolerancePercent from an anticipated vsync event.
39      */
40     VSyncPredictor(PhysicalDisplayId, nsecs_t idealPeriod, size_t historySize,
41                    size_t minimumSamplesForPrediction, uint32_t outlierTolerancePercent);
42     ~VSyncPredictor();
43 
44     bool addVsyncTimestamp(nsecs_t timestamp) final EXCLUDES(mMutex);
45     nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const final EXCLUDES(mMutex);
46     nsecs_t currentPeriod() const final EXCLUDES(mMutex);
47     void resetModel() final EXCLUDES(mMutex);
48 
49     /*
50      * Inform the model that the period is anticipated to change to a new value.
51      * model will use the period parameter to predict vsync events until enough
52      * timestamps with the new period have been collected.
53      *
54      * \param [in] period   The new period that should be used.
55      */
56     void setPeriod(nsecs_t period) final EXCLUDES(mMutex);
57 
58     /* Query if the model is in need of more samples to make a prediction.
59      * \return  True, if model would benefit from more samples, False if not.
60      */
61     bool needsMoreSamples() const final EXCLUDES(mMutex);
62 
63     struct Model {
64         nsecs_t slope;
65         nsecs_t intercept;
66     };
67 
68     VSyncPredictor::Model getVSyncPredictionModel() const EXCLUDES(mMutex);
69 
70     bool isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const final EXCLUDES(mMutex);
71 
72     void setRenderRate(Fps) final EXCLUDES(mMutex);
73 
74     void dump(std::string& result) const final EXCLUDES(mMutex);
75 
76 private:
77     VSyncPredictor(VSyncPredictor const&) = delete;
78     VSyncPredictor& operator=(VSyncPredictor const&) = delete;
79     void clearTimestamps() REQUIRES(mMutex);
80 
81     const PhysicalDisplayId mId;
82 
83     inline void traceInt64If(const char* name, int64_t value) const;
84     inline void traceInt64(const char* name, int64_t value) const;
85 
86     size_t next(size_t i) const REQUIRES(mMutex);
87     bool validate(nsecs_t timestamp) const REQUIRES(mMutex);
88     Model getVSyncPredictionModelLocked() const REQUIRES(mMutex);
89     nsecs_t nextAnticipatedVSyncTimeFromLocked(nsecs_t timePoint) const REQUIRES(mMutex);
90     bool isVSyncInPhaseLocked(nsecs_t timePoint, unsigned divisor) const REQUIRES(mMutex);
91 
92     struct VsyncSequence {
93         nsecs_t vsyncTime;
94         int64_t seq;
95     };
96     VsyncSequence getVsyncSequenceLocked(nsecs_t timestamp) const REQUIRES(mMutex);
97 
98     bool const mTraceOn;
99     size_t const kHistorySize;
100     size_t const kMinimumSamplesForPrediction;
101     size_t const kOutlierTolerancePercent;
102     std::mutex mutable mMutex;
103 
104     nsecs_t mIdealPeriod GUARDED_BY(mMutex);
105     std::optional<nsecs_t> mKnownTimestamp GUARDED_BY(mMutex);
106 
107     // Map between ideal vsync period and the calculated model
108     std::unordered_map<nsecs_t, Model> mutable mRateMap GUARDED_BY(mMutex);
109 
110     size_t mLastTimestampIndex GUARDED_BY(mMutex) = 0;
111     std::vector<nsecs_t> mTimestamps GUARDED_BY(mMutex);
112 
113     std::optional<Fps> mRenderRate GUARDED_BY(mMutex);
114 
115     mutable std::optional<VsyncSequence> mLastVsyncSequence GUARDED_BY(mMutex);
116 };
117 
118 } // namespace android::scheduler
119