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 <map> 22 23 #include "../EventQueue.h" 24 #include "RefreshRateCalculator.h" 25 26 namespace android::hardware::graphics::composer { 27 28 enum PeriodRefreshRateCalculatorType { 29 kAverage = 0, 30 kMajor, 31 kTotal, 32 }; 33 34 struct PeriodRefreshRateCalculatorParameters { 35 PeriodRefreshRateCalculatorType mType = PeriodRefreshRateCalculatorType::kAverage; 36 int64_t mMeasurePeriodNs = 250000000; // default is 250 ms. 37 // When the presented time percentage exceeds or equals to this value, the Calculator becomes 38 // effective; otherwise, return kDefaultInvalidRefreshRate. 39 int mConfidencePercentage = 50; 40 bool mAlwaysCallback = false; 41 }; 42 43 class PeriodRefreshRateCalculator : public RefreshRateCalculator { 44 public: PeriodRefreshRateCalculator(EventQueue * eventQueue)45 PeriodRefreshRateCalculator(EventQueue* eventQueue) 46 : PeriodRefreshRateCalculator(eventQueue, PeriodRefreshRateCalculatorParameters()) {} 47 48 PeriodRefreshRateCalculator(EventQueue* eventQueue, 49 const PeriodRefreshRateCalculatorParameters& params); 50 51 int getRefreshRate() const final; 52 53 void onPowerStateChange(int from, int to) final; 54 55 void onPresentInternal(int64_t presentTimeNs, int flag) override; 56 57 void reset() override; 58 59 void setEnabled(bool isEnabled) final; 60 61 private: 62 int onMeasure(); 63 64 void setNewRefreshRate(int newRefreshRate); 65 66 EventQueue* mEventQueue; 67 PeriodRefreshRateCalculatorParameters mParams; 68 VrrControllerEvent mMeasureEvent; 69 70 std::map<Fraction<int>, int> mStatistics; 71 72 int64_t mLastPresentTimeNs = kDefaultInvalidPresentTimeNs; 73 int mLastRefreshRate = kDefaultInvalidRefreshRate; 74 75 int64_t mConfidenceThresholdTimeNs; 76 77 // Control then next measurement. 78 int64_t mLastMeasureTimeNs; 79 }; 80 81 } // namespace android::hardware::graphics::composer 82