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