blob: 2587ac64c9ed44fd50c5a91c9a244d0978454fdf [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
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050063 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070064 *outConfig = mConfig;
65 }
66
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050067 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070068 return true;
69 }
70
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050071 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070072
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050073 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070074
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050075 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070076 return 0;
77 }
78
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050079 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070080 return false;
81 }
82
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050083 void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070084
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050085 void pokeUserActivity(nsecs_t, int32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070086
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050087 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070088
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050089 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070090
91 InputDispatcherConfiguration mConfig;
92};
93
94class FakeApplicationHandle : public InputApplicationHandle {
95public:
96 FakeApplicationHandle() {}
97 virtual ~FakeApplicationHandle() {}
98
99 virtual bool updateInfo() {
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500100 mInfo.dispatchingTimeoutMillis =
101 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700102 return true;
103 }
104};
105
106class FakeInputReceiver {
107public:
108 void consumeEvent() {
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500109 uint32_t consumeSeq = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700110 InputEvent* event;
111
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800112 std::chrono::time_point start = std::chrono::steady_clock::now();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700113 status_t result = WOULD_BLOCK;
114 while (result == WOULD_BLOCK) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800115 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
116 if (elapsed > 10ms) {
117 ALOGE("Waited too long for consumer to produce an event, giving up");
118 break;
119 }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700120 result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
121 &event);
122 }
123 if (result != OK) {
124 ALOGE("Received result = %d from consume()", result);
125 }
126 result = mConsumer->sendFinishedSignal(consumeSeq, true);
127 if (result != OK) {
128 ALOGE("Received result = %d from sendFinishedSignal", result);
129 }
130 }
131
132protected:
133 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, const std::string name)
134 : mDispatcher(dispatcher) {
Garfield Tan15601662020-09-22 15:32:38 -0700135 mClientChannel = *mDispatcher->createInputChannel(name);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700136 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
137 }
138
139 virtual ~FakeInputReceiver() {}
140
141 sp<InputDispatcher> mDispatcher;
Garfield Tan15601662020-09-22 15:32:38 -0700142 std::shared_ptr<InputChannel> mClientChannel;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700143 std::unique_ptr<InputConsumer> mConsumer;
144 PreallocatedInputEventFactory mEventFactory;
145};
146
147class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver {
148public:
149 static const int32_t WIDTH = 200;
150 static const int32_t HEIGHT = 200;
151
Chris Yea209fde2020-07-22 13:54:51 -0700152 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700153 const sp<InputDispatcher>& dispatcher, const std::string name)
154 : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700155 inputApplicationHandle->updateInfo();
156 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
157 }
158
159 virtual bool updateInfo() override {
Garfield Tan15601662020-09-22 15:32:38 -0700160 mInfo.token = mClientChannel->getConnectionToken();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700161 mInfo.name = "FakeWindowHandle";
Michael Wright44753b12020-07-08 13:48:11 +0100162 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500163 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700164 mInfo.frameLeft = mFrame.left;
165 mInfo.frameTop = mFrame.top;
166 mInfo.frameRight = mFrame.right;
167 mInfo.frameBottom = mFrame.bottom;
168 mInfo.globalScaleFactor = 1.0;
169 mInfo.touchableRegion.clear();
170 mInfo.addTouchableRegion(mFrame);
171 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700172 mInfo.focusable = true;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700173 mInfo.hasWallpaper = false;
174 mInfo.paused = false;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700175 mInfo.ownerPid = INJECTOR_PID;
176 mInfo.ownerUid = INJECTOR_UID;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700177 mInfo.displayId = ADISPLAY_ID_DEFAULT;
178
179 return true;
180 }
181
182protected:
183 Rect mFrame;
184};
185
186static MotionEvent generateMotionEvent() {
187 PointerProperties pointerProperties[1];
188 PointerCoords pointerCoords[1];
189
190 pointerProperties[0].clear();
191 pointerProperties[0].id = 0;
192 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
193
194 pointerCoords[0].clear();
195 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
196 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
197
198 const nsecs_t currentTime = now();
199
chaviw9eaa22c2020-07-01 16:21:27 -0700200 ui::Transform identityTransform;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700201 MotionEvent event;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800202 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
203 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
204 /* actionButton */ 0, /* flags */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700205 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviw9eaa22c2020-07-01 16:21:27 -0700206 identityTransform, /* xPrecision */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700207 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
208 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, currentTime,
209 /*pointerCount*/ 1, pointerProperties, pointerCoords);
210 return event;
211}
212
213static NotifyMotionArgs generateMotionArgs() {
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 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800227 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700228 ADISPLAY_ID_DEFAULT, POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
229 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
230 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
231 pointerProperties, pointerCoords,
232 /* xPrecision */ 0, /* yPrecision */ 0,
233 AMOTION_EVENT_INVALID_CURSOR_POSITION,
234 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
235
236 return args;
237}
238
239static void benchmarkNotifyMotion(benchmark::State& state) {
240 // Create dispatcher
241 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
242 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
243 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
244 dispatcher->start();
245
246 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700247 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700248 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
249
Arthur Hung72d8dc32020-03-28 00:48:39 +0000250 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700251
252 NotifyMotionArgs motionArgs = generateMotionArgs();
253
254 for (auto _ : state) {
255 // Send ACTION_DOWN
256 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800257 motionArgs.id = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700258 motionArgs.downTime = now();
259 motionArgs.eventTime = motionArgs.downTime;
260 dispatcher->notifyMotion(&motionArgs);
261
262 // Send ACTION_UP
263 motionArgs.action = AMOTION_EVENT_ACTION_UP;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800264 motionArgs.id = 1;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700265 motionArgs.eventTime = now();
266 dispatcher->notifyMotion(&motionArgs);
267
268 window->consumeEvent();
269 window->consumeEvent();
270 }
271
272 dispatcher->stop();
273}
274
275static void benchmarkInjectMotion(benchmark::State& state) {
276 // Create dispatcher
277 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
278 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
279 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
280 dispatcher->start();
281
282 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700283 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700284 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
285
Arthur Hung72d8dc32020-03-28 00:48:39 +0000286 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700287
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700288 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800289 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700290 // Send ACTION_DOWN
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700291 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800292 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700293 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
294
295 // Send ACTION_UP
296 event.setAction(AMOTION_EVENT_ACTION_UP);
297 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800298 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700299 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
300
301 window->consumeEvent();
302 window->consumeEvent();
303 }
304
305 dispatcher->stop();
306}
307
308BENCHMARK(benchmarkNotifyMotion);
309BENCHMARK(benchmarkInjectMotion);
310
311} // namespace android::inputdispatcher
312
313BENCHMARK_MAIN();