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 | #pragma once |
| 18 | |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame] | 19 | #include "InputThread.h" |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 20 | #include "InputTracingPerfettoBackend.h" |
| 21 | |
| 22 | #include <android-base/thread_annotations.h> |
| 23 | #include <mutex> |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 24 | #include <variant> |
| 25 | #include <vector> |
| 26 | |
| 27 | namespace android::inputdispatcher::trace::impl { |
| 28 | |
| 29 | /** |
| 30 | * A wrapper around an InputTracingBackend implementation that writes to the inner tracing backend |
| 31 | * from a single new thread that it creates. The new tracing thread is started when the |
| 32 | * ThreadedBackend is created, and is stopped when it is destroyed. The ThreadedBackend is |
| 33 | * thread-safe. |
| 34 | */ |
| 35 | template <typename Backend> |
| 36 | class ThreadedBackend : public InputTracingBackendInterface { |
| 37 | public: |
| 38 | ThreadedBackend(Backend&& innerBackend); |
| 39 | ~ThreadedBackend() override; |
| 40 | |
Prabir Pradhan | c7edaaa | 2024-03-15 15:31:02 +0000 | [diff] [blame] | 41 | void traceKeyEvent(const TracedKeyEvent&, const TracedEventMetadata&) override; |
| 42 | void traceMotionEvent(const TracedMotionEvent&, const TracedEventMetadata&) override; |
| 43 | void traceWindowDispatch(const WindowDispatchArgs&, const TracedEventMetadata&) override; |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 44 | |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 45 | /** Returns a function that, when called, will block until the tracing thread is idle. */ |
| 46 | std::function<void()> getIdleWaiterForTesting(); |
| 47 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 48 | private: |
| 49 | std::mutex mLock; |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 50 | bool mThreadExit GUARDED_BY(mLock){false}; |
| 51 | std::condition_variable mThreadWakeCondition; |
| 52 | Backend mBackend; |
Prabir Pradhan | 8c3b143 | 2024-02-09 23:34:16 +0000 | [diff] [blame] | 53 | using TraceEntry = |
| 54 | std::pair<std::variant<TracedKeyEvent, TracedMotionEvent, WindowDispatchArgs>, |
Prabir Pradhan | c7edaaa | 2024-03-15 15:31:02 +0000 | [diff] [blame] | 55 | TracedEventMetadata>; |
Prabir Pradhan | 4782768 | 2024-02-23 19:31:43 +0000 | [diff] [blame] | 56 | std::vector<TraceEntry> mQueue GUARDED_BY(mLock); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 57 | |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 58 | struct IdleWaiter { |
| 59 | std::mutex idleLock; |
| 60 | std::condition_variable threadIdleCondition; |
| 61 | bool isIdle GUARDED_BY(idleLock){false}; |
| 62 | }; |
| 63 | // The lazy-initialized object used to wait for the tracing thread to idle. |
| 64 | std::shared_ptr<IdleWaiter> mIdleWaiter GUARDED_BY(mLock); |
| 65 | |
Prabir Pradhan | 1cdb2fa | 2024-03-19 17:21:45 +0000 | [diff] [blame] | 66 | // InputThread stops when its destructor is called. Initialize it last so that it is the |
| 67 | // first thing to be destructed. This will guarantee the thread will not access other |
| 68 | // members that have already been destructed. |
| 69 | InputThread mTracerThread; |
| 70 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 71 | void threadLoop(); |
Prabir Pradhan | 9a9897d | 2024-03-21 21:52:57 +0000 | [diff] [blame] | 72 | void setIdleStatus(bool isIdle) REQUIRES(mLock); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | } // namespace android::inputdispatcher::trace::impl |