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 <vector>
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 "../display/common/Constants.h"
26 #include "PowerStatsProfileTokenGenerator.h"
27 
28 // #define DEBUG_VRR_POWERSTATS 1
29 
30 namespace android::hardware::graphics::composer {
31 
32 using aidl::android::hardware::power::stats::State;
33 using aidl::android::hardware::power::stats::StateResidency;
34 
35 typedef std::vector<StateResidency> StateResidencies;
36 
37 class DisplayStateResidencyProvider {
38 public:
39     DisplayStateResidencyProvider(
40             std::shared_ptr<CommonDisplayContextProvider> displayContextProvider,
41             std::shared_ptr<StatisticsProvider> statisticsProvider);
42 
43     void getStateResidency(std::vector<StateResidency>* stats);
44 
45     const std::vector<State>& getStates();
46 
47     DisplayStateResidencyProvider(const DisplayStateResidencyProvider& other) = delete;
48     DisplayStateResidencyProvider& operator=(const DisplayStateResidencyProvider& other) = delete;
49 
50 private:
51     static const std::vector<int> kActivePowerModes;
52     static const std::vector<RefreshSource> kRefreshSource;
53 
54     void mapStatistics();
55     uint64_t aggregateStatistics();
56 
57     void generatePowerStatsStates();
58 
59     void generateUniqueStates();
60 
61     std::shared_ptr<CommonDisplayContextProvider> mDisplayContextProvider;
62 
63     std::shared_ptr<StatisticsProvider> mStatisticsProvider;
64 
65     PowerStatsProfileTokenGenerator mPowerStatsProfileTokenGenerator;
66 
67     std::set<std::pair<PowerStatsProfile, std::string>> mUniqueStates;
68     std::vector<State> mStates;
69     std::map<PowerStatsProfile, int> mPowerStatsProfileToIdMap;
70 
71 #ifdef DEBUG_VRR_POWERSTATS
72     int64_t mLastGetStateResidencyTimeNs = -1;
73     int64_t mLastPowerStatsTotalTimeNs = -1;
74 #endif
75 
76     uint64_t mStartStatisticTimeNs;
77 
78     std::vector<StateResidency> mStateResidency;
79 };
80 
81 } // namespace android::hardware::graphics::composer
82