blob: c2d165e0f4fa388b26049f56be43f1f9fd95cc3f [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
Chris Yea209fde2020-07-22 13:54:51 -070053 std::chrono::nanoseconds notifyAnr(const std::shared_ptr<InputApplicationHandle>&,
54 const sp<IBinder>&, const std::string& name) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070055 ALOGE("The window is not responding : %s", name.c_str());
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050056 return 0s;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070057 }
58
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050059 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070060
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050061 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070062
Bernardo Rufino2e1f6512020-10-08 13:42:07 +000063 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
64
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050065 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070066 *outConfig = mConfig;
67 }
68
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050069 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070070 return true;
71 }
72
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050073 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070074
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050075 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070076
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050077 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070078 return 0;
79 }
80
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050081 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070082 return false;
83 }
84
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050085 void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070086
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050087 void pokeUserActivity(nsecs_t, int32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070088
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050089 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070090
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050091 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070092
93 InputDispatcherConfiguration mConfig;
94};
95
96class FakeApplicationHandle : public InputApplicationHandle {
97public:
98 FakeApplicationHandle() {}
99 virtual ~FakeApplicationHandle() {}
100
101 virtual bool updateInfo() {
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500102 mInfo.dispatchingTimeoutMillis =
103 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700104 return true;
105 }
106};
107
108class FakeInputReceiver {
109public:
110 void consumeEvent() {
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500111 uint32_t consumeSeq = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700112 InputEvent* event;
113
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800114 std::chrono::time_point start = std::chrono::steady_clock::now();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700115 status_t result = WOULD_BLOCK;
116 while (result == WOULD_BLOCK) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800117 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
118 if (elapsed > 10ms) {
119 ALOGE("Waited too long for consumer to produce an event, giving up");
120 break;
121 }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700122 result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
123 &event);
124 }
125 if (result != OK) {
126 ALOGE("Received result = %d from consume()", result);
127 }
128 result = mConsumer->sendFinishedSignal(consumeSeq, true);
129 if (result != OK) {
130 ALOGE("Received result = %d from sendFinishedSignal", result);
131 }
132 }
133
134protected:
135 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, const std::string name)
136 : mDispatcher(dispatcher) {
Garfield Tan15601662020-09-22 15:32:38 -0700137 mClientChannel = *mDispatcher->createInputChannel(name);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700138 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
139 }
140
141 virtual ~FakeInputReceiver() {}
142
143 sp<InputDispatcher> mDispatcher;
Garfield Tan15601662020-09-22 15:32:38 -0700144 std::shared_ptr<InputChannel> mClientChannel;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700145 std::unique_ptr<InputConsumer> mConsumer;
146 PreallocatedInputEventFactory mEventFactory;
147};
148
149class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver {
150public:
151 static const int32_t WIDTH = 200;
152 static const int32_t HEIGHT = 200;
153
Chris Yea209fde2020-07-22 13:54:51 -0700154 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700155 const sp<InputDispatcher>& dispatcher, const std::string name)
156 : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700157 inputApplicationHandle->updateInfo();
158 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
159 }
160
161 virtual bool updateInfo() override {
Garfield Tan15601662020-09-22 15:32:38 -0700162 mInfo.token = mClientChannel->getConnectionToken();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700163 mInfo.name = "FakeWindowHandle";
Michael Wright44753b12020-07-08 13:48:11 +0100164 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500165 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700166 mInfo.frameLeft = mFrame.left;
167 mInfo.frameTop = mFrame.top;
168 mInfo.frameRight = mFrame.right;
169 mInfo.frameBottom = mFrame.bottom;
170 mInfo.globalScaleFactor = 1.0;
171 mInfo.touchableRegion.clear();
172 mInfo.addTouchableRegion(mFrame);
173 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700174 mInfo.focusable = true;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700175 mInfo.hasWallpaper = false;
176 mInfo.paused = false;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700177 mInfo.ownerPid = INJECTOR_PID;
178 mInfo.ownerUid = INJECTOR_UID;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700179 mInfo.displayId = ADISPLAY_ID_DEFAULT;
180
181 return true;
182 }
183
184protected:
185 Rect mFrame;
186};
187
188static MotionEvent generateMotionEvent() {
189 PointerProperties pointerProperties[1];
190 PointerCoords pointerCoords[1];
191
192 pointerProperties[0].clear();
193 pointerProperties[0].id = 0;
194 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
195
196 pointerCoords[0].clear();
197 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
198 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
199
200 const nsecs_t currentTime = now();
201
chaviw9eaa22c2020-07-01 16:21:27 -0700202 ui::Transform identityTransform;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700203 MotionEvent event;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800204 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
205 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
206 /* actionButton */ 0, /* flags */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700207 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviw9eaa22c2020-07-01 16:21:27 -0700208 identityTransform, /* xPrecision */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700209 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
210 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, currentTime,
211 /*pointerCount*/ 1, pointerProperties, pointerCoords);
212 return event;
213}
214
215static NotifyMotionArgs generateMotionArgs() {
216 PointerProperties pointerProperties[1];
217 PointerCoords pointerCoords[1];
218
219 pointerProperties[0].clear();
220 pointerProperties[0].id = 0;
221 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
222
223 pointerCoords[0].clear();
224 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
225 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
226
227 const nsecs_t currentTime = now();
228 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800229 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700230 ADISPLAY_ID_DEFAULT, POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
231 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
232 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
233 pointerProperties, pointerCoords,
234 /* xPrecision */ 0, /* yPrecision */ 0,
235 AMOTION_EVENT_INVALID_CURSOR_POSITION,
236 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
237
238 return args;
239}
240
241static void benchmarkNotifyMotion(benchmark::State& state) {
242 // Create dispatcher
243 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
244 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
245 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
246 dispatcher->start();
247
248 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700249 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700250 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
251
Arthur Hung72d8dc32020-03-28 00:48:39 +0000252 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700253
254 NotifyMotionArgs motionArgs = generateMotionArgs();
255
256 for (auto _ : state) {
257 // Send ACTION_DOWN
258 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800259 motionArgs.id = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700260 motionArgs.downTime = now();
261 motionArgs.eventTime = motionArgs.downTime;
262 dispatcher->notifyMotion(&motionArgs);
263
264 // Send ACTION_UP
265 motionArgs.action = AMOTION_EVENT_ACTION_UP;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800266 motionArgs.id = 1;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700267 motionArgs.eventTime = now();
268 dispatcher->notifyMotion(&motionArgs);
269
270 window->consumeEvent();
271 window->consumeEvent();
272 }
273
274 dispatcher->stop();
275}
276
277static void benchmarkInjectMotion(benchmark::State& state) {
278 // Create dispatcher
279 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
280 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
281 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
282 dispatcher->start();
283
284 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700285 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700286 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
287
Arthur Hung72d8dc32020-03-28 00:48:39 +0000288 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700289
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700290 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800291 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700292 // Send ACTION_DOWN
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700293 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800294 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700295 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
296
297 // Send ACTION_UP
298 event.setAction(AMOTION_EVENT_ACTION_UP);
299 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800300 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700301 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
302
303 window->consumeEvent();
304 window->consumeEvent();
305 }
306
307 dispatcher->stop();
308}
309
310BENCHMARK(benchmarkNotifyMotion);
311BENCHMARK(benchmarkInjectMotion);
312
313} // namespace android::inputdispatcher
314
315BENCHMARK_MAIN();