blob: bd4b192e22b31bff05d221358fee49ab174143fd [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
Siarhei Vishniakouf53fa6b2024-09-19 17:42:42 -070048InputThread::InputThread(std::string name, std::function<void()> loop, std::function<void()> wake,
49 bool isInCriticalPath)
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070050 : mName(name), mThreadWake(wake) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070051 mThread = sp<InputThreadImpl>::make(loop);
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070052 mThread->run(mName.c_str(), ANDROID_PRIORITY_URGENT_DISPLAY);
Siarhei Vishniakouf53fa6b2024-09-19 17:42:42 -070053 if (input_flags::enable_input_policy_profile() && isInCriticalPath) {
Jerry Chang27db62f2024-06-19 11:38:29 +000054 if (!applyInputEventProfile()) {
55 LOG(ERROR) << "Couldn't apply input policy profile for " << name;
56 }
57 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070058}
59
60InputThread::~InputThread() {
61 mThread->requestExit();
62 if (mThreadWake) {
63 mThreadWake();
64 }
65 mThread->requestExitAndWait();
66}
67
68bool InputThread::isCallingThread() {
Siarhei Vishniakou31977182022-09-30 08:51:23 -070069#if defined(__ANDROID__)
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070070 return gettid() == mThread->getTid();
Siarhei Vishniakou31977182022-09-30 08:51:23 -070071#else
72 // Assume that the caller is doing everything correctly,
73 // since thread information is not available on host
74 return false;
75#endif
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070076}
77
Jerry Chang27db62f2024-06-19 11:38:29 +000078bool InputThread::applyInputEventProfile() {
79#if defined(__ANDROID__)
80 return SetTaskProfiles(mThread->getTid(), {"InputPolicy"});
81#else
82 // Since thread information is not available and there's no benefit of
83 // applying the task profile on host, return directly.
84 return true;
85#endif
86}
87
Siarhei Vishniakouf53fa6b2024-09-19 17:42:42 -070088} // namespace android