From f8e874e945481e5ff9560483ddfb44a898a8f2b9 Mon Sep 17 00:00:00 2001 From: Jim Graham Date: Mon, 6 Jun 2022 15:23:06 -0700 Subject: [PATCH] add backdrop option to DisplayList::saveLayer (flutter/engine#33854) --- .../src/flutter/display_list/display_list.h | 2 + .../display_list/display_list_builder.cc | 23 +++-- .../display_list/display_list_builder.h | 15 ++- .../display_list_canvas_dispatcher.cc | 13 ++- .../display_list_canvas_dispatcher.h | 4 +- .../display_list_canvas_recorder.cc | 8 +- .../display_list_canvas_unittests.cc | 77 ++++++++++++++- .../display_list_complexity_gl.cc | 9 +- .../display_list/display_list_complexity_gl.h | 3 +- .../display_list_complexity_metal.cc | 9 +- .../display_list_complexity_metal.h | 3 +- .../display_list/display_list_dispatcher.h | 5 +- .../display_list/display_list_image_filter.cc | 3 +- .../display_list/display_list_image_filter.h | 5 +- .../flutter/display_list/display_list_ops.h | 48 +++++++++- .../display_list/display_list_unittests.cc | 43 ++++++++- .../display_list/display_list_utils.cc | 7 +- .../flutter/display_list/display_list_utils.h | 7 +- .../display_list/display_list_dispatcher.cc | 6 +- .../display_list/display_list_dispatcher.h | 3 +- .../flutter/testing/display_list_testing.cc | 94 ++++++++++++------- .../flutter/testing/display_list_testing.h | 13 ++- 22 files changed, 327 insertions(+), 73 deletions(-) diff --git a/engine/src/flutter/display_list/display_list.h b/engine/src/flutter/display_list/display_list.h index 204a4d8953..bf4cc40739 100644 --- a/engine/src/flutter/display_list/display_list.h +++ b/engine/src/flutter/display_list/display_list.h @@ -100,6 +100,8 @@ namespace flutter { V(Save) \ V(SaveLayer) \ V(SaveLayerBounds) \ + V(SaveLayerBackdrop) \ + V(SaveLayerBackdropBounds) \ V(Restore) \ \ V(Translate) \ diff --git a/engine/src/flutter/display_list/display_list_builder.cc b/engine/src/flutter/display_list/display_list_builder.cc index 96aacead66..221b6db186 100644 --- a/engine/src/flutter/display_list/display_list_builder.cc +++ b/engine/src/flutter/display_list/display_list_builder.cc @@ -462,12 +462,19 @@ void DisplayListBuilder::restoreToCount(int restore_count) { } } void DisplayListBuilder::saveLayer(const SkRect* bounds, - const SaveLayerOptions in_options) { + const SaveLayerOptions in_options, + const DlImageFilter* backdrop) { SaveLayerOptions options = in_options.without_optimizations(); size_t save_layer_offset = used_; - bounds // - ? Push(0, 1, *bounds, options) - : Push(0, 1, options); + if (backdrop) { + bounds // + ? Push(0, 1, *bounds, options, backdrop) + : Push(0, 1, options, backdrop); + } else { + bounds // + ? Push(0, 1, *bounds, options) + : Push(0, 1, options); + } CheckLayerOpacityCompatibility(options.renders_with_attributes()); layer_stack_.emplace_back(save_layer_offset, true); current_layer_ = &layer_stack_.back(); @@ -482,13 +489,15 @@ void DisplayListBuilder::saveLayer(const SkRect* bounds, } } } -void DisplayListBuilder::saveLayer(const SkRect* bounds, const DlPaint* paint) { +void DisplayListBuilder::saveLayer(const SkRect* bounds, + const DlPaint* paint, + const DlImageFilter* backdrop) { if (paint != nullptr) { setAttributesFromDlPaint(*paint, DisplayListOpFlags::kSaveLayerWithPaintFlags); - saveLayer(bounds, true); + saveLayer(bounds, SaveLayerOptions::kWithAttributes, backdrop); } else { - saveLayer(bounds, false); + saveLayer(bounds, SaveLayerOptions::kNoAttributes, backdrop); } } diff --git a/engine/src/flutter/display_list/display_list_builder.h b/engine/src/flutter/display_list/display_list_builder.h index 49b085d648..257b979fe9 100644 --- a/engine/src/flutter/display_list/display_list_builder.h +++ b/engine/src/flutter/display_list/display_list_builder.h @@ -154,15 +154,20 @@ class DisplayListBuilder final : public virtual Dispatcher, // Only the |renders_with_attributes()| option will be accepted here. Any // other flags will be ignored and calculated anew as the DisplayList is // built. Alternatively, use the |saveLayer(SkRect, bool)| method. - void saveLayer(const SkRect* bounds, const SaveLayerOptions options) override; + void saveLayer(const SkRect* bounds, + const SaveLayerOptions options, + const DlImageFilter* backdrop) override; // Convenience method with just a boolean to indicate whether the saveLayer // should apply the rendering attributes. void saveLayer(const SkRect* bounds, bool renders_with_attributes) { - saveLayer(bounds, renders_with_attributes - ? SaveLayerOptions::kWithAttributes - : SaveLayerOptions::kNoAttributes); + saveLayer(bounds, + renders_with_attributes ? SaveLayerOptions::kWithAttributes + : SaveLayerOptions::kNoAttributes, + nullptr); } - void saveLayer(const SkRect* bounds, const DlPaint* paint); + void saveLayer(const SkRect* bounds, + const DlPaint* paint, + const DlImageFilter* backdrop = nullptr); void restore() override; int getSaveCount() { return layer_stack_.size(); } void restoreToCount(int restore_count); diff --git a/engine/src/flutter/display_list/display_list_canvas_dispatcher.cc b/engine/src/flutter/display_list/display_list_canvas_dispatcher.cc index 71a1442f39..e9c6a05493 100644 --- a/engine/src/flutter/display_list/display_list_canvas_dispatcher.cc +++ b/engine/src/flutter/display_list/display_list_canvas_dispatcher.cc @@ -40,10 +40,13 @@ void DisplayListCanvasDispatcher::restore() { restore_opacity(); } void DisplayListCanvasDispatcher::saveLayer(const SkRect* bounds, - const SaveLayerOptions options) { - if (bounds == nullptr && options.can_distribute_opacity()) { + const SaveLayerOptions options, + const DlImageFilter* backdrop) { + if (bounds == nullptr && options.can_distribute_opacity() && + backdrop == nullptr) { // We know that: // - no bounds is needed for clipping here + // - no backdrop filter is used to initialize the layer // - the current attributes only have an alpha // - the children are compatible with individually rendering with // an inherited opacity @@ -58,7 +61,11 @@ void DisplayListCanvasDispatcher::saveLayer(const SkRect* bounds, : opacity()); } else { TRACE_EVENT0("flutter", "Canvas::saveLayer"); - canvas_->saveLayer(bounds, safe_paint(options.renders_with_attributes())); + const SkPaint* paint = safe_paint(options.renders_with_attributes()); + const sk_sp sk_backdrop = + backdrop ? backdrop->skia_object() : nullptr; + canvas_->saveLayer( + SkCanvas::SaveLayerRec(bounds, paint, sk_backdrop.get(), 0)); // saveLayer will apply the current opacity on behalf of the children // so they will inherit an opaque opacity. save_opacity(SK_Scalar1); diff --git a/engine/src/flutter/display_list/display_list_canvas_dispatcher.h b/engine/src/flutter/display_list/display_list_canvas_dispatcher.h index 3df170ca7e..6eb4e12aeb 100644 --- a/engine/src/flutter/display_list/display_list_canvas_dispatcher.h +++ b/engine/src/flutter/display_list/display_list_canvas_dispatcher.h @@ -30,7 +30,9 @@ class DisplayListCanvasDispatcher : public virtual Dispatcher, void save() override; void restore() override; - void saveLayer(const SkRect* bounds, const SaveLayerOptions options) override; + void saveLayer(const SkRect* bounds, + const SaveLayerOptions options, + const DlImageFilter* backdrop) override; void translate(SkScalar tx, SkScalar ty) override; void scale(SkScalar sx, SkScalar sy) override; diff --git a/engine/src/flutter/display_list/display_list_canvas_recorder.cc b/engine/src/flutter/display_list/display_list_canvas_recorder.cc index ea43d8c521..9862cba669 100644 --- a/engine/src/flutter/display_list/display_list_canvas_recorder.cc +++ b/engine/src/flutter/display_list/display_list_canvas_recorder.cc @@ -6,6 +6,7 @@ #include "flutter/display_list/display_list_blend_mode.h" #include "flutter/display_list/display_list_builder.h" +#include "flutter/display_list/display_list_image_filter.h" namespace flutter { @@ -59,11 +60,14 @@ void DisplayListCanvasRecorder::willSave() { } SkCanvas::SaveLayerStrategy DisplayListCanvasRecorder::getSaveLayerStrategy( const SaveLayerRec& rec) { + std::shared_ptr backdrop = DlImageFilter::From(rec.fBackdrop); if (rec.fPaint) { builder_->setAttributesFromPaint(*rec.fPaint, kSaveLayerWithPaintFlags); - builder_->saveLayer(rec.fBounds, true); + builder_->saveLayer(rec.fBounds, SaveLayerOptions::kWithAttributes, + backdrop.get()); } else { - builder_->saveLayer(rec.fBounds, false); + builder_->saveLayer(rec.fBounds, SaveLayerOptions::kNoAttributes, + backdrop.get()); } return SaveLayerStrategy::kNoLayer_SaveLayerStrategy; } diff --git a/engine/src/flutter/display_list/display_list_canvas_unittests.cc b/engine/src/flutter/display_list/display_list_canvas_unittests.cc index 5791893dc7..5b733978c8 100644 --- a/engine/src/flutter/display_list/display_list_canvas_unittests.cc +++ b/engine/src/flutter/display_list/display_list_canvas_unittests.cc @@ -831,6 +831,78 @@ class CanvasCompareTester { b.setColor(default_color); }) .with_restore(cv_safe_restore, dl_safe_restore, true)); + { + // Being able to see a backdrop blur requires a non-default background + // so we create a new environment for these tests that has a checkerboard + // background that can be blurred by the backdrop filter. We also want + // to avoid the rendered primitive from obscuring the blurred background + // so we set an alpha value which works for all primitives except for + // drawColor which can override the alpha with its color, but it now uses + // a non-opaque color to avoid that problem. + RenderEnvironment backdrop_env = RenderEnvironment::MakeN32(); + CvSetup cv_backdrop_setup = [=](SkCanvas* cv, SkPaint& p) { + SkPaint setup_p; + setup_p.setShader(kTestImageColorSource.skia_object()); + cv->drawPaint(setup_p); + p.setAlpha(p.getAlpha() / 2); + }; + DlRenderer dl_backdrop_setup = [=](DisplayListBuilder& b) { + b.setColorSource(&kTestImageColorSource); + b.drawPaint(); + b.setColorSource(nullptr); + DlColor current_color = b.getColor(); + b.setColor(current_color.withAlpha(current_color.getAlpha() / 2)); + }; + backdrop_env.init_ref(cv_backdrop_setup, testP.cv_renderer(), + dl_backdrop_setup); + + DlBlurImageFilter backdrop(5, 5, DlTileMode::kDecal); + RenderWith(testP, backdrop_env, tolerance, + CaseParameters( + "saveLayer with backdrop", + [=](SkCanvas* cv, SkPaint& p) { + cv_backdrop_setup(cv, p); + cv->saveLayer(SkCanvas::SaveLayerRec( + nullptr, nullptr, backdrop.skia_object().get(), 0)); + }, + [=](DisplayListBuilder& b) { + dl_backdrop_setup(b); + b.saveLayer(nullptr, SaveLayerOptions::kNoAttributes, + &backdrop); + }) + .with_restore(cv_safe_restore, dl_safe_restore, true)); + RenderWith( + testP, backdrop_env, tolerance, + CaseParameters( + "saveLayer with bounds and backdrop", + [=](SkCanvas* cv, SkPaint& p) { + cv_backdrop_setup(cv, p); + cv->saveLayer(SkCanvas::SaveLayerRec( + &layer_bounds, nullptr, backdrop.skia_object().get(), 0)); + }, + [=](DisplayListBuilder& b) { + dl_backdrop_setup(b); + b.saveLayer(&layer_bounds, SaveLayerOptions::kNoAttributes, + &backdrop); + }) + .with_restore(cv_safe_restore, dl_safe_restore, true)); + RenderWith(testP, backdrop_env, tolerance, + CaseParameters( + "clipped saveLayer with backdrop", + [=](SkCanvas* cv, SkPaint& p) { + cv_backdrop_setup(cv, p); + cv->clipRect(layer_bounds); + cv->saveLayer(SkCanvas::SaveLayerRec( + nullptr, nullptr, backdrop.skia_object().get(), 0)); + }, + [=](DisplayListBuilder& b) { + dl_backdrop_setup(b); + b.clipRect(layer_bounds, SkClipOp::kIntersect, false); + b.saveLayer(nullptr, SaveLayerOptions::kNoAttributes, + &backdrop); + }) + .with_restore(cv_safe_restore, dl_safe_restore, true)); + } { // clang-format off @@ -2174,13 +2246,14 @@ TEST_F(DisplayListCanvas, DrawPaint) { } TEST_F(DisplayListCanvas, DrawColor) { + // We use a non-opaque color to avoid obliterating any backdrop filter output CanvasCompareTester::RenderAll( // TestParameters( [=](SkCanvas* canvas, const SkPaint& paint) { - canvas->drawColor(SK_ColorMAGENTA); + canvas->drawColor(0x7FFF00FF); }, [=](DisplayListBuilder& builder) { - builder.drawColor(SK_ColorMAGENTA, DlBlendMode::kSrcOver); + builder.drawColor(0x7FFF00FF, DlBlendMode::kSrcOver); }, kDrawColorFlags)); } diff --git a/engine/src/flutter/display_list/display_list_complexity_gl.cc b/engine/src/flutter/display_list/display_list_complexity_gl.cc index 2f41e31ae2..4b4cf4d5f2 100644 --- a/engine/src/flutter/display_list/display_list_complexity_gl.cc +++ b/engine/src/flutter/display_list/display_list_complexity_gl.cc @@ -50,10 +50,17 @@ unsigned int DisplayListGLComplexityCalculator::GLHelper::BatchedComplexity() { void DisplayListGLComplexityCalculator::GLHelper::saveLayer( const SkRect* bounds, - const SaveLayerOptions options) { + const SaveLayerOptions options, + const DlImageFilter* backdrop) { if (IsComplex()) { return; } + if (backdrop) { + // Flutter does not offer this operation so this value can only ever be + // non-null for a frame-wide builder which is not currently evaluated for + // complexity. + AccumulateComplexity(Ceiling()); + } save_layer_count_++; } diff --git a/engine/src/flutter/display_list/display_list_complexity_gl.h b/engine/src/flutter/display_list/display_list_complexity_gl.h index 81c61ab08b..4191723003 100644 --- a/engine/src/flutter/display_list/display_list_complexity_gl.h +++ b/engine/src/flutter/display_list/display_list_complexity_gl.h @@ -38,7 +38,8 @@ class DisplayListGLComplexityCalculator draw_text_blob_count_(0) {} void saveLayer(const SkRect* bounds, - const SaveLayerOptions options) override; + const SaveLayerOptions options, + const DlImageFilter* backdrop) override; void drawLine(const SkPoint& p0, const SkPoint& p1) override; void drawRect(const SkRect& rect) override; diff --git a/engine/src/flutter/display_list/display_list_complexity_metal.cc b/engine/src/flutter/display_list/display_list_complexity_metal.cc index 49dead29b5..a18eaa40a4 100644 --- a/engine/src/flutter/display_list/display_list_complexity_metal.cc +++ b/engine/src/flutter/display_list/display_list_complexity_metal.cc @@ -64,10 +64,17 @@ DisplayListMetalComplexityCalculator::MetalHelper::BatchedComplexity() { void DisplayListMetalComplexityCalculator::MetalHelper::saveLayer( const SkRect* bounds, - const SaveLayerOptions options) { + const SaveLayerOptions options, + const DlImageFilter* backdrop) { if (IsComplex()) { return; } + if (backdrop) { + // Flutter does not offer this operation so this value can only ever be + // non-null for a frame-wide builder which is not currently evaluated for + // complexity. + AccumulateComplexity(Ceiling()); + } save_layer_count_++; } diff --git a/engine/src/flutter/display_list/display_list_complexity_metal.h b/engine/src/flutter/display_list/display_list_complexity_metal.h index 6631ab6ec9..0d8c1299bc 100644 --- a/engine/src/flutter/display_list/display_list_complexity_metal.h +++ b/engine/src/flutter/display_list/display_list_complexity_metal.h @@ -38,7 +38,8 @@ class DisplayListMetalComplexityCalculator draw_text_blob_count_(0) {} void saveLayer(const SkRect* bounds, - const SaveLayerOptions options) override; + const SaveLayerOptions options, + const DlImageFilter* backdrop) override; void drawLine(const SkPoint& p0, const SkPoint& p1) override; void drawRect(const SkRect& rect) override; diff --git a/engine/src/flutter/display_list/display_list_dispatcher.h b/engine/src/flutter/display_list/display_list_dispatcher.h index c12fd90467..a4f3c3abaa 100644 --- a/engine/src/flutter/display_list/display_list_dispatcher.h +++ b/engine/src/flutter/display_list/display_list_dispatcher.h @@ -69,8 +69,11 @@ class Dispatcher { // to avoid creating a temporary layer, these optimization options will // be determined as the |DisplayList| is constructed and should not be // specified in calling a |DisplayListBuilder| as they will be ignored. + // The |backdrop| filter, if not null, is used to initialize the new + // layer before further rendering happens. virtual void saveLayer(const SkRect* bounds, - const SaveLayerOptions options) = 0; + const SaveLayerOptions options, + const DlImageFilter* backdrop = nullptr) = 0; virtual void restore() = 0; virtual void translate(SkScalar tx, SkScalar ty) = 0; diff --git a/engine/src/flutter/display_list/display_list_image_filter.cc b/engine/src/flutter/display_list/display_list_image_filter.cc index 0f27bec975..11da9b92d0 100644 --- a/engine/src/flutter/display_list/display_list_image_filter.cc +++ b/engine/src/flutter/display_list/display_list_image_filter.cc @@ -6,7 +6,8 @@ namespace flutter { -std::shared_ptr DlImageFilter::From(SkImageFilter* sk_filter) { +std::shared_ptr DlImageFilter::From( + const SkImageFilter* sk_filter) { if (sk_filter == nullptr) { return nullptr; } diff --git a/engine/src/flutter/display_list/display_list_image_filter.h b/engine/src/flutter/display_list/display_list_image_filter.h index 25d2e93bb2..d17fbd68fb 100644 --- a/engine/src/flutter/display_list/display_list_image_filter.h +++ b/engine/src/flutter/display_list/display_list_image_filter.h @@ -52,7 +52,7 @@ class DlImageFilter // This method can only detect the ColorFilter type of ImageFilter from an // analogous SkImageFilter as there are no "asA..." methods for the other // types on SkImageFilter. - static std::shared_ptr From(SkImageFilter* sk_filter); + static std::shared_ptr From(const SkImageFilter* sk_filter); // Return a shared_ptr holding a DlImageFilter representing the indicated // Skia SkImageFilter pointer. @@ -60,7 +60,8 @@ class DlImageFilter // This method can only detect the ColorFilter type of ImageFilter from an // analogous SkImageFilter as there are no "asA..." methods for the other // types on SkImageFilter. - static std::shared_ptr From(sk_sp sk_filter) { + static std::shared_ptr From( + const sk_sp sk_filter) { return From(sk_filter.get()); } diff --git a/engine/src/flutter/display_list/display_list_ops.h b/engine/src/flutter/display_list/display_list_ops.h index 9189381362..d75c596aa0 100644 --- a/engine/src/flutter/display_list/display_list_ops.h +++ b/engine/src/flutter/display_list/display_list_ops.h @@ -245,8 +245,7 @@ struct SetImageColorSourceOp : DLOp { } }; -// 4 byte header + 24 bytes for the DlColorFilterImageFilter -// uses 32 total bytes (4 bytes unused) +// 4 byte header + 16 byte payload uses 24 total bytes (4 bytes unused) struct SetSharedImageFilterOp : DLOp { static const auto kType = DisplayListOpType::kSetSharedImageFilter; @@ -299,6 +298,51 @@ struct SaveLayerBoundsOp final : DLOp { dispatcher.saveLayer(&rect, options); } }; +// 4 byte header + 20 byte payload packs into minimum 24 bytes +struct SaveLayerBackdropOp final : DLOp { + static const auto kType = DisplayListOpType::kSaveLayerBackdrop; + + explicit SaveLayerBackdropOp(const SaveLayerOptions options, + const DlImageFilter* backdrop) + : options(options), backdrop(backdrop->shared()) {} + + SaveLayerOptions options; + const std::shared_ptr backdrop; + + void dispatch(Dispatcher& dispatcher) const { + dispatcher.saveLayer(nullptr, options, backdrop.get()); + } + + DisplayListCompare equals(const SaveLayerBackdropOp* other) const { + return options == other->options && Equals(backdrop, other->backdrop) + ? DisplayListCompare::kEqual + : DisplayListCompare::kNotEqual; + } +}; +// 4 byte header + 36 byte payload packs evenly into 36 bytes +struct SaveLayerBackdropBoundsOp final : DLOp { + static const auto kType = DisplayListOpType::kSaveLayerBackdropBounds; + + SaveLayerBackdropBoundsOp(SkRect rect, + const SaveLayerOptions options, + const DlImageFilter* backdrop) + : options(options), rect(rect), backdrop(backdrop->shared()) {} + + SaveLayerOptions options; + const SkRect rect; + const std::shared_ptr backdrop; + + void dispatch(Dispatcher& dispatcher) const { + dispatcher.saveLayer(&rect, options, backdrop.get()); + } + + DisplayListCompare equals(const SaveLayerBackdropBoundsOp* other) const { + return (options == other->options && rect == other->rect && + Equals(backdrop, other->backdrop)) + ? DisplayListCompare::kEqual + : DisplayListCompare::kNotEqual; + } +}; // 4 byte header + no payload uses minimum 8 bytes (4 bytes unused) struct RestoreOp final : DLOp { static const auto kType = DisplayListOpType::kRestore; diff --git a/engine/src/flutter/display_list/display_list_unittests.cc b/engine/src/flutter/display_list/display_list_unittests.cc index 5ff4bec513..e9ef2e5bc8 100644 --- a/engine/src/flutter/display_list/display_list_unittests.cc +++ b/engine/src/flutter/display_list/display_list_unittests.cc @@ -450,6 +450,13 @@ std::vector allGroups = { } }, { "Save(Layer)+Restore", { + {5, 104, 5, 104, [](DisplayListBuilder& b) { + b.saveLayer(nullptr, SaveLayerOptions::kNoAttributes, &kTestCFImageFilter1); + b.clipRect({0, 0, 25, 25}, SkClipOp::kIntersect, true); + b.drawRect({5, 5, 15, 15}); + b.drawRect({10, 10, 20, 20}); + b.restore(); + }}, // There are many reasons that save and restore can elide content, including // whether or not there are any draw operations between them, whether or not // there are any state changes to restore, and whether group rendering (opacity) @@ -491,6 +498,36 @@ std::vector allGroups = { b.drawRect({10, 10, 20, 20}); b.restore(); }}, + // backdrop variants - using the TestCFImageFilter because it can be + // reconstituted in the DL->SkCanvas->DL stream + // {5, 104, 5, 104, [](DisplayListBuilder& b) { + // b.saveLayer(nullptr, SaveLayerOptions::kNoAttributes, &kTestCFImageFilter1); + // b.clipRect({0, 0, 25, 25}, SkClipOp::kIntersect, true); + // b.drawRect({5, 5, 15, 15}); + // b.drawRect({10, 10, 20, 20}); + // b.restore(); + // }}, + {5, 104, 5, 104, [](DisplayListBuilder& b) { + b.saveLayer(nullptr, SaveLayerOptions::kWithAttributes, &kTestCFImageFilter1); + b.clipRect({0, 0, 25, 25}, SkClipOp::kIntersect, true); + b.drawRect({5, 5, 15, 15}); + b.drawRect({10, 10, 20, 20}); + b.restore(); + }}, + {5, 120, 5, 120, [](DisplayListBuilder& b) { + b.saveLayer(&kTestBounds, SaveLayerOptions::kNoAttributes, &kTestCFImageFilter1); + b.clipRect({0, 0, 25, 25}, SkClipOp::kIntersect, true); + b.drawRect({5, 5, 15, 15}); + b.drawRect({10, 10, 20, 20}); + b.restore(); + }}, + {5, 120, 5, 120, [](DisplayListBuilder& b) { + b.saveLayer(&kTestBounds, SaveLayerOptions::kWithAttributes, &kTestCFImageFilter1); + b.clipRect({0, 0, 25, 25}, SkClipOp::kIntersect, true); + b.drawRect({5, 5, 15, 15}); + b.drawRect({10, 10, 20, 20}); + b.restore(); + }}, } }, { "Translate", { @@ -1603,7 +1640,8 @@ class SaveLayerOptionsExpector : public virtual Dispatcher, : expected_(expected) {} void saveLayer(const SkRect* bounds, - const SaveLayerOptions options) override { + const SaveLayerOptions options, + const DlImageFilter* backdrop) override { EXPECT_EQ(options, expected_[save_layer_count_]); save_layer_count_++; } @@ -1877,7 +1915,8 @@ TEST(DisplayList, FlutterSvgIssue661BoundsWereEmpty) { builder.save(); builder.clipRect({1172, 245, 1218, 294}, SkClipOp::kIntersect, true); { - builder.saveLayer(nullptr, SaveLayerOptions::kWithAttributes); + builder.saveLayer(nullptr, SaveLayerOptions::kWithAttributes, + nullptr); { builder.save(); builder.transform2DAffine(1.4375, 0, 1164.09, // diff --git a/engine/src/flutter/display_list/display_list_utils.cc b/engine/src/flutter/display_list/display_list_utils.cc index 9fda357261..ccdd2b65cc 100644 --- a/engine/src/flutter/display_list/display_list_utils.cc +++ b/engine/src/flutter/display_list/display_list_utils.cc @@ -290,7 +290,8 @@ void DisplayListBoundsCalculator::save() { accumulator_ = layer_infos_.back()->layer_accumulator(); } void DisplayListBoundsCalculator::saveLayer(const SkRect* bounds, - const SaveLayerOptions options) { + const SaveLayerOptions options, + const DlImageFilter* backdrop) { SkMatrixDispatchHelper::save(); ClipBoundsDispatchHelper::save(); if (options.renders_with_attributes()) { @@ -318,6 +319,10 @@ void DisplayListBoundsCalculator::saveLayer(const SkRect* bounds, if (bounds) { clipRect(*bounds, SkClipOp::kIntersect, false); } + if (backdrop) { + // A backdrop will affect up to the entire surface, bounded by the clip + AccumulateUnbounded(); + } } void DisplayListBoundsCalculator::restore() { if (layer_infos_.size() > 1) { diff --git a/engine/src/flutter/display_list/display_list_utils.h b/engine/src/flutter/display_list/display_list_utils.h index 8f3dd5486b..f214245903 100644 --- a/engine/src/flutter/display_list/display_list_utils.h +++ b/engine/src/flutter/display_list/display_list_utils.h @@ -96,7 +96,8 @@ class IgnoreDrawDispatchHelper : public virtual Dispatcher { public: void save() override {} void saveLayer(const SkRect* bounds, - const SaveLayerOptions options) override {} + const SaveLayerOptions options, + const DlImageFilter* backdrop) override {} void restore() override {} void drawColor(DlColor color, DlBlendMode mode) override {} void drawPaint() override {} @@ -408,7 +409,9 @@ class DisplayListBoundsCalculator final void setMaskFilter(const DlMaskFilter* filter) override; void save() override; - void saveLayer(const SkRect* bounds, const SaveLayerOptions options) override; + void saveLayer(const SkRect* bounds, + const SaveLayerOptions options, + const DlImageFilter* backdrop) override; void restore() override; void drawPaint() override; diff --git a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc index 02b3414b8e..91c10ae99d 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -379,7 +379,11 @@ static std::optional ToRect(const SkRect* rect) { // |flutter::Dispatcher| void DisplayListDispatcher::saveLayer(const SkRect* bounds, - const flutter::SaveLayerOptions options) { + const flutter::SaveLayerOptions options, + const flutter::DlImageFilter* backdrop) { + if (backdrop) { + UNIMPLEMENTED; + } canvas_.SaveLayer(options.renders_with_attributes() ? paint_ : Paint{}, ToRect(bounds)); } diff --git a/engine/src/flutter/impeller/display_list/display_list_dispatcher.h b/engine/src/flutter/impeller/display_list/display_list_dispatcher.h index 338a79c877..d7e430d092 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.h +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.h @@ -75,7 +75,8 @@ class DisplayListDispatcher final : public flutter::Dispatcher { // |flutter::Dispatcher| void saveLayer(const SkRect* bounds, - const flutter::SaveLayerOptions options) override; + const flutter::SaveLayerOptions options, + const flutter::DlImageFilter* backdrop) override; // |flutter::Dispatcher| void restore() override; diff --git a/engine/src/flutter/testing/display_list_testing.cc b/engine/src/flutter/testing/display_list_testing.cc index ed8b19b014..9bc72be949 100644 --- a/engine/src/flutter/testing/display_list_testing.cc +++ b/engine/src/flutter/testing/display_list_testing.cc @@ -115,7 +115,7 @@ static std::ostream& operator<<(std::ostream& os, const SkRect& rect) { } static std::ostream& operator<<(std::ostream& os, const SkRect* rect) { - return rect ? os << *rect : os << "no rect"; + return rect ? (os << "&" << *rect) : os << "no rect"; } static std::ostream& operator<<(std::ostream& os, const SkRRect& rrect) { @@ -148,7 +148,7 @@ static std::ostream& operator<<(std::ostream& os, const SkMatrix& matrix) { } static std::ostream& operator<<(std::ostream& os, const SkMatrix* matrix) { - if (matrix) return os << *matrix; + if (matrix) return os << "&" << *matrix; return os << "no matrix"; } @@ -255,7 +255,7 @@ static std::ostream& operator<<(std::ostream& os, const SkTextBlob* blob) { if (blob == nullptr) { return os << "no text"; } - return os << "SkTextBlob(ID: " << blob->uniqueID() << ", " << blob->bounds() << ")"; + return os << "&SkTextBlob(ID: " << blob->uniqueID() << ", " << blob->bounds() << ")"; } static std::ostream& operator<<(std::ostream& os, const DlVertexMode& mode) { @@ -283,7 +283,7 @@ static std::ostream& operator<<(std::ostream& os, const DlImage* image) { if (image == nullptr) { return os << "null image"; } - os << "DlImage(" << image->width() << " x " << image->height() << ", "; + os << "&DlImage(" << image->width() << " x " << image->height() << ", "; if (image->skia_image()) { os << "skia(" << image->skia_image().get() << "), "; } @@ -359,7 +359,7 @@ void DisplayListStreamDispatcher::setColorSource(const DlColorSource* source) { case DlColorSourceType::kImage: { const DlImageColorSource* image_src = source->asImage(); FML_DCHECK(image_src); - os_ << "DlImageColorSource(" << image_src->image() + os_ << "DlImageColorSource(image: " << image_src->image() << ", hMode: " << image_src->horizontal_tile_mode() << ", vMode: " << image_src->vertical_tile_mode() << ", " << image_src->sampling() @@ -420,17 +420,17 @@ void DisplayListStreamDispatcher::setColorSource(const DlColorSource* source) { } os_ << ");" << std::endl; } -void DisplayListStreamDispatcher::out(const DlColorFilter* filter) { - switch (filter->type()) { +void DisplayListStreamDispatcher::out(const DlColorFilter& filter) { + switch (filter.type()) { case DlColorFilterType::kBlend: { - const DlBlendColorFilter* blend = filter->asBlend(); + const DlBlendColorFilter* blend = filter.asBlend(); FML_DCHECK(blend); os_ << "DlBlendColorFilter(" << blend->color() << ", " << static_cast(blend->mode()) << ")"; break; } case DlColorFilterType::kMatrix: { - const DlMatrixColorFilter* matrix = filter->asMatrix(); + const DlMatrixColorFilter* matrix = filter.asMatrix(); FML_DCHECK(matrix); float values[20]; matrix->get_matrix(values); @@ -444,8 +444,8 @@ void DisplayListStreamDispatcher::out(const DlColorFilter* filter) { << values[i+4] << "," << std::endl; } - startl() << "]"; outdent(); + startl() << "]"; break; } case DlColorFilterType::kSrgbToLinearGamma: { @@ -457,15 +457,19 @@ void DisplayListStreamDispatcher::out(const DlColorFilter* filter) { break; } default: - os_ << "DlUnknownColorFilter(" << filter->skia_object().get() << ")"; + os_ << "DlUnknownColorFilter(" << filter.skia_object().get() << ")"; break; } } -void DisplayListStreamDispatcher::setColorFilter(const DlColorFilter* filter) { +void DisplayListStreamDispatcher::out(const DlColorFilter* filter) { if (filter == nullptr) { - startl() << "setColorFilter(no ColorFilter);" << std::endl; - return; + os_ << "no ColorFilter"; + } else { + os_ << "&"; + out(*filter); } +} +void DisplayListStreamDispatcher::setColorFilter(const DlColorFilter* filter) { startl() << "setColorFilter("; out(filter); os_ << ");" << std::endl; @@ -501,15 +505,10 @@ void DisplayListStreamDispatcher::setMaskFilter(const DlMaskFilter* filter) { } os_ << ");" << std::endl; } -void DisplayListStreamDispatcher::setImageFilter(const DlImageFilter* filter) { - if (filter == nullptr) { - startl() << "setImageFilter(no ImageFilter);" << std::endl; - return; - } - startl() << "setImageFilter("; - switch (filter->type()) { +void DisplayListStreamDispatcher::out(const DlImageFilter& filter) { + switch (filter.type()) { case DlImageFilterType::kBlur: { - const DlBlurImageFilter* blur = filter->asBlur(); + const DlBlurImageFilter* blur = filter.asBlur(); FML_DCHECK(blur); os_ << "DlBlurImageFilter(" << blur->sigma_x() << ", " << blur->sigma_y() << ", " @@ -517,44 +516,62 @@ void DisplayListStreamDispatcher::setImageFilter(const DlImageFilter* filter) { break; } case DlImageFilterType::kDilate: { - const DlDilateImageFilter* dilate = filter->asDilate(); + const DlDilateImageFilter* dilate = filter.asDilate(); FML_DCHECK(dilate); os_ << "DlDilateImageFilter(" << dilate->radius_x() << ", " << dilate->radius_y() << ")"; break; } case DlImageFilterType::kErode: { - const DlErodeImageFilter* erode = filter->asErode(); + const DlErodeImageFilter* erode = filter.asErode(); FML_DCHECK(erode); os_ << "DlDilateImageFilter(" << erode->radius_x() << ", " << erode->radius_y() << ")"; break; } case DlImageFilterType::kMatrix: { - const DlMatrixImageFilter* matrix = filter->asMatrix(); + const DlMatrixImageFilter* matrix = filter.asMatrix(); FML_DCHECK(matrix); os_ << "DlMatrixImageFilter(" << matrix->matrix() << ", " << matrix->sampling() << ")"; break; } case DlImageFilterType::kComposeFilter: { - const DlComposeImageFilter* compose = filter->asCompose(); + const DlComposeImageFilter* compose = filter.asCompose(); FML_DCHECK(compose); os_ << "DlComposeImageFilter(" << std::endl; indent(); - startl() << "outer: " << compose->outer() << "," << std::endl; - startl() << "inner: " << compose->inner() << "," << std::endl; + startl() << "outer: "; + out(compose->outer().get()); + os_ << "," << std::endl; + startl() << "inner: "; + out(compose->inner().get()); + os_ << "," << std::endl; outdent(); startl() << ")"; break; } case DlImageFilterType::kColorFilter: { - const DlColorFilterImageFilter* color_filter = filter->asColorFilter(); + const DlColorFilterImageFilter* color_filter = filter.asColorFilter(); FML_DCHECK(color_filter); - out(color_filter->color_filter().get()); + os_ << "DlColorFilterImageFilter("; + out(*color_filter->color_filter()); + os_ << ")"; break; } default: - os_ << "DlUnknownImageFilter(" << filter->skia_object().get() << ")"; + os_ << "DlUnknownImageFilter(" << filter.skia_object().get() << ")"; break; } +} +void DisplayListStreamDispatcher::out(const DlImageFilter* filter) { + if (filter == nullptr) { + os_ << "no ImageFilter"; + } else { + os_ << "&"; + out(*filter); + } +} +void DisplayListStreamDispatcher::setImageFilter(const DlImageFilter* filter) { + startl() << "setImageFilter("; + out(filter); os_ << ");" << std::endl; } void DisplayListStreamDispatcher::save() { @@ -563,8 +580,19 @@ void DisplayListStreamDispatcher::save() { indent(); } void DisplayListStreamDispatcher::saveLayer(const SkRect* bounds, - const SaveLayerOptions options) { - startl() << "saveLayer(" << bounds << ", " << options << ");" << std::endl; + const SaveLayerOptions options, + const DlImageFilter* backdrop) { + startl() << "saveLayer(" << bounds << ", " << options; + if (backdrop) { + os_ << "," << std::endl; + indent(10); + startl() << "backdrop: "; + out(backdrop); + outdent(10); + } else { + os_ << ", no backdrop"; + } + os_ << ");" << std::endl; startl() << "{" << std::endl; indent(); } diff --git a/engine/src/flutter/testing/display_list_testing.h b/engine/src/flutter/testing/display_list_testing.h index cadc7c6e8f..fd6a608420 100644 --- a/engine/src/flutter/testing/display_list_testing.h +++ b/engine/src/flutter/testing/display_list_testing.h @@ -59,7 +59,9 @@ class DisplayListStreamDispatcher final : public Dispatcher { void setImageFilter(const DlImageFilter* filter) override; void save() override; - void saveLayer(const SkRect* bounds, const SaveLayerOptions options) override; + void saveLayer(const SkRect* bounds, + const SaveLayerOptions options, + const DlImageFilter* backdrop) override; void restore() override; void translate(SkScalar tx, SkScalar ty) override; @@ -147,15 +149,20 @@ class DisplayListStreamDispatcher final : public Dispatcher { int cur_indent_; int indent_; - void indent() { cur_indent_ += indent_; } - void outdent() { cur_indent_ -= indent_; } + void indent() { indent(indent_); } + void outdent() { outdent(indent_); } + void indent(int spaces) { cur_indent_ += spaces; } + void outdent(int spaces) { cur_indent_ -= spaces; } template std::ostream& out_array(std::string name, int count, const T array[]); std::ostream& startl(); + void out(const DlColorFilter& filter); void out(const DlColorFilter* filter); + void out(const DlImageFilter& filter); + void out(const DlImageFilter* filter); }; } // namespace testing