| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [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 | #include <benchmark/benchmark.h> | 
 | 18 |  | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 19 | #include <android/os/IInputConstants.h> | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 20 | #include <binder/Binder.h> | 
 | 21 | #include "../dispatcher/InputDispatcher.h" | 
 | 22 |  | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 23 | using android::os::IInputConstants; | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 24 | using android::os::InputEventInjectionResult; | 
 | 25 | using android::os::InputEventInjectionSync; | 
 | 26 |  | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 27 | namespace android::inputdispatcher { | 
 | 28 |  | 
 | 29 | // An arbitrary device id. | 
 | 30 | static const int32_t DEVICE_ID = 1; | 
 | 31 |  | 
 | 32 | // An arbitrary injector pid / uid pair that has permission to inject events. | 
 | 33 | static const int32_t INJECTOR_PID = 999; | 
 | 34 | static const int32_t INJECTOR_UID = 1001; | 
 | 35 |  | 
| Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 36 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 5s; | 
 | 37 | static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 100ms; | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 38 |  | 
 | 39 | static nsecs_t now() { | 
 | 40 |     return systemTime(SYSTEM_TIME_MONOTONIC); | 
 | 41 | } | 
 | 42 |  | 
 | 43 | // --- FakeInputDispatcherPolicy --- | 
 | 44 |  | 
 | 45 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { | 
 | 46 | public: | 
 | 47 |     FakeInputDispatcherPolicy() {} | 
 | 48 |  | 
 | 49 | protected: | 
 | 50 |     virtual ~FakeInputDispatcherPolicy() {} | 
 | 51 |  | 
 | 52 | private: | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 53 |     void notifyConfigurationChanged(nsecs_t) override {} | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 54 |  | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 55 |     void notifyNoFocusedWindowAnr( | 
 | 56 |             const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { | 
 | 57 |         ALOGE("There is no focused window for %s", applicationHandle->getName().c_str()); | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 58 |     } | 
 | 59 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 60 |     void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, | 
 | 61 |                                   const std::string& reason) override { | 
 | 62 |         ALOGE("Window is not responding: %s", reason.c_str()); | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 63 |     } | 
 | 64 |  | 
| Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 65 |     void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {} | 
 | 66 |  | 
 | 67 |     void notifyMonitorUnresponsive(int32_t pid, const std::string& reason) override { | 
 | 68 |         ALOGE("Monitor is not responding: %s", reason.c_str()); | 
 | 69 |     } | 
 | 70 |  | 
 | 71 |     void notifyMonitorResponsive(int32_t pid) override {} | 
| Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 72 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 73 |     void notifyInputChannelBroken(const sp<IBinder>&) override {} | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 74 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 75 |     void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 76 |  | 
| Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 77 |     void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, | 
 | 78 |                            InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, | 
 | 79 |                            const std::vector<float>& values) override {} | 
 | 80 |  | 
 | 81 |     void notifySensorAccuracy(int32_t deviceId, InputDeviceSensorType sensorType, | 
 | 82 |                               InputDeviceSensorAccuracy accuracy) override {} | 
 | 83 |  | 
| Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 84 |     void notifyVibratorState(int32_t deviceId, bool isOn) override {} | 
 | 85 |  | 
| Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 86 |     void notifyUntrustedTouch(const std::string& obscuringPackage) override {} | 
 | 87 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 88 |     void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override { | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 89 |         *outConfig = mConfig; | 
 | 90 |     } | 
 | 91 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 92 |     bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override { | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 93 |         return true; | 
 | 94 |     } | 
 | 95 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 96 |     void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {} | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 97 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 98 |     void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {} | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 99 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 100 |     nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override { | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 101 |         return 0; | 
 | 102 |     } | 
 | 103 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 104 |     bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override { | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 105 |         return false; | 
 | 106 |     } | 
 | 107 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 108 |     void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {} | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 109 |  | 
| Sean Stout | b4e0a59 | 2021-02-23 07:34:53 -0800 | [diff] [blame] | 110 |     void pokeUserActivity(nsecs_t, int32_t, int32_t) override {} | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 111 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 112 |     bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; } | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 113 |  | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 114 |     void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {} | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 115 |  | 
| Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 116 |     void setPointerCapture(bool enabled) override {} | 
 | 117 |  | 
| arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 118 |     void notifyDropWindow(const sp<IBinder>&, float x, float y) override {} | 
 | 119 |  | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 120 |     InputDispatcherConfiguration mConfig; | 
 | 121 | }; | 
 | 122 |  | 
 | 123 | class FakeApplicationHandle : public InputApplicationHandle { | 
 | 124 | public: | 
 | 125 |     FakeApplicationHandle() {} | 
 | 126 |     virtual ~FakeApplicationHandle() {} | 
 | 127 |  | 
 | 128 |     virtual bool updateInfo() { | 
| Siarhei Vishniakou | 7062295 | 2020-07-30 11:17:23 -0500 | [diff] [blame] | 129 |         mInfo.dispatchingTimeoutMillis = | 
 | 130 |                 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count(); | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 131 |         return true; | 
 | 132 |     } | 
 | 133 | }; | 
 | 134 |  | 
 | 135 | class FakeInputReceiver { | 
 | 136 | public: | 
 | 137 |     void consumeEvent() { | 
| Siarhei Vishniakou | d549b25 | 2020-08-11 11:25:26 -0500 | [diff] [blame] | 138 |         uint32_t consumeSeq = 0; | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 139 |         InputEvent* event; | 
 | 140 |  | 
| Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 141 |         std::chrono::time_point start = std::chrono::steady_clock::now(); | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 142 |         status_t result = WOULD_BLOCK; | 
 | 143 |         while (result == WOULD_BLOCK) { | 
| Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 144 |             std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; | 
 | 145 |             if (elapsed > 10ms) { | 
 | 146 |                 ALOGE("Waited too long for consumer to produce an event, giving up"); | 
 | 147 |                 break; | 
 | 148 |             } | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 149 |             result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, | 
 | 150 |                                         &event); | 
 | 151 |         } | 
 | 152 |         if (result != OK) { | 
 | 153 |             ALOGE("Received result = %d from consume()", result); | 
 | 154 |         } | 
 | 155 |         result = mConsumer->sendFinishedSignal(consumeSeq, true); | 
 | 156 |         if (result != OK) { | 
 | 157 |             ALOGE("Received result = %d from sendFinishedSignal", result); | 
 | 158 |         } | 
 | 159 |     } | 
 | 160 |  | 
 | 161 | protected: | 
 | 162 |     explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, const std::string name) | 
 | 163 |           : mDispatcher(dispatcher) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 164 |         mClientChannel = *mDispatcher->createInputChannel(name); | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 165 |         mConsumer = std::make_unique<InputConsumer>(mClientChannel); | 
 | 166 |     } | 
 | 167 |  | 
 | 168 |     virtual ~FakeInputReceiver() {} | 
 | 169 |  | 
 | 170 |     sp<InputDispatcher> mDispatcher; | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 171 |     std::shared_ptr<InputChannel> mClientChannel; | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 172 |     std::unique_ptr<InputConsumer> mConsumer; | 
 | 173 |     PreallocatedInputEventFactory mEventFactory; | 
 | 174 | }; | 
 | 175 |  | 
 | 176 | class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver { | 
 | 177 | public: | 
 | 178 |     static const int32_t WIDTH = 200; | 
 | 179 |     static const int32_t HEIGHT = 200; | 
 | 180 |  | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 181 |     FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 182 |                      const sp<InputDispatcher>& dispatcher, const std::string name) | 
 | 183 |           : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) { | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 184 |         inputApplicationHandle->updateInfo(); | 
 | 185 |         mInfo.applicationInfo = *inputApplicationHandle->getInfo(); | 
 | 186 |     } | 
 | 187 |  | 
 | 188 |     virtual bool updateInfo() override { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 189 |         mInfo.token = mClientChannel->getConnectionToken(); | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 190 |         mInfo.name = "FakeWindowHandle"; | 
| Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 191 |         mInfo.type = InputWindowInfo::Type::APPLICATION; | 
| Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 192 |         mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 193 |         mInfo.frameLeft = mFrame.left; | 
 | 194 |         mInfo.frameTop = mFrame.top; | 
 | 195 |         mInfo.frameRight = mFrame.right; | 
 | 196 |         mInfo.frameBottom = mFrame.bottom; | 
 | 197 |         mInfo.globalScaleFactor = 1.0; | 
 | 198 |         mInfo.touchableRegion.clear(); | 
 | 199 |         mInfo.addTouchableRegion(mFrame); | 
 | 200 |         mInfo.visible = true; | 
| Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 201 |         mInfo.focusable = true; | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 202 |         mInfo.hasWallpaper = false; | 
 | 203 |         mInfo.paused = false; | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 204 |         mInfo.ownerPid = INJECTOR_PID; | 
 | 205 |         mInfo.ownerUid = INJECTOR_UID; | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 206 |         mInfo.displayId = ADISPLAY_ID_DEFAULT; | 
 | 207 |  | 
 | 208 |         return true; | 
 | 209 |     } | 
 | 210 |  | 
 | 211 | protected: | 
 | 212 |     Rect mFrame; | 
 | 213 | }; | 
 | 214 |  | 
 | 215 | static MotionEvent generateMotionEvent() { | 
 | 216 |     PointerProperties pointerProperties[1]; | 
 | 217 |     PointerCoords pointerCoords[1]; | 
 | 218 |  | 
 | 219 |     pointerProperties[0].clear(); | 
 | 220 |     pointerProperties[0].id = 0; | 
 | 221 |     pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; | 
 | 222 |  | 
 | 223 |     pointerCoords[0].clear(); | 
 | 224 |     pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100); | 
 | 225 |     pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100); | 
 | 226 |  | 
 | 227 |     const nsecs_t currentTime = now(); | 
 | 228 |  | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 229 |     ui::Transform identityTransform; | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 230 |     MotionEvent event; | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 231 |     event.initialize(IInputConstants::INVALID_INPUT_EVENT_ID, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, | 
| Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 232 |                      ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, | 
 | 233 |                      /* actionButton */ 0, /* flags */ 0, | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 234 |                      /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 235 |                      identityTransform, /* xPrecision */ 0, | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 236 |                      /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 237 |                      AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE, | 
 | 238 |                      AMOTION_EVENT_INVALID_DISPLAY_SIZE, currentTime, currentTime, | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 239 |                      /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
 | 240 |     return event; | 
 | 241 | } | 
 | 242 |  | 
 | 243 | static NotifyMotionArgs generateMotionArgs() { | 
 | 244 |     PointerProperties pointerProperties[1]; | 
 | 245 |     PointerCoords pointerCoords[1]; | 
 | 246 |  | 
 | 247 |     pointerProperties[0].clear(); | 
 | 248 |     pointerProperties[0].id = 0; | 
 | 249 |     pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; | 
 | 250 |  | 
 | 251 |     pointerCoords[0].clear(); | 
 | 252 |     pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100); | 
 | 253 |     pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100); | 
 | 254 |  | 
 | 255 |     const nsecs_t currentTime = now(); | 
 | 256 |     // Define a valid motion event. | 
| Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 257 |     NotifyMotionArgs args(IInputConstants::INVALID_INPUT_EVENT_ID, currentTime, currentTime, | 
 | 258 |                           DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, | 
 | 259 |                           POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN, | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 260 |                           /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0, | 
 | 261 |                           MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1, | 
 | 262 |                           pointerProperties, pointerCoords, | 
 | 263 |                           /* xPrecision */ 0, /* yPrecision */ 0, | 
 | 264 |                           AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
 | 265 |                           AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); | 
 | 266 |  | 
 | 267 |     return args; | 
 | 268 | } | 
 | 269 |  | 
 | 270 | static void benchmarkNotifyMotion(benchmark::State& state) { | 
 | 271 |     // Create dispatcher | 
 | 272 |     sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy(); | 
 | 273 |     sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy); | 
 | 274 |     dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
 | 275 |     dispatcher->start(); | 
 | 276 |  | 
 | 277 |     // Create a window that will receive motion events | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 278 |     std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 279 |     sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window"); | 
 | 280 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 281 |     dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 282 |  | 
 | 283 |     NotifyMotionArgs motionArgs = generateMotionArgs(); | 
 | 284 |  | 
 | 285 |     for (auto _ : state) { | 
 | 286 |         // Send ACTION_DOWN | 
 | 287 |         motionArgs.action = AMOTION_EVENT_ACTION_DOWN; | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 288 |         motionArgs.downTime = now(); | 
 | 289 |         motionArgs.eventTime = motionArgs.downTime; | 
 | 290 |         dispatcher->notifyMotion(&motionArgs); | 
 | 291 |  | 
 | 292 |         // Send ACTION_UP | 
 | 293 |         motionArgs.action = AMOTION_EVENT_ACTION_UP; | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 294 |         motionArgs.eventTime = now(); | 
 | 295 |         dispatcher->notifyMotion(&motionArgs); | 
 | 296 |  | 
 | 297 |         window->consumeEvent(); | 
 | 298 |         window->consumeEvent(); | 
 | 299 |     } | 
 | 300 |  | 
 | 301 |     dispatcher->stop(); | 
 | 302 | } | 
 | 303 |  | 
 | 304 | static void benchmarkInjectMotion(benchmark::State& state) { | 
 | 305 |     // Create dispatcher | 
 | 306 |     sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy(); | 
 | 307 |     sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy); | 
 | 308 |     dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
 | 309 |     dispatcher->start(); | 
 | 310 |  | 
 | 311 |     // Create a window that will receive motion events | 
| Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 312 |     std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 313 |     sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window"); | 
 | 314 |  | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 315 |     dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 316 |  | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 317 |     for (auto _ : state) { | 
| Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 318 |         MotionEvent event = generateMotionEvent(); | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 319 |         // Send ACTION_DOWN | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 320 |         dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 321 |                                      InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT, | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 322 |                                      POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); | 
 | 323 |  | 
 | 324 |         // Send ACTION_UP | 
 | 325 |         event.setAction(AMOTION_EVENT_ACTION_UP); | 
 | 326 |         dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
| Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 327 |                                      InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT, | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 328 |                                      POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); | 
 | 329 |  | 
 | 330 |         window->consumeEvent(); | 
 | 331 |         window->consumeEvent(); | 
 | 332 |     } | 
 | 333 |  | 
 | 334 |     dispatcher->stop(); | 
 | 335 | } | 
 | 336 |  | 
 | 337 | BENCHMARK(benchmarkNotifyMotion); | 
 | 338 | BENCHMARK(benchmarkInjectMotion); | 
 | 339 |  | 
 | 340 | } // namespace android::inputdispatcher | 
 | 341 |  | 
 | 342 | BENCHMARK_MAIN(); |