File: FilterNodeWebgl.cpp

package info (click to toggle)
firefox 148.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,719,656 kB
  • sloc: cpp: 7,618,171; javascript: 6,701,506; ansic: 3,781,787; python: 1,418,364; xml: 638,647; asm: 438,962; java: 186,285; sh: 62,885; makefile: 19,010; objc: 13,092; perl: 12,763; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (1053 lines) | stat: -rw-r--r-- 36,310 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "FilterNodeWebgl.h"

#include <limits>

#include "DrawTargetWebglInternal.h"
#include "SourceSurfaceWebgl.h"
#include "mozilla/PodOperations.h"
#include "mozilla/gfx/Blur.h"
#include "mozilla/gfx/DrawTargetSkia.h"
#include "mozilla/gfx/FilterNodeSoftware.h"
#include "mozilla/gfx/Helpers.h"
#include "mozilla/gfx/Logging.h"

namespace mozilla::gfx {

FilterNodeWebgl::FilterNodeWebgl(FilterType aType)
    : mType(aType),
      mSoftwareFilter(
          FilterNodeSoftware::Create(aType).downcast<FilterNodeSoftware>()) {}

FilterNodeWebgl::~FilterNodeWebgl() = default;

already_AddRefed<FilterNodeWebgl> FilterNodeWebgl::Create(FilterType aType) {
  RefPtr<FilterNodeWebgl> filter;
  switch (aType) {
    case FilterType::CROP:
      filter = new FilterNodeCropWebgl;
      break;
    case FilterType::TRANSFORM:
      filter = new FilterNodeTransformWebgl;
      break;
    case FilterType::GAUSSIAN_BLUR:
      filter = new FilterNodeGaussianBlurWebgl;
      break;
    case FilterType::PREMULTIPLY:
      filter = new FilterNodePremultiplyWebgl;
      break;
    case FilterType::UNPREMULTIPLY:
      filter = new FilterNodeUnpremultiplyWebgl;
      break;
    case FilterType::COLOR_MATRIX:
      filter = new FilterNodeColorMatrixWebgl;
      break;
    case FilterType::LINEAR_TRANSFER:
      filter = new FilterNodeLinearTransferWebgl;
      break;
    case FilterType::TABLE_TRANSFER:
      filter = new FilterNodeTableTransferWebgl;
      break;
    case FilterType::OPACITY:
      filter = new FilterNodeOpacityWebgl;
      break;
    default:
      filter = new FilterNodeWebgl(aType);
      break;
  }
  return filter.forget();
}

int32_t FilterNodeWebgl::InputIndex(uint32_t aInputEnumIndex) const {
  if (mSoftwareFilter) {
    return mSoftwareFilter->InputIndex(aInputEnumIndex);
  }
  return -1;
}

bool FilterNodeWebgl::ReserveInputIndex(uint32_t aIndex) {
  size_t inputIndex = aIndex;
  if (std::numeric_limits<size_t>::max() - inputIndex < 1) {
    return false;
  }
  if (mInputSurfaces.size() <= inputIndex) {
    mInputSurfaces.resize(inputIndex + 1);
  }
  if (mInputFilters.size() <= inputIndex) {
    mInputFilters.resize(inputIndex + 1);
  }
  return true;
}

bool FilterNodeWebgl::SetInputAccel(uint32_t aIndex, SourceSurface* aSurface) {
  if (ReserveInputIndex(aIndex)) {
    mInputSurfaces[aIndex] = aSurface;
    mInputFilters[aIndex] = nullptr;
    return true;
  }
  return false;
}

bool FilterNodeWebgl::SetInputSoftware(uint32_t aIndex,
                                       SourceSurface* aSurface) {
  if (mSoftwareFilter) {
    mSoftwareFilter->SetInput(aIndex, aSurface);
  }
  mInputMask |= (1 << aIndex);
  return true;
}

void FilterNodeWebgl::SetInput(uint32_t aIndex, SourceSurface* aSurface) {
  int32_t inputIndex = InputIndex(aIndex);
  if (inputIndex < 0 || !SetInputAccel(inputIndex, aSurface) ||
      !SetInputSoftware(inputIndex, aSurface)) {
    gfxDevCrash(LogReason::FilterInputSet) << "Invalid set " << inputIndex;
    return;
  }
}

void FilterNodeWebgl::SetInput(uint32_t aIndex, FilterNode* aFilter) {
  if (aFilter && aFilter->GetBackendType() != FILTER_BACKEND_WEBGL) {
    MOZ_ASSERT(false, "FilterNodeWebgl required as input");
    return;
  }

  int32_t inputIndex = InputIndex(aIndex);
  if (inputIndex < 0 || !ReserveInputIndex(inputIndex)) {
    gfxDevCrash(LogReason::FilterInputSet) << "Invalid set " << inputIndex;
    return;
  }

  auto* webglFilter = static_cast<FilterNodeWebgl*>(aFilter);
  mInputFilters[inputIndex] = webglFilter;
  mInputSurfaces[inputIndex] = nullptr;
  if (mSoftwareFilter) {
    MOZ_ASSERT(!webglFilter || webglFilter->mSoftwareFilter);
    mSoftwareFilter->SetInput(
        aIndex, webglFilter ? webglFilter->mSoftwareFilter.get() : nullptr);
  }
}

void FilterNodeWebgl::SetAttribute(uint32_t aIndex, bool aValue) {
  if (mSoftwareFilter) {
    mSoftwareFilter->SetAttribute(aIndex, aValue);
  }
}

void FilterNodeWebgl::SetAttribute(uint32_t aIndex, uint32_t aValue) {
  if (mSoftwareFilter) {
    mSoftwareFilter->SetAttribute(aIndex, aValue);
  }
}

void FilterNodeWebgl::SetAttribute(uint32_t aIndex, Float aValue) {
  if (mSoftwareFilter) {
    mSoftwareFilter->SetAttribute(aIndex, aValue);
  }
}

void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const Size& aValue) {
  if (mSoftwareFilter) {
    mSoftwareFilter->SetAttribute(aIndex, aValue);
  }
}

