blob: 7cf4e397f90f166d4ce5458f7bea99324cff082c [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
Siarhei Vishniakoua925de12024-09-19 19:02:24 -070029bool applyInputEventProfile(const Thread& thread) {
30#if defined(__ANDROID__)
31 return SetTaskProfiles(thread.getTid(), {"InputPolicy"});
32#else
33 // Since thread information is not available and there's no benefit of
34 // applying the task profile on host, return directly.
35 return true;
36#endif
37}
38
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070039// Implementation of Thread from libutils.
40class InputThreadImpl : public Thread {
41public:
42 explicit InputThreadImpl(std::function<void()> loop)
43 : Thread(/* canCallJava */ true), mThreadLoop(loop) {}
44
45 ~InputThreadImpl() {}
46
47private:
48 std::function<void()> mThreadLoop;
49
50 bool threadLoop() override {
51 mThreadLoop();
52 return true;
53 }
54};
55
56} // namespace
57
Siarhei Vishniakouf53fa6b2024-09-19 17:42:42 -070058InputThread::InputThread(std::string name, std::function<void()> loop, std::function<void()> wake,
59 bool isInCriticalPath)
Siarhei Vishniakoua925de12024-09-19 19:02:24 -070060 : mThreadWake(wake) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070061 mThread = sp<InputThreadImpl>::make(loop);
Siarhei Vishniakoua925de12024-09-19 19:02:24 -070062 mThread->run(name.c_str(), ANDROID_PRIORITY_URGENT_DISPLAY);
Siarhei Vishniakouf53fa6b2024-09-19 17:42:42 -070063 if (input_flags::enable_input_policy_profile() && isInCriticalPath) {
Siarhei Vishniakoua925de12024-09-19 19:02:24 -070064 if (!applyInputEventProfile(*mThread)) {
Jerry Chang27db62f2024-06-19 11:38:29 +000065 LOG(ERROR) << "Couldn't apply input policy profile for " << name;
66 }
67 }
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070068}
69
70InputThread::~InputThread() {
71 mThread->requestExit();
72 if (mThreadWake) {
73 mThreadWake();
74 }
75 mThread->requestExitAndWait();
76}
77
78bool InputThread::isCallingThread() {
Siarhei Vishniakou31977182022-09-30 08:51:23 -070079#if defined(__ANDROID__)
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070080 return gettid() == mThread->getTid();
Siarhei Vishniakou31977182022-09-30 08:51:23 -070081#else
82 // Assume that the caller is doing everything correctly,
83 // since thread information is not available on host
84 return false;
85#endif
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070086}
87
Siarhei Vishniakouf53fa6b2024-09-19 17:42:42 -070088} // namespace android