5#include "flutter/flow/layers/clip_rect_layer.h"
7#include "flutter/flow/layers/layer_tree.h"
8#include "flutter/flow/layers/opacity_layer.h"
9#include "flutter/flow/layers/platform_view_layer.h"
10#include "flutter/flow/testing/layer_test.h"
11#include "flutter/flow/testing/mock_embedder.h"
12#include "flutter/flow/testing/mock_layer.h"
13#include "flutter/fml/macros.h"
27 EXPECT_DEATH_IF_SUPPORTED(
29 "clip_behavior != Clip::kNone");
35 layer->Preroll(preroll_context());
38 EXPECT_EQ(preroll_context()->state_stack.device_cull_rect(),
kGiantRect);
39 EXPECT_TRUE(preroll_context()->state_stack.is_empty());
42 EXPECT_EQ(layer->child_paint_bounds(),
kEmptyRect);
43 EXPECT_FALSE(layer->needs_painting(paint_context()));
45 EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()),
46 "needs_painting\\(context\\)");
51 auto layer = std::make_shared<ClipRectLayer>(layer_bounds,
Clip::kHardEdge);
53 EXPECT_EQ(layer->child_paint_bounds(),
kEmptyRect);
54 EXPECT_FALSE(layer->needs_painting(paint_context()));
56 EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()),
57 "needs_painting\\(context\\)");
66 auto mock_layer = std::make_shared<MockLayer>(child_path);
67 auto layer = std::make_shared<ClipRectLayer>(layer_bounds,
Clip::kHardEdge);
68 layer->Add(mock_layer);
71 preroll_context()->state_stack.set_preroll_delegate(distant_bounds,
73 layer->Preroll(preroll_context());
76 EXPECT_EQ(preroll_context()->state_stack.device_cull_rect(), distant_bounds);
77 EXPECT_TRUE(preroll_context()->state_stack.is_empty());
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_cull_rect(),
kEmptyRect);
85 EXPECT_EQ(mock_layer->parent_matrix(), initial_matrix);
86 EXPECT_EQ(mock_layer->parent_mutators(),
87 std::vector({Mutator(layer_bounds)}));
89 auto mutator = paint_context().state_stack.save();
90 mutator.clipRect(distant_bounds,
false);
91 EXPECT_FALSE(mock_layer->needs_painting(paint_context()));
92 EXPECT_FALSE(layer->needs_painting(paint_context()));
93 EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()),
94 "needs_painting\\(context\\)");
101 const SkRect device_cull_bounds = initial_matrix.
mapRect(local_cull_bounds);
106 auto mock_layer = std::make_shared<MockLayer>(child_path, child_paint);
107 auto layer = std::make_shared<ClipRectLayer>(clip_rect,
Clip::kHardEdge);
108 layer->Add(mock_layer);
110 SkRect clip_cull_rect = local_cull_bounds;
111 ASSERT_TRUE(clip_cull_rect.
intersect(clip_rect));
112 SkRect clip_layer_bounds = child_bounds;
113 ASSERT_TRUE(clip_layer_bounds.
intersect(clip_rect));
116 preroll_context()->state_stack.set_preroll_delegate(device_cull_bounds,
118 paint_context().canvas->ClipRect(device_cull_bounds);
119 paint_context().canvas->Transform(initial_matrix);
121 layer->Preroll(preroll_context());
123 EXPECT_EQ(preroll_context()->state_stack.device_cull_rect(),
125 EXPECT_EQ(preroll_context()->state_stack.local_cull_rect(),
127 EXPECT_TRUE(preroll_context()->state_stack.is_empty());
129 EXPECT_EQ(mock_layer->paint_bounds(), child_bounds);
130 EXPECT_EQ(layer->paint_bounds(), clip_layer_bounds);
131 EXPECT_EQ(layer->child_paint_bounds(), child_bounds);
132 EXPECT_EQ(mock_layer->parent_cull_rect(), clip_cull_rect);
133 EXPECT_EQ(mock_layer->parent_matrix(), initial_matrix);
134 EXPECT_EQ(mock_layer->parent_mutators(), std::vector({Mutator(clip_rect)}));
136 EXPECT_FALSE(layer->needs_painting(paint_context()));
137 EXPECT_FALSE(mock_layer->needs_painting(paint_context()));
148 auto mock_layer = std::make_shared<MockLayer>(child_path, child_paint);
149 auto layer = std::make_shared<ClipRectLayer>(layer_bounds,
Clip::kHardEdge);
150 layer->Add(mock_layer);
152 preroll_context()->state_stack.set_preroll_delegate(initial_matrix);
153 layer->Preroll(preroll_context());
156 EXPECT_EQ(preroll_context()->state_stack.device_cull_rect(),
kGiantRect);
157 EXPECT_TRUE(preroll_context()->state_stack.is_empty());
159 EXPECT_EQ(mock_layer->paint_bounds(), child_bounds);
160 EXPECT_EQ(layer->paint_bounds(), mock_layer->paint_bounds());
161 EXPECT_EQ(layer->child_paint_bounds(), child_bounds);
162 EXPECT_TRUE(mock_layer->needs_painting(paint_context()));
163 EXPECT_TRUE(layer->needs_painting(paint_context()));
164 EXPECT_EQ(mock_layer->parent_cull_rect(), layer_bounds);
165 EXPECT_EQ(mock_layer->parent_matrix(), initial_matrix);
166 EXPECT_EQ(mock_layer->parent_mutators(),
167 std::vector({Mutator(layer_bounds)}));
169 layer->Paint(display_list_paint_context());
170 DisplayListBuilder expected_builder;
172 expected_builder.Save();
174 expected_builder.ClipRect(layer_bounds);
176 expected_builder.DrawPath(child_path, child_paint);
179 expected_builder.Restore();
187 const SkRect device_cull_bounds = initial_matrix.
mapRect(local_cull_bounds);
192 auto mock_layer = std::make_shared<MockLayer>(child_path, child_paint);
193 auto layer = std::make_shared<ClipRectLayer>(clip_rect,
Clip::kHardEdge);
194 layer->Add(mock_layer);
196 SkRect clip_cull_rect = clip_rect;
197 ASSERT_TRUE(clip_cull_rect.
intersect(local_cull_bounds));
198 SkRect clip_layer_bounds = clip_rect;
199 ASSERT_TRUE(clip_layer_bounds.
intersect(child_bounds));
202 preroll_context()->state_stack.set_preroll_delegate(device_cull_bounds,
204 layer->Preroll(preroll_context());
207 EXPECT_EQ(preroll_context()->state_stack.device_cull_rect(),
209 EXPECT_TRUE(preroll_context()->state_stack.is_empty());
211 EXPECT_EQ(mock_layer->paint_bounds(), child_bounds);
212 EXPECT_EQ(layer->paint_bounds(), clip_layer_bounds);
213 EXPECT_EQ(layer->child_paint_bounds(), child_bounds);
214 EXPECT_TRUE(mock_layer->needs_painting(paint_context()));
215 EXPECT_TRUE(layer->needs_painting(paint_context()));
216 EXPECT_EQ(mock_layer->parent_cull_rect(), clip_cull_rect);
217 EXPECT_EQ(mock_layer->parent_matrix(), initial_matrix);
218 EXPECT_EQ(mock_layer->parent_mutators(), std::vector({Mutator(clip_rect)}));
220 layer->Paint(display_list_paint_context());
221 DisplayListBuilder expected_builder;
223 expected_builder.Save();
225 expected_builder.ClipRect(clip_rect);
227 expected_builder.DrawPath(child_path, child_paint);
230 expected_builder.Restore();
237 const std::shared_ptr<Layer>& child,
240 auto layer = std::make_shared<ClipRectLayer>(layer_bounds, clip_behavior);
241 if (child !=
nullptr) {
245 layer->Preroll(context);
258 std::shared_ptr<MockLayer> nochild;
259 auto reader = std::make_shared<MockLayer>(
path,
paint);
260 reader->set_fake_reads_surface(
true);
261 auto nonreader = std::make_shared<MockLayer>(
path,
paint);
266 EXPECT_FALSE(
ReadbackResult(context, save_layer, nochild,
false));
276 EXPECT_FALSE(
ReadbackResult(context, save_layer, nonreader,
false));
298 auto clip_rect_layer =
300 clip_rect_layer->Add(mock1);
303 PrerollContext* context = preroll_context();
304 clip_rect_layer->Preroll(context);
305 EXPECT_EQ(context->renderable_state_flags,
310 clip_rect_layer->Add(mock2);
314 clip_rect_layer->Preroll(context);
315 EXPECT_EQ(context->renderable_state_flags,
320 clip_rect_layer->Add(mock3);
324 clip_rect_layer->Preroll(context);
325 EXPECT_EQ(context->renderable_state_flags, 0);
329 auto clip_path_savelayer = std::make_shared<ClipRectLayer>(
331 clip_path_savelayer->Add(mock1);
332 clip_path_savelayer->Add(mock2);
335 clip_path_savelayer->Preroll(context);
339 clip_path_savelayer->Add(mock3);
340 clip_path_savelayer->Preroll(context);
350 auto clip_rect_bad_child =
352 clip_rect_bad_child->Add(mock1);
353 clip_rect_bad_child->Add(mock2);
356 clip_rect_bad_child->Preroll(context);
357 EXPECT_EQ(context->renderable_state_flags,
360 clip_rect_bad_child->Add(mock4);
364 clip_rect_bad_child->Preroll(context);
365 EXPECT_EQ(context->renderable_state_flags, 0);
370 auto clip_path_savelayer_bad_child = std::make_shared<ClipRectLayer>(
372 clip_path_savelayer_bad_child->Add(mock1);
373 clip_path_savelayer_bad_child->Add(mock2);
376 clip_path_savelayer_bad_child->Preroll(context);
380 clip_path_savelayer_bad_child->Add(mock4);
381 clip_path_savelayer_bad_child->Preroll(context);
392 auto clip_rect_layer =
394 clip_rect_layer->Add(mock1);
395 clip_rect_layer->Add(mock2);
399 PrerollContext* context = preroll_context();
400 clip_rect_layer->Preroll(context);
401 EXPECT_EQ(context->renderable_state_flags,
404 int opacity_alpha = 0x7F;
406 auto opacity_layer = std::make_shared<OpacityLayer>(opacity_alpha,
offset);
407 opacity_layer->Add(clip_rect_layer);
408 opacity_layer->Preroll(context);
409 EXPECT_TRUE(opacity_layer->children_can_accept_opacity());
411 DisplayListBuilder expected_builder;
413 expected_builder.Save();
417 expected_builder.Save();
420 expected_builder.DrawPath(
path1,
DlPaint().setAlpha(opacity_alpha));
423 expected_builder.DrawPath(
path2,
DlPaint().setAlpha(opacity_alpha));
425 expected_builder.Restore();
428 expected_builder.Restore();
431 opacity_layer->Paint(display_list_paint_context());
443 auto clip_rect_layer =
445 clip_rect_layer->Add(mock1);
446 clip_rect_layer->Add(mock2);
450 PrerollContext* context = preroll_context();
451 clip_rect_layer->Preroll(context);
454 int opacity_alpha = 0x7F;
456 auto opacity_layer = std::make_shared<OpacityLayer>(opacity_alpha,
offset);
457 opacity_layer->Add(clip_rect_layer);
458 opacity_layer->Preroll(context);
459 EXPECT_TRUE(opacity_layer->children_can_accept_opacity());
461 DisplayListBuilder expected_builder;
463 expected_builder.Save();
467 expected_builder.Save();
469 expected_builder.SaveLayer(&children_bounds,
470 &
DlPaint().setAlpha(opacity_alpha));
477 expected_builder.Restore();
480 expected_builder.Restore();
483 opacity_layer->Paint(display_list_paint_context());
496 SkMatrix cache_ctm = initial_transform;
497 DisplayListBuilder cache_canvas;
498 cache_canvas.Transform(cache_ctm);
500 use_mock_raster_cache();
501 preroll_context()->state_stack.set_preroll_delegate(initial_transform);
503 const auto* clip_cache_item = layer->raster_cache_item();
505 layer->Preroll(preroll_context());
508 EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (
size_t)0);
511 layer->Preroll(preroll_context());
513 EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (
size_t)0);
516 layer->Preroll(preroll_context());
518 EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (
size_t)1);
519 EXPECT_EQ(clip_cache_item->cache_state(),
520 RasterCacheItem::CacheState::kCurrent);
523 cache_canvas, &
paint));
529 const int64_t view_id = 42;
531 std::make_shared<PlatformViewLayer>(view_offset, view_size, view_id);
536 auto embedder = MockViewEmbedder();
537 DisplayListBuilder fake_overlay_builder;
538 embedder.AddCanvas(&fake_overlay_builder);
539 preroll_context()->view_embedder = &embedder;
540 paint_context().view_embedder = &embedder;
542 clip->Preroll(preroll_context());
543 EXPECT_EQ(embedder.prerolled_views(), std::vector<int64_t>({view_id}));
545 clip->Paint(paint_context());
546 EXPECT_EQ(embedder.painted_views(), std::vector<int64_t>({view_id}));
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
static SkMatrix Translate(SkScalar dx, SkScalar dy)
bool mapRect(SkRect *dst, const SkRect &src, SkApplyPerspectiveClip pc=SkApplyPerspectiveClip::kYes) const
const SkRect & getBounds() const
SkPath & addRect(const SkRect &rect, SkPathDirection dir, unsigned start)
static constexpr int kCallerCanApplyOpacity
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 > MakeOpacityCompatible(const SkPath &path)
static std::shared_ptr< MockLayer > Make(const SkPath &path, DlPaint paint=DlPaint())
static void Draw(SkCanvas *canvas, const SkRect &rect)
TEST_F(DisplayListTest, Defaults)
LayerTestBase<::testing::Test > LayerTest
LayerTest ClipRectLayerTest
bool DisplayListsEQ_Verbose(const DisplayList *a, const DisplayList *b)
static constexpr SkRect kEmptyRect
static bool ReadbackResult(PrerollContext *context, Clip clip_behavior, const std::shared_ptr< Layer > &child, bool before)
@ kAntiAliasWithSaveLayer
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
static constexpr SkRect kGiantRect
static constexpr SkPoint Make(float x, float y)
bool intersect(const SkRect &r)
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
void join(const SkRect &r)
static constexpr SkRect MakeWH(float w, float h)
static constexpr SkSize Make(SkScalar w, SkScalar h)
static constexpr DlColor kYellow()
bool surface_needs_readback
#define EXPECT_TRUE(handle)