void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const IntSize& aValue) {
  if (mSoftwareFilter) {
    mSoftwareFilter->SetAttribute(aIndex, aValue);
  }
}

void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const IntPoint& aValue) {
  if (mSoftwareFilter) {
    mSoftwareFilter->SetAttribute(aIndex, aValue);
  }
}

void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const Rect& aValue) {
  if (mSoftwareFilter) {
    mSoftwareFilter->SetAttribute(aIndex, aValue);
  }
}

void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const IntRect& aValue) {
  if (mSoftwareFilter) {
    mSoftwareFilter->SetAttribute(aIndex, aValue);
  }
}

void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const Point& aValue) {
  if (mSoftwareFilter) {
    mSoftwareFilter->SetAttribute(aIndex, aValue);
  }
}

void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const Matrix& aValue) {
  if (mSoftwareFilter) {
    mSoftwareFilter->SetAttribute(aIndex, aValue);
  }
}

void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const Matrix5x4& aValue) {
  if (mSoftwareFilter) {
    mSoftwareFilter->SetAttribute(aIndex, aValue);
  }
}

void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const Point3D& aValue) {
  if (mSoftwareFilter) {
    mSoftwareFilter->SetAttribute(aIndex, aValue);
  }
}

void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const DeviceColor& aValue) {
  if (mSoftwareFilter) {
    mSoftwareFilter->SetAttribute(aIndex, aValue);
  }
}

void FilterNodeWebgl::SetAttribute(uint32_t aIndex, const Float* aValues,
                                   uint32_t aSize) {
  if (mSoftwareFilter) {
    mSoftwareFilter->SetAttribute(aIndex, aValues, aSize);
  }
}

IntRect FilterNodeWebgl::MapRectToSource(const IntRect& aRect,
                                         const IntRect& aMax,
                                         FilterNode* aSourceNode) {
  if (mSoftwareFilter) {
    if (aSourceNode && aSourceNode->GetBackendType() == FILTER_BACKEND_WEBGL) {
      aSourceNode = static_cast<FilterNodeWebgl*>(aSourceNode)->mSoftwareFilter;
    }
    return mSoftwareFilter->MapRectToSource(aRect, aMax, aSourceNode);
  }
  return aMax;
}

void FilterNodeWebgl::Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
                           const Point& aDestPoint, const DrawOptions& aOptions,
                           FilterNodeWebgl* aParent) {
  ResolveAllInputs(aDT, aParent);

  MOZ_ASSERT(mSoftwareFilter);
  aDT->DrawFilterFallback(mSoftwareFilter, aSourceRect, aDestPoint, aOptions);
}

already_AddRefed<SourceSurface> FilterNodeWebgl::DrawChild(
    FilterNodeWebgl* aParent, DrawTargetWebgl* aDT, const Rect& aSourceRect,
    const DrawOptions& aOptions, Point& aSurfaceOffset, DeviceColor& aColor) {
  ResolveAllInputs(aDT, aParent);

  MOZ_ASSERT(mSoftwareFilter);
  RefPtr<DrawTarget> swDT = aDT->mSkia->CreateSimilarDrawTarget(
      IntSize::Ceil(aSourceRect.Size()), aDT->GetFormat());
  if (!swDT) {
    return nullptr;
  }
  swDT->DrawFilter(mSoftwareFilter, aSourceRect, Point(0, 0), aOptions);
  aSurfaceOffset = aSourceRect.TopLeft();
  aColor = DeviceColor(1, 1, 1, 1);
  return swDT->Snapshot();
}

IntRect FilterNodeWebgl::MapInputRectToSource(uint32_t aInputEnumIndex,
                                              const IntRect& aRect,
                                              const IntRect& aMax,
                                              FilterNode* aSourceNode) {
  int32_t inputIndex = InputIndex(aInputEnumIndex);
  if (inputIndex < 0) {
    gfxDevCrash(LogReason::FilterInputError)
        << "Invalid input " << inputIndex << " vs. " << NumberOfSetInputs();
    return aMax;
  }
  if ((uint32_t)inputIndex < NumberOfSetInputs()) {
    if (RefPtr<FilterNodeWebgl> filter = mInputFilters[inputIndex]) {
      return filter->MapRectToSource(aRect, aMax, aSourceNode);
    }
  }
  if (this == aSourceNode) {
    return aRect;
  }
  return IntRect();
}

void FilterNodeWebgl::ResolveAllInputs(DrawTargetWebgl* aDT,
                                       FilterNodeWebgl* aParent) {
  ResolveInputs(aDT, false, aParent);
  for (const auto& filter : mInputFilters) {
    if (filter) {
      filter->ResolveAllInputs(aDT, this);
    }
  }
}

