Backed out changeset 2960ea3e50ca (bug 1881157) for causing crashtest assertion failu...
[gecko.git] / layout / svg / SVGSwitchFrame.cpp
blob61b5509706b19e8a284c5cf2a977352855c6568b
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 if (auto* kid = GetActiveChildFrame()) {
93 BuildDisplayListForChild(aBuilder, kid, aLists);
97 void SVGSwitchFrame::PaintSVG(gfxContext& aContext, const gfxMatrix& aTransform,
98 imgDrawingParams& aImgParams) {
99 NS_ASSERTION(HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
100 "Only painting of non-display SVG should take this code path");
102 if (StyleEffects()->IsTransparent()) {
103 return;
106 if (auto* kid = GetActiveChildFrame()) {
107 gfxMatrix tm = aTransform;
108 if (kid->GetContent()->IsSVGElement()) {
109 tm = SVGUtils::GetTransformMatrixInUserSpace(kid) * tm;
111 SVGUtils::PaintFrameWithEffects(kid, aContext, tm, aImgParams);
115 nsIFrame* SVGSwitchFrame::GetFrameForPoint(const gfxPoint& aPoint) {
116 MOZ_ASSERT_UNREACHABLE("A clipPath cannot contain an SVGSwitch element");
117 return nullptr;
120 static bool ShouldReflowSVGTextFrameInside(const nsIFrame* aFrame) {
121 return aFrame->IsSVGContainerFrame() || aFrame->IsSVGForeignObjectFrame() ||
122 !aFrame->IsSVGFrame();
125 void SVGSwitchFrame::AlwaysReflowSVGTextFrameDoForOneKid(nsIFrame* aKid) {
126 if (!aKid->IsSubtreeDirty()) {
127 return;
130 if (aKid->IsSVGTextFrame()) {
131 MOZ_ASSERT(!aKid->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
132 "A non-display SVGTextFrame directly contained in a display "
133 "container?");
134 static_cast<SVGTextFrame*>(aKid)->ReflowSVG();
135 } else if (ShouldReflowSVGTextFrameInside(aKid)) {
136 if (!aKid->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY)) {
137 for (nsIFrame* kid : aKid->PrincipalChildList()) {
138 AlwaysReflowSVGTextFrameDoForOneKid(kid);
140 } else {
141 // This child is in a nondisplay context, something like:
142 // <switch>
143 // ...
144 // <g><mask><text></text></mask></g>
145 // </switch>
146 // We should not call ReflowSVG on it.
147 SVGContainerFrame::ReflowSVGNonDisplayText(aKid);
152 void SVGSwitchFrame::ReflowAllSVGTextFramesInsideNonActiveChildren(
153 nsIFrame* aActiveChild) {
154 for (auto* kid : mFrames) {
155 if (aActiveChild == kid) {
156 continue;
159 AlwaysReflowSVGTextFrameDoForOneKid(kid);
163 void SVGSwitchFrame::ReflowSVG() {
164 NS_ASSERTION(SVGUtils::OuterSVGIsCallingReflowSVG(this),
165 "This call is probably a wasteful mistake");
167 MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
168 "ReflowSVG mechanism not designed for this");
170 if (!SVGUtils::NeedsReflowSVG(this)) {
171 return;
174 // If the NS_FRAME_FIRST_REFLOW bit has been removed from our parent frame,
175 // then our outer-<svg> has previously had its initial reflow. In that case
176 // we need to make sure that that bit has been removed from ourself _before_
177 // recursing over our children to ensure that they know too. Otherwise, we
178 // need to remove it _after_ recursing over our children so that they know
179 // the initial reflow is currently underway.
181 bool isFirstReflow = HasAnyStateBits(NS_FRAME_FIRST_REFLOW);
183 bool outerSVGHasHadFirstReflow =
184 !GetParent()->HasAnyStateBits(NS_FRAME_FIRST_REFLOW);
186 if (outerSVGHasHadFirstReflow) {
187 RemoveStateBits(NS_FRAME_FIRST_REFLOW); // tell our children
190 OverflowAreas overflowRects;
192 auto* child = GetActiveChildFrame();
193 ReflowAllSVGTextFramesInsideNonActiveChildren(child);
195 ISVGDisplayableFrame* svgChild = do_QueryFrame(child);
196 if (svgChild && !child->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY)) {
197 svgChild->ReflowSVG();
199 // We build up our child frame overflows here instead of using
200 // nsLayoutUtils::UnionChildOverflow since SVG frame's all use the same
201 // frame list, and we're iterating over that list now anyway.
202 ConsiderChildOverflow(overflowRects, child);
203 } else if (child && ShouldReflowSVGTextFrameInside(child)) {
204 MOZ_ASSERT(
205 child->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY) || !child->IsSVGFrame(),
206 "Check for this explicitly in the |if|, then");
207 ReflowSVGNonDisplayText(child);
210 if (isFirstReflow) {
211 // Make sure we have our filter property (if any) before calling
212 // FinishAndStoreOverflow (subsequent filter changes are handled off
213 // nsChangeHint_UpdateEffects):
214 SVGObserverUtils::UpdateEffects(this);
217 FinishAndStoreOverflow(overflowRects, mRect.Size());
219 // Remove state bits after FinishAndStoreOverflow so that it doesn't
220 // invalidate on first reflow:
221 RemoveStateBits(NS_FRAME_FIRST_REFLOW | NS_FRAME_IS_DIRTY |
222 NS_FRAME_HAS_DIRTY_CHILDREN);
225 SVGBBox SVGSwitchFrame::GetBBoxContribution(const Matrix& aToBBoxUserspace,
226 uint32_t aFlags) {
227 auto* kid = GetActiveChildFrame();
228 if (ISVGDisplayableFrame* svgKid = do_QueryFrame(kid)) {
229 nsIContent* content = kid->GetContent();
230 gfxMatrix transform = ThebesMatrix(aToBBoxUserspace);
231 if (content->IsSVGElement()) {
232 transform = static_cast<SVGElement*>(content)->PrependLocalTransformsTo(
233 {}, eChildToUserSpace) *
234 SVGUtils::GetTransformMatrixInUserSpace(kid) * transform;
236 return svgKid->GetBBoxContribution(ToMatrix(transform), aFlags);
238 return SVGBBox();
241 nsIFrame* SVGSwitchFrame::GetActiveChildFrame() {
242 auto* activeChild =
243 static_cast<dom::SVGSwitchElement*>(GetContent())->GetActiveChild();
245 return activeChild ? activeChild->GetPrimaryFrame() : nullptr;
248 } // namespace mozilla