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 "PowerHintSession.h"
20 
21 #include <android-base/properties.h>
22 #include <perfmgr/HintManager.h>
23 #include <utils/Looper.h>
24 
25 #include <mutex>
26 #include <optional>
27 #include <unordered_set>
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     int getDisplayRefreshRate();
49     // monitoring session status
50     void addPowerSession(PowerHintSession *session);
51     void removePowerSession(PowerHintSession *session);
52 
53     void handleMessage(const Message &message) override;
54     void setHintManager(std::shared_ptr<HintManager> const &hint_manager);
55 
56     // Singleton
getInstance()57     static sp<PowerSessionManager> getInstance() {
58         static sp<PowerSessionManager> instance = new PowerSessionManager();
59         return instance;
60     }
61 
62   private:
63     std::optional<bool> isAnySessionActive();
64     void disableSystemTopAppBoost();
65     void enableSystemTopAppBoost();
66     const std::string kDisableBoostHintName;
67     std::shared_ptr<HintManager> mHintManager;
68     std::unordered_set<PowerHintSession *> mSessions;  // protected by mLock
69     std::unordered_map<int, int> mTidRefCountMap;      // protected by mLock
70     std::mutex mLock;
71     int mDisplayRefreshRate;
72     bool mActive;  // protected by mLock
73     // Singleton
PowerSessionManager()74     PowerSessionManager()
75         : kDisableBoostHintName(::android::base::GetProperty(kPowerHalAdpfDisableTopAppBoost,
76                                                              "ADPF_DISABLE_TA_BOOST")),
77           mHintManager(nullptr),
78           mDisplayRefreshRate(60),
79           mActive(false) {}
80     PowerSessionManager(PowerSessionManager const &) = delete;
81     void operator=(PowerSessionManager const &) = delete;
82 };
83 
84 class PowerHintMonitor : public Thread {
85   public:
86     void start();
87     bool threadLoop() override;
88     sp<Looper> getLooper();
89     // Singleton
getInstance()90     static sp<PowerHintMonitor> getInstance() {
91         static sp<PowerHintMonitor> instance = new PowerHintMonitor();
92         return instance;
93     }
94     PowerHintMonitor(PowerHintMonitor const &) = delete;
95     void operator=(PowerHintMonitor const &) = delete;
96 
97   private:
98     sp<Looper> mLooper;
99     // Singleton
PowerHintMonitor()100     PowerHintMonitor() : Thread(false), mLooper(new Looper(true)) {}
101 };
102 
103 }  // namespace pixel
104 }  // namespace impl
105 }  // namespace power
106 }  // namespace hardware
107 }  // namespace google
108 }  // namespace aidl
109