blob: e9deb2d3cf5a0b3a651beec2132307af2bd88965 [file] [log] [blame]
Siarhei Vishniakouf2652122021-03-05 21:39:46 +00001/*
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 Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000018
Asmita Poddardd9a6cd2023-09-26 15:35:12 +000019#include "../InputDeviceMetricsSource.h"
20
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000021#include <binder/IBinder.h>
22#include <input/Input.h>
23#include <unordered_map>
24
25namespace android {
26
27namespace 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 */
41struct 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
72private:
73 bool mHasDispatchTimeline = false;
74 bool mHasGraphicsTimeline = false;
75};
76
77struct InputEventTimeline {
Asmita Poddardd9a6cd2023-09-26 15:35:12 +000078 InputEventTimeline(bool isDown, nsecs_t eventTime, nsecs_t readTime, uint16_t vendorId,
79 uint16_t productId, std::set<InputDeviceUsageSource> sources);
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000080 const bool isDown; // True if this is an ACTION_DOWN event
81 const nsecs_t eventTime;
82 const nsecs_t readTime;
Asmita Poddardd9a6cd2023-09-26 15:35:12 +000083 const uint16_t vendorId;
84 const uint16_t productId;
85 const std::set<InputDeviceUsageSource> sources;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000086
87 struct IBinderHash {
88 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
98class InputEventTimelineProcessor {
99protected:
100 InputEventTimelineProcessor() {}
101 virtual ~InputEventTimelineProcessor() {}
102
103public:
104 /**
105 * Process the provided timeline
106 */
107 virtual void processTimeline(const InputEventTimeline& timeline) = 0;
108};
109
110} // namespace inputdispatcher
111} // namespace android