summaryrefslogtreecommitdiff
path: root/services/inputflinger/dispatcher/InputEventTimeline.h
blob: 77b8472f89189e5b129809dbc47ad744f894a5aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _UI_INPUT_INPUTDISPATCHER_INPUTEVENTTIMELINE_H
#define _UI_INPUT_INPUTDISPATCHER_INPUTEVENTTIMELINE_H

#include <binder/IBinder.h>
#include <input/Input.h>
#include <unordered_map>

namespace android {

namespace inputdispatcher {

/**
 * Describes the input event timeline for each connection.
 * An event with the same inputEventId can go to more than 1 connection simultaneously.
 * For each connection that the input event goes to, there will be a separate ConnectionTimeline
 * created.
 * To create a complete ConnectionTimeline, we must receive two calls:
 * 1) setDispatchTimeline
 * 2) setGraphicsTimeline
 *
 * In a typical scenario, the dispatch timeline is known first. Later, if a frame is produced, the
 * graphics timeline is available.
 */
struct ConnectionTimeline {
    // DispatchTimeline
    nsecs_t deliveryTime; // time at which the event was sent to the receiver
    nsecs_t consumeTime;  // time at which the receiver read the event
    nsecs_t finishTime;   // time at which the finish event was received
    // GraphicsTimeline
    std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;

    ConnectionTimeline(nsecs_t deliveryTime, nsecs_t consumeTime, nsecs_t finishTime);
    ConnectionTimeline(std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline);

    /**
     * True if all contained timestamps are valid, false otherwise.
     */
    bool isComplete() const;
    /**
     * Set the dispatching-related times. Return true if the operation succeeded, false if the
     * dispatching times have already been set. If this function returns false, it likely indicates
     * an error from the app side.
     */
    bool setDispatchTimeline(nsecs_t deliveryTime, nsecs_t consumeTime, nsecs_t finishTime);
    /**
     * Set the graphics-related times. Return true if the operation succeeded, false if the
     * graphics times have already been set. If this function returns false, it likely indicates
     * an error from the app side.
     */
    bool setGraphicsTimeline(std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline);

    inline bool operator==(const ConnectionTimeline& rhs) const;
    inline bool operator!=(const ConnectionTimeline& rhs) const;

private:
    bool mHasDispatchTimeline = false;
    bool mHasGraphicsTimeline = false;
};

struct InputEventTimeline {
    InputEventTimeline(bool isDown, nsecs_t eventTime, nsecs_t readTime);
    const bool isDown; // True if this is an ACTION_DOWN event
    const nsecs_t eventTime;
    const nsecs_t readTime;

    struct IBinderHash {
        std::size_t operator()(const sp<IBinder>& b) const {
            return std::hash<IBinder*>{}(b.get());
        }
    };

    std::unordered_map<sp<IBinder>, ConnectionTimeline, IBinderHash> connectionTimelines;

    bool operator==(const InputEventTimeline& rhs) const;
};

class InputEventTimelineProcessor {
protected:
    InputEventTimelineProcessor() {}
    virtual ~InputEventTimelineProcessor() {}

public:
    /**
     * Process the provided timeline
     */
    virtual void processTimeline(const InputEventTimeline& timeline) = 0;
};

} // namespace inputdispatcher
} // namespace android

#endif // _UI_INPUT_INPUTDISPATCHER_INPUTEVENTTIMELINE_H