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 17 #pragma once 18 19 #include "../InputDeviceMetricsSource.h" 20 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 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 99 struct InputEventTimeline { 100 InputEventTimeline(nsecs_t eventTime, nsecs_t readTime, uint16_t vendorId, uint16_t productId, 101 const std::set<InputDeviceUsageSource>& sources, 102 InputEventActionType inputEventActionType); 103 const nsecs_t eventTime; 104 const nsecs_t readTime; 105 const uint16_t vendorId; 106 const uint16_t productId; 107 const std::set<InputDeviceUsageSource> sources; 108 const InputEventActionType inputEventActionType; 109 110 struct IBinderHash { operatorInputEventTimeline::IBinderHash111 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: InputEventTimelineProcessor()123 InputEventTimelineProcessor() {} 124 125 public: ~InputEventTimelineProcessor()126 virtual ~InputEventTimelineProcessor() {} 127 128 /** 129 * Process the provided timeline 130 */ 131 virtual void processTimeline(const InputEventTimeline& timeline) = 0; 132 133 /** 134 * Trigger a push for the input event latency statistics 135 */ 136 virtual void pushLatencyStatistics() = 0; 137 138 virtual std::string dump(const char* prefix) const = 0; 139 }; 140 141 } // namespace inputdispatcher 142 } // namespace android 143