blob: 1ded54f768684867adb6a2d12ac1bfad505ff66e [file] [log] [blame]
Kevin DuBois1678e2c2019-08-22 12:26:24 -07001/*
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 DuBois1678e2c2019-08-22 12:26:24 -070019#include <mutex>
20#include <unordered_map>
21#include <vector>
Dominik Laskowski62eff352021-12-06 09:59:41 -080022
23#include <android-base/thread_annotations.h>
24
Kevin DuBois1678e2c2019-08-22 12:26:24 -070025#include "VSyncTracker.h"
26
27namespace android::scheduler {
28
29class VSyncPredictor : public VSyncTracker {
30public:
31 /*
Leon Scroggins III31d41412022-11-18 16:42:53 -050032 * \param [in] name The name of the display this corresponds to.
Kevin DuBois1678e2c2019-08-22 12:26:24 -070033 * \param [in] idealPeriod The initial ideal period to use.
34 * \param [in] historySize The internal amount of entries to store in the model.
35 * \param [in] minimumSamplesForPrediction The minimum number of samples to collect before
36 * predicting. \param [in] outlierTolerancePercent a number 0 to 100 that will be used to filter
37 * samples that fall outlierTolerancePercent from an anticipated vsync event.
38 */
Leon Scroggins III31d41412022-11-18 16:42:53 -050039 VSyncPredictor(std::string name, nsecs_t idealPeriod, size_t historySize,
40 size_t minimumSamplesForPrediction, uint32_t outlierTolerancePercent);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070041 ~VSyncPredictor();
42
Ady Abraham0bb6a472020-10-12 10:22:13 -070043 bool addVsyncTimestamp(nsecs_t timestamp) final EXCLUDES(mMutex);
44 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const final EXCLUDES(mMutex);
45 nsecs_t currentPeriod() const final EXCLUDES(mMutex);
46 void resetModel() final EXCLUDES(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070047
48 /*
49 * Inform the model that the period is anticipated to change to a new value.
50 * model will use the period parameter to predict vsync events until enough
51 * timestamps with the new period have been collected.
52 *
53 * \param [in] period The new period that should be used.
54 */
Ady Abraham0bb6a472020-10-12 10:22:13 -070055 void setPeriod(nsecs_t period) final EXCLUDES(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070056
Kevin DuBoisb818bfa2020-07-10 14:29:36 -070057 /* Query if the model is in need of more samples to make a prediction.
Kevin DuBois1678e2c2019-08-22 12:26:24 -070058 * \return True, if model would benefit from more samples, False if not.
59 */
Ady Abraham0bb6a472020-10-12 10:22:13 -070060 bool needsMoreSamples() const final EXCLUDES(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070061
Ady Abraham0bb6a472020-10-12 10:22:13 -070062 struct Model {
63 nsecs_t slope;
64 nsecs_t intercept;
65 };
Kevin DuBois1678e2c2019-08-22 12:26:24 -070066
Ady Abraham0bb6a472020-10-12 10:22:13 -070067 VSyncPredictor::Model getVSyncPredictionModel() const EXCLUDES(mMutex);
68
Ady Abraham5cc2e262021-03-25 13:09:17 -070069 bool isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const final EXCLUDES(mMutex);
Ady Abraham0bb6a472020-10-12 10:22:13 -070070
Ady Abrahamace3d052022-11-17 16:25:05 -080071 void setDivisor(unsigned divisor) final EXCLUDES(mMutex);
72
Ady Abraham0bb6a472020-10-12 10:22:13 -070073 void dump(std::string& result) const final EXCLUDES(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -070074
Kevin DuBois1678e2c2019-08-22 12:26:24 -070075private:
76 VSyncPredictor(VSyncPredictor const&) = delete;
77 VSyncPredictor& operator=(VSyncPredictor const&) = delete;
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080078 void clearTimestamps() REQUIRES(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070079
Leon Scroggins III31d41412022-11-18 16:42:53 -050080 const std::string mName;
81
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080082 inline void traceInt64If(const char* name, int64_t value) const;
Ady Abrahamd9b9a042023-01-13 11:30:58 -080083 inline void traceInt64(const char* name, int64_t value) const;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080084 bool const mTraceOn;
85
Kevin DuBois1678e2c2019-08-22 12:26:24 -070086 size_t const kHistorySize;
87 size_t const kMinimumSamplesForPrediction;
88 size_t const kOutlierTolerancePercent;
89
90 std::mutex mutable mMutex;
Ady Abraham9c53ee72020-07-22 21:16:18 -070091 size_t next(size_t i) const REQUIRES(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070092 bool validate(nsecs_t timestamp) const REQUIRES(mMutex);
Ady Abraham0bb6a472020-10-12 10:22:13 -070093
94 Model getVSyncPredictionModelLocked() const REQUIRES(mMutex);
95
96 nsecs_t nextAnticipatedVSyncTimeFromLocked(nsecs_t timePoint) const REQUIRES(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070097
Ady Abrahamace3d052022-11-17 16:25:05 -080098 bool isVSyncInPhaseLocked(nsecs_t timePoint, unsigned divisor) const REQUIRES(mMutex);
99
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700100 nsecs_t mIdealPeriod GUARDED_BY(mMutex);
101 std::optional<nsecs_t> mKnownTimestamp GUARDED_BY(mMutex);
102
Ady Abraham0bb6a472020-10-12 10:22:13 -0700103 // Map between ideal vsync period and the calculated model
104 std::unordered_map<nsecs_t, Model> mutable mRateMap GUARDED_BY(mMutex);
105
106 // Map between the divided vsync period and the last known vsync timestamp
Ady Abrahamcc315492022-02-17 17:06:39 -0800107 std::unordered_map<nsecs_t, nsecs_t> mutable mRateDivisorKnownTimestampMap GUARDED_BY(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700108
Ady Abraham9c53ee72020-07-22 21:16:18 -0700109 size_t mLastTimestampIndex GUARDED_BY(mMutex) = 0;
Ady Abraham92fa2f42020-02-11 15:33:56 -0800110 std::vector<nsecs_t> mTimestamps GUARDED_BY(mMutex);
Ady Abrahamace3d052022-11-17 16:25:05 -0800111
112 unsigned mDivisor GUARDED_BY(mMutex) = 1;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700113};
114
115} // namespace android::scheduler