1 /*
2  * Copyright (C) 2020 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 #ifndef HARDWARE_GOOGLE_PIXEL_POWERSTATS_DISPLAYSTATERESIDENCYDATAPROVIDER_H
18 #define HARDWARE_GOOGLE_PIXEL_POWERSTATS_DISPLAYSTATERESIDENCYDATAPROVIDER_H
19 // TODO(b/167628903): Delete this file
20 #include <pixelpowerstats/PowerStats.h>
21 #include <utils/Looper.h>
22 #include <utils/Thread.h>
23 #include <cstdio>
24 #include <cstring>
25 #include <mutex>
26 #include <thread>
27 #include <unordered_map>
28 
29 namespace android {
30 namespace hardware {
31 namespace google {
32 namespace pixel {
33 namespace powerstats {
34 
35 class DisplayStateResidencyDataProvider : public IStateResidencyDataProvider {
36   public:
37     // id = powerEntityId to be associated with this data provider
38     // path = path to the display state file descriptor
39     // state = list of states to be tracked
40     DisplayStateResidencyDataProvider(uint32_t id, std::string path,
41                                       std::vector<std::string> states);
42     ~DisplayStateResidencyDataProvider();
43     bool getResults(
44             std::unordered_map<uint32_t, PowerEntityStateResidencyResult> &results) override;
45     std::vector<PowerEntityStateSpace> getStateSpaces() override;
46 
47   private:
48     // Poll for display state changes
49     void pollLoop();
50     // Main function to update the stats when display state change is detected
51     void updateStats();
52 
53     // File descriptor of display state
54     int mFd;
55     // Path to display state file descriptor
56     const std::string mPath;
57     // powerEntityId associated with this data provider
58     const uint32_t mPowerEntityId;
59     // List of states to track indexed by mCurState
60     std::vector<std::string> mStates;
61     // Lock to protect concurrent read/write to mResidencies and mCurState
62     std::mutex mLock;
63     // Accumulated display state stats indexed by mCurState
64     std::vector<PowerEntityStateResidencyData> mResidencies;
65     // Index of current state
66     int mCurState;
67     // Looper to facilitate polling of display state file desciptor
68     sp<Looper> mLooper;
69 
70     std::thread mThread;
71 };
72 
73 }  // namespace powerstats
74 }  // namespace pixel
75 }  // namespace google
76 }  // namespace hardware
77 }  // namespace android
78 
79 #endif  // HARDWARE_GOOGLE_PIXEL_POWERSTATS_DISPLAYSTATERESIDENCYDATAPROVIDER_H
80