blob: 5f955908c5e3b7895ad70eb03e394bfd367122fe [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>
chaviw3277faf2021-05-19 16:45:23 -050021#include <gui/constants.h>
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070022#include "../dispatcher/InputDispatcher.h"
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070023#include "../tests/FakeApplicationHandle.h"
24#include "../tests/FakeInputDispatcherPolicy.h"
Prabir Pradhanc5340732024-03-20 22:53:52 +000025#include "../tests/FakeWindows.h"
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070026
Siarhei Vishniakou18050092021-09-01 13:32:49 -070027using android::base::Result;
chaviw3277faf2021-05-19 16:45:23 -050028using android::gui::WindowInfo;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000029using android::os::IInputConstants;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080030using android::os::InputEventInjectionResult;
31using android::os::InputEventInjectionSync;
32
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070033namespace android::inputdispatcher {
34
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -070035namespace {
36
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070037// An arbitrary device id.
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070038constexpr DeviceId DEVICE_ID = 1;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070039
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070040// An arbitrary display id
41constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070042
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -070043static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 5s;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070044
45static nsecs_t now() {
46 return systemTime(SYSTEM_TIME_MONOTONIC);
47}
48
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070049static MotionEvent generateMotionEvent() {
50 PointerProperties pointerProperties[1];
51 PointerCoords pointerCoords[1];
52
53 pointerProperties[0].clear();
54 pointerProperties[0].id = 0;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070055 pointerProperties[0].toolType = ToolType::FINGER;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070056
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
chaviw9eaa22c2020-07-01 16:21:27 -070063 ui::Transform identityTransform;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070064 MotionEvent event;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000065 event.initialize(IInputConstants::INVALID_INPUT_EVENT_ID, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Garfield Tanfbe732e2020-01-24 11:26:14 -080066 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
67 /* actionButton */ 0, /* flags */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070068 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviw9eaa22c2020-07-01 16:21:27 -070069 identityTransform, /* xPrecision */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070070 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -070071 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, currentTime,
72 currentTime,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070073 /*pointerCount*/ 1, pointerProperties, pointerCoords);
74 return event;
75}
76
77static NotifyMotionArgs generateMotionArgs() {
78 PointerProperties pointerProperties[1];
79 PointerCoords pointerCoords[1];
80
81 pointerProperties[0].clear();
82 pointerProperties[0].id = 0;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070083 pointerProperties[0].toolType = ToolType::FINGER;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070084
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 Vishniakouf2652122021-03-05 21:39:46 +000091 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 Vishniakoud0784762019-11-01 15:33:48 -070094 /* 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
104static void benchmarkNotifyMotion(benchmark::State& state) {
105 // Create dispatcher
Prabir Pradhana41d2442023-04-20 21:30:40 +0000106 FakeInputDispatcherPolicy fakePolicy;
Prabir Pradhanc5340732024-03-20 22:53:52 +0000107 auto dispatcher = std::make_unique<InputDispatcher>(fakePolicy);
108 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
109 dispatcher->start();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700110
111 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700112 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700113 sp<FakeWindowHandle> window =
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700114 sp<FakeWindowHandle>::make(application, dispatcher, "Fake Window", DISPLAY_ID);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700115
Prabir Pradhanc5340732024-03-20 22:53:52 +0000116 dispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700117
118 NotifyMotionArgs motionArgs = generateMotionArgs();
119
120 for (auto _ : state) {
121 // Send ACTION_DOWN
122 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700123 motionArgs.downTime = now();
124 motionArgs.eventTime = motionArgs.downTime;
Prabir Pradhanc5340732024-03-20 22:53:52 +0000125 dispatcher->notifyMotion(motionArgs);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700126
127 // Send ACTION_UP
128 motionArgs.action = AMOTION_EVENT_ACTION_UP;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700129 motionArgs.eventTime = now();
Prabir Pradhanc5340732024-03-20 22:53:52 +0000130 dispatcher->notifyMotion(motionArgs);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700131
Prabir Pradhanc5340732024-03-20 22:53:52 +0000132 window->consumeMotionEvent();
133 window->consumeMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700134 }
135
Prabir Pradhanc5340732024-03-20 22:53:52 +0000136 dispatcher->stop();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700137}
138
139static void benchmarkInjectMotion(benchmark::State& state) {
140 // Create dispatcher
Prabir Pradhana41d2442023-04-20 21:30:40 +0000141 FakeInputDispatcherPolicy fakePolicy;
Prabir Pradhanc5340732024-03-20 22:53:52 +0000142 auto dispatcher = std::make_unique<InputDispatcher>(fakePolicy);
143 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
144 dispatcher->start();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700145
146 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700147 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700148 sp<FakeWindowHandle> window =
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700149 sp<FakeWindowHandle>::make(application, dispatcher, "Fake Window", DISPLAY_ID);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700150
Prabir Pradhanc5340732024-03-20 22:53:52 +0000151 dispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700152
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700153 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800154 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700155 // Send ACTION_DOWN
Prabir Pradhanc5340732024-03-20 22:53:52 +0000156 dispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
157 INJECT_EVENT_TIMEOUT,
158 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700159
160 // Send ACTION_UP
161 event.setAction(AMOTION_EVENT_ACTION_UP);
Prabir Pradhanc5340732024-03-20 22:53:52 +0000162 dispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE,
163 INJECT_EVENT_TIMEOUT,
164 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700165
Prabir Pradhanc5340732024-03-20 22:53:52 +0000166 window->consumeMotionEvent();
167 window->consumeMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700168 }
169
Prabir Pradhanc5340732024-03-20 22:53:52 +0000170 dispatcher->stop();
Siarhei Vishniakou8c84b802022-03-23 15:48:41 -0700171}
172
173static void benchmarkOnWindowInfosChanged(benchmark::State& state) {
174 // Create dispatcher
Prabir Pradhana41d2442023-04-20 21:30:40 +0000175 FakeInputDispatcherPolicy fakePolicy;
Prabir Pradhanc5340732024-03-20 22:53:52 +0000176 auto dispatcher = std::make_unique<InputDispatcher>(fakePolicy);
177 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
178 dispatcher->start();
Siarhei Vishniakou8c84b802022-03-23 15:48:41 -0700179
180 // Create a window
181 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700182 sp<FakeWindowHandle> window =
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700183 sp<FakeWindowHandle>::make(application, dispatcher, "Fake Window", DISPLAY_ID);
Siarhei Vishniakou8c84b802022-03-23 15:48:41 -0700184
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 Pradhanc5340732024-03-20 22:53:52 +0000191 dispatcher->onWindowInfosChanged(
Patrick Williamsd828f302023-04-28 17:52:08 -0500192 {windowInfos, displayInfos, /*vsyncId=*/0, /*timestamp=*/0});
Prabir Pradhanc5340732024-03-20 22:53:52 +0000193 dispatcher->onWindowInfosChanged(
Patrick Williamsd828f302023-04-28 17:52:08 -0500194 {/*windowInfos=*/{}, /*displayInfos=*/{}, /*vsyncId=*/{}, /*timestamp=*/0});
Siarhei Vishniakou8c84b802022-03-23 15:48:41 -0700195 }
Prabir Pradhanc5340732024-03-20 22:53:52 +0000196 dispatcher->stop();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700197}
198
Siarhei Vishniakouadeb6fa2023-05-26 09:11:06 -0700199} // namespace
200
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700201BENCHMARK(benchmarkNotifyMotion);
202BENCHMARK(benchmarkInjectMotion);
Siarhei Vishniakou8c84b802022-03-23 15:48:41 -0700203BENCHMARK(benchmarkOnWindowInfosChanged);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700204
205} // namespace android::inputdispatcher
206
207BENCHMARK_MAIN();