Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 20 | #include <android-base/thread_annotations.h> |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 21 | #include <future> |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 22 | #include <thread> |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 23 | #include <unordered_map> |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 24 | |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 25 | #include <aidl/android/hardware/input/processor/IInputProcessor.h> |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 26 | #include "BlockingQueue.h" |
| 27 | #include "InputListener.h" |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 28 | namespace android { |
| 29 | |
| 30 | enum class ClassifierEventType : uint8_t { |
| 31 | MOTION = 0, |
| 32 | DEVICE_RESET = 1, |
| 33 | HAL_RESET = 2, |
| 34 | EXIT = 3, |
| 35 | }; |
| 36 | |
| 37 | struct 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 | */ |
| 62 | class MotionClassifierInterface { |
| 63 | public: |
| 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 Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 71 | /** |
| 72 | * Reset all internal HAL state. |
| 73 | */ |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 74 | virtual void reset() = 0; |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Reset HAL state for a specific device. |
| 77 | */ |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 78 | virtual void reset(const NotifyDeviceResetArgs& args) = 0; |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * Dump the state of the motion classifier |
| 82 | */ |
| 83 | virtual void dump(std::string& dump) = 0; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * Base interface for an InputListener stage. |
| 88 | * Provides classification to events. |
| 89 | */ |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 90 | class InputClassifierInterface : public InputListenerInterface { |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 91 | public: |
Siarhei Vishniakou | c9ac19e | 2020-03-19 11:55:01 -0700 | [diff] [blame] | 92 | virtual void setMotionClassifierEnabled(bool enabled) = 0; |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 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 Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 98 | |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame^] | 99 | /* Called by the heatbeat to ensures that the classifier has not deadlocked. */ |
| 100 | virtual void monitor() = 0; |
| 101 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 102 | InputClassifierInterface() { } |
| 103 | virtual ~InputClassifierInterface() { } |
| 104 | }; |
| 105 | |
| 106 | // --- Implementations --- |
| 107 | |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 108 | class ScopedDeathRecipient { |
| 109 | public: |
| 110 | explicit ScopedDeathRecipient(AIBinder_DeathRecipient_onBinderDied onBinderDied, void* cookie); |
| 111 | ScopedDeathRecipient(const ScopedDeathRecipient&) = delete; |
| 112 | ScopedDeathRecipient& operator=(ScopedDeathRecipient const&) = delete; |
| 113 | void linkToDeath(AIBinder* binder); |
| 114 | ~ScopedDeathRecipient(); |
| 115 | |
| 116 | private: |
| 117 | AIBinder_DeathRecipient* mRecipient; |
| 118 | void* mCookie; |
| 119 | }; |
| 120 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 121 | /** |
| 122 | * Implementation of MotionClassifierInterface that calls the InputClassifier HAL |
| 123 | * in order to determine the classification for the current gesture. |
| 124 | * |
| 125 | * The InputClassifier HAL may keep track of the entire gesture in order to determine |
| 126 | * the classification, and may be hardware-specific. It may use the data in |
| 127 | * NotifyMotionArgs::videoFrames field to drive the classification decisions. |
| 128 | * The HAL is called from a separate thread. |
| 129 | */ |
Greg Kaiser | 04b3a05 | 2019-01-29 07:10:14 -0800 | [diff] [blame] | 130 | class MotionClassifier final : public MotionClassifierInterface { |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 131 | public: |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 132 | /* |
| 133 | * Create an instance of MotionClassifier. |
| 134 | * The death recipient, if provided, will be subscribed to the HAL death. |
| 135 | * The death recipient could be used to destroy MotionClassifier. |
| 136 | * |
| 137 | * This function should be called asynchronously, because getService takes a long time. |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 138 | */ |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 139 | static std::unique_ptr<MotionClassifierInterface> create( |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 140 | std::shared_ptr<aidl::android::hardware::input::processor::IInputProcessor> service); |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 141 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 142 | ~MotionClassifier(); |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 143 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 144 | /** |
| 145 | * Classifies events asynchronously; that is, it doesn't block events on a classification, |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 146 | * but instead sends them over to the classifier HAL. After a classification of a specific |
| 147 | * event is determined, MotionClassifier then marks the next event in the stream with this |
| 148 | * classification. |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 149 | * |
| 150 | * Therefore, it is acceptable to have the classifications be delayed by 1-2 events |
| 151 | * in a particular gesture. |
| 152 | */ |
| 153 | virtual MotionClassification classify(const NotifyMotionArgs& args) override; |
| 154 | virtual void reset() override; |
| 155 | virtual void reset(const NotifyDeviceResetArgs& args) override; |
| 156 | |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 157 | virtual void dump(std::string& dump) override; |
| 158 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 159 | private: |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 160 | friend class MotionClassifierTest; // to create MotionClassifier with a test HAL implementation |
| 161 | explicit MotionClassifier( |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 162 | std::shared_ptr<aidl::android::hardware::input::processor::IInputProcessor> service); |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 163 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 164 | // The events that need to be sent to the HAL. |
| 165 | BlockingQueue<ClassifierEvent> mEvents; |
| 166 | /** |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 167 | * Add an event to the queue mEvents. |
| 168 | */ |
| 169 | void enqueueEvent(ClassifierEvent&& event); |
| 170 | /** |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 171 | * Thread that will communicate with InputClassifier HAL. |
| 172 | * This should be the only thread that communicates with InputClassifier HAL, |
| 173 | * because this thread is allowed to block on the HAL calls. |
| 174 | */ |
| 175 | std::thread mHalThread; |
| 176 | /** |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 177 | * Process events and call the InputClassifier HAL |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 178 | */ |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 179 | void processEvents(); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 180 | /** |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 181 | * Access to the InputProcessor HAL. May be null if init() hasn't completed yet. |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 182 | * When init() successfully completes, mService is guaranteed to remain non-null and to not |
| 183 | * change its value until MotionClassifier is destroyed. |
| 184 | * This variable is *not* guarded by mLock in the InputClassifier thread, because |
| 185 | * that thread knows exactly when this variable is initialized. |
| 186 | * When accessed in any other thread, mService is checked for nullness with a lock. |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 187 | */ |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 188 | std::shared_ptr<aidl::android::hardware::input::processor::IInputProcessor> mService; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 189 | std::mutex mLock; |
| 190 | /** |
| 191 | * Per-device input classifications. Should only be accessed using the |
| 192 | * getClassification / setClassification methods. |
| 193 | */ |
| 194 | std::unordered_map<int32_t /*deviceId*/, MotionClassification> |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 195 | mClassifications GUARDED_BY(mLock); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 196 | /** |
| 197 | * Set the current classification for a given device. |
| 198 | */ |
| 199 | void setClassification(int32_t deviceId, MotionClassification classification); |
| 200 | /** |
| 201 | * Get the current classification for a given device. |
| 202 | */ |
| 203 | MotionClassification getClassification(int32_t deviceId); |
| 204 | void updateClassification(int32_t deviceId, nsecs_t eventTime, |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 205 | MotionClassification classification); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 206 | /** |
| 207 | * Clear all current classifications |
| 208 | */ |
| 209 | void clearClassifications(); |
| 210 | /** |
| 211 | * Per-device times when the last ACTION_DOWN was received. |
| 212 | * Used to reject late classifications that do not belong to the current gesture. |
| 213 | * |
| 214 | * Accessed indirectly by both InputClassifier thread and the thread that receives notifyMotion. |
| 215 | */ |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 216 | std::unordered_map<int32_t /*deviceId*/, nsecs_t /*downTime*/> mLastDownTimes GUARDED_BY(mLock); |
| 217 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 218 | void updateLastDownTime(int32_t deviceId, nsecs_t downTime); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 219 | |
Siarhei Vishniakou | e3021d7 | 2020-02-28 15:25:41 -0800 | [diff] [blame] | 220 | void clearDeviceState(int32_t deviceId); |
| 221 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 222 | /** |
| 223 | * Exit the InputClassifier HAL thread. |
| 224 | * Useful for tests to ensure proper cleanup. |
| 225 | */ |
| 226 | void requestExit(); |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 227 | /** |
| 228 | * Return string status of mService |
| 229 | */ |
| 230 | const char* getServiceStatus() REQUIRES(mLock); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | /** |
| 234 | * Implementation of the InputClassifierInterface. |
| 235 | * Represents a separate stage of input processing. All of the input events go through this stage. |
| 236 | * Acts as a passthrough for all input events except for motion events. |
| 237 | * The events of motion type are sent to MotionClassifier. |
| 238 | */ |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 239 | class InputClassifier : public InputClassifierInterface { |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 240 | public: |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 241 | explicit InputClassifier(InputListenerInterface& listener); |
Siarhei Vishniakou | 45bb088 | 2019-02-04 14:25:28 -0800 | [diff] [blame] | 242 | |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 243 | void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override; |
| 244 | void notifyKey(const NotifyKeyArgs* args) override; |
| 245 | void notifyMotion(const NotifyMotionArgs* args) override; |
| 246 | void notifySwitch(const NotifySwitchArgs* args) override; |
| 247 | void notifySensor(const NotifySensorArgs* args) override; |
| 248 | void notifyVibratorState(const NotifyVibratorStateArgs* args) override; |
| 249 | void notifyDeviceReset(const NotifyDeviceResetArgs* args) override; |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 250 | void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) override; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 251 | |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 252 | void dump(std::string& dump) override; |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame^] | 253 | void monitor() override; |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 254 | |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 255 | ~InputClassifier(); |
| 256 | |
Siarhei Vishniakou | c9ac19e | 2020-03-19 11:55:01 -0700 | [diff] [blame] | 257 | // Called from InputManager |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 258 | void setMotionClassifierEnabled(bool enabled) override; |
Siarhei Vishniakou | c9ac19e | 2020-03-19 11:55:01 -0700 | [diff] [blame] | 259 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 260 | private: |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 261 | // Protect access to mMotionClassifier, since it may become null via a hidl callback |
| 262 | std::mutex mLock; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 263 | // The next stage to pass input events to |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame^] | 264 | QueuedInputListener mQueuedListener; |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 265 | |
| 266 | std::unique_ptr<MotionClassifierInterface> mMotionClassifier GUARDED_BY(mLock); |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 267 | std::future<void> mInitializeMotionClassifier GUARDED_BY(mLock); |
| 268 | |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 269 | /** |
| 270 | * Set the value of mMotionClassifier. |
| 271 | * This is called from 2 different threads: |
| 272 | * 1) mInitializeMotionClassifierThread, when we have constructed a MotionClassifier |
| 273 | * 2) A binder thread of the HalDeathRecipient, which is created when HAL dies. This would cause |
| 274 | * mMotionClassifier to become nullptr. |
| 275 | */ |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 276 | void setMotionClassifierLocked(std::unique_ptr<MotionClassifierInterface> motionClassifier) |
| 277 | REQUIRES(mLock); |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 278 | |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 279 | static void onBinderDied(void* cookie); |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 280 | |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 281 | std::unique_ptr<ScopedDeathRecipient> mHalDeathRecipient GUARDED_BY(mLock); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 282 | }; |
| 283 | |
| 284 | } // namespace android |
Greg Kaiser | 04b3a05 | 2019-01-29 07:10:14 -0800 | [diff] [blame] | 285 | #endif |