forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilterNodeSoftware.cpp
3660 lines (3235 loc) · 108 KB
/
FilterNodeSoftware.cpp
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
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <cmath>
#include "DataSurfaceHelpers.h"
#include "FilterNodeSoftware.h"
#include "2D.h"
#include "Tools.h"
#include "Blur.h"
#include <map>
#include "FilterProcessing.h"
#include "Logging.h"
#include "mozilla/PodOperations.h"
#include "mozilla/DebugOnly.h"
// #define DEBUG_DUMP_SURFACES
#ifdef DEBUG_DUMP_SURFACES
#include "gfxUtils.h" // not part of Moz2D
#endif
namespace mozilla {
namespace gfx {
namespace {
/**
* This class provides a way to get a pow() results in constant-time. It works
* by caching 256 values for bases between 0 and 1 and a fixed exponent.
**/
class PowCache
{
public:
PowCache()
{
CacheForExponent(0.0f);
}
void CacheForExponent(Float aExponent)
{
mExponent = aExponent;
int numPreSquares = 0;
while (numPreSquares < 5 && mExponent > (1 << (numPreSquares + 2))) {
numPreSquares++;
}
mNumPowTablePreSquares = numPreSquares;
for (size_t i = 0; i < sCacheSize; i++) {
// sCacheSize is chosen in such a way that a takes values
// from 0.0 to 1.0 inclusive.
Float a = i / Float(1 << sCacheIndexPrecisionBits);
MOZ_ASSERT(0.0f <= a && a <= 1.0f, "We only want to cache for bases between 0 and 1.");
for (int j = 0; j < mNumPowTablePreSquares; j++) {
a = sqrt(a);
}
uint32_t cachedInt = pow(a, mExponent) * (1 << sOutputIntPrecisionBits);
MOZ_ASSERT(cachedInt < (1 << (sizeof(mPowTable[i]) * 8)), "mPowCache integer type too small");
mPowTable[i] = cachedInt;
}
}
uint16_t Pow(uint16_t aBase)
{
// Results should be similar to what the following code would produce:
// Float x = Float(aBase) / (1 << sInputIntPrecisionBits);
// return uint16_t(pow(x, mExponent) * (1 << sOutputIntPrecisionBits));
MOZ_ASSERT(aBase <= (1 << sInputIntPrecisionBits), "aBase needs to be between 0 and 1!");
uint32_t a = aBase;
for (int j = 0; j < mNumPowTablePreSquares; j++) {
a = a * a >> sInputIntPrecisionBits;
}
uint32_t i = a >> (sInputIntPrecisionBits - sCacheIndexPrecisionBits);
MOZ_ASSERT(i < sCacheSize, "out-of-bounds mPowTable access");
return mPowTable[i];
}
static const int sInputIntPrecisionBits = 15;
static const int sOutputIntPrecisionBits = 15;
static const int sCacheIndexPrecisionBits = 7;
private:
static const size_t sCacheSize = (1 << sCacheIndexPrecisionBits) + 1;
Float mExponent;
int mNumPowTablePreSquares;
uint16_t mPowTable[sCacheSize];
};
class PointLightSoftware
{
public:
bool SetAttribute(uint32_t aIndex, Float) { return false; }
bool SetAttribute(uint32_t aIndex, const Point3D &);
void Prepare() {}
Point3D GetVectorToLight(const Point3D &aTargetPoint);
uint32_t GetColor(uint32_t aLightColor, const Point3D &aVectorToLight);
private:
Point3D mPosition;
};
class SpotLightSoftware
{
public:
SpotLightSoftware();
bool SetAttribute(uint32_t aIndex, Float);
bool SetAttribute(uint32_t aIndex, const Point3D &);
void Prepare();
Point3D GetVectorToLight(const Point3D &aTargetPoint);
uint32_t GetColor(uint32_t aLightColor, const Point3D &aVectorToLight);
private:
Point3D mPosition;
Point3D mPointsAt;
Point3D mVectorFromFocusPointToLight;
Float mSpecularFocus;
Float mLimitingConeAngle;
Float mLimitingConeCos;
PowCache mPowCache;
};
class DistantLightSoftware
{
public:
DistantLightSoftware();
bool SetAttribute(uint32_t aIndex, Float);
bool SetAttribute(uint32_t aIndex, const Point3D &) { return false; }
void Prepare();
Point3D GetVectorToLight(const Point3D &aTargetPoint);
uint32_t GetColor(uint32_t aLightColor, const Point3D &aVectorToLight);
private:
Float mAzimuth;
Float mElevation;
Point3D mVectorToLight;
};
class DiffuseLightingSoftware
{
public:
DiffuseLightingSoftware();
bool SetAttribute(uint32_t aIndex, Float);
void Prepare() {}
uint32_t LightPixel(const Point3D &aNormal, const Point3D &aVectorToLight,
uint32_t aColor);
private:
Float mDiffuseConstant;
};
class SpecularLightingSoftware
{
public:
SpecularLightingSoftware();
bool SetAttribute(uint32_t aIndex, Float);
void Prepare();
uint32_t LightPixel(const Point3D &aNormal, const Point3D &aVectorToLight,
uint32_t aColor);
private:
Float mSpecularConstant;
Float mSpecularExponent;
uint32_t mSpecularConstantInt;
PowCache mPowCache;
};
} // unnamed namespace
// from xpcom/ds/nsMathUtils.h
static int32_t
NS_lround(double x)
{
return x >= 0.0 ? int32_t(x + 0.5) : int32_t(x - 0.5);
}
already_AddRefed<DataSourceSurface>
CloneAligned(DataSourceSurface* aSource)
{
return CreateDataSourceSurfaceByCloning(aSource);
}
static void
FillRectWithPixel(DataSourceSurface *aSurface, const IntRect &aFillRect, IntPoint aPixelPos)
{
MOZ_ASSERT(!aFillRect.Overflows());
MOZ_ASSERT(IntRect(IntPoint(), aSurface->GetSize()).Contains(aFillRect),
"aFillRect needs to be completely inside the surface");
MOZ_ASSERT(SurfaceContainsPoint(aSurface, aPixelPos),
"aPixelPos needs to be inside the surface");
DataSourceSurface::ScopedMap surfMap(aSurface, DataSourceSurface::READ_WRITE);
if(MOZ2D_WARN_IF(!surfMap.IsMapped())) {
return;
}
uint8_t* sourcePixelData = DataAtOffset(aSurface, surfMap.GetMappedSurface(), aPixelPos);
uint8_t* data = DataAtOffset(aSurface, surfMap.GetMappedSurface(), aFillRect.TopLeft());
int bpp = BytesPerPixel(aSurface->GetFormat());
// Fill the first row by hand.
if (bpp == 4) {
uint32_t sourcePixel = *(uint32_t*)sourcePixelData;
for (int32_t x = 0; x < aFillRect.width; x++) {
*((uint32_t*)data + x) = sourcePixel;
}
} else if (BytesPerPixel(aSurface->GetFormat()) == 1) {
uint8_t sourcePixel = *sourcePixelData;
memset(data, sourcePixel, aFillRect.width);
}
// Copy the first row into the other rows.
for (int32_t y = 1; y < aFillRect.height; y++) {
PodCopy(data + y * surfMap.GetStride(), data, aFillRect.width * bpp);
}
}
static void
FillRectWithVerticallyRepeatingHorizontalStrip(DataSourceSurface *aSurface,
const IntRect &aFillRect,
const IntRect &aSampleRect)
{
MOZ_ASSERT(!aFillRect.Overflows());
MOZ_ASSERT(!aSampleRect.Overflows());
MOZ_ASSERT(IntRect(IntPoint(), aSurface->GetSize()).Contains(aFillRect),
"aFillRect needs to be completely inside the surface");
MOZ_ASSERT(IntRect(IntPoint(), aSurface->GetSize()).Contains(aSampleRect),
"aSampleRect needs to be completely inside the surface");
DataSourceSurface::ScopedMap surfMap(aSurface, DataSourceSurface::READ_WRITE);
if (MOZ2D_WARN_IF(!surfMap.IsMapped())) {
return;
}
uint8_t* sampleData = DataAtOffset(aSurface, surfMap.GetMappedSurface(), aSampleRect.TopLeft());
uint8_t* data = DataAtOffset(aSurface, surfMap.GetMappedSurface(), aFillRect.TopLeft());
if (BytesPerPixel(aSurface->GetFormat()) == 4) {
for (int32_t y = 0; y < aFillRect.height; y++) {
PodCopy((uint32_t*)data, (uint32_t*)sampleData, aFillRect.width);
data += surfMap.GetStride();
}
} else if (BytesPerPixel(aSurface->GetFormat()) == 1) {
for (int32_t y = 0; y < aFillRect.height; y++) {
PodCopy(data, sampleData, aFillRect.width);
data += surfMap.GetStride();
}
}
}
static void
FillRectWithHorizontallyRepeatingVerticalStrip(DataSourceSurface *aSurface,
const IntRect &aFillRect,
const IntRect &aSampleRect)
{
MOZ_ASSERT(!aFillRect.Overflows());
MOZ_ASSERT(!aSampleRect.Overflows());
MOZ_ASSERT(IntRect(IntPoint(), aSurface->GetSize()).Contains(aFillRect),
"aFillRect needs to be completely inside the surface");
MOZ_ASSERT(IntRect(IntPoint(), aSurface->GetSize()).Contains(aSampleRect),
"aSampleRect needs to be completely inside the surface");
DataSourceSurface::ScopedMap surfMap(aSurface, DataSourceSurface::READ_WRITE);
if (MOZ2D_WARN_IF(!surfMap.IsMapped())) {
return;
}
uint8_t* sampleData = DataAtOffset(aSurface, surfMap.GetMappedSurface(), aSampleRect.TopLeft());
uint8_t* data = DataAtOffset(aSurface, surfMap.GetMappedSurface(), aFillRect.TopLeft());
if (BytesPerPixel(aSurface->GetFormat()) == 4) {
for (int32_t y = 0; y < aFillRect.height; y++) {
int32_t sampleColor = *((uint32_t*)sampleData);
for (int32_t x = 0; x < aFillRect.width; x++) {
*((uint32_t*)data + x) = sampleColor;
}
data += surfMap.GetStride();
sampleData += surfMap.GetStride();
}
} else if (BytesPerPixel(aSurface->GetFormat()) == 1) {
for (int32_t y = 0; y < aFillRect.height; y++) {
uint8_t sampleColor = *sampleData;
memset(data, sampleColor, aFillRect.width);
data += surfMap.GetStride();
sampleData += surfMap.GetStride();
}
}
}
static void
DuplicateEdges(DataSourceSurface* aSurface, const IntRect &aFromRect)
{
MOZ_ASSERT(!aFromRect.Overflows());
MOZ_ASSERT(IntRect(IntPoint(), aSurface->GetSize()).Contains(aFromRect),
"aFromRect needs to be completely inside the surface");
IntSize size = aSurface->GetSize();
IntRect fill;
IntRect sampleRect;
for (int32_t ix = 0; ix < 3; ix++) {
switch (ix) {
case 0:
fill.x = 0;
fill.width = aFromRect.x;
sampleRect.x = fill.XMost();
sampleRect.width = 1;
break;
case 1:
fill.x = aFromRect.x;
fill.width = aFromRect.width;
sampleRect.x = fill.x;
sampleRect.width = fill.width;
break;
case 2:
fill.x = aFromRect.XMost();
fill.width = size.width - fill.x;
sampleRect.x = fill.x - 1;
sampleRect.width = 1;
break;
}
if (fill.width <= 0) {
continue;
}
bool xIsMiddle = (ix == 1);
for (int32_t iy = 0; iy < 3; iy++) {
switch (iy) {
case 0:
fill.y = 0;
fill.height = aFromRect.y;
sampleRect.y = fill.YMost();
sampleRect.height = 1;
break;
case 1:
fill.y = aFromRect.y;
fill.height = aFromRect.height;
sampleRect.y = fill.y;
sampleRect.height = fill.height;
break;
case 2:
fill.y = aFromRect.YMost();
fill.height = size.height - fill.y;
sampleRect.y = fill.y - 1;
sampleRect.height = 1;
break;
}
if (fill.height <= 0) {
continue;
}
bool yIsMiddle = (iy == 1);
if (!xIsMiddle && !yIsMiddle) {
// Corner
FillRectWithPixel(aSurface, fill, sampleRect.TopLeft());
}
if (xIsMiddle && !yIsMiddle) {
// Top middle or bottom middle
FillRectWithVerticallyRepeatingHorizontalStrip(aSurface, fill, sampleRect);
}
if (!xIsMiddle && yIsMiddle) {
// Left middle or right middle
FillRectWithHorizontallyRepeatingVerticalStrip(aSurface, fill, sampleRect);
}
}
}
}
static IntPoint
TileIndex(const IntRect &aFirstTileRect, const IntPoint &aPoint)
{
return IntPoint(int32_t(floor(double(aPoint.x - aFirstTileRect.x) / aFirstTileRect.width)),
int32_t(floor(double(aPoint.y - aFirstTileRect.y) / aFirstTileRect.height)));
}
static void
TileSurface(DataSourceSurface* aSource, DataSourceSurface* aTarget, const IntPoint &aOffset)
{
IntRect sourceRect(aOffset, aSource->GetSize());
IntRect targetRect(IntPoint(0, 0), aTarget->GetSize());
IntPoint startIndex = TileIndex(sourceRect, targetRect.TopLeft());
IntPoint endIndex = TileIndex(sourceRect, targetRect.BottomRight());
for (int32_t ix = startIndex.x; ix <= endIndex.x; ix++) {
for (int32_t iy = startIndex.y; iy <= endIndex.y; iy++) {
IntPoint destPoint(sourceRect.x + ix * sourceRect.width,
sourceRect.y + iy * sourceRect.height);
IntRect destRect(destPoint, sourceRect.Size());
destRect = destRect.Intersect(targetRect);
IntRect srcRect = destRect - destPoint;
CopyRect(aSource, aTarget, srcRect, destRect.TopLeft());
}
}
}
static already_AddRefed<DataSourceSurface>
GetDataSurfaceInRect(SourceSurface *aSurface,
const IntRect &aSurfaceRect,
const IntRect &aDestRect,
ConvolveMatrixEdgeMode aEdgeMode)
{
MOZ_ASSERT(aSurface ? aSurfaceRect.Size() == aSurface->GetSize() : aSurfaceRect.IsEmpty());
if (aSurfaceRect.Overflows() || aDestRect.Overflows()) {
// We can't rely on the intersection calculations below to make sense when
// XMost() or YMost() overflow. Bail out.
return nullptr;
}
IntRect sourceRect = aSurfaceRect;
if (sourceRect.IsEqualEdges(aDestRect)) {
return aSurface ? aSurface->GetDataSurface() : nullptr;
}
IntRect intersect = sourceRect.Intersect(aDestRect);
IntRect intersectInSourceSpace = intersect - sourceRect.TopLeft();
IntRect intersectInDestSpace = intersect - aDestRect.TopLeft();
SurfaceFormat format = aSurface ? aSurface->GetFormat() : SurfaceFormat(SurfaceFormat::B8G8R8A8);
RefPtr<DataSourceSurface> target =
Factory::CreateDataSourceSurface(aDestRect.Size(), format, true);
if (MOZ2D_WARN_IF(!target)) {
return nullptr;
}
if (!aSurface) {
return target.forget();
}
RefPtr<DataSourceSurface> dataSource = aSurface->GetDataSurface();
MOZ_ASSERT(dataSource);
if (aEdgeMode == EDGE_MODE_WRAP) {
TileSurface(dataSource, target, intersectInDestSpace.TopLeft());
return target.forget();
}
CopyRect(dataSource, target, intersectInSourceSpace,
intersectInDestSpace.TopLeft());
if (aEdgeMode == EDGE_MODE_DUPLICATE) {
DuplicateEdges(target, intersectInDestSpace);
}
return target.forget();
}
/* static */ already_AddRefed<FilterNode>
FilterNodeSoftware::Create(FilterType aType)
{
RefPtr<FilterNodeSoftware> filter;
switch (aType) {
case FilterType::BLEND:
filter = new FilterNodeBlendSoftware();
break;
case FilterType::TRANSFORM:
filter = new FilterNodeTransformSoftware();
break;
case FilterType::MORPHOLOGY:
filter = new FilterNodeMorphologySoftware();
break;
case FilterType::COLOR_MATRIX:
filter = new FilterNodeColorMatrixSoftware();
break;
case FilterType::FLOOD:
filter = new FilterNodeFloodSoftware();
break;
case FilterType::TILE:
filter = new FilterNodeTileSoftware();
break;
case FilterType::TABLE_TRANSFER:
filter = new FilterNodeTableTransferSoftware();
break;
case FilterType::DISCRETE_TRANSFER:
filter = new FilterNodeDiscreteTransferSoftware();
break;
case FilterType::LINEAR_TRANSFER:
filter = new FilterNodeLinearTransferSoftware();
break;
case FilterType::GAMMA_TRANSFER:
filter = new FilterNodeGammaTransferSoftware();
break;
case FilterType::CONVOLVE_MATRIX:
filter = new FilterNodeConvolveMatrixSoftware();
break;
case FilterType::DISPLACEMENT_MAP:
filter = new FilterNodeDisplacementMapSoftware();
break;
case FilterType::TURBULENCE:
filter = new FilterNodeTurbulenceSoftware();
break;
case FilterType::ARITHMETIC_COMBINE:
filter = new FilterNodeArithmeticCombineSoftware();
break;
case FilterType::COMPOSITE:
filter = new FilterNodeCompositeSoftware();
break;
case FilterType::GAUSSIAN_BLUR:
filter = new FilterNodeGaussianBlurSoftware();
break;
case FilterType::DIRECTIONAL_BLUR:
filter = new FilterNodeDirectionalBlurSoftware();
break;
case FilterType::CROP:
filter = new FilterNodeCropSoftware();
break;
case FilterType::PREMULTIPLY:
filter = new FilterNodePremultiplySoftware();
break;
case FilterType::UNPREMULTIPLY:
filter = new FilterNodeUnpremultiplySoftware();
break;
case FilterType::POINT_DIFFUSE:
filter = new FilterNodeLightingSoftware<PointLightSoftware, DiffuseLightingSoftware>("FilterNodeLightingSoftware<PointLight, DiffuseLighting>");
break;
case FilterType::POINT_SPECULAR:
filter = new FilterNodeLightingSoftware<PointLightSoftware, SpecularLightingSoftware>("FilterNodeLightingSoftware<PointLight, SpecularLighting>");
break;
case FilterType::SPOT_DIFFUSE:
filter = new FilterNodeLightingSoftware<SpotLightSoftware, DiffuseLightingSoftware>("FilterNodeLightingSoftware<SpotLight, DiffuseLighting>");
break;
case FilterType::SPOT_SPECULAR:
filter = new FilterNodeLightingSoftware<SpotLightSoftware, SpecularLightingSoftware>("FilterNodeLightingSoftware<SpotLight, SpecularLighting>");
break;
case FilterType::DISTANT_DIFFUSE:
filter = new FilterNodeLightingSoftware<DistantLightSoftware, DiffuseLightingSoftware>("FilterNodeLightingSoftware<DistantLight, DiffuseLighting>");
break;
case FilterType::DISTANT_SPECULAR:
filter = new FilterNodeLightingSoftware<DistantLightSoftware, SpecularLightingSoftware>("FilterNodeLightingSoftware<DistantLight, SpecularLighting>");
break;
}
return filter.forget();
}
void
FilterNodeSoftware::Draw(DrawTarget* aDrawTarget,
const Rect &aSourceRect,
const Point &aDestPoint,
const DrawOptions &aOptions)
{
#ifdef DEBUG_DUMP_SURFACES
printf("<style>section{margin:10px;}</style><pre>\nRendering filter %s...\n", GetName());
#endif
Rect renderRect = aSourceRect;
renderRect.RoundOut();
IntRect renderIntRect;
if (!renderRect.ToIntRect(&renderIntRect)) {
#ifdef DEBUG_DUMP_SURFACES
printf("render rect overflowed, not painting anything\n");
printf("</pre>\n");
#endif
return;
}
IntRect outputRect = GetOutputRectInRect(renderIntRect);
if (outputRect.Overflows()) {
#ifdef DEBUG_DUMP_SURFACES
printf("output rect overflowed, not painting anything\n");
printf("</pre>\n");
#endif
return;
}
RefPtr<DataSourceSurface> result;
if (!outputRect.IsEmpty()) {
result = GetOutput(outputRect);
}
if (!result) {
// Null results are allowed and treated as transparent. Don't draw anything.
#ifdef DEBUG_DUMP_SURFACES
printf("output returned null\n");
printf("</pre>\n");
#endif
return;
}
#ifdef DEBUG_DUMP_SURFACES
printf("output from %s:\n", GetName());
printf("<img src='"); gfxUtils::DumpAsDataURL(result); printf("'>\n");
printf("</pre>\n");
#endif
Point sourceToDestOffset = aDestPoint - aSourceRect.TopLeft();
Rect renderedSourceRect = Rect(outputRect).Intersect(aSourceRect);
Rect renderedDestRect = renderedSourceRect + sourceToDestOffset;
if (result->GetFormat() == SurfaceFormat::A8) {
// Interpret the result as having implicitly black color channels.
aDrawTarget->PushClipRect(renderedDestRect);
aDrawTarget->MaskSurface(ColorPattern(Color(0.0, 0.0, 0.0, 1.0)),
result,
Point(outputRect.TopLeft()) + sourceToDestOffset,
aOptions);
aDrawTarget->PopClip();
} else {
aDrawTarget->DrawSurface(result, renderedDestRect,
renderedSourceRect - Point(outputRect.TopLeft()),
DrawSurfaceOptions(), aOptions);
}
}
already_AddRefed<DataSourceSurface>
FilterNodeSoftware::GetOutput(const IntRect &aRect)
{
MOZ_ASSERT(GetOutputRectInRect(aRect).Contains(aRect));
if (aRect.Overflows()) {
return nullptr;
}
if (!mCachedRect.Contains(aRect)) {
RequestRect(aRect);
mCachedOutput = Render(mRequestedRect);
if (!mCachedOutput) {
mCachedRect = IntRect();
mRequestedRect = IntRect();
return nullptr;
}
mCachedRect = mRequestedRect;
mRequestedRect = IntRect();
} else {
MOZ_ASSERT(mCachedOutput, "cached rect but no cached output?");
}
return GetDataSurfaceInRect(mCachedOutput, mCachedRect, aRect, EDGE_MODE_NONE);
}
void
FilterNodeSoftware::RequestRect(const IntRect &aRect)
{
if (mRequestedRect.Contains(aRect)) {
// Bail out now. Otherwise pathological filters can spend time exponential
// in the number of primitives, e.g. if each primitive takes the
// previous primitive as its two inputs.
return;
}
mRequestedRect = mRequestedRect.Union(aRect);
RequestFromInputsForRect(aRect);
}
void
FilterNodeSoftware::RequestInputRect(uint32_t aInputEnumIndex, const IntRect &aRect)
{
if (aRect.Overflows()) {
return;
}
int32_t inputIndex = InputIndex(aInputEnumIndex);
if (inputIndex < 0 || (uint32_t)inputIndex >= NumberOfSetInputs()) {
gfxDevCrash(LogReason::FilterInputError) << "Invalid input " << inputIndex << " vs. " << NumberOfSetInputs();
return;
}
if (mInputSurfaces[inputIndex]) {
return;
}
RefPtr<FilterNodeSoftware> filter = mInputFilters[inputIndex];
MOZ_ASSERT(filter, "missing input");
filter->RequestRect(filter->GetOutputRectInRect(aRect));
}
SurfaceFormat
FilterNodeSoftware::DesiredFormat(SurfaceFormat aCurrentFormat,
FormatHint aFormatHint)
{
if (aCurrentFormat == SurfaceFormat::A8 && aFormatHint == CAN_HANDLE_A8) {
return SurfaceFormat::A8;
}
return SurfaceFormat::B8G8R8A8;
}
already_AddRefed<DataSourceSurface>
FilterNodeSoftware::GetInputDataSourceSurface(uint32_t aInputEnumIndex,
const IntRect& aRect,
FormatHint aFormatHint,
ConvolveMatrixEdgeMode aEdgeMode,
const IntRect *aTransparencyPaddedSourceRect)
{
if (aRect.Overflows()) {
return nullptr;
}
#ifdef DEBUG_DUMP_SURFACES
printf("<section><h1>GetInputDataSourceSurface with aRect: %d, %d, %d, %d</h1>\n",
aRect.x, aRect.y, aRect.width, aRect.height);
#endif
int32_t inputIndex = InputIndex(aInputEnumIndex);
if (inputIndex < 0 || (uint32_t)inputIndex >= NumberOfSetInputs()) {
gfxDevCrash(LogReason::FilterInputData) << "Invalid data " << inputIndex << " vs. " << NumberOfSetInputs();
return nullptr;
}
if (aRect.IsEmpty()) {
return nullptr;
}
RefPtr<SourceSurface> surface;
IntRect surfaceRect;
if (mInputSurfaces[inputIndex]) {
// Input from input surface
surface = mInputSurfaces[inputIndex];
#ifdef DEBUG_DUMP_SURFACES
printf("input from input surface:\n");
#endif
surfaceRect = IntRect(IntPoint(0, 0), surface->GetSize());
} else {
// Input from input filter
#ifdef DEBUG_DUMP_SURFACES
printf("getting input from input filter %s...\n", mInputFilters[inputIndex]->GetName());
#endif
RefPtr<FilterNodeSoftware> filter = mInputFilters[inputIndex];
MOZ_ASSERT(filter, "missing input");
IntRect inputFilterOutput = filter->GetOutputRectInRect(aRect);
if (!inputFilterOutput.IsEmpty()) {
surface = filter->GetOutput(inputFilterOutput);
}
#ifdef DEBUG_DUMP_SURFACES
printf("input from input filter %s:\n", mInputFilters[inputIndex]->GetName());
#endif
surfaceRect = inputFilterOutput;
MOZ_ASSERT(!surface || surfaceRect.Size() == surface->GetSize());
}
if (surface && surface->GetFormat() == SurfaceFormat::UNKNOWN) {
#ifdef DEBUG_DUMP_SURFACES
printf("wrong input format</section>\n\n");
#endif
return nullptr;
}
if (!surfaceRect.IsEmpty() && !surface) {
#ifdef DEBUG_DUMP_SURFACES
printf(" -- no input --</section>\n\n");
#endif
return nullptr;
}
if (aTransparencyPaddedSourceRect && !aTransparencyPaddedSourceRect->IsEmpty()) {
IntRect srcRect = aTransparencyPaddedSourceRect->Intersect(aRect);
surface = GetDataSurfaceInRect(surface, surfaceRect, srcRect, EDGE_MODE_NONE);
surfaceRect = srcRect;
}
RefPtr<DataSourceSurface> result =
GetDataSurfaceInRect(surface, surfaceRect, aRect, aEdgeMode);
if (result) {
// TODO: This isn't safe since we don't have a guarantee
// that future Maps will have the same stride
DataSourceSurface::MappedSurface map;
if (result->Map(DataSourceSurface::READ, &map)) {
// Unmap immediately since CloneAligned hasn't been updated
// to use the Map API yet. We can still read the stride/data
// values as long as we don't try to dereference them.
result->Unmap();
if (map.mStride != GetAlignedStride<16>(map.mStride, 1) ||
reinterpret_cast<uintptr_t>(map.mData) % 16 != 0) {
// Align unaligned surface.
result = CloneAligned(result);
}
} else {
result = nullptr;
}
}
if (!result) {
#ifdef DEBUG_DUMP_SURFACES
printf(" -- no input --</section>\n\n");
#endif
return nullptr;
}
SurfaceFormat currentFormat = result->GetFormat();
if (DesiredFormat(currentFormat, aFormatHint) == SurfaceFormat::B8G8R8A8 &&
currentFormat != SurfaceFormat::B8G8R8A8) {
result = FilterProcessing::ConvertToB8G8R8A8(result);
}
#ifdef DEBUG_DUMP_SURFACES
printf("<img src='"); gfxUtils::DumpAsDataURL(result); printf("'></section>");
#endif
MOZ_ASSERT(!result || result->GetSize() == aRect.Size(), "wrong surface size");
return result.forget();
}
IntRect
FilterNodeSoftware::GetInputRectInRect(uint32_t aInputEnumIndex,
const IntRect &aInRect)
{
if (aInRect.Overflows()) {
return IntRect();
}
int32_t inputIndex = InputIndex(aInputEnumIndex);
if (inputIndex < 0 || (uint32_t)inputIndex >= NumberOfSetInputs()) {
gfxDevCrash(LogReason::FilterInputRect) << "Invalid rect " << inputIndex << " vs. " << NumberOfSetInputs();
return IntRect();
}
if (mInputSurfaces[inputIndex]) {
return aInRect.Intersect(IntRect(IntPoint(0, 0),
mInputSurfaces[inputIndex]->GetSize()));
}
RefPtr<FilterNodeSoftware> filter = mInputFilters[inputIndex];
MOZ_ASSERT(filter, "missing input");
return filter->GetOutputRectInRect(aInRect);
}
size_t
FilterNodeSoftware::NumberOfSetInputs()
{
return std::max(mInputSurfaces.size(), mInputFilters.size());
}
void
FilterNodeSoftware::AddInvalidationListener(FilterInvalidationListener* aListener)
{
MOZ_ASSERT(aListener, "null listener");
mInvalidationListeners.push_back(aListener);
}
void
FilterNodeSoftware::RemoveInvalidationListener(FilterInvalidationListener* aListener)
{
MOZ_ASSERT(aListener, "null listener");
std::vector<FilterInvalidationListener*>::iterator it =
std::find(mInvalidationListeners.begin(), mInvalidationListeners.end(), aListener);
mInvalidationListeners.erase(it);
}
void
FilterNodeSoftware::FilterInvalidated(FilterNodeSoftware* aFilter)
{
Invalidate();
}
void
FilterNodeSoftware::Invalidate()
{
mCachedOutput = nullptr;
mCachedRect = IntRect();
for (std::vector<FilterInvalidationListener*>::iterator it = mInvalidationListeners.begin();
it != mInvalidationListeners.end(); it++) {
(*it)->FilterInvalidated(this);
}
}
FilterNodeSoftware::~FilterNodeSoftware()
{
MOZ_ASSERT(!mInvalidationListeners.size(),
"All invalidation listeners should have unsubscribed themselves by now!");
for (std::vector<RefPtr<FilterNodeSoftware> >::iterator it = mInputFilters.begin();
it != mInputFilters.end(); it++) {
if (*it) {
(*it)->RemoveInvalidationListener(this);
}
}
}
void
FilterNodeSoftware::SetInput(uint32_t aIndex, FilterNode *aFilter)
{
if (aFilter && aFilter->GetBackendType() != FILTER_BACKEND_SOFTWARE) {
MOZ_ASSERT(false, "can only take software filters as inputs");
return;
}
SetInput(aIndex, nullptr, static_cast<FilterNodeSoftware*>(aFilter));
}
void
FilterNodeSoftware::SetInput(uint32_t aIndex, SourceSurface *aSurface)
{
SetInput(aIndex, aSurface, nullptr);
}
void
FilterNodeSoftware::SetInput(uint32_t aInputEnumIndex,
SourceSurface *aSurface,
FilterNodeSoftware *aFilter)
{
int32_t inputIndex = InputIndex(aInputEnumIndex);
if (inputIndex < 0) {
gfxDevCrash(LogReason::FilterInputSet) << "Invalid set " << inputIndex;
return;
}
if ((uint32_t)inputIndex >= NumberOfSetInputs()) {
mInputSurfaces.resize(inputIndex + 1);
mInputFilters.resize(inputIndex + 1);
}
mInputSurfaces[inputIndex] = aSurface;
if (mInputFilters[inputIndex]) {
mInputFilters[inputIndex]->RemoveInvalidationListener(this);
}
if (aFilter) {
aFilter->AddInvalidationListener(this);
}
mInputFilters[inputIndex] = aFilter;
if (!aSurface && !aFilter && (size_t)inputIndex == NumberOfSetInputs()) {
mInputSurfaces.resize(inputIndex);
mInputFilters.resize(inputIndex);
}
Invalidate();
}
FilterNodeBlendSoftware::FilterNodeBlendSoftware()
: mBlendMode(BLEND_MODE_MULTIPLY)
{}
int32_t
FilterNodeBlendSoftware::InputIndex(uint32_t aInputEnumIndex)
{
switch (aInputEnumIndex) {
case IN_BLEND_IN: return 0;
case IN_BLEND_IN2: return 1;
default: return -1;
}
}
void
FilterNodeBlendSoftware::SetAttribute(uint32_t aIndex, uint32_t aBlendMode)
{
MOZ_ASSERT(aIndex == ATT_BLEND_BLENDMODE);
mBlendMode = static_cast<BlendMode>(aBlendMode);
Invalidate();
}
static CompositionOp ToBlendOp(BlendMode aOp)
{
switch (aOp) {
case BLEND_MODE_MULTIPLY:
return CompositionOp::OP_MULTIPLY;
case BLEND_MODE_SCREEN:
return CompositionOp::OP_SCREEN;
case BLEND_MODE_OVERLAY:
return CompositionOp::OP_OVERLAY;
case BLEND_MODE_DARKEN:
return CompositionOp::OP_DARKEN;
case BLEND_MODE_LIGHTEN:
return CompositionOp::OP_LIGHTEN;
case BLEND_MODE_COLOR_DODGE:
return CompositionOp::OP_COLOR_DODGE;
case BLEND_MODE_COLOR_BURN:
return CompositionOp::OP_COLOR_BURN;
case BLEND_MODE_HARD_LIGHT:
return CompositionOp::OP_HARD_LIGHT;
case BLEND_MODE_SOFT_LIGHT:
return CompositionOp::OP_SOFT_LIGHT;
case BLEND_MODE_DIFFERENCE:
return CompositionOp::OP_DIFFERENCE;
case BLEND_MODE_EXCLUSION:
return CompositionOp::OP_EXCLUSION;
case BLEND_MODE_HUE:
return CompositionOp::OP_HUE;
case BLEND_MODE_SATURATION:
return CompositionOp::OP_SATURATION;
case BLEND_MODE_COLOR:
return CompositionOp::OP_COLOR;
case BLEND_MODE_LUMINOSITY:
return CompositionOp::OP_LUMINOSITY;
default:
return CompositionOp::OP_OVER;
}
return CompositionOp::OP_OVER;
}
already_AddRefed<DataSourceSurface>
FilterNodeBlendSoftware::Render(const IntRect& aRect)
{
RefPtr<DataSourceSurface> input1 =
GetInputDataSourceSurface(IN_BLEND_IN, aRect, NEED_COLOR_CHANNELS);
RefPtr<DataSourceSurface> input2 =
GetInputDataSourceSurface(IN_BLEND_IN2, aRect, NEED_COLOR_CHANNELS);
// Null inputs need to be treated as transparent.
// First case: both are transparent.
if (!input1 && !input2) {
// Then the result is transparent, too.
return nullptr;
}
// Second case: one of them is transparent. Return the non-transparent one.
if (!input1 || !input2) {
return input1 ? input1.forget() : input2.forget();
}
// Third case: both are non-transparent.
// Apply normal filtering.
RefPtr<DataSourceSurface> target = FilterProcessing::ApplyBlending(input1, input2, mBlendMode);
if (target != nullptr) {
return target.forget();
}
IntSize size = input1->GetSize();
target =
Factory::CreateDataSourceSurface(size, SurfaceFormat::B8G8R8A8);
if (MOZ2D_WARN_IF(!target)) {