Add trace debug for onTouch/onTouchEvent
In order to measure and clarify the jank caused by application when
handling View.onTouchListener#onTouch and View#onTouchEvent slowly.
Bug: 256549451
Test: test with PTS CUJ tests and verify if onTouch/onTouchEvent shown
in the perfetto trace when enabling "view" trace tag in ftrace_config
Change-Id: I030f038ca57846bc9e7e66dc7e8bcbf8ffcd3d1e
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index 0d2c2cc..62d5cae 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -15858,20 +15858,7 @@
}
if (onFilterTouchEventForSecurity(event)) {
- if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
- result = true;
- }
- //noinspection SimplifiableIfStatement
- ListenerInfo li = mListenerInfo;
- if (li != null && li.mOnTouchListener != null
- && (mViewFlags & ENABLED_MASK) == ENABLED
- && li.mOnTouchListener.onTouch(this, event)) {
- result = true;
- }
-
- if (!result && onTouchEvent(event)) {
- result = true;
- }
+ result = performOnTouchCallback(event);
}
if (!result && mInputEventConsistencyVerifier != null) {
@@ -15890,6 +15877,36 @@
return result;
}
+ /**
+ * Returns {@code true} if the {@link MotionEvent} from {@link #dispatchTouchEvent} was
+ * handled by this view.
+ */
+ private boolean performOnTouchCallback(MotionEvent event) {
+ boolean handled = false;
+ if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
+ handled = true;
+ }
+ //noinspection SimplifiableIfStatement
+ ListenerInfo li = mListenerInfo;
+ if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED) {
+ try {
+ Trace.traceBegin(Trace.TRACE_TAG_VIEW, "View.onTouchListener#onTouch");
+ handled = li.mOnTouchListener.onTouch(this, event);
+ } finally {
+ Trace.traceEnd(Trace.TRACE_TAG_VIEW);
+ }
+ }
+ if (handled) {
+ return true;
+ }
+ try {
+ Trace.traceBegin(Trace.TRACE_TAG_VIEW, "View#onTouchEvent");
+ return onTouchEvent(event);
+ } finally {
+ Trace.traceEnd(Trace.TRACE_TAG_VIEW);
+ }
+ }
+
boolean isAccessibilityFocusedViewOrHost() {
return isAccessibilityFocused() || (getViewRootImpl() != null && getViewRootImpl()
.getAccessibilityFocusedHost() == this);