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 <unordered_set> 20 21 #include <aidl/android/hardware/power/stats/State.h> 22 #include <aidl/android/hardware/power/stats/StateResidency.h> 23 24 #include "../Statistics/VariableRefreshRateStatistic.h" 25 #include "PowerStatsPresentProfileTokenGenerator.h" 26 27 // #define DEBUG_VRR_POWERSTATS 1 28 29 namespace android::hardware::graphics::composer { 30 31 using aidl::android::hardware::power::stats::State; 32 using aidl::android::hardware::power::stats::StateResidency; 33 34 typedef std::vector<StateResidency> StateResidencies; 35 36 class DisplayStateResidencyProvider { 37 public: 38 DisplayStateResidencyProvider( 39 std::shared_ptr<CommonDisplayContextProvider> displayContextProvider, 40 std::shared_ptr<StatisticsProvider> statisticsProvider); 41 42 void getStateResidency(std::vector<StateResidency>* stats); 43 44 const std::vector<State>& getStates(); 45 46 DisplayStateResidencyProvider(const DisplayStateResidencyProvider& other) = delete; 47 DisplayStateResidencyProvider& operator=(const DisplayStateResidencyProvider& other) = delete; 48 49 private: 50 static const std::set<Fraction<int>> kFpsMappingTable; 51 static const std::unordered_set<int> kFpsLowPowerModeMappingTable; 52 static const std::unordered_set<int> kActivePowerModes; 53 54 // The format of pattern is: ([token label]'delimiter'?)* 55 static constexpr std::string_view kDisplayStateResidencyPattern = 56 "[mode](:)[width](x)[height](@)[fps]()"; 57 58 static constexpr char kTokenLabelStart = '['; 59 static constexpr char kTokenLabelEnd = ']'; 60 static constexpr char kDelimiterStart = '('; 61 static constexpr char kDelimiterEnd = ')'; 62 63 void mapStatistics(); 64 uint64_t aggregateStatistics(); 65 66 void generatePowerStatsStates(); 67 68 bool parseDisplayStateResidencyPattern(); 69 70 std::shared_ptr<CommonDisplayContextProvider> mDisplayContextProvider; 71 72 std::shared_ptr<StatisticsProvider> mStatisticsProvider; 73 74 DisplayPresentStatistics mStatistics; 75 76 typedef std::map<PowerStatsPresentProfile, DisplayPresentRecord> PowerStatsPresentStatistics; 77 78 PowerStatsPresentStatistics mRemappedStatistics; 79 80 PowerStatsPresentProfileTokenGenerator mPowerStatsPresentProfileTokenGenerator; 81 std::vector<std::pair<std::string, std::string>> mDisplayStateResidencyPattern; 82 83 std::vector<State> mStates; 84 std::map<PowerStatsPresentProfile, int> mPowerStatsPresentProfileToIdMap; 85 86 #ifdef DEBUG_VRR_POWERSTATS 87 int64_t mLastGetStateResidencyTimeNs = -1; 88 int64_t mLastPowerStatsTotalTimeNs = -1; 89 #endif 90 91 uint64_t mStartStatisticTimeNs; 92 93 std::vector<StateResidency> mStateResidency; 94 }; 95 96 } // namespace android::hardware::graphics::composer 97