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