int32_t FilterNodeCropWebgl::InputIndex(uint32_t aInputEnumIndex) const {
  switch (aInputEnumIndex) {
    case IN_CROP_IN:
      return 0;
    default:
      return -1;
  }
}

void FilterNodeCropWebgl::SetAttribute(uint32_t aIndex, const Rect& aValue) {
  MOZ_ASSERT(aIndex == ATT_CROP_RECT);
  Rect srcRect = aValue;
  srcRect.Round();
  if (!srcRect.ToIntRect(&mCropRect)) {
    mCropRect = IntRect();
  }
  FilterNodeWebgl::SetAttribute(aIndex, aValue);
}

IntRect FilterNodeCropWebgl::MapRectToSource(const IntRect& aRect,
                                             const IntRect& aMax,
                                             FilterNode* aSourceNode) {
  return MapInputRectToSource(IN_CROP_IN, aRect.Intersect(mCropRect), aMax,
                              aSourceNode);
}

void FilterNodeCropWebgl::Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
                               const Point& aDestPoint,
                               const DrawOptions& aOptions,
                               FilterNodeWebgl* aParent) {
  ResolveInputs(aDT, true, aParent);

  uint32_t inputIdx = InputIndex(IN_CROP_IN);
  if (inputIdx < NumberOfSetInputs()) {
    Rect croppedSource = aSourceRect.Intersect(Rect(mCropRect));
    if (RefPtr<FilterNodeWebgl> filter = mInputFilters[inputIdx]) {
      filter->Draw(aDT, croppedSource,
                   aDestPoint + croppedSource.TopLeft() - aSourceRect.TopLeft(),
                   aOptions, this);
    } else if (RefPtr<SourceSurface> surface = mInputSurfaces[inputIdx]) {
      aDT->DrawSurface(surface,
                       croppedSource - aSourceRect.TopLeft() + aDestPoint,
                       croppedSource, DrawSurfaceOptions(), aOptions);
    }
  }
}

bool FilterNodeCropWebgl::DrawAccel(DrawTargetWebgl* aDT,
                                    const Rect& aSourceRect,
                                    const Point& aDestPoint,
                                    const DrawOptions& aOptions,
                                    FilterNodeWebgl* aParent) {
  uint32_t inputIdx = InputIndex(IN_CROP_IN);
  if (inputIdx < NumberOfSetInputs() && mInputFilters[inputIdx]) {
    Rect croppedSource = aSourceRect.Intersect(Rect(mCropRect));
    FilterNodeWebgl* filter = mInputFilters[inputIdx];
    switch (filter->GetType()) {
      case FilterType::COLOR_MATRIX:
      case FilterType::LINEAR_TRANSFER:
      case FilterType::TABLE_TRANSFER:
        // Crop filters are sometimes generated before evaluating a color matrix
        // filter.
        return filter->DrawAccel(
            aDT, croppedSource,
            aDestPoint + croppedSource.TopLeft() - aSourceRect.TopLeft(),
            aOptions, this);
      default:
        break;
    }
  }
  return false;
}

already_AddRefed<SourceSurface> FilterNodeCropWebgl::DrawChild(
    FilterNodeWebgl* aParent, DrawTargetWebgl* aDT, const Rect& aSourceRect,
    const DrawOptions& aOptions, Point& aSurfaceOffset, DeviceColor& aColor) {
  ResolveInputs(aDT, true, aParent);

  uint32_t inputIdx = InputIndex(IN_CROP_IN);
  if (inputIdx < NumberOfSetInputs()) {
    if (RefPtr<FilterNodeWebgl> filter = mInputFilters[inputIdx]) {
      Rect croppedSource = aSourceRect.Intersect(Rect(mCropRect));
      return filter->DrawChild(this, aDT, croppedSource, aOptions,
                               aSurfaceOffset, aColor);
    }
    return FilterNodeWebgl::DrawChild(aParent, aDT, aSourceRect, aOptions,
                                      aSurfaceOffset, aColor);
  }
  return nullptr;
}

int32_t FilterNodeTransformWebgl::InputIndex(uint32_t aInputEnumIndex) const {
  switch (aInputEnumIndex) {
    case IN_TRANSFORM_IN:
      return 0;
    default:
      return -1;
  }
}

void FilterNodeTransformWebgl::SetAttribute(uint32_t aIndex, uint32_t aValue) {
  MOZ_ASSERT(aIndex == ATT_TRANSFORM_FILTER);
  mSamplingFilter = static_cast<SamplingFilter>(aValue);
  FilterNodeWebgl::SetAttribute(aIndex, aValue);
}

void FilterNodeTransformWebgl::SetAttribute(uint32_t aIndex,
                                            const Matrix& aValue) {
  MOZ_ASSERT(aIndex == ATT_TRANSFORM_MATRIX);
  mMatrix = aValue;
  FilterNodeWebgl::SetAttribute(aIndex, aValue);
}

IntRect FilterNodeTransformWebgl::MapRectToSource(const IntRect& aRect,
                                                  const IntRect& aMax,
                                                  FilterNode* aSourceNode) {
  if (aRect.IsEmpty()) {
    return IntRect();
  }
  Matrix inv(mMatrix);
  if (!inv.Invert()) {
    return aMax;
  }
  Rect rect = inv.TransformBounds(Rect(aRect));
  rect.RoundOut();
  IntRect intRect;
  if (!rect.ToIntRect(&intRect)) {
    return aMax;
  }
  return MapInputRectToSource(IN_TRANSFORM_IN, intRect, aMax, aSourceNode);
}

