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