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