File: user_model.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (170 lines) | stat: -rw-r--r-- 6,185 bytes parent folder | download | duplicates (7)
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/scheduler/main_thread/user_model.h"

#include "base/trace_event/trace_event.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_value.h"

namespace blink {
namespace scheduler {

UserModel::UserModel() = default;

void UserModel::DidStartProcessingInputEvent(blink::WebInputEvent::Type type,
                                             const base::TimeTicks now) {
  last_input_signal_time_ = now;
  if (type == blink::WebInputEvent::Type::kTouchStart ||
      type == blink::WebInputEvent::Type::kGestureScrollBegin ||
      type == blink::WebInputEvent::Type::kGesturePinchBegin) {
    // Only update stats once per gesture.
    if (!is_gesture_active_)
      last_gesture_start_time_ = now;

    is_gesture_active_ = true;
  }

  // We need to track continuous gestures seperatly for scroll detection
  // because taps should not be confused with scrolls.
  if (type == blink::WebInputEvent::Type::kGestureScrollBegin ||
      type == blink::WebInputEvent::Type::kGestureScrollEnd ||
      type == blink::WebInputEvent::Type::kGestureScrollUpdate ||
      type == blink::WebInputEvent::Type::kGestureFlingStart ||
      type == blink::WebInputEvent::Type::kGestureFlingCancel ||
      type == blink::WebInputEvent::Type::kGesturePinchBegin ||
      type == blink::WebInputEvent::Type::kGesturePinchEnd ||
      type == blink::WebInputEvent::Type::kGesturePinchUpdate) {
    last_continuous_gesture_time_ = now;
  }

  // If the gesture has ended, clear |is_gesture_active_| and record a UMA
  // metric that tracks its duration.
  if (type == blink::WebInputEvent::Type::kGestureScrollEnd ||
      type == blink::WebInputEvent::Type::kGesturePinchEnd ||
      type == blink::WebInputEvent::Type::kGestureFlingStart ||
      type == blink::WebInputEvent::Type::kTouchEnd) {
    is_gesture_active_ = false;
  }

  TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"),
                 "is_gesture_active", is_gesture_active_);

  pending_input_event_count_++;
}

void UserModel::DidFinishProcessingInputEvent(const base::TimeTicks now) {
  last_input_signal_time_ = now;
  if (pending_input_event_count_ > 0)
    pending_input_event_count_--;
}

void UserModel::DidProcessDiscreteInputEvent(const base::TimeTicks now) {
  last_discrete_input_time_ = now;
}

void UserModel::DidProcessDiscreteInputResponse() {
  last_discrete_input_time_ = base::TimeTicks();
}

base::TimeDelta UserModel::TimeLeftInContinuousUserGesture(
    base::TimeTicks now) const {
  // If the input event is still pending, go into input prioritized policy and
  // check again later.
  if (pending_input_event_count_ > 0) {
    return kGestureEstimationLimit;
  }
  if (last_input_signal_time_.is_null() ||
      last_input_signal_time_ + kGestureEstimationLimit < now) {
    return base::TimeDelta();
  }
  return last_input_signal_time_ + kGestureEstimationLimit - now;
}

base::TimeDelta UserModel::TimeLeftUntilDiscreteInputResponseDeadline(
    base::TimeTicks now) const {
  if (last_discrete_input_time_.is_null() ||
      last_discrete_input_time_ + kDiscreteInputResponseDeadline < now) {
    return base::TimeDelta();
  }
  return last_discrete_input_time_ + kDiscreteInputResponseDeadline - now;
}

bool UserModel::IsGestureExpectedSoon(
    const base::TimeTicks now,
    base::TimeDelta* prediction_valid_duration) {
  bool was_gesture_expected = is_gesture_expected_;
  is_gesture_expected_ =
      IsGestureExpectedSoonImpl(now, prediction_valid_duration);

  // Track when we start expecting a gesture so we can work out later if a
  // gesture actually happened.
  if (!was_gesture_expected && is_gesture_expected_)
    last_gesture_expected_start_time_ = now;
  return is_gesture_expected_;
}

bool UserModel::IsGestureExpectedSoonImpl(
    const base::TimeTicks now,
    base::TimeDelta* prediction_valid_duration) const {
  if (is_gesture_active_) {
    if (IsGestureExpectedToContinue(now, prediction_valid_duration))
      return false;
    *prediction_valid_duration = kExpectSubsequentGestureDeadline;
    return true;
  } else {
    // If we have finished a gesture then a subsequent gesture is deemed likely.
    if (last_continuous_gesture_time_.is_null() ||
        last_continuous_gesture_time_ + kExpectSubsequentGestureDeadline <=
            now) {
      return false;
    }
    *prediction_valid_duration =
        last_continuous_gesture_time_ + kExpectSubsequentGestureDeadline - now;
    return true;
  }
}

bool UserModel::IsGestureExpectedToContinue(
    const base::TimeTicks now,
    base::TimeDelta* prediction_valid_duration) const {
  if (!is_gesture_active_)
    return false;

  base::TimeTicks expected_gesture_end_time =
      last_gesture_start_time_ + kMedianGestureDuration;

  if (expected_gesture_end_time > now) {
    *prediction_valid_duration = expected_gesture_end_time - now;
    return true;
  }
  return false;
}

void UserModel::Reset(base::TimeTicks now) {
  last_input_signal_time_ = base::TimeTicks();
  last_gesture_start_time_ = base::TimeTicks();
  last_continuous_gesture_time_ = base::TimeTicks();
  last_gesture_expected_start_time_ = base::TimeTicks();
  last_discrete_input_time_ = base::TimeTicks();
  last_reset_time_ = now;
  is_gesture_active_ = false;
  is_gesture_expected_ = false;
  pending_input_event_count_ = 0;
}

void UserModel::WriteIntoTrace(perfetto::TracedValue context) const {
  auto dict = std::move(context).WriteDictionary();
  dict.Add("pending_input_event_count", pending_input_event_count_);
  dict.Add("last_input_signal_time", last_input_signal_time_);
  dict.Add("last_gesture_start_time", last_gesture_start_time_);
  dict.Add("last_continuous_gesture_time", last_continuous_gesture_time_);
  dict.Add("last_gesture_expected_start_time",
           last_gesture_expected_start_time_);
  dict.Add("last_reset_time", last_reset_time_);
  dict.Add("is_gesture_expected", is_gesture_expected_);
  dict.Add("is_gesture_active", is_gesture_active_);
}

}  // namespace scheduler
}  // namespace blink