forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresentation_time_recorder.cc
275 lines (222 loc) · 8.84 KB
/
presentation_time_recorder.cc
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/compositor/presentation_time_recorder.h"
#include <ostream>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
namespace ui {
namespace {
bool report_immediately_for_test = false;
} // namespace
// PresentationTimeRecorderInternal -------------------------------------------
class PresentationTimeRecorder::PresentationTimeRecorderInternal
: public ui::CompositorObserver {
public:
explicit PresentationTimeRecorderInternal(ui::Compositor* compositor)
: compositor_(compositor) {
compositor_->AddObserver(this);
VLOG(1) << "Start Recording Frame Time";
}
PresentationTimeRecorderInternal(const PresentationTimeRecorderInternal&) =
delete;
PresentationTimeRecorderInternal& operator=(
const PresentationTimeRecorderInternal&) = delete;
~PresentationTimeRecorderInternal() override {
const int average_latency_ms =
present_count_ ? total_latency_ms_ / present_count_ : 0;
VLOG(1) << "Finished Recording FrameTime: average latency="
<< average_latency_ms << "ms, max latency=" << max_latency_ms_
<< "ms";
if (compositor_)
compositor_->RemoveObserver(this);
}
// Start recording next frame. It skips requesting next frame and returns
// false if the previous frame has not been committed yet.
bool RequestNext();
// ui::CompositorObserver:
void OnCompositingDidCommit(ui::Compositor* compositor) override {
// Skip updating the state if commit happened after present without
// request because the commit is for unrelated activity.
if (state_ != PRESENTED)
state_ = COMMITTED;
}
void OnCompositingShuttingDown(ui::Compositor* compositor) override {
DCHECK_EQ(compositor_, compositor);
compositor_->RemoveObserver(this);
compositor_ = nullptr;
if (!recording_)
delete this;
}
// Mark the recorder to be deleted when the last presentation feedback
// is reported.
void EndRecording() {
recording_ = false;
if (state_ == PRESENTED)
delete this;
}
protected:
int max_latency_ms() const { return max_latency_ms_; }
int present_count() const { return present_count_; }
private:
friend class TestApi;
enum State {
// The frame has been presented to the screen. This is the initial state.
PRESENTED,
// The presentation feedback has been requested.
REQUESTED,
// The changes to layers have been submitted, and waiting to be presented.
COMMITTED,
};
// |delta| is the duration between the successful request time and
// presentation time.
virtual void ReportTime(base::TimeDelta delta) = 0;
void OnPresented(int count,
base::TimeTicks requested_time,
base::TimeTicks presentation_timestamp);
State state_ = PRESENTED;
int present_count_ = 0;
int request_count_ = 0;
int total_latency_ms_ = 0;
int max_latency_ms_ = 0;
raw_ptr<ui::Compositor> compositor_ = nullptr;
bool recording_ = true;
base::WeakPtrFactory<PresentationTimeRecorderInternal> weak_ptr_factory_{
this};
};
bool PresentationTimeRecorder::PresentationTimeRecorderInternal::RequestNext() {
if (!compositor_)
return false;
if (state_ == REQUESTED)
return false;
const base::TimeTicks now = base::TimeTicks::Now();
VLOG(1) << "Start Next (" << request_count_
<< ") state=" << (state_ == COMMITTED ? "Committed" : "Presented");
state_ = REQUESTED;
if (report_immediately_for_test) {
state_ = COMMITTED;
OnPresented(request_count_++, now, now);
return true;
}
compositor_->RequestSuccessfulPresentationTimeForNextFrame(
base::BindOnce(&PresentationTimeRecorderInternal::OnPresented,
weak_ptr_factory_.GetWeakPtr(), request_count_++, now));
return true;
}
void PresentationTimeRecorder::PresentationTimeRecorderInternal::OnPresented(
int count,
base::TimeTicks requested_time,
base::TimeTicks presentation_timestamp) {
std::unique_ptr<PresentationTimeRecorderInternal> deleter;
if (!recording_ && (count == (request_count_ - 1)))
deleter = base::WrapUnique(this);
if (state_ == COMMITTED)
state_ = PRESENTED;
if (presentation_timestamp.is_null()) {
// TODO(b/165951963): ideally feedback.timestamp should not be null.
// Consider replacing this by DCHECK or CHECK.
LOG(ERROR) << "Invalid feedback timestamp (" << count << "):"
<< " timestamp is not set";
return;
}
const base::TimeDelta delta = presentation_timestamp - requested_time;
if (delta.InMilliseconds() < 0) {
LOG(ERROR) << "Invalid timestamp for presentation feedback (" << count
<< "): requested_time=" << requested_time
<< " presentation_timestamp=" << presentation_timestamp;
return;
}
if (delta.InMilliseconds() > max_latency_ms_)
max_latency_ms_ = delta.InMilliseconds();
present_count_++;
total_latency_ms_ += delta.InMilliseconds();
ReportTime(delta);
VLOG(1) << "OnPresented (" << count << "):" << delta.InMilliseconds();
}
// PresentationTimeRecorder ---------------------------------------------------
PresentationTimeRecorder::PresentationTimeRecorder(
std::unique_ptr<PresentationTimeRecorderInternal> internal)
: recorder_internal_(std::move(internal)) {}
PresentationTimeRecorder::~PresentationTimeRecorder() {
auto* recorder_internal = recorder_internal_.release();
// The internal recorder self destruct when finished its job.
recorder_internal->EndRecording();
}
bool PresentationTimeRecorder::RequestNext() {
return recorder_internal_->RequestNext();
}
// static
void PresentationTimeRecorder::SetReportPresentationTimeImmediatelyForTest(
bool enable) {
report_immediately_for_test = enable;
}
namespace {
base::HistogramBase* CreateTimesHistogram(const char* name) {
return base::Histogram::FactoryTimeGet(
name, base::Milliseconds(1), base::Milliseconds(200), 50,
base::HistogramBase::kUmaTargetedHistogramFlag);
}
// PresentationTimeHistogramRecorder ------------------------------------------
class PresentationTimeHistogramRecorder
: public PresentationTimeRecorder::PresentationTimeRecorderInternal {
public:
// |presentation_time_histogram_name| records latency reported on
// |ReportTime()|. If |max_latency_histogram_name| is not empty, it records
// the maximum latency reported during the lifetime of this object. Histogram
// names must be the name of the UMA histogram defined in histograms.xml.
PresentationTimeHistogramRecorder(
ui::Compositor* compositor,
const char* presentation_time_histogram_name,
const char* max_latency_histogram_name)
: PresentationTimeRecorderInternal(compositor),
presentation_time_histogram_(
CreateTimesHistogram(presentation_time_histogram_name)),
max_latency_histogram_name_(max_latency_histogram_name) {}
PresentationTimeHistogramRecorder(const PresentationTimeHistogramRecorder&) =
delete;
PresentationTimeHistogramRecorder& operator=(
const PresentationTimeHistogramRecorder&) = delete;
~PresentationTimeHistogramRecorder() override {
if (present_count() > 0 && !max_latency_histogram_name_.empty()) {
CreateTimesHistogram(max_latency_histogram_name_.c_str())
->AddTimeMillisecondsGranularity(
base::Milliseconds(max_latency_ms()));
}
}
// PresentationTimeRecorderInternal:
void ReportTime(base::TimeDelta delta) override {
presentation_time_histogram_->AddTimeMillisecondsGranularity(delta);
}
private:
raw_ptr<base::HistogramBase> presentation_time_histogram_;
std::string max_latency_histogram_name_;
};
} // namespace
std::unique_ptr<PresentationTimeRecorder>
CreatePresentationTimeHistogramRecorder(
ui::Compositor* compositor,
const char* presentation_time_histogram_name,
const char* max_latency_histogram_name) {
return std::make_unique<PresentationTimeRecorder>(
std::make_unique<PresentationTimeHistogramRecorder>(
compositor, presentation_time_histogram_name,
max_latency_histogram_name));
}
// TestApi --------------------------------------------------------------------
PresentationTimeRecorder::TestApi::TestApi(PresentationTimeRecorder* recorder)
: recorder_(recorder) {}
void PresentationTimeRecorder::TestApi::OnCompositingDidCommit(
ui::Compositor* compositor) {
recorder_->recorder_internal_->OnCompositingDidCommit(compositor);
}
void PresentationTimeRecorder::TestApi::OnPresented(
int count,
base::TimeTicks requested_time,
base::TimeTicks presentation_timestamp) {
recorder_->recorder_internal_->OnPresented(count, requested_time,
presentation_timestamp);
}
} // namespace ui