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