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 | |
| 20 | #include <utils/RefBase.h> |
| 21 | #include <unordered_map> |
| 22 | #include <thread> |
| 23 | |
| 24 | #include "BlockingQueue.h" |
| 25 | #include "InputListener.h" |
| 26 | #include <android/hardware/input/classifier/1.0/IInputClassifier.h> |
| 27 | |
| 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; |
| 71 | virtual void reset() = 0; |
| 72 | virtual void reset(const NotifyDeviceResetArgs& args) = 0; |
| 73 | }; |
| 74 | |
| 75 | /** |
| 76 | * Base interface for an InputListener stage. |
| 77 | * Provides classification to events. |
| 78 | */ |
| 79 | class InputClassifierInterface : public virtual RefBase, public InputListenerInterface { |
| 80 | protected: |
| 81 | InputClassifierInterface() { } |
| 82 | virtual ~InputClassifierInterface() { } |
| 83 | }; |
| 84 | |
| 85 | // --- Implementations --- |
| 86 | |
| 87 | /** |
| 88 | * Implementation of MotionClassifierInterface that calls the InputClassifier HAL |
| 89 | * in order to determine the classification for the current gesture. |
| 90 | * |
| 91 | * The InputClassifier HAL may keep track of the entire gesture in order to determine |
| 92 | * the classification, and may be hardware-specific. It may use the data in |
| 93 | * NotifyMotionArgs::videoFrames field to drive the classification decisions. |
| 94 | * The HAL is called from a separate thread. |
| 95 | */ |
Greg Kaiser | 04b3a05 | 2019-01-29 07:10:14 -0800 | [diff] [blame^] | 96 | class MotionClassifier final : public MotionClassifierInterface { |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 97 | public: |
| 98 | MotionClassifier(sp<android::hardware::input::classifier::V1_0::IInputClassifier> service); |
| 99 | ~MotionClassifier(); |
| 100 | /** |
| 101 | * Classifies events asynchronously; that is, it doesn't block events on a classification, |
| 102 | * but instead sends them over to the classifier HAL |
| 103 | * and after a classification is determined, |
| 104 | * it then marks the next event it sees in the stream with it. |
| 105 | * |
| 106 | * Therefore, it is acceptable to have the classifications be delayed by 1-2 events |
| 107 | * in a particular gesture. |
| 108 | */ |
| 109 | virtual MotionClassification classify(const NotifyMotionArgs& args) override; |
| 110 | virtual void reset() override; |
| 111 | virtual void reset(const NotifyDeviceResetArgs& args) override; |
| 112 | |
| 113 | private: |
| 114 | // The events that need to be sent to the HAL. |
| 115 | BlockingQueue<ClassifierEvent> mEvents; |
| 116 | /** |
| 117 | * Thread that will communicate with InputClassifier HAL. |
| 118 | * This should be the only thread that communicates with InputClassifier HAL, |
| 119 | * because this thread is allowed to block on the HAL calls. |
| 120 | */ |
| 121 | std::thread mHalThread; |
| 122 | /** |
| 123 | * Print an error message if the caller is not on the InputClassifier thread. |
| 124 | * Caller must supply the name of the calling function as __function__ |
| 125 | */ |
| 126 | void ensureHalThread(const char* function); |
| 127 | /** |
| 128 | * Call the InputClassifier HAL |
| 129 | */ |
| 130 | void callInputClassifierHal(); |
| 131 | /** |
| 132 | * Access to the InputClassifier HAL |
| 133 | */ |
| 134 | sp<android::hardware::input::classifier::V1_0::IInputClassifier> mService; |
| 135 | std::mutex mLock; |
| 136 | /** |
| 137 | * Per-device input classifications. Should only be accessed using the |
| 138 | * getClassification / setClassification methods. |
| 139 | */ |
| 140 | std::unordered_map<int32_t /*deviceId*/, MotionClassification> |
| 141 | mClassifications; //GUARDED_BY(mLock); |
| 142 | /** |
| 143 | * Set the current classification for a given device. |
| 144 | */ |
| 145 | void setClassification(int32_t deviceId, MotionClassification classification); |
| 146 | /** |
| 147 | * Get the current classification for a given device. |
| 148 | */ |
| 149 | MotionClassification getClassification(int32_t deviceId); |
| 150 | void updateClassification(int32_t deviceId, nsecs_t eventTime, |
| 151 | MotionClassification classification); |
| 152 | /** |
| 153 | * Clear all current classifications |
| 154 | */ |
| 155 | void clearClassifications(); |
| 156 | /** |
| 157 | * Per-device times when the last ACTION_DOWN was received. |
| 158 | * Used to reject late classifications that do not belong to the current gesture. |
| 159 | * |
| 160 | * Accessed indirectly by both InputClassifier thread and the thread that receives notifyMotion. |
| 161 | */ |
| 162 | std::unordered_map<int32_t /*deviceId*/, nsecs_t /*downTime*/> |
| 163 | mLastDownTimes; //GUARDED_BY(mLock); |
| 164 | void updateLastDownTime(int32_t deviceId, nsecs_t downTime); |
| 165 | // Should only be accessed through isResetNeeded() and setResetNeeded() |
| 166 | bool mResetNeeded = false; //GUARDED_BY(mLock); |
| 167 | /** |
| 168 | * Check whether reset should be performed. Reset should be performed |
| 169 | * if the eventTime of the current event is older than mLastDownTime, |
| 170 | * i.e. a new gesture has already begun, but an older gesture is still being processed. |
| 171 | */ |
| 172 | bool isResetNeeded(nsecs_t eventTime); |
| 173 | void setResetNeeded(bool isNeeded); |
| 174 | |
| 175 | /** |
| 176 | * Exit the InputClassifier HAL thread. |
| 177 | * Useful for tests to ensure proper cleanup. |
| 178 | */ |
| 179 | void requestExit(); |
| 180 | }; |
| 181 | |
| 182 | /** |
| 183 | * Implementation of the InputClassifierInterface. |
| 184 | * Represents a separate stage of input processing. All of the input events go through this stage. |
| 185 | * Acts as a passthrough for all input events except for motion events. |
| 186 | * The events of motion type are sent to MotionClassifier. |
| 187 | */ |
| 188 | class InputClassifier : public InputClassifierInterface { |
| 189 | public: |
| 190 | explicit InputClassifier(const sp<InputListenerInterface>& listener); |
| 191 | virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args); |
| 192 | virtual void notifyKey(const NotifyKeyArgs* args); |
| 193 | virtual void notifyMotion(const NotifyMotionArgs* args); |
| 194 | virtual void notifySwitch(const NotifySwitchArgs* args); |
| 195 | virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args); |
| 196 | |
| 197 | private: |
| 198 | std::unique_ptr<MotionClassifierInterface> mMotionClassifier = nullptr; |
| 199 | // The next stage to pass input events to |
| 200 | sp<InputListenerInterface> mListener; |
| 201 | }; |
| 202 | |
| 203 | } // namespace android |
Greg Kaiser | 04b3a05 | 2019-01-29 07:10:14 -0800 | [diff] [blame^] | 204 | #endif |