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