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
|
// 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_mojo.h"
#include <utility>
#include "components/viz/service/frame_sinks/frame_sink_manager_impl.h"
namespace viz {
ExternalBeginFrameSourceMojo::ExternalBeginFrameSourceMojo(
FrameSinkManagerImpl* frame_sink_manager,
mojo::PendingAssociatedReceiver<mojom::ExternalBeginFrameController>
controller_receiver,
mojo::PendingAssociatedRemote<mojom::ExternalBeginFrameControllerClient>
controller_client_remote,
uint32_t restart_id)
: ExternalBeginFrameSource(this, restart_id),
frame_sink_manager_(frame_sink_manager),
receiver_(this, std::move(controller_receiver)),
remote_client_(std::move(controller_client_remote)) {
frame_sink_manager_->AddObserver(this);
}
ExternalBeginFrameSourceMojo::~ExternalBeginFrameSourceMojo() {
frame_sink_manager_->RemoveObserver(this);
DCHECK(!display_);
}
void ExternalBeginFrameSourceMojo::IssueExternalBeginFrame(
const BeginFrameArgs& args,
bool force,
base::OnceCallback<void(const BeginFrameAck&)> callback) {
DCHECK(!pending_frame_callback_) << "Got overlapping IssueExternalBeginFrame";
DCHECK(pending_frame_sinks_.empty());
original_source_id_ = args.frame_id.source_id;
OnBeginFrame(args);
pending_frame_callback_ = std::move(callback);
// When not forcing a frame, wait for it to occur when sinks needs a frame.
if (!force)
return;
// Ensure that Display will receive the BeginFrame (as a missed one), even
// if it doesn't currently need it. This way, we ensure that
// OnDisplayDidFinishFrame will be called for this BeginFrame.
DCHECK(display_);
display_->SetNeedsOneBeginFrame();
MaybeProduceFrameCallback();
}
void ExternalBeginFrameSourceMojo::OnDestroyedCompositorFrameSink(
const FrameSinkId& sink_id) {
pending_frame_sinks_.erase(sink_id);
MaybeProduceFrameCallback();
}
void ExternalBeginFrameSourceMojo::OnFrameSinkDidBeginFrame(
const FrameSinkId& sink_id,
const BeginFrameArgs& args) {
if (args.frame_id.source_id != original_source_id_)
return;
pending_frame_sinks_.insert(sink_id);
}
void ExternalBeginFrameSourceMojo::OnFrameSinkDidFinishFrame(
const FrameSinkId& sink_id,
const BeginFrameArgs& args) {
if (args.frame_id.source_id != original_source_id_)
return;
pending_frame_sinks_.erase(sink_id);
MaybeProduceFrameCallback();
}
void ExternalBeginFrameSourceMojo::MaybeProduceFrameCallback() {
if (!pending_frame_sinks_.empty())
return;
if (!pending_frame_callback_)
return;
if (pending_ack_) {
DispatchFrameCallback(*pending_ack_);
pending_ack_.reset();
return;
}
// If there aren't pending surfaces and the root frame is not missing,
// the display scheduler is likely to produce proper frame, so let it do
// its work. Otherwise, fire the pending frame callback early.
if (!display_->IsRootFrameMissing() &&
!display_->HasPendingSurfaces(last_begin_frame_args_)) {
return;
}
// All frame sinks are done with frame, yet the root frame is still missing,
// the display won't draw, so resolve callback now.
BeginFrameAck nak(last_begin_frame_args_.frame_id.source_id,
last_begin_frame_args_.frame_id.sequence_number,
/*has_damage=*/false);
DispatchFrameCallback(nak);
}
void ExternalBeginFrameSourceMojo::DispatchFrameCallback(
const BeginFrameAck& ack) {
// If there are pending copy output requests that have not been fulfilled,
// cancel them, as they won't be served till the next frame. This prevents
// the client for waiting for them indefinitely.
frame_sink_manager_->DiscardPendingCopyOfOutputRequests(this);
// Prevent missing begin frames from being sent to sinks that came late,
// as this may result in two overlapping frames being sent, which is not
// supported with full pipeline mode.
last_begin_frame_args_ = BeginFrameArgs();
std::move(pending_frame_callback_).Run(ack);
}
void ExternalBeginFrameSourceMojo::OnDisplayDidFinishFrame(
const BeginFrameAck& ack) {
if (!pending_frame_callback_)
return;
if (!pending_frame_sinks_.empty()) {
DCHECK(!pending_ack_);
pending_ack_ = ack;
return;
}
DispatchFrameCallback(ack);
}
void ExternalBeginFrameSourceMojo::OnDisplayDestroyed() {
// As part of destruction, we are automatically removed as a display
// observer. No need to call RemoveObserver.
display_ = nullptr;
}
void ExternalBeginFrameSourceMojo::SetDisplay(Display* display) {
if (display_)
display_->RemoveObserver(this);
display_ = display;
if (display_)
display_->AddObserver(this);
}
void ExternalBeginFrameSourceMojo::OnNeedsBeginFrames(bool needs_begin_frames) {
if (remote_client_) {
remote_client_->SetNeedsBeginFrame(needs_begin_frames);
}
}
void ExternalBeginFrameSourceMojo::SetPreferredInterval(
base::TimeDelta interval) {
if (remote_client_) {
remote_client_->SetPreferredInterval(interval);
}
}
} // namespace viz
|