forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsSVGUtils.cpp
1977 lines (1738 loc) · 63.2 KB
/
nsSVGUtils.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: 2; 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/. */
// Main header first:
// This is also necessary to ensure our definition of M_SQRT1_2 is picked up
#include "nsSVGUtils.h"
#include <algorithm>
// Keep others in (case-insensitive) order:
#include "gfxContext.h"
#include "gfxImageSurface.h"
#include "gfxMatrix.h"
#include "gfxPlatform.h"
#include "gfxRect.h"
#include "gfxUtils.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/Preferences.h"
#include "nsCSSFrameConstructor.h"
#include "nsDisplayList.h"
#include "nsFrameList.h"
#include "nsGkAtoms.h"
#include "nsIContent.h"
#include "nsIDocument.h"
#include "nsIFrame.h"
#include "nsINameSpaceManager.h"
#include "nsIPresShell.h"
#include "nsISVGChildFrame.h"
#include "nsPresContext.h"
#include "nsRenderingContext.h"
#include "nsStyleCoord.h"
#include "nsStyleStruct.h"
#include "nsSVGClipPathFrame.h"
#include "nsSVGContainerFrame.h"
#include "nsSVGEffects.h"
#include "nsSVGFilterFrame.h"
#include "nsSVGFilterPaintCallback.h"
#include "nsSVGForeignObjectFrame.h"
#include "nsSVGGeometryFrame.h"
#include "nsSVGGlyphFrame.h"
#include "nsSVGInnerSVGFrame.h"
#include "nsSVGIntegrationUtils.h"
#include "nsSVGLength2.h"
#include "nsSVGMaskFrame.h"
#include "nsSVGOuterSVGFrame.h"
#include "mozilla/dom/SVGPathElement.h"
#include "nsSVGPathGeometryElement.h"
#include "nsSVGPathGeometryFrame.h"
#include "nsSVGPaintServerFrame.h"
#include "mozilla/dom/SVGSVGElement.h"
#include "nsSVGTextContainerFrame.h"
#include "nsTextFrame.h"
#include "SVGContentUtils.h"
#include "mozilla/unused.h"
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::gfx;
// c = n / 255
// (c <= 0.0031308 ? c * 12.92 : 1.055 * pow(c, 1 / 2.4) - 0.055) * 255 + 0.5
static const uint8_t glinearRGBTosRGBMap[256] = {
0, 13, 22, 28, 34, 38, 42, 46,
50, 53, 56, 59, 61, 64, 66, 69,
71, 73, 75, 77, 79, 81, 83, 85,
86, 88, 90, 92, 93, 95, 96, 98,
99, 101, 102, 104, 105, 106, 108, 109,
110, 112, 113, 114, 115, 117, 118, 119,
120, 121, 122, 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, 148, 149, 150, 151,
152, 153, 154, 155, 155, 156, 157, 158,
159, 159, 160, 161, 162, 163, 163, 164,
165, 166, 167, 167, 168, 169, 170, 170,
171, 172, 173, 173, 174, 175, 175, 176,
177, 178, 178, 179, 180, 180, 181, 182,
182, 183, 184, 185, 185, 186, 187, 187,
188, 189, 189, 190, 190, 191, 192, 192,
193, 194, 194, 195, 196, 196, 197, 197,
198, 199, 199, 200, 200, 201, 202, 202,
203, 203, 204, 205, 205, 206, 206, 207,
208, 208, 209, 209, 210, 210, 211, 212,
212, 213, 213, 214, 214, 215, 215, 216,
216, 217, 218, 218, 219, 219, 220, 220,
221, 221, 222, 222, 223, 223, 224, 224,
225, 226, 226, 227, 227, 228, 228, 229,
229, 230, 230, 231, 231, 232, 232, 233,
233, 234, 234, 235, 235, 236, 236, 237,
237, 238, 238, 238, 239, 239, 240, 240,
241, 241, 242, 242, 243, 243, 244, 244,
245, 245, 246, 246, 246, 247, 247, 248,
248, 249, 249, 250, 250, 251, 251, 251,
252, 252, 253, 253, 254, 254, 255, 255
};
// c = n / 255
// c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4)) * 255 + 0.5
static const uint8_t gsRGBToLinearRGBMap[256] = {
0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 5, 5, 5,
5, 6, 6, 6, 6, 7, 7, 7,
8, 8, 8, 8, 9, 9, 9, 10,
10, 10, 11, 11, 12, 12, 12, 13,
13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 17, 18, 18, 19, 19, 20,
20, 21, 22, 22, 23, 23, 24, 24,
25, 25, 26, 27, 27, 28, 29, 29,
30, 30, 31, 32, 32, 33, 34, 35,
35, 36, 37, 37, 38, 39, 40, 41,
41, 42, 43, 44, 45, 45, 46, 47,
48, 49, 50, 51, 51, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, 62,
63, 64, 65, 66, 67, 68, 69, 70,
71, 72, 73, 74, 76, 77, 78, 79,
80, 81, 82, 84, 85, 86, 87, 88,
90, 91, 92, 93, 95, 96, 97, 99,
100, 101, 103, 104, 105, 107, 108, 109,
111, 112, 114, 115, 116, 118, 119, 121,
122, 124, 125, 127, 128, 130, 131, 133,
134, 136, 138, 139, 141, 142, 144, 146,
147, 149, 151, 152, 154, 156, 157, 159,
161, 163, 164, 166, 168, 170, 171, 173,
175, 177, 179, 181, 183, 184, 186, 188,
190, 192, 194, 196, 198, 200, 202, 204,
206, 208, 210, 212, 214, 216, 218, 220,
222, 224, 226, 229, 231, 233, 235, 237,
239, 242, 244, 246, 248, 250, 253, 255
};
static bool sSVGDisplayListHitTestingEnabled;
static bool sSVGDisplayListPaintingEnabled;
static bool sSVGTextCSSFramesEnabled;
bool
NS_SVGDisplayListHitTestingEnabled()
{
return sSVGDisplayListHitTestingEnabled;
}
bool
NS_SVGDisplayListPaintingEnabled()
{
return sSVGDisplayListPaintingEnabled;
}
bool
NS_SVGTextCSSFramesEnabled()
{
return sSVGTextCSSFramesEnabled;
}
// we only take the address of this:
static mozilla::gfx::UserDataKey sSVGAutoRenderStateKey;
SVGAutoRenderState::SVGAutoRenderState(nsRenderingContext *aContext,
RenderMode aMode)
: mContext(aContext)
, mOriginalRenderState(nullptr)
, mMode(aMode)
, mPaintingToWindow(false)
{
mOriginalRenderState = aContext->RemoveUserData(&sSVGAutoRenderStateKey);
// We always remove ourselves from aContext before it dies, so
// passing nullptr as the destroy function is okay.
aContext->AddUserData(&sSVGAutoRenderStateKey, this, nullptr);
}
SVGAutoRenderState::~SVGAutoRenderState()
{
mContext->RemoveUserData(&sSVGAutoRenderStateKey);
if (mOriginalRenderState) {
mContext->AddUserData(&sSVGAutoRenderStateKey, mOriginalRenderState, nullptr);
}
}
void
SVGAutoRenderState::SetPaintingToWindow(bool aPaintingToWindow)
{
mPaintingToWindow = aPaintingToWindow;
}
/* static */ SVGAutoRenderState::RenderMode
SVGAutoRenderState::GetRenderMode(nsRenderingContext *aContext)
{
void *state = aContext->GetUserData(&sSVGAutoRenderStateKey);
if (state) {
return static_cast<SVGAutoRenderState*>(state)->mMode;
}
return NORMAL;
}
/* static */ bool
SVGAutoRenderState::IsPaintingToWindow(nsRenderingContext *aContext)
{
void *state = aContext->GetUserData(&sSVGAutoRenderStateKey);
if (state) {
return static_cast<SVGAutoRenderState*>(state)->mPaintingToWindow;
}
return false;
}
void
nsSVGUtils::Init()
{
Preferences::AddBoolVarCache(&sSVGDisplayListHitTestingEnabled,
"svg.display-lists.hit-testing.enabled");
Preferences::AddBoolVarCache(&sSVGDisplayListPaintingEnabled,
"svg.display-lists.painting.enabled");
Preferences::AddBoolVarCache(&sSVGTextCSSFramesEnabled,
"svg.text.css-frames.enabled");
}
void
nsSVGUtils::UnPremultiplyImageDataAlpha(uint8_t *data,
int32_t stride,
const nsIntRect &rect)
{
for (int32_t y = rect.y; y < rect.YMost(); y++) {
for (int32_t x = rect.x; x < rect.XMost(); x++) {
uint8_t *pixel = data + stride * y + 4 * x;
uint8_t a = pixel[GFX_ARGB32_OFFSET_A];
if (a == 255)
continue;
if (a) {
pixel[GFX_ARGB32_OFFSET_B] = (255 * pixel[GFX_ARGB32_OFFSET_B]) / a;
pixel[GFX_ARGB32_OFFSET_G] = (255 * pixel[GFX_ARGB32_OFFSET_G]) / a;
pixel[GFX_ARGB32_OFFSET_R] = (255 * pixel[GFX_ARGB32_OFFSET_R]) / a;
} else {
pixel[GFX_ARGB32_OFFSET_B] = 0;
pixel[GFX_ARGB32_OFFSET_G] = 0;
pixel[GFX_ARGB32_OFFSET_R] = 0;
}
}
}
}
void
nsSVGUtils::PremultiplyImageDataAlpha(uint8_t *data,
int32_t stride,
const nsIntRect &rect)
{
for (int32_t y = rect.y; y < rect.YMost(); y++) {
for (int32_t x = rect.x; x < rect.XMost(); x++) {
uint8_t *pixel = data + stride * y + 4 * x;
uint8_t a = pixel[GFX_ARGB32_OFFSET_A];
if (a == 255)
continue;
FAST_DIVIDE_BY_255(pixel[GFX_ARGB32_OFFSET_B],
pixel[GFX_ARGB32_OFFSET_B] * a);
FAST_DIVIDE_BY_255(pixel[GFX_ARGB32_OFFSET_G],
pixel[GFX_ARGB32_OFFSET_G] * a);
FAST_DIVIDE_BY_255(pixel[GFX_ARGB32_OFFSET_R],
pixel[GFX_ARGB32_OFFSET_R] * a);
}
}
}
void
nsSVGUtils::ConvertImageDataToLinearRGB(uint8_t *data,
int32_t stride,
const nsIntRect &rect)
{
for (int32_t y = rect.y; y < rect.YMost(); y++) {
for (int32_t x = rect.x; x < rect.XMost(); x++) {
uint8_t *pixel = data + stride * y + 4 * x;
pixel[GFX_ARGB32_OFFSET_B] =
gsRGBToLinearRGBMap[pixel[GFX_ARGB32_OFFSET_B]];
pixel[GFX_ARGB32_OFFSET_G] =
gsRGBToLinearRGBMap[pixel[GFX_ARGB32_OFFSET_G]];
pixel[GFX_ARGB32_OFFSET_R] =
gsRGBToLinearRGBMap[pixel[GFX_ARGB32_OFFSET_R]];
}
}
}
void
nsSVGUtils::ConvertImageDataFromLinearRGB(uint8_t *data,
int32_t stride,
const nsIntRect &rect)
{
for (int32_t y = rect.y; y < rect.YMost(); y++) {
for (int32_t x = rect.x; x < rect.XMost(); x++) {
uint8_t *pixel = data + stride * y + 4 * x;
pixel[GFX_ARGB32_OFFSET_B] =
glinearRGBTosRGBMap[pixel[GFX_ARGB32_OFFSET_B]];
pixel[GFX_ARGB32_OFFSET_G] =
glinearRGBTosRGBMap[pixel[GFX_ARGB32_OFFSET_G]];
pixel[GFX_ARGB32_OFFSET_R] =
glinearRGBTosRGBMap[pixel[GFX_ARGB32_OFFSET_R]];
}
}
}
void
nsSVGUtils::ComputesRGBLuminanceMask(uint8_t *aData,
int32_t aStride,
const nsIntRect &aRect,
float aOpacity)
{
for (int32_t y = aRect.y; y < aRect.YMost(); y++) {
for (int32_t x = aRect.x; x < aRect.XMost(); x++) {
uint8_t *pixel = aData + aStride * y + 4 * x;
uint8_t a = pixel[GFX_ARGB32_OFFSET_A];
uint8_t luminance;
if (a) {
/* sRGB -> intensity (unpremultiply cancels out the
* (a/255.0) multiplication with aOpacity */
luminance =
static_cast<uint8_t>
((pixel[GFX_ARGB32_OFFSET_R] * 0.2125 +
pixel[GFX_ARGB32_OFFSET_G] * 0.7154 +
pixel[GFX_ARGB32_OFFSET_B] * 0.0721) *
aOpacity);
} else {
luminance = 0;
}
memset(pixel, luminance, 4);
}
}
}
void
nsSVGUtils::ComputeLinearRGBLuminanceMask(uint8_t *aData,
int32_t aStride,
const nsIntRect &aRect,
float aOpacity)
{
for (int32_t y = aRect.y; y < aRect.YMost(); y++) {
for (int32_t x = aRect.x; x < aRect.XMost(); x++) {
uint8_t *pixel = aData + aStride * y + 4 * x;
uint8_t a = pixel[GFX_ARGB32_OFFSET_A];
uint8_t luminance;
// unpremultiply
if (a) {
if (a != 255) {
pixel[GFX_ARGB32_OFFSET_B] =
(255 * pixel[GFX_ARGB32_OFFSET_B]) / a;
pixel[GFX_ARGB32_OFFSET_G] =
(255 * pixel[GFX_ARGB32_OFFSET_G]) / a;
pixel[GFX_ARGB32_OFFSET_R] =
(255 * pixel[GFX_ARGB32_OFFSET_R]) / a;
}
/* sRGB -> linearRGB -> intensity */
luminance =
static_cast<uint8_t>
((gsRGBToLinearRGBMap[pixel[GFX_ARGB32_OFFSET_R]] *
0.2125 +
gsRGBToLinearRGBMap[pixel[GFX_ARGB32_OFFSET_G]] *
0.7154 +
gsRGBToLinearRGBMap[pixel[GFX_ARGB32_OFFSET_B]] *
0.0721) * (a / 255.0) * aOpacity);
} else {
luminance = 0;
}
memset(pixel, luminance, 4);
}
}
}
void
nsSVGUtils::ComputeAlphaMask(uint8_t *aData,
int32_t aStride,
const nsIntRect &aRect,
float aOpacity)
{
for (int32_t y = aRect.y; y < aRect.YMost(); y++) {
for (int32_t x = aRect.x; x < aRect.XMost(); x++) {
uint8_t *pixel = aData + aStride * y + 4 * x;
uint8_t luminance = pixel[GFX_ARGB32_OFFSET_A] * aOpacity;
memset(pixel, luminance, 4);
}
}
}
float
nsSVGUtils::CoordToFloat(nsPresContext *aPresContext,
nsSVGElement *aContent,
const nsStyleCoord &aCoord)
{
switch (aCoord.GetUnit()) {
case eStyleUnit_Factor:
// user units
return aCoord.GetFactorValue();
case eStyleUnit_Coord:
return nsPresContext::AppUnitsToFloatCSSPixels(aCoord.GetCoordValue());
case eStyleUnit_Percent: {
SVGSVGElement* ctx = aContent->GetCtx();
return ctx ? aCoord.GetPercentValue() * ctx->GetLength(SVGContentUtils::XY) : 0.0f;
}
default:
return 0.0f;
}
}
nsSVGDisplayContainerFrame*
nsSVGUtils::GetNearestSVGViewport(nsIFrame *aFrame)
{
NS_ASSERTION(aFrame->IsFrameOfType(nsIFrame::eSVG), "SVG frame expected");
if (aFrame->GetType() == nsGkAtoms::svgOuterSVGFrame) {
return nullptr;
}
while ((aFrame = aFrame->GetParent())) {
NS_ASSERTION(aFrame->IsFrameOfType(nsIFrame::eSVG), "SVG frame expected");
if (aFrame->GetType() == nsGkAtoms::svgInnerSVGFrame ||
aFrame->GetType() == nsGkAtoms::svgOuterSVGFrame) {
return do_QueryFrame(aFrame);
}
}
NS_NOTREACHED("This is not reached. It's only needed to compile.");
return nullptr;
}
nsRect
nsSVGUtils::GetPostFilterVisualOverflowRect(nsIFrame *aFrame,
const nsRect &aPreFilterRect)
{
NS_ABORT_IF_FALSE(aFrame->GetStateBits() & NS_FRAME_SVG_LAYOUT,
"Called on invalid frame type");
nsSVGFilterFrame *filter = nsSVGEffects::GetFilterFrame(aFrame);
if (!filter) {
return aPreFilterRect;
}
return filter->GetPostFilterBounds(aFrame, nullptr, &aPreFilterRect);
}
bool
nsSVGUtils::OuterSVGIsCallingReflowSVG(nsIFrame *aFrame)
{
return GetOuterSVGFrame(aFrame)->IsCallingReflowSVG();
}
void
nsSVGUtils::InvalidateBounds(nsIFrame *aFrame, bool aDuringUpdate,
const nsRect *aBoundsSubArea, uint32_t aFlags)
{
NS_ABORT_IF_FALSE(aFrame->IsFrameOfType(nsIFrame::eSVG) &&
!(aFrame->GetStateBits() & NS_STATE_IS_OUTER_SVG),
"Passed bad frame!");
NS_ASSERTION(aDuringUpdate == OuterSVGIsCallingReflowSVG(aFrame),
"aDuringUpdate lies!");
// Rendering observers must be notified about changes to the frames that they
// are observing _before_ ReflowSVG is called on the SVG frame tree, so we
// only need to notify observers if we're not under an ReflowSVG call.
// In fact, it would actually be wrong to notify observers while under
// ReflowSVG because the observers will try to mark themselves as dirty
// and, since ReflowSVG would be in the process of _removeing_ dirty bits
// from frames, that would mess things up.
if (!aDuringUpdate) {
NS_ASSERTION(!OuterSVGIsCallingReflowSVG(aFrame),
"Must not InvalidateRenderingObservers() under "
"nsISVGChildFrame::ReflowSVG!");
nsSVGEffects::InvalidateRenderingObservers(aFrame);
}
// Must come after InvalidateRenderingObservers
if (aFrame->GetStateBits() & NS_STATE_SVG_NONDISPLAY_CHILD) {
return;
}
// XXXjwatt: can this come before InvalidateRenderingObservers?
if (aFrame->GetStateBits() &
(NS_FRAME_IS_DIRTY | NS_FRAME_FIRST_REFLOW)) {
// Nothing to do if we're already dirty, or if the outer-<svg>
// hasn't yet had its initial reflow.
return;
}
aFrame->InvalidateFrameSubtree();
if ((aFrame->GetType() == nsGkAtoms::svgPathGeometryFrame ||
aFrame->GetType() == nsGkAtoms::svgGlyphFrame) &&
NS_SVGDisplayListPaintingEnabled()) {
return;
}
// Okay, so now we pass the area that needs to be invalidated up our parent
// chain, accounting for filter effects and transforms as we go, until we
// reach our nsSVGOuterSVGFrame where we can invalidate:
nsRect invalidArea;
if (aBoundsSubArea) {
invalidArea = *aBoundsSubArea;
} else {
invalidArea = aFrame->GetVisualOverflowRect();
// GetVisualOverflowRect() already includes filter effects and transforms,
// so advance to our parent before the loop below:
invalidArea += aFrame->GetPosition();
aFrame = aFrame->GetParent();
}
int32_t appUnitsPerCSSPx = aFrame->PresContext()->AppUnitsPerCSSPixel();
while (aFrame) {
if ((aFrame->GetStateBits() & NS_FRAME_IS_DIRTY)) {
// This ancestor frame has already been invalidated, so nothing to do.
return;
}
if (aFrame->GetStateBits() & NS_STATE_IS_OUTER_SVG) {
break;
}
if (aFrame->GetType() == nsGkAtoms::svgInnerSVGFrame &&
aFrame->StyleDisplay()->IsScrollableOverflow()) {
// Clip rect to the viewport established by this inner-<svg>:
float x, y, width, height;
static_cast<SVGSVGElement*>(aFrame->GetContent())->
GetAnimatedLengthValues(&x, &y, &width, &height, nullptr);
if (width <= 0.0f || height <= 0.0f) {
return; // Nothing to invalidate
}
nsRect viewportRect =
nsLayoutUtils::RoundGfxRectToAppRect(gfxRect(0.0, 0.0, width, height),
appUnitsPerCSSPx);
invalidArea = invalidArea.Intersect(viewportRect);
if (invalidArea.IsEmpty()) {
return; // Nothing to invalidate
}
}
nsSVGFilterFrame *filterFrame = nsSVGEffects::GetFilterFrame(aFrame);
if (filterFrame) {
invalidArea =
filterFrame->GetPostFilterDirtyArea(aFrame, invalidArea);
}
if (aFrame->IsTransformed()) {
invalidArea =
nsDisplayTransform::TransformRect(invalidArea, aFrame, nsPoint(0, 0));
}
invalidArea += aFrame->GetPosition();
aFrame = aFrame->GetParent();
}
if (!aFrame) {
// We seem to be able to get here, even though SVG frames are never created
// without an ancestor nsSVGOuterSVGFrame. See bug 767996.
return;
}
NS_ASSERTION(aFrame->GetStateBits() & NS_STATE_IS_OUTER_SVG,
"SVG frames must always have an nsSVGOuterSVGFrame ancestor!");
static_cast<nsSVGOuterSVGFrame*>(aFrame)->InvalidateSVG(invalidArea);
}
void
nsSVGUtils::ScheduleReflowSVG(nsIFrame *aFrame)
{
NS_ABORT_IF_FALSE(aFrame->IsFrameOfType(nsIFrame::eSVG),
"Passed bad frame!");
// If this is triggered, the callers should be fixed to call us before
// ReflowSVG is called. If we try to mark dirty bits on frames while we're
// in the process of removing them, things will get messed up.
NS_ASSERTION(!OuterSVGIsCallingReflowSVG(aFrame),
"Do not call under nsISVGChildFrame::ReflowSVG!");
// We don't call nsSVGEffects::InvalidateRenderingObservers here because
// we should only be called under InvalidateAndScheduleReflowSVG (which
// calls InvalidateBounds) or nsSVGDisplayContainerFrame::InsertFrames
// (at which point the frame has no observers).
if (aFrame->GetStateBits() & NS_STATE_SVG_NONDISPLAY_CHILD) {
return;
}
if (aFrame->GetStateBits() &
(NS_FRAME_IS_DIRTY | NS_FRAME_FIRST_REFLOW)) {
// Nothing to do if we're already dirty, or if the outer-<svg>
// hasn't yet had its initial reflow.
return;
}
nsSVGOuterSVGFrame *outerSVGFrame = nullptr;
// We must not add dirty bits to the nsSVGOuterSVGFrame or else
// PresShell::FrameNeedsReflow won't work when we pass it in below.
if (aFrame->GetStateBits() & NS_STATE_IS_OUTER_SVG) {
outerSVGFrame = static_cast<nsSVGOuterSVGFrame*>(aFrame);
} else {
aFrame->AddStateBits(NS_FRAME_IS_DIRTY);
nsIFrame *f = aFrame->GetParent();
while (f && !(f->GetStateBits() & NS_STATE_IS_OUTER_SVG)) {
if (f->GetStateBits() &
(NS_FRAME_IS_DIRTY | NS_FRAME_HAS_DIRTY_CHILDREN)) {
return;
}
f->AddStateBits(NS_FRAME_HAS_DIRTY_CHILDREN);
f = f->GetParent();
NS_ABORT_IF_FALSE(f->IsFrameOfType(nsIFrame::eSVG),
"NS_STATE_IS_OUTER_SVG check above not valid!");
}
outerSVGFrame = static_cast<nsSVGOuterSVGFrame*>(f);
NS_ABORT_IF_FALSE(outerSVGFrame &&
outerSVGFrame->GetType() == nsGkAtoms::svgOuterSVGFrame,
"Did not find nsSVGOuterSVGFrame!");
}
if (outerSVGFrame->GetStateBits() & NS_FRAME_IN_REFLOW) {
// We're currently under an nsSVGOuterSVGFrame::Reflow call so there is no
// need to call PresShell::FrameNeedsReflow, since we have an
// nsSVGOuterSVGFrame::DidReflow call pending.
return;
}
nsFrameState dirtyBit =
(outerSVGFrame == aFrame ? NS_FRAME_IS_DIRTY : NS_FRAME_HAS_DIRTY_CHILDREN);
aFrame->PresContext()->PresShell()->FrameNeedsReflow(
outerSVGFrame, nsIPresShell::eResize, dirtyBit);
}
bool
nsSVGUtils::NeedsReflowSVG(nsIFrame *aFrame)
{
NS_ABORT_IF_FALSE(aFrame->IsFrameOfType(nsIFrame::eSVG),
"SVG uses bits differently!");
// The flags we test here may change, hence why we have this separate
// function.
return NS_SUBTREE_DIRTY(aFrame);
}
void
nsSVGUtils::NotifyAncestorsOfFilterRegionChange(nsIFrame *aFrame)
{
NS_ABORT_IF_FALSE(!(aFrame->GetStateBits() & NS_STATE_IS_OUTER_SVG),
"Not expecting to be called on the outer SVG Frame");
aFrame = aFrame->GetParent();
while (aFrame) {
if (aFrame->GetStateBits() & NS_STATE_IS_OUTER_SVG)
return;
nsSVGFilterProperty *property = nsSVGEffects::GetFilterProperty(aFrame);
if (property) {
property->Invalidate();
}
aFrame = aFrame->GetParent();
}
}
float
nsSVGUtils::ObjectSpace(const gfxRect &aRect, const nsSVGLength2 *aLength)
{
float axis;
switch (aLength->GetCtxType()) {
case SVGContentUtils::X:
axis = aRect.Width();
break;
case SVGContentUtils::Y:
axis = aRect.Height();
break;
case SVGContentUtils::XY:
axis = float(SVGContentUtils::ComputeNormalizedHypotenuse(
aRect.Width(), aRect.Height()));
break;
default:
NS_NOTREACHED("unexpected ctx type");
axis = 0.0f;
break;
}
if (aLength->IsPercentage()) {
// Multiply first to avoid precision errors:
return axis * aLength->GetAnimValInSpecifiedUnits() / 100;
}
return aLength->GetAnimValue(static_cast<SVGSVGElement*>(nullptr)) * axis;
}
float
nsSVGUtils::UserSpace(nsSVGElement *aSVGElement, const nsSVGLength2 *aLength)
{
return aLength->GetAnimValue(aSVGElement);
}
float
nsSVGUtils::UserSpace(nsIFrame *aNonSVGContext, const nsSVGLength2 *aLength)
{
return aLength->GetAnimValue(aNonSVGContext);
}
nsSVGOuterSVGFrame *
nsSVGUtils::GetOuterSVGFrame(nsIFrame *aFrame)
{
while (aFrame) {
if (aFrame->GetStateBits() & NS_STATE_IS_OUTER_SVG) {
return static_cast<nsSVGOuterSVGFrame*>(aFrame);
}
aFrame = aFrame->GetParent();
}
return nullptr;
}
nsIFrame*
nsSVGUtils::GetOuterSVGFrameAndCoveredRegion(nsIFrame* aFrame, nsRect* aRect)
{
nsISVGChildFrame* svg = do_QueryFrame(aFrame);
if (!svg)
return nullptr;
*aRect = (aFrame->GetStateBits() & NS_STATE_SVG_NONDISPLAY_CHILD) ?
nsRect(0, 0, 0, 0) : svg->GetCoveredRegion();
return GetOuterSVGFrame(aFrame);
}
gfxMatrix
nsSVGUtils::GetCanvasTM(nsIFrame *aFrame, uint32_t aFor)
{
// XXX yuck, we really need a common interface for GetCanvasTM
if (!aFrame->IsFrameOfType(nsIFrame::eSVG)) {
return nsSVGIntegrationUtils::GetCSSPxToDevPxMatrix(aFrame);
}
if (!(aFrame->GetStateBits() & NS_STATE_SVG_NONDISPLAY_CHILD)) {
if ((aFor == nsISVGChildFrame::FOR_PAINTING &&
NS_SVGDisplayListPaintingEnabled()) ||
(aFor == nsISVGChildFrame::FOR_HIT_TESTING &&
NS_SVGDisplayListHitTestingEnabled())) {
return nsSVGIntegrationUtils::GetCSSPxToDevPxMatrix(aFrame);
}
}
nsIAtom* type = aFrame->GetType();
if (type == nsGkAtoms::svgForeignObjectFrame) {
return static_cast<nsSVGForeignObjectFrame*>(aFrame)->GetCanvasTM(aFor);
}
if (type == nsGkAtoms::svgOuterSVGFrame) {
return nsSVGIntegrationUtils::GetCSSPxToDevPxMatrix(aFrame);
}
nsSVGContainerFrame *containerFrame = do_QueryFrame(aFrame);
if (containerFrame) {
return containerFrame->GetCanvasTM(aFor);
}
return static_cast<nsSVGGeometryFrame*>(aFrame)->GetCanvasTM(aFor);
}
gfxMatrix
nsSVGUtils::GetUserToCanvasTM(nsIFrame *aFrame, uint32_t aFor)
{
NS_ASSERTION(aFor == nsISVGChildFrame::FOR_OUTERSVG_TM,
"Unexpected aFor?");
nsISVGChildFrame* svgFrame = do_QueryFrame(aFrame);
NS_ASSERTION(svgFrame, "bad frame");
gfxMatrix tm;
if (svgFrame) {
nsSVGElement *content = static_cast<nsSVGElement*>(aFrame->GetContent());
tm = content->PrependLocalTransformsTo(
GetCanvasTM(aFrame->GetParent(), aFor),
nsSVGElement::eUserSpaceToParent);
}
return tm;
}
void
nsSVGUtils::NotifyChildrenOfSVGChange(nsIFrame *aFrame, uint32_t aFlags)
{
nsIFrame *kid = aFrame->GetFirstPrincipalChild();
while (kid) {
nsISVGChildFrame* SVGFrame = do_QueryFrame(kid);
if (SVGFrame) {
SVGFrame->NotifySVGChanged(aFlags);
} else {
NS_ASSERTION(kid->IsFrameOfType(nsIFrame::eSVG) || kid->IsSVGText(),
"SVG frame expected");
// recurse into the children of container frames e.g. <clipPath>, <mask>
// in case they have child frames with transformation matrices
if (kid->IsFrameOfType(nsIFrame::eSVG)) {
NotifyChildrenOfSVGChange(kid, aFlags);
}
}
kid = kid->GetNextSibling();
}
}
// ************************************************************
class SVGPaintCallback : public nsSVGFilterPaintCallback
{
public:
virtual void Paint(nsRenderingContext *aContext, nsIFrame *aTarget,
const nsIntRect* aDirtyRect)
{
nsISVGChildFrame *svgChildFrame = do_QueryFrame(aTarget);
NS_ASSERTION(svgChildFrame, "Expected SVG frame here");
nsIntRect* dirtyRect = nullptr;
nsIntRect tmpDirtyRect;
// aDirtyRect is in user-space pixels, we need to convert to
// outer-SVG-frame-relative device pixels.
if (aDirtyRect) {
gfxMatrix userToDeviceSpace =
nsSVGUtils::GetCanvasTM(aTarget, nsISVGChildFrame::FOR_PAINTING);
if (userToDeviceSpace.IsSingular()) {
return;
}
gfxRect dirtyBounds = userToDeviceSpace.TransformBounds(
gfxRect(aDirtyRect->x, aDirtyRect->y, aDirtyRect->width, aDirtyRect->height));
dirtyBounds.RoundOut();
if (gfxUtils::GfxRectToIntRect(dirtyBounds, &tmpDirtyRect)) {
dirtyRect = &tmpDirtyRect;
}
}
svgChildFrame->PaintSVG(aContext, dirtyRect);
}
};
void
nsSVGUtils::PaintFrameWithEffects(nsRenderingContext *aContext,
const nsIntRect *aDirtyRect,
nsIFrame *aFrame)
{
NS_ASSERTION(!NS_SVGDisplayListPaintingEnabled() ||
(aFrame->GetStateBits() & NS_STATE_SVG_NONDISPLAY_CHILD) ||
aFrame->PresContext()->IsGlyph(),
"If display lists are enabled, only painting of non-display "
"SVG should take this code path");
nsISVGChildFrame *svgChildFrame = do_QueryFrame(aFrame);
if (!svgChildFrame)
return;
float opacity = aFrame->StyleDisplay()->mOpacity;
if (opacity == 0.0f)
return;
const nsIContent* content = aFrame->GetContent();
if (content->IsSVG() &&
!static_cast<const nsSVGElement*>(content)->HasValidDimensions()) {
return;
}
/* Properties are added lazily and may have been removed by a restyle,
so make sure all applicable ones are set again. */
nsSVGEffects::EffectProperties effectProperties =
nsSVGEffects::GetEffectProperties(aFrame);
bool isOK = true;
nsSVGFilterFrame *filterFrame = effectProperties.GetFilterFrame(&isOK);
if (aDirtyRect &&
!(aFrame->GetStateBits() & NS_STATE_SVG_NONDISPLAY_CHILD)) {
// Here we convert aFrame's paint bounds to outer-<svg> device space,
// compare it to aDirtyRect, and return early if they don't intersect.
// We don't do this optimization for nondisplay SVG since nondisplay
// SVG doesn't maintain bounds/overflow rects.
nsRect overflowRect = aFrame->GetVisualOverflowRectRelativeToSelf();
if (aFrame->IsFrameOfType(nsIFrame::eSVGGeometry) ||
aFrame->IsSVGText()) {
// Unlike containers, leaf frames do not include GetPosition() in
// GetCanvasTM().
overflowRect = overflowRect + aFrame->GetPosition();
}
int32_t appUnitsPerDevPx = aFrame->PresContext()->AppUnitsPerDevPixel();
gfxMatrix tm = GetCanvasTM(aFrame, nsISVGChildFrame::FOR_PAINTING);
if (aFrame->IsFrameOfType(nsIFrame::eSVG | nsIFrame::eSVGContainer)) {
gfxMatrix childrenOnlyTM;
if (static_cast<nsSVGContainerFrame*>(aFrame)->
HasChildrenOnlyTransform(&childrenOnlyTM)) {
// Undo the children-only transform:
tm = childrenOnlyTM.Invert() * tm;
}
}
nsIntRect bounds = TransformFrameRectToOuterSVG(overflowRect,
tm, aFrame->PresContext()).
ToOutsidePixels(appUnitsPerDevPx);
if (!aDirtyRect->Intersects(bounds)) {
return;
}
}
/* SVG defines the following rendering model:
*
* 1. Render fill
* 2. Render stroke
* 3. Render markers
* 4. Apply filter
* 5. Apply clipping, masking, group opacity
*
* We follow this, but perform a couple of optimizations:
*
* + Use cairo's clipPath when representable natively (single object
* clip region).
*
* + Merge opacity and masking if both used together.
*/
if (opacity != 1.0f && CanOptimizeOpacity(aFrame))
opacity = 1.0f;
gfxContext *gfx = aContext->ThebesContext();
bool complexEffects = false;
nsSVGClipPathFrame *clipPathFrame = effectProperties.GetClipPathFrame(&isOK);
nsSVGMaskFrame *maskFrame = effectProperties.GetMaskFrame(&isOK);
bool isTrivialClip = clipPathFrame ? clipPathFrame->IsTrivial() : true;
if (!isOK) {
// Some resource is invalid. We shouldn't paint anything.
return;
}
gfxMatrix matrix;
if (clipPathFrame || maskFrame)
matrix = GetCanvasTM(aFrame, nsISVGChildFrame::FOR_PAINTING);
/* Check if we need to do additional operations on this child's
* rendering, which necessitates rendering into another surface. */
if (opacity != 1.0f || maskFrame || (clipPathFrame && !isTrivialClip)) {
complexEffects = true;
gfx->Save();
if (!(aFrame->GetStateBits() & NS_STATE_SVG_NONDISPLAY_CHILD)) {
// aFrame has a valid visual overflow rect, so clip to it before calling
// PushGroup() to minimize the size of the surfaces we'll composite:
gfxContextMatrixAutoSaveRestore matrixAutoSaveRestore(gfx);
gfx->Multiply(GetCanvasTM(aFrame, nsISVGChildFrame::FOR_PAINTING));
nsRect overflowRect = aFrame->GetVisualOverflowRectRelativeToSelf();
if (aFrame->IsFrameOfType(nsIFrame::eSVGGeometry) ||
aFrame->IsSVGText()) {
// Unlike containers, leaf frames do not include GetPosition() in
// GetCanvasTM().
overflowRect = overflowRect + aFrame->GetPosition();
}
aContext->IntersectClip(overflowRect);
}
gfx->PushGroup(gfxASurface::CONTENT_COLOR_ALPHA);
}
/* If this frame has only a trivial clipPath, set up cairo's clipping now so
* we can just do normal painting and get it clipped appropriately.
*/
if (clipPathFrame && isTrivialClip) {
gfx->Save();
clipPathFrame->ClipPaint(aContext, aFrame, matrix);
}
/* Paint the child */
if (filterFrame) {
nsRect* dirtyRect = nullptr;
nsRect tmpDirtyRect;
if (aDirtyRect) {
// aDirtyRect is in outer-<svg> device pixels, but the filter code needs
// it in frame space.
gfxMatrix userToDeviceSpace =
GetUserToCanvasTM(aFrame, nsISVGChildFrame::FOR_OUTERSVG_TM);
if (userToDeviceSpace.IsSingular()) {
return;
}
gfxMatrix deviceToUserSpace = userToDeviceSpace;
deviceToUserSpace.Invert();
gfxRect dirtyBounds = deviceToUserSpace.TransformBounds(
gfxRect(aDirtyRect->x, aDirtyRect->y,
aDirtyRect->width, aDirtyRect->height));
tmpDirtyRect =
nsLayoutUtils::RoundGfxRectToAppRect(
dirtyBounds, aFrame->PresContext()->AppUnitsPerCSSPixel()) -
aFrame->GetPosition();
dirtyRect = &tmpDirtyRect;
}
SVGPaintCallback paintCallback;
filterFrame->PaintFilteredFrame(aContext, aFrame, &paintCallback, dirtyRect);
} else {
svgChildFrame->PaintSVG(aContext, aDirtyRect);
}