blob: 1b5f1ab76ef28417d000e4fcf2f7ab91117c8c5c [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
19#include <binder/Binder.h>
20#include "../dispatcher/InputDispatcher.h"
21
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080022using android::os::InputEventInjectionResult;
23using android::os::InputEventInjectionSync;
24
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070025namespace android::inputdispatcher {
26
27// An arbitrary device id.
28static const int32_t DEVICE_ID = 1;
29
30// An arbitrary injector pid / uid pair that has permission to inject events.
31static const int32_t INJECTOR_PID = 999;
32static const int32_t INJECTOR_UID = 1001;
33
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -070034static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 5s;
35static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 100ms;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070036
37static nsecs_t now() {
38 return systemTime(SYSTEM_TIME_MONOTONIC);
39}
40
41// --- FakeInputDispatcherPolicy ---
42
43class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
44public:
45 FakeInputDispatcherPolicy() {}
46
47protected:
48 virtual ~FakeInputDispatcherPolicy() {}
49
50private:
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050051 void notifyConfigurationChanged(nsecs_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070052
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050053 void notifyNoFocusedWindowAnr(
54 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
55 ALOGE("There is no focused window for %s", applicationHandle->getName().c_str());
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070056 }
57
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +000058 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken,
59 const std::string& reason) override {
60 ALOGE("Window is not responding: %s", reason.c_str());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050061 }
62
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +000063 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {}
64
65 void notifyMonitorUnresponsive(int32_t pid, const std::string& reason) override {
66 ALOGE("Monitor is not responding: %s", reason.c_str());
67 }
68
69 void notifyMonitorResponsive(int32_t pid) override {}
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050070
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050071 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070072
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050073 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070074
Chris Yef59a2f42020-10-16 12:55:26 -070075 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
76 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
77 const std::vector<float>& values) override {}
78
79 void notifySensorAccuracy(int32_t deviceId, InputDeviceSensorType sensorType,
80 InputDeviceSensorAccuracy accuracy) override {}
81
Chris Yefb552902021-02-03 17:18:37 -080082 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
83
Bernardo Rufino2e1f6512020-10-08 13:42:07 +000084 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
85
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050086 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070087 *outConfig = mConfig;
88 }
89
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050090 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070091 return true;
92 }
93
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050094 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070095
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050096 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070097
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050098 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070099 return 0;
100 }
101
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500102 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700103 return false;
104 }
105
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500106 void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700107
Sean Stoutb4e0a592021-02-23 07:34:53 -0800108 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700109
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500110 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700111
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500112 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700113
Prabir Pradhan99987712020-11-10 18:43:05 -0800114 void setPointerCapture(bool enabled) override {}
115
arthurhungf452d0b2021-01-06 00:19:52 +0800116 void notifyDropWindow(const sp<IBinder>&, float x, float y) override {}
117
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700118 InputDispatcherConfiguration mConfig;
119};
120
121class FakeApplicationHandle : public InputApplicationHandle {
122public:
123 FakeApplicationHandle() {}
124 virtual ~FakeApplicationHandle() {}
125
126 virtual bool updateInfo() {
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500127 mInfo.dispatchingTimeoutMillis =
128 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700129 return true;
130 }
131};
132
133class FakeInputReceiver {
134public:
135 void consumeEvent() {
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500136 uint32_t consumeSeq = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700137 InputEvent* event;
138
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800139 std::chrono::time_point start = std::chrono::steady_clock::now();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700140 status_t result = WOULD_BLOCK;
141 while (result == WOULD_BLOCK) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800142 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
143 if (elapsed > 10ms) {
144 ALOGE("Waited too long for consumer to produce an event, giving up");
145 break;
146 }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700147 result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
148 &event);
149 }
150 if (result != OK) {
151 ALOGE("Received result = %d from consume()", result);
152 }
153 result = mConsumer->sendFinishedSignal(consumeSeq, true);
154 if (result != OK) {
155 ALOGE("Received result = %d from sendFinishedSignal", result);
156 }
157 }
158
159protected:
160 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, const std::string name)
161 : mDispatcher(dispatcher) {
Garfield Tan15601662020-09-22 15:32:38 -0700162 mClientChannel = *mDispatcher->createInputChannel(name);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700163 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
164 }
165
166 virtual ~FakeInputReceiver() {}
167
168 sp<InputDispatcher> mDispatcher;
Garfield Tan15601662020-09-22 15:32:38 -0700169 std::shared_ptr<InputChannel> mClientChannel;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700170 std::unique_ptr<InputConsumer> mConsumer;
171 PreallocatedInputEventFactory mEventFactory;
172};
173
174class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver {
175public:
176 static const int32_t WIDTH = 200;
177 static const int32_t HEIGHT = 200;
178
Chris Yea209fde2020-07-22 13:54:51 -0700179 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700180 const sp<InputDispatcher>& dispatcher, const std::string name)
181 : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700182 inputApplicationHandle->updateInfo();
183 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
184 }
185
186 virtual bool updateInfo() override {
Garfield Tan15601662020-09-22 15:32:38 -0700187 mInfo.token = mClientChannel->getConnectionToken();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700188 mInfo.name = "FakeWindowHandle";
Michael Wright44753b12020-07-08 13:48:11 +0100189 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500190 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700191 mInfo.frameLeft = mFrame.left;
192 mInfo.frameTop = mFrame.top;
193 mInfo.frameRight = mFrame.right;
194 mInfo.frameBottom = mFrame.bottom;
195 mInfo.globalScaleFactor = 1.0;
196 mInfo.touchableRegion.clear();
197 mInfo.addTouchableRegion(mFrame);
198 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700199 mInfo.focusable = true;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700200 mInfo.hasWallpaper = false;
201 mInfo.paused = false;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700202 mInfo.ownerPid = INJECTOR_PID;
203 mInfo.ownerUid = INJECTOR_UID;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700204 mInfo.displayId = ADISPLAY_ID_DEFAULT;
205
206 return true;
207 }
208
209protected:
210 Rect mFrame;
211};
212
213static MotionEvent generateMotionEvent() {
214 PointerProperties pointerProperties[1];
215 PointerCoords pointerCoords[1];
216
217 pointerProperties[0].clear();
218 pointerProperties[0].id = 0;
219 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
220
221 pointerCoords[0].clear();
222 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
223 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
224
225 const nsecs_t currentTime = now();
226
chaviw9eaa22c2020-07-01 16:21:27 -0700227 ui::Transform identityTransform;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700228 MotionEvent event;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800229 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
230 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
231 /* actionButton */ 0, /* flags */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700232 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviw9eaa22c2020-07-01 16:21:27 -0700233 identityTransform, /* xPrecision */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700234 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700235 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
236 AMOTION_EVENT_INVALID_DISPLAY_SIZE, currentTime, currentTime,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700237 /*pointerCount*/ 1, pointerProperties, pointerCoords);
238 return event;
239}
240
241static NotifyMotionArgs generateMotionArgs() {
242 PointerProperties pointerProperties[1];
243 PointerCoords pointerCoords[1];
244
245 pointerProperties[0].clear();
246 pointerProperties[0].id = 0;
247 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
248
249 pointerCoords[0].clear();
250 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
251 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
252
253 const nsecs_t currentTime = now();
254 // Define a valid motion event.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000255 NotifyMotionArgs args(/* id */ 0, currentTime, currentTime, DEVICE_ID,
256 AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, POLICY_FLAG_PASS_TO_USER,
257 AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700258 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
259 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
260 pointerProperties, pointerCoords,
261 /* xPrecision */ 0, /* yPrecision */ 0,
262 AMOTION_EVENT_INVALID_CURSOR_POSITION,
263 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
264
265 return args;
266}
267
268static void benchmarkNotifyMotion(benchmark::State& state) {
269 // Create dispatcher
270 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
271 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
272 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
273 dispatcher->start();
274
275 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700276 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700277 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
278
Arthur Hung72d8dc32020-03-28 00:48:39 +0000279 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700280
281 NotifyMotionArgs motionArgs = generateMotionArgs();
282
283 for (auto _ : state) {
284 // Send ACTION_DOWN
285 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800286 motionArgs.id = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700287 motionArgs.downTime = now();
288 motionArgs.eventTime = motionArgs.downTime;
289 dispatcher->notifyMotion(&motionArgs);
290
291 // Send ACTION_UP
292 motionArgs.action = AMOTION_EVENT_ACTION_UP;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800293 motionArgs.id = 1;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700294 motionArgs.eventTime = now();
295 dispatcher->notifyMotion(&motionArgs);
296
297 window->consumeEvent();
298 window->consumeEvent();
299 }
300
301 dispatcher->stop();
302}
303
304static void benchmarkInjectMotion(benchmark::State& state) {
305 // Create dispatcher
306 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
307 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
308 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
309 dispatcher->start();
310
311 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700312 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700313 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
314
Arthur Hung72d8dc32020-03-28 00:48:39 +0000315 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700316
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700317 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800318 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700319 // Send ACTION_DOWN
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700320 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800321 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700322 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
323
324 // Send ACTION_UP
325 event.setAction(AMOTION_EVENT_ACTION_UP);
326 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800327 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700328 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
329
330 window->consumeEvent();
331 window->consumeEvent();
332 }
333
334 dispatcher->stop();
335}
336
337BENCHMARK(benchmarkNotifyMotion);
338BENCHMARK(benchmarkInjectMotion);
339
340} // namespace android::inputdispatcher
341
342BENCHMARK_MAIN();