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