blob: deeae7c6f42e31dbddb20c94fd20e5fb3c6dfbae [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 <thread>
Siarhei Vishniakou16523972020-03-04 17:48:39 -080022#include <unordered_map>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080023
24#include "BlockingQueue.h"
25#include "InputListener.h"
26#include <android/hardware/input/classifier/1.0/IInputClassifier.h>
27
28namespace android {
29
30enum class ClassifierEventType : uint8_t {
31 MOTION = 0,
32 DEVICE_RESET = 1,
33 HAL_RESET = 2,
34 EXIT = 3,
35};
36
37struct ClassifierEvent {
38 ClassifierEventType type;
39 std::unique_ptr<NotifyArgs> args;
40
41 ClassifierEvent(ClassifierEventType type, std::unique_ptr<NotifyArgs> args);
42 ClassifierEvent(std::unique_ptr<NotifyMotionArgs> args);
43 ClassifierEvent(std::unique_ptr<NotifyDeviceResetArgs> args);
44 ClassifierEvent(ClassifierEvent&& other);
45 ClassifierEvent& operator=(ClassifierEvent&& other);
46
47 // Convenience function to create a HAL_RESET event
48 static ClassifierEvent createHalResetEvent();
49 // Convenience function to create an EXIT event
50 static ClassifierEvent createExitEvent();
51
52 std::optional<int32_t> getDeviceId() const;
53};
54
55// --- Interfaces ---
56
57/**
58 * Interface for adding a MotionClassification to NotifyMotionArgs.
59 *
60 * To implement, override the classify function.
61 */
62class MotionClassifierInterface {
63public:
64 MotionClassifierInterface() { }
65 virtual ~MotionClassifierInterface() { }
66 /**
67 * Based on the motion event described by NotifyMotionArgs,
68 * provide a MotionClassification for the current gesture.
69 */
70 virtual MotionClassification classify(const NotifyMotionArgs& args) = 0;
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -080071 /**
72 * Reset all internal HAL state.
73 */
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080074 virtual void reset() = 0;
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -080075 /**
76 * Reset HAL state for a specific device.
77 */
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080078 virtual void reset(const NotifyDeviceResetArgs& args) = 0;
Siarhei Vishniakoua028c442019-02-04 14:33:23 -080079
80 /**
81 * Dump the state of the motion classifier
82 */
83 virtual void dump(std::string& dump) = 0;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080084};
85
86/**
87 * Base interface for an InputListener stage.
88 * Provides classification to events.
89 */
Siarhei Vishniakou18050092021-09-01 13:32:49 -070090class InputClassifierInterface : public InputListenerInterface {
Siarhei Vishniakoua028c442019-02-04 14:33:23 -080091public:
Siarhei Vishniakouc9ac19e2020-03-19 11:55:01 -070092 virtual void setMotionClassifierEnabled(bool enabled) = 0;
Siarhei Vishniakoua028c442019-02-04 14:33:23 -080093 /**
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 Vishniakou18050092021-09-01 13:32:49 -070098
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080099 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 Vishniakou16523972020-03-04 17:48:39 -0800116 /*
117 * Create an instance of MotionClassifier.
118 * The death recipient, if provided, will be subscribed to the HAL death.
119 * The death recipient could be used to destroy MotionClassifier.
120 *
121 * This function should be called asynchronously, because getService takes a long time.
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800122 */
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800123 static std::unique_ptr<MotionClassifierInterface> create(
124 sp<android::hardware::hidl_death_recipient> deathRecipient);
125
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800126 ~MotionClassifier();
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700127
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800128 /**
129 * Classifies events asynchronously; that is, it doesn't block events on a classification,
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800130 * but instead sends them over to the classifier HAL. After a classification of a specific
131 * event is determined, MotionClassifier then marks the next event in the stream with this
132 * classification.
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800133 *
134 * Therefore, it is acceptable to have the classifications be delayed by 1-2 events
135 * in a particular gesture.
136 */
137 virtual MotionClassification classify(const NotifyMotionArgs& args) override;
138 virtual void reset() override;
139 virtual void reset(const NotifyDeviceResetArgs& args) override;
140
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800141 virtual void dump(std::string& dump) override;
142
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800143private:
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800144 friend class MotionClassifierTest; // to create MotionClassifier with a test HAL implementation
145 explicit MotionClassifier(
146 sp<android::hardware::input::classifier::V1_0::IInputClassifier> service);
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700147
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800148 // The events that need to be sent to the HAL.
149 BlockingQueue<ClassifierEvent> mEvents;
150 /**
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800151 * Add an event to the queue mEvents.
152 */
153 void enqueueEvent(ClassifierEvent&& event);
154 /**
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800155 * Thread that will communicate with InputClassifier HAL.
156 * This should be the only thread that communicates with InputClassifier HAL,
157 * because this thread is allowed to block on the HAL calls.
158 */
159 std::thread mHalThread;
160 /**
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800161 * Process events and call the InputClassifier HAL
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800162 */
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800163 void processEvents();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800164 /**
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700165 * Access to the InputClassifier HAL. May be null if init() hasn't completed yet.
166 * When init() successfully completes, mService is guaranteed to remain non-null and to not
167 * change its value until MotionClassifier is destroyed.
168 * This variable is *not* guarded by mLock in the InputClassifier thread, because
169 * that thread knows exactly when this variable is initialized.
170 * When accessed in any other thread, mService is checked for nullness with a lock.
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800171 */
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700172 sp<android::hardware::input::classifier::V1_0::IInputClassifier> mService;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800173 std::mutex mLock;
174 /**
175 * Per-device input classifications. Should only be accessed using the
176 * getClassification / setClassification methods.
177 */
178 std::unordered_map<int32_t /*deviceId*/, MotionClassification>
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800179 mClassifications GUARDED_BY(mLock);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800180 /**
181 * Set the current classification for a given device.
182 */
183 void setClassification(int32_t deviceId, MotionClassification classification);
184 /**
185 * Get the current classification for a given device.
186 */
187 MotionClassification getClassification(int32_t deviceId);
188 void updateClassification(int32_t deviceId, nsecs_t eventTime,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800189 MotionClassification classification);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800190 /**
191 * Clear all current classifications
192 */
193 void clearClassifications();
194 /**
195 * Per-device times when the last ACTION_DOWN was received.
196 * Used to reject late classifications that do not belong to the current gesture.
197 *
198 * Accessed indirectly by both InputClassifier thread and the thread that receives notifyMotion.
199 */
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800200 std::unordered_map<int32_t /*deviceId*/, nsecs_t /*downTime*/> mLastDownTimes GUARDED_BY(mLock);
201
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800202 void updateLastDownTime(int32_t deviceId, nsecs_t downTime);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800203
Siarhei Vishniakoue3021d72020-02-28 15:25:41 -0800204 void clearDeviceState(int32_t deviceId);
205
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800206 /**
207 * Exit the InputClassifier HAL thread.
208 * Useful for tests to ensure proper cleanup.
209 */
210 void requestExit();
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700211 /**
212 * Return string status of mService
213 */
214 const char* getServiceStatus() REQUIRES(mLock);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800215};
216
217/**
218 * Implementation of the InputClassifierInterface.
219 * Represents a separate stage of input processing. All of the input events go through this stage.
220 * Acts as a passthrough for all input events except for motion events.
221 * The events of motion type are sent to MotionClassifier.
222 */
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800223class InputClassifier : public InputClassifierInterface {
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800224public:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700225 explicit InputClassifier(InputListenerInterface& listener);
Siarhei Vishniakou45bb0882019-02-04 14:25:28 -0800226
227 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
228 virtual void notifyKey(const NotifyKeyArgs* args) override;
229 virtual void notifyMotion(const NotifyMotionArgs* args) override;
230 virtual void notifySwitch(const NotifySwitchArgs* args) override;
Chris Yef59a2f42020-10-16 12:55:26 -0700231 virtual void notifySensor(const NotifySensorArgs* args) override;
Chris Yefb552902021-02-03 17:18:37 -0800232 virtual void notifyVibratorState(const NotifyVibratorStateArgs* args) override;
Siarhei Vishniakou45bb0882019-02-04 14:25:28 -0800233 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
Prabir Pradhan7e186182020-11-10 13:56:45 -0800234 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) override;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800235
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800236 virtual void dump(std::string& dump) override;
237
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800238 ~InputClassifier();
239
Siarhei Vishniakouc9ac19e2020-03-19 11:55:01 -0700240 // Called from InputManager
241 virtual void setMotionClassifierEnabled(bool enabled) override;
242
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800243private:
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800244 // Protect access to mMotionClassifier, since it may become null via a hidl callback
245 std::mutex mLock;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800246 // The next stage to pass input events to
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700247 InputListenerInterface& mListener;
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800248
249 std::unique_ptr<MotionClassifierInterface> mMotionClassifier GUARDED_BY(mLock);
250 std::thread mInitializeMotionClassifierThread;
251 /**
252 * Set the value of mMotionClassifier.
253 * This is called from 2 different threads:
254 * 1) mInitializeMotionClassifierThread, when we have constructed a MotionClassifier
255 * 2) A binder thread of the HalDeathRecipient, which is created when HAL dies. This would cause
256 * mMotionClassifier to become nullptr.
257 */
258 void setMotionClassifier(std::unique_ptr<MotionClassifierInterface> motionClassifier);
259
260 /**
261 * The deathRecipient will call setMotionClassifier(null) when the HAL dies.
262 */
263 class HalDeathRecipient : public android::hardware::hidl_death_recipient {
264 public:
265 explicit HalDeathRecipient(InputClassifier& parent);
266 virtual void serviceDied(uint64_t cookie,
267 const wp<android::hidl::base::V1_0::IBase>& who) override;
268
269 private:
270 InputClassifier& mParent;
271 };
272 // We retain a reference to death recipient, because the death recipient will be calling
273 // ~MotionClassifier if the HAL dies.
274 // If we don't retain a reference, and MotionClassifier is the only owner of the death
275 // recipient, the serviceDied call will cause death recipient to call its own destructor.
276 sp<HalDeathRecipient> mHalDeathRecipient;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800277};
278
279} // namespace android
Greg Kaiser04b3a052019-01-29 07:10:14 -0800280#endif