1 /*
2  * Copyright (C) 2024 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 <stdint.h>
20 #include <chrono>
21 #include <list>
22 
23 #include "../EventQueue.h"
24 #include "PeriodRefreshRateCalculator.h"
25 #include "RefreshRateCalculator.h"
26 
27 namespace android::hardware::graphics::composer {
28 
29 struct VideoFrameRateCalculatorParameters {
VideoFrameRateCalculatorParametersVideoFrameRateCalculatorParameters30     VideoFrameRateCalculatorParameters() {
31         mPeriodParams.mAlwaysCallback = true;
32         mPeriodParams.mConfidencePercentage = 50;
33     }
34 
35     int mDelta = 5;
36 
37     int mWindowSize = 5;
38 
39     int mMinStableRuns = 3;
40 
41     PeriodRefreshRateCalculatorParameters mPeriodParams;
42 
43     int mMinInterestedFrameRate = 1;
44     int mMaxInterestedFrameRate = 120;
45 };
46 
47 class VideoFrameRateCalculator : public RefreshRateCalculator {
48 public:
VideoFrameRateCalculator(EventQueue * eventQueue)49     VideoFrameRateCalculator(EventQueue* eventQueue)
50           : VideoFrameRateCalculator(eventQueue, VideoFrameRateCalculatorParameters()) {}
51 
52     VideoFrameRateCalculator(EventQueue* eventQueue,
53                              const VideoFrameRateCalculatorParameters& params);
54 
55     int getRefreshRate() const final;
56 
57     void onPowerStateChange(int from, int to) final;
58 
59     void onPresentInternal(int64_t presentTimeNs, int flag) override;
60 
61     void reset() override;
62 
63     void setEnabled(bool isEnabled) final;
64 
65     void setVrrConfigAttributes(int64_t vsyncPeriodNs, int64_t minFrameIntervalNs) final;
66 
67 private:
68     int onReportRefreshRate(int);
69 
70     void setNewRefreshRate(int newRefreshRate);
71 
72     std::shared_ptr<RefreshRateCalculator> mRefreshRateCalculator;
73 
74     EventQueue* mEventQueue;
75     VideoFrameRateCalculatorParameters mParams;
76 
77     int mLastVideoFrameRate = kDefaultInvalidRefreshRate;
78 
79     int mLastPeriodFrameRate = kDefaultInvalidRefreshRate;
80     int mLastPeriodFrameRateRuns = 0;
81 
82     std::list<int> mHistory;
83 };
84 
85 } // namespace android::hardware::graphics::composer
86