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
This commit is contained in:
@@ -38,7 +38,6 @@ static_library("compositor") {
|
||||
"//mojo/services/public/interfaces/surfaces:surface_id",
|
||||
"//mojo/skia",
|
||||
"//skia",
|
||||
"//sky/scheduler",
|
||||
"//ui/gfx/geometry",
|
||||
]
|
||||
}
|
||||
|
||||
@@ -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> layer) {
|
||||
@@ -40,11 +42,15 @@ void LayerHost::SetRootLayer(scoped_refptr<Layer> 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
|
||||
|
||||
@@ -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<mojo::ReturnedResourcePtr> 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<mojo::GLContext> gl_context_;
|
||||
mojo::GaneshContext ganesh_context_;
|
||||
ResourceManager resource_manager_;
|
||||
Scheduler scheduler_;
|
||||
|
||||
scoped_refptr<Layer> root_layer_;
|
||||
|
||||
base::WeakPtrFactory<LayerHost> weak_factory_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(LayerHost);
|
||||
};
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<mojo::ReturnedResourcePtr> 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
|
||||
|
||||
Reference in New Issue
Block a user