void FilterNodeTransformWebgl::Draw(DrawTargetWebgl* aDT,
                                    const Rect& aSourceRect,
                                    const Point& aDestPoint,
                                    const DrawOptions& aOptions,
                                    FilterNodeWebgl* aParent) {
  if (!mMatrix.IsTranslation()) {
    FilterNodeWebgl::Draw(aDT, aSourceRect, aDestPoint, aOptions, aParent);
    return;
  }

  ResolveInputs(aDT, true, aParent);

  uint32_t inputIdx = InputIndex(IN_TRANSFORM_IN);
  if (inputIdx < NumberOfSetInputs()) {
    if (RefPtr<FilterNodeWebgl> filter = mInputFilters[inputIdx]) {
      filter->Draw(aDT, aSourceRect - mMatrix.GetTranslation(), aDestPoint,
                   aOptions, aParent);
    } else if (RefPtr<SourceSurface> surface = mInputSurfaces[inputIdx]) {
      aDT->DrawSurface(surface, Rect(aDestPoint, aSourceRect.Size()),
                       aSourceRect - mMatrix.GetTranslation(),
                       DrawSurfaceOptions(mSamplingFilter), aOptions);
    }
  }
}

already_AddRefed<SourceSurface> FilterNodeTransformWebgl::DrawChild(
    FilterNodeWebgl* aParent, DrawTargetWebgl* aDT, const Rect& aSourceRect,
    const DrawOptions& aOptions, Point& aSurfaceOffset, DeviceColor& aColor) {
  if (!mMatrix.IsIntegerTranslation()) {
    return FilterNodeWebgl::DrawChild(aParent, aDT, aSourceRect, aOptions,
                                      aSurfaceOffset, aColor);
  }

  ResolveInputs(aDT, true, aParent);

  uint32_t inputIdx = InputIndex(IN_TRANSFORM_IN);
  if (inputIdx < NumberOfSetInputs()) {
    if (RefPtr<SourceSurface> surface = mInputSurfaces[inputIdx]) {
      aSurfaceOffset = mMatrix.GetTranslation().Round();
      aColor = DeviceColor(1, 1, 1, aOptions.mAlpha);
      return surface.forget();
    }
    return FilterNodeWebgl::DrawChild(aParent, aDT, aSourceRect, aOptions,
                                      aSurfaceOffset, aColor);
  }
  return nullptr;
}

FilterNodeDeferInputWebgl::FilterNodeDeferInputWebgl(
    RefPtr<Path> aPath, const Pattern& aPattern, const IntRect& aSourceRect,
    const Matrix& aDestTransform, const DrawOptions& aOptions,
    const StrokeOptions* aStrokeOptions)
    : mPath(std::move(aPath)),
      mSourceRect(aSourceRect),
      mDestTransform(aDestTransform),
      mOptions(aOptions) {
  mPattern.Init(aPattern);
  if (aStrokeOptions) {
    mStrokeOptions = Some(*aStrokeOptions);
    if (aStrokeOptions->mDashLength > 0) {
      mDashPatternStorage.reset(new Float[aStrokeOptions->mDashLength]);
      PodCopy(mDashPatternStorage.get(), aStrokeOptions->mDashPattern,
              aStrokeOptions->mDashLength);
      mStrokeOptions->mDashPattern = mDashPatternStorage.get();
    }
  }
  SetAttribute(ATT_TRANSFORM_MATRIX,
               Matrix::Translation(mSourceRect.TopLeft()));
}

void FilterNodeDeferInputWebgl::ResolveInputs(DrawTargetWebgl* aDT, bool aAccel,
                                              FilterNodeWebgl* aParent) {
  uint32_t inputIdx = InputIndex(IN_TRANSFORM_IN);
  bool hasAccel = false;
  if (inputIdx < NumberOfSetInputs() && mInputSurfaces[inputIdx]) {
    if (aAccel || (mInputMask & (1 << inputIdx))) {
      return;
    }
    hasAccel = true;
  }
  RefPtr<SourceSurface> surface;
  SurfaceFormat format = SurfaceFormat::B8G8R8A8;
  static const ColorPattern maskPattern(DeviceColor(1, 1, 1, 1));
  const Pattern* pattern = mPattern.GetPattern();
  if (aAccel) {
    // If using acceleration on a color pattern, attempt to blur solely on the
    // alpha values to significantly reduce data churn, as the color will only
    // vary linearly with alpha over the input surface. The color will be
    // incorporated on the final mask draw.
    if (mPattern.GetPattern()->GetType() == PatternType::COLOR) {
      format = SurfaceFormat::A8;
      pattern = &maskPattern;
    }
    surface = aDT->ResolveFilterInputAccel(
        mPath, *pattern, mSourceRect, mDestTransform, mOptions,
        mStrokeOptions.ptrOr(nullptr), format);
  }
  if (!surface) {
    surface = aDT->mSkia->ResolveFilterInput(
        mPath, *pattern, mSourceRect, mDestTransform, mOptions,
        mStrokeOptions.ptrOr(nullptr), format);
  }
  if (hasAccel) {
    SetInputSoftware(inputIdx, surface);
  } else if (surface && surface->GetFormat() == SurfaceFormat::A8) {
    SetInputAccel(inputIdx, surface);
  } else {
    SetInput(inputIdx, surface);
  }
}

