blob: 4e55872bc69cabf4770e2dcdb8d95e8a6aba812f [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
Chris Yef59a2f42020-10-16 12:55:26 -070069 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
70 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
71 const std::vector<float>& values) override {}
72
73 void notifySensorAccuracy(int32_t deviceId, InputDeviceSensorType sensorType,
74 InputDeviceSensorAccuracy accuracy) override {}
75
Bernardo Rufino2e1f6512020-10-08 13:42:07 +000076 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
77
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050078 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070079 *outConfig = mConfig;
80 }
81
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050082 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070083 return true;
84 }
85
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050086 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070087
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050088 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070089
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050090 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070091 return 0;
92 }
93
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050094 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070095 return false;
96 }
97
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050098 void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070099
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500100 void pokeUserActivity(nsecs_t, int32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700101
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500102 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700103
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500104 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700105
Prabir Pradhan99987712020-11-10 18:43:05 -0800106 void setPointerCapture(bool enabled) override {}
107
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700108 InputDispatcherConfiguration mConfig;
109};
110
111class FakeApplicationHandle : public InputApplicationHandle {
112public:
113 FakeApplicationHandle() {}
114 virtual ~FakeApplicationHandle() {}
115
116 virtual bool updateInfo() {
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500117 mInfo.dispatchingTimeoutMillis =
118 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700119 return true;
120 }
121};
122
123class FakeInputReceiver {
124public:
125 void consumeEvent() {
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500126 uint32_t consumeSeq = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700127 InputEvent* event;
128
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800129 std::chrono::time_point start = std::chrono::steady_clock::now();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700130 status_t result = WOULD_BLOCK;
131 while (result == WOULD_BLOCK) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800132 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
133 if (elapsed > 10ms) {
134 ALOGE("Waited too long for consumer to produce an event, giving up");
135 break;
136 }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700137 result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
138 &event);
139 }
140 if (result != OK) {
141 ALOGE("Received result = %d from consume()", result);
142 }
143 result = mConsumer->sendFinishedSignal(consumeSeq, true);
144 if (result != OK) {
145 ALOGE("Received result = %d from sendFinishedSignal", result);
146 }
147 }
148
149protected:
150 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, const std::string name)
151 : mDispatcher(dispatcher) {
Garfield Tan15601662020-09-22 15:32:38 -0700152 mClientChannel = *mDispatcher->createInputChannel(name);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700153 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
154 }
155
156 virtual ~FakeInputReceiver() {}
157
158 sp<InputDispatcher> mDispatcher;
Garfield Tan15601662020-09-22 15:32:38 -0700159 std::shared_ptr<InputChannel> mClientChannel;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700160 std::unique_ptr<InputConsumer> mConsumer;
161 PreallocatedInputEventFactory mEventFactory;
162};
163
164class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver {
165public:
166 static const int32_t WIDTH = 200;
167 static const int32_t HEIGHT = 200;
168
Chris Yea209fde2020-07-22 13:54:51 -0700169 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700170 const sp<InputDispatcher>& dispatcher, const std::string name)
171 : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700172 inputApplicationHandle->updateInfo();
173 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
174 }
175
176 virtual bool updateInfo() override {
Garfield Tan15601662020-09-22 15:32:38 -0700177 mInfo.token = mClientChannel->getConnectionToken();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700178 mInfo.name = "FakeWindowHandle";
Michael Wright44753b12020-07-08 13:48:11 +0100179 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500180 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700181 mInfo.frameLeft = mFrame.left;
182 mInfo.frameTop = mFrame.top;
183 mInfo.frameRight = mFrame.right;
184 mInfo.frameBottom = mFrame.bottom;
185 mInfo.globalScaleFactor = 1.0;
186 mInfo.touchableRegion.clear();
187 mInfo.addTouchableRegion(mFrame);
188 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700189 mInfo.focusable = true;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700190 mInfo.hasWallpaper = false;
191 mInfo.paused = false;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700192 mInfo.ownerPid = INJECTOR_PID;
193 mInfo.ownerUid = INJECTOR_UID;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700194 mInfo.displayId = ADISPLAY_ID_DEFAULT;
195
196 return true;
197 }
198
199protected:
200 Rect mFrame;
201};
202
203static MotionEvent generateMotionEvent() {
204 PointerProperties pointerProperties[1];
205 PointerCoords pointerCoords[1];
206
207 pointerProperties[0].clear();
208 pointerProperties[0].id = 0;
209 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
210
211 pointerCoords[0].clear();
212 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
213 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
214
215 const nsecs_t currentTime = now();
216
chaviw9eaa22c2020-07-01 16:21:27 -0700217 ui::Transform identityTransform;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700218 MotionEvent event;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800219 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
220 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
221 /* actionButton */ 0, /* flags */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700222 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviw9eaa22c2020-07-01 16:21:27 -0700223 identityTransform, /* xPrecision */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700224 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
225 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, currentTime,
226 /*pointerCount*/ 1, pointerProperties, pointerCoords);
227 return event;
228}
229
230static NotifyMotionArgs generateMotionArgs() {
231 PointerProperties pointerProperties[1];
232 PointerCoords pointerCoords[1];
233
234 pointerProperties[0].clear();
235 pointerProperties[0].id = 0;
236 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
237
238 pointerCoords[0].clear();
239 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
240 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
241
242 const nsecs_t currentTime = now();
243 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800244 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700245 ADISPLAY_ID_DEFAULT, POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
246 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
247 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
248 pointerProperties, pointerCoords,
249 /* xPrecision */ 0, /* yPrecision */ 0,
250 AMOTION_EVENT_INVALID_CURSOR_POSITION,
251 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
252
253 return args;
254}
255
256static void benchmarkNotifyMotion(benchmark::State& state) {
257 // Create dispatcher
258 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
259 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
260 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
261 dispatcher->start();
262
263 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700264 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700265 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
266
Arthur Hung72d8dc32020-03-28 00:48:39 +0000267 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700268
269 NotifyMotionArgs motionArgs = generateMotionArgs();
270
271 for (auto _ : state) {
272 // Send ACTION_DOWN
273 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800274 motionArgs.id = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700275 motionArgs.downTime = now();
276 motionArgs.eventTime = motionArgs.downTime;
277 dispatcher->notifyMotion(&motionArgs);
278
279 // Send ACTION_UP
280 motionArgs.action = AMOTION_EVENT_ACTION_UP;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800281 motionArgs.id = 1;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700282 motionArgs.eventTime = now();
283 dispatcher->notifyMotion(&motionArgs);
284
285 window->consumeEvent();
286 window->consumeEvent();
287 }
288
289 dispatcher->stop();
290}
291
292static void benchmarkInjectMotion(benchmark::State& state) {
293 // Create dispatcher
294 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
295 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
296 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
297 dispatcher->start();
298
299 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700300 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700301 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
302
Arthur Hung72d8dc32020-03-28 00:48:39 +0000303 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700304
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700305 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800306 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700307 // Send ACTION_DOWN
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700308 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800309 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700310 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
311
312 // Send ACTION_UP
313 event.setAction(AMOTION_EVENT_ACTION_UP);
314 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800315 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700316 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
317
318 window->consumeEvent();
319 window->consumeEvent();
320 }
321
322 dispatcher->stop();
323}
324
325BENCHMARK(benchmarkNotifyMotion);
326BENCHMARK(benchmarkInjectMotion);
327
328} // namespace android::inputdispatcher
329
330BENCHMARK_MAIN();