Backed out changeset 496886cb30a5 (bug 1867152) for bc failures on browser_user_input...
[gecko.git] / layout / svg / SVGSwitchFrame.cpp
blob0b94e7e0512c5b49b73e806fcadba737efe7cf8b
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // Keep in (case-insensitive) order:
8 #include "gfxRect.h"
9 #include "SVGGFrame.h"
10 #include "mozilla/PresShell.h"
11 #include "mozilla/SVGContainerFrame.h"
12 #include "mozilla/SVGObserverUtils.h"
13 #include "mozilla/SVGTextFrame.h"
14 #include "mozilla/SVGUtils.h"
15 #include "mozilla/dom/SVGSwitchElement.h"
17 using namespace mozilla::dom;
18 using namespace mozilla::gfx;
19 using namespace mozilla::image;
21 nsIFrame* NS_NewSVGSwitchFrame(mozilla::PresShell* aPresShell,
22 mozilla::ComputedStyle* aStyle);
24 namespace mozilla {
26 class SVGSwitchFrame final : public SVGGFrame {
27 friend nsIFrame* ::NS_NewSVGSwitchFrame(mozilla::PresShell* aPresShell,
28 ComputedStyle* aStyle);
30 protected:
31 explicit SVGSwitchFrame(ComputedStyle* aStyle, nsPresContext* aPresContext)
32 : SVGGFrame(aStyle, aPresContext, kClassID) {}
34 public:
35 NS_DECL_FRAMEARENA_HELPERS(SVGSwitchFrame)
37 #ifdef DEBUG
38 void Init(nsIContent* aContent, nsContainerFrame* aParent,
39 nsIFrame* aPrevInFlow) override;
40 #endif
42 #ifdef DEBUG_FRAME_DUMP
43 nsresult GetFrameName(nsAString& aResult) const override {
44 return MakeFrameName(u"SVGSwitch"_ns, aResult);
46 #endif
48 void BuildDisplayList(nsDisplayListBuilder* aBuilder,
49 const nsDisplayListSet& aLists) override;
51 // ISVGDisplayableFrame interface:
52 void PaintSVG(gfxContext& aContext, const gfxMatrix& aTransform,
53 imgDrawingParams& aImgParams) override;
54 nsIFrame* GetFrameForPoint(const gfxPoint& aPoint) override;
55 void ReflowSVG() override;
56 SVGBBox GetBBoxContribution(const Matrix& aToBBoxUserspace,
57 uint32_t aFlags) override;
59 private:
60 nsIFrame* GetActiveChildFrame();
61 void ReflowAllSVGTextFramesInsideNonActiveChildren(nsIFrame* aActiveChild);
62 static void AlwaysReflowSVGTextFrameDoForOneKid(nsIFrame* aKid);
65 } // namespace mozilla
67 //----------------------------------------------------------------------
68 // Implementation
70 nsIFrame* NS_NewSVGSwitchFrame(mozilla::PresShell* aPresShell,
71 mozilla::ComputedStyle* aStyle) {
72 return new (aPresShell)
73 mozilla::SVGSwitchFrame(aStyle, aPresShell->GetPresContext());
76 namespace mozilla {
78 NS_IMPL_FRAMEARENA_HELPERS(SVGSwitchFrame)
80 #ifdef DEBUG
81 void SVGSwitchFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
82 nsIFrame* aPrevInFlow) {
83 NS_ASSERTION(aContent->IsSVGElement(nsGkAtoms::svgSwitch),
84 "Content is not an SVG switch");
86 SVGGFrame::Init(aContent, aParent, aPrevInFlow);
88 #endif /* DEBUG */
90 void SVGSwitchFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
91 const nsDisplayListSet& aLists) {
92 nsIFrame* kid = GetActiveChildFrame();
93 if (kid) {
94 BuildDisplayListForChild(aBuilder, kid, aLists);
98 void SVGSwitchFrame::PaintSVG(gfxContext& aContext, const gfxMatrix& aTransform,
99 imgDrawingParams& aImgParams) {
100 NS_ASSERTION(HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
101 "Only painting of non-display SVG should take this code path");
103 if (StyleEffects()->IsTransparent()) {
104 return;
107 nsIFrame* kid = GetActiveChildFrame();
108 if (kid) {
109 gfxMatrix tm = aTransform;
110 if (kid->GetContent()->IsSVGElement()) {
111 tm = SVGUtils::GetTransformMatrixInUserSpace(kid) * tm;
113 SVGUtils::PaintFrameWithEffects(kid, aContext, tm, aImgParams);
117 nsIFrame* SVGSwitchFrame::GetFrameForPoint(const gfxPoint& aPoint) {
118 MOZ_ASSERT_UNREACHABLE("A clipPath cannot contain an SVGSwitch element");
119 return nullptr;
122 static bool ShouldReflowSVGTextFrameInside(nsIFrame* aFrame) {
123 return aFrame->IsSVGContainerFrame() || aFrame->IsSVGForeignObjectFrame() ||
124 !aFrame->IsSVGFrame();
127 void SVGSwitchFrame::AlwaysReflowSVGTextFrameDoForOneKid(nsIFrame* aKid) {
128 if (!aKid->IsSubtreeDirty()) {
129 return;
132 if (aKid->IsSVGTextFrame()) {
133 MOZ_ASSERT(!aKid->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
134 "A non-display SVGTextFrame directly contained in a display "
135 "container?");
136 static_cast<SVGTextFrame*>(aKid)->ReflowSVG();
137 } else if (ShouldReflowSVGTextFrameInside(aKid)) {
138 if (!aKid->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY)) {
139 for (nsIFrame* kid : aKid->PrincipalChildList()) {
140 AlwaysReflowSVGTextFrameDoForOneKid(kid);
142 } else {
143 // This child is in a nondisplay context, something like:
144 // <switch>
145 // ...
146 // <g><mask><text></text></mask></g>
147 // </switch>
148 // We should not call ReflowSVG on it.
149 SVGContainerFrame::ReflowSVGNonDisplayText(aKid);
154 void SVGSwitchFrame::ReflowAllSVGTextFramesInsideNonActiveChildren(
155 nsIFrame* aActiveChild) {
156 for (nsIFrame* kid = mFrames.FirstChild(); kid; kid = kid->GetNextSibling()) {
157 if (aActiveChild == kid) {
158 continue;
161 AlwaysReflowSVGTextFrameDoForOneKid(kid);
165 void SVGSwitchFrame::ReflowSVG() {
166 NS_ASSERTION(SVGUtils::OuterSVGIsCallingReflowSVG(this),
167 "This call is probably a wasteful mistake");
169 MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
170 "ReflowSVG mechanism not designed for this");
172 if (!SVGUtils::NeedsReflowSVG(this)) {
173 return;
176 // If the NS_FRAME_FIRST_REFLOW bit has been removed from our parent frame,
177 // then our outer-<svg> has previously had its initial reflow. In that case
178 // we need to make sure that that bit has been removed from ourself _before_
179 // recursing over our children to ensure that they know too. Otherwise, we
180 // need to remove it _after_ recursing over our children so that they know
181 // the initial reflow is currently underway.
183 bool isFirstReflow = HasAnyStateBits(NS_FRAME_FIRST_REFLOW);
185 bool outerSVGHasHadFirstReflow =
186 !GetParent()->HasAnyStateBits(NS_FRAME_FIRST_REFLOW);
188 if (outerSVGHasHadFirstReflow) {
189 RemoveStateBits(NS_FRAME_FIRST_REFLOW); // tell our children
192 OverflowAreas overflowRects;
194 nsIFrame* child = GetActiveChildFrame();
195 ReflowAllSVGTextFramesInsideNonActiveChildren(child);
197 ISVGDisplayableFrame* svgChild = do_QueryFrame(child);
198 if (svgChild) {
199 MOZ_ASSERT(!child->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
200 "Check for this explicitly in the |if|, then");
201 svgChild->ReflowSVG();
203 // We build up our child frame overflows here instead of using
204 // nsLayoutUtils::UnionChildOverflow since SVG frame's all use the same
205 // frame list, and we're iterating over that list now anyway.
206 ConsiderChildOverflow(overflowRects, child);
207 } else if (child && ShouldReflowSVGTextFrameInside(child)) {
208 MOZ_ASSERT(
209 child->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY) || !child->IsSVGFrame(),
210 "Check for this explicitly in the |if|, then");
211 ReflowSVGNonDisplayText(child);
214 if (isFirstReflow) {
215 // Make sure we have our filter property (if any) before calling
216 // FinishAndStoreOverflow (subsequent filter changes are handled off
217 // nsChangeHint_UpdateEffects):
218 SVGObserverUtils::UpdateEffects(this);
221 FinishAndStoreOverflow(overflowRects, mRect.Size());
223 // Remove state bits after FinishAndStoreOverflow so that it doesn't
224 // invalidate on first reflow:
225 RemoveStateBits(NS_FRAME_FIRST_REFLOW | NS_FRAME_IS_DIRTY |
226 NS_FRAME_HAS_DIRTY_CHILDREN);
229 SVGBBox SVGSwitchFrame::GetBBoxContribution(const Matrix& aToBBoxUserspace,
230 uint32_t aFlags) {
231 nsIFrame* kid = GetActiveChildFrame();
232 ISVGDisplayableFrame* svgKid = do_QueryFrame(kid);
233 if (svgKid) {
234 nsIContent* content = kid->GetContent();
235 gfxMatrix transform = ThebesMatrix(aToBBoxUserspace);
236 if (content->IsSVGElement()) {
237 transform = static_cast<SVGElement*>(content)->PrependLocalTransformsTo(
238 {}, eChildToUserSpace) *
239 SVGUtils::GetTransformMatrixInUserSpace(kid) * transform;
241 return svgKid->GetBBoxContribution(ToMatrix(transform), aFlags);
243 return SVGBBox();
246 nsIFrame* SVGSwitchFrame::GetActiveChildFrame() {
247 nsIContent* activeChild =
248 static_cast<dom::SVGSwitchElement*>(GetContent())->GetActiveChild();
250 if (activeChild) {
251 for (nsIFrame* kid = mFrames.FirstChild(); kid;
252 kid = kid->GetNextSibling()) {
253 if (activeChild == kid->GetContent()) {
254 return kid;
258 return nullptr;
261 } // namespace mozilla