| /* | |
| * Copyright 2026 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. | |
| */ | |
| #pragma once | |
| #include <deque> | |
| #include <optional> | |
| #include <string> | |
| #include <unordered_map> | |
| #include <vector> | |
| #include <android/os/IInputConstants.h> | |
| #include <com_android_input_flags.h> | |
| #include <input/InputDeviceUsageSource.h> | |
| #include <input/InputTransport.h> | |
| namespace android { | |
| struct JankFrameTelemetry { | |
| int32_t latestInputEventId = | |
| android::os::IInputConstants::INVALID_INPUT_EVENT_ID; | |
| nsecs_t latestInputEventTimestamp = 0; | |
| int32_t oldestInputEventId = | |
| android::os::IInputConstants::INVALID_INPUT_EVENT_ID; | |
| nsecs_t oldestInputEventTimestamp = 0; | |
| int32_t consumedInputEventCount = 0; | |
| int32_t totalFrameCount = 0; | |
| int32_t jankFrameCount = 0; | |
| int32_t inputSource = | |
| static_cast<int32_t>(InputDeviceUsageSource::UNKNOWN); | |
| }; | |
| class InputJankTracker { | |
| public: | |
| // Tracks long-term persistent cumulative jank stats for a specific input device | |
| class DeviceStats { | |
| public: | |
| DeviceStats() : mDeviceId(-1) {} | |
| DeviceStats(int32_t deviceId) : mDeviceId(deviceId) {} | |
| void updateAndReport(size_t gestureJankCount, size_t gestureTotalCount, | |
| const std::string& logTag); | |
| void setLastFrameTelemetry(const JankFrameTelemetry& telemetry) { | |
| mLastFrameTelemetry = telemetry; | |
| } | |
| int32_t getDeviceId() const { return mDeviceId; } | |
| size_t getOverallJankCount() const { return mOverallJankCount; } | |
| size_t getOverallTotalCount() const { return mOverallTotalCount; } | |
| const JankFrameTelemetry& getLastFrameTelemetry() const { return mLastFrameTelemetry; } | |
| private: | |
| int32_t mDeviceId; | |
| size_t mOverallJankCount = 0; | |
| size_t mOverallTotalCount = 0; | |
| JankFrameTelemetry mLastFrameTelemetry; | |
| }; | |
| InputJankTracker(const std::string& logTag); | |
| void processConsumedMotion(const InputMessage::Body::Motion& motion); | |
| void onNewFrame(nsecs_t requestedFrameTime); | |
| JankFrameTelemetry getLastFrameTelemetry() const { | |
| if (!com::android::input::flags::enable_input_jank_tracker()) { | |
| return JankFrameTelemetry{}; | |
| } | |
| for (const auto& [deviceId, tracker] : mActiveGestures) { | |
| auto telemetry = tracker.getLatestEvaluatedTelemetry(); | |
| if (telemetry.has_value()) { | |
| return *telemetry; | |
| } | |
| } | |
| return JankFrameTelemetry{}; | |
| } | |
| const DeviceStats* getDeviceStats(std::optional<int32_t> deviceId = std::nullopt) const { | |
| if (deviceId.has_value()) { | |
| auto it = mDeviceStats.find(*deviceId); | |
| return it != mDeviceStats.end() ? &it->second : nullptr; | |
| } | |
| return mDeviceStats.empty() ? nullptr : &mDeviceStats.begin()->second; | |
| } | |
| std::vector<DeviceStats> getAllDeviceStats() const { | |
| std::vector<DeviceStats> result; | |
| result.reserve(mDeviceStats.size()); | |
| for (const auto& [id, stats] : mDeviceStats) { | |
| result.push_back(stats); | |
| } | |
| return result; | |
| } | |
| private: | |
| /** | |
| * Called on ACTION_DOWN to begin tracking a new gesture for the given device. | |
| * | |
| * Divides input consumption into contiguous frame intervals [Frame.start, Frame.end]. | |
| * Normal frame boundaries are defined by consecutive Choreographer VSync times | |
| * (mLastFrameTime to mCurrentFrameTime). | |
| * | |
| * Because the start of a gesture is not aligned with VSync, `startTime` (from ACTION_DOWN | |
| * eventTime) is used to establish the starting boundary for the first frame interval | |
| * [startTime, firstVsyncTime]. | |
| */ | |
| void onGestureStart(int32_t deviceId, InputDeviceUsageSource inputSource, nsecs_t startTime); | |
| /** | |
| * Called on ACTION_UP or ACTION_CANCEL to terminate tracking for the active gesture. | |
| * | |
| * Because the end of a gesture is not aligned with VSync, `endTime` (from ACTION_UP/CANCEL | |
| * eventTime) is used to establish the terminal boundary for the last frame interval | |
| * [lastVsyncTime, max(endTime, mCurrentFrameTime)]. | |
| * | |
| * Evaluates jank for the concluding frame, updates cumulative DeviceStats, and releases | |
| * gesture tracking resources. | |
| */ | |
| void onGestureEnd(int32_t deviceId, nsecs_t endTime); | |
| void onEventConsumed(int32_t deviceId, InputDeviceUsageSource inputSource, int32_t id, | |
| int32_t action, nsecs_t eventTime); | |
| // Single-gesture tracker for a specific input device | |
| class GestureTracker { | |
| public: | |
| GestureTracker(InputDeviceUsageSource source, const std::string& logTag); | |
| void onNewFrame(nsecs_t requestedFrameTime, DeviceStats& deviceStats); | |
| void onEventConsumed(int32_t id, int32_t action, nsecs_t eventTime); | |
| void onGestureEnd(nsecs_t endTime, const std::string& logTag, DeviceStats& deviceStats); | |
| InputDeviceUsageSource getSource() const { return mSource; } | |
| nsecs_t getCurrentFrameTime() const { return mCurrentFrameTime; } | |
| std::optional<JankFrameTelemetry> getLatestEvaluatedTelemetry() const { | |
| return mLatestEvaluatedTelemetry; | |
| } | |
| private: | |
| struct FrameRecord { | |
| nsecs_t start; | |
| nsecs_t end; | |
| size_t consumedCount = 0; | |
| size_t expectedCount = 0; | |
| }; | |
| struct EventRecord { | |
| int32_t id; | |
| nsecs_t eventTime; | |
| int32_t action; | |
| }; | |
| InputDeviceUsageSource mSource; | |
| std::string mLogTag; | |
| size_t mAccumulatedJankCount = 0; | |
| size_t mAccumulatedTotalCount = 0; | |
| nsecs_t mCurrentFrameTime = -1; | |
| nsecs_t mLastFrameTime = -1; | |
| std::deque<FrameRecord> mPendingFrames; | |
| std::vector<EventRecord> mConsumedEvents; | |
| std::optional<JankFrameTelemetry> mLatestEvaluatedTelemetry; | |
| void evaluateFrameJank(nsecs_t frameTime, bool isGestureEnd, DeviceStats& deviceStats); | |
| private: | |
| struct JankEvaluationResult { | |
| size_t jankCount = 0; | |
| size_t totalCount = 0; | |
| std::deque<FrameRecord> remainingFrames; | |
| }; | |
| void mapEventsToPendingFrames(); | |
| JankEvaluationResult determineJank(bool isGestureEnd); | |
| void updateTelemetry(size_t totalCount, size_t jankCount, DeviceStats& deviceStats); | |
| void logJank(nsecs_t frameTime, size_t jankCount, size_t totalCount) const; | |
| }; | |
| std::string mLogTag; | |
| std::unordered_map<int32_t /*deviceId*/, GestureTracker> mActiveGestures; | |
| std::unordered_map<int32_t /*deviceId*/, DeviceStats> mDeviceStats; | |
| DeviceStats& getOrCreateDeviceStats(int32_t deviceId); | |
| }; | |
| } // namespace android |