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 #ifndef _UI_INPUT_INPUTDISPATCHER_INPUTEVENTTIMELINE_H 18 #define _UI_INPUT_INPUTDISPATCHER_INPUTEVENTTIMELINE_H 19 20 #include <binder/IBinder.h> 21 #include <input/Input.h> 22 #include <unordered_map> 23 24 namespace android { 25 26 namespace inputdispatcher { 27 28 /** 29 * Describes the input event timeline for each connection. 30 * An event with the same inputEventId can go to more than 1 connection simultaneously. 31 * For each connection that the input event goes to, there will be a separate ConnectionTimeline 32 * created. 33 * To create a complete ConnectionTimeline, we must receive two calls: 34 * 1) setDispatchTimeline 35 * 2) setGraphicsTimeline 36 * 37 * In a typical scenario, the dispatch timeline is known first. Later, if a frame is produced, the 38 * graphics timeline is available. 39 */ 40 struct ConnectionTimeline { 41 // DispatchTimeline 42 nsecs_t deliveryTime; // time at which the event was sent to the receiver 43 nsecs_t consumeTime; // time at which the receiver read the event 44 nsecs_t finishTime; // time at which the finish event was received 45 // GraphicsTimeline 46 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; 47 48 ConnectionTimeline(nsecs_t deliveryTime, nsecs_t consumeTime, nsecs_t finishTime); 49 ConnectionTimeline(std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline); 50 51 /** 52 * True if all contained timestamps are valid, false otherwise. 53 */ 54 bool isComplete() const; 55 /** 56 * Set the dispatching-related times. Return true if the operation succeeded, false if the 57 * dispatching times have already been set. If this function returns false, it likely indicates 58 * an error from the app side. 59 */ 60 bool setDispatchTimeline(nsecs_t deliveryTime, nsecs_t consumeTime, nsecs_t finishTime); 61 /** 62 * Set the graphics-related times. Return true if the operation succeeded, false if the 63 * graphics times have already been set. If this function returns false, it likely indicates 64 * an error from the app side. 65 */ 66 bool setGraphicsTimeline(std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline); 67 68 inline bool operator==(const ConnectionTimeline& rhs) const; 69 inline bool operator!=(const ConnectionTimeline& rhs) const; 70 71 private: 72 bool mHasDispatchTimeline = false; 73 bool mHasGraphicsTimeline = false; 74 }; 75 76 struct InputEventTimeline { 77 InputEventTimeline(bool isDown, nsecs_t eventTime, nsecs_t readTime); 78 const bool isDown; // True if this is an ACTION_DOWN event 79 const nsecs_t eventTime; 80 const nsecs_t readTime; 81 82 struct IBinderHash { operatorInputEventTimeline::IBinderHash83 std::size_t operator()(const sp<IBinder>& b) const { 84 return std::hash<IBinder*>{}(b.get()); 85 } 86 }; 87 88 std::unordered_map<sp<IBinder>, ConnectionTimeline, IBinderHash> connectionTimelines; 89 90 bool operator==(const InputEventTimeline& rhs) const; 91 }; 92 93 class InputEventTimelineProcessor { 94 protected: InputEventTimelineProcessor()95 InputEventTimelineProcessor() {} ~InputEventTimelineProcessor()96 virtual ~InputEventTimelineProcessor() {} 97 98 public: 99 /** 100 * Process the provided timeline 101 */ 102 virtual void processTimeline(const InputEventTimeline& timeline) = 0; 103 }; 104 105 } // namespace inputdispatcher 106 } // namespace android 107 108 #endif // _UI_INPUT_INPUTDISPATCHER_INPUTEVENTTIMELINE_H 109