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 ANDROID_POWERHALCONTROLLER_H 18 #define ANDROID_POWERHALCONTROLLER_H 19 20 #include <android-base/thread_annotations.h> 21 #include <android/hardware/power/Boost.h> 22 #include <android/hardware/power/IPower.h> 23 #include <android/hardware/power/IPowerHintSession.h> 24 #include <android/hardware/power/Mode.h> 25 #include <powermanager/PowerHalWrapper.h> 26 27 namespace android { 28 29 namespace power { 30 31 // ------------------------------------------------------------------------------------------------- 32 33 // Connects to underlying Power HAL handles. 34 class HalConnector { 35 public: 36 HalConnector() = default; 37 virtual ~HalConnector() = default; 38 39 virtual std::unique_ptr<HalWrapper> connect(); 40 virtual void reset(); 41 }; 42 43 // ------------------------------------------------------------------------------------------------- 44 45 // Controller for Power HAL handle. 46 // This relies on HalConnector to connect to the underlying Power HAL 47 // service and reconnects to it after each failed api call. This also ensures 48 // connecting to the service is thread-safe. 49 class PowerHalController : public HalWrapper { 50 public: PowerHalController()51 PowerHalController() : PowerHalController(std::make_unique<HalConnector>()) {} PowerHalController(std::unique_ptr<HalConnector> connector)52 explicit PowerHalController(std::unique_ptr<HalConnector> connector) 53 : mHalConnector(std::move(connector)) {} 54 virtual ~PowerHalController() = default; 55 56 void init(); 57 58 virtual HalResult<void> setBoost(hardware::power::Boost boost, int32_t durationMs) override; 59 virtual HalResult<void> setMode(hardware::power::Mode mode, bool enabled) override; 60 virtual HalResult<sp<hardware::power::IPowerHintSession>> createHintSession( 61 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, 62 int64_t durationNanos) override; 63 virtual HalResult<int64_t> getHintSessionPreferredRate() override; 64 65 private: 66 std::mutex mConnectedHalMutex; 67 std::unique_ptr<HalConnector> mHalConnector; 68 69 // Shared pointers to keep global pointer and allow local copies to be used in 70 // different threads 71 std::shared_ptr<HalWrapper> mConnectedHal GUARDED_BY(mConnectedHalMutex) = nullptr; 72 const std::shared_ptr<HalWrapper> mDefaultHal = std::make_shared<EmptyHalWrapper>(); 73 74 std::shared_ptr<HalWrapper> initHal(); 75 template <typename T> 76 HalResult<T> processHalResult(HalResult<T> result, const char* functionName); 77 }; 78 79 // ------------------------------------------------------------------------------------------------- 80 81 }; // namespace power 82 83 }; // namespace android 84 85 #endif // ANDROID_POWERHALCONTROLLER_H 86