void FilterNodeDeferInputWebgl::Draw(DrawTargetWebgl* aDT,
                                     const Rect& aSourceRect,
                                     const Point& aDestPoint,
                                     const DrawOptions& aOptions,
                                     FilterNodeWebgl* aParent) {
  const Pattern* pattern = mPattern.GetPattern();
  AutoRestoreTransform restore(aDT);
  aDT->PushClipRect(Rect(aDestPoint, aSourceRect.Size()));
  aDT->ConcatTransform(
      Matrix(mDestTransform).PostTranslate(aDestPoint - aSourceRect.TopLeft()));
  DrawOptions options(aOptions.mAlpha * mOptions.mAlpha,
                      aOptions.mCompositionOp, mOptions.mAntialiasMode);
  if (mStrokeOptions) {
    aDT->Stroke(mPath, *pattern, *mStrokeOptions, options);
  } else {
    aDT->Fill(mPath, *pattern, options);
  }
  aDT->PopClip();
}

already_AddRefed<SourceSurface> FilterNodeDeferInputWebgl::DrawChild(
    FilterNodeWebgl* aParent, DrawTargetWebgl* aDT, const Rect& aSourceRect,
    const DrawOptions& aOptions, Point& aSurfaceOffset, DeviceColor& aColor) {
  ResolveInputs(aDT, true, aParent);

  uint32_t inputIdx = InputIndex(IN_TRANSFORM_IN);
  if (inputIdx < NumberOfSetInputs()) {
    if (RefPtr<SourceSurface> surface = mInputSurfaces[inputIdx]) {
      aSurfaceOffset = mMatrix.GetTranslation().Round();
      // If the output will be a mask, then supply the color that should be
      // rendered with it.
      aColor =
          mPattern.GetPattern()->GetType() == PatternType::COLOR
              ? static_cast<const ColorPattern*>(mPattern.GetPattern())->mColor
              : DeviceColor(1, 1, 1, 1);
      aColor.a *= aOptions.mAlpha;
      return surface.forget();
    }
    return FilterNodeWebgl::DrawChild(aParent, aDT, aSourceRect, aOptions,
                                      aSurfaceOffset, aColor);
  }
  return nullptr;
}

int32_t FilterNodeGaussianBlurWebgl::InputIndex(
    uint32_t aInputEnumIndex) const {
  switch (aInputEnumIndex) {
    case IN_GAUSSIAN_BLUR_IN:
      return 0;
    default:
      return -1;
  }
}

void FilterNodeGaussianBlurWebgl::SetAttribute(uint32_t aIndex, float aValue) {
  MOZ_ASSERT(aIndex == ATT_GAUSSIAN_BLUR_STD_DEVIATION);
  // Match the FilterNodeSoftware blur limit.
  mStdDeviation = std::clamp(aValue, 0.0f, 100.0f);
  FilterNodeWebgl::SetAttribute(aIndex, aValue);
}

IntRect FilterNodeGaussianBlurWebgl::MapRectToSource(const IntRect& aRect,
                                                     const IntRect& aMax,
                                                     FilterNode* aSourceNode) {
  return MapInputRectToSource(IN_GAUSSIAN_BLUR_IN, aRect, aMax, aSourceNode);
}

void FilterNodeGaussianBlurWebgl::Draw(DrawTargetWebgl* aDT,
                                       const Rect& aSourceRect,
                                       const Point& aDestPoint,
                                       const DrawOptions& aOptions,
                                       FilterNodeWebgl* aParent) {
  ResolveInputs(aDT, true, aParent);

  uint32_t inputIdx = InputIndex(IN_GAUSSIAN_BLUR_IN);
  if (inputIdx < NumberOfSetInputs()) {
    bool success = false;
    Point surfaceOffset;
    DeviceColor color(1, 1, 1, 1);
    if (RefPtr<SourceSurface> surface =
            mInputFilters[inputIdx] ? mInputFilters[inputIdx]->DrawChild(
                                          this, aDT, aSourceRect, DrawOptions(),
                                          surfaceOffset, color)
                                    : mInputSurfaces[inputIdx]) {
      aDT->PushClipRect(Rect(aDestPoint, aSourceRect.Size()));
      IntRect surfRect = RoundedOut(
          Rect(surface->GetRect()).Intersect(aSourceRect - surfaceOffset));
      Point destOffset = aDestPoint + Point(surfRect.TopLeft()) +
                         surfaceOffset - aSourceRect.TopLeft();
      success = surfRect.IsEmpty() ||
                aDT->BlurSurface(mStdDeviation, surface, surfRect, destOffset,
                                 aOptions, color);
      aDT->PopClip();
    }
    if (!success) {
      FilterNodeWebgl::Draw(aDT, aSourceRect, aDestPoint, aOptions, aParent);
    }
  }
}

void FilterNodeColorMatrixWebgl::SetAttribute(uint32_t aIndex,
                                              const Matrix5x4& aValue) {
  MOZ_ASSERT(aIndex == ATT_COLOR_MATRIX_MATRIX);
  mMatrix = aValue;
  FilterNodeWebgl::SetAttribute(aIndex, aValue);
}

