From ccf1ee86616b7a35701ebc397cac8a0d384606d3 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 10 Sep 2015 11:14:19 -0700 Subject: [PATCH 1/4] Allow compositor options to be modified at runtime --- sky/compositor/BUILD.gn | 3 +- sky/compositor/compositor_config.h | 13 -------- sky/compositor/compositor_options.cc | 43 +++++++++++++++++++++++++++ sky/compositor/compositor_options.h | 44 ++++++++++++++++++++++++++++ sky/compositor/picture_rasterizer.cc | 9 +++--- 5 files changed, 94 insertions(+), 18 deletions(-) delete mode 100644 sky/compositor/compositor_config.h create mode 100644 sky/compositor/compositor_options.cc create mode 100644 sky/compositor/compositor_options.h diff --git a/sky/compositor/BUILD.gn b/sky/compositor/BUILD.gn index 61a6b5cb47e..32828d47c9b 100644 --- a/sky/compositor/BUILD.gn +++ b/sky/compositor/BUILD.gn @@ -14,7 +14,8 @@ source_set("compositor") { "clip_rrect_layer.h", "color_filter_layer.cc", "color_filter_layer.h", - "compositor_config.h", + "compositor_options.cc", + "compositor_options.h", "container_layer.cc", "container_layer.h", "layer.cc", diff --git a/sky/compositor/compositor_config.h b/sky/compositor/compositor_config.h deleted file mode 100644 index d54e6feef97..00000000000 --- a/sky/compositor/compositor_config.h +++ /dev/null @@ -1,13 +0,0 @@ -// 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_COMPOSITOR_CONFIG_H_ -#define SKY_COMPOSITOR_COMPOSITOR_CONFIG_H_ - -// SkPictures that dont mutate from frame to frame are rasterized by the picture -// rasterizer. This guard enables highlighting the rasterized images. Useful -// when debugging caching -#define COMPOSITOR_HIGHLIGHT_RASTERIZED_PICTURES 0 - -#endif // SKY_COMPOSITOR_COMPOSITOR_CONFIG_H_ diff --git a/sky/compositor/compositor_options.cc b/sky/compositor/compositor_options.cc new file mode 100644 index 00000000000..102c23938a9 --- /dev/null +++ b/sky/compositor/compositor_options.cc @@ -0,0 +1,43 @@ +// 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/compositor_options.h" + +#include + +namespace sky { +namespace compositor { + +CompositorOptions& CompositorOptions::Shared() { + static std::once_flag once; + static CompositorOptions* options = nullptr; + std::call_once(once, []() { options = new CompositorOptions(); }); + return *options; +} + +CompositorOptions::CompositorOptions() { + static_assert(std::is_unsigned::value, + "OptionType must be unsigned"); + options_.resize(static_cast(Option::TerminationSentinel), false); +} + +bool CompositorOptions::isEnabled(Option option) const { + if (option >= Option::TerminationSentinel) { + return false; + } + + return options_[static_cast(option)]; +} + +void CompositorOptions::setEnabled(Option option, bool enabled) { + if (option < Option::TerminationSentinel) { + options_[static_cast(option)] = enabled; + } +} + +CompositorOptions::~CompositorOptions() { +} + +} // namespace compositor +} // namespace sky diff --git a/sky/compositor/compositor_options.h b/sky/compositor/compositor_options.h new file mode 100644 index 00000000000..f6cc8766e24 --- /dev/null +++ b/sky/compositor/compositor_options.h @@ -0,0 +1,44 @@ +// 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_COMPOSITOR_OPTIONS_H_ +#define SKY_COMPOSITOR_COMPOSITOR_OPTIONS_H_ + +#include "base/macros.h" +#include + +namespace sky { +namespace compositor { + +class CompositorOptions { + public: + using OptionType = unsigned int; + enum class Option : OptionType { + // SkPictures that dont mutate from frame to frame are rasterized by the + // picture rasterizer. This guard enables highlighting the rasterized + // images. Useful when debugging caching. + HightlightRasterizedImages, + + TerminationSentinel, + }; + + static CompositorOptions& Shared(); + + bool isEnabled(Option option) const; + + void setEnabled(Option option, bool enabled); + + private: + std::vector options_; + + CompositorOptions(); + ~CompositorOptions(); + + DISALLOW_COPY_AND_ASSIGN(CompositorOptions); +}; + +} // namespace compositor +} // namespace sky + +#endif // SKY_COMPOSITOR_COMPOSITOR_OPTIONS_H_ diff --git a/sky/compositor/picture_rasterizer.cc b/sky/compositor/picture_rasterizer.cc index 8a60fe4ec95..77589b9b085 100644 --- a/sky/compositor/picture_rasterizer.cc +++ b/sky/compositor/picture_rasterizer.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "sky/compositor/compositor_config.h" +#include "sky/compositor/compositor_options.h" #include "sky/compositor/checkerboard.h" #include "sky/compositor/picture_rasterizer.h" #include "base/logging.h" @@ -82,9 +82,10 @@ static RefPtr ImageFromPicture(GrContext* context, canvas->drawPicture(picture); -#if COMPOSITOR_HIGHLIGHT_RASTERIZED_PICTURES - DrawCheckerboard(canvas, desc.fWidth, desc.fHeight); -#endif + if (CompositorOptions::Shared().isEnabled( + CompositorOptions::Option::HightlightRasterizedImages)) { + DrawCheckerboard(canvas, desc.fWidth, desc.fHeight); + } // Step 4: Create an image representation from the texture From 08da55706cce044377c98e29b763c3bb34e25001 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 10 Sep 2015 16:41:54 -0700 Subject: [PATCH 2/4] Make the paint context own the rasterizer and options. Also give it the ability to vend frames to render into. --- sky/compositor/clip_path_layer.cc | 8 ++--- sky/compositor/clip_path_layer.h | 1 + sky/compositor/clip_rect_layer.cc | 8 ++--- sky/compositor/clip_rect_layer.h | 1 + sky/compositor/clip_rrect_layer.cc | 8 ++--- sky/compositor/clip_rrect_layer.h | 1 + sky/compositor/color_filter_layer.cc | 6 ++-- sky/compositor/color_filter_layer.h | 1 + sky/compositor/compositor_options.cc | 7 ----- sky/compositor/compositor_options.h | 6 ++-- sky/compositor/layer.h | 7 ++++- sky/compositor/opacity_layer.cc | 6 ++-- sky/compositor/opacity_layer.h | 1 + sky/compositor/paint_context.cc | 27 +++++++++++++--- sky/compositor/paint_context.h | 47 +++++++++++++++++++++++++--- sky/compositor/picture_layer.cc | 14 ++++----- sky/compositor/picture_layer.h | 5 +-- sky/compositor/picture_rasterizer.cc | 22 +++++++------ sky/compositor/picture_rasterizer.h | 3 +- sky/compositor/transform_layer.cc | 8 ++--- sky/compositor/transform_layer.h | 1 + sky/shell/gpu/rasterizer.cc | 6 ++-- sky/shell/gpu/rasterizer.h | 4 +-- 23 files changed, 132 insertions(+), 66 deletions(-) diff --git a/sky/compositor/clip_path_layer.cc b/sky/compositor/clip_path_layer.cc index 3cf9079270e..be74e84ad11 100644 --- a/sky/compositor/clip_path_layer.cc +++ b/sky/compositor/clip_path_layer.cc @@ -14,11 +14,11 @@ ClipPathLayer::~ClipPathLayer() { } void ClipPathLayer::Paint(PaintContext& context) { - SkCanvas* canvas = context.canvas(); - canvas->saveLayer(&clip_path_.getBounds(), nullptr); - canvas->clipPath(clip_path_); + SkCanvas& canvas = context.canvas(); + canvas.saveLayer(&clip_path_.getBounds(), nullptr); + canvas.clipPath(clip_path_); PaintChildren(context); - canvas->restore(); + canvas.restore(); } } // namespace compositor diff --git a/sky/compositor/clip_path_layer.h b/sky/compositor/clip_path_layer.h index 67c09fa54e0..4e76798462d 100644 --- a/sky/compositor/clip_path_layer.h +++ b/sky/compositor/clip_path_layer.h @@ -17,6 +17,7 @@ class ClipPathLayer : public ContainerLayer { void set_clip_path(const SkPath& clip_path) { clip_path_ = clip_path; } + protected: void Paint(PaintContext& context) override; private: diff --git a/sky/compositor/clip_rect_layer.cc b/sky/compositor/clip_rect_layer.cc index e3ae0ee4eea..8e22da9b3d1 100644 --- a/sky/compositor/clip_rect_layer.cc +++ b/sky/compositor/clip_rect_layer.cc @@ -14,11 +14,11 @@ ClipRectLayer::~ClipRectLayer() { } void ClipRectLayer::Paint(PaintContext& context) { - SkCanvas* canvas = context.canvas(); - canvas->save(); - canvas->clipRect(clip_rect_); + SkCanvas& canvas = context.canvas(); + canvas.save(); + canvas.clipRect(clip_rect_); PaintChildren(context); - canvas->restore(); + canvas.restore(); } } // namespace compositor diff --git a/sky/compositor/clip_rect_layer.h b/sky/compositor/clip_rect_layer.h index 69d752301b3..4e0266160fe 100644 --- a/sky/compositor/clip_rect_layer.h +++ b/sky/compositor/clip_rect_layer.h @@ -17,6 +17,7 @@ class ClipRectLayer : public ContainerLayer { void set_clip_rect(const SkRect& clip_rect) { clip_rect_ = clip_rect; } + protected: void Paint(PaintContext& context) override; private: diff --git a/sky/compositor/clip_rrect_layer.cc b/sky/compositor/clip_rrect_layer.cc index 7ec0d5448c4..62ef9a56ff6 100644 --- a/sky/compositor/clip_rrect_layer.cc +++ b/sky/compositor/clip_rrect_layer.cc @@ -14,11 +14,11 @@ ClipRRectLayer::~ClipRRectLayer() { } void ClipRRectLayer::Paint(PaintContext& context) { - SkCanvas* canvas = context.canvas(); - canvas->saveLayer(&clip_rrect_.getBounds(), nullptr); - canvas->clipRRect(clip_rrect_); + SkCanvas& canvas = context.canvas(); + canvas.saveLayer(&clip_rrect_.getBounds(), nullptr); + canvas.clipRRect(clip_rrect_); PaintChildren(context); - canvas->restore(); + canvas.restore(); } } // namespace compositor diff --git a/sky/compositor/clip_rrect_layer.h b/sky/compositor/clip_rrect_layer.h index 817b3f26713..5304112ad53 100644 --- a/sky/compositor/clip_rrect_layer.h +++ b/sky/compositor/clip_rrect_layer.h @@ -17,6 +17,7 @@ class ClipRRectLayer : public ContainerLayer { void set_clip_rrect(const SkRRect& clip_rrect) { clip_rrect_ = clip_rrect; } + protected: void Paint(PaintContext& context) override; private: diff --git a/sky/compositor/color_filter_layer.cc b/sky/compositor/color_filter_layer.cc index 6235f6f9151..5fb272f704f 100644 --- a/sky/compositor/color_filter_layer.cc +++ b/sky/compositor/color_filter_layer.cc @@ -18,10 +18,10 @@ void ColorFilterLayer::Paint(PaintContext& context) { adoptRef(SkColorFilter::CreateModeFilter(color_, transfer_mode_)); SkPaint paint; paint.setColorFilter(color_filter.get()); - SkCanvas* canvas = context.canvas(); - canvas->saveLayer(&paint_bounds(), &paint); + SkCanvas& canvas = context.canvas(); + canvas.saveLayer(&paint_bounds(), &paint); PaintChildren(context); - canvas->restore(); + canvas.restore(); } } // namespace compositor diff --git a/sky/compositor/color_filter_layer.h b/sky/compositor/color_filter_layer.h index 9695896c75a..69337c7977e 100644 --- a/sky/compositor/color_filter_layer.h +++ b/sky/compositor/color_filter_layer.h @@ -21,6 +21,7 @@ class ColorFilterLayer : public ContainerLayer { transfer_mode_ = transfer_mode; } + protected: void Paint(PaintContext& context) override; private: diff --git a/sky/compositor/compositor_options.cc b/sky/compositor/compositor_options.cc index 102c23938a9..3f0111e5def 100644 --- a/sky/compositor/compositor_options.cc +++ b/sky/compositor/compositor_options.cc @@ -9,13 +9,6 @@ namespace sky { namespace compositor { -CompositorOptions& CompositorOptions::Shared() { - static std::once_flag once; - static CompositorOptions* options = nullptr; - std::call_once(once, []() { options = new CompositorOptions(); }); - return *options; -} - CompositorOptions::CompositorOptions() { static_assert(std::is_unsigned::value, "OptionType must be unsigned"); diff --git a/sky/compositor/compositor_options.h b/sky/compositor/compositor_options.h index f6cc8766e24..c20ce861233 100644 --- a/sky/compositor/compositor_options.h +++ b/sky/compositor/compositor_options.h @@ -23,7 +23,8 @@ class CompositorOptions { TerminationSentinel, }; - static CompositorOptions& Shared(); + CompositorOptions(); + ~CompositorOptions(); bool isEnabled(Option option) const; @@ -32,9 +33,6 @@ class CompositorOptions { private: std::vector options_; - CompositorOptions(); - ~CompositorOptions(); - DISALLOW_COPY_AND_ASSIGN(CompositorOptions); }; diff --git a/sky/compositor/layer.h b/sky/compositor/layer.h index d48c6cf802c..a601570befe 100644 --- a/sky/compositor/layer.h +++ b/sky/compositor/layer.h @@ -32,7 +32,7 @@ class Layer { Layer(); virtual ~Layer(); - virtual void Paint(PaintContext& context) = 0; + void Paint(PaintContext::ScopedFrame& frame) { Paint(frame.paint_context()); } virtual SkMatrix model_view_matrix(const SkMatrix& model_matrix) const; @@ -46,10 +46,15 @@ class Layer { paint_bounds_ = paint_bounds; } + protected: + virtual void Paint(PaintContext& context) = 0; + private: ContainerLayer* parent_; SkRect paint_bounds_; + friend class ContainerLayer; + DISALLOW_COPY_AND_ASSIGN(Layer); }; diff --git a/sky/compositor/opacity_layer.cc b/sky/compositor/opacity_layer.cc index 619a769d555..c8d30f97f8c 100644 --- a/sky/compositor/opacity_layer.cc +++ b/sky/compositor/opacity_layer.cc @@ -19,10 +19,10 @@ void OpacityLayer::Paint(PaintContext& context) { SkColorFilter::CreateModeFilter(color, SkXfermode::kSrcOver_Mode)); SkPaint paint; paint.setColorFilter(colorFilter.get()); - SkCanvas* canvas = context.canvas(); - canvas->saveLayer(&paint_bounds(), &paint); + SkCanvas& canvas = context.canvas(); + canvas.saveLayer(&paint_bounds(), &paint); PaintChildren(context); - canvas->restore(); + canvas.restore(); } } // namespace compositor diff --git a/sky/compositor/opacity_layer.h b/sky/compositor/opacity_layer.h index e4d26116e66..37896aaf433 100644 --- a/sky/compositor/opacity_layer.h +++ b/sky/compositor/opacity_layer.h @@ -17,6 +17,7 @@ class OpacityLayer : public ContainerLayer { void set_alpha(int alpha) { alpha_ = alpha; } + protected: void Paint(PaintContext& context) override; private: diff --git a/sky/compositor/paint_context.cc b/sky/compositor/paint_context.cc index ab21b96b46d..daef046b70a 100644 --- a/sky/compositor/paint_context.cc +++ b/sky/compositor/paint_context.cc @@ -3,14 +3,33 @@ // found in the LICENSE file. #include "sky/compositor/paint_context.h" +#include "base/logging.h" namespace sky { namespace compositor { -PaintContext::PaintContext(PictureRasterzier& rasterizer, - GrContext* gr_context, - SkCanvas* canvas) - : rasterizer_(rasterizer), gr_context_(gr_context), canvas_(canvas) { +PaintContext::PaintContext() { +} + +void PaintContext::beginFrame(SkCanvas& canvas, GrContext* gr_context) { + canvas_ = &canvas; // required + gr_context_ = gr_context; // optional + + DCHECK(canvas_); +} + +void PaintContext::endFrame() { + DCHECK(canvas_); + + rasterizer_.PurgeCache(); + + canvas_ = nullptr; + gr_context_ = nullptr; +} + +PaintContext::ScopedFrame PaintContext::AcquireFrame(SkCanvas& canvas, + GrContext* gr_context) { + return ScopedFrame(*this, canvas, gr_context); } PaintContext::~PaintContext() { diff --git a/sky/compositor/paint_context.h b/sky/compositor/paint_context.h index d3af878f578..4b5455394fb 100644 --- a/sky/compositor/paint_context.h +++ b/sky/compositor/paint_context.h @@ -6,6 +6,8 @@ #define SKY_COMPOSITOR_PAINT_CONTEXT_CC_ #include "base/macros.h" +#include "base/logging.h" +#include "sky/compositor/compositor_options.h" #include "sky/compositor/picture_rasterizer.h" namespace sky { @@ -13,22 +15,57 @@ namespace compositor { class PaintContext { public: - PaintContext(PictureRasterzier& rasterizer, - GrContext* gr_context, - SkCanvas* canvas); + class ScopedFrame { + public: + PaintContext& paint_context() { return context_; }; + + ScopedFrame(ScopedFrame&& frame) = default; + + ~ScopedFrame() { context_.endFrame(); } + + private: + PaintContext& context_; + + ScopedFrame() = delete; + + ScopedFrame(PaintContext& context, SkCanvas& canvas, GrContext* gr_context) + : context_(context) { + context_.beginFrame(canvas, gr_context); + }; + + friend class PaintContext; + + DISALLOW_COPY_AND_ASSIGN(ScopedFrame); + }; + + PaintContext(); ~PaintContext(); PictureRasterzier& rasterizer() { return rasterizer_; } + CompositorOptions& options() { return options_; }; + GrContext* gr_context() { return gr_context_; } - SkCanvas* canvas() { return canvas_; } + SkCanvas& canvas() { + DCHECK(canvas_) << "Tried to access the canvas of a context whose frame " + "was not initialized. Did you forget to " + "`AcquireFrame`?"; + return *canvas_; + } + + ScopedFrame AcquireFrame(SkCanvas& canvas, GrContext* gr_context); private: - PictureRasterzier& rasterizer_; + PictureRasterzier rasterizer_; + CompositorOptions options_; GrContext* gr_context_; SkCanvas* canvas_; + void beginFrame(SkCanvas& canvas, GrContext* context); + + void endFrame(); + DISALLOW_COPY_AND_ASSIGN(PaintContext); }; diff --git a/sky/compositor/picture_layer.cc b/sky/compositor/picture_layer.cc index a04da168677..e38f0bf2764 100644 --- a/sky/compositor/picture_layer.cc +++ b/sky/compositor/picture_layer.cc @@ -27,16 +27,16 @@ void PictureLayer::Paint(PaintContext& context) { SkISize size = SkISize::Make(bounds.width(), bounds.height()); RefPtr image = context.rasterizer().GetCachedImageIfPresent( - context.gr_context(), picture_.get(), size); - SkCanvas* canvas = context.canvas(); + context, picture_.get(), size); + SkCanvas& canvas = context.canvas(); if (image) { - canvas->drawImage(image.get(), offset_.x(), offset_.y()); + canvas.drawImage(image.get(), offset_.x(), offset_.y()); } else { - canvas->save(); - canvas->translate(offset_.x(), offset_.y()); - canvas->drawPicture(picture_.get()); - canvas->restore(); + canvas.save(); + canvas.translate(offset_.x(), offset_.y()); + canvas.drawPicture(picture_.get()); + canvas.restore(); } } diff --git a/sky/compositor/picture_layer.h b/sky/compositor/picture_layer.h index 44a84601e4c..5bd620a77f9 100644 --- a/sky/compositor/picture_layer.h +++ b/sky/compositor/picture_layer.h @@ -21,10 +21,11 @@ class PictureLayer : public Layer { SkMatrix model_view_matrix(const SkMatrix& model_matrix) const override; - void Paint(PaintContext& context) override; - SkPicture* picture() const { return picture_.get(); } + protected: + void Paint(PaintContext& context) override; + private: SkPoint offset_; RefPtr picture_; diff --git a/sky/compositor/picture_rasterizer.cc b/sky/compositor/picture_rasterizer.cc index 77589b9b085..4930a9b8baa 100644 --- a/sky/compositor/picture_rasterizer.cc +++ b/sky/compositor/picture_rasterizer.cc @@ -5,6 +5,7 @@ #include "sky/compositor/compositor_options.h" #include "sky/compositor/checkerboard.h" #include "sky/compositor/picture_rasterizer.h" +#include "sky/compositor/paint_context.h" #include "base/logging.h" #include "third_party/skia/include/core/SkPicture.h" #include "third_party/skia/include/gpu/GrContext.h" @@ -37,7 +38,7 @@ PictureRasterzier::Value::Value() PictureRasterzier::Value::~Value() { } -static RefPtr ImageFromPicture(GrContext* context, +static RefPtr ImageFromPicture(PaintContext& context, SkPicture* picture, const SkISize& size) { // Step 1: Create a texture from the context's texture provider @@ -48,7 +49,8 @@ static RefPtr ImageFromPicture(GrContext* context, desc.fFlags = kRenderTarget_GrSurfaceFlag; desc.fConfig = kRGBA_8888_GrPixelConfig; - GrTexture* texture = context->textureProvider()->createTexture(desc, true); + GrTexture* texture = + context.gr_context()->textureProvider()->createTexture(desc, true); if (!texture) { // The texture provider could not allocate a texture backing. Render @@ -82,22 +84,24 @@ static RefPtr ImageFromPicture(GrContext* context, canvas->drawPicture(picture); - if (CompositorOptions::Shared().isEnabled( + if (context.options().isEnabled( CompositorOptions::Option::HightlightRasterizedImages)) { DrawCheckerboard(canvas, desc.fWidth, desc.fHeight); } // Step 4: Create an image representation from the texture - RefPtr image = adoptRef(SkImage::NewFromTexture( - context, backendDesc, kPremul_SkAlphaType, &ImageReleaseProc, texture)); + RefPtr image = adoptRef( + SkImage::NewFromTexture(context.gr_context(), backendDesc, + kPremul_SkAlphaType, &ImageReleaseProc, texture)); return image; } -RefPtr PictureRasterzier::GetCachedImageIfPresent(GrContext* context, - SkPicture* picture, - SkISize size) { - if (size.isEmpty() || picture == nullptr || context == nullptr) { +RefPtr PictureRasterzier::GetCachedImageIfPresent( + PaintContext& context, + SkPicture* picture, + SkISize size) { + if (size.isEmpty() || picture == nullptr || context.gr_context() == nullptr) { return nullptr; } diff --git a/sky/compositor/picture_rasterizer.h b/sky/compositor/picture_rasterizer.h index fbde8d40021..797af750d04 100644 --- a/sky/compositor/picture_rasterizer.h +++ b/sky/compositor/picture_rasterizer.h @@ -18,12 +18,13 @@ namespace sky { namespace compositor { +class PaintContext; class PictureRasterzier { public: PictureRasterzier(); ~PictureRasterzier(); - RefPtr GetCachedImageIfPresent(GrContext* context, + RefPtr GetCachedImageIfPresent(PaintContext& context, SkPicture* picture, SkISize size); diff --git a/sky/compositor/transform_layer.cc b/sky/compositor/transform_layer.cc index 2d9dfe1bcb2..2115e5e57da 100644 --- a/sky/compositor/transform_layer.cc +++ b/sky/compositor/transform_layer.cc @@ -20,11 +20,11 @@ SkMatrix TransformLayer::model_view_matrix(const SkMatrix& model_matrix) const { } void TransformLayer::Paint(PaintContext& context) { - SkCanvas* canvas = context.canvas(); - canvas->save(); - canvas->concat(transform_); + SkCanvas& canvas = context.canvas(); + canvas.save(); + canvas.concat(transform_); PaintChildren(context); - canvas->restore(); + canvas.restore(); } } // namespace compositor diff --git a/sky/compositor/transform_layer.h b/sky/compositor/transform_layer.h index b669efae3c3..cfeb82cbd18 100644 --- a/sky/compositor/transform_layer.h +++ b/sky/compositor/transform_layer.h @@ -19,6 +19,7 @@ class TransformLayer : public ContainerLayer { SkMatrix model_view_matrix(const SkMatrix& model_matrix) const override; + protected: void Paint(PaintContext& context) override; private: diff --git a/sky/shell/gpu/rasterizer.cc b/sky/shell/gpu/rasterizer.cc index 03a814d4f44..90c5d0861c9 100644 --- a/sky/shell/gpu/rasterizer.cc +++ b/sky/shell/gpu/rasterizer.cc @@ -78,8 +78,10 @@ void Rasterizer::Draw(scoped_ptr layer_tree) { SkCanvas* canvas = ganesh_surface_->canvas(); canvas->clear(SK_ColorBLACK); - compositor::PaintContext context(rasterizer_, ganesh_context_->gr(), canvas); - layer_tree->root_layer()->Paint(context); + { + auto frame = paint_context_.AcquireFrame(*canvas, ganesh_context_->gr()); + layer_tree->root_layer()->Paint(frame); + } canvas->flush(); surface_->SwapBuffers(); diff --git a/sky/shell/gpu/rasterizer.h b/sky/shell/gpu/rasterizer.h index cb766342ab1..8b56784fa6b 100644 --- a/sky/shell/gpu/rasterizer.h +++ b/sky/shell/gpu/rasterizer.h @@ -11,7 +11,7 @@ #include "sky/shell/gpu_delegate.h" #include "ui/gfx/geometry/size.h" #include "ui/gfx/native_widget_types.h" -#include "sky/compositor/picture_rasterizer.h" +#include "sky/compositor/paint_context.h" class SkPicture; @@ -48,7 +48,7 @@ class Rasterizer : public GPUDelegate { scoped_ptr ganesh_context_; scoped_ptr ganesh_surface_; - compositor::PictureRasterzier rasterizer_; + compositor::PaintContext paint_context_; base::WeakPtrFactory weak_factory_; From d2bd4cba157b1bd3fd4d419d4b6d38760bc50db8 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 10 Sep 2015 17:06:13 -0700 Subject: [PATCH 3/4] Remove unnecessary include --- sky/compositor/compositor_options.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/sky/compositor/compositor_options.cc b/sky/compositor/compositor_options.cc index 3f0111e5def..8317da170cd 100644 --- a/sky/compositor/compositor_options.cc +++ b/sky/compositor/compositor_options.cc @@ -4,8 +4,6 @@ #include "sky/compositor/compositor_options.h" -#include - namespace sky { namespace compositor { From 804f0c7b464772e26c08557c3404cac064fd8960 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 10 Sep 2015 17:32:42 -0700 Subject: [PATCH 4/4] The gr_context and canvas are stored at the frame level instead of the storing the same temporarily in the context --- sky/compositor/clip_path_layer.cc | 6 +++--- sky/compositor/clip_path_layer.h | 2 +- sky/compositor/clip_rect_layer.cc | 6 +++--- sky/compositor/clip_rect_layer.h | 2 +- sky/compositor/clip_rrect_layer.cc | 6 +++--- sky/compositor/clip_rrect_layer.h | 3 +-- sky/compositor/color_filter_layer.cc | 6 +++--- sky/compositor/color_filter_layer.h | 3 +-- sky/compositor/container_layer.cc | 4 ++-- sky/compositor/container_layer.h | 2 +- sky/compositor/layer.h | 7 +------ sky/compositor/opacity_layer.cc | 6 +++--- sky/compositor/opacity_layer.h | 2 +- sky/compositor/paint_context.cc | 11 +---------- sky/compositor/paint_context.h | 24 ++++++++++-------------- sky/compositor/picture_layer.cc | 9 +++++---- sky/compositor/picture_layer.h | 3 +-- sky/compositor/picture_rasterizer.cc | 13 +++++++------ sky/compositor/picture_rasterizer.h | 1 + sky/compositor/transform_layer.cc | 6 +++--- sky/compositor/transform_layer.h | 3 +-- 21 files changed, 53 insertions(+), 72 deletions(-) diff --git a/sky/compositor/clip_path_layer.cc b/sky/compositor/clip_path_layer.cc index be74e84ad11..5415eeb2026 100644 --- a/sky/compositor/clip_path_layer.cc +++ b/sky/compositor/clip_path_layer.cc @@ -13,11 +13,11 @@ ClipPathLayer::ClipPathLayer() { ClipPathLayer::~ClipPathLayer() { } -void ClipPathLayer::Paint(PaintContext& context) { - SkCanvas& canvas = context.canvas(); +void ClipPathLayer::Paint(PaintContext::ScopedFrame& frame) { + SkCanvas& canvas = frame.canvas(); canvas.saveLayer(&clip_path_.getBounds(), nullptr); canvas.clipPath(clip_path_); - PaintChildren(context); + PaintChildren(frame); canvas.restore(); } diff --git a/sky/compositor/clip_path_layer.h b/sky/compositor/clip_path_layer.h index 4e76798462d..ee6d36b5fb8 100644 --- a/sky/compositor/clip_path_layer.h +++ b/sky/compositor/clip_path_layer.h @@ -18,7 +18,7 @@ class ClipPathLayer : public ContainerLayer { void set_clip_path(const SkPath& clip_path) { clip_path_ = clip_path; } protected: - void Paint(PaintContext& context) override; + void Paint(PaintContext::ScopedFrame& frame) override; private: SkPath clip_path_; diff --git a/sky/compositor/clip_rect_layer.cc b/sky/compositor/clip_rect_layer.cc index 8e22da9b3d1..e2e53838ce6 100644 --- a/sky/compositor/clip_rect_layer.cc +++ b/sky/compositor/clip_rect_layer.cc @@ -13,11 +13,11 @@ ClipRectLayer::ClipRectLayer() { ClipRectLayer::~ClipRectLayer() { } -void ClipRectLayer::Paint(PaintContext& context) { - SkCanvas& canvas = context.canvas(); +void ClipRectLayer::Paint(PaintContext::ScopedFrame& frame) { + SkCanvas& canvas = frame.canvas(); canvas.save(); canvas.clipRect(clip_rect_); - PaintChildren(context); + PaintChildren(frame); canvas.restore(); } diff --git a/sky/compositor/clip_rect_layer.h b/sky/compositor/clip_rect_layer.h index 4e0266160fe..527ee322637 100644 --- a/sky/compositor/clip_rect_layer.h +++ b/sky/compositor/clip_rect_layer.h @@ -18,7 +18,7 @@ class ClipRectLayer : public ContainerLayer { void set_clip_rect(const SkRect& clip_rect) { clip_rect_ = clip_rect; } protected: - void Paint(PaintContext& context) override; + void Paint(PaintContext::ScopedFrame& frame) override; private: SkRect clip_rect_; diff --git a/sky/compositor/clip_rrect_layer.cc b/sky/compositor/clip_rrect_layer.cc index 62ef9a56ff6..44ab495a1f1 100644 --- a/sky/compositor/clip_rrect_layer.cc +++ b/sky/compositor/clip_rrect_layer.cc @@ -13,11 +13,11 @@ ClipRRectLayer::ClipRRectLayer() { ClipRRectLayer::~ClipRRectLayer() { } -void ClipRRectLayer::Paint(PaintContext& context) { - SkCanvas& canvas = context.canvas(); +void ClipRRectLayer::Paint(PaintContext::ScopedFrame& frame) { + SkCanvas& canvas = frame.canvas(); canvas.saveLayer(&clip_rrect_.getBounds(), nullptr); canvas.clipRRect(clip_rrect_); - PaintChildren(context); + PaintChildren(frame); canvas.restore(); } diff --git a/sky/compositor/clip_rrect_layer.h b/sky/compositor/clip_rrect_layer.h index 5304112ad53..833b5f3d0ab 100644 --- a/sky/compositor/clip_rrect_layer.h +++ b/sky/compositor/clip_rrect_layer.h @@ -17,8 +17,7 @@ class ClipRRectLayer : public ContainerLayer { void set_clip_rrect(const SkRRect& clip_rrect) { clip_rrect_ = clip_rrect; } - protected: - void Paint(PaintContext& context) override; + void Paint(PaintContext::ScopedFrame& frame) override; private: SkRRect clip_rrect_; diff --git a/sky/compositor/color_filter_layer.cc b/sky/compositor/color_filter_layer.cc index 5fb272f704f..8c8f106361a 100644 --- a/sky/compositor/color_filter_layer.cc +++ b/sky/compositor/color_filter_layer.cc @@ -13,14 +13,14 @@ ColorFilterLayer::ColorFilterLayer() { ColorFilterLayer::~ColorFilterLayer() { } -void ColorFilterLayer::Paint(PaintContext& context) { +void ColorFilterLayer::Paint(PaintContext::ScopedFrame& frame) { RefPtr color_filter = adoptRef(SkColorFilter::CreateModeFilter(color_, transfer_mode_)); SkPaint paint; paint.setColorFilter(color_filter.get()); - SkCanvas& canvas = context.canvas(); + SkCanvas& canvas = frame.canvas(); canvas.saveLayer(&paint_bounds(), &paint); - PaintChildren(context); + PaintChildren(frame); canvas.restore(); } diff --git a/sky/compositor/color_filter_layer.h b/sky/compositor/color_filter_layer.h index 69337c7977e..db083275621 100644 --- a/sky/compositor/color_filter_layer.h +++ b/sky/compositor/color_filter_layer.h @@ -21,8 +21,7 @@ class ColorFilterLayer : public ContainerLayer { transfer_mode_ = transfer_mode; } - protected: - void Paint(PaintContext& context) override; + void Paint(PaintContext::ScopedFrame& frame) override; private: SkColor color_; diff --git a/sky/compositor/container_layer.cc b/sky/compositor/container_layer.cc index 51323e54c69..18d8115b4dd 100644 --- a/sky/compositor/container_layer.cc +++ b/sky/compositor/container_layer.cc @@ -18,9 +18,9 @@ void ContainerLayer::Add(std::unique_ptr layer) { layers_.push_back(std::move(layer)); } -void ContainerLayer::PaintChildren(PaintContext& context) const { +void ContainerLayer::PaintChildren(PaintContext::ScopedFrame& frame) const { for (auto& layer : layers_) - layer->Paint(context); + layer->Paint(frame); } } // namespace compositor diff --git a/sky/compositor/container_layer.h b/sky/compositor/container_layer.h index 6c35ebfacce..191f39c2d73 100644 --- a/sky/compositor/container_layer.h +++ b/sky/compositor/container_layer.h @@ -17,7 +17,7 @@ class ContainerLayer : public Layer { void Add(std::unique_ptr layer); - void PaintChildren(PaintContext& context) const; + void PaintChildren(PaintContext::ScopedFrame& frame) const; const std::vector>& layers() const { return layers_; } diff --git a/sky/compositor/layer.h b/sky/compositor/layer.h index a601570befe..eef63a7b8f5 100644 --- a/sky/compositor/layer.h +++ b/sky/compositor/layer.h @@ -32,7 +32,7 @@ class Layer { Layer(); virtual ~Layer(); - void Paint(PaintContext::ScopedFrame& frame) { Paint(frame.paint_context()); } + virtual void Paint(PaintContext::ScopedFrame& frame) = 0; virtual SkMatrix model_view_matrix(const SkMatrix& model_matrix) const; @@ -46,15 +46,10 @@ class Layer { paint_bounds_ = paint_bounds; } - protected: - virtual void Paint(PaintContext& context) = 0; - private: ContainerLayer* parent_; SkRect paint_bounds_; - friend class ContainerLayer; - DISALLOW_COPY_AND_ASSIGN(Layer); }; diff --git a/sky/compositor/opacity_layer.cc b/sky/compositor/opacity_layer.cc index c8d30f97f8c..fc10aacc7e2 100644 --- a/sky/compositor/opacity_layer.cc +++ b/sky/compositor/opacity_layer.cc @@ -13,15 +13,15 @@ OpacityLayer::OpacityLayer() { OpacityLayer::~OpacityLayer() { } -void OpacityLayer::Paint(PaintContext& context) { +void OpacityLayer::Paint(PaintContext::ScopedFrame& frame) { SkColor color = SkColorSetARGB(alpha_, 0, 0, 0); RefPtr colorFilter = adoptRef( SkColorFilter::CreateModeFilter(color, SkXfermode::kSrcOver_Mode)); SkPaint paint; paint.setColorFilter(colorFilter.get()); - SkCanvas& canvas = context.canvas(); + SkCanvas& canvas = frame.canvas(); canvas.saveLayer(&paint_bounds(), &paint); - PaintChildren(context); + PaintChildren(frame); canvas.restore(); } diff --git a/sky/compositor/opacity_layer.h b/sky/compositor/opacity_layer.h index 37896aaf433..5d684f0bcda 100644 --- a/sky/compositor/opacity_layer.h +++ b/sky/compositor/opacity_layer.h @@ -18,7 +18,7 @@ class OpacityLayer : public ContainerLayer { void set_alpha(int alpha) { alpha_ = alpha; } protected: - void Paint(PaintContext& context) override; + void Paint(PaintContext::ScopedFrame& frame) override; private: int alpha_; diff --git a/sky/compositor/paint_context.cc b/sky/compositor/paint_context.cc index daef046b70a..36bced89b5f 100644 --- a/sky/compositor/paint_context.cc +++ b/sky/compositor/paint_context.cc @@ -11,20 +11,11 @@ namespace compositor { PaintContext::PaintContext() { } -void PaintContext::beginFrame(SkCanvas& canvas, GrContext* gr_context) { - canvas_ = &canvas; // required - gr_context_ = gr_context; // optional - - DCHECK(canvas_); +void PaintContext::beginFrame() { } void PaintContext::endFrame() { - DCHECK(canvas_); - rasterizer_.PurgeCache(); - - canvas_ = nullptr; - gr_context_ = nullptr; } PaintContext::ScopedFrame PaintContext::AcquireFrame(SkCanvas& canvas, diff --git a/sky/compositor/paint_context.h b/sky/compositor/paint_context.h index 4b5455394fb..40d96bc9ac1 100644 --- a/sky/compositor/paint_context.h +++ b/sky/compositor/paint_context.h @@ -19,18 +19,25 @@ class PaintContext { public: PaintContext& paint_context() { return context_; }; + GrContext* gr_context() { return gr_context_; } + + SkCanvas& canvas() { return canvas_; } + ScopedFrame(ScopedFrame&& frame) = default; ~ScopedFrame() { context_.endFrame(); } private: PaintContext& context_; + SkCanvas& canvas_; + GrContext* gr_context_; ScopedFrame() = delete; ScopedFrame(PaintContext& context, SkCanvas& canvas, GrContext* gr_context) - : context_(context) { - context_.beginFrame(canvas, gr_context); + : context_(context), canvas_(canvas), gr_context_(gr_context) { + DCHECK(&canvas) << "The frame requries a valid canvas"; + context_.beginFrame(); }; friend class PaintContext; @@ -45,24 +52,13 @@ class PaintContext { CompositorOptions& options() { return options_; }; - GrContext* gr_context() { return gr_context_; } - - SkCanvas& canvas() { - DCHECK(canvas_) << "Tried to access the canvas of a context whose frame " - "was not initialized. Did you forget to " - "`AcquireFrame`?"; - return *canvas_; - } - ScopedFrame AcquireFrame(SkCanvas& canvas, GrContext* gr_context); private: PictureRasterzier rasterizer_; CompositorOptions options_; - GrContext* gr_context_; - SkCanvas* canvas_; - void beginFrame(SkCanvas& canvas, GrContext* context); + void beginFrame(); void endFrame(); diff --git a/sky/compositor/picture_layer.cc b/sky/compositor/picture_layer.cc index e38f0bf2764..36e06d59dd9 100644 --- a/sky/compositor/picture_layer.cc +++ b/sky/compositor/picture_layer.cc @@ -20,15 +20,16 @@ SkMatrix PictureLayer::model_view_matrix(const SkMatrix& model_matrix) const { return modelView; } -void PictureLayer::Paint(PaintContext& context) { +void PictureLayer::Paint(PaintContext::ScopedFrame& frame) { DCHECK(picture_); const SkRect& bounds = paint_bounds(); SkISize size = SkISize::Make(bounds.width(), bounds.height()); - RefPtr image = context.rasterizer().GetCachedImageIfPresent( - context, picture_.get(), size); - SkCanvas& canvas = context.canvas(); + RefPtr image = + frame.paint_context().rasterizer().GetCachedImageIfPresent( + frame.paint_context(), frame.gr_context(), picture_.get(), size); + SkCanvas& canvas = frame.canvas(); if (image) { canvas.drawImage(image.get(), offset_.x(), offset_.y()); diff --git a/sky/compositor/picture_layer.h b/sky/compositor/picture_layer.h index 5bd620a77f9..f225e76cf6d 100644 --- a/sky/compositor/picture_layer.h +++ b/sky/compositor/picture_layer.h @@ -23,8 +23,7 @@ class PictureLayer : public Layer { SkPicture* picture() const { return picture_.get(); } - protected: - void Paint(PaintContext& context) override; + void Paint(PaintContext::ScopedFrame& frame) override; private: SkPoint offset_; diff --git a/sky/compositor/picture_rasterizer.cc b/sky/compositor/picture_rasterizer.cc index 4930a9b8baa..35af3778ef0 100644 --- a/sky/compositor/picture_rasterizer.cc +++ b/sky/compositor/picture_rasterizer.cc @@ -39,6 +39,7 @@ PictureRasterzier::Value::~Value() { } static RefPtr ImageFromPicture(PaintContext& context, + GrContext* gr_context, SkPicture* picture, const SkISize& size) { // Step 1: Create a texture from the context's texture provider @@ -49,8 +50,7 @@ static RefPtr ImageFromPicture(PaintContext& context, desc.fFlags = kRenderTarget_GrSurfaceFlag; desc.fConfig = kRGBA_8888_GrPixelConfig; - GrTexture* texture = - context.gr_context()->textureProvider()->createTexture(desc, true); + GrTexture* texture = gr_context->textureProvider()->createTexture(desc, true); if (!texture) { // The texture provider could not allocate a texture backing. Render @@ -92,16 +92,17 @@ static RefPtr ImageFromPicture(PaintContext& context, // Step 4: Create an image representation from the texture RefPtr image = adoptRef( - SkImage::NewFromTexture(context.gr_context(), backendDesc, - kPremul_SkAlphaType, &ImageReleaseProc, texture)); + SkImage::NewFromTexture(gr_context, backendDesc, kPremul_SkAlphaType, + &ImageReleaseProc, texture)); return image; } RefPtr PictureRasterzier::GetCachedImageIfPresent( PaintContext& context, + GrContext* gr_context, SkPicture* picture, SkISize size) { - if (size.isEmpty() || picture == nullptr || context.gr_context() == nullptr) { + if (size.isEmpty() || picture == nullptr || gr_context == nullptr) { return nullptr; } @@ -119,7 +120,7 @@ RefPtr PictureRasterzier::GetCachedImageIfPresent( << "Did you forget to call purge_cache between frames?"; if (!value.image) { - value.image = ImageFromPicture(context, picture, size); + value.image = ImageFromPicture(context, gr_context, picture, size); } return value.image; diff --git a/sky/compositor/picture_rasterizer.h b/sky/compositor/picture_rasterizer.h index 797af750d04..634cb887676 100644 --- a/sky/compositor/picture_rasterizer.h +++ b/sky/compositor/picture_rasterizer.h @@ -25,6 +25,7 @@ class PictureRasterzier { ~PictureRasterzier(); RefPtr GetCachedImageIfPresent(PaintContext& context, + GrContext* gr_context, SkPicture* picture, SkISize size); diff --git a/sky/compositor/transform_layer.cc b/sky/compositor/transform_layer.cc index 2115e5e57da..9eda297d4db 100644 --- a/sky/compositor/transform_layer.cc +++ b/sky/compositor/transform_layer.cc @@ -19,11 +19,11 @@ SkMatrix TransformLayer::model_view_matrix(const SkMatrix& model_matrix) const { return modelView; } -void TransformLayer::Paint(PaintContext& context) { - SkCanvas& canvas = context.canvas(); +void TransformLayer::Paint(PaintContext::ScopedFrame& frame) { + SkCanvas& canvas = frame.canvas(); canvas.save(); canvas.concat(transform_); - PaintChildren(context); + PaintChildren(frame); canvas.restore(); } diff --git a/sky/compositor/transform_layer.h b/sky/compositor/transform_layer.h index cfeb82cbd18..4f2fea1577b 100644 --- a/sky/compositor/transform_layer.h +++ b/sky/compositor/transform_layer.h @@ -19,8 +19,7 @@ class TransformLayer : public ContainerLayer { SkMatrix model_view_matrix(const SkMatrix& model_matrix) const override; - protected: - void Paint(PaintContext& context) override; + void Paint(PaintContext::ScopedFrame& frame) override; private: SkMatrix transform_;