blob: 9691ad8a79c109100037d6e63565ec2bd27523da [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
Prabir Pradhanedd96402022-02-15 01:46:16 -080064 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<int32_t> pid,
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +000065 const std::string& reason) override {
66 ALOGE("Window is not responding: %s", reason.c_str());
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050067 }
68
Prabir Pradhanedd96402022-02-15 01:46:16 -080069 void notifyWindowResponsive(const sp<IBinder>& connectionToken,
70 std::optional<int32_t> pid) override {}
Siarhei Vishniakou234129c2020-10-22 22:28:12 -050071
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050072 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070073
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050074 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070075
Chris Yef59a2f42020-10-16 12:55:26 -070076 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
77 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
78 const std::vector<float>& values) override {}
79
80 void notifySensorAccuracy(int32_t deviceId, InputDeviceSensorType sensorType,
81 InputDeviceSensorAccuracy accuracy) override {}
82
Chris Yefb552902021-02-03 17:18:37 -080083 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
84
Bernardo Rufino2e1f6512020-10-08 13:42:07 +000085 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
86
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050087 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070088 *outConfig = mConfig;
89 }
90
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050091 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070092 return true;
93 }
94
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050095 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070096
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050097 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070098
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050099 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700100 return 0;
101 }
102
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500103 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700104 return false;
105 }
106
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500107 void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700108
Sean Stoutb4e0a592021-02-23 07:34:53 -0800109 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700110
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500111 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700112
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500113 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700114
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000115 void setPointerCapture(const PointerCaptureRequest&) override {}
Prabir Pradhan99987712020-11-10 18:43:05 -0800116
arthurhungf452d0b2021-01-06 00:19:52 +0800117 void notifyDropWindow(const sp<IBinder>&, float x, float y) override {}
118
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700119 InputDispatcherConfiguration mConfig;
120};
121
122class FakeApplicationHandle : public InputApplicationHandle {
123public:
124 FakeApplicationHandle() {}
125 virtual ~FakeApplicationHandle() {}
126
127 virtual bool updateInfo() {
Siarhei Vishniakou70622952020-07-30 11:17:23 -0500128 mInfo.dispatchingTimeoutMillis =
129 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700130 return true;
131 }
132};
133
134class FakeInputReceiver {
135public:
136 void consumeEvent() {
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500137 uint32_t consumeSeq = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700138 InputEvent* event;
139
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800140 std::chrono::time_point start = std::chrono::steady_clock::now();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700141 status_t result = WOULD_BLOCK;
142 while (result == WOULD_BLOCK) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800143 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
144 if (elapsed > 10ms) {
145 ALOGE("Waited too long for consumer to produce an event, giving up");
146 break;
147 }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700148 result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
149 &event);
150 }
151 if (result != OK) {
152 ALOGE("Received result = %d from consume()", result);
153 }
154 result = mConsumer->sendFinishedSignal(consumeSeq, true);
155 if (result != OK) {
156 ALOGE("Received result = %d from sendFinishedSignal", result);
157 }
158 }
159
160protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700161 explicit FakeInputReceiver(InputDispatcher& dispatcher, const std::string name) {
162 Result<std::unique_ptr<InputChannel>> channelResult = dispatcher.createInputChannel(name);
163 LOG_ALWAYS_FATAL_IF(!channelResult.ok());
164 mClientChannel = std::move(*channelResult);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700165 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
166 }
167
168 virtual ~FakeInputReceiver() {}
169
Garfield Tan15601662020-09-22 15:32:38 -0700170 std::shared_ptr<InputChannel> mClientChannel;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700171 std::unique_ptr<InputConsumer> mConsumer;
172 PreallocatedInputEventFactory mEventFactory;
173};
174
chaviw3277faf2021-05-19 16:45:23 -0500175class FakeWindowHandle : public WindowInfoHandle, public FakeInputReceiver {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700176public:
177 static const int32_t WIDTH = 200;
178 static const int32_t HEIGHT = 200;
179
Chris Yea209fde2020-07-22 13:54:51 -0700180 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700181 InputDispatcher& dispatcher, const std::string name)
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700182 : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700183 inputApplicationHandle->updateInfo();
chaviw15fab6f2021-06-07 14:15:52 -0500184 updateInfo();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700185 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
186 }
187
chaviw15fab6f2021-06-07 14:15:52 -0500188 void updateInfo() {
Garfield Tan15601662020-09-22 15:32:38 -0700189 mInfo.token = mClientChannel->getConnectionToken();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700190 mInfo.name = "FakeWindowHandle";
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500191 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700192 mInfo.frameLeft = mFrame.left;
193 mInfo.frameTop = mFrame.top;
194 mInfo.frameRight = mFrame.right;
195 mInfo.frameBottom = mFrame.bottom;
196 mInfo.globalScaleFactor = 1.0;
197 mInfo.touchableRegion.clear();
198 mInfo.addTouchableRegion(mFrame);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700199 mInfo.ownerPid = INJECTOR_PID;
200 mInfo.ownerUid = INJECTOR_UID;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700201 mInfo.displayId = ADISPLAY_ID_DEFAULT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700202 }
203
204protected:
205 Rect mFrame;
206};
207
208static MotionEvent generateMotionEvent() {
209 PointerProperties pointerProperties[1];
210 PointerCoords pointerCoords[1];
211
212 pointerProperties[0].clear();
213 pointerProperties[0].id = 0;
214 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
215
216 pointerCoords[0].clear();
217 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
218 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
219
220 const nsecs_t currentTime = now();
221
chaviw9eaa22c2020-07-01 16:21:27 -0700222 ui::Transform identityTransform;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700223 MotionEvent event;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000224 event.initialize(IInputConstants::INVALID_INPUT_EVENT_ID, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Garfield Tanfbe732e2020-01-24 11:26:14 -0800225 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
226 /* actionButton */ 0, /* flags */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700227 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviw9eaa22c2020-07-01 16:21:27 -0700228 identityTransform, /* xPrecision */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700229 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700230 AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, currentTime,
231 currentTime,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700232 /*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.
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000250 NotifyMotionArgs args(IInputConstants::INVALID_INPUT_EVENT_ID, currentTime, currentTime,
251 DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
252 POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700253 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
254 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
255 pointerProperties, pointerCoords,
256 /* xPrecision */ 0, /* yPrecision */ 0,
257 AMOTION_EVENT_INVALID_CURSOR_POSITION,
258 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
259
260 return args;
261}
262
263static void benchmarkNotifyMotion(benchmark::State& state) {
264 // Create dispatcher
265 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700266 std::unique_ptr<InputDispatcher> dispatcher = std::make_unique<InputDispatcher>(fakePolicy);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700267 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
268 dispatcher->start();
269
270 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700271 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700272 sp<FakeWindowHandle> window = new FakeWindowHandle(application, *dispatcher, "Fake Window");
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700273
Arthur Hung72d8dc32020-03-28 00:48:39 +0000274 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700275
276 NotifyMotionArgs motionArgs = generateMotionArgs();
277
278 for (auto _ : state) {
279 // Send ACTION_DOWN
280 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
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;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700287 motionArgs.eventTime = now();
288 dispatcher->notifyMotion(&motionArgs);
289
290 window->consumeEvent();
291 window->consumeEvent();
292 }
293
294 dispatcher->stop();
295}
296
297static void benchmarkInjectMotion(benchmark::State& state) {
298 // Create dispatcher
299 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700300 std::unique_ptr<InputDispatcher> dispatcher = std::make_unique<InputDispatcher>(fakePolicy);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700301 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
302 dispatcher->start();
303
304 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700305 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700306 sp<FakeWindowHandle> window = new FakeWindowHandle(application, *dispatcher, "Fake Window");
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700307
Arthur Hung72d8dc32020-03-28 00:48:39 +0000308 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700309
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700310 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800311 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700312 // Send ACTION_DOWN
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700313 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800314 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700315 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
316
317 // Send ACTION_UP
318 event.setAction(AMOTION_EVENT_ACTION_UP);
319 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
Siarhei Vishniakouae6229e2019-12-30 16:23:19 -0800320 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700321 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
322
323 window->consumeEvent();
324 window->consumeEvent();
325 }
326
327 dispatcher->stop();
328}
329
330BENCHMARK(benchmarkNotifyMotion);
331BENCHMARK(benchmarkInjectMotion);
332
333} // namespace android::inputdispatcher
334
335BENCHMARK_MAIN();