• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 struct InputEventTimeline {
78     InputEventTimeline(bool isDown, nsecs_t eventTime, nsecs_t readTime, uint16_t vendorId,
79                        uint16_t productId, std::set<InputDeviceUsageSource> sources);
80     const bool isDown; // True if this is an ACTION_DOWN event
81     const nsecs_t eventTime;
82     const nsecs_t readTime;
83     const uint16_t vendorId;
84     const uint16_t productId;
85     const std::set<InputDeviceUsageSource> sources;
86 
87     struct IBinderHash {
operatorInputEventTimeline::IBinderHash88         std::size_t operator()(const sp<IBinder>& b) const {
89             return std::hash<IBinder*>{}(b.get());
90         }
91     };
92 
93     std::unordered_map<sp<IBinder>, ConnectionTimeline, IBinderHash> connectionTimelines;
94 
95     bool operator==(const InputEventTimeline& rhs) const;
96 };
97 
98 class InputEventTimelineProcessor {
99 protected:
InputEventTimelineProcessor()100     InputEventTimelineProcessor() {}
~InputEventTimelineProcessor()101     virtual ~InputEventTimelineProcessor() {}
102 
103 public:
104     /**
105      * Process the provided timeline
106      */
107     virtual void processTimeline(const InputEventTimeline& timeline) = 0;
108 };
109 
110 } // namespace inputdispatcher
111 } // namespace android
112