Move scroll_jank to a full TBMv3/perfetto metric.

Add a proto to store the main results, and compute the new proto as an output
table. You can see this in action:
https://screenshot.googleplex.com/7BYQuQHhxt6sEGU

Bug: 182162821
Change-Id: If84d6d639b381ce2a6e63a6f5523cec38a76b4e4
diff --git a/Android.bp b/Android.bp
index 10d3039..e525314 100644
--- a/Android.bp
+++ b/Android.bp
@@ -3660,6 +3660,7 @@
     "protos/perfetto/metrics/chrome/all_chrome_metrics.proto",
     "protos/perfetto/metrics/chrome/frame_times.proto",
     "protos/perfetto/metrics/chrome/reported_by_page.proto",
+    "protos/perfetto/metrics/chrome/scroll_jank.proto",
     "protos/perfetto/metrics/chrome/test_chrome_metric.proto",
     "protos/perfetto/metrics/custom_options.proto",
     "protos/perfetto/metrics/metrics.proto",
diff --git a/BUILD b/BUILD
index 80fb3be..b0129be 100644
--- a/BUILD
+++ b/BUILD
@@ -2399,6 +2399,7 @@
         "protos/perfetto/metrics/chrome/all_chrome_metrics.proto",
         "protos/perfetto/metrics/chrome/frame_times.proto",
         "protos/perfetto/metrics/chrome/reported_by_page.proto",
+        "protos/perfetto/metrics/chrome/scroll_jank.proto",
         "protos/perfetto/metrics/chrome/test_chrome_metric.proto",
     ],
     visibility = [
diff --git a/protos/perfetto/metrics/chrome/BUILD.gn b/protos/perfetto/metrics/chrome/BUILD.gn
index 41712a0..10b0f08 100644
--- a/protos/perfetto/metrics/chrome/BUILD.gn
+++ b/protos/perfetto/metrics/chrome/BUILD.gn
@@ -24,6 +24,7 @@
     "all_chrome_metrics.proto",
     "frame_times.proto",
     "reported_by_page.proto",
+    "scroll_jank.proto",
     "test_chrome_metric.proto",
   ]
 }
diff --git a/protos/perfetto/metrics/chrome/all_chrome_metrics.proto b/protos/perfetto/metrics/chrome/all_chrome_metrics.proto
index 7850972..69170ba 100644
--- a/protos/perfetto/metrics/chrome/all_chrome_metrics.proto
+++ b/protos/perfetto/metrics/chrome/all_chrome_metrics.proto
@@ -20,6 +20,7 @@
 
 import "protos/perfetto/metrics/metrics.proto";
 import "protos/perfetto/metrics/chrome/frame_times.proto";
+import "protos/perfetto/metrics/chrome/scroll_jank.proto";
 import "protos/perfetto/metrics/chrome/test_chrome_metric.proto";
 import "protos/perfetto/metrics/chrome/reported_by_page.proto";
 
@@ -27,4 +28,5 @@
   optional TestChromeMetric test_chrome_metric = 1001;
   optional FrameTimes frame_times = 1002;
   optional ReportedByPage reported_by_page = 1003;
+  optional ScrollJank scroll_jank = 1004;
 }
diff --git a/protos/perfetto/metrics/chrome/scroll_jank.proto b/protos/perfetto/metrics/chrome/scroll_jank.proto
new file mode 100644
index 0000000..6fea1d8
--- /dev/null
+++ b/protos/perfetto/metrics/chrome/scroll_jank.proto
@@ -0,0 +1,33 @@
+/*
+ * 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.
+ */
+
+syntax = "proto2";
+
+package perfetto.protos;
+
+import "protos/perfetto/metrics/custom_options.proto";
+
+message ScrollJank {
+  // The percentage of time we consider janky of the total time spent scrolling
+  // during the trace. I.E. approximately equal to |scroll_jank_ms|/|scroll_ms|.
+  optional double scroll_jank_percentage = 1 [(unit) = "n%_smallerIsBetter"];
+  optional double scroll_ms = 2 [(unit) = "ms_biggerIsBetter"];
+  optional double scroll_processing_ms = 3 [(unit) = "ms_biggerIsBetter"];
+  optional double scroll_jank_processing_ms = 4 [(unit) = "ms_smallerIsBetter"];
+  optional int64 num_scroll_update_count = 5 [(unit) = "count_biggerIsBetter"];
+  optional int64 num_scroll_update_jank_count = 6
+      [(unit) = "count_smallerIsBetter"];
+}
diff --git a/src/trace_processor/metrics/chrome/scroll_jank.sql b/src/trace_processor/metrics/chrome/scroll_jank.sql
index f3092c7..0564bd5 100644
--- a/src/trace_processor/metrics/chrome/scroll_jank.sql
+++ b/src/trace_processor/metrics/chrome/scroll_jank.sql
@@ -262,9 +262,42 @@
 DROP VIEW IF EXISTS scroll_jank;
 CREATE VIEW scroll_jank AS
   SELECT
-    *,
+    id AS slice_id,
     (next_jank IS NOT NULL AND next_jank) OR
     (prev_jank IS NOT NULL AND prev_jank)
-    AS jank
+    AS jank,
+    *
   FROM scroll_jank_maybe_null_prev_and_next
   ORDER BY gesture_scroll_id ASC, ts ASC;
+
+DROP VIEW IF EXISTS scroll_jank_ms;
+
+DROP VIEW IF EXISTS scroll_jank_output;
+CREATE VIEW scroll_jank_output AS
+  SELECT
+    ScrollJank(
+      'scroll_jank_percentage', (
+        SELECT
+          (
+            SUM(CASE WHEN jank THEN dur ELSE 0 END)/CAST(SUM(dur) AS REAL)
+          ) * 100.0
+        FROM scroll_jank
+      ),
+      'scroll_ms', (
+        SELECT
+          CAST(SUM(scroll_dur)/1e6 AS REAL)
+        FROM (
+          SELECT
+            MAX(scroll_dur) AS scroll_dur
+          FROM scroll_jank
+          GROUP BY gesture_scroll_id
+        )
+      ),
+      'scroll_processing_ms', CAST(SUM(dur)/1e6 AS REAL),
+      'scroll_jank_processing_ms', (
+        SELECT CAST(SUM(dur)/1e6 AS REAL) FROM scroll_jank WHERE jank
+      ),
+      'num_scroll_update_count', COUNT(*),
+      'num_scroll_update_jank_count', SUM(jank)
+    )
+  FROM scroll_jank;