1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Mozilla SVG project.
17 * The Initial Developer of the Original Code is IBM Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * the Initial Developer. All Rights Reserved.
22 * rocallahan@mozilla.com
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef NSSVGEFFECTS_H_
39 #define NSSVGEFFECTS_H_
41 #include "nsIContent.h"
43 #include "nsReferencedElement.h"
44 #include "nsStubMutationObserver.h"
45 #include "nsSVGUtils.h"
46 #include "nsTHashtable.h"
48 class nsSVGClipPathFrame
;
49 class nsSVGFilterFrame
;
53 * SVG elements reference supporting resources by element ID. We need to
54 * track when those resources change and when the DOM changes in ways
55 * that affect which element is referenced by a given ID (e.g., when
56 * element IDs change). The code here is responsible for that.
58 * When a frame references a supporting resource, we create a property
59 * object derived from nsSVGRenderingObserver to manage the relationship. The
60 * property object is attached to the referencing frame.
62 class nsSVGRenderingObserver
: public nsStubMutationObserver
{
64 nsSVGRenderingObserver(nsIURI
* aURI
, nsIFrame
*aFrame
);
65 virtual ~nsSVGRenderingObserver();
70 // nsIMutationObserver
71 NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
72 NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
73 NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
74 NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
76 void InvalidateViaReferencedFrame();
78 nsIFrame
* GetReferencedFrame();
80 * @param aOK this is only for the convenience of callers. We set *aOK to false
81 * if this function returns null.
83 nsIFrame
* GetReferencedFrame(nsIAtom
* aFrameType
, PRBool
* aOK
);
86 // This is called when the referenced resource changes.
87 virtual void DoUpdate();
89 class SourceReference
: public nsReferencedElement
{
91 SourceReference(nsSVGRenderingObserver
* aContainer
) : mContainer(aContainer
) {}
93 virtual void ContentChanged(nsIContent
* aFrom
, nsIContent
* aTo
) {
95 aFrom
->RemoveMutationObserver(mContainer
);
97 nsReferencedElement::ContentChanged(aFrom
, aTo
);
99 aTo
->AddMutationObserver(mContainer
);
101 mContainer
->DoUpdate();
104 * Override IsPersistent because we want to keep tracking the element
105 * for the ID even when it changes.
107 virtual PRBool
IsPersistent() { return PR_TRUE
; }
109 nsSVGRenderingObserver
* mContainer
;
112 SourceReference mElement
;
113 // The frame that this property is attached to
115 // When a presshell is torn down, we don't delete the properties for
116 // each frame until after the frames are destroyed. So here we remember
117 // the presshell for the frames we care about and, before we use the frame,
118 // we test the presshell to see if it's destroying itself. If it is,
119 // then the frame pointer is not valid and we know the frame has gone away.
120 nsIPresShell
*mFramePresShell
;
121 nsIFrame
*mReferencedFrame
;
122 nsIPresShell
*mReferencedFramePresShell
;
125 class nsSVGFilterProperty
:
126 public nsSVGRenderingObserver
, public nsISVGFilterProperty
{
128 nsSVGFilterProperty(nsIURI
*aURI
, nsIFrame
*aFilteredFrame
)
129 : nsSVGRenderingObserver(aURI
, aFilteredFrame
) {}
132 * @return the filter frame, or null if there is no filter frame
134 nsSVGFilterFrame
*GetFilterFrame();
139 // nsISVGFilterProperty
140 virtual void Invalidate() { DoUpdate(); }
143 // nsSVGRenderingObserver
144 virtual void DoUpdate();
147 class nsSVGMarkerProperty
: public nsSVGRenderingObserver
{
149 nsSVGMarkerProperty(nsIURI
*aURI
, nsIFrame
*aFrame
)
150 : nsSVGRenderingObserver(aURI
, aFrame
) {}
153 virtual void DoUpdate();
156 class nsSVGTextPathProperty
: public nsSVGRenderingObserver
{
158 nsSVGTextPathProperty(nsIURI
*aURI
, nsIFrame
*aFrame
)
159 : nsSVGRenderingObserver(aURI
, aFrame
) {}
162 virtual void DoUpdate();
165 class nsSVGPaintingProperty
: public nsSVGRenderingObserver
{
167 nsSVGPaintingProperty(nsIURI
*aURI
, nsIFrame
*aFrame
)
168 : nsSVGRenderingObserver(aURI
, aFrame
) {}
171 virtual void DoUpdate();
175 * A manager for one-shot nsSVGRenderingObserver tracking.
176 * nsSVGRenderingObservers can be added or removed. They are not strongly
177 * referenced so an observer must be removed before it before it dies.
178 * When InvalidateAll is called, all outstanding references get
179 * InvalidateViaReferencedFrame()
180 * called on them and the list is cleared. The intent is that
181 * the observer will force repainting of whatever part of the document
182 * is needed, and then at paint time the observer will be re-added.
184 * InvalidateAll must be called before this object is destroyed, i.e.
185 * before the referenced frame is destroyed. This should normally happen
186 * via nsSVGContainerFrame::RemoveFrame, since only frames in the frame
187 * tree should be referenced.
189 class nsSVGRenderingObserverList
{
191 nsSVGRenderingObserverList() { mObservers
.Init(5); }
192 ~nsSVGRenderingObserverList() { InvalidateAll(); }
194 void Add(nsSVGRenderingObserver
* aObserver
)
195 { mObservers
.PutEntry(aObserver
); }
196 void Remove(nsSVGRenderingObserver
* aObserver
)
197 { mObservers
.RemoveEntry(aObserver
); }
198 void InvalidateAll();
201 nsTHashtable
<nsVoidPtrHashKey
> mObservers
;
206 struct EffectProperties
{
207 nsSVGFilterProperty
* mFilter
;
208 nsSVGPaintingProperty
* mMask
;
209 nsSVGPaintingProperty
* mClipPath
;
212 * @return the clip-path frame, or null if there is no clip-path frame
213 * @param aOK if a clip-path was specified but the designated element
214 * does not exist or is an element of the wrong type, *aOK is set
215 * to false. Otherwise *aOK is untouched.
217 nsSVGClipPathFrame
*GetClipPathFrame(PRBool
*aOK
);
219 * @return the mask frame, or null if there is no mask frame
220 * @param aOK if a mask was specified but the designated element
221 * does not exist or is an element of the wrong type, *aOK is set
222 * to false. Otherwise *aOK is untouched.
224 nsSVGMaskFrame
*GetMaskFrame(PRBool
*aOK
);
226 * @return the filter frame, or null if there is no filter frame
227 * @param aOK if a filter was specified but the designated element
228 * does not exist or is an element of the wrong type, *aOK is set
229 * to false. Otherwise *aOK is untouched.
231 nsSVGFilterFrame
*GetFilterFrame(PRBool
*aOK
) {
234 nsSVGFilterFrame
*filter
= mFilter
->GetFilterFrame();
243 * @param aFrame should be the first continuation
245 static EffectProperties
GetEffectProperties(nsIFrame
*aFrame
);
247 * Called by nsCSSFrameConstructor when style changes require the
248 * effect properties on aFrame to be updated
250 static void UpdateEffects(nsIFrame
*aFrame
);
252 * @param aFrame should be the first continuation
254 static nsSVGFilterProperty
*GetFilterProperty(nsIFrame
*aFrame
);
255 static nsSVGFilterFrame
*GetFilterFrame(nsIFrame
*aFrame
) {
256 nsSVGFilterProperty
*prop
= GetFilterProperty(aFrame
);
257 return prop
? prop
->GetFilterFrame() : nsnull
;
261 * @param aFrame must be a first-continuation.
263 static void AddRenderingObserver(nsIFrame
*aFrame
, nsSVGRenderingObserver
*aObserver
);
265 * @param aFrame must be a first-continuation.
267 static void RemoveRenderingObserver(nsIFrame
*aFrame
, nsSVGRenderingObserver
*aObserver
);
269 * This can be called on any first-continuation frame. We walk up to
270 * the nearest observable frame and invalidate its observers.
272 static void InvalidateRenderingObservers(nsIFrame
*aFrame
);
274 * This can be called on any frame. Direct observers
275 * of this frame are all invalidated.
277 static void InvalidateDirectRenderingObservers(nsIFrame
*aFrame
);
280 * Get an nsSVGMarkerProperty for the frame, creating a fresh one if necessary
282 static nsSVGMarkerProperty
*
283 GetMarkerProperty(nsIURI
*aURI
, nsIFrame
*aFrame
, nsIAtom
*aProp
);
285 * Get an nsSVGTextPathProperty for the frame, creating a fresh one if necessary
287 static nsSVGTextPathProperty
*
288 GetTextPathProperty(nsIURI
*aURI
, nsIFrame
*aFrame
, nsIAtom
*aProp
);
290 * Get an nsSVGPaintingProperty for the frame, creating a fresh one if necessary
292 static nsSVGPaintingProperty
*
293 GetPaintingProperty(nsIURI
*aURI
, nsIFrame
*aFrame
, nsIAtom
*aProp
);
296 #endif /*NSSVGEFFECTS_H_*/