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 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 21 | #include <memory> |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 22 | |
| 23 | #include "../Entry.h" |
| 24 | #include "InputTracingBackendInterface.h" |
| 25 | |
| 26 | namespace android::inputdispatcher::trace::impl { |
| 27 | |
| 28 | /** |
| 29 | * The tracer implementation for InputDispatcher. |
| 30 | * |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 31 | * 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 Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 34 | * |
| 35 | * See the documentation in InputTracerInterface for the API surface. |
| 36 | */ |
| 37 | class InputTracer : public InputTracerInterface { |
| 38 | public: |
| 39 | explicit InputTracer(std::unique_ptr<InputTracingBackendInterface>); |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 40 | ~InputTracer() = default; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 41 | InputTracer(const InputTracer&) = delete; |
| 42 | InputTracer& operator=(const InputTracer&) = delete; |
| 43 | |
| 44 | std::unique_ptr<EventTrackerInterface> traceInboundEvent(const EventEntry&) override; |
Prabir Pradhan | d6b2b05 | 2024-02-21 23:25:15 +0000 | [diff] [blame] | 45 | std::unique_ptr<EventTrackerInterface> createTrackerForSyntheticEvent() override; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 46 | void dispatchToTargetHint(const EventTrackerInterface&, const InputTarget&) override; |
Prabir Pradhan | 4fc32e0 | 2024-06-14 16:00:58 +0000 | [diff] [blame] | 47 | void eventProcessingComplete(const EventTrackerInterface&, |
| 48 | nsecs_t processingTimestamp) override; |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 49 | std::unique_ptr<EventTrackerInterface> traceDerivedEvent(const EventEntry&, |
| 50 | const EventTrackerInterface&) override; |
Prabir Pradhan | d6b2b05 | 2024-02-21 23:25:15 +0000 | [diff] [blame] | 51 | void traceEventDispatch(const DispatchEntry&, const EventTrackerInterface&) override; |
Prabir Pradhan | 04a6642 | 2024-03-15 22:45:27 +0000 | [diff] [blame] | 52 | void setInputMethodConnectionIsActive(bool isActive) override { |
| 53 | mIsImeConnectionActive = isActive; |
| 54 | } |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 55 | |
| 56 | private: |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 57 | std::unique_ptr<InputTracingBackendInterface> mBackend; |
Prabir Pradhan | 04a6642 | 2024-03-15 22:45:27 +0000 | [diff] [blame] | 58 | bool mIsImeConnectionActive{false}; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 59 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 60 | // The state of a tracked event, shared across all events derived from the original event. |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 61 | struct EventState { |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 62 | explicit inline EventState(InputTracer& tracer) : tracer(tracer){}; |
| 63 | ~EventState(); |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 64 | |
Prabir Pradhan | 4fc32e0 | 2024-06-14 16:00:58 +0000 | [diff] [blame] | 65 | void onEventProcessingComplete(nsecs_t processingTimestamp); |
Prabir Pradhan | e770164 | 2024-02-22 04:06:53 +0000 | [diff] [blame] | 66 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 67 | InputTracer& tracer; |
| 68 | std::vector<const TracedEvent> events; |
Prabir Pradhan | 1ea04a3 | 2024-02-10 03:02:59 +0000 | [diff] [blame] | 69 | bool isEventProcessingComplete{false}; |
Prabir Pradhan | 52ec3ff | 2024-02-22 03:26:38 +0000 | [diff] [blame] | 70 | // A queue to hold dispatch args from being traced until event processing is complete. |
| 71 | std::vector<const WindowDispatchArgs> pendingDispatchArgs; |
Prabir Pradhan | ac63702 | 2024-03-19 00:02:04 +0000 | [diff] [blame] | 72 | // The metadata should not be modified after event processing is complete. |
| 73 | TracedEventMetadata metadata{}; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 74 | }; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 75 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 76 | // Get the event state associated with a tracking cookie. |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 77 | std::shared_ptr<EventState>& getState(const EventTrackerInterface&); |
| 78 | bool isDerivedCookie(const EventTrackerInterface&); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 79 | |
Prabir Pradhan | bb7a020 | 2024-02-10 02:09:01 +0000 | [diff] [blame] | 80 | // 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 Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 82 | class EventTrackerImpl : public EventTrackerInterface { |
| 83 | public: |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 84 | inline EventTrackerImpl(const std::shared_ptr<EventState>& state, bool isDerivedEvent) |
| 85 | : mState(state), mIsDerived(isDerivedEvent) {} |
| 86 | EventTrackerImpl(const EventTrackerImpl&) = default; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 87 | |
| 88 | private: |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 89 | mutable std::shared_ptr<EventState> mState; |
| 90 | const bool mIsDerived; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 91 | |
Prabir Pradhan | a67623c | 2024-02-21 06:57:36 +0000 | [diff] [blame] | 92 | friend std::shared_ptr<EventState>& InputTracer::getState(const EventTrackerInterface&); |
| 93 | friend bool InputTracer::isDerivedCookie(const EventTrackerInterface&); |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 94 | }; |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | } // namespace android::inputdispatcher::trace::impl |