blob: 3bfc72b4d01016c728c8debe9ec4e12ef2e62fb0 [file] [log] [blame]
Prabir Pradhanbb7a0202024-02-10 02:09:01 +00001/*
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 Pradhan47827682024-02-23 19:31:43 +000019#include "InputThread.h"
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000020#include "InputTracingPerfettoBackend.h"
21
22#include <android-base/thread_annotations.h>
23#include <mutex>
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000024#include <variant>
25#include <vector>
26
27namespace 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 */
35template <typename Backend>
36class ThreadedBackend : public InputTracingBackendInterface {
37public:
38 ThreadedBackend(Backend&& innerBackend);
39 ~ThreadedBackend() override;
40
Prabir Pradhan8c3b1432024-02-09 23:34:16 +000041 void traceKeyEvent(const TracedKeyEvent&, const TracedEventArgs&) override;
42 void traceMotionEvent(const TracedMotionEvent&, const TracedEventArgs&) override;
43 void traceWindowDispatch(const WindowDispatchArgs&, const TracedEventArgs&) override;
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000044
45private:
46 std::mutex mLock;
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000047 bool mThreadExit GUARDED_BY(mLock){false};
48 std::condition_variable mThreadWakeCondition;
49 Backend mBackend;
Prabir Pradhan8c3b1432024-02-09 23:34:16 +000050 using TraceEntry =
51 std::pair<std::variant<TracedKeyEvent, TracedMotionEvent, WindowDispatchArgs>,
52 TracedEventArgs>;
Prabir Pradhan47827682024-02-23 19:31:43 +000053 std::vector<TraceEntry> mQueue GUARDED_BY(mLock);
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000054
Prabir Pradhan1cdb2fa2024-03-19 17:21:45 +000055 // InputThread stops when its destructor is called. Initialize it last so that it is the
56 // first thing to be destructed. This will guarantee the thread will not access other
57 // members that have already been destructed.
58 InputThread mTracerThread;
59
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000060 void threadLoop();
61};
62
63} // namespace android::inputdispatcher::trace::impl