Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +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 | |
| 19 | #include "InputTracerInterface.h" |
| 20 | |
| 21 | #include <android-base/thread_annotations.h> |
| 22 | #include <gui/WindowInfo.h> |
| 23 | |
| 24 | #include <memory> |
| 25 | #include <mutex> |
| 26 | #include <thread> |
| 27 | #include <unordered_set> |
| 28 | #include <vector> |
| 29 | |
| 30 | #include "../Entry.h" |
| 31 | #include "InputTracingBackendInterface.h" |
| 32 | |
| 33 | namespace android::inputdispatcher::trace::impl { |
| 34 | |
| 35 | /** |
| 36 | * The tracer implementation for InputDispatcher. |
| 37 | * |
| 38 | * InputTracer is thread-safe, so it can be called from any thread. Upon construction, InputTracer |
| 39 | * will start its own thread that it uses for write events into the tracing backend. That is the |
| 40 | * one and only thread that will interact with the tracing backend, since the Perfetto backend |
| 41 | * uses thread-local storage. |
| 42 | * |
| 43 | * See the documentation in InputTracerInterface for the API surface. |
| 44 | */ |
| 45 | class InputTracer : public InputTracerInterface { |
| 46 | public: |
| 47 | explicit InputTracer(std::unique_ptr<InputTracingBackendInterface>); |
| 48 | ~InputTracer() override; |
| 49 | InputTracer(const InputTracer&) = delete; |
| 50 | InputTracer& operator=(const InputTracer&) = delete; |
| 51 | |
| 52 | std::unique_ptr<EventTrackerInterface> traceInboundEvent(const EventEntry&) override; |
| 53 | void dispatchToTargetHint(const EventTrackerInterface&, const InputTarget&) override; |
| 54 | void eventProcessingComplete(const EventTrackerInterface&) override; |
| 55 | void traceEventDispatch(const DispatchEntry&, const EventTrackerInterface*) override; |
| 56 | |
| 57 | private: |
| 58 | std::mutex mLock; |
| 59 | std::thread mTracerThread; |
| 60 | bool mThreadExit GUARDED_BY(mLock){false}; |
| 61 | std::condition_variable mThreadWakeCondition; |
| 62 | std::unique_ptr<InputTracingBackendInterface> mBackend; |
| 63 | |
| 64 | // The state of a tracked event. |
| 65 | struct EventState { |
| 66 | const TracedEvent event; |
| 67 | // TODO(b/210460522): Add additional args for tracking event sensitivity and |
| 68 | // dispatch target UIDs. |
| 69 | }; |
| 70 | std::vector<const EventState> mTraceQueue GUARDED_BY(mLock); |
| 71 | |
| 72 | // Provides thread-safe access to the state from an event tracker cookie. |
| 73 | std::optional<EventState>& getState(const EventTrackerInterface&) REQUIRES(mLock); |
| 74 | |
| 75 | // Implementation of the event tracker cookie. |
| 76 | class EventTrackerImpl : public EventTrackerInterface { |
| 77 | public: |
| 78 | explicit EventTrackerImpl(InputTracer&, TracedEvent&& entry); |
| 79 | virtual ~EventTrackerImpl() override; |
| 80 | |
| 81 | private: |
| 82 | InputTracer& mTracer; |
| 83 | // This event tracker cookie will only hold the state as long as it has not been written |
| 84 | // to the trace. The state is released when the event is written to the trace. |
| 85 | mutable std::optional<EventState> mLockedState; |
| 86 | |
| 87 | // Only allow InputTracer access to the locked state through getTrackerState() to ensure |
| 88 | // that the InputTracer lock is held when this is accessed. |
| 89 | friend std::optional<EventState>& InputTracer::getState(const EventTrackerInterface&); |
| 90 | }; |
| 91 | |
| 92 | void threadLoop(); |
| 93 | void writeEventsToBackend(const std::vector<const EventState>& events); |
| 94 | }; |
| 95 | |
| 96 | } // namespace android::inputdispatcher::trace::impl |