blob: 2116cbcc7f85eaf0dff1f5a0ed45440f6f937ad7 [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
22namespace android::inputdispatcher {
23
24// An arbitrary device id.
25static const int32_t DEVICE_ID = 1;
26
27// An arbitrary injector pid / uid pair that has permission to inject events.
28static const int32_t INJECTOR_PID = 999;
29static const int32_t INJECTOR_UID = 1001;
30
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -070031static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 5s;
32static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 100ms;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070033
34static nsecs_t now() {
35 return systemTime(SYSTEM_TIME_MONOTONIC);
36}
37
38// --- FakeInputDispatcherPolicy ---
39
40class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
41public:
42 FakeInputDispatcherPolicy() {}
43
44protected:
45 virtual ~FakeInputDispatcherPolicy() {}
46
47private:
48 virtual void notifyConfigurationChanged(nsecs_t) override {}
49
50 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>&, const sp<IBinder>&,
51 const std::string& name) override {
52 ALOGE("The window is not responding : %s", name.c_str());
53 return 0;
54 }
55
56 virtual void notifyInputChannelBroken(const sp<IBinder>&) override {}
57
58 virtual void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
59
60 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
61 *outConfig = mConfig;
62 }
63
64 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
65 return true;
66 }
67
68 virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
69
70 virtual void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
71
72 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*,
73 uint32_t) override {
74 return 0;
75 }
76
77 virtual bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t,
78 KeyEvent*) override {
79 return false;
80 }
81
82 virtual void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {}
83
84 virtual void pokeUserActivity(nsecs_t, int32_t) override {}
85
86 virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override {
87 return false;
88 }
89
90 virtual void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {}
91
92 InputDispatcherConfiguration mConfig;
93};
94
95class FakeApplicationHandle : public InputApplicationHandle {
96public:
97 FakeApplicationHandle() {}
98 virtual ~FakeApplicationHandle() {}
99
100 virtual bool updateInfo() {
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700101 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT.count();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700102 return true;
103 }
104};
105
106class FakeInputReceiver {
107public:
108 void consumeEvent() {
109 uint32_t consumeSeq;
110 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) {
135 InputChannel::openInputChannelPair(name, mServerChannel, mClientChannel);
136 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
137 }
138
139 virtual ~FakeInputReceiver() {}
140
141 sp<InputDispatcher> mDispatcher;
142 sp<InputChannel> mServerChannel, mClientChannel;
143 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
152 FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle,
153 const sp<InputDispatcher>& dispatcher, const std::string name)
154 : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
155 mDispatcher->registerInputChannel(mServerChannel);
156
157 inputApplicationHandle->updateInfo();
158 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
159 }
160
161 virtual bool updateInfo() override {
162 mInfo.token = mServerChannel->getConnectionToken();
163 mInfo.name = "FakeWindowHandle";
164 mInfo.layoutParamsFlags = 0;
165 mInfo.layoutParamsType = InputWindowInfo::TYPE_APPLICATION;
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700166 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT.count();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700167 mInfo.frameLeft = mFrame.left;
168 mInfo.frameTop = mFrame.top;
169 mInfo.frameRight = mFrame.right;
170 mInfo.frameBottom = mFrame.bottom;
171 mInfo.globalScaleFactor = 1.0;
172 mInfo.touchableRegion.clear();
173 mInfo.addTouchableRegion(mFrame);
174 mInfo.visible = true;
175 mInfo.canReceiveKeys = true;
176 mInfo.hasFocus = true;
177 mInfo.hasWallpaper = false;
178 mInfo.paused = false;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700179 mInfo.ownerPid = INJECTOR_PID;
180 mInfo.ownerUid = INJECTOR_UID;
181 mInfo.inputFeatures = 0;
182 mInfo.displayId = ADISPLAY_ID_DEFAULT;
183
184 return true;
185 }
186
187protected:
188 Rect mFrame;
189};
190
191static MotionEvent generateMotionEvent() {
192 PointerProperties pointerProperties[1];
193 PointerCoords pointerCoords[1];
194
195 pointerProperties[0].clear();
196 pointerProperties[0].id = 0;
197 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
198
199 pointerCoords[0].clear();
200 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
201 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
202
203 const nsecs_t currentTime = now();
204
205 MotionEvent event;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800206 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
207 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
208 /* actionButton */ 0, /* flags */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700209 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600210 1 /* xScale */, 1 /* yScale */,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700211 /* xOffset */ 0, /* yOffset */ 0, /* xPrecision */ 0,
212 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
213 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, currentTime,
214 /*pointerCount*/ 1, pointerProperties, pointerCoords);
215 return event;
216}
217
218static NotifyMotionArgs generateMotionArgs() {
219 PointerProperties pointerProperties[1];
220 PointerCoords pointerCoords[1];
221
222 pointerProperties[0].clear();
223 pointerProperties[0].id = 0;
224 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
225
226 pointerCoords[0].clear();
227 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
228 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
229
230 const nsecs_t currentTime = now();
231 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800232 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700233 ADISPLAY_ID_DEFAULT, POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
234 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
235 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
236 pointerProperties, pointerCoords,
237 /* xPrecision */ 0, /* yPrecision */ 0,
238 AMOTION_EVENT_INVALID_CURSOR_POSITION,
239 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
240
241 return args;
242}
243
244static void benchmarkNotifyMotion(benchmark::State& state) {
245 // Create dispatcher
246 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
247 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
248 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
249 dispatcher->start();
250
251 // Create a window that will receive motion events
252 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
253 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
254
Arthur Hung72d8dc32020-03-28 00:48:39 +0000255 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700256
257 NotifyMotionArgs motionArgs = generateMotionArgs();
258
259 for (auto _ : state) {
260 // Send ACTION_DOWN
261 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800262 motionArgs.id = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700263 motionArgs.downTime = now();
264 motionArgs.eventTime = motionArgs.downTime;
265 dispatcher->notifyMotion(&motionArgs);
266
267 // Send ACTION_UP
268 motionArgs.action = AMOTION_EVENT_ACTION_UP;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800269 motionArgs.id = 1;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700270 motionArgs.eventTime = now();
271 dispatcher->notifyMotion(&motionArgs);
272
273 window->consumeEvent();
274 window->consumeEvent();
275 }
276
277 dispatcher->stop();
278}
279
280static void benchmarkInjectMotion(benchmark::State& state) {
281 // Create dispatcher
282 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
283 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
284 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
285 dispatcher->start();
286
287 // Create a window that will receive motion events
288 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
289 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
290
Arthur Hung72d8dc32020-03-28 00:48:39 +0000291 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700292
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700293 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800294 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700295 // Send ACTION_DOWN
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700296 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
297 INPUT_EVENT_INJECTION_SYNC_NONE, INJECT_EVENT_TIMEOUT,
298 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
299
300 // Send ACTION_UP
301 event.setAction(AMOTION_EVENT_ACTION_UP);
302 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
303 INPUT_EVENT_INJECTION_SYNC_NONE, INJECT_EVENT_TIMEOUT,
304 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
305
306 window->consumeEvent();
307 window->consumeEvent();
308 }
309
310 dispatcher->stop();
311}
312
313BENCHMARK(benchmarkNotifyMotion);
314BENCHMARK(benchmarkInjectMotion);
315
316} // namespace android::inputdispatcher
317
318BENCHMARK_MAIN();