blob: 41e5469c2061bdb552f9cf08cff50dbec9bbb647 [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;
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080042 nsecs_t currentPeriod() const final;
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080043 void resetModel() final;
Kevin DuBois1678e2c2019-08-22 12:26:24 -070044
45 /*
46 * Inform the model that the period is anticipated to change to a new value.
47 * model will use the period parameter to predict vsync events until enough
48 * timestamps with the new period have been collected.
49 *
50 * \param [in] period The new period that should be used.
51 */
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080052 void setPeriod(nsecs_t period) final;
Kevin DuBois1678e2c2019-08-22 12:26:24 -070053
54 /* Query if the model is in need of more samples to make a prediction at timePoint.
55 * \param [in] timePoint The timePoint to inquire of.
56 * \return True, if model would benefit from more samples, False if not.
57 */
58 bool needsMoreSamples(nsecs_t timePoint) const;
59
60 std::tuple<nsecs_t /* slope */, nsecs_t /* intercept */> getVSyncPredictionModel() const;
61
62private:
63 VSyncPredictor(VSyncPredictor const&) = delete;
64 VSyncPredictor& operator=(VSyncPredictor const&) = delete;
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080065 void clearTimestamps() REQUIRES(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070066
67 size_t const kHistorySize;
68 size_t const kMinimumSamplesForPrediction;
69 size_t const kOutlierTolerancePercent;
70
71 std::mutex mutable mMutex;
72 size_t next(int i) const REQUIRES(mMutex);
73 bool validate(nsecs_t timestamp) const REQUIRES(mMutex);
74 std::tuple<nsecs_t, nsecs_t> getVSyncPredictionModel(std::lock_guard<std::mutex> const&) const
75 REQUIRES(mMutex);
76
77 nsecs_t mIdealPeriod GUARDED_BY(mMutex);
78 std::optional<nsecs_t> mKnownTimestamp GUARDED_BY(mMutex);
79
80 std::unordered_map<nsecs_t, std::tuple<nsecs_t, nsecs_t>> mutable mRateMap GUARDED_BY(mMutex);
81
82 int lastTimestampIndex GUARDED_BY(mMutex) = 0;
83 std::vector<nsecs_t> timestamps GUARDED_BY(mMutex);
84};
85
86} // namespace android::scheduler