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
|
// 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"
#include "base/metrics/histogram.h"
#include "base/trace_event/trace_event.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;
// 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_)
SelfDestruct();
}
// Mark the recorder to be deleted when the last presentation feedback
// is reported.
void EndRecording() {
recording_ = false;
const bool has_compositing_shut_down = !compositor_;
// If compositing has shut down and the frame hasn't been presented yet,
// it won't happen at this point. This class must self destruct in this case
// otherwise it will leak.
if (state_ == PRESENTED || has_compositing_shut_down) {
SelfDestruct();
}
}
protected:
int max_latency_ms() const { return max_latency_ms_; }
int present_count() const { return present_count_; }
~PresentationTimeRecorderInternal() override {
DCHECK(!recording_);
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);
}
}
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,
};
class Deleter {
public:
explicit Deleter(PresentationTimeRecorderInternal* recorder_internal)
: recorder_internal_(recorder_internal) {}
Deleter(const Deleter&) = delete;
Deleter& operator=(const Deleter&) = delete;
~Deleter() { recorder_internal_.ExtractAsDangling()->SelfDestruct(); }
private:
raw_ptr<PresentationTimeRecorderInternal> recorder_internal_ = nullptr;
};
// |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,
const viz::FrameTimingDetails& frame_timing_details);
void SelfDestruct();
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;
viz::FrameTimingDetails details;
details.presentation_feedback.timestamp = now;
OnPresented(request_count_++, now, details);
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,
const viz::FrameTimingDetails& frame_timing_details) {
base::TimeTicks presentation_timestamp =
frame_timing_details.presentation_feedback.timestamp;
std::optional<Deleter> deleter;
if (!recording_ && (count == (request_count_ - 1)))
deleter.emplace(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();
}
void PresentationTimeRecorder::PresentationTimeRecorderInternal::
SelfDestruct() {
delete this;
}
// PresentationTimeRecorder ---------------------------------------------------
PresentationTimeRecorder::PresentationTimeRecorder(
raw_ptr<PresentationTimeRecorderInternal> internal)
: recorder_internal_(std::move(internal)) {}
PresentationTimeRecorder::~PresentationTimeRecorder() {
// The internal recorder self destruct when finished its job.
recorder_internal_.ExtractAsDangling()->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,
const PresentationTimeRecorder::BucketParams& bucket_params) {
return base::Histogram::FactoryTimeGet(
name, bucket_params.min_latency, bucket_params.max_latency,
bucket_params.num_buckets,
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,
const PresentationTimeRecorder::BucketParams& bucket_params,
bool emit_trace_event)
: PresentationTimeRecorderInternal(compositor),
presentation_time_histogram_(
CreateTimesHistogram(presentation_time_histogram_name,
bucket_params)),
max_latency_histogram_name_(max_latency_histogram_name),
bucket_params_(bucket_params),
presentation_time_histogram_name_(
emit_trace_event ? presentation_time_histogram_name : nullptr) {
if (presentation_time_histogram_name_) {
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("ui", presentation_time_histogram_name_,
this);
}
}
PresentationTimeHistogramRecorder(const PresentationTimeHistogramRecorder&) =
delete;
PresentationTimeHistogramRecorder& operator=(
const PresentationTimeHistogramRecorder&) = delete;
// PresentationTimeRecorderInternal:
void ReportTime(base::TimeDelta delta) override {
if (presentation_time_histogram_name_) {
TRACE_EVENT_NESTABLE_ASYNC_END0("ui", presentation_time_histogram_name_,
this);
}
presentation_time_histogram_->AddTimeMillisecondsGranularity(delta);
}
private:
~PresentationTimeHistogramRecorder() override {
if (present_count() > 0 && !max_latency_histogram_name_.empty()) {
CreateTimesHistogram(max_latency_histogram_name_.c_str(), bucket_params_)
->AddTimeMillisecondsGranularity(
base::Milliseconds(max_latency_ms()));
}
}
raw_ptr<base::HistogramBase> presentation_time_histogram_;
std::string max_latency_histogram_name_;
const PresentationTimeRecorder::BucketParams bucket_params_;
// Only set if `emit_trace_event_` is true since that's its only use.
const char* const presentation_time_histogram_name_ = nullptr;
};
} // namespace
// BucketParams ------------------------------------------
PresentationTimeRecorder::BucketParams::BucketParams() = default;
PresentationTimeRecorder::BucketParams::BucketParams(
base::TimeDelta min_latency,
base::TimeDelta max_latency,
int num_buckets)
: min_latency(min_latency),
max_latency(max_latency),
num_buckets(num_buckets) {}
PresentationTimeRecorder::BucketParams::BucketParams(const BucketParams&) =
default;
PresentationTimeRecorder::BucketParams&
PresentationTimeRecorder::BucketParams::operator=(const BucketParams&) =
default;
PresentationTimeRecorder::BucketParams::~BucketParams() = default;
// static
PresentationTimeRecorder::BucketParams
PresentationTimeRecorder::BucketParams::CreateWithMaximum(
base::TimeDelta max_latency) {
BucketParams params;
params.max_latency = max_latency;
return params;
}
std::unique_ptr<PresentationTimeRecorder>
CreatePresentationTimeHistogramRecorder(
ui::Compositor* compositor,
const char* presentation_time_histogram_name,
const char* max_latency_histogram_name,
PresentationTimeRecorder::BucketParams bucket_params,
bool emit_trace_event) {
return std::make_unique<PresentationTimeRecorder>(
new PresentationTimeHistogramRecorder(
compositor, presentation_time_histogram_name,
max_latency_histogram_name, bucket_params, emit_trace_event));
}
// 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,
const viz::FrameTimingDetails& frame_timing_details) {
recorder_->recorder_internal_->OnPresented(count, requested_time,
frame_timing_details);
}
} // namespace ui
|