void FilterNodeColorMatrixWebgl::SetAttribute(uint32_t aIndex,
                                              uint32_t aValue) {
  MOZ_ASSERT(aIndex == ATT_COLOR_MATRIX_ALPHA_MODE);
  mAlphaMode = (AlphaMode)aValue;
  FilterNodeWebgl::SetAttribute(aIndex, aValue);
}

int32_t FilterNodeColorMatrixWebgl::InputIndex(uint32_t aInputEnumIndex) const {
  switch (aInputEnumIndex) {
    case IN_COLOR_MATRIX_IN:
      return 0;
    default:
      return -1;
  }
}

static bool DrawColorMatrixFilter(DrawTargetWebgl* aDT, const Point& aDestPoint,
                                  const DrawOptions& aOptions,
                                  const RefPtr<SourceSurface>& aSurface,
                                  const Rect& aSourceRect,
                                  const Point& aSurfaceOffset,
                                  const Matrix5x4& aMatrix,
                                  const DeviceColor& aColor) {
  IntRect surfRect = RoundedOut(
      Rect(aSurface->GetRect()).Intersect(aSourceRect - aSurfaceOffset));
  if (surfRect.IsEmpty()) {
    return true;
  }
  aDT->PushClipRect(Rect(aDestPoint, aSourceRect.Size()));
  Point destOffset = aDestPoint + Point(surfRect.TopLeft()) + aSurfaceOffset -
                     aSourceRect.TopLeft();
  bool success = true;
  if (aSurface->GetFormat() == SurfaceFormat::A8) {
    // Mask surfaces only use a solid color that is supplied outside the
    // surface. This color can be transformed without requiring a shader.
    Point4D outColor =
        Matrix4x4(aMatrix.components)
            .TransformPoint(Point4D(aColor.r, aColor.g, aColor.b, aColor.a)) +
        Point4D(aMatrix._51, aMatrix._52, aMatrix._53, aMatrix._54);
    SurfacePattern maskPattern(aSurface, ExtendMode::CLAMP,
                               Matrix::Translation(destOffset));
    if (!surfRect.IsEqualEdges(aSurface->GetRect())) {
      maskPattern.mSamplingRect = surfRect;
    }
    aDT->Mask(ColorPattern(
                  DeviceColor(outColor.x, outColor.y, outColor.z, outColor.w)),
              maskPattern, aOptions);
  } else {
    // For normal surfaces, try to use the color matrix filter shader.
    success =
        aDT->FilterSurface(aMatrix, aSurface, surfRect, destOffset, aOptions);
  }
  aDT->PopClip();
  return success;
}

bool FilterNodeColorMatrixWebgl::DrawAccel(DrawTargetWebgl* aDT,
                                           const Rect& aSourceRect,
                                           const Point& aDestPoint,
                                           const DrawOptions& aOptions,
                                           FilterNodeWebgl* aParent) {
  if (!aParent || mAlphaMode != ALPHA_MODE_STRAIGHT) {
    return false;
  }
  switch (aParent->GetType()) {
    case FilterType::PREMULTIPLY:
    case FilterType::CROP:
      break;
    default:
      return false;
  }

  ResolveInputs(aDT, true, aParent);

  uint32_t inputIdx = InputIndex(IN_COLOR_MATRIX_IN);
  if (inputIdx < NumberOfSetInputs() && mInputFilters[inputIdx]) {
    FilterNodeWebgl* filter = mInputFilters[inputIdx];
    if (filter->GetType() == FilterType::UNPREMULTIPLY) {
      bool success = false;
      Point surfaceOffset;
      DeviceColor color;
      if (RefPtr<SourceSurface> surface = filter->DrawChild(
              this, aDT, aSourceRect, DrawOptions(), surfaceOffset, color)) {
        success =
            DrawColorMatrixFilter(aDT, aDestPoint, aOptions, surface,
                                  aSourceRect, surfaceOffset, mMatrix, color);
      }
      return success;
    }
  }
  return false;
}

void FilterNodeComponentTransferWebgl::SetAttribute(uint32_t aIndex,
                                                    bool aValue) {
  switch (aIndex) {
    case ATT_TRANSFER_DISABLE_R:
      mDisableR = aValue;
      break;
    case ATT_TRANSFER_DISABLE_G:
      mDisableG = aValue;
      break;
    case ATT_TRANSFER_DISABLE_B:
      mDisableB = aValue;
      break;
    case ATT_TRANSFER_DISABLE_A:
      mDisableA = aValue;
      break;
    default:
      gfxDevCrash(LogReason::FilterInputError)
          << "FilterNodeComponentTransferWebgl: Invalid attribute " << aIndex;
      break;
  }
  FilterNodeWebgl::SetAttribute(aIndex, aValue);
}

int32_t FilterNodeComponentTransferWebgl::InputIndex(
    uint32_t aInputEnumIndex) const {
  switch (aInputEnumIndex) {
    case IN_TRANSFER_IN:
      return 0;
    default:
      return -1;
  }
}

