forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaint_filter.cc
1651 lines (1474 loc) · 64.8 KB
/
paint_filter.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 2017 The Chromium 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 "cc/paint/paint_filter.h"
#include <string>
#include <utility>
#include <vector>
#include "base/memory/values_equivalent.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/stl_util.h"
#include "build/build_config.h"
#include "cc/paint/draw_image.h"
#include "cc/paint/filter_operations.h"
#include "cc/paint/image_provider.h"
#include "cc/paint/paint_image_builder.h"
#include "cc/paint/paint_op_writer.h"
#include "cc/paint/paint_record.h"
#include "cc/paint/scoped_raster_flags.h"
#include "third_party/skia/include/core/SkColorFilter.h"
#include "third_party/skia/include/core/SkColorSpace.h"
#include "third_party/skia/include/core/SkMath.h"
#include "third_party/skia/include/core/SkString.h"
#include "third_party/skia/include/effects/SkImageFilters.h"
#include "third_party/skia/include/effects/SkPerlinNoiseShader.h"
#include "third_party/skia/include/effects/SkRuntimeEffect.h"
#include "third_party/skia/src/effects/imagefilters/SkRuntimeImageFilter.h"
namespace cc {
namespace {
const bool kHasNoDiscardableImages = false;
#if BUILDFLAG(IS_ANDROID)
struct StretchShaderUniforms {
// multiplier to apply to scale effect
float uMaxStretchIntensity;
// Maximum percentage to stretch beyond bounds of target
float uStretchAffectedDistX;
float uStretchAffectedDistY;
// Distance stretched as a function of the normalized overscroll times
// scale intensity
float uDistanceStretchedX;
float uDistanceStretchedY;
float uInverseDistanceStretchedX;
float uInverseDistanceStretchedY;
float uDistDiffX;
// Difference between the peak stretch amount and overscroll amount normalized
float uDistDiffY;
// Horizontal offset represented as a ratio of pixels divided by the target
// width
float uScrollX;
// Vertical offset represented as a ratio of pixels divided by the target
// height
float uScrollY;
// Normalized overscroll amount in the horizontal direction
float uOverscrollX;
// Normalized overscroll amount in the vertical direction
float uOverscrollY;
float viewportWidth; // target height in pixels
float viewportHeight; // target width in pixels
// uInterpolationStrength is the intensity of the interpolation.
// if uInterpolationStrength is 0, then the stretch is constant for all the
// uStretchAffectedDist. if uInterpolationStrength is 1, then stretch
// intensity is interpolated based on the pixel position in the
// uStretchAffectedDist area; The closer we are from the scroll anchor point,
// the more it stretches, and the other way around.
float uInterpolationStrength;
};
const char* kStretchShader = R"(
uniform shader uContentTexture;
// multiplier to apply to scale effect
uniform float uMaxStretchIntensity;
// Maximum percentage to stretch beyond bounds of target
uniform float uStretchAffectedDistX;
uniform float uStretchAffectedDistY;
// Distance stretched as a function of the normalized overscroll times
// scale intensity
uniform float uDistanceStretchedX;
uniform float uDistanceStretchedY;
uniform float uInverseDistanceStretchedX;
uniform float uInverseDistanceStretchedY;
uniform float uDistDiffX;
// Difference between the peak stretch amount and overscroll amount
// normalized
uniform float uDistDiffY;
// Horizontal offset represented as a ratio of pixels divided by the target
// width
uniform float uScrollX;
// Vertical offset represented as a ratio of pixels divided by the target
// height
uniform float uScrollY;
// Normalized overscroll amount in the horizontal direction
uniform float uOverscrollX;
// Normalized overscroll amount in the vertical direction
uniform float uOverscrollY;
uniform float viewportWidth; // target height in pixels
uniform float viewportHeight; // target width in pixels
// uInterpolationStrength is the intensity of the interpolation.
// if uInterpolationStrength is 0, then the stretch is constant for all the
// uStretchAffectedDist. if uInterpolationStrength is 1, then stretch
// intensity is interpolated based on the pixel position in the
// uStretchAffectedDist area; The closer we are from the scroll anchor
// point, the more it stretches, and the other way around.
uniform float uInterpolationStrength;
float easeInCubic(float t, float d) {
float tmp = t * d;
return tmp * tmp * tmp;
}
float computeOverscrollStart(
float inPos,
float overscroll,
float uStretchAffectedDist,
float uInverseStretchAffectedDist,
float distanceStretched,
float interpolationStrength
) {
float offsetPos = uStretchAffectedDist - inPos;
float posBasedVariation = mix(
1. ,easeInCubic(offsetPos, uInverseStretchAffectedDist),
interpolationStrength);
float stretchIntensity = overscroll * posBasedVariation;
return distanceStretched - (offsetPos / (1. + stretchIntensity));
}
float computeOverscrollEnd(
float inPos,
float overscroll,
float reverseStretchDist,
float uStretchAffectedDist,
float uInverseStretchAffectedDist,
float distanceStretched,
float interpolationStrength
) {
float offsetPos = inPos - reverseStretchDist;
float posBasedVariation = mix(
1. ,easeInCubic(offsetPos, uInverseStretchAffectedDist),
interpolationStrength);
float stretchIntensity = (-overscroll) * posBasedVariation;
return 1 - (distanceStretched - (offsetPos / (1. + stretchIntensity)));
}
// Prefer usage of return values over out parameters as it enables
// SKSL to properly inline method calls and works around potential GPU
// driver issues on Wembly. See b/182566543 for details
float computeOverscroll(
float inPos,
float overscroll,
float uStretchAffectedDist,
float uInverseStretchAffectedDist,
float distanceStretched,
float distanceDiff,
float interpolationStrength
) {
float outPos = inPos;
if (overscroll > 0) {
if (inPos <= uStretchAffectedDist) {
outPos = computeOverscrollStart(
inPos,
overscroll,
uStretchAffectedDist,
uInverseStretchAffectedDist,
distanceStretched,
interpolationStrength
);
} else if (inPos >= distanceStretched) {
outPos = distanceDiff + inPos;
}
}
if (overscroll < 0) {
float stretchAffectedDist = 1. - uStretchAffectedDist;
if (inPos >= stretchAffectedDist) {
outPos = computeOverscrollEnd(
inPos,
overscroll,
stretchAffectedDist,
uStretchAffectedDist,
uInverseStretchAffectedDist,
distanceStretched,
interpolationStrength
);
} else if (inPos < stretchAffectedDist) {
outPos = -distanceDiff + inPos;
}
}
return outPos;
}
vec4 main(vec2 coord) {
// Normalize SKSL pixel coordinate into a unit vector
float inU = coord.x / viewportWidth;
float inV = coord.y / viewportHeight;
float outU;
float outV;
float stretchIntensity;
// Add the normalized scroll position within scrolling list
inU += uScrollX;
inV += uScrollY;
outU = inU;
outV = inV;
outU = computeOverscroll(
inU,
uOverscrollX,
uStretchAffectedDistX,
uInverseDistanceStretchedX,
uDistanceStretchedX,
uDistDiffX,
uInterpolationStrength
);
outV = computeOverscroll(
inV,
uOverscrollY,
uStretchAffectedDistY,
uInverseDistanceStretchedY,
uDistanceStretchedY,
uDistDiffY,
uInterpolationStrength
);
coord.x = outU * viewportWidth;
coord.y = outV * viewportHeight;
return uContentTexture.eval(coord);
})";
static const float CONTENT_DISTANCE_STRETCHED = 1.f;
static const float INTERPOLATION_STRENGTH_VALUE = 0.7f;
sk_sp<SkRuntimeEffect> getStretchEffect() {
static base::NoDestructor<SkRuntimeEffect::Result> effect(
SkRuntimeEffect::MakeForShader(SkString(kStretchShader)));
return effect->effect;
}
#endif
bool AreScalarsEqual(SkScalar one, SkScalar two) {
return PaintOp::AreEqualEvenIfNaN(one, two);
}
bool HasDiscardableImages(const sk_sp<PaintFilter>& filter) {
return filter ? filter->has_discardable_images() : false;
}
bool HasDiscardableImages(const sk_sp<PaintFilter>* const filters, int count) {
for (int i = 0; i < count; ++i) {
if (filters[i] && filters[i]->has_discardable_images())
return true;
}
return false;
}
sk_sp<PaintFilter> Snapshot(const sk_sp<PaintFilter>& filter,
ImageProvider* image_provider) {
if (!filter)
return nullptr;
return filter->SnapshotWithImages(image_provider);
}
} // namespace
PaintFilter::PaintFilter(Type type,
const CropRect* crop_rect,
bool has_discardable_images)
: type_(type), has_discardable_images_(has_discardable_images) {
if (crop_rect)
crop_rect_.emplace(*crop_rect);
}
PaintFilter::~PaintFilter() = default;
// static
std::string PaintFilter::TypeToString(Type type) {
switch (type) {
case Type::kNullFilter:
return "kNullFilter";
case Type::kColorFilter:
return "kColorFilter";
case Type::kBlur:
return "kBlur";
case Type::kDropShadow:
return "kDropShadow";
case Type::kMagnifier:
return "kMagnifier";
case Type::kCompose:
return "kCompose";
case Type::kAlphaThreshold:
return "kAlphaThreshold";
case Type::kXfermode:
return "kXfermode";
case Type::kArithmetic:
return "kArithmetic";
case Type::kMatrixConvolution:
return "kMatrixConvolution";
case Type::kDisplacementMapEffect:
return "kDisplacementMapEffect";
case Type::kImage:
return "kImage";
case Type::kPaintRecord:
return "kPaintRecord";
case Type::kMerge:
return "kMerge";
case Type::kMorphology:
return "kMorphology";
case Type::kOffset:
return "kOffset";
case Type::kTile:
return "kTile";
case Type::kTurbulence:
return "kTurbulence";
case Type::kShader:
return "kShader";
case Type::kMatrix:
return "kMatrix";
case Type::kLightingDistant:
return "kLightingDistant";
case Type::kLightingPoint:
return "kLightingPoint";
case Type::kLightingSpot:
return "kLightingSpot";
case Type::kStretch:
return "kStretch";
}
NOTREACHED();
return "Unknown";
}
const PaintFilter::CropRect* PaintFilter::GetCropRect() const {
return base::OptionalOrNullptr(crop_rect_);
}
size_t PaintFilter::GetFilterSize(const PaintFilter* filter) {
// A null type is used to indicate no filter.
if (!filter)
return sizeof(uint32_t);
return filter->SerializedSize() + PaintOpWriter::Alignment();
}
size_t PaintFilter::BaseSerializedSize() const {
size_t total_size = 0u;
// Filter type.
total_size += sizeof(uint32_t);
// Bool to indicate whether crop exists.
total_size += sizeof(uint32_t);
if (crop_rect_) {
// CropRect.
total_size += sizeof(*crop_rect_);
}
return total_size;
}
sk_sp<PaintFilter> PaintFilter::SnapshotWithImages(
ImageProvider* image_provider) const {
if (!has_discardable_images_)
return sk_ref_sp<PaintFilter>(this);
return SnapshotWithImagesInternal(image_provider);
}
bool PaintFilter::operator==(const PaintFilter& other) const {
if (type_ != other.type_)
return false;
if (!!crop_rect_ != !!other.crop_rect_)
return false;
if (crop_rect_) {
if (!PaintOp::AreSkRectsEqual(*crop_rect_, *other.crop_rect_)) {
return false;
}
}
switch (type_) {
case Type::kNullFilter:
return true;
case Type::kColorFilter:
return *static_cast<const ColorFilterPaintFilter*>(this) ==
static_cast<const ColorFilterPaintFilter&>(other);
case Type::kBlur:
return *static_cast<const BlurPaintFilter*>(this) ==
static_cast<const BlurPaintFilter&>(other);
case Type::kDropShadow:
return *static_cast<const DropShadowPaintFilter*>(this) ==
static_cast<const DropShadowPaintFilter&>(other);
case Type::kMagnifier:
return *static_cast<const MagnifierPaintFilter*>(this) ==
static_cast<const MagnifierPaintFilter&>(other);
case Type::kCompose:
return *static_cast<const ComposePaintFilter*>(this) ==
static_cast<const ComposePaintFilter&>(other);
case Type::kAlphaThreshold:
return *static_cast<const AlphaThresholdPaintFilter*>(this) ==
static_cast<const AlphaThresholdPaintFilter&>(other);
case Type::kXfermode:
return *static_cast<const XfermodePaintFilter*>(this) ==
static_cast<const XfermodePaintFilter&>(other);
case Type::kArithmetic:
return *static_cast<const ArithmeticPaintFilter*>(this) ==
static_cast<const ArithmeticPaintFilter&>(other);
case Type::kMatrixConvolution:
return *static_cast<const MatrixConvolutionPaintFilter*>(this) ==
static_cast<const MatrixConvolutionPaintFilter&>(other);
case Type::kDisplacementMapEffect:
return *static_cast<const DisplacementMapEffectPaintFilter*>(this) ==
static_cast<const DisplacementMapEffectPaintFilter&>(other);
case Type::kImage:
return *static_cast<const ImagePaintFilter*>(this) ==
static_cast<const ImagePaintFilter&>(other);
case Type::kPaintRecord:
return *static_cast<const RecordPaintFilter*>(this) ==
static_cast<const RecordPaintFilter&>(other);
case Type::kMerge:
return *static_cast<const MergePaintFilter*>(this) ==
static_cast<const MergePaintFilter&>(other);
case Type::kMorphology:
return *static_cast<const MorphologyPaintFilter*>(this) ==
static_cast<const MorphologyPaintFilter&>(other);
case Type::kOffset:
return *static_cast<const OffsetPaintFilter*>(this) ==
static_cast<const OffsetPaintFilter&>(other);
case Type::kTile:
return *static_cast<const TilePaintFilter*>(this) ==
static_cast<const TilePaintFilter&>(other);
case Type::kTurbulence:
return *static_cast<const TurbulencePaintFilter*>(this) ==
static_cast<const TurbulencePaintFilter&>(other);
case Type::kShader:
return *static_cast<const ShaderPaintFilter*>(this) ==
static_cast<const ShaderPaintFilter&>(other);
case Type::kMatrix:
return *static_cast<const MatrixPaintFilter*>(this) ==
static_cast<const MatrixPaintFilter&>(other);
case Type::kLightingDistant:
return *static_cast<const LightingDistantPaintFilter*>(this) ==
static_cast<const LightingDistantPaintFilter&>(other);
case Type::kLightingPoint:
return *static_cast<const LightingPointPaintFilter*>(this) ==
static_cast<const LightingPointPaintFilter&>(other);
case Type::kLightingSpot:
return *static_cast<const LightingSpotPaintFilter*>(this) ==
static_cast<const LightingSpotPaintFilter&>(other);
case Type::kStretch:
return *static_cast<const StretchPaintFilter*>(this) ==
static_cast<const StretchPaintFilter&>(other);
}
NOTREACHED();
return true;
}
ColorFilterPaintFilter::ColorFilterPaintFilter(
sk_sp<SkColorFilter> color_filter,
sk_sp<PaintFilter> input,
const CropRect* crop_rect)
: PaintFilter(kType, crop_rect, HasDiscardableImages(input)),
color_filter_(std::move(color_filter)),
input_(std::move(input)) {
DCHECK(color_filter_);
cached_sk_filter_ = SkImageFilters::ColorFilter(
color_filter_, GetSkFilter(input_.get()), crop_rect);
}
ColorFilterPaintFilter::~ColorFilterPaintFilter() = default;
size_t ColorFilterPaintFilter::SerializedSize() const {
base::CheckedNumeric<size_t> total_size = 0u;
total_size += BaseSerializedSize();
total_size += PaintOpWriter::GetFlattenableSize(color_filter_.get());
total_size += GetFilterSize(input_.get());
return total_size.ValueOrDefault(0u);
}
sk_sp<PaintFilter> ColorFilterPaintFilter::SnapshotWithImagesInternal(
ImageProvider* image_provider) const {
return sk_make_sp<ColorFilterPaintFilter>(
color_filter_, Snapshot(input_, image_provider), GetCropRect());
}
bool ColorFilterPaintFilter::operator==(
const ColorFilterPaintFilter& other) const {
return PaintOp::AreSkFlattenablesEqual(color_filter_.get(),
other.color_filter_.get()) &&
base::ValuesEquivalent(input_.get(), other.input_.get());
}
BlurPaintFilter::BlurPaintFilter(SkScalar sigma_x,
SkScalar sigma_y,
SkTileMode tile_mode,
sk_sp<PaintFilter> input,
const CropRect* crop_rect)
: PaintFilter(kType, crop_rect, HasDiscardableImages(input)),
sigma_x_(sigma_x),
sigma_y_(sigma_y),
tile_mode_(tile_mode),
input_(std::move(input)) {
cached_sk_filter_ = SkImageFilters::Blur(
sigma_x, sigma_y, tile_mode_, GetSkFilter(input_.get()), crop_rect);
}
BlurPaintFilter::~BlurPaintFilter() = default;
size_t BlurPaintFilter::SerializedSize() const {
base::CheckedNumeric<size_t> total_size =
BaseSerializedSize() + sizeof(sigma_x_) + sizeof(sigma_y_) +
sizeof(tile_mode_);
total_size += GetFilterSize(input_.get());
return total_size.ValueOrDefault(0u);
}
sk_sp<PaintFilter> BlurPaintFilter::SnapshotWithImagesInternal(
ImageProvider* image_provider) const {
return sk_make_sp<BlurPaintFilter>(sigma_x_, sigma_y_, tile_mode_,
Snapshot(input_, image_provider),
GetCropRect());
}
bool BlurPaintFilter::operator==(const BlurPaintFilter& other) const {
return PaintOp::AreEqualEvenIfNaN(sigma_x_, other.sigma_x_) &&
PaintOp::AreEqualEvenIfNaN(sigma_y_, other.sigma_y_) &&
tile_mode_ == other.tile_mode_ &&
base::ValuesEquivalent(input_.get(), other.input_.get());
}
DropShadowPaintFilter::DropShadowPaintFilter(SkScalar dx,
SkScalar dy,
SkScalar sigma_x,
SkScalar sigma_y,
SkColor4f color,
ShadowMode shadow_mode,
sk_sp<PaintFilter> input,
const CropRect* crop_rect)
: PaintFilter(kType, crop_rect, HasDiscardableImages(input)),
dx_(dx),
dy_(dy),
sigma_x_(sigma_x),
sigma_y_(sigma_y),
color_(color),
shadow_mode_(shadow_mode),
input_(std::move(input)) {
if (shadow_mode == ShadowMode::kDrawShadowOnly) {
// TODO(crbug/1308932): Remove toSkColor and make all SkColor4f.
cached_sk_filter_ = SkImageFilters::DropShadowOnly(
dx_, dy_, sigma_x_, sigma_y_, color_.toSkColor(),
GetSkFilter(input_.get()), crop_rect);
} else {
// TODO(crbug/1308932): Remove toSkColor and make all SkColor4f.
cached_sk_filter_ = SkImageFilters::DropShadow(
dx_, dy_, sigma_x_, sigma_y_, color_.toSkColor(),
GetSkFilter(input_.get()), crop_rect);
}
}
DropShadowPaintFilter::~DropShadowPaintFilter() = default;
size_t DropShadowPaintFilter::SerializedSize() const {
base::CheckedNumeric<size_t> total_size =
BaseSerializedSize() + sizeof(dx_) + sizeof(dy_) + sizeof(sigma_x_) +
sizeof(sigma_y_) + sizeof(color_) + sizeof(shadow_mode_);
total_size += GetFilterSize(input_.get());
return total_size.ValueOrDefault(0u);
}
sk_sp<PaintFilter> DropShadowPaintFilter::SnapshotWithImagesInternal(
ImageProvider* image_provider) const {
return sk_make_sp<DropShadowPaintFilter>(
dx_, dy_, sigma_x_, sigma_y_, color_, shadow_mode_,
Snapshot(input_, image_provider), GetCropRect());
}
bool DropShadowPaintFilter::operator==(
const DropShadowPaintFilter& other) const {
return PaintOp::AreEqualEvenIfNaN(dx_, other.dx_) &&
PaintOp::AreEqualEvenIfNaN(dy_, other.dy_) &&
PaintOp::AreEqualEvenIfNaN(sigma_x_, other.sigma_x_) &&
PaintOp::AreEqualEvenIfNaN(sigma_y_, other.sigma_y_) &&
color_ == other.color_ && shadow_mode_ == other.shadow_mode_ &&
base::ValuesEquivalent(input_.get(), other.input_.get());
}
MagnifierPaintFilter::MagnifierPaintFilter(const SkRect& src_rect,
SkScalar inset,
sk_sp<PaintFilter> input,
const CropRect* crop_rect)
: PaintFilter(kType, crop_rect, HasDiscardableImages(input)),
src_rect_(src_rect),
inset_(inset),
input_(std::move(input)) {
cached_sk_filter_ = SkImageFilters::Magnifier(
src_rect_, inset_, GetSkFilter(input_.get()), crop_rect);
}
MagnifierPaintFilter::~MagnifierPaintFilter() = default;
size_t MagnifierPaintFilter::SerializedSize() const {
base::CheckedNumeric<size_t> total_size =
BaseSerializedSize() + sizeof(src_rect_) + sizeof(inset_);
total_size += GetFilterSize(input_.get());
return total_size.ValueOrDefault(0u);
}
sk_sp<PaintFilter> MagnifierPaintFilter::SnapshotWithImagesInternal(
ImageProvider* image_provider) const {
return sk_make_sp<MagnifierPaintFilter>(
src_rect_, inset_, Snapshot(input_, image_provider), GetCropRect());
}
bool MagnifierPaintFilter::operator==(const MagnifierPaintFilter& other) const {
return PaintOp::AreSkRectsEqual(src_rect_, other.src_rect_) &&
PaintOp::AreEqualEvenIfNaN(inset_, other.inset_) &&
base::ValuesEquivalent(input_.get(), other.input_.get());
}
ComposePaintFilter::ComposePaintFilter(sk_sp<PaintFilter> outer,
sk_sp<PaintFilter> inner)
: PaintFilter(Type::kCompose,
nullptr,
HasDiscardableImages(outer) || HasDiscardableImages(inner)),
outer_(std::move(outer)),
inner_(std::move(inner)) {
cached_sk_filter_ = SkImageFilters::Compose(GetSkFilter(outer_.get()),
GetSkFilter(inner_.get()));
}
ComposePaintFilter::~ComposePaintFilter() = default;
size_t ComposePaintFilter::SerializedSize() const {
base::CheckedNumeric<size_t> total_size = BaseSerializedSize();
total_size += GetFilterSize(outer_.get());
total_size += GetFilterSize(inner_.get());
return total_size.ValueOrDefault(0u);
}
sk_sp<PaintFilter> ComposePaintFilter::SnapshotWithImagesInternal(
ImageProvider* image_provider) const {
return sk_make_sp<ComposePaintFilter>(Snapshot(outer_, image_provider),
Snapshot(inner_, image_provider));
}
bool ComposePaintFilter::operator==(const ComposePaintFilter& other) const {
return base::ValuesEquivalent(outer_.get(), other.outer_.get()) &&
base::ValuesEquivalent(inner_.get(), other.inner_.get());
}
AlphaThresholdPaintFilter::AlphaThresholdPaintFilter(const SkRegion& region,
SkScalar inner_min,
SkScalar outer_max,
sk_sp<PaintFilter> input,
const CropRect* crop_rect)
: PaintFilter(kType, crop_rect, HasDiscardableImages(input)),
region_(region),
inner_min_(inner_min),
outer_max_(outer_max),
input_(std::move(input)) {
cached_sk_filter_ = SkImageFilters::AlphaThreshold(
region_, inner_min_, outer_max_, GetSkFilter(input_.get()), crop_rect);
}
AlphaThresholdPaintFilter::~AlphaThresholdPaintFilter() = default;
size_t AlphaThresholdPaintFilter::SerializedSize() const {
size_t region_size = region_.writeToMemory(nullptr);
base::CheckedNumeric<size_t> total_size;
total_size = BaseSerializedSize() + sizeof(uint64_t) +
base::bits::AlignUp(region_size, PaintOpWriter::Alignment()) +
sizeof(inner_min_) + sizeof(outer_max_);
total_size += GetFilterSize(input_.get());
return total_size.ValueOrDefault(0u);
}
sk_sp<PaintFilter> AlphaThresholdPaintFilter::SnapshotWithImagesInternal(
ImageProvider* image_provider) const {
return sk_make_sp<AlphaThresholdPaintFilter>(region_, inner_min_, outer_max_,
Snapshot(input_, image_provider),
GetCropRect());
}
bool AlphaThresholdPaintFilter::operator==(
const AlphaThresholdPaintFilter& other) const {
return region_ == other.region_ &&
PaintOp::AreEqualEvenIfNaN(inner_min_, other.inner_min_) &&
PaintOp::AreEqualEvenIfNaN(outer_max_, other.outer_max_) &&
base::ValuesEquivalent(input_.get(), other.input_.get());
}
XfermodePaintFilter::XfermodePaintFilter(SkBlendMode blend_mode,
sk_sp<PaintFilter> background,
sk_sp<PaintFilter> foreground,
const CropRect* crop_rect)
: PaintFilter(
kType,
crop_rect,
HasDiscardableImages(background) || HasDiscardableImages(foreground)),
blend_mode_(blend_mode),
background_(std::move(background)),
foreground_(std::move(foreground)) {
cached_sk_filter_ =
SkImageFilters::Blend(blend_mode_, GetSkFilter(background_.get()),
GetSkFilter(foreground_.get()), crop_rect);
}
XfermodePaintFilter::~XfermodePaintFilter() = default;
size_t XfermodePaintFilter::SerializedSize() const {
base::CheckedNumeric<size_t> total_size =
BaseSerializedSize() + sizeof(blend_mode_);
total_size += GetFilterSize(background_.get());
total_size += GetFilterSize(foreground_.get());
return total_size.ValueOrDefault(0u);
}
sk_sp<PaintFilter> XfermodePaintFilter::SnapshotWithImagesInternal(
ImageProvider* image_provider) const {
return sk_make_sp<XfermodePaintFilter>(
blend_mode_, Snapshot(background_, image_provider),
Snapshot(foreground_, image_provider), GetCropRect());
}
bool XfermodePaintFilter::operator==(const XfermodePaintFilter& other) const {
return blend_mode_ == other.blend_mode_ &&
base::ValuesEquivalent(background_.get(), other.background_.get()) &&
base::ValuesEquivalent(foreground_.get(), other.foreground_.get());
}
ArithmeticPaintFilter::ArithmeticPaintFilter(float k1,
float k2,
float k3,
float k4,
bool enforce_pm_color,
sk_sp<PaintFilter> background,
sk_sp<PaintFilter> foreground,
const CropRect* crop_rect)
: PaintFilter(
kType,
crop_rect,
HasDiscardableImages(background) || HasDiscardableImages(foreground)),
k1_(k1),
k2_(k2),
k3_(k3),
k4_(k4),
enforce_pm_color_(enforce_pm_color),
background_(std::move(background)),
foreground_(std::move(foreground)) {
cached_sk_filter_ = SkImageFilters::Arithmetic(
k1_, k2_, k3_, k4_, enforce_pm_color_, GetSkFilter(background_.get()),
GetSkFilter(foreground_.get()), crop_rect);
}
ArithmeticPaintFilter::~ArithmeticPaintFilter() = default;
size_t ArithmeticPaintFilter::SerializedSize() const {
base::CheckedNumeric<size_t> total_size =
BaseSerializedSize() + sizeof(k1_) + sizeof(k2_) + sizeof(k3_) +
sizeof(k4_) + sizeof(enforce_pm_color_);
total_size += GetFilterSize(background_.get());
total_size += GetFilterSize(foreground_.get());
return total_size.ValueOrDefault(0u);
}
sk_sp<PaintFilter> ArithmeticPaintFilter::SnapshotWithImagesInternal(
ImageProvider* image_provider) const {
return sk_make_sp<ArithmeticPaintFilter>(
k1_, k2_, k3_, k4_, enforce_pm_color_,
Snapshot(background_, image_provider),
Snapshot(foreground_, image_provider), GetCropRect());
}
bool ArithmeticPaintFilter::operator==(
const ArithmeticPaintFilter& other) const {
return PaintOp::AreEqualEvenIfNaN(k1_, other.k1_) &&
PaintOp::AreEqualEvenIfNaN(k2_, other.k2_) &&
PaintOp::AreEqualEvenIfNaN(k3_, other.k3_) &&
PaintOp::AreEqualEvenIfNaN(k4_, other.k4_) &&
enforce_pm_color_ == other.enforce_pm_color_ &&
base::ValuesEquivalent(background_.get(), other.background_.get()) &&
base::ValuesEquivalent(foreground_.get(), other.foreground_.get());
}
MatrixConvolutionPaintFilter::MatrixConvolutionPaintFilter(
const SkISize& kernel_size,
const SkScalar* kernel,
SkScalar gain,
SkScalar bias,
const SkIPoint& kernel_offset,
SkTileMode tile_mode,
bool convolve_alpha,
sk_sp<PaintFilter> input,
const CropRect* crop_rect)
: PaintFilter(kType, crop_rect, HasDiscardableImages(input)),
kernel_size_(kernel_size),
gain_(gain),
bias_(bias),
kernel_offset_(kernel_offset),
tile_mode_(tile_mode),
convolve_alpha_(convolve_alpha),
input_(std::move(input)) {
auto len = static_cast<size_t>(
sk_64_mul(kernel_size_.width(), kernel_size_.height()));
kernel_->reserve(len);
for (size_t i = 0; i < len; ++i)
kernel_->push_back(kernel[i]);
cached_sk_filter_ = SkImageFilters::MatrixConvolution(
kernel_size_, kernel, gain_, bias_, kernel_offset_, tile_mode_,
convolve_alpha_, GetSkFilter(input_.get()), crop_rect);
}
MatrixConvolutionPaintFilter::~MatrixConvolutionPaintFilter() = default;
size_t MatrixConvolutionPaintFilter::SerializedSize() const {
base::CheckedNumeric<size_t> total_size =
BaseSerializedSize() + sizeof(kernel_size_) + sizeof(size_t) +
kernel_->size() * sizeof(SkScalar) + sizeof(gain_) + sizeof(bias_) +
sizeof(kernel_offset_) + sizeof(tile_mode_) + sizeof(convolve_alpha_);
total_size += GetFilterSize(input_.get());
return total_size.ValueOrDefault(0u);
}
sk_sp<PaintFilter> MatrixConvolutionPaintFilter::SnapshotWithImagesInternal(
ImageProvider* image_provider) const {
return sk_make_sp<MatrixConvolutionPaintFilter>(
kernel_size_, &kernel_[0], gain_, bias_, kernel_offset_, tile_mode_,
convolve_alpha_, Snapshot(input_, image_provider), GetCropRect());
}
bool MatrixConvolutionPaintFilter::operator==(
const MatrixConvolutionPaintFilter& other) const {
return kernel_size_ == other.kernel_size_ &&
std::equal(kernel_.container().begin(), kernel_.container().end(),
other.kernel_.container().begin(), AreScalarsEqual) &&
PaintOp::AreEqualEvenIfNaN(gain_, other.gain_) &&
PaintOp::AreEqualEvenIfNaN(bias_, other.bias_) &&
kernel_offset_ == other.kernel_offset_ &&
tile_mode_ == other.tile_mode_ &&
convolve_alpha_ == other.convolve_alpha_ &&
base::ValuesEquivalent(input_.get(), other.input_.get());
}
DisplacementMapEffectPaintFilter::DisplacementMapEffectPaintFilter(
SkColorChannel channel_x,
SkColorChannel channel_y,
SkScalar scale,
sk_sp<PaintFilter> displacement,
sk_sp<PaintFilter> color,
const CropRect* crop_rect)
: PaintFilter(
kType,
crop_rect,
HasDiscardableImages(displacement) || HasDiscardableImages(color)),
channel_x_(channel_x),
channel_y_(channel_y),
scale_(scale),
displacement_(std::move(displacement)),
color_(std::move(color)) {
cached_sk_filter_ = SkImageFilters::DisplacementMap(
channel_x_, channel_y_, scale_, GetSkFilter(displacement_.get()),
GetSkFilter(color_.get()), crop_rect);
}
DisplacementMapEffectPaintFilter::~DisplacementMapEffectPaintFilter() = default;
size_t DisplacementMapEffectPaintFilter::SerializedSize() const {
base::CheckedNumeric<size_t> total_size = BaseSerializedSize() +
sizeof(channel_x_) +
sizeof(channel_y_) + sizeof(scale_);
total_size += GetFilterSize(displacement_.get());
total_size += GetFilterSize(color_.get());
return total_size.ValueOrDefault(0u);
}
sk_sp<PaintFilter> DisplacementMapEffectPaintFilter::SnapshotWithImagesInternal(
ImageProvider* image_provider) const {
return sk_make_sp<DisplacementMapEffectPaintFilter>(
channel_x_, channel_y_, scale_, Snapshot(displacement_, image_provider),
Snapshot(color_, image_provider), GetCropRect());
}
bool DisplacementMapEffectPaintFilter::operator==(
const DisplacementMapEffectPaintFilter& other) const {
return channel_x_ == other.channel_x_ && channel_y_ == other.channel_y_ &&
PaintOp::AreEqualEvenIfNaN(scale_, other.scale_) &&
base::ValuesEquivalent(displacement_.get(),
other.displacement_.get()) &&
base::ValuesEquivalent(color_.get(), other.color_.get());
}
ImagePaintFilter::ImagePaintFilter(PaintImage image,
const SkRect& src_rect,
const SkRect& dst_rect,
PaintFlags::FilterQuality filter_quality)
: PaintFilter(kType, nullptr, !image.IsTextureBacked()),
image_(std::move(image)),
src_rect_(src_rect),
dst_rect_(dst_rect),
filter_quality_(filter_quality) {
SkSamplingOptions sampling(
PaintFlags::FilterQualityToSkSamplingOptions(filter_quality));
cached_sk_filter_ = SkImageFilters::Image(image_.GetSkImage(), src_rect_,
dst_rect_, sampling);
}
ImagePaintFilter::~ImagePaintFilter() = default;
size_t ImagePaintFilter::SerializedSize() const {
base::CheckedNumeric<size_t> total_size =
BaseSerializedSize() + sizeof(src_rect_) + sizeof(dst_rect_) +
sizeof(filter_quality_);
total_size += PaintOpWriter::GetImageSize(image_);
return total_size.ValueOrDefault(0u);
}
sk_sp<PaintFilter> ImagePaintFilter::SnapshotWithImagesInternal(
ImageProvider* image_provider) const {
DrawImage draw_image(image_, false,
SkIRect::MakeWH(image_.width(), image_.height()),
filter_quality_, SkM44());
auto scoped_result = image_provider->GetRasterContent(draw_image);
if (!scoped_result)
return nullptr;
auto decoded_sk_image = sk_ref_sp<SkImage>(
const_cast<SkImage*>(scoped_result.decoded_image().image().get()));
PaintImage decoded_paint_image =
PaintImageBuilder::WithDefault()
.set_id(image_.stable_id())
.set_texture_image(decoded_sk_image, PaintImage::GetNextContentId())
.TakePaintImage();
return sk_make_sp<ImagePaintFilter>(std::move(decoded_paint_image), src_rect_,
dst_rect_, filter_quality_);
}
bool ImagePaintFilter::operator==(const ImagePaintFilter& other) const {
return !!image_ == !!other.image_ &&
PaintOp::AreSkRectsEqual(src_rect_, other.src_rect_) &&
PaintOp::AreSkRectsEqual(dst_rect_, other.dst_rect_) &&
filter_quality_ == other.filter_quality_;
}
RecordPaintFilter::RecordPaintFilter(sk_sp<PaintRecord> record,
const SkRect& record_bounds,
const gfx::SizeF& raster_scale,
ScalingBehavior scaling_behavior)
: RecordPaintFilter(std::move(record),
record_bounds,
raster_scale,
scaling_behavior,
nullptr) {}
RecordPaintFilter::RecordPaintFilter(sk_sp<PaintRecord> record,
const SkRect& record_bounds,
const gfx::SizeF& raster_scale,
ScalingBehavior scaling_behavior,
ImageProvider* image_provider)
: PaintFilter(kType, nullptr, record->HasDiscardableImages()),
record_(std::move(record)),
record_bounds_(record_bounds),
raster_scale_(raster_scale),
scaling_behavior_(scaling_behavior) {
DCHECK(raster_scale_.width() > 0.f && raster_scale_.height() > 0.f);
DCHECK(scaling_behavior == ScalingBehavior::kFixedScale ||
(raster_scale_.width() == 1.f && raster_scale_.height() == 1.f));
sk_sp<SkPicture> picture =
ToSkPicture(record_, record_bounds_, image_provider);
if (scaling_behavior == ScalingBehavior::kRasterAtScale ||
record_bounds_.isEmpty()) {
cached_sk_filter_ = SkImageFilters::Picture(std::move(picture));
} else {
DCHECK(scaling_behavior == ScalingBehavior::kFixedScale);
// Convert the record to an image at the scaled resolution, but draw it in
// the filter DAG at the original record bounds.
int width = SkScalarCeilToInt(record_bounds.width());
int height = SkScalarCeilToInt(record_bounds.height());
SkMatrix originAdjust =
SkMatrix::Translate(-record_bounds.fLeft, -record_bounds.fTop);
auto image = SkImage::MakeFromPicture(
std::move(picture), SkISize::Make(width, height), &originAdjust,
nullptr, SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
// Must account for the raster scale when drawing the picture image,
SkRect src = SkRect::MakeWH(record_bounds.width(), record_bounds.height());
SkScalar inv_x = 1.f / raster_scale_.width();
SkScalar inv_y = 1.f / raster_scale_.height();
SkRect dst = {inv_x * record_bounds.fLeft, inv_y * record_bounds.fTop,
inv_x * record_bounds.fRight, inv_y * record_bounds.fBottom};