From 804f0c7b464772e26c08557c3404cac064fd8960 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 10 Sep 2015 17:32:42 -0700 Subject: [PATCH] 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 be74e84ad..5415eeb20 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 4e7679846..ee6d36b5f 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 8e22da9b3..e2e53838c 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 4e0266160..527ee3226 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 62ef9a56f..44ab495a1 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 5304112ad..833b5f3d0 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 5fb272f70..8c8f10636 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 69337c797..db0832756 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 51323e54c..18d8115b4 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 6c35ebfac..191f39c2d 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 a601570be..eef63a7b8 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 c8d30f97f..fc10aacc7 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 37896aaf4..5d684f0bc 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 daef046b7..36bced89b 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 4b5455394..40d96bc9a 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 e38f0bf27..36e06d59d 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 5bd620a77..f225e76cf 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 4930a9b8b..35af3778e 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 797af750d..634cb8876 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 2115e5e57..9eda297d4 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 cfeb82cbd..4f2fea157 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_; -- GitLab