Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | |
Prabir Pradhan | 4810866 | 2022-09-09 21:22:04 +0000 | [diff] [blame] | 17 | #pragma once |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 18 | |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 19 | #include "../InputDeviceMetricsSource.h" |
| 20 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 21 | #include <binder/IBinder.h> |
| 22 | #include <input/Input.h> |
| 23 | #include <unordered_map> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | namespace inputdispatcher { |
| 28 | |
| 29 | /** |
| 30 | * Describes the input event timeline for each connection. |
| 31 | * An event with the same inputEventId can go to more than 1 connection simultaneously. |
| 32 | * For each connection that the input event goes to, there will be a separate ConnectionTimeline |
| 33 | * created. |
| 34 | * To create a complete ConnectionTimeline, we must receive two calls: |
| 35 | * 1) setDispatchTimeline |
| 36 | * 2) setGraphicsTimeline |
| 37 | * |
| 38 | * In a typical scenario, the dispatch timeline is known first. Later, if a frame is produced, the |
| 39 | * graphics timeline is available. |
| 40 | */ |
| 41 | struct ConnectionTimeline { |
| 42 | // DispatchTimeline |
| 43 | nsecs_t deliveryTime; // time at which the event was sent to the receiver |
| 44 | nsecs_t consumeTime; // time at which the receiver read the event |
| 45 | nsecs_t finishTime; // time at which the finish event was received |
| 46 | // GraphicsTimeline |
| 47 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 48 | |
| 49 | ConnectionTimeline(nsecs_t deliveryTime, nsecs_t consumeTime, nsecs_t finishTime); |
| 50 | ConnectionTimeline(std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline); |
| 51 | |
| 52 | /** |
| 53 | * True if all contained timestamps are valid, false otherwise. |
| 54 | */ |
| 55 | bool isComplete() const; |
| 56 | /** |
| 57 | * Set the dispatching-related times. Return true if the operation succeeded, false if the |
| 58 | * dispatching times have already been set. If this function returns false, it likely indicates |
| 59 | * an error from the app side. |
| 60 | */ |
| 61 | bool setDispatchTimeline(nsecs_t deliveryTime, nsecs_t consumeTime, nsecs_t finishTime); |
| 62 | /** |
| 63 | * Set the graphics-related times. Return true if the operation succeeded, false if the |
| 64 | * graphics times have already been set. If this function returns false, it likely indicates |
| 65 | * an error from the app side. |
| 66 | */ |
| 67 | bool setGraphicsTimeline(std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline); |
| 68 | |
| 69 | inline bool operator==(const ConnectionTimeline& rhs) const; |
| 70 | inline bool operator!=(const ConnectionTimeline& rhs) const; |
| 71 | |
| 72 | private: |
| 73 | bool mHasDispatchTimeline = false; |
| 74 | bool mHasGraphicsTimeline = false; |
| 75 | }; |
| 76 | |
jioana | 0bdbea1 | 2024-08-10 19:26:04 +0000 | [diff] [blame] | 77 | enum class InputEventActionType : int32_t { |
| 78 | UNKNOWN_INPUT_EVENT = 0, |
| 79 | MOTION_ACTION_DOWN = 1, |
| 80 | // Motion events for ACTION_MOVE (characterizes scrolling motion) |
| 81 | MOTION_ACTION_MOVE = 2, |
| 82 | // Motion events for ACTION_UP (when the pointer first goes up) |
| 83 | MOTION_ACTION_UP = 3, |
| 84 | // Motion events for ACTION_HOVER_MOVE (pointer position on screen changes but pointer is not |
| 85 | // down) |
| 86 | MOTION_ACTION_HOVER_MOVE = 4, |
| 87 | // Motion events for ACTION_SCROLL (moving the mouse wheel) |
| 88 | MOTION_ACTION_SCROLL = 5, |
| 89 | // Key events for both ACTION_DOWN and ACTION_UP (key press and key release) |
| 90 | KEY = 6, |
| 91 | |
| 92 | ftl_first = UNKNOWN_INPUT_EVENT, |
| 93 | ftl_last = KEY, |
| 94 | // Used by latency fuzzer |
| 95 | kMaxValue = ftl_last |
| 96 | |
| 97 | }; |
| 98 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 99 | struct InputEventTimeline { |
jioana | 97cc8ac | 2024-09-09 15:01:43 +0000 | [diff] [blame] | 100 | InputEventTimeline(nsecs_t eventTime, nsecs_t readTime, uint16_t vendorId, uint16_t productId, |
| 101 | const std::set<InputDeviceUsageSource>& sources, |
jioana | 0bdbea1 | 2024-08-10 19:26:04 +0000 | [diff] [blame] | 102 | InputEventActionType inputEventActionType); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 103 | const nsecs_t eventTime; |
| 104 | const nsecs_t readTime; |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 105 | const uint16_t vendorId; |
| 106 | const uint16_t productId; |
| 107 | const std::set<InputDeviceUsageSource> sources; |
jioana | 0bdbea1 | 2024-08-10 19:26:04 +0000 | [diff] [blame] | 108 | const InputEventActionType inputEventActionType; |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 109 | |
| 110 | struct IBinderHash { |
| 111 | std::size_t operator()(const sp<IBinder>& b) const { |
| 112 | return std::hash<IBinder*>{}(b.get()); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | std::unordered_map<sp<IBinder>, ConnectionTimeline, IBinderHash> connectionTimelines; |
| 117 | |
| 118 | bool operator==(const InputEventTimeline& rhs) const; |
| 119 | }; |
| 120 | |
| 121 | class InputEventTimelineProcessor { |
| 122 | protected: |
| 123 | InputEventTimelineProcessor() {} |
| 124 | virtual ~InputEventTimelineProcessor() {} |
| 125 | |
| 126 | public: |
| 127 | /** |
| 128 | * Process the provided timeline |
| 129 | */ |
| 130 | virtual void processTimeline(const InputEventTimeline& timeline) = 0; |
| 131 | }; |
| 132 | |
| 133 | } // namespace inputdispatcher |
| 134 | } // namespace android |