File: delay_based_time_source.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (200 lines) | stat: -rw-r--r-- 7,249 bytes parent folder | download | duplicates (5)
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
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/viz/common/frame_sinks/delay_based_time_source.h"

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <string>

#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/traced_value.h"
#include "components/viz/common/features.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"

namespace viz {

// The following methods correspond to the DelayBasedTimeSource that uses
// the base::TimeTicks::Now as the timebase.
DelayBasedTimeSource::DelayBasedTimeSource(
    base::SingleThreadTaskRunner* task_runner)
    : client_(nullptr),
      active_(false),
      timebase_(base::TimeTicks()),
      interval_(BeginFrameArgs::DefaultInterval()),
      last_tick_time_(base::TimeTicks() - interval_),
      task_runner_(task_runner),
      tick_closure_(base::BindRepeating(&DelayBasedTimeSource::OnTimerTick,
                                        base::Unretained(this))) {}

DelayBasedTimeSource::~DelayBasedTimeSource() = default;

void DelayBasedTimeSource::SetActive(bool active) {
  TRACE_EVENT1("viz", "DelayBasedTimeSource::SetActive", "active", active);

  if (active == active_)
    return;

  active_ = active;

  if (active_) {
    timer_.SetTaskRunner(task_runner_.get());
    PostNextTickTask(Now());
  } else {
    timer_.Stop();
    last_tick_time_ = base::TimeTicks();
    next_tick_time_ = base::TimeTicks();
  }
}

base::TimeDelta DelayBasedTimeSource::Interval() const {
  return interval_;
}

bool DelayBasedTimeSource::Active() const {
  return active_;
}

base::TimeTicks DelayBasedTimeSource::LastTickTime() const {
  return last_tick_time_;
}

base::TimeTicks DelayBasedTimeSource::NextTickTime() const {
  return next_tick_time_;
}

void DelayBasedTimeSource::OnTimerTick() {
  DCHECK(active_);

  last_tick_time_ = next_tick_time_;

  PostNextTickTask(Now());

  // Fire the tick.
  if (client_)
    client_->OnTimerTick();
}

void DelayBasedTimeSource::SetClient(DelayBasedTimeSourceClient* client) {
  client_ = client;
}

void DelayBasedTimeSource::SetTimebaseAndInterval(base::TimeTicks timebase,
                                                  base::TimeDelta interval) {
  interval_ = interval;
  timebase_ = timebase;
}

base::TimeTicks DelayBasedTimeSource::Now() const {
  return base::TimeTicks::Now();
}

// This code tries to achieve an average tick rate as close to interval_ as
// possible.  To do this, it has to deal with a few basic issues:
//   1. PostDelayedTask can delay only at a millisecond granularity. So, 16.666
//   has to posted as 16 or 17.
//   2. A delayed task may come back a bit late (a few ms), or really late
//   (frames later)
//
// The basic idea with this scheduler here is to keep track of where we *want*
// to run in tick_target_. We update this with the exact interval.
//
// Then, when we post our task, we take the floor of (tick_target_ and Now()).
// If we started at now=0, and 60FPs (all times in milliseconds):
//      now=0    target=16.667   PostDelayedTask(16)
//
// When our callback runs, we figure out how far off we were from that goal.
// Because of the flooring operation, and assuming our timer runs exactly when
// it should, this yields:
//      now=16   target=16.667
//
// Since we can't post a 0.667 ms task to get to now=16, we just treat this as a
// tick. Then, we update target to be 33.333. We now post another task based on
// the difference between our target and now:
//      now=16   tick_target=16.667  new_target=33.333   -->
//          PostDelayedTask(floor(33.333 - 16)) --> PostDelayedTask(17)
//
// Over time, with no late tasks, this leads to us posting tasks like this:
//      now=0    tick_target=0       new_target=16.667   -->
//          tick(), PostDelayedTask(16)
//      now=16   tick_target=16.667  new_target=33.333   -->
//          tick(), PostDelayedTask(17)
//      now=33   tick_target=33.333  new_target=50.000   -->
//          tick(), PostDelayedTask(17)
//      now=50   tick_target=50.000  new_target=66.667   -->
//          tick(), PostDelayedTask(16)
//
// We treat delays in tasks differently depending on the amount of delay we
// encounter. Suppose we posted a task with a target=16.667:
//   Case 1: late but not unrecoverably-so
//      now=18 tick_target=16.667
//
//   Case 2: so late we obviously missed the tick
//      now=25.0 tick_target=16.667
//
// We treat the first case as a tick anyway, and assume the delay was unusual.
// Thus, we compute the new_target based on the old timebase:
//      now=18   tick_target=16.667  new_target=33.333   -->
//          tick(), PostDelayedTask(floor(33.333-18)) --> PostDelayedTask(15)
// This brings us back to 18+15 = 33, which was where we would have been if the
// task hadn't been late.
//
// For the really late delay, we we move to the next logical tick. The timebase
// is not reset.
//      now=37   tick_target=16.667  new_target=50.000  -->
//          tick(), PostDelayedTask(floor(50.000-37)) --> PostDelayedTask(13)
void DelayBasedTimeSource::PostNextTickTask(base::TimeTicks now) {
  if (interval_.is_zero()) {
    next_tick_time_ = now;
    timer_.Start(FROM_HERE, base::TimeTicks(), tick_closure_,
                 base::subtle::DelayPolicy::kPrecise);
  } else {
    next_tick_time_ = now.SnappedToNextTick(timebase_, interval_);
    // TODO( crbug.com/398090404 ): Remove the code in this condition as it is
    // being superseded by the next block.
    if (next_tick_time_ == now) {
      next_tick_time_ += interval_;
    }

    if (base::FeatureList::IsEnabled(
            features::kAvoidDuplicateDelayBeginFrame)) {
      // Some devices report vblank timings with enough variance as to confuse
      // 'SnappedToNextTick' on the time remaining for next begin frame. This
      // code allows for up to 'kMaxJudderAllowed' time for these values to
      // simply be corrected to the next interval and thereby avoid an erroneous
      // double tick. See crbug.com/398090404 for details.
      const auto kMaxJudderAllowed = base::Microseconds(500);
      if (now + kMaxJudderAllowed >= next_tick_time_) {
        next_tick_time_ += interval_;
      }
    }
    DCHECK_GT(next_tick_time_, now);
    timer_.Start(FROM_HERE, next_tick_time_, tick_closure_,
                 base::subtle::DelayPolicy::kPrecise);
  }
}

std::string DelayBasedTimeSource::TypeString() const {
  return "DelayBasedTimeSource";
}

void DelayBasedTimeSource::AsValueInto(
    base::trace_event::TracedValue* state) const {
  state->SetString("type", TypeString());
  state->SetDouble("last_tick_time_us",
                   LastTickTime().since_origin().InMicrosecondsF());
  state->SetDouble("next_tick_time_us",
                   NextTickTime().since_origin().InMicrosecondsF());
  state->SetDouble("interval_us", interval_.InMicrosecondsF());
  state->SetDouble("timebase_us", timebase_.since_origin().InMicrosecondsF());
  state->SetBoolean("active", active_);
}

}  // namespace viz