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