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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/*
* Copyright (C) 2018 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_INPUTFLINGER_TOUCH_VIDEO_DEVICE_H
#define _UI_INPUTFLINGER_TOUCH_VIDEO_DEVICE_H
#include <android-base/unique_fd.h>
#include <input/TouchVideoFrame.h>
#include <stdint.h>
#include <array>
#include <optional>
#include <string>
#include <vector>
namespace android {
/**
* Represents a video device that uses v4l2 api to report touch heatmap data.
*/
class TouchVideoDevice {
public:
/**
* Create a new TouchVideoDevice for the path provided.
* Return nullptr upon failure.
*/
static std::unique_ptr<TouchVideoDevice> create(std::string devicePath);
~TouchVideoDevice();
bool hasValidFd() const { return mFd.get() != INVALID_FD; }
/**
* Obtain the file descriptor associated with this video device.
* Could be used for adding to epoll.
*/
int getFd() const { return mFd.get(); }
/**
* Get the name of this video device.
*/
const std::string& getName() const { return mName; }
/**
* Get the file path of this video device.
*/
const std::string& getPath() const { return mPath; }
/**
* Get the height of the heatmap frame
*/
uint32_t getHeight() const { return mHeight; }
/**
* Get the width of the heatmap frame
*/
uint32_t getWidth() const { return mWidth; }
/**
* Direct read of the frame. Stores the frame into internal buffer.
* Return the number of frames that were successfully read.
*
* This function should not be called unless buffer is ready!
* This must be checked with select, poll, epoll, or similar api first.
* If epoll indicates that there is data ready to read, but this function
* returns zero, then it is likely an error occurred.
*/
size_t readAndQueueFrames();
/**
* Return all of the queued frames, and erase them from the local buffer.
* The returned frames are in the order that they were received from the
* v4l2 device, with the oldest frame at the index 0.
*/
std::vector<TouchVideoFrame> consumeFrames();
/**
* Get string representation of this video device.
*/
std::string dump() const;
private:
android::base::unique_fd mFd;
std::string mName;
std::string mPath;
uint32_t mHeight;
uint32_t mWidth;
static constexpr int INVALID_FD = -1;
/**
* How many buffers to request for heatmap.
* The kernel driver will be allocating these buffers for us,
* and will provide memory locations to read these from.
*/
static constexpr size_t NUM_BUFFERS = 3;
std::array<const int16_t*, NUM_BUFFERS> mReadLocations;
/**
* How many buffers to keep for the internal queue. When the internal buffer
* exceeds this capacity, oldest frames will be dropped.
*/
static constexpr size_t MAX_QUEUE_SIZE = 20;
std::vector<TouchVideoFrame> mFrames;
/**
* The constructor is private because opening a v4l2 device requires many checks.
* To get a new TouchVideoDevice, use 'create' instead.
*/
explicit TouchVideoDevice(int fd, std::string&& name, std::string&& devicePath, uint32_t height,
uint32_t width,
const std::array<const int16_t*, NUM_BUFFERS>& readLocations);
/**
* Read all currently available frames.
*/
std::vector<TouchVideoFrame> readFrames();
/**
* Read a single frame. May return nullopt if no data is currently available for reading.
*/
std::optional<TouchVideoFrame> readFrame();
};
} // namespace android
#endif // _UI_INPUTFLINGER_TOUCH_VIDEO_DEVICE_H
|