blob: 96e619c10d9bddeb1e94bf23f9716ee820bc5813 [file] [log] [blame]
Prabir Pradhandae52792023-12-15 07:36:40 +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
19#include "InputTracerInterface.h"
20
Prabir Pradhandae52792023-12-15 07:36:40 +000021#include <memory>
Prabir Pradhandae52792023-12-15 07:36:40 +000022
23#include "../Entry.h"
24#include "InputTracingBackendInterface.h"
25
26namespace android::inputdispatcher::trace::impl {
27
28/**
29 * The tracer implementation for InputDispatcher.
30 *
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000031 * InputTracer's responsibility is to keep track of events as they are processed by InputDispatcher,
32 * and to write the events to the tracing backend when enough information is collected. InputTracer
33 * is not thread-safe.
Prabir Pradhandae52792023-12-15 07:36:40 +000034 *
35 * See the documentation in InputTracerInterface for the API surface.
36 */
37class InputTracer : public InputTracerInterface {
38public:
39 explicit InputTracer(std::unique_ptr<InputTracingBackendInterface>);
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000040 ~InputTracer() = default;
Prabir Pradhandae52792023-12-15 07:36:40 +000041 InputTracer(const InputTracer&) = delete;
42 InputTracer& operator=(const InputTracer&) = delete;
43
44 std::unique_ptr<EventTrackerInterface> traceInboundEvent(const EventEntry&) override;
Prabir Pradhand6b2b052024-02-21 23:25:15 +000045 std::unique_ptr<EventTrackerInterface> createTrackerForSyntheticEvent() override;
Prabir Pradhandae52792023-12-15 07:36:40 +000046 void dispatchToTargetHint(const EventTrackerInterface&, const InputTarget&) override;
Prabir Pradhan4fc32e02024-06-14 16:00:58 +000047 void eventProcessingComplete(const EventTrackerInterface&,
48 nsecs_t processingTimestamp) override;
Prabir Pradhana67623c2024-02-21 06:57:36 +000049 std::unique_ptr<EventTrackerInterface> traceDerivedEvent(const EventEntry&,
50 const EventTrackerInterface&) override;
Prabir Pradhand6b2b052024-02-21 23:25:15 +000051 void traceEventDispatch(const DispatchEntry&, const EventTrackerInterface&) override;
Prabir Pradhan04a66422024-03-15 22:45:27 +000052 void setInputMethodConnectionIsActive(bool isActive) override {
53 mIsImeConnectionActive = isActive;
54 }
Prabir Pradhandae52792023-12-15 07:36:40 +000055
56private:
Prabir Pradhandae52792023-12-15 07:36:40 +000057 std::unique_ptr<InputTracingBackendInterface> mBackend;
Prabir Pradhan04a66422024-03-15 22:45:27 +000058 bool mIsImeConnectionActive{false};
Prabir Pradhandae52792023-12-15 07:36:40 +000059
Prabir Pradhana67623c2024-02-21 06:57:36 +000060 // The state of a tracked event, shared across all events derived from the original event.
Prabir Pradhandae52792023-12-15 07:36:40 +000061 struct EventState {
Prabir Pradhana67623c2024-02-21 06:57:36 +000062 explicit inline EventState(InputTracer& tracer) : tracer(tracer){};
63 ~EventState();
Prabir Pradhan1ea04a32024-02-10 03:02:59 +000064
Prabir Pradhan4fc32e02024-06-14 16:00:58 +000065 void onEventProcessingComplete(nsecs_t processingTimestamp);
Prabir Pradhane7701642024-02-22 04:06:53 +000066
Prabir Pradhana67623c2024-02-21 06:57:36 +000067 InputTracer& tracer;
Ryan Prichardbd5c2702024-08-07 14:40:14 -070068 std::vector<TracedEvent> events;
Prabir Pradhan1ea04a32024-02-10 03:02:59 +000069 bool isEventProcessingComplete{false};
Prabir Pradhan52ec3ff2024-02-22 03:26:38 +000070 // A queue to hold dispatch args from being traced until event processing is complete.
Ryan Prichardbd5c2702024-08-07 14:40:14 -070071 std::vector<WindowDispatchArgs> pendingDispatchArgs;
Prabir Pradhanac637022024-03-19 00:02:04 +000072 // The metadata should not be modified after event processing is complete.
73 TracedEventMetadata metadata{};
Prabir Pradhandae52792023-12-15 07:36:40 +000074 };
Prabir Pradhandae52792023-12-15 07:36:40 +000075
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000076 // Get the event state associated with a tracking cookie.
Prabir Pradhana67623c2024-02-21 06:57:36 +000077 std::shared_ptr<EventState>& getState(const EventTrackerInterface&);
78 bool isDerivedCookie(const EventTrackerInterface&);
Prabir Pradhandae52792023-12-15 07:36:40 +000079
Prabir Pradhanbb7a0202024-02-10 02:09:01 +000080 // Implementation of the event tracker cookie. The cookie holds the event state directly for
81 // convenience to avoid the overhead of tracking the state separately in InputTracer.
Prabir Pradhandae52792023-12-15 07:36:40 +000082 class EventTrackerImpl : public EventTrackerInterface {
83 public:
Prabir Pradhana67623c2024-02-21 06:57:36 +000084 inline EventTrackerImpl(const std::shared_ptr<EventState>& state, bool isDerivedEvent)
85 : mState(state), mIsDerived(isDerivedEvent) {}
86 EventTrackerImpl(const EventTrackerImpl&) = default;
Prabir Pradhandae52792023-12-15 07:36:40 +000087
88 private:
Prabir Pradhana67623c2024-02-21 06:57:36 +000089 mutable std::shared_ptr<EventState> mState;
90 const bool mIsDerived;
Prabir Pradhandae52792023-12-15 07:36:40 +000091
Prabir Pradhana67623c2024-02-21 06:57:36 +000092 friend std::shared_ptr<EventState>& InputTracer::getState(const EventTrackerInterface&);
93 friend bool InputTracer::isDerivedCookie(const EventTrackerInterface&);
Prabir Pradhandae52792023-12-15 07:36:40 +000094 };
Prabir Pradhandae52792023-12-15 07:36:40 +000095};
96
97} // namespace android::inputdispatcher::trace::impl