blob: 68d25f9ec712eaae9a23a713492aeb9b0f88539a [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"
23
chaviw3277faf2021-05-19 16:45:23 -050024using android::gui::WindowInfo;
25using android::gui::WindowInfoHandle;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000026using android::os::IInputConstants;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080027using android::os::InputEventInjectionResult;
28using android::os::InputEventInjectionSync;
29
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070030namespace android::inputdispatcher {
31
32// An arbitrary device id.
33static const int32_t DEVICE_ID = 1;
34
35// An arbitrary injector pid / uid pair that has permission to inject events.
36static const int32_t INJECTOR_PID = 999;
37static const int32_t INJECTOR_UID = 1001;
38
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -070039static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 5s;
40static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 100ms;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070041
42static nsecs_t now() {
43 return systemTime(SYSTEM_TIME_MONOTONIC);
44}
45
46// --- FakeInputDispatcherPolicy ---
47
48class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
49public:
50 FakeInputDispatcherPolicy() {}
51
52protected:
53 virtual ~FakeInputDispatcherPolicy() {}
54
55private:
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050056 void notifyConfigurationChanged(nsecs_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070057
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050058 void notifyNoFocusedWindowAnr(
59 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
60 ALOGE("There is no focused window for %s", applicationHandle->getName().c_str());
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070061 }
62
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +000063 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken,
64 const std::string& reason) override {
65 ALOGE("Window is not responding: %s", reason.c_str());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050066 }
67
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +000068 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {}
69
70 void notifyMonitorUnresponsive(int32_t pid, const std::string& reason) override {
71 ALOGE("Monitor is not responding: %s", reason.c_str());
72 }
73
74 void notifyMonitorResponsive(int32_t pid) override {}
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050075
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050076 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070077
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050078 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070079
Chris Yef59a2f42020-10-16 12:55:26 -070080 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
81 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
82 const std::vector<float>& values) override {}
83
84 void notifySensorAccuracy(int32_t deviceId, InputDeviceSensorType sensorType,
85 InputDeviceSensorAccuracy accuracy) override {}
86
Chris Yefb552902021-02-03 17:18:37 -080087 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
88
Bernardo Rufino2e1f6512020-10-08 13:42:07 +000089 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
90
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050091 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070092 *outConfig = mConfig;
93 }
94
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050095 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070096 return true;
97 }
98
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050099 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700100
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500101 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700102
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500103 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700104 return 0;
105 }
106
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500107 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700108 return false;
109 }
110
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500111 void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700112
Sean Stoutb4e0a592021-02-23 07:34:53 -0800113 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700114
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500115 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700116
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500117 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700118
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000119 void setPointerCapture(const PointerCaptureRequest&) override {}
Prabir Pradhan99987712020-11-10 18:43:05 -0800120
arthurhungf452d0b2021-01-06 00:19:52 +0800121 void notifyDropWindow(const sp<IBinder>&, float x, float y) override {}
122
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700123 InputDispatcherConfiguration mConfig;
124};
125
126class FakeApplicationHandle : public InputApplicationHandle {
127public:
128 FakeApplicationHandle() {}
129 virtual ~FakeApplicationHandle() {}
130
131 virtual bool updateInfo() {
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500132 mInfo.dispatchingTimeoutMillis =
133 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700134 return true;
135 }
136};
137
138class FakeInputReceiver {
139public:
140 void consumeEvent() {
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500141 uint32_t consumeSeq = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700142 InputEvent* event;
143
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800144 std::chrono::time_point start = std::chrono::steady_clock::now();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700145 status_t result = WOULD_BLOCK;
146 while (result == WOULD_BLOCK) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800147 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
148 if (elapsed > 10ms) {
149 ALOGE("Waited too long for consumer to produce an event, giving up");
150 break;
151 }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700152 result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
153 &event);
154 }
155 if (result != OK) {
156 ALOGE("Received result = %d from consume()", result);
157 }
158 result = mConsumer->sendFinishedSignal(consumeSeq, true);
159 if (result != OK) {
160 ALOGE("Received result = %d from sendFinishedSignal", result);
161 }
162 }
163
164protected:
165 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, const std::string name)
166 : mDispatcher(dispatcher) {
Garfield Tan15601662020-09-22 15:32:38 -0700167 mClientChannel = *mDispatcher->createInputChannel(name);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700168 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
169 }
170
171 virtual ~FakeInputReceiver() {}
172
173 sp<InputDispatcher> mDispatcher;
Garfield Tan15601662020-09-22 15:32:38 -0700174 std::shared_ptr<InputChannel> mClientChannel;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700175 std::unique_ptr<InputConsumer> mConsumer;
176 PreallocatedInputEventFactory mEventFactory;
177};
178
chaviw3277faf2021-05-19 16:45:23 -0500179class FakeWindowHandle : public WindowInfoHandle, public FakeInputReceiver {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700180public:
181 static const int32_t WIDTH = 200;
182 static const int32_t HEIGHT = 200;
183
Chris Yea209fde2020-07-22 13:54:51 -0700184 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700185 const sp<InputDispatcher>& dispatcher, const std::string name)
186 : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700187 inputApplicationHandle->updateInfo();
chaviw15fab6f2021-06-07 14:15:52 -0500188 updateInfo();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700189 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
190 }
191
chaviw15fab6f2021-06-07 14:15:52 -0500192 void updateInfo() {
Garfield Tan15601662020-09-22 15:32:38 -0700193 mInfo.token = mClientChannel->getConnectionToken();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700194 mInfo.name = "FakeWindowHandle";
chaviw3277faf2021-05-19 16:45:23 -0500195 mInfo.type = WindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500196 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700197 mInfo.frameLeft = mFrame.left;
198 mInfo.frameTop = mFrame.top;
199 mInfo.frameRight = mFrame.right;
200 mInfo.frameBottom = mFrame.bottom;
201 mInfo.globalScaleFactor = 1.0;
202 mInfo.touchableRegion.clear();
203 mInfo.addTouchableRegion(mFrame);
204 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700205 mInfo.focusable = true;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700206 mInfo.hasWallpaper = false;
207 mInfo.paused = false;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700208 mInfo.ownerPid = INJECTOR_PID;
209 mInfo.ownerUid = INJECTOR_UID;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700210 mInfo.displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700211 }
212
213protected:
214 Rect mFrame;
215};
216
217static MotionEvent generateMotionEvent() {
218 PointerProperties pointerProperties[1];
219 PointerCoords pointerCoords[1];
220
221 pointerProperties[0].clear();
222 pointerProperties[0].id = 0;
223 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
224
225 pointerCoords[0].clear();
226 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
227 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
228
229 const nsecs_t currentTime = now();
230
chaviw9eaa22c2020-07-01 16:21:27 -0700231 ui::Transform identityTransform;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700232 MotionEvent event;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000233 event.initialize(IInputConstants::INVALID_INPUT_EVENT_ID, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Garfield Tanfbe732e2020-01-24 11:26:14 -0800234 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
235 /* actionButton */ 0, /* flags */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700236 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviw9eaa22c2020-07-01 16:21:27 -0700237 identityTransform, /* xPrecision */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700238 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700239 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, currentTime,
240 currentTime,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700241 /*pointerCount*/ 1, pointerProperties, pointerCoords);
242 return event;
243}
244
245static NotifyMotionArgs generateMotionArgs() {
246 PointerProperties pointerProperties[1];
247 PointerCoords pointerCoords[1];
248
249 pointerProperties[0].clear();
250 pointerProperties[0].id = 0;
251 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
252
253 pointerCoords[0].clear();
254 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
255 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
256
257 const nsecs_t currentTime = now();
258 // Define a valid motion event.
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000259 NotifyMotionArgs args(IInputConstants::INVALID_INPUT_EVENT_ID, currentTime, currentTime,
260 DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
261 POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700262 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
263 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
264 pointerProperties, pointerCoords,
265 /* xPrecision */ 0, /* yPrecision */ 0,
266 AMOTION_EVENT_INVALID_CURSOR_POSITION,
267 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
268
269 return args;
270}
271
272static void benchmarkNotifyMotion(benchmark::State& state) {
273 // Create dispatcher
274 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
275 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
276 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
277 dispatcher->start();
278
279 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700280 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700281 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
282
Arthur Hung72d8dc32020-03-28 00:48:39 +0000283 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700284
285 NotifyMotionArgs motionArgs = generateMotionArgs();
286
287 for (auto _ : state) {
288 // Send ACTION_DOWN
289 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700290 motionArgs.downTime = now();
291 motionArgs.eventTime = motionArgs.downTime;
292 dispatcher->notifyMotion(&motionArgs);
293
294 // Send ACTION_UP
295 motionArgs.action = AMOTION_EVENT_ACTION_UP;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700296 motionArgs.eventTime = now();
297 dispatcher->notifyMotion(&motionArgs);
298
299 window->consumeEvent();
300 window->consumeEvent();
301 }
302
303 dispatcher->stop();
304}
305
306static void benchmarkInjectMotion(benchmark::State& state) {
307 // Create dispatcher
308 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
309 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
310 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
311 dispatcher->start();
312
313 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700314 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700315 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
316
Arthur Hung72d8dc32020-03-28 00:48:39 +0000317 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700318
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700319 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800320 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700321 // Send ACTION_DOWN
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700322 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800323 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700324 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
325
326 // Send ACTION_UP
327 event.setAction(AMOTION_EVENT_ACTION_UP);
328 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800329 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700330 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
331
332 window->consumeEvent();
333 window->consumeEvent();
334 }
335
336 dispatcher->stop();
337}
338
339BENCHMARK(benchmarkNotifyMotion);
340BENCHMARK(benchmarkInjectMotion);
341
342} // namespace android::inputdispatcher
343
344BENCHMARK_MAIN();