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 | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame^] | 41 | : mTracerThread( |
| 42 | "InputTracer", [this]() { threadLoop(); }, |
| 43 | [this]() { mThreadWakeCondition.notify_all(); }), |
| 44 | mBackend(std::move(innerBackend)) {} |
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> |
| 56 | void ThreadedBackend<Backend>::traceMotionEvent(const TracedMotionEvent& event) { |
| 57 | std::scoped_lock lock(mLock); |
| 58 | mQueue.emplace_back(event); |
| 59 | mThreadWakeCondition.notify_all(); |
| 60 | } |
| 61 | |
| 62 | template <typename Backend> |
| 63 | void ThreadedBackend<Backend>::traceKeyEvent(const TracedKeyEvent& event) { |
| 64 | std::scoped_lock lock(mLock); |
| 65 | mQueue.emplace_back(event); |
| 66 | mThreadWakeCondition.notify_all(); |
| 67 | } |
| 68 | |
| 69 | template <typename Backend> |
| 70 | void ThreadedBackend<Backend>::traceWindowDispatch(const WindowDispatchArgs& dispatchArgs) { |
| 71 | std::scoped_lock lock(mLock); |
| 72 | mQueue.emplace_back(dispatchArgs); |
| 73 | mThreadWakeCondition.notify_all(); |
| 74 | } |
| 75 | |
| 76 | template <typename Backend> |
| 77 | void ThreadedBackend<Backend>::threadLoop() { |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame^] | 78 | std::vector<TraceEntry> entries; |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 79 | |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame^] | 80 | { // acquire lock |
| 81 | std::unique_lock lock(mLock); |
| 82 | base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 83 | |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame^] | 84 | // Wait until we need to process more events or exit. |
| 85 | mThreadWakeCondition.wait(lock, |
| 86 | [&]() REQUIRES(mLock) { return mThreadExit || !mQueue.empty(); }); |
| 87 | if (mThreadExit) { |
| 88 | return; |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 89 | } |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame^] | 90 | |
| 91 | mQueue.swap(entries); |
| 92 | } // release lock |
| 93 | |
| 94 | // Trace the events into the backend without holding the lock to reduce the amount of |
| 95 | // work performed in the critical section. |
| 96 | for (const auto& entry : entries) { |
| 97 | std::visit(Visitor{[&](const TracedMotionEvent& e) { mBackend.traceMotionEvent(e); }, |
| 98 | [&](const TracedKeyEvent& e) { mBackend.traceKeyEvent(e); }, |
| 99 | [&](const WindowDispatchArgs& args) { |
| 100 | mBackend.traceWindowDispatch(args); |
| 101 | }}, |
| 102 | entry); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 103 | } |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame^] | 104 | entries.clear(); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Explicit template instantiation for the PerfettoBackend. |
| 108 | template class ThreadedBackend<PerfettoBackend>; |
| 109 | |
| 110 | } // namespace android::inputdispatcher::trace::impl |