blob: 449eb45b4bf23013dd97d5c2b12d30c4b6c5692f [file] [log] [blame]
Prabir Pradhan5a57cff2019-10-31 18:40:33 -07001/*
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#include "InputThread.h"
18
Jerry Chang27db62f2024-06-19 11:38:29 +000019#include <android-base/logging.h>
20#include <com_android_input_flags.h>
21#include <processgroup/processgroup.h>
22
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070023namespace android {
24
Jerry Chang27db62f2024-06-19 11:38:29 +000025namespace input_flags = com::android::input::flags;
26
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070027namespace {
28
29// Implementation of Thread from libutils.
30class InputThreadImpl : public Thread {
31public:
32 explicit InputThreadImpl(std::function<void()> loop)
33 : Thread(/* canCallJava */ true), mThreadLoop(loop) {}
34
35 ~InputThreadImpl() {}
36
37private:
38 std::function<void()> mThreadLoop;
39
40 bool threadLoop() override {
41 mThreadLoop();
42 return true;
43 }
44};
45
46} // namespace
47
48InputThread::InputThread(std::string name, std::function<void()> loop, std::function<void()> wake)
49 : mName(name), mThreadWake(wake) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070050 mThread = sp<InputThreadImpl>::make(loop);
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070051 mThread->run(mName.c_str(), ANDROID_PRIORITY_URGENT_DISPLAY);
Jerry Chang27db62f2024-06-19 11:38:29 +000052 if (input_flags::enable_input_policy_profile()) {
53 if (!applyInputEventProfile()) {
54 LOG(ERROR) << "Couldn't apply input policy profile for " << name;
55 }
56 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070057}
58
59InputThread::~InputThread() {
60 mThread->requestExit();
61 if (mThreadWake) {
62 mThreadWake();
63 }
64 mThread->requestExitAndWait();
65}
66
67bool InputThread::isCallingThread() {
Siarhei Vishniakou31977182022-09-30 08:51:23 -070068#if defined(__ANDROID__)
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070069 return gettid() == mThread->getTid();
Siarhei Vishniakou31977182022-09-30 08:51:23 -070070#else
71 // Assume that the caller is doing everything correctly,
72 // since thread information is not available on host
73 return false;
74#endif
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070075}
76
Jerry Chang27db62f2024-06-19 11:38:29 +000077bool InputThread::applyInputEventProfile() {
78#if defined(__ANDROID__)
79 return SetTaskProfiles(mThread->getTid(), {"InputPolicy"});
80#else
81 // Since thread information is not available and there's no benefit of
82 // applying the task profile on host, return directly.
83 return true;
84#endif
85}
86
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070087} // namespace android