• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 <android-base/properties.h>
20 #include <perfmgr/HintManager.h>
21 #include <utils/Looper.h>
22 
23 #include <mutex>
24 #include <optional>
25 #include <unordered_set>
26 
27 #include "PowerHintSession.h"
28 
29 namespace aidl {
30 namespace google {
31 namespace hardware {
32 namespace power {
33 namespace impl {
34 namespace pixel {
35 
36 using ::android::Looper;
37 using ::android::Message;
38 using ::android::MessageHandler;
39 using ::android::Thread;
40 using ::android::perfmgr::HintManager;
41 
42 constexpr char kPowerHalAdpfDisableTopAppBoost[] = "vendor.powerhal.adpf.disable.hint";
43 
44 class PowerSessionManager : public MessageHandler {
45   public:
46     // current hint info
47     void updateHintMode(const std::string &mode, bool enabled);
48     void updateHintBoost(const std::string &boost, int32_t durationMs);
49     int getDisplayRefreshRate();
50     // monitoring session status
51     void addPowerSession(PowerHintSession *session);
52     void removePowerSession(PowerHintSession *session);
53     void addThreadsFromPowerSession(PowerHintSession *session);
54     void addThreadsFromPowerSessionLocked(PowerHintSession *session);
55     void removeThreadsFromPowerSession(PowerHintSession *session);
56     void removeThreadsFromPowerSessionLocked(PowerHintSession *session);
57     void setUclampMin(PowerHintSession *session, int min);
58     void setUclampMinLocked(PowerHintSession *session, int min);
59     void handleMessage(const Message &message) override;
60     void dumpToFd(int fd);
61 
62     // Singleton
getInstance()63     static sp<PowerSessionManager> getInstance() {
64         static sp<PowerSessionManager> instance = new PowerSessionManager();
65         return instance;
66     }
67 
68   private:
69     std::optional<bool> isAnyAppSessionActive();
70     void disableSystemTopAppBoost();
71     void enableSystemTopAppBoost();
72     const std::string kDisableBoostHintName;
73 
74     std::unordered_set<PowerHintSession *> mSessions;  // protected by mLock
75     std::unordered_map<int, std::unordered_set<PowerHintSession *>> mTidSessionListMap;
76     bool mActive;  // protected by mLock
77     /**
78      * mLock to pretect the above data objects opertions.
79      **/
80     std::mutex mLock;
81     int mDisplayRefreshRate;
82     // Singleton
PowerSessionManager()83     PowerSessionManager()
84         : kDisableBoostHintName(::android::base::GetProperty(kPowerHalAdpfDisableTopAppBoost,
85                                                              "ADPF_DISABLE_TA_BOOST")),
86           mActive(false),
87           mDisplayRefreshRate(60) {}
88     PowerSessionManager(PowerSessionManager const &) = delete;
89     void operator=(PowerSessionManager const &) = delete;
90 };
91 
92 class PowerHintMonitor : public Thread {
93   public:
94     void start();
95     bool threadLoop() override;
96     sp<Looper> getLooper();
97     // Singleton
getInstance()98     static sp<PowerHintMonitor> getInstance() {
99         static sp<PowerHintMonitor> instance = new PowerHintMonitor();
100         return instance;
101     }
102     PowerHintMonitor(PowerHintMonitor const &) = delete;
103     void operator=(PowerHintMonitor const &) = delete;
104 
105   private:
106     sp<Looper> mLooper;
107     // Singleton
PowerHintMonitor()108     PowerHintMonitor() : Thread(false), mLooper(new Looper(true)) {}
109 };
110 
111 }  // namespace pixel
112 }  // namespace impl
113 }  // namespace power
114 }  // namespace hardware
115 }  // namespace google
116 }  // namespace aidl
117