5#include "flutter/flow/layers/shader_mask_layer.h"
7#include "flutter/flow/layers/layer_tree.h"
8#include "flutter/flow/layers/opacity_layer.h"
9#include "flutter/flow/raster_cache.h"
10#include "flutter/flow/raster_cache_util.h"
11#include "flutter/flow/testing/layer_test.h"
12#include "flutter/flow/testing/mock_layer.h"
13#include "flutter/fml/macros.h"
14#include "gtest/gtest.h"
26 color.withAlpha(0x7f),
42 layer->Preroll(preroll_context());
44 EXPECT_EQ(layer->child_paint_bounds(),
kEmptyRect);
45 EXPECT_FALSE(layer->needs_painting(paint_context()));
47 EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()),
48 "needs_painting\\(context\\)");
54 auto mock_layer = std::make_shared<MockLayer>(child_path);
57 layer->Add(mock_layer);
60 EXPECT_EQ(layer->child_paint_bounds(),
kEmptyRect);
61 EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()),
62 "needs_painting\\(context\\)");
72 auto mock_layer = std::make_shared<MockLayer>(child_path, child_paint);
73 auto layer = std::make_shared<ShaderMaskLayer>(
nullptr, layer_bounds,
75 layer->Add(mock_layer);
77 preroll_context()->state_stack.set_preroll_delegate(initial_transform);
78 layer->Preroll(preroll_context());
79 EXPECT_EQ(mock_layer->paint_bounds(), child_bounds);
80 EXPECT_EQ(layer->paint_bounds(), child_bounds);
81 EXPECT_EQ(layer->child_paint_bounds(), child_bounds);
82 EXPECT_TRUE(mock_layer->needs_painting(paint_context()));
83 EXPECT_TRUE(layer->needs_painting(paint_context()));
84 EXPECT_EQ(mock_layer->parent_matrix(), initial_transform);
90 layer->Paint(display_list_paint_context());
91 DisplayListBuilder expected_builder;
93 expected_builder.Save();
95 expected_builder.SaveLayer(&child_bounds);
98 expected_builder.DrawPath(child_path, child_paint);
100 expected_builder.Translate(layer_bounds.
fLeft, layer_bounds.
fTop);
101 expected_builder.DrawRect(
105 expected_builder.Restore();
107 expected_builder.Restore();
119 auto mock_layer = std::make_shared<MockLayer>(child_path, child_paint);
120 auto layer = std::make_shared<ShaderMaskLayer>(dl_filter, layer_bounds,
122 layer->Add(mock_layer);
124 preroll_context()->state_stack.set_preroll_delegate(initial_transform);
125 layer->Preroll(preroll_context());
126 EXPECT_EQ(layer->paint_bounds(), child_bounds);
127 EXPECT_EQ(layer->child_paint_bounds(), child_bounds);
128 EXPECT_TRUE(layer->needs_painting(paint_context()));
129 EXPECT_EQ(mock_layer->parent_matrix(), initial_transform);
135 layer->Paint(display_list_paint_context());
136 DisplayListBuilder expected_builder;
138 expected_builder.Save();
140 expected_builder.SaveLayer(&child_bounds);
143 expected_builder.DrawPath(child_path, child_paint);
145 expected_builder.Translate(layer_bounds.
fLeft, layer_bounds.
fTop);
146 expected_builder.DrawRect(
150 expected_builder.Restore();
152 expected_builder.Restore();
162 const SkPath child_path2 =
167 auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
168 auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
169 auto layer = std::make_shared<ShaderMaskLayer>(dl_filter, layer_bounds,
171 layer->Add(mock_layer1);
172 layer->Add(mock_layer2);
176 preroll_context()->state_stack.set_preroll_delegate(initial_transform);
177 layer->Preroll(preroll_context());
178 EXPECT_EQ(mock_layer1->paint_bounds(), child_path1.
getBounds());
179 EXPECT_EQ(mock_layer2->paint_bounds(), child_path2.
getBounds());
180 EXPECT_EQ(layer->paint_bounds(), children_bounds);
181 EXPECT_EQ(layer->child_paint_bounds(), children_bounds);
182 EXPECT_TRUE(mock_layer1->needs_painting(paint_context()));
183 EXPECT_TRUE(mock_layer2->needs_painting(paint_context()));
184 EXPECT_TRUE(layer->needs_painting(paint_context()));
185 EXPECT_EQ(mock_layer1->parent_matrix(), initial_transform);
186 EXPECT_EQ(mock_layer2->parent_matrix(), initial_transform);
192 layer->Paint(display_list_paint_context());
193 DisplayListBuilder expected_builder;
195 expected_builder.Save();
197 expected_builder.SaveLayer(&children_bounds);
200 expected_builder.DrawPath(child_path1, child_paint1);
203 expected_builder.DrawPath(child_path2, child_paint2);
205 expected_builder.Translate(layer_bounds.
fLeft, layer_bounds.
fTop);
206 expected_builder.DrawRect(
210 expected_builder.Restore();
212 expected_builder.Restore();
222 const SkPath child_path2 =
228 auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
229 auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
230 auto layer1 = std::make_shared<ShaderMaskLayer>(dl_filter1, layer_bounds,
232 auto layer2 = std::make_shared<ShaderMaskLayer>(dl_filter2, layer_bounds,
234 layer2->Add(mock_layer2);
235 layer1->Add(mock_layer1);
240 preroll_context()->state_stack.set_preroll_delegate(initial_transform);
241 layer1->Preroll(preroll_context());
242 EXPECT_EQ(mock_layer1->paint_bounds(), child_path1.
getBounds());
243 EXPECT_EQ(mock_layer2->paint_bounds(), child_path2.
getBounds());
244 EXPECT_EQ(layer1->paint_bounds(), children_bounds);
245 EXPECT_EQ(layer1->child_paint_bounds(), children_bounds);
246 EXPECT_EQ(layer2->paint_bounds(), mock_layer2->paint_bounds());
247 EXPECT_EQ(layer2->child_paint_bounds(), mock_layer2->paint_bounds());
248 EXPECT_TRUE(mock_layer1->needs_painting(paint_context()));
249 EXPECT_TRUE(mock_layer2->needs_painting(paint_context()));
250 EXPECT_TRUE(layer1->needs_painting(paint_context()));
251 EXPECT_TRUE(layer2->needs_painting(paint_context()));
252 EXPECT_EQ(mock_layer1->parent_matrix(), initial_transform);
253 EXPECT_EQ(mock_layer2->parent_matrix(), initial_transform);
255 DlPaint filter_paint1, filter_paint2;
261 layer1->Paint(display_list_paint_context());
262 DisplayListBuilder expected_builder;
264 expected_builder.Save();
266 expected_builder.SaveLayer(&children_bounds);
269 expected_builder.DrawPath(child_path1, child_paint1);
272 expected_builder.Save();
274 expected_builder.SaveLayer(&child_path2.
getBounds());
277 expected_builder.DrawPath(child_path2, child_paint2);
279 expected_builder.Translate(layer_bounds.
fLeft, layer_bounds.
fTop);
280 expected_builder.DrawRect(
284 expected_builder.Restore();
286 expected_builder.Restore();
288 expected_builder.Translate(layer_bounds.
fLeft, layer_bounds.
fTop);
289 expected_builder.DrawRect(
293 expected_builder.Restore();
295 expected_builder.Restore();
303 auto layer = std::make_shared<ShaderMaskLayer>(dl_filter, layer_bounds,
307 preroll_context()->surface_needs_readback =
false;
308 layer->Preroll(preroll_context());
309 EXPECT_FALSE(preroll_context()->surface_needs_readback);
312 auto mock_layer = std::make_shared<MockLayer>(
SkPath(),
DlPaint());
313 mock_layer->set_fake_reads_surface(
true);
314 layer->Add(mock_layer);
315 preroll_context()->surface_needs_readback =
false;
316 layer->Preroll(preroll_context());
317 EXPECT_FALSE(preroll_context()->surface_needs_readback);
326 auto mock_layer = std::make_shared<MockLayer>(child_path);
327 auto layer = std::make_shared<ShaderMaskLayer>(dl_filter, layer_bounds,
329 layer->Add(mock_layer);
331 SkMatrix cache_ctm = initial_transform;
332 DisplayListBuilder cache_canvas;
333 cache_canvas.Transform(cache_ctm);
335 use_mock_raster_cache();
336 preroll_context()->state_stack.set_preroll_delegate(initial_transform);
337 const auto* cacheable_shader_masker_item = layer->raster_cache_item();
339 EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (
size_t)0);
340 EXPECT_EQ(cacheable_shader_masker_item->cache_state(),
342 EXPECT_FALSE(cacheable_shader_masker_item->GetId().has_value());
345 layer->Preroll(preroll_context());
348 EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (
size_t)0);
349 EXPECT_EQ(cacheable_shader_masker_item->cache_state(),
351 EXPECT_FALSE(cacheable_shader_masker_item->GetId().has_value());
354 layer->Preroll(preroll_context());
356 EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (
size_t)0);
357 EXPECT_EQ(cacheable_shader_masker_item->cache_state(),
359 EXPECT_FALSE(cacheable_shader_masker_item->GetId().has_value());
362 layer->Preroll(preroll_context());
364 EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (
size_t)1);
365 EXPECT_EQ(cacheable_shader_masker_item->cache_state(),
366 RasterCacheItem::CacheState::kCurrent);
369 cacheable_shader_masker_item->GetId().value(), cache_canvas, &
paint));
377 auto shader_mask_layer =
379 shader_mask_layer->Add(mock_layer);
382 PrerollContext* context = preroll_context();
383 shader_mask_layer->Preroll(context);
386 int opacity_alpha = 0x7F;
388 auto opacity_layer = std::make_shared<OpacityLayer>(opacity_alpha,
offset);
389 opacity_layer->Add(shader_mask_layer);
390 opacity_layer->Preroll(context);
391 EXPECT_TRUE(opacity_layer->children_can_accept_opacity());
393 DisplayListBuilder expected_builder;
395 expected_builder.Save();
400 expected_builder.SaveLayer(&child_path.
getBounds(), &sl_paint);
403 expected_builder.DrawPath(child_path,
DlPaint());
405 expected_builder.Translate(mask_rect.
fLeft, mask_rect.
fTop);
406 expected_builder.DrawRect(
410 expected_builder.Restore();
413 expected_builder.Restore();
416 opacity_layer->Paint(display_list_paint_context());
421 use_mock_raster_cache();
429 auto mock_layer = std::make_shared<MockLayer>(child_path, child_paint);
430 auto layer = std::make_shared<ShaderMaskLayer>(dl_filter, layer_bounds,
432 layer->Add(mock_layer);
434 preroll_context()->state_stack.set_preroll_delegate(initial_transform);
435 layer->Preroll(preroll_context());
441 layer->Paint(display_list_paint_context());
444 expected_builder.
Save();
450 expected_builder.
SaveLayer(&child_bounds);
453 expected_builder.
DrawPath(child_path, child_paint);
static SkMatrix Translate(SkScalar dx, SkScalar dy)
const SkRect & getBounds() const
SkPath & addRect(const SkRect &rect, SkPathDirection dir, unsigned start)
void DrawRect(const SkRect &rect, const DlPaint &paint) override
void Translate(SkScalar tx, SkScalar ty) override
void DrawPath(const SkPath &path, const DlPaint &paint) override
void SaveLayer(const SkRect *bounds, const DlPaint *paint=nullptr, const DlImageFilter *backdrop=nullptr) override
sk_sp< DisplayList > Build()
static std::shared_ptr< DlLinearGradientColorSource > MakeLinear(const SkPoint start_point, const SkPoint end_point, uint32_t stop_count, const DlColor *colors, const float *stops, DlTileMode tile_mode, const SkMatrix *matrix=nullptr)
DlPaint & setBlendMode(DlBlendMode mode)
DlPaint & setColorSource(std::shared_ptr< const DlColorSource > source)
static void TryToRasterCache(const std::vector< RasterCacheItem * > &raster_cached_entries, const PaintContext *paint_context, bool ignore_raster_cache=false)
static constexpr int kSaveLayerRenderFlags
static std::shared_ptr< MockLayer > Make(const SkPath &path, DlPaint paint=DlPaint())
static void Draw(SkCanvas *canvas, const SkRect &rect)
PODArray< SkColor > colors
TEST_F(DisplayListTest, Defaults)
LayerTestBase<::testing::Test > LayerTest
static std::shared_ptr< DlColorSource > MakeFilter(DlColor color)
LayerTest ShaderMaskLayerTest
bool DisplayListsEQ_Verbose(const DisplayList *a, const DisplayList *b)
static constexpr SkRect kEmptyRect
static constexpr SkPoint Make(float x, float y)
constexpr SkRect makeOffset(float dx, float dy) const
SkScalar fLeft
smaller x-axis bounds
constexpr float height() const
constexpr float width() const
void join(const SkRect &r)
static constexpr SkRect MakeWH(float w, float h)
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
SkScalar fTop
smaller y-axis bounds
static constexpr DlColor kMagenta()
static constexpr DlColor kBlue()
static constexpr DlColor kYellow()
static constexpr DlColor kGreen()
static constexpr DlColor kCyan()
#define EXPECT_TRUE(handle)