From ba7752917f3081fd6b4e10d8ef6cecbdfe59a985 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Mon, 17 Sep 2018 11:43:41 -0700 Subject: [PATCH] Ensure that Layer::AutoSaveLayer objects are not immediately destructed (#6264) Fixes https://github.com/flutter/flutter/issues/20859 --- flow/layers/backdrop_filter_layer.cc | 5 +++-- flow/layers/color_filter_layer.cc | 3 ++- flow/layers/layer.cc | 13 +++++++++++++ flow/layers/layer.h | 16 +++++++++++++--- flow/layers/opacity_layer.cc | 3 ++- flow/layers/shader_mask_layer.cc | 3 ++- 6 files changed, 35 insertions(+), 8 deletions(-) diff --git a/flow/layers/backdrop_filter_layer.cc b/flow/layers/backdrop_filter_layer.cc index b8d04803f1..1b93b9cabb 100644 --- a/flow/layers/backdrop_filter_layer.cc +++ b/flow/layers/backdrop_filter_layer.cc @@ -16,8 +16,9 @@ void BackdropFilterLayer::Paint(PaintContext& context) const { TRACE_EVENT0("flutter", "BackdropFilterLayer::Paint"); FML_DCHECK(needs_painting()); - Layer::AutoSaveLayer(context, SkCanvas::SaveLayerRec{&paint_bounds(), nullptr, - filter_.get(), 0}); + Layer::AutoSaveLayer save = Layer::AutoSaveLayer::Create( + context, + SkCanvas::SaveLayerRec{&paint_bounds(), nullptr, filter_.get(), 0}); PaintChildren(context); } diff --git a/flow/layers/color_filter_layer.cc b/flow/layers/color_filter_layer.cc index a3c678aa15..287507d179 100644 --- a/flow/layers/color_filter_layer.cc +++ b/flow/layers/color_filter_layer.cc @@ -19,7 +19,8 @@ void ColorFilterLayer::Paint(PaintContext& context) const { SkPaint paint; paint.setColorFilter(std::move(color_filter)); - Layer::AutoSaveLayer(context, paint_bounds(), &paint); + Layer::AutoSaveLayer save = + Layer::AutoSaveLayer::Create(context, paint_bounds(), &paint); PaintChildren(context); } diff --git a/flow/layers/layer.cc b/flow/layers/layer.cc index 67148dc0c0..c1d9187212 100644 --- a/flow/layers/layer.cc +++ b/flow/layers/layer.cc @@ -35,6 +35,19 @@ Layer::AutoSaveLayer::AutoSaveLayer(const PaintContext& paint_context, paint_context_.canvas.saveLayer(layer_rec); } +Layer::AutoSaveLayer Layer::AutoSaveLayer::Create( + const PaintContext& paint_context, + const SkRect& bounds, + const SkPaint* paint) { + return Layer::AutoSaveLayer(paint_context, bounds, paint); +} + +Layer::AutoSaveLayer Layer::AutoSaveLayer::Create( + const PaintContext& paint_context, + const SkCanvas::SaveLayerRec& layer_rec) { + return Layer::AutoSaveLayer(paint_context, layer_rec); +} + Layer::AutoSaveLayer::~AutoSaveLayer() { if (paint_context_.checkerboard_offscreen_layers) { DrawCheckerboard(&paint_context_.canvas, bounds_); diff --git a/flow/layers/layer.h b/flow/layers/layer.h index 173c2890b6..a52f6e04aa 100644 --- a/flow/layers/layer.h +++ b/flow/layers/layer.h @@ -12,6 +12,7 @@ #include "flutter/flow/raster_cache.h" #include "flutter/flow/texture.h" #include "flutter/fml/build_config.h" +#include "flutter/fml/compiler_specific.h" #include "flutter/fml/logging.h" #include "flutter/fml/macros.h" #include "flutter/fml/trace_event.h" @@ -67,6 +68,18 @@ class Layer { // draws a checkerboard over the layer if that is enabled in the PaintContext. class AutoSaveLayer { public: + FML_WARN_UNUSED_RESULT static AutoSaveLayer Create( + const PaintContext& paint_context, + const SkRect& bounds, + const SkPaint* paint); + + FML_WARN_UNUSED_RESULT static AutoSaveLayer Create( + const PaintContext& paint_context, + const SkCanvas::SaveLayerRec& layer_rec); + + ~AutoSaveLayer(); + + private: AutoSaveLayer(const PaintContext& paint_context, const SkRect& bounds, const SkPaint* paint); @@ -74,9 +87,6 @@ class Layer { AutoSaveLayer(const PaintContext& paint_context, const SkCanvas::SaveLayerRec& layer_rec); - ~AutoSaveLayer(); - - private: const PaintContext& paint_context_; const SkRect bounds_; }; diff --git a/flow/layers/opacity_layer.cc b/flow/layers/opacity_layer.cc index 84b8bd64c0..2272e79093 100644 --- a/flow/layers/opacity_layer.cc +++ b/flow/layers/opacity_layer.cc @@ -17,7 +17,8 @@ void OpacityLayer::Paint(PaintContext& context) const { SkPaint paint; paint.setAlpha(alpha_); - Layer::AutoSaveLayer save(context, paint_bounds(), &paint); + Layer::AutoSaveLayer save = + Layer::AutoSaveLayer::Create(context, paint_bounds(), &paint); PaintChildren(context); } diff --git a/flow/layers/shader_mask_layer.cc b/flow/layers/shader_mask_layer.cc index 3ea69322fc..6cb73bbf64 100644 --- a/flow/layers/shader_mask_layer.cc +++ b/flow/layers/shader_mask_layer.cc @@ -14,7 +14,8 @@ void ShaderMaskLayer::Paint(PaintContext& context) const { TRACE_EVENT0("flutter", "ShaderMaskLayer::Paint"); FML_DCHECK(needs_painting()); - Layer::AutoSaveLayer(context, paint_bounds(), nullptr); + Layer::AutoSaveLayer save = + Layer::AutoSaveLayer::Create(context, paint_bounds(), nullptr); PaintChildren(context); SkPaint paint; -- GitLab