1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsSVGInnerSVGFrame.h"
9 // Keep others in (case-insensitive) order:
10 #include "gfxContext.h"
12 #include "nsISVGChildFrame.h"
13 #include "nsRenderingContext.h"
14 #include "nsSVGContainerFrame.h"
15 #include "nsSVGEffects.h"
16 #include "nsSVGIntegrationUtils.h"
17 #include "mozilla/dom/SVGSVGElement.h"
19 using namespace mozilla
;
20 using namespace mozilla::dom
;
23 NS_NewSVGInnerSVGFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
)
25 return new (aPresShell
) nsSVGInnerSVGFrame(aContext
);
28 NS_IMPL_FRAMEARENA_HELPERS(nsSVGInnerSVGFrame
)
30 //----------------------------------------------------------------------
33 NS_QUERYFRAME_HEAD(nsSVGInnerSVGFrame
)
34 NS_QUERYFRAME_ENTRY(nsSVGInnerSVGFrame
)
35 NS_QUERYFRAME_ENTRY(nsISVGSVGFrame
)
36 NS_QUERYFRAME_TAIL_INHERITING(nsSVGInnerSVGFrameBase
)
40 nsSVGInnerSVGFrame::Init(nsIContent
* aContent
,
41 nsContainerFrame
* aParent
,
42 nsIFrame
* aPrevInFlow
)
44 NS_ASSERTION(aContent
->IsSVG(nsGkAtoms::svg
),
45 "Content is not an SVG 'svg' element!");
47 nsSVGInnerSVGFrameBase::Init(aContent
, aParent
, aPrevInFlow
);
52 nsSVGInnerSVGFrame::GetType() const
54 return nsGkAtoms::svgInnerSVGFrame
;
57 //----------------------------------------------------------------------
58 // nsISVGChildFrame methods
61 nsSVGInnerSVGFrame::PaintSVG(nsRenderingContext
*aContext
,
62 const nsIntRect
*aDirtyRect
,
63 nsIFrame
* aTransformRoot
)
65 NS_ASSERTION(!NS_SVGDisplayListPaintingEnabled() ||
66 (mState
& NS_FRAME_IS_NONDISPLAY
),
67 "If display lists are enabled, only painting of non-display "
68 "SVG should take this code path");
70 gfxContextAutoSaveRestore autoSR
;
72 if (StyleDisplay()->IsScrollableOverflow()) {
73 float x
, y
, width
, height
;
74 static_cast<SVGSVGElement
*>(mContent
)->
75 GetAnimatedLengthValues(&x
, &y
, &width
, &height
, nullptr);
77 if (width
<= 0 || height
<= 0) {
81 nsSVGContainerFrame
*parent
= static_cast<nsSVGContainerFrame
*>(GetParent());
82 gfxMatrix clipTransform
= parent
->GetCanvasTM(FOR_PAINTING
, aTransformRoot
);
84 gfxContext
*gfx
= aContext
->ThebesContext();
85 autoSR
.SetContext(gfx
);
87 nsSVGUtils::GetClipRectForFrame(this, x
, y
, width
, height
);
88 nsSVGUtils::SetClipRect(gfx
, clipTransform
, clipRect
);
91 return nsSVGInnerSVGFrameBase::PaintSVG(aContext
, aDirtyRect
);
95 nsSVGInnerSVGFrame::GetCoveredRegion()
98 static_cast<SVGSVGElement
*>(mContent
)->
99 GetAnimatedLengthValues(&x
, &y
, &w
, &h
, nullptr);
100 if (w
< 0.0f
) w
= 0.0f
;
101 if (h
< 0.0f
) h
= 0.0f
;
102 // GetCanvasTM includes the x,y translation
103 nsRect bounds
= nsSVGUtils::ToCanvasBounds(gfxRect(0.0, 0.0, w
, h
),
104 GetCanvasTM(FOR_OUTERSVG_TM
),
107 if (!StyleDisplay()->IsScrollableOverflow()) {
108 bounds
.UnionRect(bounds
, nsSVGUtils::GetCoveredRegion(mFrames
));
114 nsSVGInnerSVGFrame::ReflowSVG()
116 // mRect must be set before FinishAndStoreOverflow is called in order
117 // for our overflow areas to be clipped correctly.
118 float x
, y
, width
, height
;
119 static_cast<SVGSVGElement
*>(mContent
)->
120 GetAnimatedLengthValues(&x
, &y
, &width
, &height
, nullptr);
121 mRect
= nsLayoutUtils::RoundGfxRectToAppRect(
122 gfxRect(x
, y
, width
, height
),
123 PresContext()->AppUnitsPerCSSPixel());
125 // If we have a filter, we need to invalidate ourselves because filter
126 // output can change even if none of our descendants need repainting.
127 if (StyleSVGReset()->HasFilters()) {
131 nsSVGInnerSVGFrameBase::ReflowSVG();
135 nsSVGInnerSVGFrame::NotifySVGChanged(uint32_t aFlags
)
137 NS_ABORT_IF_FALSE(aFlags
& (TRANSFORM_CHANGED
| COORD_CONTEXT_CHANGED
),
138 "Invalidation logic may need adjusting");
140 if (aFlags
& COORD_CONTEXT_CHANGED
) {
142 SVGSVGElement
*svg
= static_cast<SVGSVGElement
*>(mContent
);
144 bool xOrYIsPercentage
=
145 svg
->mLengthAttributes
[SVGSVGElement::ATTR_X
].IsPercentage() ||
146 svg
->mLengthAttributes
[SVGSVGElement::ATTR_Y
].IsPercentage();
147 bool widthOrHeightIsPercentage
=
148 svg
->mLengthAttributes
[SVGSVGElement::ATTR_WIDTH
].IsPercentage() ||
149 svg
->mLengthAttributes
[SVGSVGElement::ATTR_HEIGHT
].IsPercentage();
151 if (xOrYIsPercentage
|| widthOrHeightIsPercentage
) {
152 // Ancestor changes can't affect how we render from the perspective of
153 // any rendering observers that we may have, so we don't need to
154 // invalidate them. We also don't need to invalidate ourself, since our
155 // changed ancestor will have invalidated its entire area, which includes
157 // For perf reasons we call this before calling NotifySVGChanged() below.
158 nsSVGUtils::ScheduleReflowSVG(this);
161 // Coordinate context changes affect mCanvasTM if we have a
162 // percentage 'x' or 'y', or if we have a percentage 'width' or 'height' AND
165 if (!(aFlags
& TRANSFORM_CHANGED
) &&
167 (widthOrHeightIsPercentage
&& svg
->HasViewBoxRect()))) {
168 aFlags
|= TRANSFORM_CHANGED
;
171 if (svg
->HasViewBoxRect() || !widthOrHeightIsPercentage
) {
172 // Remove COORD_CONTEXT_CHANGED, since we establish the coordinate
173 // context for our descendants and this notification won't change its
175 aFlags
&= ~COORD_CONTEXT_CHANGED
;
178 return; // No notification flags left
183 if (aFlags
& TRANSFORM_CHANGED
) {
184 // make sure our cached transform matrix gets (lazily) updated
188 nsSVGInnerSVGFrameBase::NotifySVGChanged(aFlags
);
192 nsSVGInnerSVGFrame::AttributeChanged(int32_t aNameSpaceID
,
196 if (aNameSpaceID
== kNameSpaceID_None
&&
197 !(GetStateBits() & NS_FRAME_IS_NONDISPLAY
)) {
199 SVGSVGElement
* content
= static_cast<SVGSVGElement
*>(mContent
);
201 if (aAttribute
== nsGkAtoms::width
||
202 aAttribute
== nsGkAtoms::height
) {
203 nsSVGEffects::InvalidateRenderingObservers(this);
204 nsSVGUtils::ScheduleReflowSVG(this);
206 if (content
->HasViewBoxOrSyntheticViewBox()) {
207 // make sure our cached transform matrix gets (lazily) updated
209 content
->ChildrenOnlyTransformChanged();
210 nsSVGUtils::NotifyChildrenOfSVGChange(this, TRANSFORM_CHANGED
);
212 uint32_t flags
= COORD_CONTEXT_CHANGED
;
213 if (mCanvasTM
&& mCanvasTM
->IsSingular()) {
215 flags
|= TRANSFORM_CHANGED
;
217 nsSVGUtils::NotifyChildrenOfSVGChange(this, flags
);
220 } else if (aAttribute
== nsGkAtoms::transform
||
221 aAttribute
== nsGkAtoms::preserveAspectRatio
||
222 aAttribute
== nsGkAtoms::viewBox
||
223 aAttribute
== nsGkAtoms::x
||
224 aAttribute
== nsGkAtoms::y
) {
225 // make sure our cached transform matrix gets (lazily) updated
228 nsSVGUtils::NotifyChildrenOfSVGChange(
229 this, aAttribute
== nsGkAtoms::viewBox
?
230 TRANSFORM_CHANGED
| COORD_CONTEXT_CHANGED
: TRANSFORM_CHANGED
);
232 // We don't invalidate for transform changes (the layers code does that).
233 // Also note that SVGTransformableElement::GetAttributeChangeHint will
234 // return nsChangeHint_UpdateOverflow for "transform" attribute changes
235 // and cause DoApplyRenderingChangeToTree to make the SchedulePaint call.
237 if (aAttribute
== nsGkAtoms::x
|| aAttribute
== nsGkAtoms::y
) {
238 nsSVGEffects::InvalidateRenderingObservers(this);
239 nsSVGUtils::ScheduleReflowSVG(this);
240 } else if (aAttribute
== nsGkAtoms::viewBox
||
241 (aAttribute
== nsGkAtoms::preserveAspectRatio
&&
242 content
->HasViewBoxOrSyntheticViewBox())) {
243 content
->ChildrenOnlyTransformChanged();
244 // SchedulePaint sets a global state flag so we only need to call it once
245 // (on ourself is fine), not once on each child (despite bug 828240).
255 nsSVGInnerSVGFrame::GetFrameForPoint(const nsPoint
&aPoint
)
257 NS_ASSERTION(!NS_SVGDisplayListHitTestingEnabled() ||
258 (mState
& NS_FRAME_IS_NONDISPLAY
),
259 "If display lists are enabled, only hit-testing of non-display "
260 "SVG should take this code path");
262 if (StyleDisplay()->IsScrollableOverflow()) {
263 nsSVGElement
*content
= static_cast<nsSVGElement
*>(mContent
);
264 nsSVGContainerFrame
*parent
= static_cast<nsSVGContainerFrame
*>(GetParent());
266 float clipX
, clipY
, clipWidth
, clipHeight
;
267 content
->GetAnimatedLengthValues(&clipX
, &clipY
, &clipWidth
, &clipHeight
, nullptr);
269 if (!nsSVGUtils::HitTestRect(gfx::ToMatrix(parent
->GetCanvasTM(FOR_HIT_TESTING
)),
270 clipX
, clipY
, clipWidth
, clipHeight
,
271 PresContext()->AppUnitsToDevPixels(aPoint
.x
),
272 PresContext()->AppUnitsToDevPixels(aPoint
.y
))) {
277 return nsSVGInnerSVGFrameBase::GetFrameForPoint(aPoint
);
280 //----------------------------------------------------------------------
281 // nsISVGSVGFrame methods:
284 nsSVGInnerSVGFrame::NotifyViewportOrTransformChanged(uint32_t aFlags
)
286 // The dimensions of inner-<svg> frames are purely defined by their "width"
287 // and "height" attributes, and transform changes can only occur as a result
288 // of changes to their "width", "height", "viewBox" or "preserveAspectRatio"
289 // attributes. Changes to all of these attributes are handled in
290 // AttributeChanged(), so we should never be called.
291 NS_ERROR("Not called for nsSVGInnerSVGFrame");
294 //----------------------------------------------------------------------
295 // nsSVGContainerFrame methods:
298 nsSVGInnerSVGFrame::GetCanvasTM(uint32_t aFor
, nsIFrame
* aTransformRoot
)
300 if (!(GetStateBits() & NS_FRAME_IS_NONDISPLAY
) && !aTransformRoot
) {
301 if (aFor
== FOR_PAINTING
&& NS_SVGDisplayListPaintingEnabled()) {
302 return nsSVGIntegrationUtils::GetCSSPxToDevPxMatrix(this);
304 if (aFor
== FOR_HIT_TESTING
&& NS_SVGDisplayListHitTestingEnabled()) {
309 NS_ASSERTION(GetParent(), "null parent");
311 nsSVGContainerFrame
*parent
= static_cast<nsSVGContainerFrame
*>(GetParent());
312 SVGSVGElement
*content
= static_cast<SVGSVGElement
*>(mContent
);
314 gfxMatrix tm
= content
->PrependLocalTransformsTo(
315 this == aTransformRoot
? gfxMatrix() :
316 parent
->GetCanvasTM(aFor
, aTransformRoot
));
318 mCanvasTM
= new gfxMatrix(tm
);
324 nsSVGInnerSVGFrame::HasChildrenOnlyTransform(gfx::Matrix
*aTransform
) const
326 SVGSVGElement
*content
= static_cast<SVGSVGElement
*>(mContent
);
328 if (content
->HasViewBoxOrSyntheticViewBox()) {
329 // XXX Maybe return false if the transform is the identity transform?
331 *aTransform
= content
->GetViewBoxTransform();