blob: 887bdd4edc3ac695c0c65b8df469dd740b2ed71a [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 Vishniakou3c63fa42020-12-15 02:59:54 +000058 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken,
59 const std::string& reason) override {
60 ALOGE("Window is not responding: %s", reason.c_str());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050061 }
62
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +000063 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {}
64
65 void notifyMonitorUnresponsive(int32_t pid, const std::string& reason) override {
66 ALOGE("Monitor is not responding: %s", reason.c_str());
67 }
68
69 void notifyMonitorResponsive(int32_t pid) override {}
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050070
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050071 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070072
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050073 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070074
Chris Yef59a2f42020-10-16 12:55:26 -070075 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
76 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
77 const std::vector<float>& values) override {}
78
79 void notifySensorAccuracy(int32_t deviceId, InputDeviceSensorType sensorType,
80 InputDeviceSensorAccuracy accuracy) override {}
81
Bernardo Rufino2e1f6512020-10-08 13:42:07 +000082 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
83
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050084 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070085 *outConfig = mConfig;
86 }
87
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050088 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070089 return true;
90 }
91
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050092 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070093
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050094 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070095
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050096 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070097 return 0;
98 }
99
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500100 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700101 return false;
102 }
103
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500104 void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700105
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500106 void pokeUserActivity(nsecs_t, int32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700107
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500108 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700109
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500110 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700111
Prabir Pradhan99987712020-11-10 18:43:05 -0800112 void setPointerCapture(bool enabled) override {}
113
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700114 InputDispatcherConfiguration mConfig;
115};
116
117class FakeApplicationHandle : public InputApplicationHandle {
118public:
119 FakeApplicationHandle() {}
120 virtual ~FakeApplicationHandle() {}
121
122 virtual bool updateInfo() {
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500123 mInfo.dispatchingTimeoutMillis =
124 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700125 return true;
126 }
127};
128
129class FakeInputReceiver {
130public:
131 void consumeEvent() {
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500132 uint32_t consumeSeq = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700133 InputEvent* event;
134
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800135 std::chrono::time_point start = std::chrono::steady_clock::now();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700136 status_t result = WOULD_BLOCK;
137 while (result == WOULD_BLOCK) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800138 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
139 if (elapsed > 10ms) {
140 ALOGE("Waited too long for consumer to produce an event, giving up");
141 break;
142 }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700143 result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
144 &event);
145 }
146 if (result != OK) {
147 ALOGE("Received result = %d from consume()", result);
148 }
149 result = mConsumer->sendFinishedSignal(consumeSeq, true);
150 if (result != OK) {
151 ALOGE("Received result = %d from sendFinishedSignal", result);
152 }
153 }
154
155protected:
156 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, const std::string name)
157 : mDispatcher(dispatcher) {
Garfield Tan15601662020-09-22 15:32:38 -0700158 mClientChannel = *mDispatcher->createInputChannel(name);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700159 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
160 }
161
162 virtual ~FakeInputReceiver() {}
163
164 sp<InputDispatcher> mDispatcher;
Garfield Tan15601662020-09-22 15:32:38 -0700165 std::shared_ptr<InputChannel> mClientChannel;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700166 std::unique_ptr<InputConsumer> mConsumer;
167 PreallocatedInputEventFactory mEventFactory;
168};
169
170class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver {
171public:
172 static const int32_t WIDTH = 200;
173 static const int32_t HEIGHT = 200;
174
Chris Yea209fde2020-07-22 13:54:51 -0700175 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700176 const sp<InputDispatcher>& dispatcher, const std::string name)
177 : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700178 inputApplicationHandle->updateInfo();
179 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
180 }
181
182 virtual bool updateInfo() override {
Garfield Tan15601662020-09-22 15:32:38 -0700183 mInfo.token = mClientChannel->getConnectionToken();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700184 mInfo.name = "FakeWindowHandle";
Michael Wright44753b12020-07-08 13:48:11 +0100185 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500186 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700187 mInfo.frameLeft = mFrame.left;
188 mInfo.frameTop = mFrame.top;
189 mInfo.frameRight = mFrame.right;
190 mInfo.frameBottom = mFrame.bottom;
191 mInfo.globalScaleFactor = 1.0;
192 mInfo.touchableRegion.clear();
193 mInfo.addTouchableRegion(mFrame);
194 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700195 mInfo.focusable = true;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700196 mInfo.hasWallpaper = false;
197 mInfo.paused = false;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700198 mInfo.ownerPid = INJECTOR_PID;
199 mInfo.ownerUid = INJECTOR_UID;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700200 mInfo.displayId = ADISPLAY_ID_DEFAULT;
201
202 return true;
203 }
204
205protected:
206 Rect mFrame;
207};
208
209static MotionEvent generateMotionEvent() {
210 PointerProperties pointerProperties[1];
211 PointerCoords pointerCoords[1];
212
213 pointerProperties[0].clear();
214 pointerProperties[0].id = 0;
215 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
216
217 pointerCoords[0].clear();
218 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
219 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
220
221 const nsecs_t currentTime = now();
222
chaviw9eaa22c2020-07-01 16:21:27 -0700223 ui::Transform identityTransform;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700224 MotionEvent event;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800225 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
226 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
227 /* actionButton */ 0, /* flags */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700228 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviw9eaa22c2020-07-01 16:21:27 -0700229 identityTransform, /* xPrecision */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700230 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
231 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, currentTime,
232 /*pointerCount*/ 1, pointerProperties, pointerCoords);
233 return event;
234}
235
236static NotifyMotionArgs generateMotionArgs() {
237 PointerProperties pointerProperties[1];
238 PointerCoords pointerCoords[1];
239
240 pointerProperties[0].clear();
241 pointerProperties[0].id = 0;
242 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
243
244 pointerCoords[0].clear();
245 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
246 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
247
248 const nsecs_t currentTime = now();
249 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800250 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700251 ADISPLAY_ID_DEFAULT, POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
252 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
253 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
254 pointerProperties, pointerCoords,
255 /* xPrecision */ 0, /* yPrecision */ 0,
256 AMOTION_EVENT_INVALID_CURSOR_POSITION,
257 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
258
259 return args;
260}
261
262static void benchmarkNotifyMotion(benchmark::State& state) {
263 // Create dispatcher
264 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
265 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
266 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
267 dispatcher->start();
268
269 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700270 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700271 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
272
Arthur Hung72d8dc32020-03-28 00:48:39 +0000273 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700274
275 NotifyMotionArgs motionArgs = generateMotionArgs();
276
277 for (auto _ : state) {
278 // Send ACTION_DOWN
279 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800280 motionArgs.id = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700281 motionArgs.downTime = now();
282 motionArgs.eventTime = motionArgs.downTime;
283 dispatcher->notifyMotion(&motionArgs);
284
285 // Send ACTION_UP
286 motionArgs.action = AMOTION_EVENT_ACTION_UP;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800287 motionArgs.id = 1;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700288 motionArgs.eventTime = now();
289 dispatcher->notifyMotion(&motionArgs);
290
291 window->consumeEvent();
292 window->consumeEvent();
293 }
294
295 dispatcher->stop();
296}
297
298static void benchmarkInjectMotion(benchmark::State& state) {
299 // Create dispatcher
300 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
301 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
302 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
303 dispatcher->start();
304
305 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700306 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700307 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
308
Arthur Hung72d8dc32020-03-28 00:48:39 +0000309 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700310
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700311 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800312 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700313 // Send ACTION_DOWN
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700314 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 // Send ACTION_UP
319 event.setAction(AMOTION_EVENT_ACTION_UP);
320 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800321 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700322 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
323
324 window->consumeEvent();
325 window->consumeEvent();
326 }
327
328 dispatcher->stop();
329}
330
331BENCHMARK(benchmarkNotifyMotion);
332BENCHMARK(benchmarkInjectMotion);
333
334} // namespace android::inputdispatcher
335
336BENCHMARK_MAIN();