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
|
// Copyright 2018 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/service/frame_sinks/external_begin_frame_source_win.h"
#include <utility>
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/trace_event/trace_event.h"
namespace viz {
namespace {
BASE_FEATURE(kExternalBeginFrameSourceWinUsesRunOrPostTask,
"ExternalBeginFrameSourceWinUsesRunOrPostTask",
base::FEATURE_DISABLED_BY_DEFAULT);
} // namespace
ExternalBeginFrameSourceWin::ExternalBeginFrameSourceWin(
uint32_t restart_id,
scoped_refptr<base::SequencedTaskRunner> task_runner)
: ExternalBeginFrameSource(this, restart_id),
task_runner_(std::move(task_runner)) {}
ExternalBeginFrameSourceWin::~ExternalBeginFrameSourceWin() {
if (observing_vsync_) {
gl::VSyncThreadWin::GetInstance()->RemoveObserver(this);
}
}
void ExternalBeginFrameSourceWin::OnVSync(base::TimeTicks vsync_time,
base::TimeDelta vsync_interval) {
auto callback =
base::BindOnce(&ExternalBeginFrameSourceWin::OnVSyncOnSequence,
weak_factory_.GetWeakPtr(), vsync_time, vsync_interval);
if (base::FeatureList::IsEnabled(
kExternalBeginFrameSourceWinUsesRunOrPostTask)) {
task_runner_->RunOrPostTask(base::subtle::RunOrPostTaskPassKey(), FROM_HERE,
std::move(callback));
} else {
task_runner_->PostTask(FROM_HERE, std::move(callback));
}
}
void ExternalBeginFrameSourceWin::OnVSyncOnSequence(
base::TimeTicks vsync_time,
base::TimeDelta vsync_interval) {
DCHECK(task_runner_->RunsTasksInCurrentSequence());
vsync_interval_ = vsync_interval;
if (skip_next_vsync_) {
TRACE_EVENT_INSTANT0("gpu",
"ExternalBeginFrameSourceWin::OnVSync - skip_vsync",
TRACE_EVENT_SCOPE_THREAD);
skip_next_vsync_ = false;
return;
}
if (run_at_half_refresh_rate_) {
skip_next_vsync_ = true;
vsync_interval *= 2;
}
auto begin_frame_args = begin_frame_args_generator_.GenerateBeginFrameArgs(
source_id(), vsync_time, vsync_time + vsync_interval, vsync_interval);
ExternalBeginFrameSource::OnBeginFrame(begin_frame_args);
}
BeginFrameArgs ExternalBeginFrameSourceWin::GetMissedBeginFrameArgs(
BeginFrameObserver* obs) {
auto frame_time = last_begin_frame_args_.frame_time;
auto interval = last_begin_frame_args_.interval;
auto now = base::TimeTicks::Now();
if (last_begin_frame_args_.IsValid()) {
frame_time = now.SnappedToNextTick(frame_time, interval) - interval;
} else {
// Create BeginFrameArgs for now so that we don't have to wait until vsync.
frame_time = now;
interval = BeginFrameArgs::DefaultInterval();
}
// Don't create new args unless we've actually moved past the previous frame.
if (!last_begin_frame_args_.IsValid() ||
frame_time > last_begin_frame_args_.frame_time) {
last_begin_frame_args_ = begin_frame_args_generator_.GenerateBeginFrameArgs(
source_id(), frame_time, frame_time + interval, interval);
}
return ExternalBeginFrameSource::GetMissedBeginFrameArgs(obs);
}
void ExternalBeginFrameSourceWin::SetPreferredInterval(
base::TimeDelta interval) {
auto interval_for_half_refresh_rate = vsync_interval_ * 2;
constexpr auto kMaxDelta = base::Milliseconds(0.5);
bool run_at_half_refresh_rate =
interval > (interval_for_half_refresh_rate - kMaxDelta);
if (run_at_half_refresh_rate_ == run_at_half_refresh_rate)
return;
TRACE_EVENT1("gpu", "ExternalBeginFrameSourceWin::SetPreferredInterval",
"run_at_half_refresh_rate", run_at_half_refresh_rate);
run_at_half_refresh_rate_ = run_at_half_refresh_rate;
skip_next_vsync_ = false;
}
void ExternalBeginFrameSourceWin::OnNeedsBeginFrames(bool needs_begin_frames) {
if (observing_vsync_ == needs_begin_frames) {
return;
}
observing_vsync_ = needs_begin_frames;
skip_next_vsync_ = false;
if (needs_begin_frames) {
gl::VSyncThreadWin::GetInstance()->AddObserver(this);
} else {
gl::VSyncThreadWin::GetInstance()->RemoveObserver(this);
}
}
void ExternalBeginFrameSourceWin::SetVSyncDisplayID(int64_t display_id) {
// TODO(sunnyps): See if we should use non-primary displays for driving vsync.
}
} // namespace viz
|