From c7a656aca9dbf31886729878dcb3c255880f10c8 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 24 Nov 2014 16:21:53 -0800 Subject: [PATCH] Surfaces should acknowledge frame submissions Previously, there was no way for clients of the surfaces service to appropriately rate-limit their frame submissions. We tried using the "return resources" signal to rate-limit, but that's really a measure of how quickly you're submitting new resources rather than how quickly the system is putting up frames. Currently, only the Sky compositor listens to this signal. Using this signal, we're able to run sky/examples/spinning-square.sky in sync with the surfaces service (that is, submitting exactly one frames every 17ms). R=jamesr@chromium.org Review URL: https://codereview.chromium.org/756673004 --- engine/src/flutter/compositor/BUILD.gn | 1 - engine/src/flutter/compositor/layer_host.cc | 74 ++++++++++--------- engine/src/flutter/compositor/layer_host.h | 23 +++--- .../src/flutter/compositor/surface_holder.cc | 6 +- .../src/flutter/compositor/surface_holder.h | 4 +- 5 files changed, 57 insertions(+), 51 deletions(-) diff --git a/engine/src/flutter/compositor/BUILD.gn b/engine/src/flutter/compositor/BUILD.gn index 575593e45f..be370a8b83 100644 --- a/engine/src/flutter/compositor/BUILD.gn +++ b/engine/src/flutter/compositor/BUILD.gn @@ -38,7 +38,6 @@ static_library("compositor") { "//mojo/services/public/interfaces/surfaces:surface_id", "//mojo/skia", "//skia", - "//sky/scheduler", "//ui/gfx/geometry", ] } diff --git a/engine/src/flutter/compositor/layer_host.cc b/engine/src/flutter/compositor/layer_host.cc index 4045214346..a4d5a0897e 100644 --- a/engine/src/flutter/compositor/layer_host.cc +++ b/engine/src/flutter/compositor/layer_host.cc @@ -16,22 +16,24 @@ namespace sky { LayerHost::LayerHost(LayerHostClient* client) : client_(client), - state_(kIdle), + state_(kWaitingForSurfaceService), + frame_requested_(false), surface_holder_(this, client->GetShell()), gl_context_(mojo::GLContext::Create(client->GetShell())), ganesh_context_(gl_context_), resource_manager_(gl_context_), - scheduler_(this, base::MessageLoop::current()->task_runner()) { - scheduler_.UpdateVSync( - TimeInterval(base::TimeTicks(), base::TimeDelta::FromSecondsD(1.0 / 60))); + weak_factory_(this) { } LayerHost::~LayerHost() { } void LayerHost::SetNeedsAnimate() { - scheduler_.SetNeedsFrame(); - state_ = kWaitingForBeginFrame; + if (frame_requested_) + return; + frame_requested_ = true; + if (state_ == kReadyForFrame) + BeginFrameSoon(); } void LayerHost::SetRootLayer(scoped_refptr layer) { @@ -40,11 +42,15 @@ void LayerHost::SetRootLayer(scoped_refptr layer) { root_layer_->set_host(this); } +void LayerHost::OnSurfaceConnectionCreated() { + DCHECK_EQ(state_, kWaitingForSurfaceService); + state_ = kReadyForFrame; + if (frame_requested_) + BeginFrameSoon(); +} + void LayerHost::OnSurfaceIdAvailable(mojo::SurfaceIdPtr surface_id) { client_->OnSurfaceIdAvailable(surface_id.Pass()); - - if (state_ == kWaitingForSurfaceToUploadFrame) - Upload(root_layer_.get()); } void LayerHost::ReturnResources( @@ -52,14 +58,22 @@ void LayerHost::ReturnResources( resource_manager_.ReturnResources(resources.Pass()); } -void LayerHost::BeginFrame(base::TimeTicks frame_time, - base::TimeTicks deadline) { +void LayerHost::BeginFrameSoon() { + base::MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind(&LayerHost::BeginFrame, weak_factory_.GetWeakPtr())); +} +void LayerHost::BeginFrame() { TRACE_EVENT0("sky", "LayerHost::BeginFrame"); - DCHECK_EQ(state_, kWaitingForBeginFrame); - state_ = kProducingFrame; - client_->BeginFrame(frame_time); + DCHECK(frame_requested_); + frame_requested_ = false; + + DCHECK_EQ(state_, kReadyForFrame); + state_ = kWaitingForFrameAcknowldgement; + + client_->BeginFrame(base::TimeTicks::Now()); { mojo::GaneshContext::Scope scope(&ganesh_context_); @@ -68,32 +82,11 @@ void LayerHost::BeginFrame(base::TimeTicks frame_time, } Upload(root_layer_.get()); - - if (state_ == kProducingFrame) - state_ = kIdle; } void LayerHost::Upload(Layer* layer) { TRACE_EVENT0("sky", "LayerHost::Upload"); - if (!surface_holder_.IsReadyForFrame()) { - if (state_ == kProducingFrame) { - // Currently we use a timer to drive the BeginFrame cycle, which means we - // can produce frames before the surfaces service is ready to receive - // frames from us. In that situation, we wait for surfaces before - // uploading the frame. The upload will actually happen when the surface - // id is available (i.e., in OnSurfaceIdAvailable). If SetNeedsAnimate is - // called before then, we'll go back into the kWaitingForBeginFrame state - // and defer to the timer again. - // - // We can avoid this complexity if we use feedback from the surfaces - // service to drive the BeginFrame cycle. In that approach, we wouldn't - // get here before we've attached to a surface. - state_ = kWaitingForSurfaceToUploadFrame; - } - return; - } - gfx::Size size = layer->size(); surface_holder_.SetSize(size); @@ -141,7 +134,16 @@ void LayerHost::Upload(Layer* layer) { pass->quads.push_back(quad.Pass()); frame->passes.push_back(pass.Pass()); - surface_holder_.SubmitFrame(frame.Pass()); + surface_holder_.SubmitFrame( + frame.Pass(), + base::Bind(&LayerHost::DidCompleteFrame, weak_factory_.GetWeakPtr())); +} + +void LayerHost::DidCompleteFrame() { + DCHECK_EQ(state_, kWaitingForFrameAcknowldgement); + state_ = kReadyForFrame; + if (frame_requested_) + BeginFrame(); } } // namespace sky diff --git a/engine/src/flutter/compositor/layer_host.h b/engine/src/flutter/compositor/layer_host.h index 17b8ae58d2..2f8ccdbeee 100644 --- a/engine/src/flutter/compositor/layer_host.h +++ b/engine/src/flutter/compositor/layer_host.h @@ -7,18 +7,18 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" +#include "base/memory/weak_ptr.h" #include "mojo/skia/ganesh_context.h" #include "sky/compositor/layer_host_client.h" #include "sky/compositor/resource_manager.h" #include "sky/compositor/surface_holder.h" -#include "sky/scheduler/scheduler.h" namespace sky { class ResourceManager; class Layer; class LayerHostClient; -class LayerHost : public SurfaceHolder::Client, public Scheduler::Client { +class LayerHost : public SurfaceHolder::Client { public: explicit LayerHost(LayerHostClient* client); ~LayerHost(); @@ -42,33 +42,34 @@ class LayerHost : public SurfaceHolder::Client, public Scheduler::Client { private: enum State { - kIdle, - kWaitingForBeginFrame, - kProducingFrame, - kWaitingForSurfaceToUploadFrame, + kWaitingForSurfaceService, + kReadyForFrame, + kWaitingForFrameAcknowldgement, }; // SurfaceHolder::Client + void OnSurfaceConnectionCreated() override; void OnSurfaceIdAvailable(mojo::SurfaceIdPtr surface_id) override; void ReturnResources( mojo::Array resources) override; - // Scheduler::Client - void BeginFrame(base::TimeTicks frame_time, - base::TimeTicks deadline) override; + void BeginFrameSoon(); + void BeginFrame(); void Upload(Layer* layer); + void DidCompleteFrame(); LayerHostClient* client_; State state_; + bool frame_requested_; SurfaceHolder surface_holder_; base::WeakPtr gl_context_; mojo::GaneshContext ganesh_context_; ResourceManager resource_manager_; - Scheduler scheduler_; - scoped_refptr root_layer_; + base::WeakPtrFactory weak_factory_; + DISALLOW_COPY_AND_ASSIGN(LayerHost); }; diff --git a/engine/src/flutter/compositor/surface_holder.cc b/engine/src/flutter/compositor/surface_holder.cc index de9662209b..26d2098c96 100644 --- a/engine/src/flutter/compositor/surface_holder.cc +++ b/engine/src/flutter/compositor/surface_holder.cc @@ -36,8 +36,9 @@ bool SurfaceHolder::IsReadyForFrame() const { return surface_; } -void SurfaceHolder::SubmitFrame(mojo::FramePtr frame) { - surface_->SubmitFrame(surface_id_.Clone(), frame.Pass()); +void SurfaceHolder::SubmitFrame(mojo::FramePtr frame, + const base::Closure& callback) { + surface_->SubmitFrame(surface_id_.Clone(), frame.Pass(), callback); } void SurfaceHolder::SetSize(const gfx::Size& size) { @@ -70,6 +71,7 @@ void SurfaceHolder::OnSurfaceConnectionCreated(mojo::SurfacePtr surface, surface_ = surface.Pass(); surface_.set_client(this); surface_allocator_.reset(new SurfaceAllocator(id_namespace)); + client_->OnSurfaceConnectionCreated(); } } // namespace sky diff --git a/engine/src/flutter/compositor/surface_holder.h b/engine/src/flutter/compositor/surface_holder.h index 59a0fdb1df..4abeb3f668 100644 --- a/engine/src/flutter/compositor/surface_holder.h +++ b/engine/src/flutter/compositor/surface_holder.h @@ -5,6 +5,7 @@ #ifndef SKY_COMPOSITOR_SURFACE_HOLDER_H_ #define SKY_COMPOSITOR_SURFACE_HOLDER_H_ +#include "base/callback_forward.h" #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" #include "mojo/services/public/interfaces/surfaces/surface_id.mojom.h" @@ -23,6 +24,7 @@ class SurfaceHolder : public mojo::SurfaceClient { public: class Client { public: + virtual void OnSurfaceConnectionCreated() = 0; virtual void OnSurfaceIdAvailable(mojo::SurfaceIdPtr surface_id) = 0; virtual void ReturnResources( mojo::Array resources) = 0; @@ -37,7 +39,7 @@ class SurfaceHolder : public mojo::SurfaceClient { bool IsReadyForFrame() const; void SetSize(const gfx::Size& size); - void SubmitFrame(mojo::FramePtr frame); + void SubmitFrame(mojo::FramePtr frame, const base::Closure& callback); private: // mojo::SurfaceClient