bool FilterNodeComponentTransferWebgl::DrawAccel(DrawTargetWebgl* aDT,
                                                 const Rect& aSourceRect,
                                                 const Point& aDestPoint,
                                                 const DrawOptions& aOptions,
                                                 FilterNodeWebgl* aParent) {
  if (!aParent) {
    return false;
  }
  switch (aParent->GetType()) {
    case FilterType::PREMULTIPLY:
    case FilterType::CROP:
      break;
    default:
      return false;
  }

  Matrix5x4 mat5x4;
  if (!ToColorMatrix(mat5x4)) {
    return false;
  }

  ResolveInputs(aDT, true, aParent);

  uint32_t inputIdx = InputIndex(IN_TRANSFER_IN);
  if (inputIdx < NumberOfSetInputs() && mInputFilters[inputIdx]) {
    FilterNodeWebgl* filter = mInputFilters[inputIdx];
    if (filter->GetType() == FilterType::UNPREMULTIPLY) {
      bool success = false;
      Point surfaceOffset;
      DeviceColor color;
      if (RefPtr<SourceSurface> surface = filter->DrawChild(
              this, aDT, aSourceRect, DrawOptions(), surfaceOffset, color)) {
        success =
            DrawColorMatrixFilter(aDT, aDestPoint, aOptions, surface,
                                  aSourceRect, surfaceOffset, mat5x4, color);
      }
      return success;
    }
  }
  return false;
}

void FilterNodeLinearTransferWebgl::SetAttribute(uint32_t aIndex,
                                                 Float aValue) {
  switch (aIndex) {
    case ATT_LINEAR_TRANSFER_SLOPE_R:
      mSlope.r = aValue;
      break;
    case ATT_LINEAR_TRANSFER_INTERCEPT_R:
      mIntercept.r = aValue;
      break;
    case ATT_LINEAR_TRANSFER_SLOPE_G:
      mSlope.g = aValue;
      break;
    case ATT_LINEAR_TRANSFER_INTERCEPT_G:
      mIntercept.g = aValue;
      break;
    case ATT_LINEAR_TRANSFER_SLOPE_B:
      mSlope.b = aValue;
      break;
    case ATT_LINEAR_TRANSFER_INTERCEPT_B:
      mIntercept.b = aValue;
      break;
    case ATT_LINEAR_TRANSFER_SLOPE_A:
      mSlope.a = aValue;
      break;
    case ATT_LINEAR_TRANSFER_INTERCEPT_A:
      mIntercept.a = aValue;
      break;
    default:
      MOZ_ASSERT(false);
      break;
  }
  FilterNodeWebgl::SetAttribute(aIndex, aValue);
}

bool FilterNodeLinearTransferWebgl::ToColorMatrix(Matrix5x4& aMatrix) const {
  // Linear filters can be interpreted as a scale and translation matrix.
  aMatrix = Matrix5x4();
  if (!mDisableR) {
    aMatrix._11 = mSlope.r;
    aMatrix._51 = mIntercept.r;
  }
  if (!mDisableG) {
    aMatrix._22 = mSlope.g;
    aMatrix._52 = mIntercept.g;
  }
  if (!mDisableB) {
    aMatrix._33 = mSlope.b;
    aMatrix._53 = mIntercept.b;
  }
  if (!mDisableA) {
    aMatrix._44 = mSlope.a;
    aMatrix._54 = mIntercept.a;
  }
  return true;
}

void FilterNodeTableTransferWebgl::SetAttribute(uint32_t aIndex,
                                                const Float* aValues,
                                                uint32_t aSize) {
  std::vector<Float> table(aValues, aValues + aSize);
  switch (aIndex) {
    case ATT_TABLE_TRANSFER_TABLE_R:
      mTableR = table;
      break;
    case ATT_TABLE_TRANSFER_TABLE_G:
      mTableG = table;
      break;
    case ATT_TABLE_TRANSFER_TABLE_B:
      mTableB = table;
      break;
    case ATT_TABLE_TRANSFER_TABLE_A:
      mTableA = table;
      break;
    default:
      MOZ_ASSERT(false);
      break;
  }
  FilterNodeWebgl::SetAttribute(aIndex, aValues, aSize);
}

bool FilterNodeTableTransferWebgl::ToColorMatrix(Matrix5x4& aMatrix) const {
  // 2 element table transfers are effectively linear transfers. These can be
  // interpreted as a scale and translation matrix.
  if ((mDisableR || mTableR.size() == 2) &&
      (mDisableG || mTableG.size() == 2) &&
      (mDisableB || mTableB.size() == 2) &&
      (mDisableA || mTableA.size() == 2)) {
    aMatrix = Matrix5x4();
    if (!mDisableR) {
      aMatrix._11 = mTableR[1] - mTableR[0];
      aMatrix._51 = mTableR[0];
    }
    if (!mDisableG) {
      aMatrix._22 = mTableG[1] - mTableG[0];
      aMatrix._52 = mTableG[0];
    }
    if (!mDisableB) {
      aMatrix._33 = mTableB[1] - mTableB[0];
      aMatrix._53 = mTableB[0];
    }
    if (!mDisableA) {
      aMatrix._44 = mTableA[1] - mTableA[0];
      aMatrix._54 = mTableA[0];
    }
    return true;
  }
  return true;
}

int32_t FilterNodePremultiplyWebgl::InputIndex(uint32_t aInputEnumIndex) const {
  switch (aInputEnumIndex) {
    case IN_PREMULTIPLY_IN:
      return 0;
    default:
      return -1;
  }
}

