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 "../EventQueue.h"
20 #include "RefreshRateCalculator.h"
21 
22 namespace android::hardware::graphics::composer {
23 
24 class CombinedRefreshRateCalculator : public RefreshRateCalculator {
25 public:
26     CombinedRefreshRateCalculator(
27             std::vector<std::shared_ptr<RefreshRateCalculator>> refreshRateCalculators);
28 
29     CombinedRefreshRateCalculator(
30             std::vector<std::shared_ptr<RefreshRateCalculator>> refreshRateCalculators,
31             int minValidRefreshRate, int maxValidRefreshRate);
32 
33     int getRefreshRate() const override;
34 
35     void onPowerStateChange(int from, int to) final;
36 
37     void onPresentInternal(int64_t presentTimeNs, int flag) override;
38 
39     void reset() override;
40 
41     void setEnabled(bool isEnabled) final;
42 
43     void setVrrConfigAttributes(int64_t vsyncPeriodNs, int64_t minFrameIntervalNs) final;
44 
45 private:
46     static constexpr int kDefaultMinValidRefreshRate = 1;
47     static constexpr int kDefaultMaxValidRefreshRate = 120;
48 
49     void onRefreshRateChanged(int refreshRate);
50 
51     void setNewRefreshRate(int newRefreshRate);
52 
53     void updateRefreshRate();
54 
55     CombinedRefreshRateCalculator(const CombinedRefreshRateCalculator&) = delete;
56     CombinedRefreshRateCalculator& operator=(const CombinedRefreshRateCalculator&) = delete;
57 
58     std::vector<std::shared_ptr<RefreshRateCalculator>> mRefreshRateCalculators;
59 
60     VrrControllerEvent mMeasureEvent;
61 
62     int mMinValidRefreshRate;
63     int mMaxValidRefreshRate;
64 
65     int mLastRefreshRate = kDefaultInvalidRefreshRate;
66 
67     bool mIsOnPresent = false;
68     int mHasRefreshRateChage = false;
69 };
70 
71 } // namespace android::hardware::graphics::composer
72