From 6bd6833506d9f0b5eb3b8252f5bd42d7ab1a87db Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 19 Jan 2015 18:47:53 -0800 Subject: [PATCH] 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 --- engine/src/flutter/compositor/BUILD.gn | 12 +++--- .../flutter/compositor/display_delegate.cc | 24 ----------- .../src/flutter/compositor/display_delegate.h | 43 ------------------- .../compositor/display_delegate_bitmap.h | 33 -------------- .../compositor/display_delegate_ganesh.cc | 27 ------------ .../compositor/display_delegate_ganesh.h | 31 ------------- engine/src/flutter/compositor/layer.cc | 32 +++++++------- engine/src/flutter/compositor/layer.h | 14 +++--- engine/src/flutter/compositor/layer_host.cc | 5 --- engine/src/flutter/compositor/layer_host.h | 2 - engine/src/flutter/compositor/rasterizer.cc | 15 +++++++ engine/src/flutter/compositor/rasterizer.h | 28 ++++++++++++ ...elegate_bitmap.cc => rasterizer_bitmap.cc} | 30 +++++++------ .../flutter/compositor/rasterizer_bitmap.h | 31 +++++++++++++ .../flutter/compositor/rasterizer_ganesh.cc | 38 ++++++++++++++++ .../flutter/compositor/rasterizer_ganesh.h | 28 ++++++++++++ 16 files changed, 188 insertions(+), 205 deletions(-) delete mode 100644 engine/src/flutter/compositor/display_delegate.cc delete mode 100644 engine/src/flutter/compositor/display_delegate.h delete mode 100644 engine/src/flutter/compositor/display_delegate_bitmap.h delete mode 100644 engine/src/flutter/compositor/display_delegate_ganesh.cc delete mode 100644 engine/src/flutter/compositor/display_delegate_ganesh.h create mode 100644 engine/src/flutter/compositor/rasterizer.cc create mode 100644 engine/src/flutter/compositor/rasterizer.h rename engine/src/flutter/compositor/{display_delegate_bitmap.cc => rasterizer_bitmap.cc} (50%) create mode 100644 engine/src/flutter/compositor/rasterizer_bitmap.h create mode 100644 engine/src/flutter/compositor/rasterizer_ganesh.cc create mode 100644 engine/src/flutter/compositor/rasterizer_ganesh.h diff --git a/engine/src/flutter/compositor/BUILD.gn b/engine/src/flutter/compositor/BUILD.gn index 71a4888359..6f037aca41 100644 --- a/engine/src/flutter/compositor/BUILD.gn +++ b/engine/src/flutter/compositor/BUILD.gn @@ -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", diff --git a/engine/src/flutter/compositor/display_delegate.cc b/engine/src/flutter/compositor/display_delegate.cc deleted file mode 100644 index c868558424..0000000000 --- a/engine/src/flutter/compositor/display_delegate.cc +++ /dev/null @@ -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 diff --git a/engine/src/flutter/compositor/display_delegate.h b/engine/src/flutter/compositor/display_delegate.h deleted file mode 100644 index 800e4c7562..0000000000 --- a/engine/src/flutter/compositor/display_delegate.h +++ /dev/null @@ -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 - -#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* 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_ diff --git a/engine/src/flutter/compositor/display_delegate_bitmap.h b/engine/src/flutter/compositor/display_delegate_bitmap.h deleted file mode 100644 index 4df1cf90a5..0000000000 --- a/engine/src/flutter/compositor/display_delegate_bitmap.h +++ /dev/null @@ -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* 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_ diff --git a/engine/src/flutter/compositor/display_delegate_ganesh.cc b/engine/src/flutter/compositor/display_delegate_ganesh.cc deleted file mode 100644 index 0d93c968b2..0000000000 --- a/engine/src/flutter/compositor/display_delegate_ganesh.cc +++ /dev/null @@ -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* 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 diff --git a/engine/src/flutter/compositor/display_delegate_ganesh.h b/engine/src/flutter/compositor/display_delegate_ganesh.h deleted file mode 100644 index 1b2deb58e5..0000000000 --- a/engine/src/flutter/compositor/display_delegate_ganesh.h +++ /dev/null @@ -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* 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_ diff --git a/engine/src/flutter/compositor/layer.cc b/engine/src/flutter/compositor/layer.cc index 1142093f0c..c1bac833b6 100644 --- a/engine/src/flutter/compositor/layer.cc +++ b/engine/src/flutter/compositor/layer.cc @@ -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* 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 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 Layer::GetTexture() { diff --git a/engine/src/flutter/compositor/layer.h b/engine/src/flutter/compositor/layer.h index 0f478bb9d1..761d2bdbf8 100644 --- a/engine/src/flutter/compositor/layer.h +++ b/engine/src/flutter/compositor/layer.h @@ -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 { @@ -20,24 +22,26 @@ class Layer : public base::RefCounted { explicit Layer(LayerClient* client); void SetSize(const gfx::Size& size); - void GetPixelsForTesting(std::vector* pixels); void Display(); scoped_ptr GetTexture(); const gfx::Size& size() const { return size_; } - void set_host(LayerHost* host) { host_ = host; } + void set_rasterizer(scoped_ptr rasterizer) { + rasterizer_ = rasterizer.Pass(); + } private: friend class base::RefCounted; ~Layer(); + skia::RefPtr RecordPicture(); + LayerClient* client_; - LayerHost* host_; gfx::Size size_; scoped_ptr texture_; - scoped_ptr delegate_; + scoped_ptr rasterizer_; DISALLOW_COPY_AND_ASSIGN(Layer); }; diff --git a/engine/src/flutter/compositor/layer_host.cc b/engine/src/flutter/compositor/layer_host.cc index d8ca387d14..697ed265f0 100644 --- a/engine/src/flutter/compositor/layer_host.cc +++ b/engine/src/flutter/compositor/layer_host.cc @@ -39,11 +39,6 @@ void LayerHost::SetNeedsAnimate() { void LayerHost::SetRootLayer(scoped_refptr layer) { DCHECK(!root_layer_.get()); root_layer_ = layer; - root_layer_->set_host(this); -} - -void LayerHost::GetPixelsForTesting(std::vector* pixels) { - return root_layer_->GetPixelsForTesting(pixels); } void LayerHost::OnSurfaceIdAvailable(mojo::SurfaceIdPtr surface_id) { diff --git a/engine/src/flutter/compositor/layer_host.h b/engine/src/flutter/compositor/layer_host.h index 377dec95c3..1e4fcc7b17 100644 --- a/engine/src/flutter/compositor/layer_host.h +++ b/engine/src/flutter/compositor/layer_host.h @@ -41,8 +41,6 @@ class LayerHost : public SurfaceHolder::Client { void SetNeedsAnimate(); void SetRootLayer(scoped_refptr layer); - void GetPixelsForTesting(std::vector* pixels); - private: enum State { kReadyForFrame, diff --git a/engine/src/flutter/compositor/rasterizer.cc b/engine/src/flutter/compositor/rasterizer.cc new file mode 100644 index 0000000000..e0eb2d5c18 --- /dev/null +++ b/engine/src/flutter/compositor/rasterizer.cc @@ -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 diff --git a/engine/src/flutter/compositor/rasterizer.h b/engine/src/flutter/compositor/rasterizer.h new file mode 100644 index 0000000000..7b1a4fad5a --- /dev/null +++ b/engine/src/flutter/compositor/rasterizer.h @@ -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 Rasterize(SkPicture* picture) = 0; + + private: + DISALLOW_COPY_AND_ASSIGN(Rasterizer); +}; + +} // namespace sky + +#endif // SKY_COMPOSITOR_RASTERIZER_H_ diff --git a/engine/src/flutter/compositor/display_delegate_bitmap.cc b/engine/src/flutter/compositor/rasterizer_bitmap.cc similarity index 50% rename from engine/src/flutter/compositor/display_delegate_bitmap.cc rename to engine/src/flutter/compositor/rasterizer_bitmap.cc index c8ba553784..ee926f212d 100644 --- a/engine/src/flutter/compositor/display_delegate_bitmap.cc +++ b/engine/src/flutter/compositor/rasterizer_bitmap.cc @@ -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* pixels) { +RasterizerBitmap::~RasterizerBitmap() { +} + +void RasterizerBitmap::GetPixelsForTesting(std::vector* pixels) { gfx::PNGCodec::EncodeBGRASkBitmap(bitmap_, true, pixels); } -void DisplayDelegateBitmap::Paint(mojo::GaneshSurface& surface, const gfx::Rect& size) { +scoped_ptr 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 diff --git a/engine/src/flutter/compositor/rasterizer_bitmap.h b/engine/src/flutter/compositor/rasterizer_bitmap.h new file mode 100644 index 0000000000..22886233ab --- /dev/null +++ b/engine/src/flutter/compositor/rasterizer_bitmap.h @@ -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 Rasterize(SkPicture* picture) override; + void GetPixelsForTesting(std::vector* pixels); + + private: + LayerHost* host_; + SkBitmap bitmap_; + + DISALLOW_COPY_AND_ASSIGN(RasterizerBitmap); +}; + +} // namespace sky + +#endif // SKY_COMPOSITOR_DISPLAY_RASTERIZER_BITMAP_H_ diff --git a/engine/src/flutter/compositor/rasterizer_ganesh.cc b/engine/src/flutter/compositor/rasterizer_ganesh.cc new file mode 100644 index 0000000000..d91749e378 --- /dev/null +++ b/engine/src/flutter/compositor/rasterizer_ganesh.cc @@ -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 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 diff --git a/engine/src/flutter/compositor/rasterizer_ganesh.h b/engine/src/flutter/compositor/rasterizer_ganesh.h new file mode 100644 index 0000000000..6ee23f518a --- /dev/null +++ b/engine/src/flutter/compositor/rasterizer_ganesh.h @@ -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 Rasterize(SkPicture* picture) override; + + private: + LayerHost* host_; + + DISALLOW_COPY_AND_ASSIGN(RasterizerGanesh); +}; + +} // namespace sky + +#endif // SKY_COMPOSITOR_DISPLAY_RASTERIZER_GANESH_H_