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 #define LOG_TAG "android.hardware.power-service.pixel.ext-libperfmgr"
18 
19 #include "PowerExt.h"
20 
21 #include <android-base/file.h>
22 #include <android-base/logging.h>
23 #include <android-base/properties.h>
24 #include <android-base/stringprintf.h>
25 #include <android-base/strings.h>
26 #include <perfmgr/HintManager.h>
27 #include <utils/Log.h>
28 
29 #include <mutex>
30 
31 #include "PowerSessionManager.h"
32 
33 namespace aidl {
34 namespace google {
35 namespace hardware {
36 namespace power {
37 namespace impl {
38 namespace pixel {
39 
40 using ::android::perfmgr::HintManager;
41 
setMode(const std::string & mode,bool enabled)42 ndk::ScopedAStatus PowerExt::setMode(const std::string &mode, bool enabled) {
43     LOG(DEBUG) << "PowerExt setMode: " << mode << " to: " << enabled;
44 
45     if (enabled) {
46         HintManager::GetInstance()->DoHint(mode);
47     } else {
48         HintManager::GetInstance()->EndHint(mode);
49     }
50     if (HintManager::GetInstance()->IsAdpfSupported()) {
51         PowerSessionManager<>::getInstance()->updateHintMode(mode, enabled);
52     }
53 
54     return ndk::ScopedAStatus::ok();
55 }
56 
isModeSupported(const std::string & mode,bool * _aidl_return)57 ndk::ScopedAStatus PowerExt::isModeSupported(const std::string &mode, bool *_aidl_return) {
58     bool supported = HintManager::GetInstance()->IsHintSupported(mode);
59 
60     if (!supported && HintManager::GetInstance()->IsAdpfProfileSupported(mode)) {
61         supported = true;
62     }
63     LOG(INFO) << "PowerExt mode " << mode << " isModeSupported: " << supported;
64     *_aidl_return = supported;
65     return ndk::ScopedAStatus::ok();
66 }
67 
setBoost(const std::string & boost,int32_t durationMs)68 ndk::ScopedAStatus PowerExt::setBoost(const std::string &boost, int32_t durationMs) {
69     LOG(DEBUG) << "PowerExt setBoost: " << boost << " duration: " << durationMs;
70 
71     if (durationMs > 0) {
72         HintManager::GetInstance()->DoHint(boost, std::chrono::milliseconds(durationMs));
73     } else if (durationMs == 0) {
74         HintManager::GetInstance()->DoHint(boost);
75     } else {
76         HintManager::GetInstance()->EndHint(boost);
77     }
78 
79     return ndk::ScopedAStatus::ok();
80 }
81 
isBoostSupported(const std::string & boost,bool * _aidl_return)82 ndk::ScopedAStatus PowerExt::isBoostSupported(const std::string &boost, bool *_aidl_return) {
83     bool supported = HintManager::GetInstance()->IsHintSupported(boost);
84     if (!supported && HintManager::GetInstance()->IsAdpfProfileSupported(boost)) {
85         supported = true;
86     }
87     LOG(INFO) << "PowerExt boost " << boost << " isBoostSupported: " << supported;
88     *_aidl_return = supported;
89     return ndk::ScopedAStatus::ok();
90 }
91 
92 }  // namespace pixel
93 }  // namespace impl
94 }  // namespace power
95 }  // namespace hardware
96 }  // namespace google
97 }  // namespace aidl
98