blob: 7c572b468276a6bf3e1ad55563c831ce8b3b67d7 [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
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050053 void notifyNoFocusedWindowAnr(
54 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
55 ALOGE("There is no focused window for %s", applicationHandle->getName().c_str());
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070056 }
57
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050058 void notifyConnectionUnresponsive(const sp<IBinder>& connectionToken,
59 const std::string& reason) override {
60 ALOGE("Connection is not responding: %s", reason.c_str());
61 }
62
63 void notifyConnectionResponsive(const sp<IBinder>& connectionToken) override {}
64
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050065 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070066
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050067 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070068
Bernardo Rufino2e1f6512020-10-08 13:42:07 +000069 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
70
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050071 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070072 *outConfig = mConfig;
73 }
74
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050075 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070076 return true;
77 }
78
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050079 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070080
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050081 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070082
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050083 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070084 return 0;
85 }
86
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050087 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070088 return false;
89 }
90
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050091 void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070092
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050093 void pokeUserActivity(nsecs_t, int32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070094
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050095 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070096
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050097 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070098
Prabir Pradhan99987712020-11-10 18:43:05 -080099 void setPointerCapture(bool enabled) override {}
100
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700101 InputDispatcherConfiguration mConfig;
102};
103
104class FakeApplicationHandle : public InputApplicationHandle {
105public:
106 FakeApplicationHandle() {}
107 virtual ~FakeApplicationHandle() {}
108
109 virtual bool updateInfo() {
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500110 mInfo.dispatchingTimeoutMillis =
111 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700112 return true;
113 }
114};
115
116class FakeInputReceiver {
117public:
118 void consumeEvent() {
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500119 uint32_t consumeSeq = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700120 InputEvent* event;
121
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800122 std::chrono::time_point start = std::chrono::steady_clock::now();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700123 status_t result = WOULD_BLOCK;
124 while (result == WOULD_BLOCK) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800125 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
126 if (elapsed > 10ms) {
127 ALOGE("Waited too long for consumer to produce an event, giving up");
128 break;
129 }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700130 result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
131 &event);
132 }
133 if (result != OK) {
134 ALOGE("Received result = %d from consume()", result);
135 }
136 result = mConsumer->sendFinishedSignal(consumeSeq, true);
137 if (result != OK) {
138 ALOGE("Received result = %d from sendFinishedSignal", result);
139 }
140 }
141
142protected:
143 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, const std::string name)
144 : mDispatcher(dispatcher) {
Garfield Tan15601662020-09-22 15:32:38 -0700145 mClientChannel = *mDispatcher->createInputChannel(name);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700146 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
147 }
148
149 virtual ~FakeInputReceiver() {}
150
151 sp<InputDispatcher> mDispatcher;
Garfield Tan15601662020-09-22 15:32:38 -0700152 std::shared_ptr<InputChannel> mClientChannel;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700153 std::unique_ptr<InputConsumer> mConsumer;
154 PreallocatedInputEventFactory mEventFactory;
155};
156
157class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver {
158public:
159 static const int32_t WIDTH = 200;
160 static const int32_t HEIGHT = 200;
161
Chris Yea209fde2020-07-22 13:54:51 -0700162 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700163 const sp<InputDispatcher>& dispatcher, const std::string name)
164 : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700165 inputApplicationHandle->updateInfo();
166 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
167 }
168
169 virtual bool updateInfo() override {
Garfield Tan15601662020-09-22 15:32:38 -0700170 mInfo.token = mClientChannel->getConnectionToken();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700171 mInfo.name = "FakeWindowHandle";
Michael Wright44753b12020-07-08 13:48:11 +0100172 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500173 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700174 mInfo.frameLeft = mFrame.left;
175 mInfo.frameTop = mFrame.top;
176 mInfo.frameRight = mFrame.right;
177 mInfo.frameBottom = mFrame.bottom;
178 mInfo.globalScaleFactor = 1.0;
179 mInfo.touchableRegion.clear();
180 mInfo.addTouchableRegion(mFrame);
181 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700182 mInfo.focusable = true;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700183 mInfo.hasWallpaper = false;
184 mInfo.paused = false;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700185 mInfo.ownerPid = INJECTOR_PID;
186 mInfo.ownerUid = INJECTOR_UID;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700187 mInfo.displayId = ADISPLAY_ID_DEFAULT;
188
189 return true;
190 }
191
192protected:
193 Rect mFrame;
194};
195
196static MotionEvent generateMotionEvent() {
197 PointerProperties pointerProperties[1];
198 PointerCoords pointerCoords[1];
199
200 pointerProperties[0].clear();
201 pointerProperties[0].id = 0;
202 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
203
204 pointerCoords[0].clear();
205 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
206 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
207
208 const nsecs_t currentTime = now();
209
chaviw9eaa22c2020-07-01 16:21:27 -0700210 ui::Transform identityTransform;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700211 MotionEvent event;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800212 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
213 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
214 /* actionButton */ 0, /* flags */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700215 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviw9eaa22c2020-07-01 16:21:27 -0700216 identityTransform, /* xPrecision */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700217 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
218 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, currentTime,
219 /*pointerCount*/ 1, pointerProperties, pointerCoords);
220 return event;
221}
222
223static NotifyMotionArgs generateMotionArgs() {
224 PointerProperties pointerProperties[1];
225 PointerCoords pointerCoords[1];
226
227 pointerProperties[0].clear();
228 pointerProperties[0].id = 0;
229 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
230
231 pointerCoords[0].clear();
232 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
233 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
234
235 const nsecs_t currentTime = now();
236 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800237 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700238 ADISPLAY_ID_DEFAULT, POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
239 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
240 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
241 pointerProperties, pointerCoords,
242 /* xPrecision */ 0, /* yPrecision */ 0,
243 AMOTION_EVENT_INVALID_CURSOR_POSITION,
244 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
245
246 return args;
247}
248
249static void benchmarkNotifyMotion(benchmark::State& state) {
250 // Create dispatcher
251 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
252 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
253 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
254 dispatcher->start();
255
256 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700257 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700258 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
259
Arthur Hung72d8dc32020-03-28 00:48:39 +0000260 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700261
262 NotifyMotionArgs motionArgs = generateMotionArgs();
263
264 for (auto _ : state) {
265 // Send ACTION_DOWN
266 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800267 motionArgs.id = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700268 motionArgs.downTime = now();
269 motionArgs.eventTime = motionArgs.downTime;
270 dispatcher->notifyMotion(&motionArgs);
271
272 // Send ACTION_UP
273 motionArgs.action = AMOTION_EVENT_ACTION_UP;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800274 motionArgs.id = 1;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700275 motionArgs.eventTime = now();
276 dispatcher->notifyMotion(&motionArgs);
277
278 window->consumeEvent();
279 window->consumeEvent();
280 }
281
282 dispatcher->stop();
283}
284
285static void benchmarkInjectMotion(benchmark::State& state) {
286 // Create dispatcher
287 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
288 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
289 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
290 dispatcher->start();
291
292 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700293 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700294 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
295
Arthur Hung72d8dc32020-03-28 00:48:39 +0000296 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700297
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700298 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800299 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700300 // Send ACTION_DOWN
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700301 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800302 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700303 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
304
305 // Send ACTION_UP
306 event.setAction(AMOTION_EVENT_ACTION_UP);
307 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800308 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700309 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
310
311 window->consumeEvent();
312 window->consumeEvent();
313 }
314
315 dispatcher->stop();
316}
317
318BENCHMARK(benchmarkNotifyMotion);
319BENCHMARK(benchmarkInjectMotion);
320
321} // namespace android::inputdispatcher
322
323BENCHMARK_MAIN();