forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SVGObserverUtils.cpp
1583 lines (1338 loc) · 53.8 KB
/
SVGObserverUtils.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: 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/. */
// Main header first:
#include "SVGObserverUtils.h"
// Keep others in (case-insensitive) order:
#include "mozilla/css/ImageLoader.h"
#include "mozilla/dom/CanvasRenderingContext2D.h"
#include "mozilla/net/ReferrerPolicy.h"
#include "mozilla/RestyleManager.h"
#include "nsCSSFrameConstructor.h"
#include "nsCycleCollectionParticipant.h"
#include "nsIContent.h"
#include "nsIContentInlines.h"
#include "nsIReflowCallback.h"
#include "nsISupportsImpl.h"
#include "nsSVGClipPathFrame.h"
#include "nsSVGFilterFrame.h"
#include "nsSVGMarkerFrame.h"
#include "nsSVGMaskFrame.h"
#include "nsSVGPaintServerFrame.h"
#include "SVGGeometryElement.h"
#include "SVGTextPathElement.h"
#include "SVGUseElement.h"
using namespace mozilla;
using namespace mozilla::dom;
static already_AddRefed<URLAndReferrerInfo> ResolveURLUsingLocalRef(
nsIFrame* aFrame, const css::URLValue* aURL) {
MOZ_ASSERT(aFrame);
if (!aURL) {
return nullptr;
}
nsCOMPtr<nsIURI> uri = aURL->GetURI();
if (aURL->IsLocalRef()) {
uri = SVGObserverUtils::GetBaseURLForLocalRef(aFrame->GetContent(), uri);
uri = aURL->ResolveLocalRef(uri);
}
if (!uri) {
return nullptr;
}
RefPtr<URLAndReferrerInfo> info =
new URLAndReferrerInfo(uri, aURL->ExtraData());
return info.forget();
}
namespace mozilla {
class SVGFilterObserverList;
/**
* A class used as a member of the "observer" classes below to help them
* avoid dereferencing their frame during presshell teardown when their frame
* may have been destroyed (leaving their pointer to their frame dangling).
*
* When a presshell is torn down, the properties for each frame may not be
* deleted until after the frames are destroyed. "Observer" objects (attached
* as frame properties) must therefore check whether the presshell is being
* torn down before using their pointer to their frame.
*
* mFramePresShell may be null, but when mFrame is non-null, mFramePresShell
* is guaranteed to be non-null, too.
*/
struct nsSVGFrameReferenceFromProperty {
explicit nsSVGFrameReferenceFromProperty(nsIFrame* aFrame)
: mFrame(aFrame), mFramePresShell(aFrame->PresShell()) {}
// Clear our reference to the frame.
void Detach() {
mFrame = nullptr;
mFramePresShell = nullptr;
}
// null if the frame has become invalid
nsIFrame* Get() {
if (mFramePresShell && mFramePresShell->IsDestroying()) {
Detach(); // mFrame is no longer valid.
}
return mFrame;
}
private:
// The frame that our property is attached to (may be null).
nsIFrame* mFrame;
nsIPresShell* mFramePresShell;
};
void SVGRenderingObserver::StartObserving() {
Element* target = GetReferencedElementWithoutObserving();
if (target) {
target->AddMutationObserver(this);
}
}
void SVGRenderingObserver::StopObserving() {
Element* target = GetReferencedElementWithoutObserving();
if (target) {
target->RemoveMutationObserver(this);
if (mInObserverList) {
SVGObserverUtils::RemoveRenderingObserver(target, this);
mInObserverList = false;
}
}
NS_ASSERTION(!mInObserverList, "still in an observer list?");
}
Element* SVGRenderingObserver::GetAndObserveReferencedElement() {
#ifdef DEBUG
DebugObserverSet();
#endif
Element* referencedElement = GetReferencedElementWithoutObserving();
if (referencedElement && !mInObserverList) {
SVGObserverUtils::AddRenderingObserver(referencedElement, this);
mInObserverList = true;
}
return referencedElement;
}
nsIFrame* SVGRenderingObserver::GetAndObserveReferencedFrame() {
Element* referencedElement = GetAndObserveReferencedElement();
return referencedElement ? referencedElement->GetPrimaryFrame() : nullptr;
}
nsIFrame* SVGRenderingObserver::GetAndObserveReferencedFrame(
LayoutFrameType aFrameType, bool* aOK) {
nsIFrame* frame = GetAndObserveReferencedFrame();
if (frame) {
if (frame->Type() == aFrameType) {
return frame;
}
if (aOK) {
*aOK = false;
}
}
return nullptr;
}
void SVGRenderingObserver::OnNonDOMMutationRenderingChange() {
mInObserverList = false;
OnRenderingChange();
}
void SVGRenderingObserver::NotifyEvictedFromRenderingObserverSet() {
mInObserverList = false; // We've been removed from rendering-obs. list.
StopObserving(); // Remove ourselves from mutation-obs. list.
}
void SVGRenderingObserver::AttributeChanged(dom::Element* aElement,
int32_t aNameSpaceID,
nsAtom* aAttribute,
int32_t aModType,
const nsAttrValue* aOldValue) {
// An attribute belonging to the element that we are observing *or one of its
// descendants* has changed.
//
// In the case of observing a gradient element, say, we want to know if any
// of its 'stop' element children change, but we don't actually want to do
// anything for changes to SMIL element children, for example. Maybe it's not
// worth having logic to optimize for that, but in most cases it could be a
// small check?
//
// XXXjwatt: do we really want to blindly break the link between our
// observers and ourselves for all attribute changes? For non-ID changes
// surely that is unnecessary.
OnRenderingChange();
}
void SVGRenderingObserver::ContentAppended(nsIContent* aFirstNewContent) {
OnRenderingChange();
}
void SVGRenderingObserver::ContentInserted(nsIContent* aChild) {
OnRenderingChange();
}
void SVGRenderingObserver::ContentRemoved(nsIContent* aChild,
nsIContent* aPreviousSibling) {
OnRenderingChange();
}
/**
* SVG elements reference supporting resources by element ID. We need to
* track when those resources change and when the document changes in ways
* that affect which element is referenced by a given ID (e.g., when
* element IDs change). The code here is responsible for that.
*
* When a frame references a supporting resource, we create a property
* object derived from SVGIDRenderingObserver to manage the relationship. The
* property object is attached to the referencing frame.
*/
class SVGIDRenderingObserver : public SVGRenderingObserver {
public:
SVGIDRenderingObserver(URLAndReferrerInfo* aURI,
nsIContent* aObservingContent, bool aReferenceImage);
protected:
virtual ~SVGIDRenderingObserver() {
// This needs to call our GetReferencedElementWithoutObserving override,
// so must be called here rather than in our base class's dtor.
StopObserving();
}
Element* GetReferencedElementWithoutObserving() final {
return mObservedElementTracker.get();
}
void OnRenderingChange() override;
/**
* Helper that provides a reference to the element with the ID that our
* observer wants to observe, and that will invalidate our observer if the
* element that that ID identifies changes to a different element (or none).
*/
class ElementTracker final : public IDTracker {
public:
explicit ElementTracker(SVGIDRenderingObserver* aOwningObserver)
: mOwningObserver(aOwningObserver) {}
protected:
void ElementChanged(Element* aFrom, Element* aTo) override {
mOwningObserver->StopObserving(); // stop observing the old element
IDTracker::ElementChanged(aFrom, aTo);
mOwningObserver->StartObserving(); // start observing the new element
mOwningObserver->OnRenderingChange();
}
/**
* Override IsPersistent because we want to keep tracking the element
* for the ID even when it changes.
*/
bool IsPersistent() override { return true; }
private:
SVGIDRenderingObserver* mOwningObserver;
};
ElementTracker mObservedElementTracker;
};
/**
* Note that in the current setup there are two separate observer lists.
*
* In SVGIDRenderingObserver's ctor, the new object adds itself to the
* mutation observer list maintained by the referenced element. In this way the
* SVGIDRenderingObserver is notified if there are any attribute or content
* tree changes to the element or any of its *descendants*.
*
* In SVGIDRenderingObserver::GetAndObserveReferencedElement() the
* SVGIDRenderingObserver object also adds itself to an
* SVGRenderingObserverSet object belonging to the referenced
* element.
*
* XXX: it would be nice to have a clear and concise executive summary of the
* benefits/necessity of maintaining a second observer list.
*/
SVGIDRenderingObserver::SVGIDRenderingObserver(URLAndReferrerInfo* aURI,
nsIContent* aObservingContent,
bool aReferenceImage)
: mObservedElementTracker(this) {
// Start watching the target element
nsCOMPtr<nsIURI> uri;
nsCOMPtr<nsIURI> referrer;
uint32_t referrerPolicy = mozilla::net::RP_Unset;
if (aURI) {
uri = aURI->GetURI();
referrer = aURI->GetReferrer();
referrerPolicy = aURI->GetReferrerPolicy();
}
mObservedElementTracker.ResetToURIFragmentID(
aObservingContent, uri, referrer, referrerPolicy, true, aReferenceImage);
StartObserving();
}
void SVGIDRenderingObserver::OnRenderingChange() {
if (mObservedElementTracker.get() && mInObserverList) {
SVGObserverUtils::RemoveRenderingObserver(mObservedElementTracker.get(),
this);
mInObserverList = false;
}
}
class nsSVGRenderingObserverProperty : public SVGIDRenderingObserver {
public:
NS_DECL_ISUPPORTS
nsSVGRenderingObserverProperty(URLAndReferrerInfo* aURI, nsIFrame* aFrame,
bool aReferenceImage)
: SVGIDRenderingObserver(aURI, aFrame->GetContent(), aReferenceImage),
mFrameReference(aFrame) {}
protected:
virtual ~nsSVGRenderingObserverProperty() = default; // non-public
void OnRenderingChange() override;
nsSVGFrameReferenceFromProperty mFrameReference;
};
NS_IMPL_ISUPPORTS(nsSVGRenderingObserverProperty, nsIMutationObserver)
void nsSVGRenderingObserverProperty::OnRenderingChange() {
SVGIDRenderingObserver::OnRenderingChange();
nsIFrame* frame = mFrameReference.Get();
if (frame && frame->HasAllStateBits(NS_FRAME_SVG_LAYOUT)) {
// We need to notify anything that is observing the referencing frame or
// any of its ancestors that the referencing frame has been invalidated.
// Since walking the parent chain checking for observers is expensive we
// do that using a change hint (multiple change hints of the same type are
// coalesced).
nsLayoutUtils::PostRestyleEvent(frame->GetContent()->AsElement(),
nsRestyleHint(0),
nsChangeHint_InvalidateRenderingObservers);
}
}
class SVGTextPathObserver final : public nsSVGRenderingObserverProperty {
public:
SVGTextPathObserver(URLAndReferrerInfo* aURI, nsIFrame* aFrame,
bool aReferenceImage)
: nsSVGRenderingObserverProperty(aURI, aFrame, aReferenceImage),
mValid(true) {}
protected:
void OnRenderingChange() override;
private:
/**
* Returns true if the target of the textPath is the frame of a 'path'
* element.
*/
bool TargetIsValid() {
Element* target = GetReferencedElementWithoutObserving();
return target && target->IsSVGElement(nsGkAtoms::path);
}
bool mValid;
};
void SVGTextPathObserver::OnRenderingChange() {
nsSVGRenderingObserverProperty::OnRenderingChange();
nsIFrame* frame = mFrameReference.Get();
if (!frame) {
return;
}
MOZ_ASSERT(frame->IsFrameOfType(nsIFrame::eSVG) ||
nsSVGUtils::IsInSVGTextSubtree(frame),
"SVG frame expected");
// Avoid getting into an infinite loop of reflows if the <textPath> is
// pointing to one of its ancestors. TargetIsValid returns true iff
// the target element is a <path> element, and we would not have this
// SVGTextPathObserver if this <textPath> were a descendant of the
// target <path>.
//
// Note that we still have to post the restyle event when we
// change from being valid to invalid, so that mPositions on the
// SVGTextFrame gets updated, skipping the <textPath>, ensuring
// that nothing gets painted for that element.
bool nowValid = TargetIsValid();
if (!mValid && !nowValid) {
// Just return if we were previously invalid, and are still invalid.
return;
}
mValid = nowValid;
// Repaint asynchronously in case the path frame is being torn down
nsChangeHint changeHint =
nsChangeHint(nsChangeHint_RepaintFrame | nsChangeHint_UpdateTextPath);
frame->PresContext()->RestyleManager()->PostRestyleEvent(
frame->GetContent()->AsElement(), nsRestyleHint(0), changeHint);
}
class SVGMarkerObserver final : public nsSVGRenderingObserverProperty {
public:
SVGMarkerObserver(URLAndReferrerInfo* aURI, nsIFrame* aFrame,
bool aReferenceImage)
: nsSVGRenderingObserverProperty(aURI, aFrame, aReferenceImage) {}
protected:
void OnRenderingChange() override;
};
void SVGMarkerObserver::OnRenderingChange() {
nsSVGRenderingObserverProperty::OnRenderingChange();
nsIFrame* frame = mFrameReference.Get();
if (!frame) {
return;
}
MOZ_ASSERT(frame->IsFrameOfType(nsIFrame::eSVG), "SVG frame expected");
// Don't need to request ReflowFrame if we're being reflowed.
if (!(frame->GetStateBits() & NS_FRAME_IN_REFLOW)) {
// XXXjwatt: We need to unify SVG into standard reflow so we can just use
// nsChangeHint_NeedReflow | nsChangeHint_NeedDirtyReflow here.
// XXXSDL KILL THIS!!!
nsSVGUtils::ScheduleReflowSVG(frame);
}
frame->PresContext()->RestyleManager()->PostRestyleEvent(
frame->GetContent()->AsElement(), nsRestyleHint(0),
nsChangeHint_RepaintFrame);
}
class nsSVGPaintingProperty : public nsSVGRenderingObserverProperty {
public:
nsSVGPaintingProperty(URLAndReferrerInfo* aURI, nsIFrame* aFrame,
bool aReferenceImage)
: nsSVGRenderingObserverProperty(aURI, aFrame, aReferenceImage) {}
protected:
void OnRenderingChange() override;
};
void nsSVGPaintingProperty::OnRenderingChange() {
nsSVGRenderingObserverProperty::OnRenderingChange();
nsIFrame* frame = mFrameReference.Get();
if (!frame) {
return;
}
if (frame->GetStateBits() & NS_FRAME_SVG_LAYOUT) {
frame->InvalidateFrameSubtree();
} else {
for (nsIFrame* f = frame; f;
f = nsLayoutUtils::GetNextContinuationOrIBSplitSibling(f)) {
f->InvalidateFrame();
}
}
}
class SVGMozElementObserver final : public nsSVGPaintingProperty {
public:
SVGMozElementObserver(URLAndReferrerInfo* aURI, nsIFrame* aFrame,
bool aReferenceImage)
: nsSVGPaintingProperty(aURI, aFrame, aReferenceImage) {}
// We only return true here because GetAndObserveBackgroundImage uses us
// to implement observing of arbitrary elements (including HTML elements)
// that may require us to repaint if the referenced element is reflowed.
// Bug 1496065 has been filed to remove that support though.
bool ObservesReflow() override { return true; }
};
/**
* In a filter chain, there can be multiple SVG reference filters.
* e.g. filter: url(#svg-filter-1) blur(10px) url(#svg-filter-2);
*
* This class keeps track of one SVG reference filter in a filter chain.
* e.g. url(#svg-filter-1)
*
* It fires invalidations when the SVG filter element's id changes or when
* the SVG filter element's content changes.
*
* The SVGFilterObserverList class manages a list of SVGFilterObservers.
*/
class SVGFilterObserver final : public SVGIDRenderingObserver {
public:
SVGFilterObserver(URLAndReferrerInfo* aURI, nsIContent* aObservingContent,
SVGFilterObserverList* aFilterChainObserver)
: SVGIDRenderingObserver(aURI, aObservingContent, false),
mFilterObserverList(aFilterChainObserver) {}
// XXXjwatt: This will return false if the reference is to a filter in an
// external resource document that hasn't loaded yet!
bool ReferencesValidResource() { return GetAndObserveFilterFrame(); }
void DetachFromChainObserver() { mFilterObserverList = nullptr; }
/**
* @return the filter frame, or null if there is no filter frame
*/
nsSVGFilterFrame* GetAndObserveFilterFrame();
// nsISupports
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(SVGFilterObserver)
void Invalidate() { OnRenderingChange(); };
protected:
virtual ~SVGFilterObserver() = default; // non-public
// SVGIDRenderingObserver
void OnRenderingChange() override;
private:
SVGFilterObserverList* mFilterObserverList;
};
NS_IMPL_CYCLE_COLLECTING_ADDREF(SVGFilterObserver)
NS_IMPL_CYCLE_COLLECTING_RELEASE(SVGFilterObserver)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SVGFilterObserver)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_CLASS(SVGFilterObserver)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(SVGFilterObserver)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mObservedElementTracker)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(SVGFilterObserver)
tmp->StopObserving();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mObservedElementTracker);
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
nsSVGFilterFrame* SVGFilterObserver::GetAndObserveFilterFrame() {
return static_cast<nsSVGFilterFrame*>(
GetAndObserveReferencedFrame(LayoutFrameType::SVGFilter, nullptr));
}
/**
* This class manages a list of SVGFilterObservers, which correspond to
* reference to SVG filters in a list of filters in a given 'filter' property.
* e.g. filter: url(#svg-filter-1) blur(10px) url(#svg-filter-2);
*
* In the above example, the SVGFilterObserverList will manage two
* SVGFilterObservers, one for each of the references to SVG filters. CSS
* filters like "blur(10px)" don't reference filter elements, so they don't
* need an SVGFilterObserver. The style system invalidates changes to CSS
* filters.
*/
class SVGFilterObserverList : public nsISupports {
public:
SVGFilterObserverList(const nsTArray<nsStyleFilter>& aFilters,
nsIContent* aFilteredElement,
nsIFrame* aFiltedFrame = nullptr);
bool ReferencesValidResources();
void Invalidate() { OnRenderingChange(); }
const nsTArray<RefPtr<SVGFilterObserver>>& GetObservers() const {
return mObservers;
}
// nsISupports
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(SVGFilterObserverList)
protected:
virtual ~SVGFilterObserverList();
virtual void OnRenderingChange() = 0;
private:
void DetachObservers() {
for (uint32_t i = 0; i < mObservers.Length(); i++) {
mObservers[i]->DetachFromChainObserver();
}
}
nsTArray<RefPtr<SVGFilterObserver>> mObservers;
};
void SVGFilterObserver::OnRenderingChange() {
SVGIDRenderingObserver::OnRenderingChange();
if (mFilterObserverList) {
mFilterObserverList->Invalidate();
}
}
NS_IMPL_CYCLE_COLLECTING_ADDREF(SVGFilterObserverList)
NS_IMPL_CYCLE_COLLECTING_RELEASE(SVGFilterObserverList)
NS_IMPL_CYCLE_COLLECTION_CLASS(SVGFilterObserverList)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(SVGFilterObserverList)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mObservers)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(SVGFilterObserverList)
tmp->DetachObservers();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mObservers);
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SVGFilterObserverList)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
SVGFilterObserverList::SVGFilterObserverList(
const nsTArray<nsStyleFilter>& aFilters, nsIContent* aFilteredElement,
nsIFrame* aFilteredFrame) {
for (uint32_t i = 0; i < aFilters.Length(); i++) {
if (aFilters[i].GetType() != NS_STYLE_FILTER_URL) {
continue;
}
// aFilteredFrame can be null if this filter belongs to a
// CanvasRenderingContext2D.
RefPtr<URLAndReferrerInfo> filterURL;
if (aFilteredFrame) {
filterURL = ResolveURLUsingLocalRef(aFilteredFrame, aFilters[i].GetURL());
} else {
nsCOMPtr<nsIURI> resolvedURI =
aFilters[i].GetURL()->ResolveLocalRef(aFilteredElement);
if (resolvedURI) {
filterURL = new URLAndReferrerInfo(resolvedURI,
aFilters[i].GetURL()->ExtraData());
}
}
RefPtr<SVGFilterObserver> observer =
new SVGFilterObserver(filterURL, aFilteredElement, this);
mObservers.AppendElement(observer);
}
}
SVGFilterObserverList::~SVGFilterObserverList() { DetachObservers(); }
bool SVGFilterObserverList::ReferencesValidResources() {
for (uint32_t i = 0; i < mObservers.Length(); i++) {
if (!mObservers[i]->ReferencesValidResource()) {
return false;
}
}
return true;
}
class SVGFilterObserverListForCSSProp final : public SVGFilterObserverList {
public:
SVGFilterObserverListForCSSProp(const nsTArray<nsStyleFilter>& aFilters,
nsIFrame* aFilteredFrame)
: SVGFilterObserverList(aFilters, aFilteredFrame->GetContent(),
aFilteredFrame),
mFrameReference(aFilteredFrame) {}
void DetachFromFrame() { mFrameReference.Detach(); }
protected:
void OnRenderingChange() override;
nsSVGFrameReferenceFromProperty mFrameReference;
};
void SVGFilterObserverListForCSSProp::OnRenderingChange() {
nsIFrame* frame = mFrameReference.Get();
if (!frame) {
return;
}
// Repaint asynchronously in case the filter frame is being torn down
nsChangeHint changeHint = nsChangeHint(nsChangeHint_RepaintFrame);
// Since we don't call nsSVGRenderingObserverProperty::
// OnRenderingChange, we have to add this bit ourselves.
if (frame->HasAllStateBits(NS_FRAME_SVG_LAYOUT)) {
// Changes should propagate out to things that might be observing
// the referencing frame or its ancestors.
changeHint |= nsChangeHint_InvalidateRenderingObservers;
}
// Don't need to request UpdateOverflow if we're being reflowed.
if (!(frame->GetStateBits() & NS_FRAME_IN_REFLOW)) {
changeHint |= nsChangeHint_UpdateOverflow;
}
frame->PresContext()->RestyleManager()->PostRestyleEvent(
frame->GetContent()->AsElement(), nsRestyleHint(0), changeHint);
}
class SVGFilterObserverListForCanvasContext final
: public SVGFilterObserverList {
public:
SVGFilterObserverListForCanvasContext(CanvasRenderingContext2D* aContext,
Element* aCanvasElement,
nsTArray<nsStyleFilter>& aFilters)
: SVGFilterObserverList(aFilters, aCanvasElement), mContext(aContext) {}
void OnRenderingChange() override;
void DetachFromContext() { mContext = nullptr; }
private:
CanvasRenderingContext2D* mContext;
};
void SVGFilterObserverListForCanvasContext::OnRenderingChange() {
if (!mContext) {
MOZ_CRASH("GFX: This should never be called without a context");
}
// Refresh the cached FilterDescription in mContext->CurrentState().filter.
// If this filter is not at the top of the state stack, we'll refresh the
// wrong filter, but that's ok, because we'll refresh the right filter
// when we pop the state stack in CanvasRenderingContext2D::Restore().
RefPtr<CanvasRenderingContext2D> kungFuDeathGrip(mContext);
kungFuDeathGrip->UpdateFilter();
}
class SVGMaskObserverList final : public nsISupports {
public:
explicit SVGMaskObserverList(nsIFrame* aFrame);
// nsISupports
NS_DECL_ISUPPORTS
const nsTArray<RefPtr<nsSVGPaintingProperty>>& GetObservers() const {
return mProperties;
}
void ResolveImage(uint32_t aIndex);
private:
virtual ~SVGMaskObserverList() = default; // non-public
nsTArray<RefPtr<nsSVGPaintingProperty>> mProperties;
nsIFrame* mFrame;
};
NS_IMPL_ISUPPORTS(SVGMaskObserverList, nsISupports)
SVGMaskObserverList::SVGMaskObserverList(nsIFrame* aFrame) : mFrame(aFrame) {
const nsStyleSVGReset* svgReset = aFrame->StyleSVGReset();
for (uint32_t i = 0; i < svgReset->mMask.mImageCount; i++) {
const css::URLValue* data = svgReset->mMask.mLayers[i].mImage.GetURLValue();
RefPtr<URLAndReferrerInfo> maskUri = ResolveURLUsingLocalRef(aFrame, data);
bool hasRef = false;
if (maskUri) {
maskUri->GetURI()->GetHasRef(&hasRef);
}
// Accrording to maskUri, nsSVGPaintingProperty's ctor may trigger an
// external SVG resource download, so we should pass maskUri in only if
// maskUri has a chance pointing to an SVG mask resource.
//
// And, an URL may refer to an SVG mask resource if it consists of
// a fragment.
nsSVGPaintingProperty* prop = new nsSVGPaintingProperty(
hasRef ? maskUri.get() : nullptr, aFrame, false);
mProperties.AppendElement(prop);
}
}
void SVGMaskObserverList::ResolveImage(uint32_t aIndex) {
const nsStyleSVGReset* svgReset = mFrame->StyleSVGReset();
MOZ_ASSERT(aIndex < svgReset->mMask.mImageCount);
nsStyleImage& image =
const_cast<nsStyleImage&>(svgReset->mMask.mLayers[aIndex].mImage);
if (!image.IsResolved()) {
MOZ_ASSERT(image.GetType() == nsStyleImageType::eStyleImageType_Image);
image.ResolveImage(mFrame->PresContext(), nullptr);
mozilla::css::ImageLoader* imageLoader =
mFrame->PresContext()->Document()->StyleImageLoader();
if (imgRequestProxy* req = image.GetImageData()) {
imageLoader->AssociateRequestToFrame(req, mFrame, 0);
}
}
}
/**
* Used for gradient-to-gradient, pattern-to-pattern and filter-to-filter
* references to "template" elements (specified via the 'href' attributes).
*
* This is a special class for the case where we know we only want to call
* InvalidateDirectRenderingObservers (as opposed to
* InvalidateRenderingObservers).
*
* TODO(jwatt): If we added a new NS_FRAME_RENDERING_OBSERVER_CONTAINER state
* bit to clipPath, filter, gradients, marker, mask, pattern and symbol, and
* could have InvalidateRenderingObservers stop on reaching such an element,
* then we would no longer need this class (not to mention improving perf by
* significantly cutting down on ancestor traversal).
*/
class SVGTemplateElementObserver : public SVGIDRenderingObserver {
public:
NS_DECL_ISUPPORTS
SVGTemplateElementObserver(URLAndReferrerInfo* aURI, nsIFrame* aFrame,
bool aReferenceImage)
: SVGIDRenderingObserver(aURI, aFrame->GetContent(), aReferenceImage),
mFrameReference(aFrame) {}
protected:
virtual ~SVGTemplateElementObserver() = default; // non-public
void OnRenderingChange() override;
nsSVGFrameReferenceFromProperty mFrameReference;
};
NS_IMPL_ISUPPORTS(SVGTemplateElementObserver, nsIMutationObserver)
void SVGTemplateElementObserver::OnRenderingChange() {
SVGIDRenderingObserver::OnRenderingChange();
if (nsIFrame* frame = mFrameReference.Get()) {
// We know that we don't need to walk the parent chain notifying rendering
// observers since changes to a gradient etc. do not affect ancestor
// elements. So we only invalidate *direct* rendering observers here.
// Since we don't need to walk the parent chain, we don't need to worry
// about coalescing multiple invalidations by using a change hint as we do
// in nsSVGRenderingObserverProperty::OnRenderingChange.
SVGObserverUtils::InvalidateDirectRenderingObservers(frame);
}
}
/**
* An instance of this class is stored on an observed frame (as a frame
* property) whenever the frame has active rendering observers. It is used to
* store pointers to the SVGRenderingObserver instances belonging to any
* observing frames, allowing invalidations from the observed frame to be sent
* to all observing frames.
*
* SVGRenderingObserver instances that are added are not strongly referenced,
* so they must remove themselves before they die.
*
* This class is "single-shot", which is to say that when something about the
* observed element changes, InvalidateAll() clears our hashtable of
* SVGRenderingObservers. SVGRenderingObserver objects will be added back
* again if/when the observing frame looks up our observed frame to use it.
*
* XXXjwatt: is this the best thing to do nowadays? Back when that mechanism
* landed in bug 330498 we had two pass, recursive invalidation up the frame
* tree, and I think reference loops were a problem. Nowadays maybe a flag
* on the SVGRenderingObserver objects to coalesce invalidations may work
* better?
*
* InvalidateAll must be called before this object is destroyed, i.e.
* before the referenced frame is destroyed. This should normally happen
* via nsSVGContainerFrame::RemoveFrame, since only frames in the frame
* tree should be referenced.
*/
class SVGRenderingObserverSet {
public:
SVGRenderingObserverSet() : mObservers(4) {
MOZ_COUNT_CTOR(SVGRenderingObserverSet);
}
~SVGRenderingObserverSet() {
InvalidateAll();
MOZ_COUNT_DTOR(SVGRenderingObserverSet);
}
void Add(SVGRenderingObserver* aObserver) { mObservers.PutEntry(aObserver); }
void Remove(SVGRenderingObserver* aObserver) {
mObservers.RemoveEntry(aObserver);
}
#ifdef DEBUG
bool Contains(SVGRenderingObserver* aObserver) {
return (mObservers.GetEntry(aObserver) != nullptr);
}
#endif
bool IsEmpty() { return mObservers.IsEmpty(); }
/**
* Drop all our observers, and notify them that we have changed and dropped
* our reference to them.
*/
void InvalidateAll();
/**
* Drop all observers that observe reflow, and notify them that we have
* changed and dropped our reference to them.
*/
void InvalidateAllForReflow();
/**
* Drop all our observers, and notify them that we have dropped our reference
* to them.
*/
void RemoveAll();
private:
nsTHashtable<nsPtrHashKey<SVGRenderingObserver>> mObservers;
};
void SVGRenderingObserverSet::InvalidateAll() {
if (mObservers.IsEmpty()) {
return;
}
AutoTArray<SVGRenderingObserver*, 10> observers;
for (auto it = mObservers.Iter(); !it.Done(); it.Next()) {
observers.AppendElement(it.Get()->GetKey());
}
mObservers.Clear();
for (uint32_t i = 0; i < observers.Length(); ++i) {
observers[i]->OnNonDOMMutationRenderingChange();
}
}
void SVGRenderingObserverSet::InvalidateAllForReflow() {
if (mObservers.IsEmpty()) {
return;
}
AutoTArray<SVGRenderingObserver*, 10> observers;
for (auto it = mObservers.Iter(); !it.Done(); it.Next()) {
SVGRenderingObserver* obs = it.Get()->GetKey();
if (obs->ObservesReflow()) {
observers.AppendElement(obs);
it.Remove();
}
}
for (uint32_t i = 0; i < observers.Length(); ++i) {
observers[i]->OnNonDOMMutationRenderingChange();
}
}
void SVGRenderingObserverSet::RemoveAll() {
AutoTArray<SVGRenderingObserver*, 10> observers;
for (auto it = mObservers.Iter(); !it.Done(); it.Next()) {
observers.AppendElement(it.Get()->GetKey());
}
mObservers.Clear();
// Our list is now cleared. We need to notify the observers we've removed,
// so they can update their state & remove themselves as mutation-observers.
for (uint32_t i = 0; i < observers.Length(); ++i) {
observers[i]->NotifyEvictedFromRenderingObserverSet();
}
}
static SVGRenderingObserverSet* GetObserverSet(Element* aElement) {
return static_cast<SVGRenderingObserverSet*>(
aElement->GetProperty(nsGkAtoms::renderingobserverset));
}
#ifdef DEBUG
// Defined down here because we need SVGRenderingObserverSet's definition.
void SVGRenderingObserver::DebugObserverSet() {
Element* referencedElement = GetReferencedElementWithoutObserving();
if (referencedElement) {
SVGRenderingObserverSet* observers = GetObserverSet(referencedElement);
bool inObserverSet = observers && observers->Contains(this);
MOZ_ASSERT(inObserverSet == mInObserverList,
"failed to track whether we're in our referenced element's "
"observer set!");
} else {
MOZ_ASSERT(!mInObserverList, "In whose observer set are we, then?");
}
}
#endif
typedef nsInterfaceHashtable<nsRefPtrHashKey<URLAndReferrerInfo>,
nsIMutationObserver>
URIObserverHashtable;
using PaintingPropertyDescriptor =
const mozilla::FramePropertyDescriptor<nsSVGPaintingProperty>*;
void DestroyFilterProperty(SVGFilterObserverListForCSSProp* aProp) {
// SVGFilterObserverListForCSSProp is cycle-collected, so dropping the last
// reference doesn't necessarily destroy it. We need to tell it that the
// frame has now become invalid.
aProp->DetachFromFrame();
aProp->Release();
}
NS_DECLARE_FRAME_PROPERTY_RELEASABLE(HrefToTemplateProperty,
SVGTemplateElementObserver)
NS_DECLARE_FRAME_PROPERTY_WITH_DTOR(FilterProperty,
SVGFilterObserverListForCSSProp,
DestroyFilterProperty)
NS_DECLARE_FRAME_PROPERTY_RELEASABLE(MaskProperty, SVGMaskObserverList)
NS_DECLARE_FRAME_PROPERTY_RELEASABLE(ClipPathProperty, nsSVGPaintingProperty)
NS_DECLARE_FRAME_PROPERTY_RELEASABLE(MarkerStartProperty, SVGMarkerObserver)
NS_DECLARE_FRAME_PROPERTY_RELEASABLE(MarkerMidProperty, SVGMarkerObserver)
NS_DECLARE_FRAME_PROPERTY_RELEASABLE(MarkerEndProperty, SVGMarkerObserver)
NS_DECLARE_FRAME_PROPERTY_RELEASABLE(FillProperty, nsSVGPaintingProperty)
NS_DECLARE_FRAME_PROPERTY_RELEASABLE(StrokeProperty, nsSVGPaintingProperty)
NS_DECLARE_FRAME_PROPERTY_RELEASABLE(HrefAsTextPathProperty,
SVGTextPathObserver)
NS_DECLARE_FRAME_PROPERTY_DELETABLE(BackgroundImageProperty,
URIObserverHashtable)
template <class T>
static T* GetEffectProperty(
URLAndReferrerInfo* aURI, nsIFrame* aFrame,
const mozilla::FramePropertyDescriptor<T>* aProperty) {
if (!aURI) {
return nullptr;