blob: 0d3c821215accaf1d17f6837971fa57c24b37576 [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
31static const int32_t INJECT_EVENT_TIMEOUT = 5000;
32static const int32_t DISPATCHING_TIMEOUT = 100000;
33
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:
48 virtual void notifyConfigurationChanged(nsecs_t) override {}
49
50 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>&, const sp<IBinder>&,
51 const std::string& name) override {
52 ALOGE("The window is not responding : %s", name.c_str());
53 return 0;
54 }
55
56 virtual void notifyInputChannelBroken(const sp<IBinder>&) override {}
57
58 virtual void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
59
60 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
61 *outConfig = mConfig;
62 }
63
64 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
65 return true;
66 }
67
68 virtual void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
69
70 virtual void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
71
72 virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*,
73 uint32_t) override {
74 return 0;
75 }
76
77 virtual bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t,
78 KeyEvent*) override {
79 return false;
80 }
81
82 virtual void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {}
83
84 virtual void pokeUserActivity(nsecs_t, int32_t) override {}
85
86 virtual bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override {
87 return false;
88 }
89
90 virtual void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {}
91
92 InputDispatcherConfiguration mConfig;
93};
94
95class FakeApplicationHandle : public InputApplicationHandle {
96public:
97 FakeApplicationHandle() {}
98 virtual ~FakeApplicationHandle() {}
99
100 virtual bool updateInfo() {
101 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
102 return true;
103 }
104};
105
106class FakeInputReceiver {
107public:
108 void consumeEvent() {
109 uint32_t consumeSeq;
110 InputEvent* event;
111
112 status_t result = WOULD_BLOCK;
113 while (result == WOULD_BLOCK) {
114 result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
115 &event);
116 }
117 if (result != OK) {
118 ALOGE("Received result = %d from consume()", result);
119 }
120 result = mConsumer->sendFinishedSignal(consumeSeq, true);
121 if (result != OK) {
122 ALOGE("Received result = %d from sendFinishedSignal", result);
123 }
124 }
125
126protected:
127 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, const std::string name)
128 : mDispatcher(dispatcher) {
129 InputChannel::openInputChannelPair(name, mServerChannel, mClientChannel);
130 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
131 }
132
133 virtual ~FakeInputReceiver() {}
134
135 sp<InputDispatcher> mDispatcher;
136 sp<InputChannel> mServerChannel, mClientChannel;
137 std::unique_ptr<InputConsumer> mConsumer;
138 PreallocatedInputEventFactory mEventFactory;
139};
140
141class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver {
142public:
143 static const int32_t WIDTH = 200;
144 static const int32_t HEIGHT = 200;
145
146 FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle,
147 const sp<InputDispatcher>& dispatcher, const std::string name)
148 : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
149 mDispatcher->registerInputChannel(mServerChannel);
150
151 inputApplicationHandle->updateInfo();
152 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
153 }
154
155 virtual bool updateInfo() override {
156 mInfo.token = mServerChannel->getConnectionToken();
157 mInfo.name = "FakeWindowHandle";
158 mInfo.layoutParamsFlags = 0;
159 mInfo.layoutParamsType = InputWindowInfo::TYPE_APPLICATION;
160 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
161 mInfo.frameLeft = mFrame.left;
162 mInfo.frameTop = mFrame.top;
163 mInfo.frameRight = mFrame.right;
164 mInfo.frameBottom = mFrame.bottom;
165 mInfo.globalScaleFactor = 1.0;
166 mInfo.touchableRegion.clear();
167 mInfo.addTouchableRegion(mFrame);
168 mInfo.visible = true;
169 mInfo.canReceiveKeys = true;
170 mInfo.hasFocus = true;
171 mInfo.hasWallpaper = false;
172 mInfo.paused = false;
Siarhei Vishniakoud0784762019-11-01 15:33:48 -0700173 mInfo.ownerPid = INJECTOR_PID;
174 mInfo.ownerUid = INJECTOR_UID;
175 mInfo.inputFeatures = 0;
176 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
199 MotionEvent event;
200 event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
201 AMOTION_EVENT_ACTION_DOWN, /* actionButton */ 0, /* flags */ 0,
202 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
203 /* xOffset */ 0, /* yOffset */ 0, /* xPrecision */ 0,
204 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
205 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, currentTime,
206 /*pointerCount*/ 1, pointerProperties, pointerCoords);
207 return event;
208}
209
210static NotifyMotionArgs generateMotionArgs() {
211 PointerProperties pointerProperties[1];
212 PointerCoords pointerCoords[1];
213
214 pointerProperties[0].clear();
215 pointerProperties[0].id = 0;
216 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
217
218 pointerCoords[0].clear();
219 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
220 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
221
222 const nsecs_t currentTime = now();
223 // Define a valid motion event.
224 NotifyMotionArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
225 ADISPLAY_ID_DEFAULT, POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
226 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
227 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
228 pointerProperties, pointerCoords,
229 /* xPrecision */ 0, /* yPrecision */ 0,
230 AMOTION_EVENT_INVALID_CURSOR_POSITION,
231 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
232
233 return args;
234}
235
236static void benchmarkNotifyMotion(benchmark::State& state) {
237 // Create dispatcher
238 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
239 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
240 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
241 dispatcher->start();
242
243 // Create a window that will receive motion events
244 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
245 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
246
247 dispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
248
249 NotifyMotionArgs motionArgs = generateMotionArgs();
250
251 for (auto _ : state) {
252 // Send ACTION_DOWN
253 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
254 motionArgs.sequenceNum = 0;
255 motionArgs.downTime = now();
256 motionArgs.eventTime = motionArgs.downTime;
257 dispatcher->notifyMotion(&motionArgs);
258
259 // Send ACTION_UP
260 motionArgs.action = AMOTION_EVENT_ACTION_UP;
261 motionArgs.sequenceNum = 1;
262 motionArgs.eventTime = now();
263 dispatcher->notifyMotion(&motionArgs);
264
265 window->consumeEvent();
266 window->consumeEvent();
267 }
268
269 dispatcher->stop();
270}
271
272static void benchmarkInjectMotion(benchmark::State& state) {
273 // Create dispatcher
274 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
275 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
276 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
277 dispatcher->start();
278
279 // Create a window that will receive motion events
280 sp<FakeApplicationHandle> application = new FakeApplicationHandle();
281 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
282
283 dispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT);
284
285 MotionEvent event = generateMotionEvent();
286
287 for (auto _ : state) {
288 // Send ACTION_DOWN
289 event.setAction(AMOTION_EVENT_ACTION_DOWN);
290 event.setDownTime(now());
291 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
292 INPUT_EVENT_INJECTION_SYNC_NONE, INJECT_EVENT_TIMEOUT,
293 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
294
295 // Send ACTION_UP
296 event.setAction(AMOTION_EVENT_ACTION_UP);
297 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
298 INPUT_EVENT_INJECTION_SYNC_NONE, INJECT_EVENT_TIMEOUT,
299 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
300
301 window->consumeEvent();
302 window->consumeEvent();
303 }
304
305 dispatcher->stop();
306}
307
308BENCHMARK(benchmarkNotifyMotion);
309BENCHMARK(benchmarkInjectMotion);
310
311} // namespace android::inputdispatcher
312
313BENCHMARK_MAIN();