Sky should use an SkPicture when drawing

This CL teaches the Sky compositor to record an SkPicture instead of just
drawing in immediate mode. Using an SkPicture will let us experiment with
various optimizations in Skia that preprocess the SkPicture before translating
the Skia commands to GL commands.

This CL also refactors the "display delegate" system now that we can hand off
an SkPicture to a rasterization backend. The new design requires fewer
backpointers and removes the static display delegate factory.

R=eseidel@google.com

Review URL: https://codereview.chromium.org/854303002
This commit is contained in:
Adam Barth
2015-01-19 18:47:53 -08:00
parent 75f62f9a49
commit 6bd6833506
16 changed files with 188 additions and 205 deletions

View File

@@ -4,12 +4,6 @@
source_set("compositor") {
sources = [
"display_delegate.cc",
"display_delegate.h",
"display_delegate_bitmap.cc",
"display_delegate_bitmap.h",
"display_delegate_ganesh.cc",
"display_delegate_ganesh.h",
"layer.cc",
"layer.h",
"layer_client.cc",
@@ -18,6 +12,12 @@ source_set("compositor") {
"layer_host.h",
"layer_host_client.cc",
"layer_host_client.h",
"rasterizer.cc",
"rasterizer.h",
"rasterizer_bitmap.cc",
"rasterizer_bitmap.h",
"rasterizer_ganesh.cc",
"rasterizer_ganesh.h",
"resource_manager.cc",
"resource_manager.h",
"surface_allocator.cc",

View File

@@ -1,24 +0,0 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sky/compositor/display_delegate.h"
namespace sky {
static CreateDisplayDelegate createDisplayDelegateFunction = 0;
void DisplayDelegate::setDisplayDelegateCreateFunction(CreateDisplayDelegate createFunction)
{
DCHECK(createFunction);
DCHECK(!createDisplayDelegateFunction);
createDisplayDelegateFunction = createFunction;
}
DisplayDelegate* DisplayDelegate::create(LayerClient* client)
{
DCHECK(createDisplayDelegateFunction);
return createDisplayDelegateFunction(client);
}
} // namespace sky

View File

@@ -1,43 +0,0 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SKY_COMPOSITOR_DISPLAY_DELEGATE_H_
#define SKY_COMPOSITOR_DISPLAY_DELEGATE_H_
#include <vector>
#include "mojo/skia/ganesh_surface.h"
class SkCanvas;
namespace gfx {
class Rect;
}
namespace sky {
class DisplayDelegate;
class LayerClient;
typedef DisplayDelegate* (*CreateDisplayDelegate)(LayerClient*);
class DisplayDelegate {
public:
DisplayDelegate() {}
virtual ~DisplayDelegate() {}
static DisplayDelegate* create(LayerClient*);
static void setDisplayDelegateCreateFunction(CreateDisplayDelegate);
virtual void GetPixelsForTesting(std::vector<unsigned char>* pixels) = 0;
virtual void Paint(mojo::GaneshSurface& surface, const gfx::Rect& size) = 0;
DISALLOW_COPY_AND_ASSIGN(DisplayDelegate);
};
} // namespace sky
#endif // SKY_COMPOSITOR_DISPLAY_DELEGATE_H_

View File

@@ -1,33 +0,0 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SKY_COMPOSITOR_DISPLAY_DELEGATE_BITMAP_H_
#define SKY_COMPOSITOR_DISPLAY_DELEGATE_BITMAP_H_
#include "sky/compositor/display_delegate.h"
namespace sky {
class LayerClient;
class DisplayDelegateBitmap final : public DisplayDelegate {
public:
explicit DisplayDelegateBitmap(LayerClient* client);
~DisplayDelegateBitmap() override;
static DisplayDelegate* create(LayerClient* client);
void GetPixelsForTesting(std::vector<unsigned char>* pixels) override;
void Paint(mojo::GaneshSurface& surface, const gfx::Rect& size) override;
private:
SkBitmap bitmap_;
LayerClient* client_;
DISALLOW_COPY_AND_ASSIGN(DisplayDelegateBitmap);
};
} // namespace sky
#endif // SKY_COMPOSITOR_DISPLAY_DELEGATE_BITMAP_H_

View File

@@ -1,27 +0,0 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sky/compositor/display_delegate_ganesh.h"
#include "sky/compositor/layer_client.h"
#include "third_party/skia/include/core/SkCanvas.h"
namespace sky {
DisplayDelegate* DisplayDelegateGanesh::create(LayerClient* client) {
return new DisplayDelegateGanesh(client);
}
void DisplayDelegateGanesh::GetPixelsForTesting(std::vector<unsigned char>* pixels) {
// TODO(ojan): When we change notifyTestComplete to only GetPixelsForTesting
// in pixel/ref tests, add a NOTREACHED() here.
}
void DisplayDelegateGanesh::Paint(mojo::GaneshSurface& surface, const gfx::Rect& size) {
SkCanvas* canvas = surface.canvas();
client_->PaintContents(canvas, size);
canvas->flush();
}
} // namespace sky

View File

@@ -1,31 +0,0 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SKY_COMPOSITOR_DISPLAY_DELEGATE_GANESH_H_
#define SKY_COMPOSITOR_DISPLAY_DELEGATE_GANESH_H_
#include "sky/compositor/display_delegate.h"
namespace sky {
class LayerClient;
class DisplayDelegateGanesh final : public DisplayDelegate {
public:
explicit DisplayDelegateGanesh(LayerClient* client) : client_(client) {}
~DisplayDelegateGanesh() override {}
static DisplayDelegate* create(LayerClient* client);
void GetPixelsForTesting(std::vector<unsigned char>* pixels) override;
void Paint(mojo::GaneshSurface& surface, const gfx::Rect& size) override;
private:
LayerClient* client_;
DISALLOW_COPY_AND_ASSIGN(DisplayDelegateGanesh);
};
} // namespace sky
#endif // SKY_COMPOSITOR_DISPLAY_DELEGATE_GANESH_H_

View File

@@ -5,17 +5,14 @@
#include "sky/compositor/layer.h"
#include "base/debug/trace_event.h"
#include "mojo/skia/ganesh_surface.h"
#include "sky/compositor/display_delegate.h"
#include "sky/compositor/layer_host.h"
#include "sky/compositor/rasterizer.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
namespace sky {
Layer::Layer(LayerClient* client)
: client_(client),
host_(nullptr) {
delegate_.reset(DisplayDelegate::create(client));
Layer::Layer(LayerClient* client) : client_(client) {
}
Layer::~Layer() {
@@ -25,22 +22,25 @@ void Layer::SetSize(const gfx::Size& size) {
size_ = size;
}
void Layer::GetPixelsForTesting(std::vector<unsigned char>* pixels) {
delegate_->GetPixelsForTesting(pixels);
}
void Layer::Display() {
TRACE_EVENT0("sky", "Layer::Display");
DCHECK(rasterizer_);
auto picture = RecordPicture();
texture_ = rasterizer_->Rasterize(picture.get());
}
DCHECK(host_);
skia::RefPtr<SkPicture> Layer::RecordPicture() {
TRACE_EVENT0("sky", "Layer::RecordPicture");
mojo::GaneshSurface surface(host_->ganesh_context(),
host_->resource_manager()->CreateTexture(size_));
SkRTreeFactory factory;
SkPictureRecorder recorder;
gfx::Rect rect(size_);
delegate_->Paint(surface, rect);
auto canvas = skia::SharePtr(recorder.beginRecording(
size_.width(), size_.height(), &factory,
SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag));
texture_ = surface.TakeTexture();
client_->PaintContents(canvas.get(), gfx::Rect(size_));
return skia::AdoptRef(recorder.endRecordingAsPicture());
}
scoped_ptr<mojo::GLTexture> Layer::GetTexture() {

View File

@@ -7,12 +7,14 @@
#include "base/memory/ref_counted.h"
#include "mojo/gpu/gl_texture.h"
#include "skia/ext/refptr.h"
#include "sky/compositor/layer_client.h"
#include "sky/compositor/rasterizer.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "ui/gfx/geometry/rect.h"
namespace sky {
class DisplayDelegate;
class LayerHost;
class Layer : public base::RefCounted<Layer> {
@@ -20,24 +22,26 @@ class Layer : public base::RefCounted<Layer> {
explicit Layer(LayerClient* client);
void SetSize(const gfx::Size& size);
void GetPixelsForTesting(std::vector<unsigned char>* pixels);
void Display();
scoped_ptr<mojo::GLTexture> GetTexture();
const gfx::Size& size() const { return size_; }
void set_host(LayerHost* host) { host_ = host; }
void set_rasterizer(scoped_ptr<Rasterizer> rasterizer) {
rasterizer_ = rasterizer.Pass();
}
private:
friend class base::RefCounted<Layer>;
~Layer();
skia::RefPtr<SkPicture> RecordPicture();
LayerClient* client_;
LayerHost* host_;
gfx::Size size_;
scoped_ptr<mojo::GLTexture> texture_;
scoped_ptr<DisplayDelegate> delegate_;
scoped_ptr<Rasterizer> rasterizer_;
DISALLOW_COPY_AND_ASSIGN(Layer);
};

View File

@@ -39,11 +39,6 @@ void LayerHost::SetNeedsAnimate() {
void LayerHost::SetRootLayer(scoped_refptr<Layer> layer) {
DCHECK(!root_layer_.get());
root_layer_ = layer;
root_layer_->set_host(this);
}
void LayerHost::GetPixelsForTesting(std::vector<unsigned char>* pixels) {
return root_layer_->GetPixelsForTesting(pixels);
}
void LayerHost::OnSurfaceIdAvailable(mojo::SurfaceIdPtr surface_id) {

View File

@@ -41,8 +41,6 @@ class LayerHost : public SurfaceHolder::Client {
void SetNeedsAnimate();
void SetRootLayer(scoped_refptr<Layer> layer);
void GetPixelsForTesting(std::vector<unsigned char>* pixels);
private:
enum State {
kReadyForFrame,

View File

@@ -0,0 +1,15 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sky/compositor/rasterizer.h"
namespace sky {
Rasterizer::Rasterizer() {
}
Rasterizer::~Rasterizer() {
}
} // namespace sky

View File

@@ -0,0 +1,28 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SKY_COMPOSITOR_RASTERIZER_H_
#define SKY_COMPOSITOR_RASTERIZER_H_
#include "base/memory/scoped_ptr.h"
#include "mojo/gpu/gl_texture.h"
class SkPicture;
namespace sky {
class Rasterizer {
public:
Rasterizer();
virtual ~Rasterizer();
virtual scoped_ptr<mojo::GLTexture> Rasterize(SkPicture* picture) = 0;
private:
DISALLOW_COPY_AND_ASSIGN(Rasterizer);
};
} // namespace sky
#endif // SKY_COMPOSITOR_RASTERIZER_H_

View File

@@ -1,39 +1,43 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sky/compositor/display_delegate_bitmap.h"
#include "sky/compositor/rasterizer_bitmap.h"
#include "sky/compositor/layer_client.h"
#include "sky/compositor/layer_host.h"
#include "third_party/skia/include/core/SkBitmapDevice.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/geometry/rect.h"
namespace sky {
DisplayDelegateBitmap::DisplayDelegateBitmap(LayerClient* client)
: client_(client) {}
DisplayDelegateBitmap::~DisplayDelegateBitmap() {}
DisplayDelegate* DisplayDelegateBitmap::create(LayerClient* client) {
return new DisplayDelegateBitmap(client);
RasterizerBitmap::RasterizerBitmap(LayerHost* host) : host_(host) {
DCHECK(host_);
}
void DisplayDelegateBitmap::GetPixelsForTesting(std::vector<unsigned char>* pixels) {
RasterizerBitmap::~RasterizerBitmap() {
}
void RasterizerBitmap::GetPixelsForTesting(std::vector<unsigned char>* pixels) {
gfx::PNGCodec::EncodeBGRASkBitmap(bitmap_, true, pixels);
}
void DisplayDelegateBitmap::Paint(mojo::GaneshSurface& surface, const gfx::Rect& size) {
scoped_ptr<mojo::GLTexture> RasterizerBitmap::Rasterize(SkPicture* picture) {
auto size = picture->cullRect();
bitmap_.allocN32Pixels(size.width(), size.height());
SkBitmapDevice device(bitmap_);
SkCanvas canvas(&device);
// Draw red so we can see when we fail to paint.
canvas.drawColor(SK_ColorRED);
client_->PaintContents(&canvas, size);
canvas.drawPicture(picture);
canvas.flush();
return host_->resource_manager()->CreateTexture(
gfx::Size(size.width(), size.height()));
}
} // namespace sky

View File

@@ -0,0 +1,31 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SKY_COMPOSITOR_DISPLAY_RASTERIZER_BITMAP_H_
#define SKY_COMPOSITOR_DISPLAY_RASTERIZER_BITMAP_H_
#include "sky/compositor/rasterizer.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace sky {
class LayerHost;
class RasterizerBitmap : public Rasterizer {
public:
explicit RasterizerBitmap(LayerHost* host);
~RasterizerBitmap() override;
scoped_ptr<mojo::GLTexture> Rasterize(SkPicture* picture) override;
void GetPixelsForTesting(std::vector<unsigned char>* pixels);
private:
LayerHost* host_;
SkBitmap bitmap_;
DISALLOW_COPY_AND_ASSIGN(RasterizerBitmap);
};
} // namespace sky
#endif // SKY_COMPOSITOR_DISPLAY_RASTERIZER_BITMAP_H_

View File

@@ -0,0 +1,38 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sky/compositor/rasterizer_ganesh.h"
#include "base/debug/trace_event.h"
#include "mojo/skia/ganesh_surface.h"
#include "sky/compositor/layer_host.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPicture.h"
namespace sky {
RasterizerGanesh::RasterizerGanesh(LayerHost* host) : host_(host) {
DCHECK(host_);
}
RasterizerGanesh::~RasterizerGanesh() {
}
scoped_ptr<mojo::GLTexture> RasterizerGanesh::Rasterize(SkPicture* picture) {
TRACE_EVENT0("sky", "RasterizerGanesh::Rasterize");
SkRect cull_rect = picture->cullRect();
gfx::Size size(cull_rect.width(), cull_rect.height());
mojo::GaneshSurface surface(host_->ganesh_context(),
host_->resource_manager()->CreateTexture(size));
SkCanvas* canvas = surface.canvas();
canvas->drawPicture(picture);
canvas->flush();
return surface.TakeTexture();
}
} // namespace sky

View File

@@ -0,0 +1,28 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SKY_COMPOSITOR_DISPLAY_RASTERIZER_GANESH_H_
#define SKY_COMPOSITOR_DISPLAY_RASTERIZER_GANESH_H_
#include "sky/compositor/rasterizer.h"
namespace sky {
class LayerHost;
class RasterizerGanesh : public Rasterizer {
public:
explicit RasterizerGanesh(LayerHost* host);
~RasterizerGanesh() override;
scoped_ptr<mojo::GLTexture> Rasterize(SkPicture* picture) override;
private:
LayerHost* host_;
DISALLOW_COPY_AND_ASSIGN(RasterizerGanesh);
};
} // namespace sky
#endif // SKY_COMPOSITOR_DISPLAY_RASTERIZER_GANESH_H_