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 setUclampMin(PowerHintSession *session, int min); 54 void setUclampMinLocked(PowerHintSession *session, int min); 55 void handleMessage(const Message &message) override; 56 void dumpToFd(int fd); 57 58 // Singleton getInstance()59 static sp<PowerSessionManager> getInstance() { 60 static sp<PowerSessionManager> instance = new PowerSessionManager(); 61 return instance; 62 } 63 64 private: 65 class WakeupHandler : public MessageHandler { 66 public: WakeupHandler()67 WakeupHandler() {} 68 void handleMessage(const Message &message) override; 69 }; 70 71 private: 72 void wakeSessions(); 73 std::optional<bool> isAnyAppSessionActive(); 74 void disableSystemTopAppBoost(); 75 void enableSystemTopAppBoost(); 76 const std::string kDisableBoostHintName; 77 78 std::unordered_set<PowerHintSession *> mSessions; // protected by mLock 79 std::unordered_map<int, int> mTidRefCountMap; // protected by mLock 80 std::unordered_map<int, std::unordered_set<PowerHintSession *>> mTidSessionListMap; 81 sp<WakeupHandler> mWakeupHandler; 82 bool mActive; // protected by mLock 83 /** 84 * mLock to pretect the above data objects opertions. 85 **/ 86 std::mutex mLock; 87 int mDisplayRefreshRate; 88 // Singleton PowerSessionManager()89 PowerSessionManager() 90 : kDisableBoostHintName(::android::base::GetProperty(kPowerHalAdpfDisableTopAppBoost, 91 "ADPF_DISABLE_TA_BOOST")), 92 mActive(false), 93 mDisplayRefreshRate(60) { 94 mWakeupHandler = sp<WakeupHandler>(new WakeupHandler()); 95 } 96 PowerSessionManager(PowerSessionManager const &) = delete; 97 void operator=(PowerSessionManager const &) = delete; 98 }; 99 100 class PowerHintMonitor : public Thread { 101 public: 102 void start(); 103 bool threadLoop() override; 104 sp<Looper> getLooper(); 105 // Singleton getInstance()106 static sp<PowerHintMonitor> getInstance() { 107 static sp<PowerHintMonitor> instance = new PowerHintMonitor(); 108 return instance; 109 } 110 PowerHintMonitor(PowerHintMonitor const &) = delete; 111 void operator=(PowerHintMonitor const &) = delete; 112 113 private: 114 sp<Looper> mLooper; 115 // Singleton PowerHintMonitor()116 PowerHintMonitor() : Thread(false), mLooper(new Looper(true)) {} 117 }; 118 119 } // namespace pixel 120 } // namespace impl 121 } // namespace power 122 } // namespace hardware 123 } // namespace google 124 } // namespace aidl 125