Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <benchmark/benchmark.h> |
| 18 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 19 | #include <android/os/IInputConstants.h> |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 20 | #include <binder/Binder.h> |
| 21 | #include "../dispatcher/InputDispatcher.h" |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 22 | #include "../tests/FakeApplicationHandle.h" |
| 23 | #include "../tests/FakeInputDispatcherPolicy.h" |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 24 | #include "../tests/FakeWindows.h" |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 25 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 26 | using android::base::Result; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 27 | using android::gui::WindowInfo; |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 28 | using android::os::IInputConstants; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 29 | using android::os::InputEventInjectionResult; |
| 30 | using android::os::InputEventInjectionSync; |
| 31 | |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 32 | namespace android::inputdispatcher { |
| 33 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 34 | namespace { |
| 35 | |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 36 | // An arbitrary device id. |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 37 | constexpr DeviceId DEVICE_ID = 1; |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 38 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 39 | // An arbitrary display id |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 40 | constexpr ui::LogicalDisplayId DISPLAY_ID = ui::LogicalDisplayId::DEFAULT; |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 41 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 42 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 5s; |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 43 | |
| 44 | static nsecs_t now() { |
| 45 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 46 | } |
| 47 | |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 48 | static MotionEvent generateMotionEvent() { |
| 49 | PointerProperties pointerProperties[1]; |
| 50 | PointerCoords pointerCoords[1]; |
| 51 | |
| 52 | pointerProperties[0].clear(); |
| 53 | pointerProperties[0].id = 0; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 54 | pointerProperties[0].toolType = ToolType::FINGER; |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 55 | |
| 56 | pointerCoords[0].clear(); |
| 57 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100); |
| 58 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100); |
| 59 | |
| 60 | const nsecs_t currentTime = now(); |
| 61 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 62 | ui::Transform identityTransform; |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 63 | MotionEvent event; |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 64 | event.initialize(IInputConstants::INVALID_INPUT_EVENT_ID, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 65 | ui::LogicalDisplayId::DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 66 | /* actionButton */ 0, /* flags */ 0, |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 67 | /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 68 | identityTransform, /* xPrecision */ 0, |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 69 | /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 70 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, currentTime, |
| 71 | currentTime, |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 72 | /*pointerCount*/ 1, pointerProperties, pointerCoords); |
| 73 | return event; |
| 74 | } |
| 75 | |
| 76 | static NotifyMotionArgs generateMotionArgs() { |
| 77 | PointerProperties pointerProperties[1]; |
| 78 | PointerCoords pointerCoords[1]; |
| 79 | |
| 80 | pointerProperties[0].clear(); |
| 81 | pointerProperties[0].id = 0; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 82 | pointerProperties[0].toolType = ToolType::FINGER; |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 83 | |
| 84 | pointerCoords[0].clear(); |
| 85 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100); |
| 86 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100); |
| 87 | |
| 88 | const nsecs_t currentTime = now(); |
| 89 | // Define a valid motion event. |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 90 | NotifyMotionArgs args(IInputConstants::INVALID_INPUT_EVENT_ID, currentTime, currentTime, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 91 | DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 92 | POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 93 | /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0, |
| 94 | MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1, |
| 95 | pointerProperties, pointerCoords, |
| 96 | /* xPrecision */ 0, /* yPrecision */ 0, |
| 97 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 98 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); |
| 99 | |
| 100 | return args; |
| 101 | } |
| 102 | |
| 103 | static void benchmarkNotifyMotion(benchmark::State& state) { |
| 104 | // Create dispatcher |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 105 | FakeInputDispatcherPolicy fakePolicy; |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 106 | auto dispatcher = std::make_unique<InputDispatcher>(fakePolicy); |
| 107 | dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 108 | dispatcher->start(); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 109 | |
| 110 | // Create a window that will receive motion events |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 111 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 112 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 113 | sp<FakeWindowHandle>::make(application, dispatcher, "Fake Window", DISPLAY_ID); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 114 | |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 115 | dispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 116 | |
| 117 | NotifyMotionArgs motionArgs = generateMotionArgs(); |
| 118 | |
| 119 | for (auto _ : state) { |
| 120 | // Send ACTION_DOWN |
| 121 | motionArgs.action = AMOTION_EVENT_ACTION_DOWN; |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 122 | motionArgs.downTime = now(); |
| 123 | motionArgs.eventTime = motionArgs.downTime; |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 124 | dispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 125 | |
| 126 | // Send ACTION_UP |
| 127 | motionArgs.action = AMOTION_EVENT_ACTION_UP; |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 128 | motionArgs.eventTime = now(); |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 129 | dispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 130 | |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 131 | window->consumeMotionEvent(); |
| 132 | window->consumeMotionEvent(); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 135 | dispatcher->stop(); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static void benchmarkInjectMotion(benchmark::State& state) { |
| 139 | // Create dispatcher |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 140 | FakeInputDispatcherPolicy fakePolicy; |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 141 | auto dispatcher = std::make_unique<InputDispatcher>(fakePolicy); |
| 142 | dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 143 | dispatcher->start(); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 144 | |
| 145 | // Create a window that will receive motion events |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 146 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 147 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 148 | sp<FakeWindowHandle>::make(application, dispatcher, "Fake Window", DISPLAY_ID); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 149 | |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 150 | dispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 151 | |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 152 | for (auto _ : state) { |
Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 153 | MotionEvent event = generateMotionEvent(); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 154 | // Send ACTION_DOWN |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 155 | dispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
| 156 | INJECT_EVENT_TIMEOUT, |
| 157 | POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 158 | |
| 159 | // Send ACTION_UP |
| 160 | event.setAction(AMOTION_EVENT_ACTION_UP); |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 161 | dispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
| 162 | INJECT_EVENT_TIMEOUT, |
| 163 | POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 164 | |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 165 | window->consumeMotionEvent(); |
| 166 | window->consumeMotionEvent(); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 169 | dispatcher->stop(); |
Siarhei Vishniakou | 8c84b80 | 2022-03-23 15:48:41 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static void benchmarkOnWindowInfosChanged(benchmark::State& state) { |
| 173 | // Create dispatcher |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 174 | FakeInputDispatcherPolicy fakePolicy; |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 175 | auto dispatcher = std::make_unique<InputDispatcher>(fakePolicy); |
| 176 | dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); |
| 177 | dispatcher->start(); |
Siarhei Vishniakou | 8c84b80 | 2022-03-23 15:48:41 -0700 | [diff] [blame] | 178 | |
| 179 | // Create a window |
| 180 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 181 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 182 | sp<FakeWindowHandle>::make(application, dispatcher, "Fake Window", DISPLAY_ID); |
Siarhei Vishniakou | 8c84b80 | 2022-03-23 15:48:41 -0700 | [diff] [blame] | 183 | |
| 184 | std::vector<gui::WindowInfo> windowInfos{*window->getInfo()}; |
| 185 | gui::DisplayInfo info; |
| 186 | info.displayId = window->getInfo()->displayId; |
| 187 | std::vector<gui::DisplayInfo> displayInfos{info}; |
| 188 | |
| 189 | for (auto _ : state) { |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 190 | dispatcher->onWindowInfosChanged( |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 191 | {windowInfos, displayInfos, /*vsyncId=*/0, /*timestamp=*/0}); |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 192 | dispatcher->onWindowInfosChanged( |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 193 | {/*windowInfos=*/{}, /*displayInfos=*/{}, /*vsyncId=*/{}, /*timestamp=*/0}); |
Siarhei Vishniakou | 8c84b80 | 2022-03-23 15:48:41 -0700 | [diff] [blame] | 194 | } |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 195 | dispatcher->stop(); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 198 | } // namespace |
| 199 | |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 200 | BENCHMARK(benchmarkNotifyMotion); |
| 201 | BENCHMARK(benchmarkInjectMotion); |
Siarhei Vishniakou | 8c84b80 | 2022-03-23 15:48:41 -0700 | [diff] [blame] | 202 | BENCHMARK(benchmarkOnWindowInfosChanged); |
Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 203 | |
| 204 | } // namespace android::inputdispatcher |
| 205 | |
| 206 | BENCHMARK_MAIN(); |