blob: b31980b87f3a45a3ae08fded0e6fdf0f265e62e7 [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
Chris Yea209fde2020-07-22 13:54:51 -070050 std::chrono::nanoseconds notifyAnr(const std::shared_ptr<InputApplicationHandle>&,
51 const sp<IBinder>&, 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 Vishniakou70622952020-07-30 11:17:23 -050097 mInfo.dispatchingTimeoutMillis =
98 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070099 return true;
100 }
101};
102
103class FakeInputReceiver {
104public:
105 void consumeEvent() {
106 uint32_t consumeSeq;
107 InputEvent* event;
108
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800109 std::chrono::time_point start = std::chrono::steady_clock::now();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700110 status_t result = WOULD_BLOCK;
111 while (result == WOULD_BLOCK) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800112 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
113 if (elapsed > 10ms) {
114 ALOGE("Waited too long for consumer to produce an event, giving up");
115 break;
116 }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700117 result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
118 &event);
119 }
120 if (result != OK) {
121 ALOGE("Received result = %d from consume()", result);
122 }
123 result = mConsumer->sendFinishedSignal(consumeSeq, true);
124 if (result != OK) {
125 ALOGE("Received result = %d from sendFinishedSignal", result);
126 }
127 }
128
129protected:
130 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, const std::string name)
131 : mDispatcher(dispatcher) {
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500132 std::unique_ptr<InputChannel> serverChannel, clientChannel;
133 InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
134 mServerChannel = std::move(serverChannel);
135 mClientChannel = std::move(clientChannel);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700136 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
137 }
138
139 virtual ~FakeInputReceiver() {}
140
141 sp<InputDispatcher> mDispatcher;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500142 std::shared_ptr<InputChannel> mServerChannel, 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)) {
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";
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;
174 mInfo.canReceiveKeys = true;
175 mInfo.hasFocus = true;
176 mInfo.hasWallpaper = false;
177 mInfo.paused = false;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700178 mInfo.ownerPid = INJECTOR_PID;
179 mInfo.ownerUid = INJECTOR_UID;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700180 mInfo.displayId = ADISPLAY_ID_DEFAULT;
181
182 return true;
183 }
184
185protected:
186 Rect mFrame;
187};
188
189static MotionEvent generateMotionEvent() {
190 PointerProperties pointerProperties[1];
191 PointerCoords pointerCoords[1];
192
193 pointerProperties[0].clear();
194 pointerProperties[0].id = 0;
195 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
196
197 pointerCoords[0].clear();
198 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
199 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
200
201 const nsecs_t currentTime = now();
202
chaviw9eaa22c2020-07-01 16:21:27 -0700203 ui::Transform identityTransform;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700204 MotionEvent event;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800205 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
206 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
207 /* actionButton */ 0, /* flags */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700208 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviw9eaa22c2020-07-01 16:21:27 -0700209 identityTransform, /* xPrecision */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700210 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
211 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, currentTime,
212 /*pointerCount*/ 1, pointerProperties, pointerCoords);
213 return event;
214}
215
216static NotifyMotionArgs generateMotionArgs() {
217 PointerProperties pointerProperties[1];
218 PointerCoords pointerCoords[1];
219
220 pointerProperties[0].clear();
221 pointerProperties[0].id = 0;
222 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
223
224 pointerCoords[0].clear();
225 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
226 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
227
228 const nsecs_t currentTime = now();
229 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800230 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700231 ADISPLAY_ID_DEFAULT, POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
232 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
233 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
234 pointerProperties, pointerCoords,
235 /* xPrecision */ 0, /* yPrecision */ 0,
236 AMOTION_EVENT_INVALID_CURSOR_POSITION,
237 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
238
239 return args;
240}
241
242static void benchmarkNotifyMotion(benchmark::State& state) {
243 // Create dispatcher
244 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
245 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
246 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
247 dispatcher->start();
248
249 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700250 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700251 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
252
Arthur Hung72d8dc32020-03-28 00:48:39 +0000253 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700254
255 NotifyMotionArgs motionArgs = generateMotionArgs();
256
257 for (auto _ : state) {
258 // Send ACTION_DOWN
259 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800260 motionArgs.id = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700261 motionArgs.downTime = now();
262 motionArgs.eventTime = motionArgs.downTime;
263 dispatcher->notifyMotion(&motionArgs);
264
265 // Send ACTION_UP
266 motionArgs.action = AMOTION_EVENT_ACTION_UP;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800267 motionArgs.id = 1;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700268 motionArgs.eventTime = now();
269 dispatcher->notifyMotion(&motionArgs);
270
271 window->consumeEvent();
272 window->consumeEvent();
273 }
274
275 dispatcher->stop();
276}
277
278static void benchmarkInjectMotion(benchmark::State& state) {
279 // Create dispatcher
280 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
281 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
282 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
283 dispatcher->start();
284
285 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700286 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700287 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
288
Arthur Hung72d8dc32020-03-28 00:48:39 +0000289 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700290
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700291 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800292 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700293 // Send ACTION_DOWN
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700294 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
295 INPUT_EVENT_INJECTION_SYNC_NONE, INJECT_EVENT_TIMEOUT,
296 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
297
298 // Send ACTION_UP
299 event.setAction(AMOTION_EVENT_ACTION_UP);
300 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
301 INPUT_EVENT_INJECTION_SYNC_NONE, INJECT_EVENT_TIMEOUT,
302 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
303
304 window->consumeEvent();
305 window->consumeEvent();
306 }
307
308 dispatcher->stop();
309}
310
311BENCHMARK(benchmarkNotifyMotion);
312BENCHMARK(benchmarkInjectMotion);
313
314} // namespace android::inputdispatcher
315
316BENCHMARK_MAIN();