forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathraster_cache_unittests.cc
888 lines (718 loc) · 34.3 KB
/
raster_cache_unittests.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/display_list/display_list.h"
#include "flutter/display_list/display_list_builder.h"
#include "flutter/display_list/testing/dl_test_snippets.h"
#include "flutter/flow/layers/container_layer.h"
#include "flutter/flow/layers/display_list_layer.h"
#include "flutter/flow/layers/image_filter_layer.h"
#include "flutter/flow/layers/layer_tree.h"
#include "flutter/flow/layers/transform_layer.h"
#include "flutter/flow/raster_cache.h"
#include "flutter/flow/raster_cache_item.h"
#include "flutter/flow/testing/mock_raster_cache.h"
#include "flutter/flow/testing/skia_gpu_object_layer_test.h"
#include "flutter/testing/assertions_skia.h"
#include "gtest/gtest.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPoint.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
namespace flutter {
namespace testing {
TEST(RasterCache, SimpleInitialization) {
flutter::RasterCache cache;
ASSERT_TRUE(true);
}
TEST(RasterCache, MetricsOmitUnpopulatedEntries) {
size_t threshold = 2;
flutter::RasterCache cache(threshold);
SkMatrix matrix = SkMatrix::I();
auto display_list = GetSampleDisplayList();
SkCanvas dummy_canvas(1000, 1000);
SkPaint paint;
LayerStateStack preroll_state_stack;
preroll_state_stack.set_preroll_delegate(kGiantRect, matrix);
LayerStateStack paint_state_stack;
preroll_state_stack.set_delegate(&dummy_canvas);
FixedRefreshRateStopwatch raster_time;
FixedRefreshRateStopwatch ui_time;
PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder(
preroll_state_stack, &cache, &raster_time, &ui_time);
PaintContextHolder paint_context_holder = GetSamplePaintContextHolder(
paint_state_stack, &cache, &raster_time, &ui_time);
auto& preroll_context = preroll_context_holder.preroll_context;
auto& paint_context = paint_context_holder.paint_context;
cache.BeginFrame();
DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(),
true, false);
// 1st access.
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
cache.EndFrame();
ASSERT_EQ(cache.picture_metrics().total_count(), 0u);
ASSERT_EQ(cache.picture_metrics().total_bytes(), 0u);
cache.BeginFrame();
// 2nd access.
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
cache.EndFrame();
ASSERT_EQ(cache.picture_metrics().total_count(), 0u);
ASSERT_EQ(cache.picture_metrics().total_bytes(), 0u);
cache.BeginFrame();
// Now Prepare should cache it.
ASSERT_TRUE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_TRUE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
cache.EndFrame();
ASSERT_EQ(cache.picture_metrics().total_count(), 1u);
// 150w * 100h * 4bpp
ASSERT_EQ(cache.picture_metrics().total_bytes(), 25600u);
}
TEST(RasterCache, ThresholdIsRespectedForDisplayList) {
size_t threshold = 2;
flutter::RasterCache cache(threshold);
SkMatrix matrix = SkMatrix::I();
auto display_list = GetSampleDisplayList();
SkCanvas dummy_canvas(1000, 1000);
SkPaint paint;
LayerStateStack preroll_state_stack;
preroll_state_stack.set_preroll_delegate(kGiantRect, matrix);
LayerStateStack paint_state_stack;
preroll_state_stack.set_delegate(&dummy_canvas);
FixedRefreshRateStopwatch raster_time;
FixedRefreshRateStopwatch ui_time;
PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder(
preroll_state_stack, &cache, &raster_time, &ui_time);
PaintContextHolder paint_context_holder = GetSamplePaintContextHolder(
paint_state_stack, &cache, &raster_time, &ui_time);
auto& preroll_context = preroll_context_holder.preroll_context;
auto& paint_context = paint_context_holder.paint_context;
cache.BeginFrame();
DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(),
true, false);
// 1st access.
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
cache.EndFrame();
cache.BeginFrame();
// 2nd access.
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
cache.EndFrame();
cache.BeginFrame();
// Now Prepare should cache it.
ASSERT_TRUE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_TRUE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
}
TEST(RasterCache, SetCheckboardCacheImages) {
size_t threshold = 1;
flutter::RasterCache cache(threshold);
SkMatrix matrix = SkMatrix::I();
auto display_list = GetSampleDisplayList();
LayerStateStack preroll_state_stack;
preroll_state_stack.set_preroll_delegate(kGiantRect, matrix);
FixedRefreshRateStopwatch raster_time;
FixedRefreshRateStopwatch ui_time;
PaintContextHolder paint_context_holder = GetSamplePaintContextHolder(
preroll_state_stack, &cache, &raster_time, &ui_time);
auto& paint_context = paint_context_holder.paint_context;
auto dummy_draw_function = [](SkCanvas* canvas) {};
bool did_draw_checkerboard = false;
auto draw_checkerboard = [&](SkCanvas* canvas, const SkRect&) {
did_draw_checkerboard = true;
};
RasterCache::Context r_context = {
// clang-format off
.gr_context = paint_context.gr_context,
.dst_color_space = paint_context.dst_color_space,
.matrix = matrix,
.logical_rect = display_list->bounds(),
.flow_type = "RasterCacheFlow::DisplayList",
// clang-format on
};
cache.SetCheckboardCacheImages(false);
cache.Rasterize(r_context, dummy_draw_function, draw_checkerboard);
ASSERT_FALSE(did_draw_checkerboard);
cache.SetCheckboardCacheImages(true);
cache.Rasterize(r_context, dummy_draw_function, draw_checkerboard);
ASSERT_TRUE(did_draw_checkerboard);
}
TEST(RasterCache, AccessThresholdOfZeroDisablesCachingForSkPicture) {
size_t threshold = 0;
flutter::RasterCache cache(threshold);
SkMatrix matrix = SkMatrix::I();
auto display_list = GetSampleDisplayList();
SkCanvas dummy_canvas(1000, 1000);
SkPaint paint;
LayerStateStack preroll_state_stack;
preroll_state_stack.set_preroll_delegate(kGiantRect, matrix);
LayerStateStack paint_state_stack;
preroll_state_stack.set_delegate(&dummy_canvas);
FixedRefreshRateStopwatch raster_time;
FixedRefreshRateStopwatch ui_time;
PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder(
preroll_state_stack, &cache, &raster_time, &ui_time);
PaintContextHolder paint_context_holder = GetSamplePaintContextHolder(
paint_state_stack, &cache, &raster_time, &ui_time);
auto& preroll_context = preroll_context_holder.preroll_context;
auto& paint_context = paint_context_holder.paint_context;
cache.BeginFrame();
DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(),
true, false);
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
}
TEST(RasterCache, AccessThresholdOfZeroDisablesCachingForDisplayList) {
size_t threshold = 0;
flutter::RasterCache cache(threshold);
SkMatrix matrix = SkMatrix::I();
auto display_list = GetSampleDisplayList();
SkCanvas dummy_canvas(1000, 1000);
SkPaint paint;
LayerStateStack preroll_state_stack;
preroll_state_stack.set_preroll_delegate(kGiantRect, matrix);
LayerStateStack paint_state_stack;
preroll_state_stack.set_delegate(&dummy_canvas);
FixedRefreshRateStopwatch raster_time;
FixedRefreshRateStopwatch ui_time;
PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder(
preroll_state_stack, &cache, &raster_time, &ui_time);
PaintContextHolder paint_context_holder = GetSamplePaintContextHolder(
paint_state_stack, &cache, &raster_time, &ui_time);
auto& preroll_context = preroll_context_holder.preroll_context;
auto& paint_context = paint_context_holder.paint_context;
cache.BeginFrame();
DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(),
true, false);
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
}
TEST(RasterCache, PictureCacheLimitPerFrameIsRespectedWhenZeroForSkPicture) {
size_t picture_cache_limit_per_frame = 0;
flutter::RasterCache cache(3, picture_cache_limit_per_frame);
SkMatrix matrix = SkMatrix::I();
auto display_list = GetSampleDisplayList();
SkCanvas dummy_canvas(1000, 1000);
SkPaint paint;
LayerStateStack preroll_state_stack;
preroll_state_stack.set_preroll_delegate(kGiantRect, matrix);
LayerStateStack paint_state_stack;
preroll_state_stack.set_delegate(&dummy_canvas);
FixedRefreshRateStopwatch raster_time;
FixedRefreshRateStopwatch ui_time;
PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder(
preroll_state_stack, &cache, &raster_time, &ui_time);
PaintContextHolder paint_context_holder = GetSamplePaintContextHolder(
paint_state_stack, &cache, &raster_time, &ui_time);
auto& preroll_context = preroll_context_holder.preroll_context;
auto& paint_context = paint_context_holder.paint_context;
cache.BeginFrame();
DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(),
true, false);
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
}
TEST(RasterCache, PictureCacheLimitPerFrameIsRespectedWhenZeroForDisplayList) {
size_t picture_cache_limit_per_frame = 0;
flutter::RasterCache cache(3, picture_cache_limit_per_frame);
SkMatrix matrix = SkMatrix::I();
auto display_list = GetSampleDisplayList();
SkCanvas dummy_canvas(1000, 1000);
SkPaint paint;
LayerStateStack preroll_state_stack;
preroll_state_stack.set_preroll_delegate(kGiantRect, matrix);
LayerStateStack paint_state_stack;
preroll_state_stack.set_delegate(&dummy_canvas);
FixedRefreshRateStopwatch raster_time;
FixedRefreshRateStopwatch ui_time;
PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder(
preroll_state_stack, &cache, &raster_time, &ui_time);
PaintContextHolder paint_context_holder = GetSamplePaintContextHolder(
paint_state_stack, &cache, &raster_time, &ui_time);
auto& preroll_context = preroll_context_holder.preroll_context;
auto& paint_context = paint_context_holder.paint_context;
cache.BeginFrame();
DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(),
true, false);
// 1st access.
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
// 2nd access.
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
// the picture_cache_limit_per_frame = 0, so don't cache it
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
}
TEST(RasterCache, EvitUnusedCacheEntries) {
size_t threshold = 1;
flutter::RasterCache cache(threshold);
SkMatrix matrix = SkMatrix::I();
auto display_list_1 = GetSampleDisplayList();
auto display_list_2 = GetSampleDisplayList();
SkCanvas dummy_canvas(1000, 1000);
SkPaint paint;
LayerStateStack preroll_state_stack;
preroll_state_stack.set_preroll_delegate(kGiantRect, matrix);
LayerStateStack paint_state_stack;
preroll_state_stack.set_delegate(&dummy_canvas);
FixedRefreshRateStopwatch raster_time;
FixedRefreshRateStopwatch ui_time;
PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder(
preroll_state_stack, &cache, &raster_time, &ui_time);
PaintContextHolder paint_context_holder = GetSamplePaintContextHolder(
paint_state_stack, &cache, &raster_time, &ui_time);
auto& preroll_context = preroll_context_holder.preroll_context;
auto& paint_context = paint_context_holder.paint_context;
DisplayListRasterCacheItem display_list_item_1(display_list_1.get(),
SkPoint(), true, false);
DisplayListRasterCacheItem display_list_item_2(display_list_2.get(),
SkPoint(), true, false);
cache.BeginFrame();
RasterCacheItemPreroll(display_list_item_1, preroll_context, matrix);
RasterCacheItemPreroll(display_list_item_2, preroll_context, matrix);
cache.EvictUnusedCacheEntries();
ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 0u);
ASSERT_FALSE(
RasterCacheItemTryToRasterCache(display_list_item_1, paint_context));
ASSERT_FALSE(
RasterCacheItemTryToRasterCache(display_list_item_2, paint_context));
ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 0u);
ASSERT_FALSE(display_list_item_1.Draw(paint_context, &dummy_canvas, &paint));
ASSERT_FALSE(display_list_item_2.Draw(paint_context, &dummy_canvas, &paint));
cache.EndFrame();
ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 0u);
ASSERT_EQ(cache.picture_metrics().total_count(), 0u);
ASSERT_EQ(cache.picture_metrics().total_bytes(), 0u);
cache.BeginFrame();
RasterCacheItemPreroll(display_list_item_1, preroll_context, matrix);
RasterCacheItemPreroll(display_list_item_2, preroll_context, matrix);
cache.EvictUnusedCacheEntries();
ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 0u);
ASSERT_TRUE(
RasterCacheItemTryToRasterCache(display_list_item_1, paint_context));
ASSERT_TRUE(
RasterCacheItemTryToRasterCache(display_list_item_2, paint_context));
ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 51200u);
ASSERT_TRUE(display_list_item_1.Draw(paint_context, &dummy_canvas, &paint));
ASSERT_TRUE(display_list_item_2.Draw(paint_context, &dummy_canvas, &paint));
cache.EndFrame();
ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 51200u);
ASSERT_EQ(cache.picture_metrics().total_count(), 2u);
ASSERT_EQ(cache.picture_metrics().total_bytes(), 51200u);
cache.BeginFrame();
RasterCacheItemPreroll(display_list_item_1, preroll_context, matrix);
cache.EvictUnusedCacheEntries();
ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 25600u);
ASSERT_TRUE(
RasterCacheItemTryToRasterCache(display_list_item_1, paint_context));
ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 25600u);
ASSERT_TRUE(display_list_item_1.Draw(paint_context, &dummy_canvas, &paint));
cache.EndFrame();
ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 25600u);
ASSERT_EQ(cache.picture_metrics().total_count(), 1u);
ASSERT_EQ(cache.picture_metrics().total_bytes(), 25600u);
cache.BeginFrame();
cache.EvictUnusedCacheEntries();
ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 0u);
cache.EndFrame();
ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 0u);
ASSERT_EQ(cache.picture_metrics().total_count(), 0u);
ASSERT_EQ(cache.picture_metrics().total_bytes(), 0u);
cache.BeginFrame();
ASSERT_FALSE(
cache.Draw(display_list_item_1.GetId().value(), dummy_canvas, &paint));
ASSERT_FALSE(display_list_item_1.Draw(paint_context, &dummy_canvas, &paint));
ASSERT_FALSE(
cache.Draw(display_list_item_2.GetId().value(), dummy_canvas, &paint));
ASSERT_FALSE(display_list_item_2.Draw(paint_context, &dummy_canvas, &paint));
cache.EndFrame();
}
TEST(RasterCache, ComputeDeviceRectBasedOnFractionalTranslation) {
SkRect logical_rect = SkRect::MakeLTRB(0, 0, 300.2, 300.3);
SkMatrix ctm = SkMatrix::MakeAll(2.0, 0, 0, 0, 2.0, 0, 0, 0, 1);
auto result = RasterCacheUtil::GetDeviceBounds(logical_rect, ctm);
ASSERT_EQ(result, SkRect::MakeLTRB(0.0, 0.0, 600.4, 600.6));
}
// Construct a cache result whose device target rectangle rounds out to be one
// pixel wider than the cached image. Verify that it can be drawn without
// triggering any assertions.
TEST(RasterCache, DeviceRectRoundOutForDisplayList) {
size_t threshold = 1;
flutter::RasterCache cache(threshold);
SkRect logical_rect = SkRect::MakeLTRB(28, 0, 354.56731, 310.288);
DisplayListBuilder builder(logical_rect);
builder.setColor(SK_ColorRED);
builder.drawRect(logical_rect);
sk_sp<DisplayList> display_list = builder.Build();
SkMatrix ctm = SkMatrix::MakeAll(1.3312, 0, 233, 0, 1.3312, 206, 0, 0, 1);
SkPaint paint;
SkCanvas canvas(1000, 1000, nullptr);
canvas.setMatrix(ctm);
LayerStateStack preroll_state_stack;
preroll_state_stack.set_preroll_delegate(kGiantRect, ctm);
LayerStateStack paint_state_stack;
preroll_state_stack.set_delegate(&canvas);
FixedRefreshRateStopwatch raster_time;
FixedRefreshRateStopwatch ui_time;
PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder(
preroll_state_stack, &cache, &raster_time, &ui_time);
PaintContextHolder paint_context_holder = GetSamplePaintContextHolder(
paint_state_stack, &cache, &raster_time, &ui_time);
auto& preroll_context = preroll_context_holder.preroll_context;
auto& paint_context = paint_context_holder.paint_context;
cache.BeginFrame();
DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(),
true, false);
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, ctm));
ASSERT_FALSE(display_list_item.Draw(paint_context, &canvas, &paint));
cache.EndFrame();
cache.BeginFrame();
ASSERT_TRUE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, ctm));
ASSERT_TRUE(display_list_item.Draw(paint_context, &canvas, &paint));
canvas.translate(248, 0);
ASSERT_TRUE(cache.Draw(display_list_item.GetId().value(), canvas, &paint));
ASSERT_TRUE(display_list_item.Draw(paint_context, &canvas, &paint));
}
TEST(RasterCache, NestedOpCountMetricUsedForDisplayList) {
size_t threshold = 1;
flutter::RasterCache cache(threshold);
SkMatrix matrix = SkMatrix::I();
auto display_list = GetSampleNestedDisplayList();
ASSERT_EQ(display_list->op_count(), 1u);
ASSERT_EQ(display_list->op_count(true), 36u);
SkCanvas dummy_canvas(1000, 1000);
SkPaint paint;
LayerStateStack preroll_state_stack;
preroll_state_stack.set_preroll_delegate(kGiantRect, matrix);
LayerStateStack paint_state_stack;
preroll_state_stack.set_delegate(&dummy_canvas);
FixedRefreshRateStopwatch raster_time;
FixedRefreshRateStopwatch ui_time;
PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder(
preroll_state_stack, &cache, &raster_time, &ui_time);
PaintContextHolder paint_context_holder = GetSamplePaintContextHolder(
paint_state_stack, &cache, &raster_time, &ui_time);
auto& preroll_context = preroll_context_holder.preroll_context;
auto& paint_context = paint_context_holder.paint_context;
cache.BeginFrame();
DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(),
false, false);
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
cache.EndFrame();
cache.BeginFrame();
ASSERT_TRUE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_TRUE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
}
TEST(RasterCache, NaiveComplexityScoringDisplayList) {
DisplayListComplexityCalculator* calculator =
DisplayListNaiveComplexityCalculator::GetInstance();
size_t threshold = 1;
flutter::RasterCache cache(threshold);
SkMatrix matrix = SkMatrix::I();
// Five raster ops will not be cached
auto display_list = GetSampleDisplayList(5);
unsigned int complexity_score = calculator->Compute(display_list.get());
ASSERT_EQ(complexity_score, 5u);
ASSERT_EQ(display_list->op_count(), 5u);
ASSERT_FALSE(calculator->ShouldBeCached(complexity_score));
SkCanvas dummy_canvas(1000, 1000);
SkPaint paint;
LayerStateStack preroll_state_stack;
preroll_state_stack.set_preroll_delegate(kGiantRect, matrix);
LayerStateStack paint_state_stack;
preroll_state_stack.set_delegate(&dummy_canvas);
FixedRefreshRateStopwatch raster_time;
FixedRefreshRateStopwatch ui_time;
PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder(
preroll_state_stack, &cache, &raster_time, &ui_time);
PaintContextHolder paint_context_holder = GetSamplePaintContextHolder(
paint_state_stack, &cache, &raster_time, &ui_time);
auto& preroll_context = preroll_context_holder.preroll_context;
auto& paint_context = paint_context_holder.paint_context;
cache.BeginFrame();
DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(),
false, false);
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
cache.EndFrame();
cache.BeginFrame();
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
// Six raster ops should be cached
display_list = GetSampleDisplayList(6);
complexity_score = calculator->Compute(display_list.get());
ASSERT_EQ(complexity_score, 6u);
ASSERT_EQ(display_list->op_count(), 6u);
ASSERT_TRUE(calculator->ShouldBeCached(complexity_score));
DisplayListRasterCacheItem display_list_item_2 =
DisplayListRasterCacheItem(display_list.get(), SkPoint(), false, false);
cache.BeginFrame();
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item_2, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item_2.Draw(paint_context, &dummy_canvas, &paint));
cache.EndFrame();
cache.BeginFrame();
ASSERT_TRUE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item_2, preroll_context, paint_context, matrix));
ASSERT_TRUE(display_list_item_2.Draw(paint_context, &dummy_canvas, &paint));
}
TEST(RasterCache, DisplayListWithSingularMatrixIsNotCached) {
size_t threshold = 2;
flutter::RasterCache cache(threshold);
SkMatrix matrices[] = {
SkMatrix::Scale(0, 1),
SkMatrix::Scale(1, 0),
SkMatrix::Skew(1, 1),
};
int matrix_count = sizeof(matrices) / sizeof(matrices[0]);
auto display_list = GetSampleDisplayList();
SkCanvas dummy_canvas(1000, 1000);
SkPaint paint;
LayerStateStack preroll_state_stack;
preroll_state_stack.set_preroll_delegate(kGiantRect, SkMatrix::I());
LayerStateStack paint_state_stack;
preroll_state_stack.set_delegate(&dummy_canvas);
FixedRefreshRateStopwatch raster_time;
FixedRefreshRateStopwatch ui_time;
PrerollContextHolder preroll_context_holder = GetSamplePrerollContextHolder(
preroll_state_stack, &cache, &raster_time, &ui_time);
PaintContextHolder paint_context_holder = GetSamplePaintContextHolder(
paint_state_stack, &cache, &raster_time, &ui_time);
auto& preroll_context = preroll_context_holder.preroll_context;
auto& paint_context = paint_context_holder.paint_context;
DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(),
true, false);
for (int i = 0; i < 10; i++) {
cache.BeginFrame();
for (int j = 0; j < matrix_count; j++) {
display_list_item.set_matrix(matrices[j]);
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrices[j]));
}
for (int j = 0; j < matrix_count; j++) {
dummy_canvas.setMatrix(matrices[j]);
ASSERT_FALSE(
display_list_item.Draw(paint_context, &dummy_canvas, &paint));
}
cache.EndFrame();
}
}
TEST(RasterCache, PrepareLayerTransform) {
SkRect child_bounds = SkRect::MakeLTRB(10, 10, 50, 50);
SkPath child_path = SkPath().addOval(child_bounds);
auto child_layer = MockLayer::Make(child_path);
auto blur_filter =
std::make_shared<DlBlurImageFilter>(5, 5, DlTileMode::kClamp);
auto blur_layer = std::make_shared<ImageFilterLayer>(blur_filter);
SkMatrix matrix = SkMatrix::Scale(2, 2);
auto transform_layer = std::make_shared<TransformLayer>(matrix);
SkMatrix cache_matrix = SkMatrix::Translate(-20, -20);
cache_matrix.preConcat(matrix);
child_layer->set_expected_paint_matrix(cache_matrix);
blur_layer->Add(child_layer);
transform_layer->Add(blur_layer);
size_t threshold = 2;
MockRasterCache cache(threshold);
SkCanvas dummy_canvas(1000, 1000);
LayerStateStack preroll_state_stack;
preroll_state_stack.set_preroll_delegate(kGiantRect, matrix);
LayerStateStack paint_state_stack;
preroll_state_stack.set_delegate(&dummy_canvas);
FixedRefreshRateStopwatch raster_time;
FixedRefreshRateStopwatch ui_time;
std::vector<RasterCacheItem*> cache_items;
cache.BeginFrame();
auto preroll_holder = GetSamplePrerollContextHolder(
preroll_state_stack, &cache, &raster_time, &ui_time);
preroll_holder.preroll_context.raster_cached_entries = &cache_items;
transform_layer->Preroll(&preroll_holder.preroll_context);
auto paint_holder = GetSamplePaintContextHolder(paint_state_stack, &cache,
&raster_time, &ui_time);
cache.EvictUnusedCacheEntries();
LayerTree::TryToRasterCache(
*preroll_holder.preroll_context.raster_cached_entries,
&paint_holder.paint_context);
// Condition tested inside MockLayer::Paint against expected paint matrix.
}
TEST(RasterCache, RasterCacheKeyHashFunction) {
RasterCacheKey::Map<int> map;
auto hash_function = map.hash_function();
SkMatrix matrix = SkMatrix::I();
uint64_t id = 5;
RasterCacheKey layer_key(id, RasterCacheKeyType::kLayer, matrix);
RasterCacheKey display_list_key(id, RasterCacheKeyType::kDisplayList, matrix);
RasterCacheKey layer_children_key(id, RasterCacheKeyType::kLayerChildren,
matrix);
auto layer_cache_key_id = RasterCacheKeyID(id, RasterCacheKeyType::kLayer);
auto layer_hash_code = hash_function(layer_key);
ASSERT_EQ(layer_hash_code, layer_cache_key_id.GetHash());
auto display_list_cache_key_id =
RasterCacheKeyID(id, RasterCacheKeyType::kDisplayList);
auto display_list_hash_code = hash_function(display_list_key);
ASSERT_EQ(display_list_hash_code, display_list_cache_key_id.GetHash());
auto layer_children_cache_key_id =
RasterCacheKeyID(id, RasterCacheKeyType::kLayerChildren);
auto layer_children_hash_code = hash_function(layer_children_key);
ASSERT_EQ(layer_children_hash_code, layer_children_cache_key_id.GetHash());
}
TEST(RasterCache, RasterCacheKeySameID) {
RasterCacheKey::Map<int> map;
SkMatrix matrix = SkMatrix::I();
uint64_t id = 5;
RasterCacheKey layer_key(id, RasterCacheKeyType::kLayer, matrix);
RasterCacheKey display_list_key(id, RasterCacheKeyType::kDisplayList, matrix);
RasterCacheKey layer_children_key(id, RasterCacheKeyType::kLayerChildren,
matrix);
map[layer_key] = 100;
map[display_list_key] = 300;
map[layer_children_key] = 400;
ASSERT_EQ(map[layer_key], 100);
ASSERT_EQ(map[display_list_key], 300);
ASSERT_EQ(map[layer_children_key], 400);
}
TEST(RasterCache, RasterCacheKeySameType) {
RasterCacheKey::Map<int> map;
SkMatrix matrix = SkMatrix::I();
RasterCacheKeyType type = RasterCacheKeyType::kLayer;
RasterCacheKey layer_first_key(5, type, matrix);
RasterCacheKey layer_second_key(10, type, matrix);
RasterCacheKey layer_third_key(15, type, matrix);
map[layer_first_key] = 50;
map[layer_second_key] = 100;
map[layer_third_key] = 150;
ASSERT_EQ(map[layer_first_key], 50);
ASSERT_EQ(map[layer_second_key], 100);
ASSERT_EQ(map[layer_third_key], 150);
type = RasterCacheKeyType::kDisplayList;
RasterCacheKey picture_first_key(20, type, matrix);
RasterCacheKey picture_second_key(25, type, matrix);
RasterCacheKey picture_third_key(30, type, matrix);
map[picture_first_key] = 200;
map[picture_second_key] = 250;
map[picture_third_key] = 300;
ASSERT_EQ(map[picture_first_key], 200);
ASSERT_EQ(map[picture_second_key], 250);
ASSERT_EQ(map[picture_third_key], 300);
type = RasterCacheKeyType::kDisplayList;
RasterCacheKey display_list_first_key(35, type, matrix);
RasterCacheKey display_list_second_key(40, type, matrix);
RasterCacheKey display_list_third_key(45, type, matrix);
map[display_list_first_key] = 350;
map[display_list_second_key] = 400;
map[display_list_third_key] = 450;
ASSERT_EQ(map[display_list_first_key], 350);
ASSERT_EQ(map[display_list_second_key], 400);
ASSERT_EQ(map[display_list_third_key], 450);
type = RasterCacheKeyType::kLayerChildren;
RasterCacheKeyID foo = RasterCacheKeyID(10, RasterCacheKeyType::kLayer);
RasterCacheKeyID bar = RasterCacheKeyID(20, RasterCacheKeyType::kLayer);
RasterCacheKeyID baz = RasterCacheKeyID(30, RasterCacheKeyType::kLayer);
RasterCacheKey layer_children_first_key(
RasterCacheKeyID({foo, bar, baz}, type), matrix);
RasterCacheKey layer_children_second_key(
RasterCacheKeyID({foo, baz, bar}, type), matrix);
RasterCacheKey layer_children_third_key(
RasterCacheKeyID({baz, bar, foo}, type), matrix);
map[layer_children_first_key] = 100;
map[layer_children_second_key] = 200;
map[layer_children_third_key] = 300;
ASSERT_EQ(map[layer_children_first_key], 100);
ASSERT_EQ(map[layer_children_second_key], 200);
ASSERT_EQ(map[layer_children_third_key], 300);
}
TEST(RasterCache, RasterCacheKeyIDEqual) {
RasterCacheKeyID first = RasterCacheKeyID(1, RasterCacheKeyType::kLayer);
RasterCacheKeyID second = RasterCacheKeyID(2, RasterCacheKeyType::kLayer);
RasterCacheKeyID third =
RasterCacheKeyID(1, RasterCacheKeyType::kLayerChildren);
ASSERT_NE(first, second);
ASSERT_NE(first, third);
ASSERT_NE(second, third);
RasterCacheKeyID fourth =
RasterCacheKeyID({first, second}, RasterCacheKeyType::kLayer);
RasterCacheKeyID fifth =
RasterCacheKeyID({first, second}, RasterCacheKeyType::kLayerChildren);
RasterCacheKeyID sixth =
RasterCacheKeyID({second, first}, RasterCacheKeyType::kLayerChildren);
ASSERT_NE(fourth, fifth);
ASSERT_NE(fifth, sixth);
}
TEST(RasterCache, RasterCacheKeyIDHashCode) {
uint64_t foo = 1;
uint64_t bar = 2;
RasterCacheKeyID first = RasterCacheKeyID(foo, RasterCacheKeyType::kLayer);
RasterCacheKeyID second = RasterCacheKeyID(bar, RasterCacheKeyType::kLayer);
std::size_t first_hash = first.GetHash();
std::size_t second_hash = second.GetHash();
ASSERT_EQ(first_hash, fml::HashCombine(foo, RasterCacheKeyType::kLayer));
ASSERT_EQ(second_hash, fml::HashCombine(bar, RasterCacheKeyType::kLayer));
RasterCacheKeyID third =
RasterCacheKeyID({first, second}, RasterCacheKeyType::kLayerChildren);
RasterCacheKeyID fourth =
RasterCacheKeyID({second, first}, RasterCacheKeyType::kLayerChildren);
std::size_t third_hash = third.GetHash();
std::size_t fourth_hash = fourth.GetHash();
ASSERT_EQ(third_hash, fml::HashCombine(RasterCacheKeyID::kDefaultUniqueID,
RasterCacheKeyType::kLayerChildren,
first.GetHash(), second.GetHash()));
ASSERT_EQ(fourth_hash, fml::HashCombine(RasterCacheKeyID::kDefaultUniqueID,
RasterCacheKeyType::kLayerChildren,
second.GetHash(), first.GetHash()));
// Verify that the cached hash code is correct.
ASSERT_EQ(first_hash, first.GetHash());
ASSERT_EQ(second_hash, second.GetHash());
ASSERT_EQ(third_hash, third.GetHash());
ASSERT_EQ(fourth_hash, fourth.GetHash());
}
using RasterCacheTest = SkiaGPUObjectLayerTest;
TEST_F(RasterCacheTest, RasterCacheKeyIDLayerChildrenIds) {
auto layer = std::make_shared<ContainerLayer>();
const SkPath child_path = SkPath().addRect(SkRect::MakeWH(5.0f, 5.0f));
auto mock_layer = std::make_shared<MockLayer>(child_path);
layer->Add(mock_layer);
auto display_list = GetSampleDisplayList();
auto display_list_layer = std::make_shared<DisplayListLayer>(
SkPoint::Make(0.0f, 0.0f),
SkiaGPUObject<DisplayList>(display_list, unref_queue()), false, false);
layer->Add(display_list_layer);
auto ids = RasterCacheKeyID::LayerChildrenIds(layer.get()).value();
std::vector<RasterCacheKeyID> expected_ids;
expected_ids.emplace_back(
RasterCacheKeyID(mock_layer->unique_id(), RasterCacheKeyType::kLayer));
expected_ids.emplace_back(RasterCacheKeyID(display_list->unique_id(),
RasterCacheKeyType::kDisplayList));
ASSERT_EQ(expected_ids[0], mock_layer->caching_key_id());
ASSERT_EQ(expected_ids[1], display_list_layer->caching_key_id());
ASSERT_EQ(ids, expected_ids);
}
} // namespace testing
} // namespace flutter