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