blob: 96923526da6314c64b792e7b552f32f1d50f85f0 [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 /**
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700117 * The deathRecipient will be subscribed to the HAL death. If the death recipient
118 * owns MotionClassifier and receives HAL death, it should delete its copy of it.
119 * The callback serviceDied will also be sent if the MotionClassifier itself fails
120 * to initialize. If the MotionClassifier fails to initialize, it is not useful, and
121 * should be deleted.
122 * If no death recipient is supplied, then the registration step will be skipped, so there will
123 * be no listeners registered for the HAL death. This is useful for testing
124 * MotionClassifier in isolation.
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800125 */
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700126 explicit MotionClassifier(sp<android::hardware::hidl_death_recipient> deathRecipient = nullptr);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800127 ~MotionClassifier();
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700128
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800129 /**
130 * Classifies events asynchronously; that is, it doesn't block events on a classification,
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700131 * but instead sends them over to the classifier HAL and after a classification is
132 * determined, it then marks the next event it sees in the stream with it.
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 Vishniakou4bdbb6a2019-04-11 09:42:09 -0700144 /**
145 * Initialize MotionClassifier.
146 * Return true if initializaion is successful.
147 */
148 bool init();
149 /**
150 * Entity that will be notified of the HAL death (most likely InputClassifier).
151 */
152 wp<android::hardware::hidl_death_recipient> mDeathRecipient;
153
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800154 // The events that need to be sent to the HAL.
155 BlockingQueue<ClassifierEvent> mEvents;
156 /**
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800157 * Add an event to the queue mEvents.
158 */
159 void enqueueEvent(ClassifierEvent&& event);
160 /**
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800161 * Thread that will communicate with InputClassifier HAL.
162 * This should be the only thread that communicates with InputClassifier HAL,
163 * because this thread is allowed to block on the HAL calls.
164 */
165 std::thread mHalThread;
166 /**
167 * Print an error message if the caller is not on the InputClassifier thread.
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700168 * Caller must supply the name of the calling function as __func__
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800169 */
170 void ensureHalThread(const char* function);
171 /**
172 * Call the InputClassifier HAL
173 */
174 void callInputClassifierHal();
175 /**
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700176 * Access to the InputClassifier HAL. May be null if init() hasn't completed yet.
177 * When init() successfully completes, mService is guaranteed to remain non-null and to not
178 * change its value until MotionClassifier is destroyed.
179 * This variable is *not* guarded by mLock in the InputClassifier thread, because
180 * that thread knows exactly when this variable is initialized.
181 * When accessed in any other thread, mService is checked for nullness with a lock.
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800182 */
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700183 sp<android::hardware::input::classifier::V1_0::IInputClassifier> mService;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800184 std::mutex mLock;
185 /**
186 * Per-device input classifications. Should only be accessed using the
187 * getClassification / setClassification methods.
188 */
189 std::unordered_map<int32_t /*deviceId*/, MotionClassification>
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800190 mClassifications GUARDED_BY(mLock);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800191 /**
192 * Set the current classification for a given device.
193 */
194 void setClassification(int32_t deviceId, MotionClassification classification);
195 /**
196 * Get the current classification for a given device.
197 */
198 MotionClassification getClassification(int32_t deviceId);
199 void updateClassification(int32_t deviceId, nsecs_t eventTime,
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800200 MotionClassification classification);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800201 /**
202 * Clear all current classifications
203 */
204 void clearClassifications();
205 /**
206 * Per-device times when the last ACTION_DOWN was received.
207 * Used to reject late classifications that do not belong to the current gesture.
208 *
209 * Accessed indirectly by both InputClassifier thread and the thread that receives notifyMotion.
210 */
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800211 std::unordered_map<int32_t /*deviceId*/, nsecs_t /*downTime*/> mLastDownTimes GUARDED_BY(mLock);
212
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800213 void updateLastDownTime(int32_t deviceId, nsecs_t downTime);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800214
Siarhei Vishniakoue3021d72020-02-28 15:25:41 -0800215 void clearDeviceState(int32_t deviceId);
216
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800217 /**
218 * Exit the InputClassifier HAL thread.
219 * Useful for tests to ensure proper cleanup.
220 */
221 void requestExit();
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700222 /**
223 * Return string status of mService
224 */
225 const char* getServiceStatus() REQUIRES(mLock);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800226};
227
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800228
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800229/**
230 * Implementation of the InputClassifierInterface.
231 * Represents a separate stage of input processing. All of the input events go through this stage.
232 * Acts as a passthrough for all input events except for motion events.
233 * The events of motion type are sent to MotionClassifier.
234 */
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800235class InputClassifier : public InputClassifierInterface,
236 public android::hardware::hidl_death_recipient {
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800237public:
238 explicit InputClassifier(const sp<InputListenerInterface>& listener);
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800239 // Some of the constructor logic is finished in onFirstRef
240 virtual void onFirstRef() override;
Siarhei Vishniakou45bb0882019-02-04 14:25:28 -0800241
242 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
243 virtual void notifyKey(const NotifyKeyArgs* args) override;
244 virtual void notifyMotion(const NotifyMotionArgs* args) override;
245 virtual void notifySwitch(const NotifySwitchArgs* args) override;
246 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800247
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800248 virtual void serviceDied(uint64_t cookie,
249 const wp<android::hidl::base::V1_0::IBase>& who) override;
250
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800251 virtual void dump(std::string& dump) override;
252
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800253private:
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800254 // Protect access to mMotionClassifier, since it may become null via a hidl callback
255 std::mutex mLock;
256 std::unique_ptr<MotionClassifierInterface> mMotionClassifier GUARDED_BY(mLock);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800257 // The next stage to pass input events to
258 sp<InputListenerInterface> mListener;
259};
260
261} // namespace android
Greg Kaiser04b3a052019-01-29 07:10:14 -0800262#endif