1 /*
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 <android/os/IInputConstants.h>
20 #include <binder/Binder.h>
21 #include <gui/constants.h>
22 #include "../dispatcher/InputDispatcher.h"
23
24 using android::gui::WindowInfo;
25 using android::gui::WindowInfoHandle;
26 using android::os::IInputConstants;
27 using android::os::InputEventInjectionResult;
28 using android::os::InputEventInjectionSync;
29
30 namespace android::inputdispatcher {
31
32 // An arbitrary device id.
33 static const int32_t DEVICE_ID = 1;
34
35 // An arbitrary injector pid / uid pair that has permission to inject events.
36 static const int32_t INJECTOR_PID = 999;
37 static const int32_t INJECTOR_UID = 1001;
38
39 static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 5s;
40 static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 100ms;
41
now()42 static nsecs_t now() {
43 return systemTime(SYSTEM_TIME_MONOTONIC);
44 }
45
46 // --- FakeInputDispatcherPolicy ---
47
48 class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
49 public:
FakeInputDispatcherPolicy()50 FakeInputDispatcherPolicy() {}
51
52 protected:
~FakeInputDispatcherPolicy()53 virtual ~FakeInputDispatcherPolicy() {}
54
55 private:
notifyConfigurationChanged(nsecs_t)56 void notifyConfigurationChanged(nsecs_t) override {}
57
notifyNoFocusedWindowAnr(const std::shared_ptr<InputApplicationHandle> & applicationHandle)58 void notifyNoFocusedWindowAnr(
59 const std::shared_ptr<InputApplicationHandle>& applicationHandle) override {
60 ALOGE("There is no focused window for %s", applicationHandle->getName().c_str());
61 }
62
notifyWindowUnresponsive(const sp<IBinder> & connectionToken,const std::string & reason)63 void notifyWindowUnresponsive(const sp<IBinder>& connectionToken,
64 const std::string& reason) override {
65 ALOGE("Window is not responding: %s", reason.c_str());
66 }
67
notifyWindowResponsive(const sp<IBinder> & connectionToken)68 void notifyWindowResponsive(const sp<IBinder>& connectionToken) override {}
69
notifyMonitorUnresponsive(int32_t pid,const std::string & reason)70 void notifyMonitorUnresponsive(int32_t pid, const std::string& reason) override {
71 ALOGE("Monitor is not responding: %s", reason.c_str());
72 }
73
notifyMonitorResponsive(int32_t pid)74 void notifyMonitorResponsive(int32_t pid) override {}
75
notifyInputChannelBroken(const sp<IBinder> &)76 void notifyInputChannelBroken(const sp<IBinder>&) override {}
77
notifyFocusChanged(const sp<IBinder> &,const sp<IBinder> &)78 void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {}
79
notifySensorEvent(int32_t deviceId,InputDeviceSensorType sensorType,InputDeviceSensorAccuracy accuracy,nsecs_t timestamp,const std::vector<float> & values)80 void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType,
81 InputDeviceSensorAccuracy accuracy, nsecs_t timestamp,
82 const std::vector<float>& values) override {}
83
notifySensorAccuracy(int32_t deviceId,InputDeviceSensorType sensorType,InputDeviceSensorAccuracy accuracy)84 void notifySensorAccuracy(int32_t deviceId, InputDeviceSensorType sensorType,
85 InputDeviceSensorAccuracy accuracy) override {}
86
notifyVibratorState(int32_t deviceId,bool isOn)87 void notifyVibratorState(int32_t deviceId, bool isOn) override {}
88
notifyUntrustedTouch(const std::string & obscuringPackage)89 void notifyUntrustedTouch(const std::string& obscuringPackage) override {}
90
getDispatcherConfiguration(InputDispatcherConfiguration * outConfig)91 void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) override {
92 *outConfig = mConfig;
93 }
94
filterInputEvent(const InputEvent * inputEvent,uint32_t policyFlags)95 bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) override {
96 return true;
97 }
98
interceptKeyBeforeQueueing(const KeyEvent *,uint32_t &)99 void interceptKeyBeforeQueueing(const KeyEvent*, uint32_t&) override {}
100
interceptMotionBeforeQueueing(int32_t,nsecs_t,uint32_t &)101 void interceptMotionBeforeQueueing(int32_t, nsecs_t, uint32_t&) override {}
102
interceptKeyBeforeDispatching(const sp<IBinder> &,const KeyEvent *,uint32_t)103 nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent*, uint32_t) override {
104 return 0;
105 }
106
dispatchUnhandledKey(const sp<IBinder> &,const KeyEvent *,uint32_t,KeyEvent *)107 bool dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent*, uint32_t, KeyEvent*) override {
108 return false;
109 }
110
notifySwitch(nsecs_t,uint32_t,uint32_t,uint32_t)111 void notifySwitch(nsecs_t, uint32_t, uint32_t, uint32_t) override {}
112
pokeUserActivity(nsecs_t,int32_t,int32_t)113 void pokeUserActivity(nsecs_t, int32_t, int32_t) override {}
114
checkInjectEventsPermissionNonReentrant(int32_t,int32_t)115 bool checkInjectEventsPermissionNonReentrant(int32_t, int32_t) override { return false; }
116
onPointerDownOutsideFocus(const sp<IBinder> & newToken)117 void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override {}
118
setPointerCapture(const PointerCaptureRequest &)119 void setPointerCapture(const PointerCaptureRequest&) override {}
120
notifyDropWindow(const sp<IBinder> &,float x,float y)121 void notifyDropWindow(const sp<IBinder>&, float x, float y) override {}
122
123 InputDispatcherConfiguration mConfig;
124 };
125
126 class FakeApplicationHandle : public InputApplicationHandle {
127 public:
FakeApplicationHandle()128 FakeApplicationHandle() {}
~FakeApplicationHandle()129 virtual ~FakeApplicationHandle() {}
130
updateInfo()131 virtual bool updateInfo() {
132 mInfo.dispatchingTimeoutMillis =
133 std::chrono::duration_cast<std::chrono::milliseconds>(DISPATCHING_TIMEOUT).count();
134 return true;
135 }
136 };
137
138 class FakeInputReceiver {
139 public:
consumeEvent()140 void consumeEvent() {
141 uint32_t consumeSeq = 0;
142 InputEvent* event;
143
144 std::chrono::time_point start = std::chrono::steady_clock::now();
145 status_t result = WOULD_BLOCK;
146 while (result == WOULD_BLOCK) {
147 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
148 if (elapsed > 10ms) {
149 ALOGE("Waited too long for consumer to produce an event, giving up");
150 break;
151 }
152 result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq,
153 &event);
154 }
155 if (result != OK) {
156 ALOGE("Received result = %d from consume()", result);
157 }
158 result = mConsumer->sendFinishedSignal(consumeSeq, true);
159 if (result != OK) {
160 ALOGE("Received result = %d from sendFinishedSignal", result);
161 }
162 }
163
164 protected:
FakeInputReceiver(const sp<InputDispatcher> & dispatcher,const std::string name)165 explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, const std::string name)
166 : mDispatcher(dispatcher) {
167 mClientChannel = *mDispatcher->createInputChannel(name);
168 mConsumer = std::make_unique<InputConsumer>(mClientChannel);
169 }
170
~FakeInputReceiver()171 virtual ~FakeInputReceiver() {}
172
173 sp<InputDispatcher> mDispatcher;
174 std::shared_ptr<InputChannel> mClientChannel;
175 std::unique_ptr<InputConsumer> mConsumer;
176 PreallocatedInputEventFactory mEventFactory;
177 };
178
179 class FakeWindowHandle : public WindowInfoHandle, public FakeInputReceiver {
180 public:
181 static const int32_t WIDTH = 200;
182 static const int32_t HEIGHT = 200;
183
FakeWindowHandle(const std::shared_ptr<InputApplicationHandle> & inputApplicationHandle,const sp<InputDispatcher> & dispatcher,const std::string name)184 FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle,
185 const sp<InputDispatcher>& dispatcher, const std::string name)
186 : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) {
187 inputApplicationHandle->updateInfo();
188 updateInfo();
189 mInfo.applicationInfo = *inputApplicationHandle->getInfo();
190 }
191
updateInfo()192 void updateInfo() {
193 mInfo.token = mClientChannel->getConnectionToken();
194 mInfo.name = "FakeWindowHandle";
195 mInfo.type = WindowInfo::Type::APPLICATION;
196 mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT;
197 mInfo.frameLeft = mFrame.left;
198 mInfo.frameTop = mFrame.top;
199 mInfo.frameRight = mFrame.right;
200 mInfo.frameBottom = mFrame.bottom;
201 mInfo.globalScaleFactor = 1.0;
202 mInfo.touchableRegion.clear();
203 mInfo.addTouchableRegion(mFrame);
204 mInfo.visible = true;
205 mInfo.focusable = true;
206 mInfo.hasWallpaper = false;
207 mInfo.paused = false;
208 mInfo.ownerPid = INJECTOR_PID;
209 mInfo.ownerUid = INJECTOR_UID;
210 mInfo.displayId = ADISPLAY_ID_DEFAULT;
211 }
212
213 protected:
214 Rect mFrame;
215 };
216
generateMotionEvent()217 static MotionEvent generateMotionEvent() {
218 PointerProperties pointerProperties[1];
219 PointerCoords pointerCoords[1];
220
221 pointerProperties[0].clear();
222 pointerProperties[0].id = 0;
223 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
224
225 pointerCoords[0].clear();
226 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
227 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
228
229 const nsecs_t currentTime = now();
230
231 ui::Transform identityTransform;
232 MotionEvent event;
233 event.initialize(IInputConstants::INVALID_INPUT_EVENT_ID, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN,
234 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN,
235 /* actionButton */ 0, /* flags */ 0,
236 /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE,
237 identityTransform, /* xPrecision */ 0,
238 /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
239 AMOTION_EVENT_INVALID_CURSOR_POSITION, ui::Transform::ROT_0,
240 INVALID_DISPLAY_SIZE, INVALID_DISPLAY_SIZE, currentTime, currentTime,
241 /*pointerCount*/ 1, pointerProperties, pointerCoords);
242 return event;
243 }
244
generateMotionArgs()245 static NotifyMotionArgs generateMotionArgs() {
246 PointerProperties pointerProperties[1];
247 PointerCoords pointerCoords[1];
248
249 pointerProperties[0].clear();
250 pointerProperties[0].id = 0;
251 pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
252
253 pointerCoords[0].clear();
254 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100);
255 pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100);
256
257 const nsecs_t currentTime = now();
258 // Define a valid motion event.
259 NotifyMotionArgs args(IInputConstants::INVALID_INPUT_EVENT_ID, currentTime, currentTime,
260 DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT,
261 POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN,
262 /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0,
263 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
264 pointerProperties, pointerCoords,
265 /* xPrecision */ 0, /* yPrecision */ 0,
266 AMOTION_EVENT_INVALID_CURSOR_POSITION,
267 AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {});
268
269 return args;
270 }
271
benchmarkNotifyMotion(benchmark::State & state)272 static void benchmarkNotifyMotion(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 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
281 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
282
283 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
284
285 NotifyMotionArgs motionArgs = generateMotionArgs();
286
287 for (auto _ : state) {
288 // Send ACTION_DOWN
289 motionArgs.action = AMOTION_EVENT_ACTION_DOWN;
290 motionArgs.downTime = now();
291 motionArgs.eventTime = motionArgs.downTime;
292 dispatcher->notifyMotion(&motionArgs);
293
294 // Send ACTION_UP
295 motionArgs.action = AMOTION_EVENT_ACTION_UP;
296 motionArgs.eventTime = now();
297 dispatcher->notifyMotion(&motionArgs);
298
299 window->consumeEvent();
300 window->consumeEvent();
301 }
302
303 dispatcher->stop();
304 }
305
benchmarkInjectMotion(benchmark::State & state)306 static void benchmarkInjectMotion(benchmark::State& state) {
307 // Create dispatcher
308 sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy();
309 sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy);
310 dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false);
311 dispatcher->start();
312
313 // Create a window that will receive motion events
314 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
315 sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window");
316
317 dispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}});
318
319 for (auto _ : state) {
320 MotionEvent event = generateMotionEvent();
321 // Send ACTION_DOWN
322 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
323 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
324 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
325
326 // Send ACTION_UP
327 event.setAction(AMOTION_EVENT_ACTION_UP);
328 dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID,
329 InputEventInjectionSync::NONE, INJECT_EVENT_TIMEOUT,
330 POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER);
331
332 window->consumeEvent();
333 window->consumeEvent();
334 }
335
336 dispatcher->stop();
337 }
338
339 BENCHMARK(benchmarkNotifyMotion);
340 BENCHMARK(benchmarkInjectMotion);
341
342 } // namespace android::inputdispatcher
343
344 BENCHMARK_MAIN();
345