Bug 886842 - Add clang trunk builds for ASan. r=froydnj
[gecko.git] / layout / svg / nsSVGSwitchFrame.cpp
blob27411570f9f9ef7599f34bbfa04acfb5fb55cbb5
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/. */
6 // Keep in (case-insensitive) order:
7 #include "gfxMatrix.h"
8 #include "gfxRect.h"
9 #include "nsSVGEffects.h"
10 #include "nsSVGGFrame.h"
11 #include "mozilla/dom/SVGSwitchElement.h"
12 #include "nsSVGUtils.h"
14 class nsRenderingContext;
16 typedef nsSVGGFrame nsSVGSwitchFrameBase;
18 class nsSVGSwitchFrame : public nsSVGSwitchFrameBase
20 friend nsIFrame*
21 NS_NewSVGSwitchFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
22 protected:
23 nsSVGSwitchFrame(nsStyleContext* aContext) :
24 nsSVGSwitchFrameBase(aContext) {}
26 public:
27 NS_DECL_FRAMEARENA_HELPERS
29 #ifdef DEBUG
30 virtual void Init(nsIContent* aContent,
31 nsIFrame* aParent,
32 nsIFrame* aPrevInFlow) MOZ_OVERRIDE;
33 #endif
35 /**
36 * Get the "type" of the frame
38 * @see nsGkAtoms::svgSwitchFrame
40 virtual nsIAtom* GetType() const;
42 #ifdef DEBUG
43 NS_IMETHOD GetFrameName(nsAString& aResult) const
45 return MakeFrameName(NS_LITERAL_STRING("SVGSwitch"), aResult);
47 #endif
49 virtual void BuildDisplayList(nsDisplayListBuilder* aBuilder,
50 const nsRect& aDirtyRect,
51 const nsDisplayListSet& aLists) MOZ_OVERRIDE;
53 // nsISVGChildFrame interface:
54 NS_IMETHOD PaintSVG(nsRenderingContext* aContext, const nsIntRect *aDirtyRect);
55 NS_IMETHODIMP_(nsIFrame*) GetFrameForPoint(const nsPoint &aPoint);
56 NS_IMETHODIMP_(nsRect) GetCoveredRegion();
57 virtual void ReflowSVG();
58 virtual SVGBBox GetBBoxContribution(const gfxMatrix &aToBBoxUserspace,
59 uint32_t aFlags);
61 private:
62 nsIFrame *GetActiveChildFrame();
65 //----------------------------------------------------------------------
66 // Implementation
68 nsIFrame*
69 NS_NewSVGSwitchFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
71 return new (aPresShell) nsSVGSwitchFrame(aContext);
74 NS_IMPL_FRAMEARENA_HELPERS(nsSVGSwitchFrame)
76 #ifdef DEBUG
77 void
78 nsSVGSwitchFrame::Init(nsIContent* aContent,
79 nsIFrame* aParent,
80 nsIFrame* aPrevInFlow)
82 NS_ASSERTION(aContent->IsSVG(nsGkAtoms::svgSwitch),
83 "Content is not an SVG switch");
85 nsSVGSwitchFrameBase::Init(aContent, aParent, aPrevInFlow);
87 #endif /* DEBUG */
89 nsIAtom *
90 nsSVGSwitchFrame::GetType() const
92 return nsGkAtoms::svgSwitchFrame;
95 void
96 nsSVGSwitchFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
97 const nsRect& aDirtyRect,
98 const nsDisplayListSet& aLists)
100 nsIFrame* kid = GetActiveChildFrame();
101 if (kid) {
102 BuildDisplayListForChild(aBuilder, kid, aDirtyRect, aLists);
106 NS_IMETHODIMP
107 nsSVGSwitchFrame::PaintSVG(nsRenderingContext* aContext,
108 const nsIntRect *aDirtyRect)
110 NS_ASSERTION(!NS_SVGDisplayListPaintingEnabled() ||
111 (mState & NS_STATE_SVG_NONDISPLAY_CHILD),
112 "If display lists are enabled, only painting of non-display "
113 "SVG should take this code path");
115 if (StyleDisplay()->mOpacity == 0.0)
116 return NS_OK;
118 nsIFrame *kid = GetActiveChildFrame();
119 if (kid) {
120 nsSVGUtils::PaintFrameWithEffects(aContext, aDirtyRect, kid);
122 return NS_OK;
126 NS_IMETHODIMP_(nsIFrame*)
127 nsSVGSwitchFrame::GetFrameForPoint(const nsPoint &aPoint)
129 NS_ASSERTION(!NS_SVGDisplayListHitTestingEnabled() ||
130 (mState & NS_STATE_SVG_NONDISPLAY_CHILD),
131 "If display lists are enabled, only hit-testing of non-display "
132 "SVG should take this code path");
134 nsIFrame *kid = GetActiveChildFrame();
135 if (kid) {
136 nsISVGChildFrame* svgFrame = do_QueryFrame(kid);
137 if (svgFrame) {
138 return svgFrame->GetFrameForPoint(aPoint);
142 return nullptr;
145 NS_IMETHODIMP_(nsRect)
146 nsSVGSwitchFrame::GetCoveredRegion()
148 nsRect rect;
150 nsIFrame *kid = GetActiveChildFrame();
151 if (kid) {
152 nsISVGChildFrame* child = do_QueryFrame(kid);
153 if (child) {
154 rect = child->GetCoveredRegion();
157 return rect;
160 void
161 nsSVGSwitchFrame::ReflowSVG()
163 NS_ASSERTION(nsSVGUtils::OuterSVGIsCallingReflowSVG(this),
164 "This call is probably a wasteful mistake");
166 NS_ABORT_IF_FALSE(!(GetStateBits() & NS_STATE_SVG_NONDISPLAY_CHILD),
167 "ReflowSVG mechanism not designed for this");
169 if (!nsSVGUtils::NeedsReflowSVG(this)) {
170 return;
173 // If the NS_FRAME_FIRST_REFLOW bit has been removed from our parent frame,
174 // then our outer-<svg> has previously had its initial reflow. In that case
175 // we need to make sure that that bit has been removed from ourself _before_
176 // recursing over our children to ensure that they know too. Otherwise, we
177 // need to remove it _after_ recursing over our children so that they know
178 // the initial reflow is currently underway.
180 bool outerSVGHasHadFirstReflow =
181 (GetParent()->GetStateBits() & NS_FRAME_FIRST_REFLOW) == 0;
183 if (outerSVGHasHadFirstReflow) {
184 mState &= ~NS_FRAME_FIRST_REFLOW; // tell our children
187 nsOverflowAreas overflowRects;
189 nsIFrame *child = GetActiveChildFrame();
190 if (child) {
191 nsISVGChildFrame* svgChild = do_QueryFrame(child);
192 if (svgChild) {
193 NS_ABORT_IF_FALSE(!(child->GetStateBits() & NS_STATE_SVG_NONDISPLAY_CHILD),
194 "Check for this explicitly in the |if|, then");
195 svgChild->ReflowSVG();
197 // We build up our child frame overflows here instead of using
198 // nsLayoutUtils::UnionChildOverflow since SVG frame's all use the same
199 // frame list, and we're iterating over that list now anyway.
200 ConsiderChildOverflow(overflowRects, child);
204 if (mState & NS_FRAME_FIRST_REFLOW) {
205 // Make sure we have our filter property (if any) before calling
206 // FinishAndStoreOverflow (subsequent filter changes are handled off
207 // nsChangeHint_UpdateEffects):
208 nsSVGEffects::UpdateEffects(this);
211 FinishAndStoreOverflow(overflowRects, mRect.Size());
213 // Remove state bits after FinishAndStoreOverflow so that it doesn't
214 // invalidate on first reflow:
215 mState &= ~(NS_FRAME_FIRST_REFLOW | NS_FRAME_IS_DIRTY |
216 NS_FRAME_HAS_DIRTY_CHILDREN);
219 SVGBBox
220 nsSVGSwitchFrame::GetBBoxContribution(const gfxMatrix &aToBBoxUserspace,
221 uint32_t aFlags)
223 nsIFrame* kid = GetActiveChildFrame();
224 if (kid) {
225 nsISVGChildFrame* svgKid = do_QueryFrame(kid);
226 if (svgKid) {
227 nsIContent *content = kid->GetContent();
228 gfxMatrix transform = aToBBoxUserspace;
229 if (content->IsSVG()) {
230 transform = static_cast<nsSVGElement*>(content)->
231 PrependLocalTransformsTo(aToBBoxUserspace);
233 return svgKid->GetBBoxContribution(transform, aFlags);
236 return SVGBBox();
239 nsIFrame *
240 nsSVGSwitchFrame::GetActiveChildFrame()
242 nsIContent *activeChild =
243 static_cast<mozilla::dom::SVGSwitchElement*>(mContent)->GetActiveChild();
245 if (activeChild) {
246 for (nsIFrame* kid = mFrames.FirstChild(); kid;
247 kid = kid->GetNextSibling()) {
249 if (activeChild == kid->GetContent()) {
250 return kid;
254 return nullptr;