/* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef _UI_INPUT_INPUTDISPATCHER_LATENCYTRACKER_H #define _UI_INPUT_INPUTDISPATCHER_LATENCYTRACKER_H #include #include #include #include #include "InputEventTimeline.h" namespace android::inputdispatcher { /** * Maintain a record for input events that are received by InputDispatcher, sent out to the apps, * and processed by the apps. Once an event becomes "mature" (older than the ANR timeout), report * the entire input event latency history to the reporting function. * * All calls to LatencyTracker should come from the same thread. It is not thread-safe. */ class LatencyTracker { public: /** * Create a LatencyTracker. * param reportingFunction: the function that will be called in order to report full latency. */ LatencyTracker(InputEventTimelineProcessor* processor); /** * Start keeping track of an event identified by inputEventId. This must be called first. */ void trackListener(int32_t inputEventId, bool isDown, nsecs_t eventTime, nsecs_t readTime); void trackFinishedEvent(int32_t inputEventId, const sp& connectionToken, nsecs_t deliveryTime, nsecs_t consumeTime, nsecs_t finishTime); void trackGraphicsLatency(int32_t inputEventId, const sp& connectionToken, std::array timeline); /** * Report all collected events immediately, even if some of them are currently incomplete * and may receive 'trackFinishedEvent' or 'trackGraphicsLatency' calls in the future. * This is useful for tests. Otherwise, tests would have to inject additional "future" events, * which is not convenient. */ void reportNow(); std::string dump(const char* prefix); private: /** * A collection of InputEventTimelines keyed by inputEventId. An InputEventTimeline is first * created when 'trackListener' is called. * When either 'trackFinishedEvent' or 'trackGraphicsLatency' is called for this input event, * the corresponding InputEventTimeline will be updated for that token. */ std::unordered_map mTimelines; /** * The collection of eventTimes will help us quickly find the events that we should prune * from the 'mTimelines'. Since 'mTimelines' is keyed by inputEventId, it would be inefficient * to walk through it directly to find the oldest input events to get rid of. * There is a 1:1 mapping between 'mTimelines' and 'mEventTimes'. * We are using 'multimap' instead of 'map' because there could be more than 1 event with the * same eventTime. */ std::multimap mEventTimes; InputEventTimelineProcessor* mTimelineProcessor; void reportAndPruneMatureRecords(nsecs_t newEventTime); }; } // namespace android::inputdispatcher #endif // _UI_INPUT_INPUTDISPATCHER_LATENCYTRACKER_H