|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "flutter/flow/layers/image_filter_layer.h" |
| 6 | + |
| 7 | +#include "flutter/flow/testing/layer_test.h" |
| 8 | +#include "flutter/flow/testing/mock_layer.h" |
| 9 | +#include "flutter/fml/macros.h" |
| 10 | +#include "flutter/testing/mock_canvas.h" |
| 11 | +#include "third_party/skia/include/core/SkImageFilter.h" |
| 12 | + |
| 13 | +namespace flutter { |
| 14 | +namespace testing { |
| 15 | + |
| 16 | +using ImageFilterLayerTest = LayerTest; |
| 17 | + |
| 18 | +#ifndef NDEBUG |
| 19 | +TEST_F(ImageFilterLayerTest, PaintingEmptyLayerDies) { |
| 20 | + auto layer = std::make_shared<ImageFilterLayer>(sk_sp<SkImageFilter>()); |
| 21 | + |
| 22 | + layer->Preroll(preroll_context(), SkMatrix()); |
| 23 | + EXPECT_EQ(layer->paint_bounds(), kEmptyRect); |
| 24 | + EXPECT_FALSE(layer->needs_painting()); |
| 25 | + EXPECT_FALSE(layer->needs_system_composite()); |
| 26 | + |
| 27 | + EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()), |
| 28 | + "needs_painting\\(\\)"); |
| 29 | +} |
| 30 | + |
| 31 | +TEST_F(ImageFilterLayerTest, PaintBeforePrerollDies) { |
| 32 | + const SkRect child_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f); |
| 33 | + const SkPath child_path = SkPath().addRect(child_bounds); |
| 34 | + auto mock_layer = std::make_shared<MockLayer>(child_path); |
| 35 | + auto layer = std::make_shared<ImageFilterLayer>(sk_sp<SkImageFilter>()); |
| 36 | + layer->Add(mock_layer); |
| 37 | + |
| 38 | + EXPECT_EQ(layer->paint_bounds(), kEmptyRect); |
| 39 | + EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()), |
| 40 | + "needs_painting\\(\\)"); |
| 41 | +} |
| 42 | +#endif |
| 43 | + |
| 44 | +TEST_F(ImageFilterLayerTest, EmptyFilter) { |
| 45 | + const SkMatrix initial_transform = SkMatrix::MakeTrans(0.5f, 1.0f); |
| 46 | + const SkRect child_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f); |
| 47 | + const SkPath child_path = SkPath().addRect(child_bounds); |
| 48 | + const SkPaint child_paint = SkPaint(SkColors::kYellow); |
| 49 | + auto mock_layer = std::make_shared<MockLayer>(child_path, child_paint); |
| 50 | + auto layer = std::make_shared<ImageFilterLayer>(nullptr); |
| 51 | + layer->Add(mock_layer); |
| 52 | + |
| 53 | + layer->Preroll(preroll_context(), initial_transform); |
| 54 | + EXPECT_EQ(layer->paint_bounds(), child_bounds); |
| 55 | + EXPECT_TRUE(layer->needs_painting()); |
| 56 | + EXPECT_EQ(mock_layer->parent_matrix(), initial_transform); |
| 57 | + |
| 58 | + SkPaint filter_paint; |
| 59 | + filter_paint.setImageFilter(nullptr); |
| 60 | + layer->Paint(paint_context()); |
| 61 | + EXPECT_EQ(mock_canvas().draw_calls(), |
| 62 | + std::vector({ |
| 63 | + MockCanvas::DrawCall{0, MockCanvas::SaveData{1}}, |
| 64 | + MockCanvas::DrawCall{1, MockCanvas::SetMatrixData{SkMatrix()}}, |
| 65 | + MockCanvas::DrawCall{ |
| 66 | + 1, MockCanvas::SaveLayerData{child_bounds, filter_paint, |
| 67 | + nullptr, 2}}, |
| 68 | + MockCanvas::DrawCall{ |
| 69 | + 2, MockCanvas::DrawPathData{child_path, child_paint}}, |
| 70 | + MockCanvas::DrawCall{2, MockCanvas::RestoreData{1}}, |
| 71 | + MockCanvas::DrawCall{1, MockCanvas::RestoreData{0}}, |
| 72 | + })); |
| 73 | +} |
| 74 | + |
| 75 | +TEST_F(ImageFilterLayerTest, SimpleFilter) { |
| 76 | + const SkMatrix initial_transform = SkMatrix::MakeTrans(0.5f, 1.0f); |
| 77 | + const SkRect child_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f); |
| 78 | + const SkPath child_path = SkPath().addRect(child_bounds); |
| 79 | + const SkPaint child_paint = SkPaint(SkColors::kYellow); |
| 80 | + auto layer_filter = SkImageFilter::MakeMatrixFilter( |
| 81 | + SkMatrix(), SkFilterQuality::kMedium_SkFilterQuality, nullptr); |
| 82 | + auto mock_layer = std::make_shared<MockLayer>(child_path, child_paint); |
| 83 | + auto layer = std::make_shared<ImageFilterLayer>(layer_filter); |
| 84 | + layer->Add(mock_layer); |
| 85 | + |
| 86 | + layer->Preroll(preroll_context(), initial_transform); |
| 87 | + EXPECT_EQ(layer->paint_bounds(), child_bounds); |
| 88 | + EXPECT_TRUE(layer->needs_painting()); |
| 89 | + EXPECT_EQ(mock_layer->parent_matrix(), initial_transform); |
| 90 | + |
| 91 | + SkPaint filter_paint; |
| 92 | + filter_paint.setImageFilter(layer_filter); |
| 93 | + layer->Paint(paint_context()); |
| 94 | + EXPECT_EQ(mock_canvas().draw_calls(), |
| 95 | + std::vector({ |
| 96 | + MockCanvas::DrawCall{0, MockCanvas::SaveData{1}}, |
| 97 | + MockCanvas::DrawCall{1, MockCanvas::SetMatrixData{SkMatrix()}}, |
| 98 | + MockCanvas::DrawCall{ |
| 99 | + 1, MockCanvas::SaveLayerData{child_bounds, filter_paint, |
| 100 | + nullptr, 2}}, |
| 101 | + MockCanvas::DrawCall{ |
| 102 | + 2, MockCanvas::DrawPathData{child_path, child_paint}}, |
| 103 | + MockCanvas::DrawCall{2, MockCanvas::RestoreData{1}}, |
| 104 | + MockCanvas::DrawCall{1, MockCanvas::RestoreData{0}}, |
| 105 | + })); |
| 106 | +} |
| 107 | + |
| 108 | +TEST_F(ImageFilterLayerTest, MultipleChildren) { |
| 109 | + const SkMatrix initial_transform = SkMatrix::MakeTrans(0.5f, 1.0f); |
| 110 | + const SkRect child_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 2.5f, 3.5f); |
| 111 | + const SkPath child_path1 = SkPath().addRect(child_bounds); |
| 112 | + const SkPath child_path2 = |
| 113 | + SkPath().addRect(child_bounds.makeOffset(3.0f, 0.0f)); |
| 114 | + const SkPaint child_paint1 = SkPaint(SkColors::kYellow); |
| 115 | + const SkPaint child_paint2 = SkPaint(SkColors::kCyan); |
| 116 | + auto layer_filter = SkImageFilter::MakeMatrixFilter( |
| 117 | + SkMatrix(), SkFilterQuality::kMedium_SkFilterQuality, nullptr); |
| 118 | + auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1); |
| 119 | + auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2); |
| 120 | + auto layer = std::make_shared<ImageFilterLayer>(layer_filter); |
| 121 | + layer->Add(mock_layer1); |
| 122 | + layer->Add(mock_layer2); |
| 123 | + |
| 124 | + SkRect children_bounds = child_path1.getBounds(); |
| 125 | + children_bounds.join(child_path2.getBounds()); |
| 126 | + layer->Preroll(preroll_context(), initial_transform); |
| 127 | + EXPECT_EQ(mock_layer1->paint_bounds(), child_path1.getBounds()); |
| 128 | + EXPECT_EQ(mock_layer2->paint_bounds(), child_path2.getBounds()); |
| 129 | + EXPECT_EQ(layer->paint_bounds(), children_bounds); |
| 130 | + EXPECT_TRUE(mock_layer1->needs_painting()); |
| 131 | + EXPECT_TRUE(mock_layer2->needs_painting()); |
| 132 | + EXPECT_TRUE(layer->needs_painting()); |
| 133 | + EXPECT_EQ(mock_layer1->parent_matrix(), initial_transform); |
| 134 | + EXPECT_EQ(mock_layer2->parent_matrix(), initial_transform); |
| 135 | + |
| 136 | + SkPaint filter_paint; |
| 137 | + filter_paint.setImageFilter(layer_filter); |
| 138 | + layer->Paint(paint_context()); |
| 139 | + EXPECT_EQ(mock_canvas().draw_calls(), |
| 140 | + std::vector( |
| 141 | + {MockCanvas::DrawCall{0, MockCanvas::SaveData{1}}, |
| 142 | + MockCanvas::DrawCall{1, MockCanvas::SetMatrixData{SkMatrix()}}, |
| 143 | + MockCanvas::DrawCall{ |
| 144 | + 1, MockCanvas::SaveLayerData{children_bounds, filter_paint, |
| 145 | + nullptr, 2}}, |
| 146 | + MockCanvas::DrawCall{ |
| 147 | + 2, MockCanvas::DrawPathData{child_path1, child_paint1}}, |
| 148 | + MockCanvas::DrawCall{ |
| 149 | + 2, MockCanvas::DrawPathData{child_path2, child_paint2}}, |
| 150 | + MockCanvas::DrawCall{2, MockCanvas::RestoreData{1}}, |
| 151 | + MockCanvas::DrawCall{1, MockCanvas::RestoreData{0}}})); |
| 152 | +} |
| 153 | + |
| 154 | +TEST_F(ImageFilterLayerTest, Nested) { |
| 155 | + const SkMatrix initial_transform = SkMatrix::MakeTrans(0.5f, 1.0f); |
| 156 | + const SkRect child_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 2.5f, 3.5f); |
| 157 | + const SkPath child_path1 = SkPath().addRect(child_bounds); |
| 158 | + const SkPath child_path2 = |
| 159 | + SkPath().addRect(child_bounds.makeOffset(3.0f, 0.0f)); |
| 160 | + const SkPaint child_paint1 = SkPaint(SkColors::kYellow); |
| 161 | + const SkPaint child_paint2 = SkPaint(SkColors::kCyan); |
| 162 | + auto layer_filter1 = SkImageFilter::MakeMatrixFilter( |
| 163 | + SkMatrix(), SkFilterQuality::kMedium_SkFilterQuality, nullptr); |
| 164 | + auto layer_filter2 = SkImageFilter::MakeMatrixFilter( |
| 165 | + SkMatrix(), SkFilterQuality::kMedium_SkFilterQuality, nullptr); |
| 166 | + auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1); |
| 167 | + auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2); |
| 168 | + auto layer1 = std::make_shared<ImageFilterLayer>(layer_filter1); |
| 169 | + auto layer2 = std::make_shared<ImageFilterLayer>(layer_filter2); |
| 170 | + layer2->Add(mock_layer2); |
| 171 | + layer1->Add(mock_layer1); |
| 172 | + layer1->Add(layer2); |
| 173 | + |
| 174 | + SkRect children_bounds = child_path1.getBounds(); |
| 175 | + children_bounds.join(child_path2.getBounds()); |
| 176 | + layer1->Preroll(preroll_context(), initial_transform); |
| 177 | + EXPECT_EQ(mock_layer1->paint_bounds(), child_path1.getBounds()); |
| 178 | + EXPECT_EQ(mock_layer2->paint_bounds(), child_path2.getBounds()); |
| 179 | + EXPECT_EQ(layer1->paint_bounds(), children_bounds); |
| 180 | + EXPECT_EQ(layer2->paint_bounds(), mock_layer2->paint_bounds()); |
| 181 | + EXPECT_TRUE(mock_layer1->needs_painting()); |
| 182 | + EXPECT_TRUE(mock_layer2->needs_painting()); |
| 183 | + EXPECT_TRUE(layer1->needs_painting()); |
| 184 | + EXPECT_TRUE(layer2->needs_painting()); |
| 185 | + EXPECT_EQ(mock_layer1->parent_matrix(), initial_transform); |
| 186 | + EXPECT_EQ(mock_layer2->parent_matrix(), initial_transform); |
| 187 | + |
| 188 | + SkPaint filter_paint1, filter_paint2; |
| 189 | + filter_paint1.setImageFilter(layer_filter1); |
| 190 | + filter_paint2.setImageFilter(layer_filter2); |
| 191 | + layer1->Paint(paint_context()); |
| 192 | + EXPECT_EQ(mock_canvas().draw_calls(), |
| 193 | + std::vector({ |
| 194 | + MockCanvas::DrawCall{0, MockCanvas::SaveData{1}}, |
| 195 | + MockCanvas::DrawCall{1, MockCanvas::SetMatrixData{SkMatrix()}}, |
| 196 | + MockCanvas::DrawCall{ |
| 197 | + 1, MockCanvas::SaveLayerData{children_bounds, filter_paint1, |
| 198 | + nullptr, 2}}, |
| 199 | + MockCanvas::DrawCall{ |
| 200 | + 2, MockCanvas::DrawPathData{child_path1, child_paint1}}, |
| 201 | + MockCanvas::DrawCall{2, MockCanvas::SaveData{3}}, |
| 202 | + MockCanvas::DrawCall{3, MockCanvas::SetMatrixData{SkMatrix()}}, |
| 203 | + MockCanvas::DrawCall{ |
| 204 | + 3, MockCanvas::SaveLayerData{child_path2.getBounds(), |
| 205 | + filter_paint2, nullptr, 4}}, |
| 206 | + MockCanvas::DrawCall{ |
| 207 | + 4, MockCanvas::DrawPathData{child_path2, child_paint2}}, |
| 208 | + MockCanvas::DrawCall{4, MockCanvas::RestoreData{3}}, |
| 209 | + MockCanvas::DrawCall{3, MockCanvas::RestoreData{2}}, |
| 210 | + MockCanvas::DrawCall{2, MockCanvas::RestoreData{1}}, |
| 211 | + MockCanvas::DrawCall{1, MockCanvas::RestoreData{0}}, |
| 212 | + })); |
| 213 | +} |
| 214 | + |
| 215 | +TEST_F(ImageFilterLayerTest, Readback) { |
| 216 | + auto layer_filter = SkImageFilter::MakeMatrixFilter( |
| 217 | + SkMatrix(), SkFilterQuality::kMedium_SkFilterQuality, nullptr); |
| 218 | + auto initial_transform = SkMatrix(); |
| 219 | + |
| 220 | + // ImageFilterLayer does not read from surface |
| 221 | + auto layer = std::make_shared<ImageFilterLayer>(layer_filter); |
| 222 | + preroll_context()->surface_needs_readback = false; |
| 223 | + layer->Preroll(preroll_context(), initial_transform); |
| 224 | + EXPECT_FALSE(preroll_context()->surface_needs_readback); |
| 225 | + |
| 226 | + // ImageFilterLayer blocks child with readback |
| 227 | + auto mock_layer = |
| 228 | + std::make_shared<MockLayer>(SkPath(), SkPaint(), false, false, true); |
| 229 | + layer->Add(mock_layer); |
| 230 | + preroll_context()->surface_needs_readback = false; |
| 231 | + layer->Preroll(preroll_context(), initial_transform); |
| 232 | + EXPECT_FALSE(preroll_context()->surface_needs_readback); |
| 233 | +} |
| 234 | + |
| 235 | +} // namespace testing |
| 236 | +} // namespace flutter |
0 commit comments