blob: 7d8ab753f387ecc53da7011808424788eca1d16c [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:
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050048 void notifyConfigurationChanged(nsecs_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070049
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050050 std::chrono::nanoseconds notifyAnr(const sp<InputApplicationHandle>&, const sp<IBinder>&,
51 const std::string& name) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070052 ALOGE("The window is not responding : %s", name.c_str());
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050053 return 0s;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070054 }
55
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050056 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070057
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050058 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070059
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050060 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070061 *outConfig = mConfig;
62 }
63
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050064 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070065 return true;
66 }
67
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050068 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070069
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050070 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070071
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050072 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070073 return 0;
74 }
75
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050076 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070077 return false;
78 }
79
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050080 void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070081
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050082 void pokeUserActivity(nsecs_t, int32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070083
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050084 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070085
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050086 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070087
88 InputDispatcherConfiguration mConfig;
89};
90
91class FakeApplicationHandle : public InputApplicationHandle {
92public:
93 FakeApplicationHandle() {}
94 virtual ~FakeApplicationHandle() {}
95
96 virtual bool updateInfo() {
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050097 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070098 return true;
99 }
100};
101
102class FakeInputReceiver {
103public:
104 void consumeEvent() {
105 uint32_t consumeSeq;
106 InputEvent* event;
107
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800108 std::chrono::time_point start = std::chrono::steady_clock::now();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700109 status_t result = WOULD_BLOCK;
110 while (result == WOULD_BLOCK) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800111 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
112 if (elapsed > 10ms) {
113 ALOGE("Waited too long for consumer to produce an event, giving up");
114 break;
115 }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700116 result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
117 &event);
118 }
119 if (result != OK) {
120 ALOGE("Received result = %d from consume()", result);
121 }
122 result = mConsumer->sendFinishedSignal(consumeSeq, true);
123 if (result != OK) {
124 ALOGE("Received result = %d from sendFinishedSignal", result);
125 }
126 }
127
128protected:
129 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, const std::string name)
130 : mDispatcher(dispatcher) {
131 InputChannel::openInputChannelPair(name, mServerChannel, mClientChannel);
132 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
133 }
134
135 virtual ~FakeInputReceiver() {}
136
137 sp<InputDispatcher> mDispatcher;
138 sp<InputChannel> mServerChannel, mClientChannel;
139 std::unique_ptr<InputConsumer> mConsumer;
140 PreallocatedInputEventFactory mEventFactory;
141};
142
143class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver {
144public:
145 static const int32_t WIDTH = 200;
146 static const int32_t HEIGHT = 200;
147
148 FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle,
149 const sp<InputDispatcher>& dispatcher, const std::string name)
150 : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
151 mDispatcher->registerInputChannel(mServerChannel);
152
153 inputApplicationHandle->updateInfo();
154 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
155 }
156
157 virtual bool updateInfo() override {
158 mInfo.token = mServerChannel->getConnectionToken();
159 mInfo.name = "FakeWindowHandle";
160 mInfo.layoutParamsFlags = 0;
161 mInfo.layoutParamsType = InputWindowInfo::TYPE_APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500162 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700163 mInfo.frameLeft = mFrame.left;
164 mInfo.frameTop = mFrame.top;
165 mInfo.frameRight = mFrame.right;
166 mInfo.frameBottom = mFrame.bottom;
167 mInfo.globalScaleFactor = 1.0;
168 mInfo.touchableRegion.clear();
169 mInfo.addTouchableRegion(mFrame);
170 mInfo.visible = true;
171 mInfo.canReceiveKeys = true;
172 mInfo.hasFocus = true;
173 mInfo.hasWallpaper = false;
174 mInfo.paused = false;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700175 mInfo.ownerPid = INJECTOR_PID;
176 mInfo.ownerUid = INJECTOR_UID;
177 mInfo.inputFeatures = 0;
178 mInfo.displayId = ADISPLAY_ID_DEFAULT;
179
180 return true;
181 }
182
183protected:
184 Rect mFrame;
185};
186
187static MotionEvent generateMotionEvent() {
188 PointerProperties pointerProperties[1];
189 PointerCoords pointerCoords[1];
190
191 pointerProperties[0].clear();
192 pointerProperties[0].id = 0;
193 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
194
195 pointerCoords[0].clear();
196 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
197 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
198
199 const nsecs_t currentTime = now();
200
201 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,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600206 1 /* xScale */, 1 /* yScale */,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700207 /* xOffset */ 0, /* yOffset */ 0, /* xPrecision */ 0,
208 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
209 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, currentTime,
210 /*pointerCount*/ 1, pointerProperties, pointerCoords);
211 return event;
212}
213
214static NotifyMotionArgs generateMotionArgs() {
215 PointerProperties pointerProperties[1];
216 PointerCoords pointerCoords[1];
217
218 pointerProperties[0].clear();
219 pointerProperties[0].id = 0;
220 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
221
222 pointerCoords[0].clear();
223 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
224 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
225
226 const nsecs_t currentTime = now();
227 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800228 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700229 ADISPLAY_ID_DEFAULT, POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
230 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
231 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
232 pointerProperties, pointerCoords,
233 /* xPrecision */ 0, /* yPrecision */ 0,
234 AMOTION_EVENT_INVALID_CURSOR_POSITION,
235 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
236
237 return args;
238}
239
240static void benchmarkNotifyMotion(benchmark::State& state) {
241 // Create dispatcher
242 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
243 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
244 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
245 dispatcher->start();
246
247 // Create a window that will receive motion events
248 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
249 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
250
Arthur Hung72d8dc32020-03-28 00:48:39 +0000251 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700252
253 NotifyMotionArgs motionArgs = generateMotionArgs();
254
255 for (auto _ : state) {
256 // Send ACTION_DOWN
257 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800258 motionArgs.id = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700259 motionArgs.downTime = now();
260 motionArgs.eventTime = motionArgs.downTime;
261 dispatcher->notifyMotion(&motionArgs);
262
263 // Send ACTION_UP
264 motionArgs.action = AMOTION_EVENT_ACTION_UP;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800265 motionArgs.id = 1;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700266 motionArgs.eventTime = now();
267 dispatcher->notifyMotion(&motionArgs);
268
269 window->consumeEvent();
270 window->consumeEvent();
271 }
272
273 dispatcher->stop();
274}
275
276static void benchmarkInjectMotion(benchmark::State& state) {
277 // Create dispatcher
278 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
279 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
280 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
281 dispatcher->start();
282
283 // Create a window that will receive motion events
284 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
285 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
286
Arthur Hung72d8dc32020-03-28 00:48:39 +0000287 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700288
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700289 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800290 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700291 // Send ACTION_DOWN
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700292 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
293 INPUT_EVENT_INJECTION_SYNC_NONE, INJECT_EVENT_TIMEOUT,
294 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
295
296 // Send ACTION_UP
297 event.setAction(AMOTION_EVENT_ACTION_UP);
298 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
299 INPUT_EVENT_INJECTION_SYNC_NONE, INJECT_EVENT_TIMEOUT,
300 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
301
302 window->consumeEvent();
303 window->consumeEvent();
304 }
305
306 dispatcher->stop();
307}
308
309BENCHMARK(benchmarkNotifyMotion);
310BENCHMARK(benchmarkInjectMotion);
311
312} // namespace android::inputdispatcher
313
314BENCHMARK_MAIN();