blob: 96c8640e2fc35394b1d5dd970d251dccc2f4cea1 [file] [log] [blame]
Siarhei Vishniakoud0784762019-11-01 15:33:48 -07001/*
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 Vishniakouf2652122021-03-05 21:39:46 +000019#include <android/os/IInputConstants.h>
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070020#include <binder/Binder.h>
21#include "../dispatcher/InputDispatcher.h"
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070022#include "../tests/FakeApplicationHandle.h"
23#include "../tests/FakeInputDispatcherPolicy.h"
Prabir Pradhanc5340732024-03-20 22:53:52 +000024#include "../tests/FakeWindows.h"
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070025
Siarhei Vishniakou18050092021-09-01 13:32:49 -070026using android::base::Result;
chaviw3277faf2021-05-19 16:45:23 -050027using android::gui::WindowInfo;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000028using android::os::IInputConstants;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080029using android::os::InputEventInjectionResult;
30using android::os::InputEventInjectionSync;
31
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070032namespace android::inputdispatcher {
33
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -070034namespace {
35
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070036// An arbitrary device id.
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070037constexpr DeviceId DEVICE_ID = 1;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070038
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070039// An arbitrary display id
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070040constexpr ui::LogicalDisplayId DISPLAY_ID = ui::LogicalDisplayId::DEFAULT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070041
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -070042static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 5s;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070043
44static nsecs_t now() {
45 return systemTime(SYSTEM_TIME_MONOTONIC);
46}
47
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070048static MotionEvent generateMotionEvent() {
49 PointerProperties pointerProperties[1];
50 PointerCoords pointerCoords[1];
51
52 pointerProperties[0].clear();
53 pointerProperties[0].id = 0;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070054 pointerProperties[0].toolType = ToolType::FINGER;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070055
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
chaviw9eaa22c2020-07-01 16:21:27 -070062 ui::Transform identityTransform;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070063 MotionEvent event;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000064 event.initialize(IInputConstants::INVALID_INPUT_EVENT_ID, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070065 ui::LogicalDisplayId::DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
Garfield Tanfbe732e2020-01-24 11:26:14 -080066 /* actionButton */ 0, /* flags */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070067 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviw9eaa22c2020-07-01 16:21:27 -070068 identityTransform, /* xPrecision */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070069 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -070070 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, currentTime,
71 currentTime,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070072 /*pointerCount*/ 1, pointerProperties, pointerCoords);
73 return event;
74}
75
76static NotifyMotionArgs generateMotionArgs() {
77 PointerProperties pointerProperties[1];
78 PointerCoords pointerCoords[1];
79
80 pointerProperties[0].clear();
81 pointerProperties[0].id = 0;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070082 pointerProperties[0].toolType = ToolType::FINGER;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070083
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 Vishniakouf2652122021-03-05 21:39:46 +000090 NotifyMotionArgs args(IInputConstants::INVALID_INPUT_EVENT_ID, currentTime, currentTime,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070091 DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000092 POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070093 /* 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
103static void benchmarkNotifyMotion(benchmark::State& state) {
104 // Create dispatcher
Prabir Pradhana41d2442023-04-20 21:30:40 +0000105 FakeInputDispatcherPolicy fakePolicy;
Prabir Pradhanc5340732024-03-20 22:53:52 +0000106 auto dispatcher = std::make_unique<InputDispatcher>(fakePolicy);
107 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
108 dispatcher->start();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700109
110 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700111 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700112 sp<FakeWindowHandle> window =
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700113 sp<FakeWindowHandle>::make(application, dispatcher, "Fake Window", DISPLAY_ID);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700114
Prabir Pradhanc5340732024-03-20 22:53:52 +0000115 dispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700116
117 NotifyMotionArgs motionArgs = generateMotionArgs();
118
119 for (auto _ : state) {
120 // Send ACTION_DOWN
121 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700122 motionArgs.downTime = now();
123 motionArgs.eventTime = motionArgs.downTime;
Prabir Pradhanc5340732024-03-20 22:53:52 +0000124 dispatcher->notifyMotion(motionArgs);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700125
126 // Send ACTION_UP
127 motionArgs.action = AMOTION_EVENT_ACTION_UP;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700128 motionArgs.eventTime = now();
Prabir Pradhanc5340732024-03-20 22:53:52 +0000129 dispatcher->notifyMotion(motionArgs);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700130
Prabir Pradhanc5340732024-03-20 22:53:52 +0000131 window->consumeMotionEvent();
132 window->consumeMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700133 }
134
Prabir Pradhanc5340732024-03-20 22:53:52 +0000135 dispatcher->stop();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700136}
137
138static void benchmarkInjectMotion(benchmark::State& state) {
139 // Create dispatcher
Prabir Pradhana41d2442023-04-20 21:30:40 +0000140 FakeInputDispatcherPolicy fakePolicy;
Prabir Pradhanc5340732024-03-20 22:53:52 +0000141 auto dispatcher = std::make_unique<InputDispatcher>(fakePolicy);
142 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
143 dispatcher->start();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700144
145 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700146 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700147 sp<FakeWindowHandle> window =
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700148 sp<FakeWindowHandle>::make(application, dispatcher, "Fake Window", DISPLAY_ID);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700149
Prabir Pradhanc5340732024-03-20 22:53:52 +0000150 dispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700151
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700152 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800153 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700154 // Send ACTION_DOWN
Prabir Pradhanc5340732024-03-20 22:53:52 +0000155 dispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
156 INJECT_EVENT_TIMEOUT,
157 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700158
159 // Send ACTION_UP
160 event.setAction(AMOTION_EVENT_ACTION_UP);
Prabir Pradhanc5340732024-03-20 22:53:52 +0000161 dispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
162 INJECT_EVENT_TIMEOUT,
163 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700164
Prabir Pradhanc5340732024-03-20 22:53:52 +0000165 window->consumeMotionEvent();
166 window->consumeMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700167 }
168
Prabir Pradhanc5340732024-03-20 22:53:52 +0000169 dispatcher->stop();
Siarhei Vishniakou8c84b802022-03-23 15:48:41 -0700170}
171
172static void benchmarkOnWindowInfosChanged(benchmark::State& state) {
173 // Create dispatcher
Prabir Pradhana41d2442023-04-20 21:30:40 +0000174 FakeInputDispatcherPolicy fakePolicy;
Prabir Pradhanc5340732024-03-20 22:53:52 +0000175 auto dispatcher = std::make_unique<InputDispatcher>(fakePolicy);
176 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
177 dispatcher->start();
Siarhei Vishniakou8c84b802022-03-23 15:48:41 -0700178
179 // Create a window
180 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700181 sp<FakeWindowHandle> window =
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700182 sp<FakeWindowHandle>::make(application, dispatcher, "Fake Window", DISPLAY_ID);
Siarhei Vishniakou8c84b802022-03-23 15:48:41 -0700183
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 Pradhanc5340732024-03-20 22:53:52 +0000190 dispatcher->onWindowInfosChanged(
Patrick Williamsd828f302023-04-28 17:52:08 -0500191 {windowInfos, displayInfos, /*vsyncId=*/0, /*timestamp=*/0});
Prabir Pradhanc5340732024-03-20 22:53:52 +0000192 dispatcher->onWindowInfosChanged(
Patrick Williamsd828f302023-04-28 17:52:08 -0500193 {/*windowInfos=*/{}, /*displayInfos=*/{}, /*vsyncId=*/{}, /*timestamp=*/0});
Siarhei Vishniakou8c84b802022-03-23 15:48:41 -0700194 }
Prabir Pradhanc5340732024-03-20 22:53:52 +0000195 dispatcher->stop();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700196}
197
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -0700198} // namespace
199
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700200BENCHMARK(benchmarkNotifyMotion);
201BENCHMARK(benchmarkInjectMotion);
Siarhei Vishniakou8c84b802022-03-23 15:48:41 -0700202BENCHMARK(benchmarkOnWindowInfosChanged);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700203
204} // namespace android::inputdispatcher
205
206BENCHMARK_MAIN();