Commit 1b0f7d5e authored by dfalcantara's avatar dfalcantara Committed by Commit bot
Browse files

Revert of android: Remove custom browser compositor scheduling logic (patchset...

Revert of android: Remove custom browser compositor scheduling logic (patchset #6 id:100001 of https://codereview.chromium.org/1513933003/ )

Reason for revert:
Speculatively reverting CL to try and fix observer flakiness.
https://crbug.com/571030

Apologies if I'm off base!

Original issue's description:
> android: Remove custom browser compositor scheduling logic
>
> Use cc scheduler instead without a deadline.
> This is mostly obsolete now that Surfaces allow independent updates
> between renderer and browser.
>
> BUG=455894
>
> Committed: https://crrev.com/7f045e4f2e9f7142c79e2eeb30b70716a1ea8f0b
> Cr-Commit-Position: refs/heads/master@{#365861}

TBR=brianderson@chromium.org,sievers@chromium.org
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=455894

Review URL: https://codereview.chromium.org/1536323003

Cr-Commit-Position: refs/heads/master@{#366437}
parent dac8ac50
......@@ -69,6 +69,7 @@ namespace content {
namespace {
const unsigned int kMaxUiSwapBuffers = 1U;
const unsigned int kMaxDisplaySwapBuffers = 1U;
// Used to override capabilities_.adjust_deadline_for_parent to false
......@@ -141,7 +142,8 @@ class OutputSurfaceWithoutParent : public cc::OutputSurface,
OutputSurface::OnSwapBuffersComplete();
}
void OnVSync(base::TimeTicks timebase, base::TimeDelta interval) override {
void OnUpdateVSyncParameters(base::TimeTicks timebase,
base::TimeDelta interval) override {
CommitVSyncParameters(timebase, interval);
}
......@@ -153,36 +155,6 @@ class OutputSurfaceWithoutParent : public cc::OutputSurface,
scoped_ptr<cc::OverlayCandidateValidator> overlay_candidate_validator_;
};
class ExternalBeginFrameSource : public cc::BeginFrameSourceBase,
public CompositorImpl::VSyncObserver {
public:
ExternalBeginFrameSource(CompositorImpl* compositor)
: compositor_(compositor) {
compositor_->AddObserver(this);
}
~ExternalBeginFrameSource() override {
compositor_->RemoveObserver(this);
}
// cc::BeginFrameSourceBase implementation:
void OnNeedsBeginFramesChange(
bool needs_begin_frames) override {
compositor_->OnNeedsBeginFramesChange(needs_begin_frames);
}
// CompositorImpl::VSyncObserver implementation:
void OnVSync(base::TimeTicks frame_time,
base::TimeDelta vsync_period) override {
CallOnBeginFrame(cc::BeginFrameArgs::Create(
BEGINFRAME_FROM_HERE, frame_time, base::TimeTicks::Now(), vsync_period,
cc::BeginFrameArgs::NORMAL));
}
private:
CompositorImpl* compositor_;
};
static bool g_initialized = false;
bool g_use_surface_manager = false;
......@@ -265,11 +237,15 @@ CompositorImpl::CompositorImpl(CompositorClient* client,
surface_id_(0),
client_(client),
root_window_(root_window),
did_post_swapbuffers_(false),
ignore_schedule_composite_(false),
needs_composite_(false),
needs_animate_(false),
will_composite_immediately_(false),
composite_on_vsync_trigger_(DO_NOT_COMPOSITE),
pending_swapbuffers_(0U),
num_successive_context_creation_failures_(0),
output_surface_request_pending_(false),
needs_begin_frames_(false),
weak_factory_(this) {
DCHECK(client);
DCHECK(root_window);
......@@ -284,6 +260,111 @@ CompositorImpl::~CompositorImpl() {
SetSurface(NULL);
}
void CompositorImpl::PostComposite(CompositingTrigger trigger) {
DCHECK(host_->visible());
DCHECK(needs_composite_);
DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY);
if (will_composite_immediately_ ||
(trigger == COMPOSITE_EVENTUALLY && WillComposite())) {
// We will already composite soon enough.
DCHECK(WillComposite());
return;
}
if (DidCompositeThisFrame()) {
DCHECK(!WillCompositeThisFrame());
if (composite_on_vsync_trigger_ != COMPOSITE_IMMEDIATELY) {
composite_on_vsync_trigger_ = trigger;
root_window_->RequestVSyncUpdate();
}
DCHECK(WillComposite());
return;
}
base::TimeDelta delay;
if (trigger == COMPOSITE_IMMEDIATELY) {
will_composite_immediately_ = true;
composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
} else {
DCHECK(!WillComposite());
const base::TimeDelta estimated_composite_time = vsync_period_ / 4;
const base::TimeTicks now = base::TimeTicks::Now();
if (!last_vsync_.is_null() && (now - last_vsync_) < vsync_period_) {
base::TimeTicks next_composite =
last_vsync_ + vsync_period_ - estimated_composite_time;
if (next_composite < now) {
// It's too late, we will reschedule composite as needed on the next
// vsync.
composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
root_window_->RequestVSyncUpdate();
DCHECK(WillComposite());
return;
}
delay = next_composite - now;
}
}
TRACE_EVENT2("cc,benchmark", "CompositorImpl::PostComposite",
"trigger", trigger,
"delay", delay.InMillisecondsF());
DCHECK(composite_on_vsync_trigger_ == DO_NOT_COMPOSITE);
if (current_composite_task_)
current_composite_task_->Cancel();
// Unretained because we cancel the task on shutdown.
current_composite_task_.reset(new base::CancelableClosure(
base::Bind(&CompositorImpl::Composite, base::Unretained(this), trigger)));
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, current_composite_task_->callback(), delay);
}
void CompositorImpl::Composite(CompositingTrigger trigger) {
if (trigger == COMPOSITE_IMMEDIATELY)
will_composite_immediately_ = false;
DCHECK(host_->visible());
DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY);
DCHECK(needs_composite_);
DCHECK(!DidCompositeThisFrame());
DCHECK_LE(pending_swapbuffers_, kMaxUiSwapBuffers);
// Swap Ack accounting is unreliable if the OutputSurface was lost.
// In that case still attempt to composite, which will cause creation of a
// new OutputSurface and reset pending_swapbuffers_.
if (pending_swapbuffers_ == kMaxUiSwapBuffers &&
!host_->output_surface_lost()) {
TRACE_EVENT0("compositor", "CompositorImpl_SwapLimit");
return;
}
// Reset state before Layout+Composite since that might create more
// requests to Composite that we need to respect.
needs_composite_ = false;
// Only allow compositing once per vsync.
current_composite_task_->Cancel();
DCHECK(DidCompositeThisFrame() && !WillComposite());
const base::TimeTicks frame_time = base::TimeTicks::Now();
if (needs_animate_) {
base::AutoReset<bool> auto_reset_ignore_schedule(
&ignore_schedule_composite_, true);
needs_animate_ = false;
root_window_->Animate(frame_time);
}
did_post_swapbuffers_ = false;
host_->Composite(frame_time);
if (did_post_swapbuffers_)
pending_swapbuffers_++;
// Need to track vsync to avoid compositing more than once per frame.
root_window_->RequestVSyncUpdate();
}
ui::UIResourceProvider& CompositorImpl::GetUIResourceProvider() {
return *this;
}
......@@ -344,20 +425,27 @@ void CompositorImpl::SetSurface(jobject surface) {
void CompositorImpl::CreateLayerTreeHost() {
DCHECK(!host_);
DCHECK(!WillCompositeThisFrame());
// Just in case, since we immediately hide the LTH in this function,
// and we do not want to end up with a pending Composite task when the
// host is hidden.
base::AutoReset<bool> auto_reset_ignore_schedule(&ignore_schedule_composite_,
true);
cc::LayerTreeSettings settings;
settings.renderer_settings.refresh_rate = 60.0;
settings.renderer_settings.allow_antialiasing = false;
settings.renderer_settings.highp_threshold_min = 2048;
settings.use_zero_copy = true;
settings.use_external_begin_frame_source = true;
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
settings.initial_debug_state.SetRecordRenderingStats(
command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
if (command_line->HasSwitch(cc::switches::kDisableCompositorPropertyTrees))
settings.use_property_trees = false;
settings.single_thread_proxy_scheduler = true;
// TODO(enne): Update this this compositor to use the scheduler.
settings.single_thread_proxy_scheduler = false;
settings.use_compositor_animation_timelines = !command_line->HasSwitch(
switches::kDisableAndroidCompositorAnimationTimelines);
......@@ -369,7 +457,6 @@ void CompositorImpl::CreateLayerTreeHost() {
params.task_graph_runner = g_task_graph_runner.Pointer();
params.main_task_runner = base::ThreadTaskRunnerHandle::Get();
params.settings = &settings;
params.external_begin_frame_source.reset(new ExternalBeginFrameSource(this));
host_ = cc::LayerTreeHost::CreateSingleThreaded(this, &params);
DCHECK(!host_->visible());
host_->SetRootLayer(root_layer_);
......@@ -387,16 +474,39 @@ void CompositorImpl::SetVisible(bool visible) {
TRACE_EVENT1("cc", "CompositorImpl::SetVisible", "visible", visible);
if (!visible) {
DCHECK(host_->visible());
// Look for any layers that were attached to the root for readback
// and are waiting for Composite() to happen.
bool readback_pending = false;
for (size_t i = 0; i < root_layer_->children().size(); ++i) {
if (root_layer_->children()[i]->HasCopyRequest()) {
readback_pending = true;
break;
}
}
if (readback_pending) {
base::AutoReset<bool> auto_reset_ignore_schedule(
&ignore_schedule_composite_, true);
host_->Composite(base::TimeTicks::Now());
}
if (WillComposite())
CancelComposite();
host_->SetVisible(false);
if (!host_->output_surface_lost())
host_->ReleaseOutputSurface();
pending_swapbuffers_ = 0;
needs_composite_ = false;
composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
establish_gpu_channel_timeout_.Stop();
display_client_.reset();
if (current_composite_task_) {
current_composite_task_->Cancel();
current_composite_task_.reset();
}
} else {
host_->SetVisible(true);
if (output_surface_request_pending_)
RequestNewOutputSurface();
SetNeedsComposite();
}
}
......@@ -427,7 +537,10 @@ void CompositorImpl::SetHasTransparentBackground(bool flag) {
void CompositorImpl::SetNeedsComposite() {
if (!host_->visible())
return;
host_->SetNeedsAnimate();
DCHECK(!needs_composite_ || WillComposite());
needs_composite_ = true;
PostComposite(COMPOSITE_IMMEDIATELY);
}
static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
......@@ -461,11 +574,9 @@ CreateGpuProcessViewContext(
}
void CompositorImpl::UpdateLayerTreeHost() {
base::AutoReset<bool> auto_reset_ignore_schedule(&ignore_schedule_composite_,
true);
client_->UpdateLayerTreeHost();
if (needs_animate_) {
needs_animate_ = false;
root_window_->Animate(base::TimeTicks::Now());
}
}
void CompositorImpl::OnGpuChannelEstablished() {
......@@ -595,15 +706,44 @@ bool CompositorImpl::SupportsETC1NonPowerOfTwo() const {
return gpu_capabilities_.texture_format_etc1_npot;
}
void CompositorImpl::ScheduleComposite() {
if (ignore_schedule_composite_ || !host_->visible())
return;
DCHECK(!needs_composite_ || WillComposite());
needs_composite_ = true;
// We currently expect layer tree invalidations at most once per frame
// during normal operation and therefore try to composite immediately
// to minimize latency.
PostComposite(COMPOSITE_IMMEDIATELY);
}
void CompositorImpl::ScheduleAnimation() {
needs_animate_ = true;
if (!host_->visible())
return;
if (needs_composite_) {
DCHECK(WillComposite());
return;
}
TRACE_EVENT0("cc", "CompositorImpl::ScheduleAnimation");
needs_composite_ = true;
PostComposite(COMPOSITE_EVENTUALLY);
}
void CompositorImpl::DidPostSwapBuffers() {
TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers");
pending_swapbuffers_++;
did_post_swapbuffers_ = true;
}
void CompositorImpl::DidCompleteSwapBuffers() {
TRACE_EVENT0("compositor", "CompositorImpl::DidCompleteSwapBuffers");
DCHECK_GT(pending_swapbuffers_, 0U);
pending_swapbuffers_--;
if (pending_swapbuffers_-- == kMaxUiSwapBuffers && needs_composite_)
PostComposite(COMPOSITE_IMMEDIATELY);
client_->OnSwapBuffersCompleted(pending_swapbuffers_);
}
......@@ -612,8 +752,7 @@ void CompositorImpl::DidAbortSwapBuffers() {
// This really gets called only once from
// SingleThreadProxy::DidLoseOutputSurfaceOnImplThread() when the
// context was lost.
if (host_->visible())
host_->SetNeedsCommit();
ScheduleComposite();
client_->OnSwapBuffersCompleted(0);
}
......@@ -632,19 +771,28 @@ void CompositorImpl::RequestCopyOfOutputOnRootLayer(
void CompositorImpl::OnVSync(base::TimeTicks frame_time,
base::TimeDelta vsync_period) {
FOR_EACH_OBSERVER(VSyncObserver, observer_list_,
OnVSync(frame_time, vsync_period));
if (needs_begin_frames_)
root_window_->RequestVSyncUpdate();
}
vsync_period_ = vsync_period;
last_vsync_ = frame_time;
if (WillCompositeThisFrame()) {
// We somehow missed the last vsync interval, so reschedule for deadline.
// We cannot schedule immediately, or will get us out-of-phase with new
// renderer frames.
CancelComposite();
composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
} else {
current_composite_task_.reset();
}
void CompositorImpl::OnNeedsBeginFramesChange(bool needs_begin_frames) {
if (needs_begin_frames_ == needs_begin_frames)
return;
DCHECK(!DidCompositeThisFrame() && !WillCompositeThisFrame());
if (composite_on_vsync_trigger_ != DO_NOT_COMPOSITE) {
CompositingTrigger trigger = composite_on_vsync_trigger_;
composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
PostComposite(trigger);
}
needs_begin_frames_ = needs_begin_frames;
if (needs_begin_frames_)
root_window_->RequestVSyncUpdate();
FOR_EACH_OBSERVER(VSyncObserver, observer_list_,
OnUpdateVSyncParameters(frame_time, vsync_period));
}
void CompositorImpl::SetNeedsAnimate() {
......
......@@ -48,8 +48,8 @@ class CONTENT_EXPORT CompositorImpl
public:
class VSyncObserver {
public:
virtual void OnVSync(base::TimeTicks timebase,
base::TimeDelta interval) = 0;
virtual void OnUpdateVSyncParameters(base::TimeTicks timebase,
base::TimeDelta interval) = 0;
};
CompositorImpl(CompositorClient* client, gfx::NativeWindow root_window);
......@@ -64,7 +64,6 @@ class CONTENT_EXPORT CompositorImpl
void AddObserver(VSyncObserver* observer);
void RemoveObserver(VSyncObserver* observer);
void OnNeedsBeginFramesChange(bool needs_begin_frames);
// ui::ResourceProvider implementation.
cc::UIResourceId CreateUIResource(cc::UIResourceClient* client) override;
......@@ -107,6 +106,8 @@ class CONTENT_EXPORT CompositorImpl
override {}
// LayerTreeHostSingleThreadClient implementation.
void ScheduleComposite() override;
void ScheduleAnimation() override;
void DidPostSwapBuffers() override;
void DidAbortSwapBuffers() override;
......@@ -117,8 +118,38 @@ class CONTENT_EXPORT CompositorImpl
void OnVSync(base::TimeTicks frame_time,
base::TimeDelta vsync_period) override;
void SetNeedsAnimate() override;
void SetVisible(bool visible);
enum CompositingTrigger {
DO_NOT_COMPOSITE,
COMPOSITE_IMMEDIATELY,
COMPOSITE_EVENTUALLY,
};
void PostComposite(CompositingTrigger trigger);
void Composite(CompositingTrigger trigger);
void CreateOutputSurface();
bool WillCompositeThisFrame() const {
return current_composite_task_ &&
!current_composite_task_->callback().is_null();
}
bool DidCompositeThisFrame() const {
return current_composite_task_ &&
current_composite_task_->callback().is_null();
}
bool WillComposite() const {
return WillCompositeThisFrame() ||
composite_on_vsync_trigger_ != DO_NOT_COMPOSITE;
}
void CancelComposite() {
DCHECK(WillComposite());
if (WillCompositeThisFrame())
current_composite_task_->Cancel();
current_composite_task_.reset();
composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
will_composite_immediately_ = false;
}
void CreateLayerTreeHost();
void OnGpuChannelEstablished();
......@@ -146,15 +177,39 @@ class CONTENT_EXPORT CompositorImpl
gfx::NativeWindow root_window_;
// Used locally to track whether a call to LTH::Composite() did result in
// a posted SwapBuffers().
bool did_post_swapbuffers_;
// Used locally to inhibit ScheduleComposite() during
// UpdateLayerTreeHost().
bool ignore_schedule_composite_;
// Whether we need to composite in general because of any invalidation or
// explicit request.
bool needs_composite_;
// Whether we need to update animations on the next composite.
bool needs_animate_;
// Whether we posted a task and are about to composite.
bool will_composite_immediately_;
// How we should schedule Composite during the next vsync.
CompositingTrigger composite_on_vsync_trigger_;
// The Composite operation scheduled for the current vsync interval.
scoped_ptr<base::CancelableClosure> current_composite_task_;
// The number of SwapBuffer calls that have not returned and ACK'd from
// the GPU thread.
unsigned int pending_swapbuffers_;
size_t num_successive_context_creation_failures_;
base::TimeDelta vsync_period_;
base::TimeTicks last_vsync_;
base::OneShotTimer establish_gpu_channel_timeout_;
// Whether there is an OutputSurface request pending from the current
......@@ -165,7 +220,6 @@ class CONTENT_EXPORT CompositorImpl
gpu::Capabilities gpu_capabilities_;
bool needs_begin_frames_;
base::ObserverList<VSyncObserver, true> observer_list_;
base::WeakPtrFactory<CompositorImpl> weak_factory_;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment