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