blob: 0b1483fdd5d317d640bb9465f6dc37a791e95004 [file] [log] [blame]
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001/*
2 * Copyright (C) 2019 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#ifndef _UI_INPUT_CLASSIFIER_H
18#define _UI_INPUT_CLASSIFIER_H
19
Siarhei Vishniakou61291d42019-02-11 18:13:20 -080020#include <android-base/thread_annotations.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080021#include <utils/RefBase.h>
22#include <unordered_map>
23#include <thread>
24
25#include "BlockingQueue.h"
26#include "InputListener.h"
27#include <android/hardware/input/classifier/1.0/IInputClassifier.h>
28
29namespace android {
30
31enum class ClassifierEventType : uint8_t {
32 MOTION = 0,
33 DEVICE_RESET = 1,
34 HAL_RESET = 2,
35 EXIT = 3,
36};
37
38struct ClassifierEvent {
39 ClassifierEventType type;
40 std::unique_ptr<NotifyArgs> args;
41
42 ClassifierEvent(ClassifierEventType type, std::unique_ptr<NotifyArgs> args);
43 ClassifierEvent(std::unique_ptr<NotifyMotionArgs> args);
44 ClassifierEvent(std::unique_ptr<NotifyDeviceResetArgs> args);
45 ClassifierEvent(ClassifierEvent&& other);
46 ClassifierEvent& operator=(ClassifierEvent&& other);
47
48 // Convenience function to create a HAL_RESET event
49 static ClassifierEvent createHalResetEvent();
50 // Convenience function to create an EXIT event
51 static ClassifierEvent createExitEvent();
52
53 std::optional<int32_t> getDeviceId() const;
54};
55
56// --- Interfaces ---
57
58/**
59 * Interface for adding a MotionClassification to NotifyMotionArgs.
60 *
61 * To implement, override the classify function.
62 */
63class MotionClassifierInterface {
64public:
65 MotionClassifierInterface() { }
66 virtual ~MotionClassifierInterface() { }
67 /**
68 * Based on the motion event described by NotifyMotionArgs,
69 * provide a MotionClassification for the current gesture.
70 */
71 virtual MotionClassification classify(const NotifyMotionArgs& args) = 0;
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -080072 /**
73 * Reset all internal HAL state.
74 */
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080075 virtual void reset() = 0;
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -080076 /**
77 * Reset HAL state for a specific device.
78 */
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080079 virtual void reset(const NotifyDeviceResetArgs& args) = 0;
Siarhei Vishniakoua028c442019-02-04 14:33:23 -080080
81 /**
82 * Dump the state of the motion classifier
83 */
84 virtual void dump(std::string& dump) = 0;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080085};
86
87/**
88 * Base interface for an InputListener stage.
89 * Provides classification to events.
90 */
91class InputClassifierInterface : public virtual RefBase, public InputListenerInterface {
Siarhei Vishniakoua028c442019-02-04 14:33:23 -080092public:
93 /**
94 * Dump the state of the input classifier.
95 * This method may be called on any thread (usually by the input manager).
96 */
97 virtual void dump(std::string& dump) = 0;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080098protected:
99 InputClassifierInterface() { }
100 virtual ~InputClassifierInterface() { }
101};
102
103// --- Implementations ---
104
105/**
106 * Implementation of MotionClassifierInterface that calls the InputClassifier HAL
107 * in order to determine the classification for the current gesture.
108 *
109 * The InputClassifier HAL may keep track of the entire gesture in order to determine
110 * the classification, and may be hardware-specific. It may use the data in
111 * NotifyMotionArgs::videoFrames field to drive the classification decisions.
112 * The HAL is called from a separate thread.
113 */
Greg Kaiser04b3a052019-01-29 07:10:14 -0800114class MotionClassifier final : public MotionClassifierInterface {
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800115public:
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800116 /**
117 * The provided pointer to the service cannot be null.
118 */
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800119 MotionClassifier(sp<android::hardware::input::classifier::V1_0::IInputClassifier> service);
120 ~MotionClassifier();
121 /**
122 * Classifies events asynchronously; that is, it doesn't block events on a classification,
123 * but instead sends them over to the classifier HAL
124 * and after a classification is determined,
125 * it then marks the next event it sees in the stream with it.
126 *
127 * Therefore, it is acceptable to have the classifications be delayed by 1-2 events
128 * in a particular gesture.
129 */
130 virtual MotionClassification classify(const NotifyMotionArgs& args) override;
131 virtual void reset() override;
132 virtual void reset(const NotifyDeviceResetArgs& args) override;
133
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800134 virtual void dump(std::string& dump) override;
135
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800136private:
137 // The events that need to be sent to the HAL.
138 BlockingQueue<ClassifierEvent> mEvents;
139 /**
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800140 * Add an event to the queue mEvents.
141 */
142 void enqueueEvent(ClassifierEvent&& event);
143 /**
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800144 * Thread that will communicate with InputClassifier HAL.
145 * This should be the only thread that communicates with InputClassifier HAL,
146 * because this thread is allowed to block on the HAL calls.
147 */
148 std::thread mHalThread;
149 /**
150 * Print an error message if the caller is not on the InputClassifier thread.
151 * Caller must supply the name of the calling function as __function__
152 */
153 void ensureHalThread(const char* function);
154 /**
155 * Call the InputClassifier HAL
156 */
157 void callInputClassifierHal();
158 /**
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800159 * Access to the InputClassifier HAL. Can always be safely dereferenced.
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800160 */
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800161 const sp<android::hardware::input::classifier::V1_0::IInputClassifier> mService;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800162 std::mutex mLock;
163 /**
164 * Per-device input classifications. Should only be accessed using the
165 * getClassification / setClassification methods.
166 */
167 std::unordered_map<int32_t /*deviceId*/, MotionClassification>
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800168 mClassifications GUARDED_BY(mLock);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800169 /**
170 * Set the current classification for a given device.
171 */
172 void setClassification(int32_t deviceId, MotionClassification classification);
173 /**
174 * Get the current classification for a given device.
175 */
176 MotionClassification getClassification(int32_t deviceId);
177 void updateClassification(int32_t deviceId, nsecs_t eventTime,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800178 MotionClassification classification);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800179 /**
180 * Clear all current classifications
181 */
182 void clearClassifications();
183 /**
184 * Per-device times when the last ACTION_DOWN was received.
185 * Used to reject late classifications that do not belong to the current gesture.
186 *
187 * Accessed indirectly by both InputClassifier thread and the thread that receives notifyMotion.
188 */
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800189 std::unordered_map<int32_t /*deviceId*/, nsecs_t /*downTime*/> mLastDownTimes GUARDED_BY(mLock);
190
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800191 void updateLastDownTime(int32_t deviceId, nsecs_t downTime);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800192
193 /**
194 * Exit the InputClassifier HAL thread.
195 * Useful for tests to ensure proper cleanup.
196 */
197 void requestExit();
198};
199
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800200
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800201/**
202 * Implementation of the InputClassifierInterface.
203 * Represents a separate stage of input processing. All of the input events go through this stage.
204 * Acts as a passthrough for all input events except for motion events.
205 * The events of motion type are sent to MotionClassifier.
206 */
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800207class InputClassifier : public InputClassifierInterface,
208 public android::hardware::hidl_death_recipient {
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800209public:
210 explicit InputClassifier(const sp<InputListenerInterface>& listener);
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800211 // Some of the constructor logic is finished in onFirstRef
212 virtual void onFirstRef() override;
Siarhei Vishniakou45bb0882019-02-04 14:25:28 -0800213
214 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
215 virtual void notifyKey(const NotifyKeyArgs* args) override;
216 virtual void notifyMotion(const NotifyMotionArgs* args) override;
217 virtual void notifySwitch(const NotifySwitchArgs* args) override;
218 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800219
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800220 virtual void serviceDied(uint64_t cookie,
221 const wp<android::hidl::base::V1_0::IBase>& who) override;
222
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800223 virtual void dump(std::string& dump) override;
224
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800225private:
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800226 // Protect access to mMotionClassifier, since it may become null via a hidl callback
227 std::mutex mLock;
228 std::unique_ptr<MotionClassifierInterface> mMotionClassifier GUARDED_BY(mLock);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800229 // The next stage to pass input events to
230 sp<InputListenerInterface> mListener;
231};
232
233} // namespace android
Greg Kaiser04b3a052019-01-29 07:10:14 -0800234#endif