blob: 41e9ce2e417886e4b3e4362a675ad29c53976155 [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>
chaviw3277faf2021-05-19 16:45:23 -050021#include <gui/constants.h>
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070022#include "../dispatcher/InputDispatcher.h"
23
Siarhei Vishniakou18050092021-09-01 13:32:49 -070024using android::base::Result;
chaviw3277faf2021-05-19 16:45:23 -050025using android::gui::WindowInfo;
26using android::gui::WindowInfoHandle;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000027using android::os::IInputConstants;
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -080028using android::os::InputEventInjectionResult;
29using android::os::InputEventInjectionSync;
30
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070031namespace android::inputdispatcher {
32
33// An arbitrary device id.
34static const int32_t DEVICE_ID = 1;
35
36// An arbitrary injector pid / uid pair that has permission to inject events.
37static const int32_t INJECTOR_PID = 999;
38static const int32_t INJECTOR_UID = 1001;
39
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -070040static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 5s;
41static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 100ms;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070042
43static nsecs_t now() {
44 return systemTime(SYSTEM_TIME_MONOTONIC);
45}
46
47// --- FakeInputDispatcherPolicy ---
48
49class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
50public:
51 FakeInputDispatcherPolicy() {}
52
53protected:
54 virtual ~FakeInputDispatcherPolicy() {}
55
56private:
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050057 void notifyConfigurationChanged(nsecs_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070058
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050059 void notifyNoFocusedWindowAnr(
60 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
61 ALOGE("There is no focused window for %s", applicationHandle->getName().c_str());
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070062 }
63
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +000064 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken,
65 const std::string& reason) override {
66 ALOGE("Window is not responding: %s", reason.c_str());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050067 }
68
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +000069 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {}
70
71 void notifyMonitorUnresponsive(int32_t pid, const std::string& reason) override {
72 ALOGE("Monitor is not responding: %s", reason.c_str());
73 }
74
75 void notifyMonitorResponsive(int32_t pid) override {}
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050076
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050077 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070078
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050079 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070080
Chris Yef59a2f42020-10-16 12:55:26 -070081 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
82 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
83 const std::vector<float>& values) override {}
84
85 void notifySensorAccuracy(int32_t deviceId, InputDeviceSensorType sensorType,
86 InputDeviceSensorAccuracy accuracy) override {}
87
Chris Yefb552902021-02-03 17:18:37 -080088 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
89
Bernardo Rufino2e1f6512020-10-08 13:42:07 +000090 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
91
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050092 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070093 *outConfig = mConfig;
94 }
95
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050096 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070097 return true;
98 }
99
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500100 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700101
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500102 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700103
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500104 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700105 return 0;
106 }
107
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500108 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700109 return false;
110 }
111
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500112 void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700113
Sean Stoutb4e0a592021-02-23 07:34:53 -0800114 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700115
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500116 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700117
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500118 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700119
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000120 void setPointerCapture(const PointerCaptureRequest&) override {}
Prabir Pradhan99987712020-11-10 18:43:05 -0800121
arthurhungf452d0b2021-01-06 00:19:52 +0800122 void notifyDropWindow(const sp<IBinder>&, float x, float y) override {}
123
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700124 InputDispatcherConfiguration mConfig;
125};
126
127class FakeApplicationHandle : public InputApplicationHandle {
128public:
129 FakeApplicationHandle() {}
130 virtual ~FakeApplicationHandle() {}
131
132 virtual bool updateInfo() {
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500133 mInfo.dispatchingTimeoutMillis =
134 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700135 return true;
136 }
137};
138
139class FakeInputReceiver {
140public:
141 void consumeEvent() {
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500142 uint32_t consumeSeq = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700143 InputEvent* event;
144
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800145 std::chrono::time_point start = std::chrono::steady_clock::now();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700146 status_t result = WOULD_BLOCK;
147 while (result == WOULD_BLOCK) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800148 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
149 if (elapsed > 10ms) {
150 ALOGE("Waited too long for consumer to produce an event, giving up");
151 break;
152 }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700153 result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
154 &event);
155 }
156 if (result != OK) {
157 ALOGE("Received result = %d from consume()", result);
158 }
159 result = mConsumer->sendFinishedSignal(consumeSeq, true);
160 if (result != OK) {
161 ALOGE("Received result = %d from sendFinishedSignal", result);
162 }
163 }
164
165protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700166 explicit FakeInputReceiver(InputDispatcher& dispatcher, const std::string name) {
167 Result<std::unique_ptr<InputChannel>> channelResult = dispatcher.createInputChannel(name);
168 LOG_ALWAYS_FATAL_IF(!channelResult.ok());
169 mClientChannel = std::move(*channelResult);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700170 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
171 }
172
173 virtual ~FakeInputReceiver() {}
174
Garfield Tan15601662020-09-22 15:32:38 -0700175 std::shared_ptr<InputChannel> mClientChannel;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700176 std::unique_ptr<InputConsumer> mConsumer;
177 PreallocatedInputEventFactory mEventFactory;
178};
179
chaviw3277faf2021-05-19 16:45:23 -0500180class FakeWindowHandle : public WindowInfoHandle, public FakeInputReceiver {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700181public:
182 static const int32_t WIDTH = 200;
183 static const int32_t HEIGHT = 200;
184
Chris Yea209fde2020-07-22 13:54:51 -0700185 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700186 InputDispatcher& dispatcher, const std::string name)
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700187 : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700188 inputApplicationHandle->updateInfo();
chaviw15fab6f2021-06-07 14:15:52 -0500189 updateInfo();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700190 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
191 }
192
chaviw15fab6f2021-06-07 14:15:52 -0500193 void updateInfo() {
Garfield Tan15601662020-09-22 15:32:38 -0700194 mInfo.token = mClientChannel->getConnectionToken();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700195 mInfo.name = "FakeWindowHandle";
chaviw3277faf2021-05-19 16:45:23 -0500196 mInfo.type = WindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500197 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700198 mInfo.frameLeft = mFrame.left;
199 mInfo.frameTop = mFrame.top;
200 mInfo.frameRight = mFrame.right;
201 mInfo.frameBottom = mFrame.bottom;
202 mInfo.globalScaleFactor = 1.0;
203 mInfo.touchableRegion.clear();
204 mInfo.addTouchableRegion(mFrame);
205 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700206 mInfo.focusable = true;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700207 mInfo.hasWallpaper = false;
208 mInfo.paused = false;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700209 mInfo.ownerPid = INJECTOR_PID;
210 mInfo.ownerUid = INJECTOR_UID;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700211 mInfo.displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700212 }
213
214protected:
215 Rect mFrame;
216};
217
218static MotionEvent generateMotionEvent() {
219 PointerProperties pointerProperties[1];
220 PointerCoords pointerCoords[1];
221
222 pointerProperties[0].clear();
223 pointerProperties[0].id = 0;
224 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
225
226 pointerCoords[0].clear();
227 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
228 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
229
230 const nsecs_t currentTime = now();
231
chaviw9eaa22c2020-07-01 16:21:27 -0700232 ui::Transform identityTransform;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700233 MotionEvent event;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000234 event.initialize(IInputConstants::INVALID_INPUT_EVENT_ID, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Garfield Tanfbe732e2020-01-24 11:26:14 -0800235 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
236 /* actionButton */ 0, /* flags */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700237 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviw9eaa22c2020-07-01 16:21:27 -0700238 identityTransform, /* xPrecision */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700239 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700240 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, currentTime,
241 currentTime,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700242 /*pointerCount*/ 1, pointerProperties, pointerCoords);
243 return event;
244}
245
246static NotifyMotionArgs generateMotionArgs() {
247 PointerProperties pointerProperties[1];
248 PointerCoords pointerCoords[1];
249
250 pointerProperties[0].clear();
251 pointerProperties[0].id = 0;
252 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
253
254 pointerCoords[0].clear();
255 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
256 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
257
258 const nsecs_t currentTime = now();
259 // Define a valid motion event.
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000260 NotifyMotionArgs args(IInputConstants::INVALID_INPUT_EVENT_ID, currentTime, currentTime,
261 DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
262 POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700263 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
264 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
265 pointerProperties, pointerCoords,
266 /* xPrecision */ 0, /* yPrecision */ 0,
267 AMOTION_EVENT_INVALID_CURSOR_POSITION,
268 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
269
270 return args;
271}
272
273static void benchmarkNotifyMotion(benchmark::State& state) {
274 // Create dispatcher
275 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700276 std::unique_ptr<InputDispatcher> dispatcher = std::make_unique<InputDispatcher>(fakePolicy);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700277 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
278 dispatcher->start();
279
280 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700281 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700282 sp<FakeWindowHandle> window = new FakeWindowHandle(application, *dispatcher, "Fake Window");
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700283
Arthur Hung72d8dc32020-03-28 00:48:39 +0000284 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700285
286 NotifyMotionArgs motionArgs = generateMotionArgs();
287
288 for (auto _ : state) {
289 // Send ACTION_DOWN
290 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700291 motionArgs.downTime = now();
292 motionArgs.eventTime = motionArgs.downTime;
293 dispatcher->notifyMotion(&motionArgs);
294
295 // Send ACTION_UP
296 motionArgs.action = AMOTION_EVENT_ACTION_UP;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700297 motionArgs.eventTime = now();
298 dispatcher->notifyMotion(&motionArgs);
299
300 window->consumeEvent();
301 window->consumeEvent();
302 }
303
304 dispatcher->stop();
305}
306
307static void benchmarkInjectMotion(benchmark::State& state) {
308 // Create dispatcher
309 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700310 std::unique_ptr<InputDispatcher> dispatcher = std::make_unique<InputDispatcher>(fakePolicy);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700311 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
312 dispatcher->start();
313
314 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700315 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700316 sp<FakeWindowHandle> window = new FakeWindowHandle(application, *dispatcher, "Fake Window");
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700317
Arthur Hung72d8dc32020-03-28 00:48:39 +0000318 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700319
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700320 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800321 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700322 // Send ACTION_DOWN
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700323 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800324 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700325 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
326
327 // Send ACTION_UP
328 event.setAction(AMOTION_EVENT_ACTION_UP);
329 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800330 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700331 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
332
333 window->consumeEvent();
334 window->consumeEvent();
335 }
336
337 dispatcher->stop();
338}
339
340BENCHMARK(benchmarkNotifyMotion);
341BENCHMARK(benchmarkInjectMotion);
342
343} // namespace android::inputdispatcher
344
345BENCHMARK_MAIN();