android: Measure outcomes of input hint yielding from the messageloop

Once the message_pump_android yields, record a histogram on two possible
outcomes: (a) handling the input event in the CompositorViewHolder, (b)
returning back to the native task queue.

This metric will allow us to do 2 things:
1. Confirm that the yield is not completely useless in the field
2. See how this measure of low-level usefulness of the yield responds to
   different throttling levels. This may help to adjust throttling.

In my local experiments with a non-optimized Release build on Pixel 8
(running Android V, of course) both of the above outcomes are
approximately similarly likely, even when touching only the content
area. Surely, in the field we should expect the outcome (b) is going to
happen more often than (a).

Another possibility is to periodically record task queue sizes or task
batch lengths to confirm that they become shorter with yielding. After
some local experimentation I am not convinced that this approach is
going to be effective. Input events are relatively rare among the
numerous tasks on the UI thread. The assumed power of input hint yield
is not in affecting many tasks, but rather in acting precisely when
needed. I'd suggest leaving this option for later.

Bug: 326417367
Change-Id: I0dfbea91e221613447c08797ddde49826f9772ef
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5942697
Commit-Queue: Egor Pasko <[email protected]>
Reviewed-by: Michael Thiessen <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1371966}
diff --git a/base/message_loop/message_pump_android.cc b/base/message_loop/message_pump_android.cc
index 2e669a52..20c0fbb 100644
--- a/base/message_loop/message_pump_android.cc
+++ b/base/message_loop/message_pump_android.cc
@@ -27,6 +27,9 @@
 #include "base/time/time.h"
 #include "build/build_config.h"
 
+using base::android::InputHintChecker;
+using base::android::InputHintResult;
+
 namespace base {
 
 namespace {
@@ -117,6 +120,7 @@
 }
 
 void MessagePumpAndroid::OnDelayedLooperCallback() {
+  OnReturnFromLooper();
   // There may be non-Chromium callbacks on the same ALooper which may have left
   // a pending exception set, and ALooper does not check for this between
   // callbacks. Check here, and if there's already an exception, just skip this
@@ -166,6 +170,7 @@
 }
 
 void MessagePumpAndroid::OnNonDelayedLooperCallback() {
+  OnReturnFromLooper();
   // There may be non-Chromium callbacks on the same ALooper which may have left
   // a pending exception set, and ALooper does not check for this between
   // callbacks. Check here, and if there's already an exception, just skip this
@@ -222,7 +227,8 @@
     // multi-window cases, or when a previous value is cached to throttle
     // polling the input channel.
     if (is_type_ui_ && next_work_info.is_immediate() &&
-        android::InputHintChecker::HasInput()) {
+        InputHintChecker::HasInput()) {
+      InputHintChecker::GetInstance().set_is_after_input_yield(true);
       ScheduleWork();
       return;
     }
@@ -339,6 +345,17 @@
   DPCHECK(ret >= 0);
 }
 
+void MessagePumpAndroid::OnReturnFromLooper() {
+  if (!is_type_ui_) {
+    return;
+  }
+  auto& checker = InputHintChecker::GetInstance();
+  if (checker.is_after_input_yield()) {
+    InputHintChecker::RecordInputHintResult(InputHintResult::kBackToNative);
+  }
+  checker.set_is_after_input_yield(false);
+}
+
 void MessagePumpAndroid::ScheduleDelayedWork(
     const Delegate::NextWorkInfo& next_work_info) {
   if (ShouldQuit())