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