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 "powerhal-libperfmgr"
18 
19 #include <thread>
20 
21 #include <android-base/logging.h>
22 #include <android-base/properties.h>
23 #include <android/binder_manager.h>
24 #include <android/binder_process.h>
25 
26 #include "Power.h"
27 #include "PowerExt.h"
28 #include "PowerSessionManager.h"
29 #include "disp-power/DisplayLowPower.h"
30 
31 using aidl::google::hardware::power::impl::pixel::DisplayLowPower;
32 using aidl::google::hardware::power::impl::pixel::Power;
33 using aidl::google::hardware::power::impl::pixel::PowerExt;
34 using aidl::google::hardware::power::impl::pixel::PowerHintMonitor;
35 using aidl::google::hardware::power::impl::pixel::PowerSessionManager;
36 using ::android::perfmgr::HintManager;
37 
38 constexpr std::string_view kPowerHalInitProp("vendor.powerhal.init");
39 constexpr std::string_view kConfigProperty("vendor.powerhal.config");
40 constexpr std::string_view kConfigDefaultFileName("powerhint.json");
41 
main()42 int main() {
43     const std::string config_path =
44             "/vendor/etc/" +
45             android::base::GetProperty(kConfigProperty.data(), kConfigDefaultFileName.data());
46     LOG(INFO) << "Pixel Power HAL AIDL Service with Extension is starting with config: "
47               << config_path;
48 
49     // Parse config but do not start the looper
50     std::shared_ptr<HintManager> hm = HintManager::GetFromJSON(config_path, false);
51     if (!hm) {
52         LOG(FATAL) << "Invalid config: " << config_path;
53     }
54 
55     std::shared_ptr<DisplayLowPower> dlpw = std::make_shared<DisplayLowPower>();
56 
57     // single thread
58     ABinderProcess_setThreadPoolMaxThreadCount(0);
59 
60     // core service
61     std::shared_ptr<Power> pw = ndk::SharedRefBase::make<Power>(hm, dlpw);
62     ndk::SpAIBinder pwBinder = pw->asBinder();
63 
64     // extension service
65     std::shared_ptr<PowerExt> pwExt = ndk::SharedRefBase::make<PowerExt>(hm, dlpw);
66 
67     // attach the extension to the same binder we will be registering
68     CHECK(STATUS_OK == AIBinder_setExtension(pwBinder.get(), pwExt->asBinder().get()));
69 
70     const std::string instance = std::string() + Power::descriptor + "/default";
71     binder_status_t status = AServiceManager_addService(pw->asBinder().get(), instance.c_str());
72     CHECK(status == STATUS_OK);
73     LOG(INFO) << "Pixel Power HAL AIDL Service with Extension is started.";
74 
75     if (::android::base::GetIntProperty("vendor.powerhal.adpf.rate", -1) != -1) {
76         PowerHintMonitor::getInstance()->start();
77         PowerSessionManager::getInstance()->setHintManager(hm);
78     }
79 
80     std::thread initThread([&]() {
81         ::android::base::WaitForProperty(kPowerHalInitProp.data(), "1");
82         hm->Start();
83         dlpw->Init();
84     });
85     initThread.detach();
86 
87     ABinderProcess_joinThreadPool();
88 
89     // should not reach
90     LOG(ERROR) << "Pixel Power HAL AIDL Service with Extension just died.";
91     return EXIT_FAILURE;
92 }
93