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 android::base::SetMinimumLogSeverity(android::base::INFO);
43 // Parse config but do not start the looper
44 HintManager *hm = HintManager::GetInstance();
45 if (!hm) {
46 LOG(FATAL) << "HintManager Init failed";
47 }
48
49 std::shared_ptr<DisplayLowPower> dlpw = std::make_shared<DisplayLowPower>();
50
51 // single thread
52 ABinderProcess_setThreadPoolMaxThreadCount(0);
53
54 // core service
55 std::shared_ptr<Power> pw = ndk::SharedRefBase::make<Power>(dlpw);
56 ndk::SpAIBinder pwBinder = pw->asBinder();
57 AIBinder_setMinSchedulerPolicy(pwBinder.get(), SCHED_NORMAL, -20);
58
59 // extension service
60 std::shared_ptr<PowerExt> pwExt = ndk::SharedRefBase::make<PowerExt>(dlpw);
61 auto pwExtBinder = pwExt->asBinder();
62 AIBinder_setMinSchedulerPolicy(pwExtBinder.get(), SCHED_NORMAL, -20);
63
64 // attach the extension to the same binder we will be registering
65 CHECK(STATUS_OK == AIBinder_setExtension(pwBinder.get(), pwExt->asBinder().get()));
66
67 const std::string instance = std::string() + Power::descriptor + "/default";
68 binder_status_t status = AServiceManager_addService(pw->asBinder().get(), instance.c_str());
69 CHECK(status == STATUS_OK);
70 LOG(INFO) << "Pixel Power HAL AIDL Service with Extension is started.";
71
72 std::thread initThread([&]() {
73 ::android::base::WaitForProperty(kPowerHalInitProp.data(), "1");
74 HintManager::GetInstance()->Start();
75 dlpw->Init();
76 });
77 initThread.detach();
78
79 ABinderProcess_joinThreadPool();
80
81 // should not reach
82 LOG(ERROR) << "Pixel Power HAL AIDL Service with Extension just died.";
83 return EXIT_FAILURE;
84 }
85