1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
cc / metrics / event_metrics.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "cc/metrics/event_metrics.h"
#include <algorithm>
#include <ostream>
#include <string>
#include <utility>
#include "base/check.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "base/time/default_tick_clock.h"
#include "cc/metrics/event_latency_tracing_recorder.h"
namespace cc {
namespace {
// Histogram bucketing for scroll event metrics.
constexpr base::TimeDelta kScrollHistogramMin = base::Milliseconds(4);
constexpr base::TimeDelta kScrollHistogramMax = base::Milliseconds(500);
constexpr size_t kScrollHistogramBucketCount = 50;
constexpr struct {
EventMetrics::EventType metrics_event_type;
const char* name;
ui::EventType ui_event_type;
std::optional<bool> scroll_is_inertial = std::nullopt;
std::optional<ScrollUpdateEventMetrics::ScrollUpdateType> scroll_update_type =
std::nullopt;
std::optional<EventMetrics::HistogramBucketing> histogram_bucketing =
std::nullopt;
} kInterestingEvents[] = {
#define EVENT_TYPE(type_name, ui_type, ...) \
{ \
.metrics_event_type = EventMetrics::EventType::k##type_name, \
.name = #type_name, .ui_event_type = ui_type, __VA_ARGS__ \
}
EVENT_TYPE(MousePressed, ui::EventType::kMousePressed),
EVENT_TYPE(MouseReleased, ui::EventType::kMouseReleased),
EVENT_TYPE(MouseWheel, ui::EventType::kMousewheel),
EVENT_TYPE(KeyPressed, ui::EventType::kKeyPressed),
EVENT_TYPE(KeyReleased, ui::EventType::kKeyReleased),
EVENT_TYPE(TouchPressed, ui::EventType::kTouchPressed),
EVENT_TYPE(TouchReleased, ui::EventType::kTouchReleased),
EVENT_TYPE(TouchMoved, ui::EventType::kTouchMoved),
EVENT_TYPE(GestureScrollBegin,
ui::EventType::kGestureScrollBegin,
.scroll_is_inertial = false,
.histogram_bucketing = {{.min = kScrollHistogramMin,
.max = kScrollHistogramMax,
.count = kScrollHistogramBucketCount,
.version_suffix = "2"}}),
EVENT_TYPE(GestureScrollUpdate,
ui::EventType::kGestureScrollUpdate,
.scroll_is_inertial = false,
.scroll_update_type =
ScrollUpdateEventMetrics::ScrollUpdateType::kContinued,
.histogram_bucketing = {{.min = kScrollHistogramMin,
.max = kScrollHistogramMax,
.count = kScrollHistogramBucketCount,
.version_suffix = "2"}}),
EVENT_TYPE(GestureScrollEnd,
ui::EventType::kGestureScrollEnd,
.scroll_is_inertial = false,
.histogram_bucketing = {{.min = kScrollHistogramMin,
.max = kScrollHistogramMax,
.count = kScrollHistogramBucketCount,
.version_suffix = "2"}}),
EVENT_TYPE(GestureDoubleTap, ui::EventType::kGestureDoubleTap),
EVENT_TYPE(GestureLongPress, ui::EventType::kGestureLongPress),
EVENT_TYPE(GestureLongTap, ui::EventType::kGestureLongTap),
EVENT_TYPE(GestureShowPress, ui::EventType::kGestureShowPress),
EVENT_TYPE(GestureTap, ui::EventType::kGestureTap),
EVENT_TYPE(GestureTapCancel, ui::EventType::kGestureTapCancel),
EVENT_TYPE(GestureTapDown, ui::EventType::kGestureTapDown),
EVENT_TYPE(GestureTapUnconfirmed, ui::EventType::kGestureTapUnconfirmed),
EVENT_TYPE(GestureTwoFingerTap, ui::EventType::kGestureTwoFingerTap),
EVENT_TYPE(FirstGestureScrollUpdate,
ui::EventType::kGestureScrollUpdate,
.scroll_is_inertial = false,
.scroll_update_type =
ScrollUpdateEventMetrics::ScrollUpdateType::kStarted,
.histogram_bucketing = {{.min = kScrollHistogramMin,
.max = kScrollHistogramMax,
.count = kScrollHistogramBucketCount,
.version_suffix = "2"}}),
EVENT_TYPE(MouseDragged, ui::EventType::kMouseDragged),
EVENT_TYPE(GesturePinchBegin, ui::EventType::kGesturePinchBegin),
EVENT_TYPE(GesturePinchEnd, ui::EventType::kGesturePinchEnd),
EVENT_TYPE(GesturePinchUpdate, ui::EventType::kGesturePinchUpdate),
EVENT_TYPE(InertialGestureScrollUpdate,
ui::EventType::kGestureScrollUpdate,
.scroll_is_inertial = true,
.scroll_update_type =
ScrollUpdateEventMetrics::ScrollUpdateType::kContinued,
.histogram_bucketing = {{.min = kScrollHistogramMin,
.max = kScrollHistogramMax,
.count = kScrollHistogramBucketCount,
.version_suffix = "2"}}),
EVENT_TYPE(MouseMoved, ui::EventType::kMouseMoved),
#undef EVENT_TYPE
};
static_assert(std::size(kInterestingEvents) ==
static_cast<int>(EventMetrics::EventType::kMaxValue) + 1,
"EventMetrics::EventType has changed.");
constexpr struct {
ScrollEventMetrics::ScrollType metrics_scroll_type;
ui::ScrollInputType ui_input_type;
const char* name;
} kScrollTypes[] = {
#define SCROLL_TYPE(name) \
{ \
ScrollEventMetrics::ScrollType::k##name, ui::ScrollInputType::k##name, \
#name \
}
SCROLL_TYPE(Autoscroll),
SCROLL_TYPE(Scrollbar),
SCROLL_TYPE(Touchscreen),
SCROLL_TYPE(Wheel),
#undef SCROLL_TYPE
};
static_assert(std::size(kScrollTypes) ==
static_cast<int>(ScrollEventMetrics::ScrollType::kMaxValue) +
1,
"ScrollEventMetrics::ScrollType has changed.");
constexpr struct {
PinchEventMetrics::PinchType metrics_pinch_type;
ui::ScrollInputType ui_input_type;
const char* name;
} kPinchTypes[] = {
#define PINCH_TYPE(metrics_name, ui_name) \
{ \
PinchEventMetrics::PinchType::k##metrics_name, \
ui::ScrollInputType::k##ui_name, #metrics_name \
}
PINCH_TYPE(Touchpad, Wheel),
PINCH_TYPE(Touchscreen, Touchscreen),
#undef PINCH_TYPE
};
static_assert(std::size(kPinchTypes) ==
static_cast<int>(PinchEventMetrics::PinchType::kMaxValue) + 1,
"PinchEventMetrics::PinchType has changed.");
std::optional<EventMetrics::EventType> ToInterestingEventType(
ui::EventType ui_event_type,
std::optional<bool> scroll_is_inertial,
std::optional<ScrollUpdateEventMetrics::ScrollUpdateType>
scroll_update_type) {
for (size_t i = 0; i < std::size(kInterestingEvents); i++) {
const auto& interesting_event = kInterestingEvents[i];
if (ui_event_type == interesting_event.ui_event_type &&
scroll_is_inertial == interesting_event.scroll_is_inertial &&
scroll_update_type == interesting_event.scroll_update_type) {
EventMetrics::EventType metrics_event_type =
static_cast<EventMetrics::EventType>(i);
DCHECK_EQ(metrics_event_type, interesting_event.metrics_event_type);
return metrics_event_type;
}
}
return std::nullopt;
}
ScrollEventMetrics::ScrollType ToScrollType(ui::ScrollInputType ui_input_type) {
for (size_t i = 0; i < std::size(kScrollTypes); i++) {
if (ui_input_type == kScrollTypes[i].ui_input_type) {
auto metrics_scroll_type = static_cast<ScrollEventMetrics::ScrollType>(i);
DCHECK_EQ(metrics_scroll_type, kScrollTypes[i].metrics_scroll_type);
return metrics_scroll_type;
}
}
NOTREACHED();
}
PinchEventMetrics::PinchType ToPinchType(ui::ScrollInputType ui_input_type) {
for (size_t i = 0; i < std::size(kPinchTypes); i++) {
if (ui_input_type == kPinchTypes[i].ui_input_type) {
auto metrics_pinch_type = static_cast<PinchEventMetrics::PinchType>(i);
DCHECK_EQ(metrics_pinch_type, kPinchTypes[i].metrics_pinch_type);
return metrics_pinch_type;
}
}
NOTREACHED();
}
bool IsGestureScroll(ui::EventType type) {
return type == ui::EventType::kGestureScrollBegin ||
type == ui::EventType::kGestureScrollUpdate ||
type == ui::EventType::kGestureScrollEnd;
}
bool IsGesturePinch(ui::EventType type) {
return type == ui::EventType::kGesturePinchBegin ||
type == ui::EventType::kGesturePinchUpdate ||
type == ui::EventType::kGesturePinchEnd;
}
bool IsGestureScrollUpdate(ui::EventType type) {
return type == ui::EventType::kGestureScrollUpdate;
}
} // namespace
// EventMetrics:
// static
std::unique_ptr<EventMetrics> EventMetrics::Create(
ui::EventType type,
base::TimeTicks timestamp,
std::optional<TraceId> trace_id) {
return Create(type, timestamp, base::TimeTicks(), trace_id);
}
// static
std::unique_ptr<EventMetrics> EventMetrics::Create(
ui::EventType type,
base::TimeTicks timestamp,
base::TimeTicks arrived_in_browser_main_timestamp,
std::optional<TraceId> trace_id) {
// TODO(crbug.com/40160689): We expect that `timestamp` is not null, but there
// seems to be some tests that are emitting events with null timestamp. We
// should investigate and try to fix those cases and add a `DCHECK` here to
// assert `timestamp` is not null.
DCHECK(!IsGestureScroll(type) && !IsGesturePinch(type));
std::unique_ptr<EventMetrics> metrics =
CreateInternal(type, timestamp, arrived_in_browser_main_timestamp,
base::DefaultTickClock::GetInstance(), trace_id);
if (!metrics)
return nullptr;
metrics->SetDispatchStageTimestamp(
DispatchStage::kArrivedInRendererCompositor);
return metrics;
}
// static
std::unique_ptr<EventMetrics> EventMetrics::CreateForTesting(
ui::EventType type,
base::TimeTicks timestamp,
base::TimeTicks arrived_in_browser_main_timestamp,
const base::TickClock* tick_clock,
std::optional<TraceId> trace_id) {
DCHECK(!timestamp.is_null());
std::unique_ptr<EventMetrics> metrics =
CreateInternal(type, timestamp, base::TimeTicks(), tick_clock, trace_id);
if (!metrics)
return nullptr;
metrics->SetDispatchStageTimestamp(
DispatchStage::kArrivedInRendererCompositor);
return metrics;
}
// static
std::unique_ptr<EventMetrics> EventMetrics::CreateFromExisting(
ui::EventType type,
DispatchStage last_dispatch_stage,
const EventMetrics* existing) {
// Generally, if `existing` is `nullptr` (the existing event is not of an
// interesting type), the new event won't be of an interesting type, too, and
// we can immediately return `nullptr`. The only exception is some tests that
// are not interested in reporting metrics, in which case we can immediately
// return `nullptr`, too, as they are not interested in reporting metrics.
if (!existing)
return nullptr;
std::unique_ptr<EventMetrics> metrics =
CreateInternal(type, base::TimeTicks(), base::TimeTicks(),
existing->tick_clock_, std::nullopt);
if (!metrics)
return nullptr;
// Use timestamps of all stages (including "Generated" stage) up to
// `last_dispatch_stage` from `existing`.
metrics->CopyTimestampsFrom(*existing, last_dispatch_stage);
return metrics;
}
// static
std::unique_ptr<EventMetrics> EventMetrics::CreateInternal(
ui::EventType type,
base::TimeTicks timestamp,
base::TimeTicks arrived_in_browser_main_timestamp,
const base::TickClock* tick_clock,
std::optional<TraceId> trace_id) {
std::optional<EventType> interesting_type =
ToInterestingEventType(type, /*scroll_is_inertial=*/std::nullopt,
/*scroll_update_type=*/std::nullopt);
if (!interesting_type)
return nullptr;
return base::WrapUnique(new EventMetrics(*interesting_type, timestamp,
arrived_in_browser_main_timestamp,
tick_clock, trace_id));
}
EventMetrics::EventMetrics(EventType type,
base::TimeTicks timestamp,
const base::TickClock* tick_clock,
std::optional<TraceId> trace_id)
: type_(type), tick_clock_(tick_clock), trace_id_(trace_id) {
dispatch_stage_timestamps_[static_cast<int>(DispatchStage::kGenerated)] =
timestamp;
}
EventMetrics::EventMetrics(EventType type,
base::TimeTicks timestamp,
base::TimeTicks arrived_in_browser_main_timestamp,
const base::TickClock* tick_clock,
std::optional<TraceId> trace_id)
: EventMetrics(type, timestamp, tick_clock, trace_id) {
dispatch_stage_timestamps_[static_cast<int>(
DispatchStage::kArrivedInBrowserMain)] =
arrived_in_browser_main_timestamp;
}
EventMetrics::EventMetrics(const EventMetrics& other)
: type_(other.type_),
tick_clock_(other.tick_clock_),
should_record_tracing_(false) {
CopyTimestampsFrom(other, DispatchStage::kMaxValue);
}
EventMetrics::~EventMetrics() {
if (should_record_tracing()) {
EventLatencyTracingRecorder::RecordEventLatencyTraceEvent(
this, base::TimeTicks::Now(), base::TimeDelta(), nullptr, nullptr);
}
}
const char* EventMetrics::GetTypeName() const {
return GetTypeName(type_);
}
// static
const char* EventMetrics::GetTypeName(EventMetrics::EventType type) {
return kInterestingEvents[static_cast<int>(type)].name;
}
const std::optional<EventMetrics::HistogramBucketing>&
EventMetrics::GetHistogramBucketing() const {
return kInterestingEvents[static_cast<int>(type_)].histogram_bucketing;
}
void EventMetrics::SetHighLatencyStage(const std::string& stage) {
high_latency_stages_.push_back(stage);
}
void EventMetrics::SetDispatchStageTimestamp(DispatchStage stage) {
DCHECK(dispatch_stage_timestamps_[static_cast<size_t>(stage)].is_null());
dispatch_stage_timestamps_[static_cast<size_t>(stage)] =
tick_clock_->NowTicks();
}
void EventMetrics::SetDispatchStageTimestamp(DispatchStage stage,
base::TimeTicks timestamp) {
DCHECK(dispatch_stage_timestamps_[static_cast<size_t>(stage)].is_null());
dispatch_stage_timestamps_[static_cast<size_t>(stage)] = timestamp;
}
base::TimeTicks EventMetrics::GetDispatchStageTimestamp(
DispatchStage stage) const {
return dispatch_stage_timestamps_[static_cast<size_t>(stage)];
}
void EventMetrics::ResetToDispatchStage(DispatchStage stage) {
for (size_t stage_index = static_cast<size_t>(stage) + 1;
stage_index <= static_cast<size_t>(DispatchStage::kMaxValue);
stage_index++) {
dispatch_stage_timestamps_[stage_index] = base::TimeTicks();
}
}
bool EventMetrics::HasSmoothInputEvent() const {
return type_ == EventType::kMouseDragged || type_ == EventType::kTouchMoved;
}
ScrollEventMetrics* EventMetrics::AsScroll() {
return nullptr;
}
const ScrollEventMetrics* EventMetrics::AsScroll() const {
return const_cast<EventMetrics*>(this)->AsScroll();
}
ScrollUpdateEventMetrics* EventMetrics::AsScrollUpdate() {
return nullptr;
}
const ScrollUpdateEventMetrics* EventMetrics::AsScrollUpdate() const {
return const_cast<EventMetrics*>(this)->AsScrollUpdate();
}
PinchEventMetrics* EventMetrics::AsPinch() {
return nullptr;
}
const PinchEventMetrics* EventMetrics::AsPinch() const {
return const_cast<EventMetrics*>(this)->AsPinch();
}
std::unique_ptr<EventMetrics> EventMetrics::Clone() const {
return base::WrapUnique(new EventMetrics(*this));
}
void EventMetrics::CopyTimestampsFrom(const EventMetrics& other,
DispatchStage last_dispatch_stage) {
DCHECK_LE(last_dispatch_stage, DispatchStage::kMaxValue);
std::copy(other.dispatch_stage_timestamps_,
other.dispatch_stage_timestamps_ +
static_cast<size_t>(last_dispatch_stage) + 1,
dispatch_stage_timestamps_);
}
// ScrollEventMetrics
// static
std::unique_ptr<ScrollEventMetrics> ScrollEventMetrics::Create(
ui::EventType type,
ui::ScrollInputType input_type,
bool is_inertial,
base::TimeTicks timestamp,
base::TimeTicks arrived_in_browser_main_timestamp,
base::TimeTicks blocking_touch_dispatched_to_renderer,
std::optional<TraceId> trace_id) {
// TODO(crbug.com/40160689): We expect that `timestamp` is not null, but there
// seems to be some tests that are emitting events with null timestamp. We
// should investigate and try to fix those cases and add a `DCHECK` here to
// assert `timestamp` is not null.
DCHECK(IsGestureScroll(type) && !IsGestureScrollUpdate(type));
std::unique_ptr<ScrollEventMetrics> metrics =
CreateInternal(type, input_type, is_inertial, timestamp,
arrived_in_browser_main_timestamp,
base::DefaultTickClock::GetInstance(), trace_id);
if (!metrics)
return nullptr;
metrics->SetDispatchStageTimestamp(
DispatchStage::kArrivedInRendererCompositor);
metrics->SetDispatchStageTimestamp(
DispatchStage::kScrollsBlockingTouchDispatchedToRenderer,
blocking_touch_dispatched_to_renderer);
return metrics;
}
// static
std::unique_ptr<ScrollEventMetrics> ScrollEventMetrics::CreateForBrowser(
ui::EventType type,
ui::ScrollInputType input_type,
bool is_inertial,
base::TimeTicks timestamp,
std::optional<TraceId> trace_id) {
return Create(type, input_type, is_inertial, timestamp,
/*arrived_in_browser_main_timestamp=*/base::TimeTicks(),
/*blocking_touch_dispatched_to_renderer=*/base::TimeTicks(),
trace_id);
}
// static
std::unique_ptr<ScrollEventMetrics> ScrollEventMetrics::CreateForTesting(
ui::EventType type,
ui::ScrollInputType input_type,
bool is_inertial,
base::TimeTicks timestamp,
base::TimeTicks arrived_in_browser_main_timestamp,
const base::TickClock* tick_clock) {
DCHECK(!timestamp.is_null());
std::unique_ptr<ScrollEventMetrics> metrics = CreateInternal(
type, input_type, is_inertial, timestamp,
arrived_in_browser_main_timestamp, tick_clock, std::nullopt);
if (!metrics)
return nullptr;
metrics->SetDispatchStageTimestamp(
DispatchStage::kArrivedInRendererCompositor);
return metrics;
}
// static
std::unique_ptr<ScrollEventMetrics> ScrollEventMetrics::CreateFromExisting(
ui::EventType type,
ui::ScrollInputType input_type,
bool is_inertial,
DispatchStage last_dispatch_stage,
const EventMetrics* existing) {
// Generally, if `existing` is `nullptr` (the existing event is not of an
// interesting type), the new event won't be of an interesting type, too, and
// we can immediately return `nullptr`. The only exception is some tests that
// are not interested in reporting metrics, in which case we can immediately
// return `nullptr`, too, as they are not interested in reporting metrics.
if (!existing)
return nullptr;
std::unique_ptr<ScrollEventMetrics> metrics =
CreateInternal(type, input_type, is_inertial, base::TimeTicks(),
base::TimeTicks(), existing->tick_clock_, std::nullopt);
if (!metrics)
return nullptr;
// Use timestamps of all stages (including "Generated" stage) up to
// `last_dispatch_stage` from `existing`.
metrics->CopyTimestampsFrom(*existing, last_dispatch_stage);
return metrics;
}
// static
std::unique_ptr<ScrollEventMetrics> ScrollEventMetrics::CreateInternal(
ui::EventType type,
ui::ScrollInputType input_type,
bool is_inertial,
base::TimeTicks timestamp,
base::TimeTicks arrived_in_browser_main_timestamp,
const base::TickClock* tick_clock,
std::optional<TraceId> trace_id) {
std::optional<EventType> interesting_type =
ToInterestingEventType(type, is_inertial,
/*scroll_update_type=*/std::nullopt);
if (!interesting_type)
return nullptr;
return base::WrapUnique(new ScrollEventMetrics(
*interesting_type, ToScrollType(input_type), timestamp,
arrived_in_browser_main_timestamp, tick_clock, trace_id));
}
ScrollEventMetrics::ScrollEventMetrics(
EventType type,
ScrollType scroll_type,
base::TimeTicks timestamp,
base::TimeTicks arrived_in_browser_main_timestamp,
const base::TickClock* tick_clock,
std::optional<TraceId> trace_id)
: EventMetrics(type,
timestamp,
arrived_in_browser_main_timestamp,
tick_clock,
trace_id),
scroll_type_(scroll_type) {}
ScrollEventMetrics::ScrollEventMetrics(const ScrollEventMetrics&) = default;
ScrollEventMetrics::~ScrollEventMetrics() {
if (should_record_tracing()) {
EventLatencyTracingRecorder::RecordEventLatencyTraceEvent(
this, base::TimeTicks::Now(), base::TimeDelta(), nullptr, nullptr);
}
}
const char* ScrollEventMetrics::GetScrollTypeName() const {
return kScrollTypes[static_cast<int>(scroll_type_)].name;
}
ScrollEventMetrics* ScrollEventMetrics::AsScroll() {
return this;
}
std::unique_ptr<EventMetrics> ScrollEventMetrics::Clone() const {
return base::WrapUnique(new ScrollEventMetrics(*this));
}
// ScrollUpdateEventMetrics
// static
std::unique_ptr<ScrollUpdateEventMetrics> ScrollUpdateEventMetrics::Create(
ui::EventType type,
ui::ScrollInputType input_type,
bool is_inertial,
ScrollUpdateType scroll_update_type,
float delta,
base::TimeTicks timestamp,
base::TimeTicks arrived_in_browser_main_timestamp,
base::TimeTicks blocking_touch_dispatched_to_renderer,
std::optional<TraceId> trace_id) {
// TODO(crbug.com/40160689): We expect that `timestamp` is not null, but there
// seems to be some tests that are emitting events with null timestamp. We
// should investigate and try to fix those cases and add a `DCHECK` here to
// assert `timestamp` is not null.
DCHECK(IsGestureScrollUpdate(type));
std::unique_ptr<ScrollUpdateEventMetrics> metrics =
CreateInternal(type, input_type, is_inertial, scroll_update_type, delta,
timestamp, arrived_in_browser_main_timestamp,
base::DefaultTickClock::GetInstance(), trace_id);
if (!metrics)
return nullptr;
metrics->SetDispatchStageTimestamp(
DispatchStage::kArrivedInRendererCompositor);
metrics->SetDispatchStageTimestamp(
DispatchStage::kScrollsBlockingTouchDispatchedToRenderer,
blocking_touch_dispatched_to_renderer);
return metrics;
}
// static
std::unique_ptr<ScrollUpdateEventMetrics>
ScrollUpdateEventMetrics::CreateForBrowser(ui::EventType type,
ui::ScrollInputType input_type,
bool is_inertial,
ScrollUpdateType scroll_update_type,
float delta,
base::TimeTicks timestamp,
TraceId trace_id) {
return Create(
type, input_type, is_inertial, scroll_update_type, delta, timestamp,
/*arrived_in_browser_main_timestamp=*/base::TimeTicks(),
/*blocking_touch_dispatched_to_renderer=*/base::TimeTicks(), trace_id);
}
// static
std::unique_ptr<ScrollUpdateEventMetrics>
ScrollUpdateEventMetrics::CreateForTesting(
ui::EventType type,
ui::ScrollInputType input_type,
bool is_inertial,
ScrollUpdateType scroll_update_type,
float delta,
base::TimeTicks timestamp,
base::TimeTicks arrived_in_browser_main_timestamp,
const base::TickClock* tick_clock,
std::optional<TraceId> trace_id) {
DCHECK(!timestamp.is_null());
std::unique_ptr<ScrollUpdateEventMetrics> metrics = CreateInternal(
type, input_type, is_inertial, scroll_update_type, delta, timestamp,
arrived_in_browser_main_timestamp, tick_clock, trace_id);
if (!metrics)
return nullptr;
metrics->SetDispatchStageTimestamp(
DispatchStage::kArrivedInRendererCompositor);
return metrics;
}
// static
std::unique_ptr<ScrollUpdateEventMetrics>
ScrollUpdateEventMetrics::CreateFromExisting(
ui::EventType type,
ui::ScrollInputType input_type,
bool is_inertial,
ScrollUpdateType scroll_update_type,
float delta,
DispatchStage last_dispatch_stage,
const EventMetrics* existing) {
// Since the new event is of an interesting type, we expect the existing event
// to be of an interesting type, too; which means `existing` should not be
// `nullptr`. However, some tests that are not interested in reporting
// metrics, don't create metrics objects even for events of interesting types.
// Return `nullptr` if that's the case.
if (!existing)
return nullptr;
base::TimeTicks generation_ts =
existing->GetDispatchStageTimestamp(DispatchStage::kGenerated);
std::unique_ptr<ScrollUpdateEventMetrics> metrics = CreateInternal(
type, input_type, is_inertial, scroll_update_type, delta, generation_ts,
base::TimeTicks(), existing->tick_clock_, std::nullopt);
if (!metrics)
return nullptr;
// Use timestamps of all stages (including "Generated" stage) up to
// `last_dispatch_stage` from `existing`.
metrics->CopyTimestampsFrom(*existing, last_dispatch_stage);
return metrics;
}
// static
std::unique_ptr<ScrollUpdateEventMetrics>
ScrollUpdateEventMetrics::CreateInternal(
ui::EventType type,
ui::ScrollInputType input_type,
bool is_inertial,
ScrollUpdateType scroll_update_type,
float delta,
base::TimeTicks timestamp,
base::TimeTicks arrived_in_browser_main_timestamp,
const base::TickClock* tick_clock,
std::optional<TraceId> trace_id) {
std::optional<EventType> interesting_type =
ToInterestingEventType(type, is_inertial, scroll_update_type);
if (!interesting_type)
return nullptr;
return base::WrapUnique(new ScrollUpdateEventMetrics(
*interesting_type, ToScrollType(input_type), scroll_update_type, delta,
timestamp, arrived_in_browser_main_timestamp, tick_clock, trace_id));
}
ScrollUpdateEventMetrics::ScrollUpdateEventMetrics(
EventType type,
ScrollType scroll_type,
ScrollUpdateType scroll_update_type,
float delta,
base::TimeTicks timestamp,
base::TimeTicks arrived_in_browser_main_timestamp,
const base::TickClock* tick_clock,
std::optional<TraceId> trace_id)
: ScrollEventMetrics(type,
scroll_type,
timestamp,
arrived_in_browser_main_timestamp,
tick_clock,
trace_id),
delta_(delta),
predicted_delta_(delta),
last_timestamp_(timestamp) {}
ScrollUpdateEventMetrics::ScrollUpdateEventMetrics(
const ScrollUpdateEventMetrics&) = default;
ScrollUpdateEventMetrics::~ScrollUpdateEventMetrics() {
if (should_record_tracing()) {
EventLatencyTracingRecorder::RecordEventLatencyTraceEvent(
this, base::TimeTicks::Now(), base::TimeDelta(), nullptr, nullptr);
}
}
void ScrollUpdateEventMetrics::CoalesceWith(
const ScrollUpdateEventMetrics& newer_scroll_update) {
last_timestamp_ = newer_scroll_update.last_timestamp_;
delta_ += newer_scroll_update.delta_;
predicted_delta_ += newer_scroll_update.predicted_delta_;
coalesced_event_count_ += newer_scroll_update.coalesced_event_count_;
}
ScrollUpdateEventMetrics* ScrollUpdateEventMetrics::AsScrollUpdate() {
return this;
}
std::unique_ptr<EventMetrics> ScrollUpdateEventMetrics::Clone() const {
return base::WrapUnique(new ScrollUpdateEventMetrics(*this));
}
// PinchEventMetrics
// static
std::unique_ptr<PinchEventMetrics> PinchEventMetrics::Create(
ui::EventType type,
ui::ScrollInputType input_type,
base::TimeTicks timestamp,
TraceId trace_id) {
// TODO(crbug.com/40160689): We expect that `timestamp` is not null, but there
// seems to be some tests that are emitting events with null timestamp. We
// should investigate and try to fix those cases and add a `DCHECK` here to
// assert `timestamp` is not null.
DCHECK(IsGesturePinch(type));
std::unique_ptr<PinchEventMetrics> metrics =
CreateInternal(type, input_type, timestamp,
base::DefaultTickClock::GetInstance(), trace_id);
if (!metrics)
return nullptr;
metrics->SetDispatchStageTimestamp(
DispatchStage::kArrivedInRendererCompositor);
return metrics;
}
// static
std::unique_ptr<PinchEventMetrics> PinchEventMetrics::CreateForTesting(
ui::EventType type,
ui::ScrollInputType input_type,
base::TimeTicks timestamp,
const base::TickClock* tick_clock) {
DCHECK(!timestamp.is_null());
std::unique_ptr<PinchEventMetrics> metrics =
CreateInternal(type, input_type, timestamp, tick_clock, std::nullopt);
if (!metrics)
return nullptr;
metrics->SetDispatchStageTimestamp(
DispatchStage::kArrivedInRendererCompositor);
return metrics;
}
// static
std::unique_ptr<PinchEventMetrics> PinchEventMetrics::CreateInternal(
ui::EventType type,
ui::ScrollInputType input_type,
base::TimeTicks timestamp,
const base::TickClock* tick_clock,
std::optional<TraceId> trace_id) {
std::optional<EventType> interesting_type =
ToInterestingEventType(type, /*scroll_is_inertial=*/std::nullopt,
/*scroll_update_type=*/std::nullopt);
if (!interesting_type)
return nullptr;
return base::WrapUnique(
new PinchEventMetrics(*interesting_type, ToPinchType(input_type),
timestamp, tick_clock, trace_id));
}
PinchEventMetrics::PinchEventMetrics(EventType type,
PinchType pinch_type,
base::TimeTicks timestamp,
const base::TickClock* tick_clock,
std::optional<TraceId> trace_id)
: EventMetrics(type, timestamp, tick_clock, trace_id),
pinch_type_(pinch_type) {}
PinchEventMetrics::PinchEventMetrics(const PinchEventMetrics&) = default;
PinchEventMetrics::~PinchEventMetrics() {
if (should_record_tracing()) {
EventLatencyTracingRecorder::RecordEventLatencyTraceEvent(
this, base::TimeTicks::Now(), base::TimeDelta(), nullptr, nullptr);
}
}
const char* PinchEventMetrics::GetPinchTypeName() const {
return kPinchTypes[static_cast<int>(pinch_type_)].name;
}
PinchEventMetrics* PinchEventMetrics::AsPinch() {
return this;
}
std::unique_ptr<EventMetrics> PinchEventMetrics::Clone() const {
return base::WrapUnique(new PinchEventMetrics(*this));
}
// EventMetricsSet
EventMetricsSet::EventMetricsSet() = default;
EventMetricsSet::~EventMetricsSet() = default;
EventMetricsSet::EventMetricsSet(EventMetrics::List main_thread_event_metrics,
EventMetrics::List impl_thread_event_metrics)
: main_event_metrics(std::move(main_thread_event_metrics)),
impl_event_metrics(std::move(impl_thread_event_metrics)) {}
EventMetricsSet::EventMetricsSet(EventMetricsSet&& other) = default;
EventMetricsSet& EventMetricsSet::operator=(EventMetricsSet&& other) = default;
} // namespace cc