5#include "flutter/flow/layers/container_layer.h"
7#include "flutter/flow/layers/layer.h"
8#include "flutter/flow/layers/layer_tree.h"
9#include "flutter/flow/testing/diff_context_test.h"
10#include "flutter/flow/testing/layer_test.h"
11#include "flutter/flow/testing/mock_layer.h"
12#include "flutter/fml/macros.h"
13#include "gtest/gtest.h"
26 auto layer = std::make_shared<ContainerLayer>();
28 preroll_context()->has_platform_view =
true;
29 EXPECT_DEATH_IF_SUPPORTED(layer->Preroll(preroll_context()),
30 "!context->has_platform_view");
34 auto layer = std::make_shared<ContainerLayer>();
36 preroll_context()->has_texture_layer =
true;
37 EXPECT_DEATH_IF_SUPPORTED(layer->Preroll(preroll_context()),
38 "!context->has_texture_layer");
42 auto layer = std::make_shared<ContainerLayer>();
44 layer->Preroll(preroll_context());
47 EXPECT_FALSE(layer->needs_painting(paint_context()));
49 EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()),
50 "needs_painting\\(context\\)");
55 child_path.
addRect(5.0f, 6.0f, 20.5f, 21.5f);
56 auto mock_layer = std::make_shared<MockLayer>(child_path);
57 auto layer = std::make_shared<ContainerLayer>();
58 layer->Add(mock_layer);
62 EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()),
63 "needs_painting\\(context\\)");
69 child_path1.
addRect(5.0f, 6.0f, 20.5f, 21.5f);
71 child_path2.
addRect(8.0f, 2.0f, 16.5f, 14.5f);
75 auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
76 mock_layer1->set_fake_has_texture_layer(
true);
77 auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
79 auto root = std::make_shared<ContainerLayer>();
80 auto container_layer1 = std::make_shared<ContainerLayer>();
81 auto container_layer2 = std::make_shared<ContainerLayer>();
82 root->Add(container_layer1);
83 root->Add(container_layer2);
84 container_layer1->Add(mock_layer1);
85 container_layer2->Add(mock_layer2);
87 EXPECT_EQ(preroll_context()->has_texture_layer,
false);
88 root->Preroll(preroll_context());
89 EXPECT_EQ(preroll_context()->has_texture_layer,
true);
91 EXPECT_EQ(mock_layer2->parent_has_texture_layer(),
false);
96 child_path.
addRect(5.0f, 6.0f, 20.5f, 21.5f);
100 auto mock_layer = std::make_shared<MockLayer>(child_path, child_paint);
101 auto layer = std::make_shared<ContainerLayer>();
102 layer->Add(mock_layer);
104 preroll_context()->state_stack.set_preroll_delegate(initial_transform);
105 layer->Preroll(preroll_context());
106 EXPECT_FALSE(preroll_context()->has_platform_view);
107 EXPECT_EQ(mock_layer->paint_bounds(), child_path.
getBounds());
108 EXPECT_EQ(layer->paint_bounds(), child_path.
getBounds());
109 EXPECT_EQ(layer->child_paint_bounds(), layer->paint_bounds());
110 EXPECT_TRUE(mock_layer->needs_painting(paint_context()));
111 EXPECT_TRUE(layer->needs_painting(paint_context()));
112 EXPECT_EQ(mock_layer->parent_matrix(), initial_transform);
113 EXPECT_EQ(mock_layer->parent_cull_rect(),
kGiantRect);
115 layer->Paint(display_list_paint_context());
119 expected_builder.
DrawPath(child_path, child_paint);
127 child_path1.
addRect(5.0f, 6.0f, 20.5f, 21.5f);
129 child_path2.
addRect(8.0f, 2.0f, 16.5f, 14.5f);
134 auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
135 mock_layer1->set_fake_has_platform_view(
true);
136 auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
137 auto layer = std::make_shared<ContainerLayer>();
138 layer->Add(mock_layer1);
139 layer->Add(mock_layer2);
143 preroll_context()->state_stack.set_preroll_delegate(initial_transform);
144 layer->Preroll(preroll_context());
146 EXPECT_EQ(mock_layer1->paint_bounds(), child_path1.
getBounds());
147 EXPECT_EQ(mock_layer2->paint_bounds(), child_path2.
getBounds());
148 EXPECT_EQ(layer->paint_bounds(), expected_total_bounds);
149 EXPECT_EQ(layer->child_paint_bounds(), layer->paint_bounds());
150 EXPECT_TRUE(mock_layer1->needs_painting(paint_context()));
151 EXPECT_TRUE(mock_layer2->needs_painting(paint_context()));
152 EXPECT_TRUE(layer->needs_painting(paint_context()));
153 EXPECT_EQ(mock_layer1->parent_matrix(), initial_transform);
154 EXPECT_EQ(mock_layer2->parent_matrix(), initial_transform);
155 EXPECT_EQ(mock_layer1->parent_cull_rect(),
kGiantRect);
156 EXPECT_EQ(mock_layer2->parent_cull_rect(),
159 layer->Paint(display_list_paint_context());
163 expected_builder.
DrawPath(child_path1, child_paint1);
166 expected_builder.
DrawPath(child_path2, child_paint2);
174 child_path1.
addRect(5.0f, 6.0f, 20.5f, 21.5f);
179 auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
180 auto mock_layer2 = std::make_shared<MockLayer>(
SkPath(), child_paint2);
181 auto layer = std::make_shared<ContainerLayer>();
182 layer->Add(mock_layer1);
183 layer->Add(mock_layer2);
185 preroll_context()->state_stack.set_preroll_delegate(initial_transform);
186 layer->Preroll(preroll_context());
187 EXPECT_FALSE(preroll_context()->has_platform_view);
188 EXPECT_EQ(mock_layer1->paint_bounds(), child_path1.
getBounds());
189 EXPECT_EQ(mock_layer2->paint_bounds(),
SkPath().getBounds());
190 EXPECT_EQ(layer->paint_bounds(), child_path1.
getBounds());
191 EXPECT_EQ(layer->child_paint_bounds(), layer->paint_bounds());
192 EXPECT_TRUE(mock_layer1->needs_painting(paint_context()));
193 EXPECT_FALSE(mock_layer2->needs_painting(paint_context()));
194 EXPECT_TRUE(layer->needs_painting(paint_context()));
195 EXPECT_EQ(mock_layer1->parent_matrix(), initial_transform);
196 EXPECT_EQ(mock_layer2->parent_matrix(), initial_transform);
197 EXPECT_EQ(mock_layer1->parent_cull_rect(),
kGiantRect);
198 EXPECT_EQ(mock_layer2->parent_cull_rect(),
kGiantRect);
200 layer->Paint(display_list_paint_context());
204 expected_builder.
DrawPath(child_path1, child_paint1);
213 child_path1.
addRect(5.0f, 6.0f, 20.5f, 21.5f);
215 child_path2.
addRect(8.0f, 2.0f, 16.5f, 14.5f);
220 auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
221 mock_layer1->set_fake_has_platform_view(
false);
222 auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
223 auto layer = std::make_shared<ContainerLayer>();
224 layer->Add(mock_layer1);
225 layer->Add(mock_layer2);
229 preroll_context()->state_stack.set_preroll_delegate(initial_transform);
230 layer->Preroll(preroll_context());
231 EXPECT_FALSE(preroll_context()->has_platform_view);
232 EXPECT_EQ(mock_layer1->paint_bounds(), child_path1.
getBounds());
233 EXPECT_EQ(mock_layer2->paint_bounds(), child_path2.
getBounds());
234 EXPECT_EQ(layer->paint_bounds(), expected_total_bounds);
235 EXPECT_EQ(layer->child_paint_bounds(), layer->paint_bounds());
236 EXPECT_TRUE(mock_layer1->needs_painting(paint_context()));
237 EXPECT_TRUE(mock_layer2->needs_painting(paint_context()));
238 EXPECT_TRUE(layer->needs_painting(paint_context()));
239 EXPECT_EQ(mock_layer1->parent_matrix(), initial_transform);
240 EXPECT_EQ(mock_layer2->parent_matrix(), initial_transform);
241 EXPECT_EQ(mock_layer1->parent_cull_rect(),
kGiantRect);
242 EXPECT_EQ(mock_layer2->parent_cull_rect(),
kGiantRect);
244 layer->Paint(display_list_paint_context());
248 expected_builder.
DrawPath(child_path1, child_paint1);
251 expected_builder.
DrawPath(child_path2, child_paint2);
265 auto cacheable_container_layer1 =
267 auto cacheable_container_layer2 =
269 auto cacheable_container_layer11 =
272 auto cacheable_layer111 =
273 std::make_shared<MockCacheableLayer>(child_path3,
paint);
276 auto cacheable_layer21 =
277 std::make_shared<MockCacheableLayer>(child_path1,
paint, 2);
291 auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
292 auto mock_layer2 = std::make_shared<MockLayer>(
SkPath(), child_paint2);
293 auto mock_layer3 = std::make_shared<MockLayer>(child_path2,
paint);
295 cacheable_container_layer1->Add(mock_layer1);
296 cacheable_container_layer1->Add(mock_layer3);
298 cacheable_container_layer1->Add(cacheable_container_layer11);
299 cacheable_container_layer11->Add(cacheable_layer111);
301 cacheable_container_layer2->Add(cacheable_layer21);
302 auto layer = std::make_shared<ContainerLayer>();
303 layer->Add(cacheable_container_layer1);
304 layer->Add(mock_layer2);
305 layer->Add(cacheable_container_layer2);
311 layer->Preroll(preroll_context());
313 EXPECT_EQ(mock_layer1->paint_bounds(),
315 EXPECT_EQ(mock_layer3->paint_bounds(),
317 EXPECT_EQ(cacheable_layer111->paint_bounds(),
319 EXPECT_EQ(cacheable_container_layer1->paint_bounds(),
323 EXPECT_EQ(preroll_context()->raster_cached_entries->size(),
324 static_cast<unsigned long>(0));
327 use_mock_raster_cache();
328 preroll_context()->raster_cache->BeginFrame();
329 layer->Preroll(preroll_context());
330 preroll_context()->raster_cache->EvictUnusedCacheEntries();
335 EXPECT_EQ(preroll_context()->raster_cached_entries->size(),
336 static_cast<unsigned long>(5));
339 EXPECT_EQ(cacheable_container_layer1->raster_cache_item()->cache_state(),
340 RasterCacheItem::CacheState::kChildren);
342 cacheable_container_layer1->raster_cache_item()->GetId().value(),
345 EXPECT_EQ(cacheable_container_layer11->raster_cache_item()->cache_state(),
346 RasterCacheItem::CacheState::kChildren);
348 cacheable_container_layer11->raster_cache_item()->GetId().value(),
350 EXPECT_FALSE(raster_cache()->
Draw(
351 cacheable_container_layer11->raster_cache_item()->GetId().value(),
352 cache_canvas, &
paint));
355 EXPECT_EQ(cacheable_layer111->raster_cache_item()->cache_state(),
358 EXPECT_EQ(cacheable_container_layer2->raster_cache_item()->cache_state(),
360 preroll_context()->raster_cache->EndFrame();
367 preroll_context()->raster_cached_entries->clear();
368 preroll_context()->raster_cache->BeginFrame();
369 layer->Preroll(preroll_context());
370 preroll_context()->raster_cache->EvictUnusedCacheEntries();
375 EXPECT_EQ(preroll_context()->raster_cached_entries->size(),
376 static_cast<unsigned long>(5));
377 EXPECT_EQ(cacheable_container_layer1->raster_cache_item()->cache_state(),
378 RasterCacheItem::CacheState::kChildren);
380 cacheable_container_layer1->raster_cache_item()->GetId().value(),
383 EXPECT_EQ(cacheable_container_layer11->raster_cache_item()->cache_state(),
384 RasterCacheItem::CacheState::kChildren);
386 cacheable_container_layer11->raster_cache_item()->GetId().value(),
388 EXPECT_FALSE(raster_cache()->
Draw(
389 cacheable_container_layer11->raster_cache_item()->GetId().value(),
390 cache_canvas, &
paint));
392 EXPECT_EQ(cacheable_container_layer2->raster_cache_item()->cache_state(),
396 EXPECT_EQ(cacheable_layer21->raster_cache_item()->cache_state(),
397 RasterCacheItem::CacheState::kCurrent);
399 cacheable_layer21->raster_cache_item()->GetId().value(),
402 cacheable_layer21->raster_cache_item()->GetId().value(), cache_canvas,
404 preroll_context()->raster_cache->EndFrame();
411 preroll_context()->raster_cache->BeginFrame();
412 preroll_context()->raster_cached_entries->clear();
413 layer->Preroll(preroll_context());
414 preroll_context()->raster_cache->EvictUnusedCacheEntries();
418 EXPECT_EQ(preroll_context()->raster_cached_entries->size(),
419 static_cast<unsigned long>(5));
420 EXPECT_EQ(cacheable_container_layer1->raster_cache_item()->cache_state(),
421 RasterCacheItem::CacheState::kCurrent);
423 cacheable_container_layer1->raster_cache_item()->GetId().value(),
426 cacheable_container_layer11->raster_cache_item()->GetId().value(),
428 EXPECT_FALSE(raster_cache()->
Draw(
429 cacheable_container_layer11->raster_cache_item()->GetId().value(),
430 cache_canvas, &
paint));
434 cacheable_layer111->raster_cache_item()->GetId().value(),
436 EXPECT_FALSE(raster_cache()->
Draw(
437 cacheable_layer111->raster_cache_item()->GetId().value(), cache_canvas,
441 EXPECT_EQ(cacheable_container_layer2->raster_cache_item()->cache_state(),
442 RasterCacheItem::CacheState::kCurrent);
445 cacheable_layer21->raster_cache_item()->GetId().value(),
447 preroll_context()->raster_cache->EndFrame();
451 preroll_context()->raster_cache->BeginFrame();
453 preroll_context()->raster_cached_entries->clear();
454 layer->Preroll(preroll_context());
455 preroll_context()->raster_cache->EvictUnusedCacheEntries();
458 preroll_context()->raster_cache->EndFrame();
461 preroll_context()->raster_cache->BeginFrame();
462 preroll_context()->raster_cached_entries->clear();
463 layer->Preroll(preroll_context());
466 preroll_context()->raster_cache->EndFrame();
469 preroll_context()->raster_cache->BeginFrame();
470 preroll_context()->raster_cached_entries->clear();
471 layer->Preroll(preroll_context());
474 preroll_context()->raster_cache->EndFrame();
481 auto container1 = std::make_shared<ContainerLayer>();
482 container1->Add(mock1);
485 PrerollContext* context = preroll_context();
486 container1->Preroll(context);
487 EXPECT_EQ(context->renderable_state_flags,
492 container1->Add(mock2);
496 container1->Preroll(context);
497 EXPECT_EQ(context->renderable_state_flags,
502 container1->Add(mock3);
506 container1->Preroll(context);
507 EXPECT_EQ(context->renderable_state_flags, 0);
509 auto container2 = std::make_shared<ContainerLayer>();
510 container2->Add(mock1);
511 container2->Add(mock2);
514 container2->Preroll(context);
515 EXPECT_EQ(context->renderable_state_flags,
520 container2->Add(mock4);
524 container2->Preroll(context);
525 EXPECT_EQ(context->renderable_state_flags, 0);
530 child_path.
addRect(5.0f, 6.0f, 20.5f, 21.5f);
534 auto mock_layer1 = std::make_shared<MockLayer>(
SkPath(), child_paint);
535 auto mock_cacheable_container_layer1 =
536 std::make_shared<MockCacheableContainerLayer>();
537 auto mock_container_layer = std::make_shared<ContainerLayer>();
538 auto mock_cacheable_layer =
539 std::make_shared<MockCacheableLayer>(child_path, child_paint);
540 mock_cacheable_container_layer1->Add(mock_cacheable_layer);
546 auto layer = std::make_shared<ContainerLayer>();
547 layer->Add(mock_cacheable_container_layer1);
548 layer->Add(mock_layer1);
550 preroll_context()->state_stack.set_preroll_delegate(initial_transform);
551 layer->Preroll(preroll_context());
553 ASSERT_EQ(preroll_context()->raster_cached_entries->size(),
554 static_cast<const unsigned long>(0));
556 use_mock_raster_cache();
558 layer->Preroll(preroll_context());
559 ASSERT_EQ(preroll_context()->raster_cached_entries->size(),
560 static_cast<const unsigned long>(2));
573 auto t1_c1 = CreateContainerLayer(CreateDisplayListLayer(pic1));
576 auto t1_c2 = CreateContainerLayer(CreateDisplayListLayer(pic2));
585 auto t2_c1 = CreateContainerLayer(CreateDisplayListLayer(pic1));
586 t2_c1->AssignOldLayer(t1_c1.get());
589 t2.
root()->
Add(CreateDisplayListLayer(pic3));
591 auto t2_c2 = CreateContainerLayer(CreateDisplayListLayer(pic2));
592 t2_c2->AssignOldLayer(t1_c2.get());
595 damage = DiffLayerTree(t2, t1);
601 t2.
root()->
Add(CreateDisplayListLayer(pic3));
604 damage = DiffLayerTree(t2, t1);
612 t2.
root()->
Add(CreateDisplayListLayer(pic3));
613 damage = DiffLayerTree(t2, t1);
624 t1.
root()->
Add(CreateDisplayListLayer(pic1));
625 t1.
root()->
Add(CreateDisplayListLayer(pic2));
631 t2.
root()->
Add(CreateDisplayListLayer(pic3));
632 t2.
root()->
Add(CreateDisplayListLayer(pic1));
633 t2.
root()->
Add(CreateDisplayListLayer(pic2));
635 damage = DiffLayerTree(t2, t1);
639 t3.
root()->
Add(CreateDisplayListLayer(pic1));
640 t3.
root()->
Add(CreateDisplayListLayer(pic3));
641 t3.
root()->
Add(CreateDisplayListLayer(pic2));
643 damage = DiffLayerTree(t3, t1);
647 t4.
root()->
Add(CreateDisplayListLayer(pic1));
648 t4.
root()->
Add(CreateDisplayListLayer(pic2));
649 t4.
root()->
Add(CreateDisplayListLayer(pic3));
651 damage = DiffLayerTree(t4, t1);
660 auto c1 = CreateContainerLayer(std::make_shared<MockLayer>(
path1));
661 auto c2 = CreateContainerLayer(std::make_shared<MockLayer>(
path2));
662 auto c3 = CreateContainerLayer(std::make_shared<MockLayer>(
path3));
676 damage = DiffLayerTree(t2, t1);
683 damage = DiffLayerTree(t3, t1);
690 damage = DiffLayerTree(t4, t1);
696 damage = DiffLayerTree(t5, t1);
702 damage = DiffLayerTree(t6, t1);
708 damage = DiffLayerTree(t7, t1);
721 auto c1 = CreateContainerLayer(std::make_shared<MockLayer>(
path1));
722 auto c2 = CreateContainerLayer(std::make_shared<MockLayer>(
path2));
723 auto c3 = CreateContainerLayer(std::make_shared<MockLayer>(
path3));
738 damage = DiffLayerTree(t2, t1);
742 t3.
root()->
Add(CreateContainerLayer({std::make_shared<MockLayer>(path1a)}));
746 damage = DiffLayerTree(t3, t1);
751 t4.
root()->
Add(CreateContainerLayer(std::make_shared<MockLayer>(path2a)));
754 damage = DiffLayerTree(t4, t1);
760 t5.
root()->
Add(CreateContainerLayer(std::make_shared<MockLayer>(path3a)));
762 damage = DiffLayerTree(t5, t1);
static SkMatrix Translate(SkScalar dx, SkScalar dy)
static const SkMatrix & I()
const SkRect & getBounds() const
SkPath & addRect(const SkRect &rect, SkPathDirection dir, unsigned start)
virtual void Add(std::shared_ptr< Layer > layer)
void TransformReset() override
void DrawPath(const SkPath &path, const DlPaint &paint) override
sk_sp< DisplayList > Build()
static constexpr int kCallerCanApplyOpacity
static void TryToRasterCache(const std::vector< RasterCacheItem * > &raster_cached_entries, const PaintContext *paint_context, bool ignore_raster_cache=false)
static std::shared_ptr< MockCacheableContainerLayer > CacheLayerOnly()
static std::shared_ptr< MockCacheableContainerLayer > CacheLayerOrChildren()
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
bool DisplayListsEQ_Verbose(const DisplayList *a, const DisplayList *b)
LayerTest ContainerLayerTest
static constexpr SkRect kGiantRect
static constexpr SkIRect MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b)
static constexpr SkRect MakeEmpty()
void join(const SkRect &r)
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
static constexpr DlColor kMidGrey()
static constexpr DlColor kGreen()
#define EXPECT_TRUE(handle)