| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 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 <binder/Binder.h> | 
 | 20 | #include "../dispatcher/InputDispatcher.h" | 
 | 21 |  | 
 | 22 | namespace android::inputdispatcher { | 
 | 23 |  | 
 | 24 | // An arbitrary device id. | 
 | 25 | static const int32_t DEVICE_ID = 1; | 
 | 26 |  | 
 | 27 | // An arbitrary injector pid / uid pair that has permission to inject events. | 
 | 28 | static const int32_t INJECTOR_PID = 999; | 
 | 29 | static const int32_t INJECTOR_UID = 1001; | 
 | 30 |  | 
 | 31 | static const int32_t INJECT_EVENT_TIMEOUT = 5000; | 
 | 32 | static const int32_t DISPATCHING_TIMEOUT = 100000; | 
 | 33 |  | 
 | 34 | static nsecs_t now() { | 
 | 35 |     return systemTime(SYSTEM_TIME_MONOTONIC); | 
 | 36 | } | 
 | 37 |  | 
 | 38 | // --- FakeInputDispatcherPolicy --- | 
 | 39 |  | 
 | 40 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { | 
 | 41 | public: | 
 | 42 |     FakeInputDispatcherPolicy() {} | 
 | 43 |  | 
 | 44 | protected: | 
 | 45 |     virtual ~FakeInputDispatcherPolicy() {} | 
 | 46 |  | 
 | 47 | private: | 
 | 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 |  | 
 | 95 | class FakeApplicationHandle : public InputApplicationHandle { | 
 | 96 | public: | 
 | 97 |     FakeApplicationHandle() {} | 
 | 98 |     virtual ~FakeApplicationHandle() {} | 
 | 99 |  | 
 | 100 |     virtual bool updateInfo() { | 
 | 101 |         mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; | 
 | 102 |         return true; | 
 | 103 |     } | 
 | 104 | }; | 
 | 105 |  | 
 | 106 | class FakeInputReceiver { | 
 | 107 | public: | 
 | 108 |     void consumeEvent() { | 
 | 109 |         uint32_t consumeSeq; | 
 | 110 |         InputEvent* event; | 
 | 111 |  | 
| Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 112 |         std::chrono::time_point start = std::chrono::steady_clock::now(); | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 113 |         status_t result = WOULD_BLOCK; | 
 | 114 |         while (result == WOULD_BLOCK) { | 
| Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 115 |             std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; | 
 | 116 |             if (elapsed > 10ms) { | 
 | 117 |                 ALOGE("Waited too long for consumer to produce an event, giving up"); | 
 | 118 |                 break; | 
 | 119 |             } | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 120 |             result = mConsumer->consume(&mEventFactory, true /*consumeBatches*/, -1, &consumeSeq, | 
 | 121 |                                         &event); | 
 | 122 |         } | 
 | 123 |         if (result != OK) { | 
 | 124 |             ALOGE("Received result = %d from consume()", result); | 
 | 125 |         } | 
 | 126 |         result = mConsumer->sendFinishedSignal(consumeSeq, true); | 
 | 127 |         if (result != OK) { | 
 | 128 |             ALOGE("Received result = %d from sendFinishedSignal", result); | 
 | 129 |         } | 
 | 130 |     } | 
 | 131 |  | 
 | 132 | protected: | 
 | 133 |     explicit FakeInputReceiver(const sp<InputDispatcher>& dispatcher, const std::string name) | 
 | 134 |           : mDispatcher(dispatcher) { | 
 | 135 |         InputChannel::openInputChannelPair(name, mServerChannel, mClientChannel); | 
 | 136 |         mConsumer = std::make_unique<InputConsumer>(mClientChannel); | 
 | 137 |     } | 
 | 138 |  | 
 | 139 |     virtual ~FakeInputReceiver() {} | 
 | 140 |  | 
 | 141 |     sp<InputDispatcher> mDispatcher; | 
 | 142 |     sp<InputChannel> mServerChannel, mClientChannel; | 
 | 143 |     std::unique_ptr<InputConsumer> mConsumer; | 
 | 144 |     PreallocatedInputEventFactory mEventFactory; | 
 | 145 | }; | 
 | 146 |  | 
 | 147 | class FakeWindowHandle : public InputWindowHandle, public FakeInputReceiver { | 
 | 148 | public: | 
 | 149 |     static const int32_t WIDTH = 200; | 
 | 150 |     static const int32_t HEIGHT = 200; | 
 | 151 |  | 
 | 152 |     FakeWindowHandle(const sp<InputApplicationHandle>& inputApplicationHandle, | 
 | 153 |                      const sp<InputDispatcher>& dispatcher, const std::string name) | 
 | 154 |           : FakeInputReceiver(dispatcher, name), mFrame(Rect(0, 0, WIDTH, HEIGHT)) { | 
 | 155 |         mDispatcher->registerInputChannel(mServerChannel); | 
 | 156 |  | 
 | 157 |         inputApplicationHandle->updateInfo(); | 
 | 158 |         mInfo.applicationInfo = *inputApplicationHandle->getInfo(); | 
 | 159 |     } | 
 | 160 |  | 
 | 161 |     virtual bool updateInfo() override { | 
 | 162 |         mInfo.token = mServerChannel->getConnectionToken(); | 
 | 163 |         mInfo.name = "FakeWindowHandle"; | 
 | 164 |         mInfo.layoutParamsFlags = 0; | 
 | 165 |         mInfo.layoutParamsType = InputWindowInfo::TYPE_APPLICATION; | 
 | 166 |         mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; | 
 | 167 |         mInfo.frameLeft = mFrame.left; | 
 | 168 |         mInfo.frameTop = mFrame.top; | 
 | 169 |         mInfo.frameRight = mFrame.right; | 
 | 170 |         mInfo.frameBottom = mFrame.bottom; | 
 | 171 |         mInfo.globalScaleFactor = 1.0; | 
 | 172 |         mInfo.touchableRegion.clear(); | 
 | 173 |         mInfo.addTouchableRegion(mFrame); | 
 | 174 |         mInfo.visible = true; | 
 | 175 |         mInfo.canReceiveKeys = true; | 
 | 176 |         mInfo.hasFocus = true; | 
 | 177 |         mInfo.hasWallpaper = false; | 
 | 178 |         mInfo.paused = false; | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 179 |         mInfo.ownerPid = INJECTOR_PID; | 
 | 180 |         mInfo.ownerUid = INJECTOR_UID; | 
 | 181 |         mInfo.inputFeatures = 0; | 
 | 182 |         mInfo.displayId = ADISPLAY_ID_DEFAULT; | 
 | 183 |  | 
 | 184 |         return true; | 
 | 185 |     } | 
 | 186 |  | 
 | 187 | protected: | 
 | 188 |     Rect mFrame; | 
 | 189 | }; | 
 | 190 |  | 
 | 191 | static MotionEvent generateMotionEvent() { | 
 | 192 |     PointerProperties pointerProperties[1]; | 
 | 193 |     PointerCoords pointerCoords[1]; | 
 | 194 |  | 
 | 195 |     pointerProperties[0].clear(); | 
 | 196 |     pointerProperties[0].id = 0; | 
 | 197 |     pointerProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; | 
 | 198 |  | 
 | 199 |     pointerCoords[0].clear(); | 
 | 200 |     pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 100); | 
 | 201 |     pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 100); | 
 | 202 |  | 
 | 203 |     const nsecs_t currentTime = now(); | 
 | 204 |  | 
 | 205 |     MotionEvent event; | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 206 |     event.initialize(DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, INVALID_HMAC, | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 207 |                      AMOTION_EVENT_ACTION_DOWN, /* actionButton */ 0, /* flags */ 0, | 
 | 208 |                      /* edgeFlags */ 0, AMETA_NONE, /* buttonState */ 0, MotionClassification::NONE, | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 209 |                      1 /* xScale */, 1 /* yScale */, | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 210 |                      /* xOffset */ 0, /* yOffset */ 0, /* xPrecision */ 0, | 
 | 211 |                      /* yPrecision */ 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
 | 212 |                      AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, currentTime, | 
 | 213 |                      /*pointerCount*/ 1, pointerProperties, pointerCoords); | 
 | 214 |     return event; | 
 | 215 | } | 
 | 216 |  | 
 | 217 | static NotifyMotionArgs generateMotionArgs() { | 
 | 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 |     // Define a valid motion event. | 
 | 231 |     NotifyMotionArgs args(/* sequenceNum */ 0, currentTime, DEVICE_ID, AINPUT_SOURCE_TOUCHSCREEN, | 
 | 232 |                           ADISPLAY_ID_DEFAULT, POLICY_FLAG_PASS_TO_USER, AMOTION_EVENT_ACTION_DOWN, | 
 | 233 |                           /* actionButton */ 0, /* flags */ 0, AMETA_NONE, /* buttonState */ 0, | 
 | 234 |                           MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1, | 
 | 235 |                           pointerProperties, pointerCoords, | 
 | 236 |                           /* xPrecision */ 0, /* yPrecision */ 0, | 
 | 237 |                           AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
 | 238 |                           AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /* videoFrames */ {}); | 
 | 239 |  | 
 | 240 |     return args; | 
 | 241 | } | 
 | 242 |  | 
 | 243 | static void benchmarkNotifyMotion(benchmark::State& state) { | 
 | 244 |     // Create dispatcher | 
 | 245 |     sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy(); | 
 | 246 |     sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy); | 
 | 247 |     dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
 | 248 |     dispatcher->start(); | 
 | 249 |  | 
 | 250 |     // Create a window that will receive motion events | 
 | 251 |     sp<FakeApplicationHandle> application = new FakeApplicationHandle(); | 
 | 252 |     sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window"); | 
 | 253 |  | 
 | 254 |     dispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT); | 
 | 255 |  | 
 | 256 |     NotifyMotionArgs motionArgs = generateMotionArgs(); | 
 | 257 |  | 
 | 258 |     for (auto _ : state) { | 
 | 259 |         // Send ACTION_DOWN | 
 | 260 |         motionArgs.action = AMOTION_EVENT_ACTION_DOWN; | 
 | 261 |         motionArgs.sequenceNum = 0; | 
 | 262 |         motionArgs.downTime = now(); | 
 | 263 |         motionArgs.eventTime = motionArgs.downTime; | 
 | 264 |         dispatcher->notifyMotion(&motionArgs); | 
 | 265 |  | 
 | 266 |         // Send ACTION_UP | 
 | 267 |         motionArgs.action = AMOTION_EVENT_ACTION_UP; | 
 | 268 |         motionArgs.sequenceNum = 1; | 
 | 269 |         motionArgs.eventTime = now(); | 
 | 270 |         dispatcher->notifyMotion(&motionArgs); | 
 | 271 |  | 
 | 272 |         window->consumeEvent(); | 
 | 273 |         window->consumeEvent(); | 
 | 274 |     } | 
 | 275 |  | 
 | 276 |     dispatcher->stop(); | 
 | 277 | } | 
 | 278 |  | 
 | 279 | static void benchmarkInjectMotion(benchmark::State& state) { | 
 | 280 |     // Create dispatcher | 
 | 281 |     sp<FakeInputDispatcherPolicy> fakePolicy = new FakeInputDispatcherPolicy(); | 
 | 282 |     sp<InputDispatcher> dispatcher = new InputDispatcher(fakePolicy); | 
 | 283 |     dispatcher->setInputDispatchMode(/*enabled*/ true, /*frozen*/ false); | 
 | 284 |     dispatcher->start(); | 
 | 285 |  | 
 | 286 |     // Create a window that will receive motion events | 
 | 287 |     sp<FakeApplicationHandle> application = new FakeApplicationHandle(); | 
 | 288 |     sp<FakeWindowHandle> window = new FakeWindowHandle(application, dispatcher, "Fake Window"); | 
 | 289 |  | 
 | 290 |     dispatcher->setInputWindows({window}, ADISPLAY_ID_DEFAULT); | 
 | 291 |  | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 292 |     for (auto _ : state) { | 
| Siarhei Vishniakou | adfd4fa | 2019-12-20 11:02:58 -0800 | [diff] [blame] | 293 |         MotionEvent event = generateMotionEvent(); | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 294 |         // Send ACTION_DOWN | 
| Siarhei Vishniakou | d078476 | 2019-11-01 15:33:48 -0700 | [diff] [blame] | 295 |         dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
 | 296 |                                      INPUT_EVENT_INJECTION_SYNC_NONE, INJECT_EVENT_TIMEOUT, | 
 | 297 |                                      POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); | 
 | 298 |  | 
 | 299 |         // Send ACTION_UP | 
 | 300 |         event.setAction(AMOTION_EVENT_ACTION_UP); | 
 | 301 |         dispatcher->injectInputEvent(&event, INJECTOR_PID, INJECTOR_UID, | 
 | 302 |                                      INPUT_EVENT_INJECTION_SYNC_NONE, INJECT_EVENT_TIMEOUT, | 
 | 303 |                                      POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER); | 
 | 304 |  | 
 | 305 |         window->consumeEvent(); | 
 | 306 |         window->consumeEvent(); | 
 | 307 |     } | 
 | 308 |  | 
 | 309 |     dispatcher->stop(); | 
 | 310 | } | 
 | 311 |  | 
 | 312 | BENCHMARK(benchmarkNotifyMotion); | 
 | 313 | BENCHMARK(benchmarkInjectMotion); | 
 | 314 |  | 
 | 315 | } // namespace android::inputdispatcher | 
 | 316 |  | 
 | 317 | BENCHMARK_MAIN(); |