forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
display_list_canvas_unittests.cc
3239 lines (3053 loc) · 128 KB
/
display_list_canvas_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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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_canvas_dispatcher.h"
#include "flutter/display_list/display_list_canvas_recorder.h"
#include "flutter/display_list/display_list_comparable.h"
#include "flutter/display_list/display_list_flags.h"
#include "flutter/display_list/display_list_sampling_options.h"
#include "flutter/fml/math.h"
#include "flutter/testing/testing.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/effects/SkBlenders.h"
#include "third_party/skia/include/effects/SkDashPathEffect.h"
#include "third_party/skia/include/effects/SkDiscretePathEffect.h"
#include "third_party/skia/include/effects/SkGradientShader.h"
#include "third_party/skia/include/effects/SkImageFilters.h"
namespace flutter {
namespace testing {
constexpr int kTestWidth = 200;
constexpr int kTestHeight = 200;
constexpr int kRenderWidth = 100;
constexpr int kRenderHeight = 100;
constexpr int kRenderHalfWidth = 50;
constexpr int kRenderHalfHeight = 50;
constexpr int kRenderLeft = (kTestWidth - kRenderWidth) / 2;
constexpr int kRenderTop = (kTestHeight - kRenderHeight) / 2;
constexpr int kRenderRight = kRenderLeft + kRenderWidth;
constexpr int kRenderBottom = kRenderTop + kRenderHeight;
constexpr int kRenderCenterX = (kRenderLeft + kRenderRight) / 2;
constexpr int kRenderCenterY = (kRenderTop + kRenderBottom) / 2;
constexpr SkScalar kRenderRadius = std::min(kRenderWidth, kRenderHeight) / 2.0;
constexpr SkScalar kRenderCornerRadius = kRenderRadius / 5.0;
constexpr SkPoint kTestCenter = SkPoint::Make(kTestWidth / 2, kTestHeight / 2);
constexpr SkRect kTestBounds = SkRect::MakeWH(kTestWidth, kTestHeight);
constexpr SkRect kRenderBounds =
SkRect::MakeLTRB(kRenderLeft, kRenderTop, kRenderRight, kRenderBottom);
// The tests try 3 miter limit values, 0.0, 4.0 (the default), and 10.0
// These values will allow us to construct a diamond that spans the
// width or height of the render box and still show the miter for 4.0
// and 10.0.
// These values were discovered by drawing a diamond path in Skia fiddle
// and then playing with the cross-axis size until the miter was about
// as large as it could get before it got cut off.
// The X offsets which will be used for tall vertical diamonds are
// expressed in terms of the rendering height to obtain the proper angle
constexpr SkScalar kMiterExtremeDiamondOffsetX = kRenderHeight * 0.04;
constexpr SkScalar kMiter10DiamondOffsetX = kRenderHeight * 0.051;
constexpr SkScalar kMiter4DiamondOffsetX = kRenderHeight * 0.14;
// The Y offsets which will be used for long horizontal diamonds are
// expressed in terms of the rendering width to obtain the proper angle
constexpr SkScalar kMiterExtremeDiamondOffsetY = kRenderWidth * 0.04;
constexpr SkScalar kMiter10DiamondOffsetY = kRenderWidth * 0.051;
constexpr SkScalar kMiter4DiamondOffsetY = kRenderWidth * 0.14;
// Render 3 vertical and horizontal diamonds each
// designed to break at the tested miter limits
// 0.0, 4.0 and 10.0
// Center is biased by 0.5 to include more pixel centers in the
// thin miters
constexpr SkScalar kXOffset0 = kRenderCenterX + 0.5;
constexpr SkScalar kXOffsetL1 = kXOffset0 - kMiter4DiamondOffsetX;
constexpr SkScalar kXOffsetL2 = kXOffsetL1 - kMiter10DiamondOffsetX;
constexpr SkScalar kXOffsetL3 = kXOffsetL2 - kMiter10DiamondOffsetX;
constexpr SkScalar kXOffsetR1 = kXOffset0 + kMiter4DiamondOffsetX;
constexpr SkScalar kXOffsetR2 = kXOffsetR1 + kMiterExtremeDiamondOffsetX;
constexpr SkScalar kXOffsetR3 = kXOffsetR2 + kMiterExtremeDiamondOffsetX;
constexpr SkPoint kVerticalMiterDiamondPoints[] = {
// Vertical diamonds:
// M10 M4 Mextreme
// /\ /|\ /\ top of RenderBounds
// / \ / | \ / \ to
// <----X--+--X----> RenderCenter
// \ / \ | / \ / to
// \/ \|/ \/ bottom of RenderBounds
// clang-format off
SkPoint::Make(kXOffsetL3, kRenderCenterY),
SkPoint::Make(kXOffsetL2, kRenderTop),
SkPoint::Make(kXOffsetL1, kRenderCenterY),
SkPoint::Make(kXOffset0, kRenderTop),
SkPoint::Make(kXOffsetR1, kRenderCenterY),
SkPoint::Make(kXOffsetR2, kRenderTop),
SkPoint::Make(kXOffsetR3, kRenderCenterY),
SkPoint::Make(kXOffsetR2, kRenderBottom),
SkPoint::Make(kXOffsetR1, kRenderCenterY),
SkPoint::Make(kXOffset0, kRenderBottom),
SkPoint::Make(kXOffsetL1, kRenderCenterY),
SkPoint::Make(kXOffsetL2, kRenderBottom),
SkPoint::Make(kXOffsetL3, kRenderCenterY),
// clang-format on
};
const int kVerticalMiterDiamondPointCount =
sizeof(kVerticalMiterDiamondPoints) /
sizeof(kVerticalMiterDiamondPoints[0]);
constexpr SkScalar kYOffset0 = kRenderCenterY + 0.5;
constexpr SkScalar kYOffsetU1 = kXOffset0 - kMiter4DiamondOffsetY;
constexpr SkScalar kYOffsetU2 = kYOffsetU1 - kMiter10DiamondOffsetY;
constexpr SkScalar kYOffsetU3 = kYOffsetU2 - kMiter10DiamondOffsetY;
constexpr SkScalar kYOffsetD1 = kXOffset0 + kMiter4DiamondOffsetY;
constexpr SkScalar kYOffsetD2 = kYOffsetD1 + kMiterExtremeDiamondOffsetY;
constexpr SkScalar kYOffsetD3 = kYOffsetD2 + kMiterExtremeDiamondOffsetY;
const SkPoint kHorizontalMiterDiamondPoints[] = {
// Horizontal diamonds
// Same configuration as Vertical diamonds above but
// rotated 90 degrees
// clang-format off
SkPoint::Make(kRenderCenterX, kYOffsetU3),
SkPoint::Make(kRenderLeft, kYOffsetU2),
SkPoint::Make(kRenderCenterX, kYOffsetU1),
SkPoint::Make(kRenderLeft, kYOffset0),
SkPoint::Make(kRenderCenterX, kYOffsetD1),
SkPoint::Make(kRenderLeft, kYOffsetD2),
SkPoint::Make(kRenderCenterX, kYOffsetD3),
SkPoint::Make(kRenderRight, kYOffsetD2),
SkPoint::Make(kRenderCenterX, kYOffsetD1),
SkPoint::Make(kRenderRight, kYOffset0),
SkPoint::Make(kRenderCenterX, kYOffsetU1),
SkPoint::Make(kRenderRight, kYOffsetU2),
SkPoint::Make(kRenderCenterX, kYOffsetU3),
// clang-format on
};
const int kHorizontalMiterDiamondPointCount =
(sizeof(kHorizontalMiterDiamondPoints) /
sizeof(kHorizontalMiterDiamondPoints[0]));
// A class to specify how much tolerance to allow in bounds estimates.
// For some attributes, the machinery must make some conservative
// assumptions as to the extent of the bounds, but some of our test
// parameters do not produce bounds that expand by the full conservative
// estimates. This class provides a number of tweaks to apply to the
// pixel bounds to account for the conservative factors.
//
// An instance is passed along through the methods and if any test adds
// a paint attribute or other modifier that will cause a more conservative
// estimate for bounds, it can modify the factors here to account for it.
// Ideally, all tests will be executed with geometry that will trigger
// the conservative cases anyway and all attributes will be combined with
// other attributes that make their output more predictable, but in those
// cases where a given test sequence cannot really provide attributes to
// demonstrate the worst case scenario, they can modify these factors to
// avoid false bounds overflow notifications.
class BoundsTolerance {
public:
BoundsTolerance() = default;
BoundsTolerance(const BoundsTolerance&) = default;
BoundsTolerance addBoundsPadding(SkScalar bounds_pad_x,
SkScalar bounds_pad_y) const {
BoundsTolerance copy = BoundsTolerance(*this);
copy.bounds_pad_.offset(bounds_pad_x, bounds_pad_y);
return copy;
}
BoundsTolerance mulScale(SkScalar scale_x, SkScalar scale_y) const {
BoundsTolerance copy = BoundsTolerance(*this);
copy.scale_.fX *= scale_x;
copy.scale_.fY *= scale_y;
return copy;
}
BoundsTolerance addAbsolutePadding(SkScalar absolute_pad_x,
SkScalar absolute_pad_y) const {
BoundsTolerance copy = BoundsTolerance(*this);
copy.absolute_pad_.offset(absolute_pad_x, absolute_pad_y);
return copy;
}
BoundsTolerance addDiscreteOffset(SkScalar discrete_offset) const {
BoundsTolerance copy = BoundsTolerance(*this);
copy.discrete_offset_ += discrete_offset;
return copy;
}
BoundsTolerance clip(SkRect clip) const {
BoundsTolerance copy = BoundsTolerance(*this);
if (!copy.clip_.intersect(clip)) {
copy.clip_.setEmpty();
}
return copy;
}
static SkRect Scale(const SkRect& rect, const SkPoint& scales) {
SkScalar outset_x = rect.width() * (scales.fX - 1);
SkScalar outset_y = rect.height() * (scales.fY - 1);
return rect.makeOutset(outset_x, outset_y);
}
bool overflows(SkIRect pix_bounds,
int worst_bounds_pad_x,
int worst_bounds_pad_y) const {
SkRect allowed = SkRect::Make(pix_bounds);
allowed.outset(bounds_pad_.fX, bounds_pad_.fY);
allowed = Scale(allowed, scale_);
allowed.outset(absolute_pad_.fX, absolute_pad_.fY);
if (!allowed.intersect(clip_)) {
allowed.setEmpty();
}
SkIRect rounded = allowed.roundOut();
int padLeft = std::max(0, pix_bounds.fLeft - rounded.fLeft);
int padTop = std::max(0, pix_bounds.fTop - rounded.fTop);
int padRight = std::max(0, pix_bounds.fRight - rounded.fRight);
int padBottom = std::max(0, pix_bounds.fBottom - rounded.fBottom);
int allowed_pad_x = std::max(padLeft, padRight);
int allowed_pad_y = std::max(padTop, padBottom);
if (worst_bounds_pad_x > allowed_pad_x ||
worst_bounds_pad_y > allowed_pad_y) {
FML_LOG(ERROR) << "allowed pad: " //
<< allowed_pad_x << ", " << allowed_pad_y;
}
return (worst_bounds_pad_x > allowed_pad_x ||
worst_bounds_pad_y > allowed_pad_y);
}
SkScalar discrete_offset() const { return discrete_offset_; }
private:
SkPoint bounds_pad_ = {0, 0};
SkPoint scale_ = {1, 1};
SkPoint absolute_pad_ = {0, 0};
SkRect clip_ = {-1E9, -1E9, 1E9, 1E9};
SkScalar discrete_offset_ = 0;
};
typedef const std::function<void(SkCanvas*, SkPaint&)> CvSetup;
typedef const std::function<void(SkCanvas*, const SkPaint&)> CvRenderer;
typedef const std::function<void(DisplayListBuilder&)> DlRenderer;
static void EmptyCvRenderer(SkCanvas*, const SkPaint&) {}
static void EmptyDlRenderer(DisplayListBuilder&) {}
class RenderSurface {
public:
explicit RenderSurface(sk_sp<SkSurface> surface) : surface_(surface) {
EXPECT_EQ(canvas()->save(), 1);
}
~RenderSurface() { sk_free(addr_); }
SkCanvas* canvas() { return surface_->getCanvas(); }
const SkPixmap* pixmap() {
if (!pixmap_.addr()) {
canvas()->restoreToCount(1);
SkImageInfo info = surface_->imageInfo();
if (info.colorType() != kN32_SkColorType ||
!surface_->peekPixels(&pixmap_)) {
info = SkImageInfo::MakeN32Premul(info.dimensions());
addr_ = malloc(info.computeMinByteSize() * info.height());
pixmap_.reset(info, addr_, info.minRowBytes());
EXPECT_TRUE(surface_->readPixels(pixmap_, 0, 0));
}
}
return &pixmap_;
}
private:
sk_sp<SkSurface> surface_;
SkPixmap pixmap_;
void* addr_ = nullptr;
};
class RenderEnvironment {
public:
static RenderEnvironment Make565() {
return RenderEnvironment(
SkImageInfo::Make({1, 1}, kRGB_565_SkColorType, kOpaque_SkAlphaType));
}
static RenderEnvironment MakeN32() {
return RenderEnvironment(SkImageInfo::MakeN32Premul(1, 1));
}
std::unique_ptr<RenderSurface> MakeSurface(
const DlColor bg = DlColor::kTransparent(),
int width = kTestWidth,
int height = kTestHeight) const {
sk_sp<SkSurface> surface =
SkSurface::MakeRaster(info_.makeWH(width, height));
surface->getCanvas()->clear(bg);
return std::make_unique<RenderSurface>(surface);
}
void init_ref(CvRenderer& cv_renderer, DlColor bg = DlColor::kTransparent()) {
init_ref([=](SkCanvas*, SkPaint&) {}, cv_renderer,
[=](DisplayListBuilder&) {}, bg);
}
void init_ref(CvSetup& cv_setup,
CvRenderer& cv_renderer,
DlRenderer& dl_setup,
DlColor bg = DlColor::kTransparent()) {
ref_canvas()->clear(bg);
dl_setup(ref_attr_);
SkPaint paint;
cv_setup(ref_canvas(), paint);
ref_matrix_ = ref_canvas()->getTotalMatrix();
ref_clip_ = ref_canvas()->getDeviceClipBounds();
cv_renderer(ref_canvas(), paint);
ref_pixmap_ = ref_surface_->pixmap();
}
const SkImageInfo& info() const { return info_; }
SkCanvas* ref_canvas() { return ref_surface_->canvas(); }
const DisplayListBuilder& ref_attr() const { return ref_attr_; }
const SkMatrix& ref_matrix() const { return ref_matrix_; }
const SkIRect& ref_clip_bounds() const { return ref_clip_; }
const SkPixmap* ref_pixmap() const { return ref_pixmap_; }
private:
explicit RenderEnvironment(const SkImageInfo& info) : info_(info) {
ref_surface_ = MakeSurface();
}
const SkImageInfo info_;
DisplayListBuilder ref_attr_;
SkMatrix ref_matrix_;
SkIRect ref_clip_;
std::unique_ptr<RenderSurface> ref_surface_;
const SkPixmap* ref_pixmap_ = nullptr;
};
class TestParameters {
public:
TestParameters(const CvRenderer& cv_renderer,
const DlRenderer& dl_renderer,
const DisplayListAttributeFlags& flags)
: cv_renderer_(cv_renderer), dl_renderer_(dl_renderer), flags_(flags) {}
bool uses_paint() const { return !flags_.ignores_paint(); }
bool should_match(const RenderEnvironment& env,
const DisplayListBuilder& attr,
const SkMatrix& matrix,
const SkIRect& device_clip,
bool has_diff_clip,
bool has_mutating_save_layer) const {
if (has_mutating_save_layer) {
return false;
}
if (env.ref_clip_bounds() != device_clip || has_diff_clip) {
return false;
}
if (env.ref_matrix() != matrix && !flags_.is_flood()) {
return false;
}
if (flags_.ignores_paint()) {
return true;
}
const DisplayListBuilder& ref_attr = env.ref_attr();
if (flags_.applies_anti_alias() && //
ref_attr.isAntiAlias() != attr.isAntiAlias()) {
return false;
}
if (flags_.applies_dither() && //
ref_attr.isDither() != attr.isDither()) {
return false;
}
if (flags_.applies_color() && //
ref_attr.getColor() != attr.getColor()) {
return false;
}
if (flags_.applies_blend() && //
ref_attr.getBlender() != attr.getBlender()) {
return false;
}
if (flags_.applies_color_filter() && //
(ref_attr.isInvertColors() != attr.isInvertColors() ||
NotEquals(ref_attr.getColorFilter(), attr.getColorFilter()))) {
return false;
}
if (flags_.applies_mask_filter() && //
NotEquals(ref_attr.getMaskFilter(), attr.getMaskFilter())) {
return false;
}
if (flags_.applies_image_filter() && //
ref_attr.getImageFilter() != attr.getImageFilter()) {
return false;
}
if (flags_.applies_shader() && //
NotEquals(ref_attr.getColorSource(), attr.getColorSource())) {
return false;
}
DisplayListSpecialGeometryFlags geo_flags =
flags_.WithPathEffect(attr.getPathEffect().get());
if (flags_.applies_path_effect() && //
ref_attr.getPathEffect() != attr.getPathEffect()) {
if (attr.getPathEffect()->asDash() == nullptr) {
return false;
}
if (!ignores_dashes()) {
return false;
}
}
bool is_stroked = flags_.is_stroked(ref_attr.getStyle());
if (flags_.is_stroked(attr.getStyle()) != is_stroked) {
return false;
}
if (!is_stroked) {
return true;
}
if (ref_attr.getStrokeWidth() != attr.getStrokeWidth()) {
return false;
}
if (geo_flags.may_have_end_caps() && //
getCap(ref_attr, geo_flags) != getCap(attr, geo_flags)) {
return false;
}
if (geo_flags.may_have_joins()) {
if (ref_attr.getStrokeJoin() != attr.getStrokeJoin()) {
return false;
}
if (ref_attr.getStrokeJoin() == DlStrokeJoin::kMiter) {
SkScalar ref_miter = ref_attr.getStrokeMiter();
SkScalar test_miter = attr.getStrokeMiter();
// miter limit < 1.4 affects right angles
if (geo_flags.may_have_acute_joins() || //
ref_miter < 1.4 || test_miter < 1.4) {
if (ref_miter != test_miter) {
return false;
}
}
}
}
return true;
}
DlStrokeCap getCap(const DisplayListBuilder& attr,
DisplayListSpecialGeometryFlags geo_flags) const {
DlStrokeCap cap = attr.getStrokeCap();
if (geo_flags.butt_cap_becomes_square() && cap == DlStrokeCap::kButt) {
return DlStrokeCap::kSquare;
}
return cap;
}
const BoundsTolerance adjust(const BoundsTolerance& tolerance,
const SkPaint& paint,
const SkMatrix& matrix) const {
if (is_draw_text_blob() && tolerance.discrete_offset() > 0) {
// drawTextBlob needs just a little more leeway when using a
// discrete path effect.
return tolerance.addBoundsPadding(2, 2);
}
if (is_draw_line()) {
return lineAdjust(tolerance, paint, matrix);
}
if (is_draw_arc_center()) {
if (paint.getStyle() != SkPaint::kFill_Style &&
paint.getStrokeJoin() == SkPaint::kMiter_Join) {
// the miter join at the center of an arc does not really affect
// its bounds in any of our test cases, but the bounds code needs
// to take it into account for the cases where it might, so we
// relax our tolerance to reflect the miter bounds padding.
SkScalar miter_pad =
paint.getStrokeMiter() * paint.getStrokeWidth() * 0.5f;
return tolerance.addBoundsPadding(miter_pad, miter_pad);
}
}
return tolerance;
}
const BoundsTolerance lineAdjust(const BoundsTolerance& tolerance,
const SkPaint& paint,
const SkMatrix& matrix) const {
SkScalar adjust = 0.0;
SkScalar half_width = paint.getStrokeWidth() * 0.5f;
if (tolerance.discrete_offset() > 0) {
// When a discrete path effect is added, the bounds calculations must
// allow for miters in any direction, but a horizontal line will not
// have miters in the horizontal direction, similarly for vertical
// lines, and diagonal lines will have miters off at a "45 degree"
// angle that don't expand the bounds much at all.
// Also, the discrete offset will not move any points parallel with
// the line, so provide tolerance for both miters and offset.
adjust =
half_width * paint.getStrokeMiter() + tolerance.discrete_offset();
}
auto paint_effect = paint.refPathEffect();
DisplayListSpecialGeometryFlags geo_flags =
flags_.WithPathEffect(DlPathEffect::From(paint.refPathEffect()).get());
if (paint.getStrokeCap() == SkPaint::kButt_Cap &&
!geo_flags.butt_cap_becomes_square()) {
adjust = std::max(adjust, half_width);
}
if (adjust == 0) {
return tolerance;
}
SkScalar hTolerance;
SkScalar vTolerance;
if (is_horizontal_line()) {
FML_DCHECK(!is_vertical_line());
hTolerance = adjust;
vTolerance = 0;
} else if (is_vertical_line()) {
hTolerance = 0;
vTolerance = adjust;
} else {
// The perpendicular miters just do not impact the bounds of
// diagonal lines at all as they are aimed in the wrong direction
// to matter. So allow tolerance in both axes.
hTolerance = vTolerance = adjust;
}
BoundsTolerance new_tolerance =
tolerance.addBoundsPadding(hTolerance, vTolerance);
return new_tolerance;
}
const CvRenderer& cv_renderer() const { return cv_renderer_; }
void render_to(SkCanvas* canvas, SkPaint& paint) const {
cv_renderer_(canvas, paint);
}
const DlRenderer& dl_renderer() const { return dl_renderer_; }
void render_to(DisplayListBuilder& builder) const { //
dl_renderer_(builder);
}
// If a test is using any shadow operations then we cannot currently
// record those in an SkCanvas and play it back into a DisplayList
// because internally the operation gets encapsulated in a Skia
// ShadowRec which is not exposed by their headers. For operations
// that use shadows, we can perform a lot of tests, but not the tests
// that require SkCanvas->DisplayList transfers.
// See: https://bugs.chromium.org/p/skia/issues/detail?id=12125
bool is_draw_shadows() const { return is_draw_shadows_; }
// Tests that call drawTextBlob with an sk_ref paint attribute will cause
// those attributes to be stored in an internal Skia cache so we need
// to expect that the |sk_ref.unique()| call will fail in those cases.
// See: (TBD(flar) - file Skia bug)
bool is_draw_text_blob() const { return is_draw_text_blob_; }
bool is_draw_display_list() const { return is_draw_display_list_; }
bool is_draw_line() const { return is_draw_line_; }
bool is_draw_arc_center() const { return is_draw_arc_center_; }
bool is_horizontal_line() const { return is_horizontal_line_; }
bool is_vertical_line() const { return is_vertical_line_; }
bool ignores_dashes() const { return ignores_dashes_; }
TestParameters& set_draw_shadows() {
is_draw_shadows_ = true;
return *this;
}
TestParameters& set_draw_text_blob() {
is_draw_text_blob_ = true;
return *this;
}
TestParameters& set_draw_display_list() {
is_draw_display_list_ = true;
return *this;
}
TestParameters& set_draw_line() {
is_draw_line_ = true;
return *this;
}
TestParameters& set_draw_arc_center() {
is_draw_arc_center_ = true;
return *this;
}
TestParameters& set_ignores_dashes() {
ignores_dashes_ = true;
return *this;
}
TestParameters& set_horizontal_line() {
is_horizontal_line_ = true;
return *this;
}
TestParameters& set_vertical_line() {
is_vertical_line_ = true;
return *this;
}
private:
const CvRenderer& cv_renderer_;
const DlRenderer& dl_renderer_;
const DisplayListAttributeFlags& flags_;
bool is_draw_shadows_ = false;
bool is_draw_text_blob_ = false;
bool is_draw_display_list_ = false;
bool is_draw_line_ = false;
bool is_draw_arc_center_ = false;
bool ignores_dashes_ = false;
bool is_horizontal_line_ = false;
bool is_vertical_line_ = false;
};
class CaseParameters {
public:
explicit CaseParameters(std::string info)
: CaseParameters(info, EmptyCvRenderer, EmptyDlRenderer) {}
CaseParameters(std::string info, CvSetup cv_setup, DlRenderer dl_setup)
: CaseParameters(info,
cv_setup,
dl_setup,
EmptyCvRenderer,
EmptyDlRenderer,
SK_ColorTRANSPARENT,
false,
false,
false) {}
CaseParameters(std::string info,
CvSetup cv_setup,
DlRenderer dl_setup,
CvRenderer cv_restore,
DlRenderer dl_restore,
DlColor bg,
bool has_diff_clip,
bool has_mutating_save_layer,
bool fuzzy_compare_components)
: info_(info),
bg_(bg),
cv_setup_(cv_setup),
dl_setup_(dl_setup),
cv_restore_(cv_restore),
dl_restore_(dl_restore),
has_diff_clip_(has_diff_clip),
has_mutating_save_layer_(has_mutating_save_layer),
fuzzy_compare_components_(fuzzy_compare_components) {}
CaseParameters with_restore(CvRenderer cv_restore,
DlRenderer dl_restore,
bool mutating_layer,
bool fuzzy_compare_components = false) {
return CaseParameters(info_, cv_setup_, dl_setup_, cv_restore, dl_restore,
bg_, has_diff_clip_, mutating_layer,
fuzzy_compare_components);
}
CaseParameters with_bg(DlColor bg) {
return CaseParameters(info_, cv_setup_, dl_setup_, cv_restore_, dl_restore_,
bg, has_diff_clip_, has_mutating_save_layer_,
fuzzy_compare_components_);
}
CaseParameters with_diff_clip() {
return CaseParameters(info_, cv_setup_, dl_setup_, cv_restore_, dl_restore_,
bg_, true, has_mutating_save_layer_,
fuzzy_compare_components_);
}
std::string info() const { return info_; }
DlColor bg() const { return bg_; }
bool has_diff_clip() const { return has_diff_clip_; }
bool has_mutating_save_layer() const { return has_mutating_save_layer_; }
bool fuzzy_compare_components() const { return fuzzy_compare_components_; }
CvSetup cv_setup() const { return cv_setup_; }
DlRenderer dl_setup() const { return dl_setup_; }
CvRenderer cv_restore() const { return cv_restore_; }
DlRenderer dl_restore() const { return dl_restore_; }
const SkPaint render_to(SkCanvas* canvas, //
const TestParameters& testP) const {
SkPaint paint;
cv_setup_(canvas, paint);
testP.render_to(canvas, paint);
cv_restore_(canvas, paint);
return paint;
}
void render_to(DisplayListBuilder& builder,
const TestParameters& testP) const {
dl_setup_(builder);
testP.render_to(builder);
dl_restore_(builder);
}
private:
const std::string info_;
const DlColor bg_;
const CvSetup cv_setup_;
const DlRenderer dl_setup_;
const CvRenderer cv_restore_;
const DlRenderer dl_restore_;
const bool has_diff_clip_;
const bool has_mutating_save_layer_;
const bool fuzzy_compare_components_;
};
class CanvasCompareTester {
public:
static BoundsTolerance DefaultTolerance;
static void RenderAll(const TestParameters& params,
const BoundsTolerance& tolerance = DefaultTolerance) {
RenderEnvironment env = RenderEnvironment::MakeN32();
env.init_ref(params.cv_renderer());
RenderWithTransforms(params, env, tolerance);
RenderWithClips(params, env, tolerance);
RenderWithSaveRestore(params, env, tolerance);
// Only test attributes if the canvas version uses the paint object
if (params.uses_paint()) {
RenderWithAttributes(params, env, tolerance);
}
}
static void RenderWithSaveRestore(const TestParameters& testP,
const RenderEnvironment& env,
const BoundsTolerance& tolerance) {
SkRect clip =
SkRect::MakeXYWH(kRenderCenterX - 1, kRenderCenterY - 1, 2, 2);
SkRect rect = SkRect::MakeXYWH(kRenderCenterX, kRenderCenterY, 10, 10);
DlColor alpha_layer_color = DlColor::kCyan().withAlpha(0x7f);
DlColor default_color = DlPaint::kDefaultColor;
CvRenderer cv_safe_restore = [=](SkCanvas* cv, const SkPaint& p) {
// Draw another primitive to disable peephole optimizations
cv->drawRect(kRenderBounds.makeOffset(500, 500), p);
cv->restore();
};
DlRenderer dl_safe_restore = [=](DisplayListBuilder& b) {
// Draw another primitive to disable peephole optimizations
b.drawRect(kRenderBounds.makeOffset(500, 500));
b.restore();
};
CvRenderer cv_opt_restore = [=](SkCanvas* cv, const SkPaint& p) {
// Just a simple restore to allow peephole optimizations to occur
cv->restore();
};
DlRenderer dl_opt_restore = [=](DisplayListBuilder& b) {
// Just a simple restore to allow peephole optimizations to occur
b.restore();
};
SkRect layer_bounds = kRenderBounds.makeInset(15, 15);
RenderWith(testP, env, tolerance,
CaseParameters(
"With prior save/clip/restore",
[=](SkCanvas* cv, SkPaint& p) {
cv->save();
cv->clipRect(clip, SkClipOp::kIntersect, false);
SkPaint p2;
cv->drawRect(rect, p2);
p2.setBlendMode(SkBlendMode::kClear);
cv->drawRect(rect, p2);
cv->restore();
},
[=](DisplayListBuilder& b) {
b.save();
b.clipRect(clip, SkClipOp::kIntersect, false);
b.drawRect(rect);
b.setBlendMode(DlBlendMode::kClear);
b.drawRect(rect);
b.setBlendMode(DlBlendMode::kSrcOver);
b.restore();
}));
RenderWith(testP, env, tolerance,
CaseParameters(
"saveLayer no paint, no bounds",
[=](SkCanvas* cv, SkPaint& p) { //
cv->saveLayer(nullptr, nullptr);
},
[=](DisplayListBuilder& b) { //
b.saveLayer(nullptr, false);
})
.with_restore(cv_safe_restore, dl_safe_restore, false));
RenderWith(testP, env, tolerance,
CaseParameters(
"saveLayer no paint, with bounds",
[=](SkCanvas* cv, SkPaint& p) { //
cv->saveLayer(layer_bounds, nullptr);
},
[=](DisplayListBuilder& b) { //
b.saveLayer(&layer_bounds, false);
})
.with_restore(cv_safe_restore, dl_safe_restore, true));
RenderWith(testP, env, tolerance,
CaseParameters(
"saveLayer with alpha, no bounds",
[=](SkCanvas* cv, SkPaint& p) {
SkPaint save_p;
save_p.setColor(alpha_layer_color);
cv->saveLayer(nullptr, &save_p);
},
[=](DisplayListBuilder& b) {
b.setColor(alpha_layer_color);
b.saveLayer(nullptr, true);
b.setColor(default_color);
})
.with_restore(cv_safe_restore, dl_safe_restore, true));
RenderWith(testP, env, tolerance,
CaseParameters(
"saveLayer with peephole alpha, no bounds",
[=](SkCanvas* cv, SkPaint& p) {
SkPaint save_p;
save_p.setColor(alpha_layer_color);
cv->saveLayer(nullptr, &save_p);
},
[=](DisplayListBuilder& b) {
b.setColor(alpha_layer_color);
b.saveLayer(nullptr, true);
b.setColor(default_color);
})
.with_restore(cv_opt_restore, dl_opt_restore, true, true));
RenderWith(testP, env, tolerance,
CaseParameters(
"saveLayer with alpha and bounds",
[=](SkCanvas* cv, SkPaint& p) {
SkPaint save_p;
save_p.setColor(alpha_layer_color);
cv->saveLayer(layer_bounds, &save_p);
},
[=](DisplayListBuilder& b) {
b.setColor(alpha_layer_color);
b.saveLayer(&layer_bounds, true);
b.setColor(default_color);
})
.with_restore(cv_safe_restore, dl_safe_restore, true));
{
// Being able to see a backdrop blur requires a non-default background
// so we create a new environment for these tests that has a checkerboard
// background that can be blurred by the backdrop filter. We also want
// to avoid the rendered primitive from obscuring the blurred background
// so we set an alpha value which works for all primitives except for
// drawColor which can override the alpha with its color, but it now uses
// a non-opaque color to avoid that problem.
RenderEnvironment backdrop_env = RenderEnvironment::MakeN32();
CvSetup cv_backdrop_setup = [=](SkCanvas* cv, SkPaint& p) {
SkPaint setup_p;
setup_p.setShader(kTestImageColorSource.skia_object());
cv->drawPaint(setup_p);
p.setAlpha(p.getAlpha() / 2);
};
DlRenderer dl_backdrop_setup = [=](DisplayListBuilder& b) {
b.setColorSource(&kTestImageColorSource);
b.drawPaint();
b.setColorSource(nullptr);
DlColor current_color = b.getColor();
b.setColor(current_color.withAlpha(current_color.getAlpha() / 2));
};
backdrop_env.init_ref(cv_backdrop_setup, testP.cv_renderer(),
dl_backdrop_setup);
DlBlurImageFilter backdrop(5, 5, DlTileMode::kDecal);
RenderWith(testP, backdrop_env, tolerance,
CaseParameters(
"saveLayer with backdrop",
[=](SkCanvas* cv, SkPaint& p) {
cv_backdrop_setup(cv, p);
cv->saveLayer(SkCanvas::SaveLayerRec(
nullptr, nullptr, backdrop.skia_object().get(), 0));
},
[=](DisplayListBuilder& b) {
dl_backdrop_setup(b);
b.saveLayer(nullptr, SaveLayerOptions::kNoAttributes,
&backdrop);
})
.with_restore(cv_safe_restore, dl_safe_restore, true));
RenderWith(
testP, backdrop_env, tolerance,
CaseParameters(
"saveLayer with bounds and backdrop",
[=](SkCanvas* cv, SkPaint& p) {
cv_backdrop_setup(cv, p);
cv->saveLayer(SkCanvas::SaveLayerRec(
&layer_bounds, nullptr, backdrop.skia_object().get(), 0));
},
[=](DisplayListBuilder& b) {
dl_backdrop_setup(b);
b.saveLayer(&layer_bounds, SaveLayerOptions::kNoAttributes,
&backdrop);
})
.with_restore(cv_safe_restore, dl_safe_restore, true));
RenderWith(testP, backdrop_env, tolerance,
CaseParameters(
"clipped saveLayer with backdrop",
[=](SkCanvas* cv, SkPaint& p) {
cv_backdrop_setup(cv, p);
cv->clipRect(layer_bounds);
cv->saveLayer(SkCanvas::SaveLayerRec(
nullptr, nullptr, backdrop.skia_object().get(), 0));
},
[=](DisplayListBuilder& b) {
dl_backdrop_setup(b);
b.clipRect(layer_bounds, SkClipOp::kIntersect, false);
b.saveLayer(nullptr, SaveLayerOptions::kNoAttributes,
&backdrop);
})
.with_restore(cv_safe_restore, dl_safe_restore, true));
}
{
// clang-format off
constexpr float rotate_alpha_color_matrix[20] = {
0, 1, 0, 0 , 0,
0, 0, 1, 0 , 0,
1, 0, 0, 0 , 0,
0, 0, 0, 0.5, 0,
};
// clang-format on
DlMatrixColorFilter filter(rotate_alpha_color_matrix);
{
RenderWith(testP, env, tolerance,
CaseParameters(
"saveLayer ColorFilter, no bounds",
[=](SkCanvas* cv, SkPaint& p) {
SkPaint save_p;
save_p.setColorFilter(filter.skia_object());
cv->saveLayer(nullptr, &save_p);
p.setStrokeWidth(5.0);
},
[=](DisplayListBuilder& b) {
b.setColorFilter(&filter);
b.saveLayer(nullptr, true);
b.setColorFilter(nullptr);
b.setStrokeWidth(5.0);
})
.with_restore(cv_safe_restore, dl_safe_restore, true));
}
{
RenderWith(testP, env, tolerance,
CaseParameters(
"saveLayer ColorFilter and bounds",
[=](SkCanvas* cv, SkPaint& p) {
SkPaint save_p;
save_p.setColorFilter(filter.skia_object());
cv->saveLayer(kRenderBounds, &save_p);
p.setStrokeWidth(5.0);
},
[=](DisplayListBuilder& b) {
b.setColorFilter(&filter);
b.saveLayer(&kRenderBounds, true);
b.setColorFilter(nullptr);
b.setStrokeWidth(5.0);
})
.with_restore(cv_safe_restore, dl_safe_restore, true));
}
}
{
sk_sp<SkImageFilter> sk_filter = SkImageFilters::Arithmetic(
0.1, 0.1, 0.1, 0.25, true, nullptr, nullptr);
DlUnknownImageFilter filter(sk_filter);
{
RenderWith(testP, env, tolerance,
CaseParameters(
"saveLayer ImageFilter, no bounds",
[=](SkCanvas* cv, SkPaint& p) {
SkPaint save_p;
save_p.setImageFilter(filter.skia_object());
cv->saveLayer(nullptr, &save_p);
p.setStrokeWidth(5.0);
},
[=](DisplayListBuilder& b) {
b.setImageFilter(&filter);
b.saveLayer(nullptr, true);
b.setImageFilter(nullptr);
b.setStrokeWidth(5.0);
})
.with_restore(cv_safe_restore, dl_safe_restore, true));
}
{
RenderWith(testP, env, tolerance,
CaseParameters(
"saveLayer ImageFilter and bounds",
[=](SkCanvas* cv, SkPaint& p) {
SkPaint save_p;
save_p.setImageFilter(filter.skia_object());
cv->saveLayer(kRenderBounds, &save_p);
p.setStrokeWidth(5.0);
},
[=](DisplayListBuilder& b) {
b.setImageFilter(&filter);
b.saveLayer(&kRenderBounds, true);
b.setImageFilter(nullptr);
b.setStrokeWidth(5.0);
})
.with_restore(cv_safe_restore, dl_safe_restore, true));
}
}
}
static void RenderWithAttributes(const TestParameters& testP,
const RenderEnvironment& env,
const BoundsTolerance& tolerance) {
RenderWith(testP, env, tolerance, CaseParameters("Defaults Test"));
{
// CPU renderer with default line width of 0 does not show antialiasing
// for stroked primitives, so we make a new reference with a non-trivial
// stroke width to demonstrate the differences
RenderEnvironment aa_env = RenderEnvironment::MakeN32();
// Tweak the bounds tolerance for the displacement of 1/10 of a pixel
const BoundsTolerance aa_tolerance = tolerance.addBoundsPadding(1, 1);
CvSetup cv_aa_setup = [=](SkCanvas* cv, SkPaint& p) {
cv->translate(0.1, 0.1);
p.setStrokeWidth(5.0);
};
DlRenderer dl_aa_setup = [=](DisplayListBuilder& b) {
b.translate(0.1, 0.1);
b.setStrokeWidth(5.0);