blob: bc77b8aef42fa9932363e352a9b4c16039172fb0 [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
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000019#include <android/os/IInputConstants.h>
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070020#include <binder/Binder.h>
21#include "../dispatcher/InputDispatcher.h"
22
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000023using android::os::IInputConstants;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080024using android::os::InputEventInjectionResult;
25using android::os::InputEventInjectionSync;
26
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070027namespace android::inputdispatcher {
28
29// An arbitrary device id.
30static const int32_t DEVICE_ID = 1;
31
32// An arbitrary injector pid / uid pair that has permission to inject events.
33static const int32_t INJECTOR_PID = 999;
34static const int32_t INJECTOR_UID = 1001;
35
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -070036static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 5s;
37static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 100ms;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070038
39static nsecs_t now() {
40 return systemTime(SYSTEM_TIME_MONOTONIC);
41}
42
43// --- FakeInputDispatcherPolicy ---
44
45class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
46public:
47 FakeInputDispatcherPolicy() {}
48
49protected:
50 virtual ~FakeInputDispatcherPolicy() {}
51
52private:
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050053 void notifyConfigurationChanged(nsecs_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070054
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050055 void notifyNoFocusedWindowAnr(
56 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
57 ALOGE("There is no focused window for %s", applicationHandle->getName().c_str());
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070058 }
59
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +000060 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken,
61 const std::string& reason) override {
62 ALOGE("Window is not responding: %s", reason.c_str());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050063 }
64
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +000065 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {}
66
67 void notifyMonitorUnresponsive(int32_t pid, const std::string& reason) override {
68 ALOGE("Monitor is not responding: %s", reason.c_str());
69 }
70
71 void notifyMonitorResponsive(int32_t pid) override {}
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050072
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050073 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070074
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050075 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070076
Chris Yef59a2f42020-10-16 12:55:26 -070077 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
78 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
79 const std::vector<float>& values) override {}
80
81 void notifySensorAccuracy(int32_t deviceId, InputDeviceSensorType sensorType,
82 InputDeviceSensorAccuracy accuracy) override {}
83
Chris Yefb552902021-02-03 17:18:37 -080084 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
85
Bernardo Rufino2e1f6512020-10-08 13:42:07 +000086 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
87
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050088 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070089 *outConfig = mConfig;
90 }
91
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050092 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070093 return true;
94 }
95
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050096 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070097
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050098 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070099
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500100 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700101 return 0;
102 }
103
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500104 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700105 return false;
106 }
107
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500108 void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700109
Sean Stoutb4e0a592021-02-23 07:34:53 -0800110 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700111
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500112 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700113
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500114 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700115
Prabir Pradhan99987712020-11-10 18:43:05 -0800116 void setPointerCapture(bool enabled) override {}
117
arthurhungf452d0b2021-01-06 00:19:52 +0800118 void notifyDropWindow(const sp<IBinder>&, float x, float y) override {}
119
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700120 InputDispatcherConfiguration mConfig;
121};
122
123class FakeApplicationHandle : public InputApplicationHandle {
124public:
125 FakeApplicationHandle() {}
126 virtual ~FakeApplicationHandle() {}
127
128 virtual bool updateInfo() {
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500129 mInfo.dispatchingTimeoutMillis =
130 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700131 return true;
132 }
133};
134
135class FakeInputReceiver {
136public:
137 void consumeEvent() {
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500138 uint32_t consumeSeq = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700139 InputEvent* event;
140
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800141 std::chrono::time_point start = std::chrono::steady_clock::now();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700142 status_t result = WOULD_BLOCK;
143 while (result == WOULD_BLOCK) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800144 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
145 if (elapsed > 10ms) {
146 ALOGE("Waited too long for consumer to produce an event, giving up");
147 break;
148 }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700149 result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
150 &event);
151 }
152 if (result != OK) {
153 ALOGE("Received result = %d from consume()", result);
154 }
155 result = mConsumer->sendFinishedSignal(consumeSeq, true);
156 if (result != OK) {
157 ALOGE("Received result = %d from sendFinishedSignal", result);
158 }
159 }
160
161protected:
162 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, const std::string name)
163 : mDispatcher(dispatcher) {
Garfield Tan15601662020-09-22 15:32:38 -0700164 mClientChannel = *mDispatcher->createInputChannel(name);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700165 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
166 }
167
168 virtual ~FakeInputReceiver() {}
169
170 sp<InputDispatcher> mDispatcher;
Garfield Tan15601662020-09-22 15:32:38 -0700171 std::shared_ptr<InputChannel> mClientChannel;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700172 std::unique_ptr<InputConsumer> mConsumer;
173 PreallocatedInputEventFactory mEventFactory;
174};
175
176class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver {
177public:
178 static const int32_t WIDTH = 200;
179 static const int32_t HEIGHT = 200;
180
Chris Yea209fde2020-07-22 13:54:51 -0700181 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700182 const sp<InputDispatcher>& dispatcher, const std::string name)
183 : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700184 inputApplicationHandle->updateInfo();
185 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
186 }
187
188 virtual bool updateInfo() override {
Garfield Tan15601662020-09-22 15:32:38 -0700189 mInfo.token = mClientChannel->getConnectionToken();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700190 mInfo.name = "FakeWindowHandle";
Michael Wright44753b12020-07-08 13:48:11 +0100191 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500192 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700193 mInfo.frameLeft = mFrame.left;
194 mInfo.frameTop = mFrame.top;
195 mInfo.frameRight = mFrame.right;
196 mInfo.frameBottom = mFrame.bottom;
197 mInfo.globalScaleFactor = 1.0;
198 mInfo.touchableRegion.clear();
199 mInfo.addTouchableRegion(mFrame);
200 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700201 mInfo.focusable = true;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700202 mInfo.hasWallpaper = false;
203 mInfo.paused = false;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700204 mInfo.ownerPid = INJECTOR_PID;
205 mInfo.ownerUid = INJECTOR_UID;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700206 mInfo.displayId = ADISPLAY_ID_DEFAULT;
207
208 return true;
209 }
210
211protected:
212 Rect mFrame;
213};
214
215static MotionEvent generateMotionEvent() {
216 PointerProperties pointerProperties[1];
217 PointerCoords pointerCoords[1];
218
219 pointerProperties[0].clear();
220 pointerProperties[0].id = 0;
221 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
222
223 pointerCoords[0].clear();
224 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
225 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
226
227 const nsecs_t currentTime = now();
228
chaviw9eaa22c2020-07-01 16:21:27 -0700229 ui::Transform identityTransform;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700230 MotionEvent event;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000231 event.initialize(IInputConstants::INVALID_INPUT_EVENT_ID, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Garfield Tanfbe732e2020-01-24 11:26:14 -0800232 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
233 /* actionButton */ 0, /* flags */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700234 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviw9eaa22c2020-07-01 16:21:27 -0700235 identityTransform, /* xPrecision */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700236 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Evan Rosky84f07f02021-04-16 10:42:42 -0700237 AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_DISPLAY_SIZE,
238 AMOTION_EVENT_INVALID_DISPLAY_SIZE, currentTime, currentTime,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700239 /*pointerCount*/ 1, pointerProperties, pointerCoords);
240 return event;
241}
242
243static NotifyMotionArgs generateMotionArgs() {
244 PointerProperties pointerProperties[1];
245 PointerCoords pointerCoords[1];
246
247 pointerProperties[0].clear();
248 pointerProperties[0].id = 0;
249 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
250
251 pointerCoords[0].clear();
252 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
253 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
254
255 const nsecs_t currentTime = now();
256 // Define a valid motion event.
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000257 NotifyMotionArgs args(IInputConstants::INVALID_INPUT_EVENT_ID, currentTime, currentTime,
258 DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
259 POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700260 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
261 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
262 pointerProperties, pointerCoords,
263 /* xPrecision */ 0, /* yPrecision */ 0,
264 AMOTION_EVENT_INVALID_CURSOR_POSITION,
265 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
266
267 return args;
268}
269
270static void benchmarkNotifyMotion(benchmark::State& state) {
271 // Create dispatcher
272 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
273 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
274 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
275 dispatcher->start();
276
277 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700278 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700279 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
280
Arthur Hung72d8dc32020-03-28 00:48:39 +0000281 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700282
283 NotifyMotionArgs motionArgs = generateMotionArgs();
284
285 for (auto _ : state) {
286 // Send ACTION_DOWN
287 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700288 motionArgs.downTime = now();
289 motionArgs.eventTime = motionArgs.downTime;
290 dispatcher->notifyMotion(&motionArgs);
291
292 // Send ACTION_UP
293 motionArgs.action = AMOTION_EVENT_ACTION_UP;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700294 motionArgs.eventTime = now();
295 dispatcher->notifyMotion(&motionArgs);
296
297 window->consumeEvent();
298 window->consumeEvent();
299 }
300
301 dispatcher->stop();
302}
303
304static void benchmarkInjectMotion(benchmark::State& state) {
305 // Create dispatcher
306 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
307 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
308 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
309 dispatcher->start();
310
311 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700312 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700313 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
314
Arthur Hung72d8dc32020-03-28 00:48:39 +0000315 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700316
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700317 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800318 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700319 // Send ACTION_DOWN
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700320 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 // Send ACTION_UP
325 event.setAction(AMOTION_EVENT_ACTION_UP);
326 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800327 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700328 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
329
330 window->consumeEvent();
331 window->consumeEvent();
332 }
333
334 dispatcher->stop();
335}
336
337BENCHMARK(benchmarkNotifyMotion);
338BENCHMARK(benchmarkInjectMotion);
339
340} // namespace android::inputdispatcher
341
342BENCHMARK_MAIN();