blob: 6cb5a678c7630349172272ab73de809ef0072fb5 [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>
Leon Scroggins III67388622023-02-06 20:36:20 -050024#include <ui/DisplayId.h>
Dominik Laskowski62eff352021-12-06 09:59:41 -080025
Kevin DuBois1678e2c2019-08-22 12:26:24 -070026#include "VSyncTracker.h"
27
28namespace android::scheduler {
29
30class VSyncPredictor : public VSyncTracker {
31public:
32 /*
Leon Scroggins III67388622023-02-06 20:36:20 -050033 * \param [in] PhysicalDisplayid The display this corresponds to.
Ady Abrahamc585dba2023-11-15 18:41:35 -080034 * \param [in] modePtr The initial display mode
Kevin DuBois1678e2c2019-08-22 12:26:24 -070035 * \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.
ramindanid4354a92023-10-02 15:11:09 -070039 * \param [in] IVsyncTrackerCallback The callback for the VSyncTracker.
Kevin DuBois1678e2c2019-08-22 12:26:24 -070040 */
Ady Abrahamc585dba2023-11-15 18:41:35 -080041 VSyncPredictor(ftl::NonNull<DisplayModePtr> modePtr, size_t historySize,
ramindanid4354a92023-10-02 15:11:09 -070042 size_t minimumSamplesForPrediction, uint32_t outlierTolerancePercent,
43 IVsyncTrackerCallback&);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070044 ~VSyncPredictor();
45
Ady Abraham0bb6a472020-10-12 10:22:13 -070046 bool addVsyncTimestamp(nsecs_t timestamp) final EXCLUDES(mMutex);
47 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const final EXCLUDES(mMutex);
48 nsecs_t currentPeriod() const final EXCLUDES(mMutex);
Ady Abraham3db8a3c2023-11-20 17:53:47 -080049 Period minFramePeriod() const final EXCLUDES(mMutex);
Ady Abraham0bb6a472020-10-12 10:22:13 -070050 void resetModel() final EXCLUDES(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070051
Kevin DuBoisb818bfa2020-07-10 14:29:36 -070052 /* Query if the model is in need of more samples to make a prediction.
Kevin DuBois1678e2c2019-08-22 12:26:24 -070053 * \return True, if model would benefit from more samples, False if not.
54 */
Ady Abraham0bb6a472020-10-12 10:22:13 -070055 bool needsMoreSamples() const final EXCLUDES(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070056
Ady Abraham0bb6a472020-10-12 10:22:13 -070057 struct Model {
58 nsecs_t slope;
59 nsecs_t intercept;
60 };
Kevin DuBois1678e2c2019-08-22 12:26:24 -070061
Ady Abraham0bb6a472020-10-12 10:22:13 -070062 VSyncPredictor::Model getVSyncPredictionModel() const EXCLUDES(mMutex);
63
Ady Abraham5cc2e262021-03-25 13:09:17 -070064 bool isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const final EXCLUDES(mMutex);
Ady Abraham0bb6a472020-10-12 10:22:13 -070065
Ady Abrahamc585dba2023-11-15 18:41:35 -080066 void setDisplayModePtr(ftl::NonNull<DisplayModePtr>) final EXCLUDES(mMutex);
67
68 void setRenderRate(Fps) final EXCLUDES(mMutex);
Ady Abrahamace3d052022-11-17 16:25:05 -080069
Ady Abraham0bb6a472020-10-12 10:22:13 -070070 void dump(std::string& result) const final EXCLUDES(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -070071
Kevin DuBois1678e2c2019-08-22 12:26:24 -070072private:
73 VSyncPredictor(VSyncPredictor const&) = delete;
74 VSyncPredictor& operator=(VSyncPredictor const&) = delete;
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080075 void clearTimestamps() REQUIRES(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -070076
Leon Scroggins III67388622023-02-06 20:36:20 -050077 const PhysicalDisplayId mId;
78
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080079 inline void traceInt64If(const char* name, int64_t value) const;
Ady Abrahamd9b9a042023-01-13 11:30:58 -080080 inline void traceInt64(const char* name, int64_t value) const;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080081
Ady Abrahamf34a8132023-02-13 20:49:48 -080082 size_t next(size_t i) const REQUIRES(mMutex);
83 bool validate(nsecs_t timestamp) const REQUIRES(mMutex);
84 Model getVSyncPredictionModelLocked() const REQUIRES(mMutex);
85 nsecs_t nextAnticipatedVSyncTimeFromLocked(nsecs_t timePoint) const REQUIRES(mMutex);
86 bool isVSyncInPhaseLocked(nsecs_t timePoint, unsigned divisor) const REQUIRES(mMutex);
87
88 struct VsyncSequence {
89 nsecs_t vsyncTime;
90 int64_t seq;
91 };
92 VsyncSequence getVsyncSequenceLocked(nsecs_t timestamp) const REQUIRES(mMutex);
Ady Abrahamc585dba2023-11-15 18:41:35 -080093 nsecs_t idealPeriod() const REQUIRES(mMutex);
Ady Abrahamf34a8132023-02-13 20:49:48 -080094
95 bool const mTraceOn;
Kevin DuBois1678e2c2019-08-22 12:26:24 -070096 size_t const kHistorySize;
97 size_t const kMinimumSamplesForPrediction;
98 size_t const kOutlierTolerancePercent;
ramindanid4354a92023-10-02 15:11:09 -070099 IVsyncTrackerCallback& mVsyncTrackerCallback;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700100 std::mutex mutable mMutex;
Ady Abrahamace3d052022-11-17 16:25:05 -0800101
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700102 std::optional<nsecs_t> mKnownTimestamp GUARDED_BY(mMutex);
103
Ady Abraham0bb6a472020-10-12 10:22:13 -0700104 // Map between ideal vsync period and the calculated model
105 std::unordered_map<nsecs_t, Model> mutable mRateMap GUARDED_BY(mMutex);
106
Ady Abraham9c53ee72020-07-22 21:16:18 -0700107 size_t mLastTimestampIndex GUARDED_BY(mMutex) = 0;
Ady Abraham92fa2f42020-02-11 15:33:56 -0800108 std::vector<nsecs_t> mTimestamps GUARDED_BY(mMutex);
Ady Abrahamace3d052022-11-17 16:25:05 -0800109
Ady Abrahamc585dba2023-11-15 18:41:35 -0800110 ftl::NonNull<DisplayModePtr> mDisplayModePtr GUARDED_BY(mMutex);
111 std::optional<Fps> mRenderRateOpt GUARDED_BY(mMutex);
Ady Abrahamf34a8132023-02-13 20:49:48 -0800112
113 mutable std::optional<VsyncSequence> mLastVsyncSequence GUARDED_BY(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700114};
115
116} // namespace android::scheduler