void FilterNodePremultiplyWebgl::Draw(DrawTargetWebgl* aDT,
                                      const Rect& aSourceRect,
                                      const Point& aDestPoint,
                                      const DrawOptions& aOptions,
                                      FilterNodeWebgl* aParent) {
  uint32_t inputIdx = InputIndex(IN_PREMULTIPLY_IN);
  if (inputIdx < NumberOfSetInputs() && mInputFilters[inputIdx]) {
    FilterNodeWebgl* filter = mInputFilters[inputIdx];
    switch (filter->GetType()) {
      case FilterType::CROP:
      case FilterType::COLOR_MATRIX:
      case FilterType::LINEAR_TRANSFER:
      case FilterType::TABLE_TRANSFER:
        // For color matrix filters, they will normally be preceded by a premul
        // filter. In certain cases, after the premul there is a crop before the
        // color matrix filter is actually evaluated. Here, we use DrawAccel to
        // only handle the filter if it can actually be accelerated, otherwise
        // falling back to a software color matrix filter below.
        if (filter->DrawAccel(aDT, aSourceRect, aDestPoint, aOptions, this)) {
          return;
        }
        break;
      default:
        break;
    }
  }
  FilterNodeWebgl::Draw(aDT, aSourceRect, aDestPoint, aOptions, aParent);
}

int32_t FilterNodeUnpremultiplyWebgl::InputIndex(
    uint32_t aInputEnumIndex) const {
  switch (aInputEnumIndex) {
    case IN_UNPREMULTIPLY_IN:
      return 0;
    default:
      return -1;
  }
}

already_AddRefed<SourceSurface> FilterNodeUnpremultiplyWebgl::DrawChild(
    FilterNodeWebgl* aParent, DrawTargetWebgl* aDT, const Rect& aSourceRect,
    const DrawOptions& aOptions, Point& aSurfaceOffset, DeviceColor& aColor) {
  switch (aParent->GetType()) {
    case FilterType::COLOR_MATRIX:
    case FilterType::LINEAR_TRANSFER:
    case FilterType::TABLE_TRANSFER:
      // Unpremul should always be the child of a color matrix filter.
      break;
    default:
      return FilterNodeWebgl::DrawChild(aParent, aDT, aSourceRect, aOptions,
                                        aSurfaceOffset, aColor);
  }

  ResolveInputs(aDT, true, aParent);

  uint32_t inputIdx = InputIndex(IN_UNPREMULTIPLY_IN);
  if (inputIdx < NumberOfSetInputs()) {
    if (RefPtr<FilterNodeWebgl> filter = mInputFilters[inputIdx]) {
      if (RefPtr<SourceSurface> surface = filter->DrawChild(
              this, aDT, aSourceRect, aOptions, aSurfaceOffset, aColor)) {
        return surface.forget();
      }
    } else if (RefPtr<SourceSurface> surface = mInputSurfaces[inputIdx]) {
      aColor = DeviceColor(1, 1, 1, aOptions.mAlpha);
      return surface.forget();
    }
    return FilterNodeWebgl::DrawChild(aParent, aDT, aSourceRect, aOptions,
                                      aSurfaceOffset, aColor);
  }
  return nullptr;
}

int32_t FilterNodeOpacityWebgl::InputIndex(uint32_t aInputEnumIndex) const {
  switch (aInputEnumIndex) {
    case IN_OPACITY_IN:
      return 0;
    default:
      return -1;
  }
}

void FilterNodeOpacityWebgl::SetAttribute(uint32_t aIndex, Float aValue) {
  MOZ_ASSERT(aIndex == ATT_OPACITY_VALUE);
  mValue = aValue;
  FilterNodeWebgl::SetAttribute(aIndex, aValue);
}

void FilterNodeOpacityWebgl::Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
                                  const Point& aDestPoint,
                                  const DrawOptions& aOptions,
                                  FilterNodeWebgl* aParent) {
  ResolveInputs(aDT, true, aParent);

  uint32_t inputIdx = InputIndex(IN_OPACITY_IN);
  if (inputIdx < NumberOfSetInputs()) {
    // Opacity filters need only modify the DrawOptions alpha value.
    DrawOptions options(aOptions);
    options.mAlpha *= mValue;
    if (RefPtr<FilterNodeWebgl> filter = mInputFilters[inputIdx]) {
      filter->Draw(aDT, aSourceRect, aDestPoint, options, this);
    } else if (RefPtr<SourceSurface> surface = mInputSurfaces[inputIdx]) {
      aDT->DrawSurface(surface, Rect(aDestPoint, aSourceRect.Size()),
                       aSourceRect, DrawSurfaceOptions(), options);
    }
  }
}

already_AddRefed<SourceSurface> FilterNodeOpacityWebgl::DrawChild(
    FilterNodeWebgl* aParent, DrawTargetWebgl* aDT, const Rect& aSourceRect,
    const DrawOptions& aOptions, Point& aSurfaceOffset, DeviceColor& aColor) {
  ResolveInputs(aDT, true, aParent);

  uint32_t inputIdx = InputIndex(IN_OPACITY_IN);
  if (inputIdx < NumberOfSetInputs()) {
    if (RefPtr<FilterNodeWebgl> filter = mInputFilters[inputIdx]) {
      // Opacity filters need only modify he DrawOptions alpha value.
      DrawOptions options(aOptions);
      options.mAlpha *= mValue;
      return filter->DrawChild(this, aDT, aSourceRect, options, aSurfaceOffset,
                               aColor);
    }
    return FilterNodeWebgl::DrawChild(aParent, aDT, aSourceRect, aOptions,
                                      aSurfaceOffset, aColor);
  }
  return nullptr;
}

}  // namespace mozilla::gfx