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