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 <utils/Errors.h> 20 #include <utils/Log.h> 21 #include <utils/Trace.h> 22 #include <chrono> 23 #include <functional> 24 #include <string> 25 26 #include <hardware/hwcomposer_defs.h> 27 28 #include "../Utils.h" 29 #include "../interface/VariableRefreshRateInterface.h" 30 31 namespace android::hardware::graphics::composer { 32 33 enum class RefreshRateCalculatorType : int { 34 kInvalid = -1, 35 kAod = 0, 36 kInstant, 37 kExitIdle, 38 kPeriodical, 39 kVideoPlayback, 40 kCombined, 41 kTotal, 42 }; 43 44 constexpr int64_t kDefaultMinimumRefreshRate = 1; 45 46 constexpr int64_t kDefaultInvalidPresentTimeNs = -1; 47 48 constexpr int64_t kDefaultInvalidRefreshRate = -1; 49 50 class RefreshRateCalculator : public PowerModeListener { 51 public: RefreshRateCalculator()52 RefreshRateCalculator() 53 : mVsyncIntervalNs(freqToDurationNs(mVsyncRate)), 54 mMinFrameIntervalNs(freqToDurationNs(mMaxFrameRate)) {} 55 56 virtual ~RefreshRateCalculator() = default; 57 getName()58 std::string getName() const { return mName; }; 59 60 virtual int getRefreshRate() const = 0; 61 onPowerStateChange(int __unused from,int __unused to)62 virtual void onPowerStateChange(int __unused from, int __unused to) override {} 63 onPresent(int64_t presentTimeNs,int flag)64 void onPresent(int64_t presentTimeNs, int flag) { 65 if (hasPresentFrameFlag(flag, PresentFrameFlag::kUpdateRefreshRateIndicatorLayerOnly)) { 66 return; 67 } 68 onPresentInternal(presentTimeNs, flag); 69 } 70 71 virtual void onPresentInternal(int64_t presentTimeNs, int flag) = 0; 72 73 virtual void reset() = 0; 74 registerRefreshRateChangeCallback(std::function<void (int)> callback)75 virtual void registerRefreshRateChangeCallback(std::function<void(int)> callback) { 76 mRefreshRateChangeCallback = std::move(callback); 77 } 78 setEnabled(bool __unused isEnabled)79 virtual void setEnabled(bool __unused isEnabled){}; 80 81 // Should be invoked when configuration changed. setVrrConfigAttributes(int64_t vsyncPeriodNs,int64_t minFrameIntervalNs)82 virtual void setVrrConfigAttributes(int64_t vsyncPeriodNs, int64_t minFrameIntervalNs) { 83 mVsyncIntervalNs = vsyncPeriodNs; 84 mMinFrameIntervalNs = minFrameIntervalNs; 85 mMaxFrameRate = durationNsToFreq(minFrameIntervalNs); 86 mVsyncRate = durationNsToFreq(vsyncPeriodNs); 87 mMinVsyncNum = roundDivide(minFrameIntervalNs, vsyncPeriodNs); 88 } 89 setName(const std::string & name)90 void setName(const std::string& name) { mName = name; } 91 92 protected: 93 static constexpr int64_t kDefaultMaxFrameRate = 120; 94 durationToVsync(int64_t duration)95 int durationToVsync(int64_t duration) const { return roundDivide(duration, mVsyncIntervalNs); } 96 97 std::function<void(int)> mRefreshRateChangeCallback; 98 99 int mVsyncRate = kDefaultMaxFrameRate; 100 int mMaxFrameRate = kDefaultMaxFrameRate; 101 int64_t mVsyncIntervalNs; 102 int64_t mMinFrameIntervalNs; 103 int mMinVsyncNum = 1; 104 105 std::string mName; 106 }; 107 108 } // namespace android::hardware::graphics::composer 109