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> |
| 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 | |
| 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: |
| 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 | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 98 | protected: |
| 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 Kaiser | 04b3a05 | 2019-01-29 07:10:14 -0800 | [diff] [blame] | 114 | class MotionClassifier final : public MotionClassifierInterface { |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 115 | public: |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 116 | /** |
| 117 | * The provided pointer to the service cannot be null. |
| 118 | */ |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 119 | 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 Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 134 | virtual void dump(std::string& dump) override; |
| 135 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 136 | private: |
| 137 | // The events that need to be sent to the HAL. |
| 138 | BlockingQueue<ClassifierEvent> mEvents; |
| 139 | /** |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 140 | * Add an event to the queue mEvents. |
| 141 | */ |
| 142 | void enqueueEvent(ClassifierEvent&& event); |
| 143 | /** |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 144 | * 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 Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 159 | * Access to the InputClassifier HAL. Can always be safely dereferenced. |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 160 | */ |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 161 | const sp<android::hardware::input::classifier::V1_0::IInputClassifier> mService; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 162 | 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 Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 168 | mClassifications GUARDED_BY(mLock); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 169 | /** |
| 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 Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 178 | MotionClassification classification); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 179 | /** |
| 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 Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 189 | std::unordered_map<int32_t /*deviceId*/, nsecs_t /*downTime*/> mLastDownTimes GUARDED_BY(mLock); |
| 190 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 191 | void updateLastDownTime(int32_t deviceId, nsecs_t downTime); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 192 | |
| 193 | /** |
| 194 | * Exit the InputClassifier HAL thread. |
| 195 | * Useful for tests to ensure proper cleanup. |
| 196 | */ |
| 197 | void requestExit(); |
| 198 | }; |
| 199 | |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 200 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 201 | /** |
| 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 Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 207 | class InputClassifier : public InputClassifierInterface, |
| 208 | public android::hardware::hidl_death_recipient { |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 209 | public: |
| 210 | explicit InputClassifier(const sp<InputListenerInterface>& listener); |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 211 | // Some of the constructor logic is finished in onFirstRef |
| 212 | virtual void onFirstRef() override; |
Siarhei Vishniakou | 45bb088 | 2019-02-04 14:25:28 -0800 | [diff] [blame] | 213 | |
| 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 Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 219 | |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 220 | virtual void serviceDied(uint64_t cookie, |
| 221 | const wp<android::hidl::base::V1_0::IBase>& who) override; |
| 222 | |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 223 | virtual void dump(std::string& dump) override; |
| 224 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 225 | private: |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 226 | // 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 Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 229 | // The next stage to pass input events to |
| 230 | sp<InputListenerInterface> mListener; |
| 231 | }; |
| 232 | |
| 233 | } // namespace android |
Greg Kaiser | 04b3a05 | 2019-01-29 07:10:14 -0800 | [diff] [blame] | 234 | #endif |