blob: b00364d9c5b63d2ad74c545285b2b2b61a2b3395 [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
22namespace android::inputdispatcher {
23
24// An arbitrary device id.
25static const int32_t DEVICE_ID = 1;
26
27// An arbitrary injector pid / uid pair that has permission to inject events.
28static const int32_t INJECTOR_PID = 999;
29static const int32_t INJECTOR_UID = 1001;
30
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -070031static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 5s;
32static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 100ms;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070033
34static nsecs_t now() {
35 return systemTime(SYSTEM_TIME_MONOTONIC);
36}
37
38// --- FakeInputDispatcherPolicy ---
39
40class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
41public:
42 FakeInputDispatcherPolicy() {}
43
44protected:
45 virtual ~FakeInputDispatcherPolicy() {}
46
47private:
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050048 void notifyConfigurationChanged(nsecs_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070049
Chris Yea209fde2020-07-22 13:54:51 -070050 std::chrono::nanoseconds notifyAnr(const std::shared_ptr<InputApplicationHandle>&,
51 const sp<IBinder>&, const std::string& name) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070052 ALOGE("The window is not responding : %s", name.c_str());
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050053 return 0s;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070054 }
55
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050056 void notifyInputChannelBroken(const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070057
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050058 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070059
Bernardo Rufino2e1f6512020-10-08 13:42:07 +000060 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
61
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050062 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070063 *outConfig = mConfig;
64 }
65
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050066 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070067 return true;
68 }
69
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050070 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070071
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050072 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070073
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050074 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070075 return 0;
76 }
77
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050078 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070079 return false;
80 }
81
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050082 void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070083
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050084 void pokeUserActivity(nsecs_t, int32_t) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070085
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050086 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070087
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -050088 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {}
Siarhei Vishniakoud0784762019-11-01 15:33:48 -070089
90 InputDispatcherConfiguration mConfig;
91};
92
93class FakeApplicationHandle : public InputApplicationHandle {
94public:
95 FakeApplicationHandle() {}
96 virtual ~FakeApplicationHandle() {}
97
98 virtual bool updateInfo() {
Siarhei Vishniakou70622952020-07-30 11:17:23 -050099 mInfo.dispatchingTimeoutMillis =
100 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700101 return true;
102 }
103};
104
105class FakeInputReceiver {
106public:
107 void consumeEvent() {
Siarhei Vishniakoud549b252020-08-11 11:25:26 -0500108 uint32_t consumeSeq = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700109 InputEvent* event;
110
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800111 std::chrono::time_point start = std::chrono::steady_clock::now();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700112 status_t result = WOULD_BLOCK;
113 while (result == WOULD_BLOCK) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800114 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
115 if (elapsed > 10ms) {
116 ALOGE("Waited too long for consumer to produce an event, giving up");
117 break;
118 }
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700119 result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
120 &event);
121 }
122 if (result != OK) {
123 ALOGE("Received result = %d from consume()", result);
124 }
125 result = mConsumer->sendFinishedSignal(consumeSeq, true);
126 if (result != OK) {
127 ALOGE("Received result = %d from sendFinishedSignal", result);
128 }
129 }
130
131protected:
132 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, const std::string name)
133 : mDispatcher(dispatcher) {
Garfield Tan15601662020-09-22 15:32:38 -0700134 mClientChannel = *mDispatcher->createInputChannel(name);
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700135 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
136 }
137
138 virtual ~FakeInputReceiver() {}
139
140 sp<InputDispatcher> mDispatcher;
Garfield Tan15601662020-09-22 15:32:38 -0700141 std::shared_ptr<InputChannel> mClientChannel;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700142 std::unique_ptr<InputConsumer> mConsumer;
143 PreallocatedInputEventFactory mEventFactory;
144};
145
146class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver {
147public:
148 static const int32_t WIDTH = 200;
149 static const int32_t HEIGHT = 200;
150
Chris Yea209fde2020-07-22 13:54:51 -0700151 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700152 const sp<InputDispatcher>& dispatcher, const std::string name)
153 : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700154 inputApplicationHandle->updateInfo();
155 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
156 }
157
158 virtual bool updateInfo() override {
Garfield Tan15601662020-09-22 15:32:38 -0700159 mInfo.token = mClientChannel->getConnectionToken();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700160 mInfo.name = "FakeWindowHandle";
Michael Wright44753b12020-07-08 13:48:11 +0100161 mInfo.type = InputWindowInfo::Type::APPLICATION;
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500162 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700163 mInfo.frameLeft = mFrame.left;
164 mInfo.frameTop = mFrame.top;
165 mInfo.frameRight = mFrame.right;
166 mInfo.frameBottom = mFrame.bottom;
167 mInfo.globalScaleFactor = 1.0;
168 mInfo.touchableRegion.clear();
169 mInfo.addTouchableRegion(mFrame);
170 mInfo.visible = true;
Vishnu Nair47074b82020-08-14 11:54:47 -0700171 mInfo.focusable = true;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700172 mInfo.hasWallpaper = false;
173 mInfo.paused = false;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700174 mInfo.ownerPid = INJECTOR_PID;
175 mInfo.ownerUid = INJECTOR_UID;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700176 mInfo.displayId = ADISPLAY_ID_DEFAULT;
177
178 return true;
179 }
180
181protected:
182 Rect mFrame;
183};
184
185static MotionEvent generateMotionEvent() {
186 PointerProperties pointerProperties[1];
187 PointerCoords pointerCoords[1];
188
189 pointerProperties[0].clear();
190 pointerProperties[0].id = 0;
191 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
192
193 pointerCoords[0].clear();
194 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
195 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
196
197 const nsecs_t currentTime = now();
198
chaviw9eaa22c2020-07-01 16:21:27 -0700199 ui::Transform identityTransform;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700200 MotionEvent event;
Garfield Tanfbe732e2020-01-24 11:26:14 -0800201 event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
202 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
203 /* actionButton */ 0, /* flags */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700204 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
chaviw9eaa22c2020-07-01 16:21:27 -0700205 identityTransform, /* xPrecision */ 0,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700206 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
207 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, currentTime,
208 /*pointerCount*/ 1, pointerProperties, pointerCoords);
209 return event;
210}
211
212static NotifyMotionArgs generateMotionArgs() {
213 PointerProperties pointerProperties[1];
214 PointerCoords pointerCoords[1];
215
216 pointerProperties[0].clear();
217 pointerProperties[0].id = 0;
218 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
219
220 pointerCoords[0].clear();
221 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
222 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
223
224 const nsecs_t currentTime = now();
225 // Define a valid motion event.
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800226 NotifyMotionArgs args(/* id */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700227 ADISPLAY_ID_DEFAULT, POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
228 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
229 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
230 pointerProperties, pointerCoords,
231 /* xPrecision */ 0, /* yPrecision */ 0,
232 AMOTION_EVENT_INVALID_CURSOR_POSITION,
233 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
234
235 return args;
236}
237
238static void benchmarkNotifyMotion(benchmark::State& state) {
239 // Create dispatcher
240 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
241 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
242 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
243 dispatcher->start();
244
245 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700246 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700247 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
248
Arthur Hung72d8dc32020-03-28 00:48:39 +0000249 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700250
251 NotifyMotionArgs motionArgs = generateMotionArgs();
252
253 for (auto _ : state) {
254 // Send ACTION_DOWN
255 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800256 motionArgs.id = 0;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700257 motionArgs.downTime = now();
258 motionArgs.eventTime = motionArgs.downTime;
259 dispatcher->notifyMotion(&motionArgs);
260
261 // Send ACTION_UP
262 motionArgs.action = AMOTION_EVENT_ACTION_UP;
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800263 motionArgs.id = 1;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700264 motionArgs.eventTime = now();
265 dispatcher->notifyMotion(&motionArgs);
266
267 window->consumeEvent();
268 window->consumeEvent();
269 }
270
271 dispatcher->stop();
272}
273
274static void benchmarkInjectMotion(benchmark::State& state) {
275 // Create dispatcher
276 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
277 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
278 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
279 dispatcher->start();
280
281 // Create a window that will receive motion events
Chris Yea209fde2020-07-22 13:54:51 -0700282 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700283 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
284
Arthur Hung72d8dc32020-03-28 00:48:39 +0000285 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700286
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700287 for (auto _ : state) {
Siarhei Vishniakouadfd4fa2019-12-20 11:02:58 -0800288 MotionEvent event = generateMotionEvent();
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700289 // Send ACTION_DOWN
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700290 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
291 INPUT_EVENT_INJECTION_SYNC_NONE, INJECT_EVENT_TIMEOUT,
292 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
293
294 // Send ACTION_UP
295 event.setAction(AMOTION_EVENT_ACTION_UP);
296 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
297 INPUT_EVENT_INJECTION_SYNC_NONE, INJECT_EVENT_TIMEOUT,
298 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
299
300 window->consumeEvent();
301 window->consumeEvent();
302 }
303
304 dispatcher->stop();
305}
306
307BENCHMARK(benchmarkNotifyMotion);
308BENCHMARK(benchmarkInjectMotion);
309
310} // namespace android::inputdispatcher
311
312BENCHMARK_MAIN();