Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2024 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 "InputTracer" |
| 18 | |
| 19 | #include "ThreadedBackend.h" |
| 20 | |
| 21 | #include "InputTracingPerfettoBackend.h" |
| 22 | |
| 23 | #include <android-base/logging.h> |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 24 | |
| 25 | namespace android::inputdispatcher::trace::impl { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | // Helper to std::visit with lambdas. |
| 30 | template <typename... V> |
| 31 | struct Visitor : V... { |
| 32 | using V::operator()...; |
| 33 | }; |
| 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | // --- ThreadedBackend --- |
| 38 | |
| 39 | template <typename Backend> |
| 40 | ThreadedBackend<Backend>::ThreadedBackend(Backend&& innerBackend) |
Prabir Pradhan | 1cdb2fa | 2024-03-19 17:21:45 +0000 | [diff] [blame] | 41 | : mBackend(std::move(innerBackend)), |
| 42 | mTracerThread( |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame] | 43 | "InputTracer", [this]() { threadLoop(); }, |
Prabir Pradhan | 1cdb2fa | 2024-03-19 17:21:45 +0000 | [diff] [blame] | 44 | [this]() { mThreadWakeCondition.notify_all(); }) {} |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 45 | |
| 46 | template <typename Backend> |
| 47 | ThreadedBackend<Backend>::~ThreadedBackend() { |
| 48 | { |
| 49 | std::scoped_lock lock(mLock); |
| 50 | mThreadExit = true; |
| 51 | } |
| 52 | mThreadWakeCondition.notify_all(); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | template <typename Backend> |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 56 | void ThreadedBackend<Backend>::traceMotionEvent(const TracedMotionEvent& event, |
Prabir Pradhan | c7edaaa | 2024-03-15 15:31:02 +0000 | [diff] [blame] | 57 | const TracedEventMetadata& metadata) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 58 | std::scoped_lock lock(mLock); |
Prabir Pradhan | c7edaaa | 2024-03-15 15:31:02 +0000 | [diff] [blame] | 59 | mQueue.emplace_back(event, metadata); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 60 | mThreadWakeCondition.notify_all(); |
| 61 | } |
| 62 | |
| 63 | template <typename Backend> |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 64 | void ThreadedBackend<Backend>::traceKeyEvent(const TracedKeyEvent& event, |
Prabir Pradhan | c7edaaa | 2024-03-15 15:31:02 +0000 | [diff] [blame] | 65 | const TracedEventMetadata& metadata) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 66 | std::scoped_lock lock(mLock); |
Prabir Pradhan | c7edaaa | 2024-03-15 15:31:02 +0000 | [diff] [blame] | 67 | mQueue.emplace_back(event, metadata); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 68 | mThreadWakeCondition.notify_all(); |
| 69 | } |
| 70 | |
| 71 | template <typename Backend> |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 72 | void ThreadedBackend<Backend>::traceWindowDispatch(const WindowDispatchArgs& dispatchArgs, |
Prabir Pradhan | c7edaaa | 2024-03-15 15:31:02 +0000 | [diff] [blame] | 73 | const TracedEventMetadata& metadata) { |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 74 | std::scoped_lock lock(mLock); |
Prabir Pradhan | c7edaaa | 2024-03-15 15:31:02 +0000 | [diff] [blame] | 75 | mQueue.emplace_back(dispatchArgs, metadata); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 76 | mThreadWakeCondition.notify_all(); |
| 77 | } |
| 78 | |
| 79 | template <typename Backend> |
| 80 | void ThreadedBackend<Backend>::threadLoop() { |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame] | 81 | std::vector<TraceEntry> entries; |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 82 | |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame] | 83 | { // acquire lock |
| 84 | std::unique_lock lock(mLock); |
| 85 | base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 86 | |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame^] | 87 | setIdleStatus(true); |
| 88 | |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame] | 89 | // Wait until we need to process more events or exit. |
| 90 | mThreadWakeCondition.wait(lock, |
| 91 | [&]() REQUIRES(mLock) { return mThreadExit || !mQueue.empty(); }); |
| 92 | if (mThreadExit) { |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame^] | 93 | setIdleStatus(true); |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame] | 94 | return; |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 95 | } |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame] | 96 | |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame^] | 97 | setIdleStatus(false); |
| 98 | |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame] | 99 | mQueue.swap(entries); |
| 100 | } // release lock |
| 101 | |
| 102 | // Trace the events into the backend without holding the lock to reduce the amount of |
| 103 | // work performed in the critical section. |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 104 | for (const auto& [entry, traceArgs] : entries) { |
| 105 | std::visit(Visitor{[&](const TracedMotionEvent& e) { |
| 106 | mBackend.traceMotionEvent(e, traceArgs); |
| 107 | }, |
| 108 | [&](const TracedKeyEvent& e) { mBackend.traceKeyEvent(e, traceArgs); }, |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame] | 109 | [&](const WindowDispatchArgs& args) { |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 110 | mBackend.traceWindowDispatch(args, traceArgs); |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame] | 111 | }}, |
| 112 | entry); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 113 | } |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame] | 114 | entries.clear(); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame^] | 117 | template <typename Backend> |
| 118 | std::function<void()> ThreadedBackend<Backend>::getIdleWaiterForTesting() { |
| 119 | std::scoped_lock lock(mLock); |
| 120 | if (!mIdleWaiter) { |
| 121 | mIdleWaiter = std::make_shared<IdleWaiter>(); |
| 122 | } |
| 123 | |
| 124 | // Return a lambda that holds a strong reference to the idle waiter, whose lifetime can extend |
| 125 | // beyond this threaded backend object. |
| 126 | return [idleWaiter = mIdleWaiter]() { |
| 127 | std::unique_lock idleLock(idleWaiter->idleLock); |
| 128 | base::ScopedLockAssertion assumeLocked(idleWaiter->idleLock); |
| 129 | idleWaiter->threadIdleCondition.wait(idleLock, [&]() REQUIRES(idleWaiter->idleLock) { |
| 130 | return idleWaiter->isIdle; |
| 131 | }); |
| 132 | }; |
| 133 | } |
| 134 | |
| 135 | template <typename Backend> |
| 136 | void ThreadedBackend<Backend>::setIdleStatus(bool isIdle) { |
| 137 | if (!mIdleWaiter) { |
| 138 | return; |
| 139 | } |
| 140 | std::scoped_lock idleLock(mIdleWaiter->idleLock); |
| 141 | mIdleWaiter->isIdle = isIdle; |
| 142 | if (isIdle) { |
| 143 | mIdleWaiter->threadIdleCondition.notify_all(); |
| 144 | } |
| 145 | } |
| 146 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 147 | // Explicit template instantiation for the PerfettoBackend. |
| 148 | template class ThreadedBackend<PerfettoBackend>; |
| 149 | |
| 150 | } // namespace android::inputdispatcher::trace::impl |