Bug 1814091 - Move CanvasContext.getPreferredFormat to GPU.getPreferredCanvasFormat...
[gecko.git] / layout / svg / SVGSwitchFrame.cpp
blob1b865e3b533c5ff72cd0bae3f8a9faab412c4538
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 virtual 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 virtual void BuildDisplayList(nsDisplayListBuilder* aBuilder,
49 const nsDisplayListSet& aLists) override;
51 // ISVGDisplayableFrame interface:
52 virtual void PaintSVG(gfxContext& aContext, const gfxMatrix& aTransform,
53 imgDrawingParams& aImgParams,
54 const nsIntRect* aDirtyRect = nullptr) override;
55 nsIFrame* GetFrameForPoint(const gfxPoint& aPoint) override;
56 void ReflowSVG() override;
57 virtual SVGBBox GetBBoxContribution(const Matrix& aToBBoxUserspace,
58 uint32_t aFlags) override;
60 private:
61 nsIFrame* GetActiveChildFrame();
62 void ReflowAllSVGTextFramesInsideNonActiveChildren(nsIFrame* aActiveChild);
63 static void AlwaysReflowSVGTextFrameDoForOneKid(nsIFrame* aKid);
66 } // namespace mozilla
68 //----------------------------------------------------------------------
69 // Implementation
71 nsIFrame* NS_NewSVGSwitchFrame(mozilla::PresShell* aPresShell,
72 mozilla::ComputedStyle* aStyle) {
73 return new (aPresShell)
74 mozilla::SVGSwitchFrame(aStyle, aPresShell->GetPresContext());
77 namespace mozilla {
79 NS_IMPL_FRAMEARENA_HELPERS(SVGSwitchFrame)
81 #ifdef DEBUG
82 void SVGSwitchFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
83 nsIFrame* aPrevInFlow) {
84 NS_ASSERTION(aContent->IsSVGElement(nsGkAtoms::svgSwitch),
85 "Content is not an SVG switch");
87 SVGGFrame::Init(aContent, aParent, aPrevInFlow);
89 #endif /* DEBUG */
91 void SVGSwitchFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
92 const nsDisplayListSet& aLists) {
93 nsIFrame* kid = GetActiveChildFrame();
94 if (kid) {
95 BuildDisplayListForChild(aBuilder, kid, aLists);
99 void SVGSwitchFrame::PaintSVG(gfxContext& aContext, const gfxMatrix& aTransform,
100 imgDrawingParams& aImgParams,
101 const nsIntRect* aDirtyRect) {
102 NS_ASSERTION(HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
103 "Only painting of non-display SVG should take this code path");
105 if (StyleEffects()->mOpacity == 0.0) {
106 return;
109 nsIFrame* kid = GetActiveChildFrame();
110 if (kid) {
111 gfxMatrix tm = aTransform;
112 if (kid->GetContent()->IsSVGElement()) {
113 tm = SVGUtils::GetTransformMatrixInUserSpace(kid) * tm;
115 SVGUtils::PaintFrameWithEffects(kid, aContext, tm, aImgParams, aDirtyRect);
119 nsIFrame* SVGSwitchFrame::GetFrameForPoint(const gfxPoint& aPoint) {
120 NS_ASSERTION(
121 HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
122 "Only hit-testing of non-display SVG should take this code path");
124 nsIFrame* kid = GetActiveChildFrame();
125 ISVGDisplayableFrame* svgFrame = do_QueryFrame(kid);
126 if (svgFrame) {
127 // Transform the point from our SVG user space to our child's.
128 gfxPoint point = aPoint;
129 gfxMatrix m =
130 static_cast<const SVGElement*>(GetContent())
131 ->PrependLocalTransformsTo(gfxMatrix(), eChildToUserSpace);
132 m = static_cast<const SVGElement*>(kid->GetContent())
133 ->PrependLocalTransformsTo(m, eUserSpaceToParent);
134 if (!m.IsIdentity()) {
135 if (!m.Invert()) {
136 return nullptr;
138 point = m.TransformPoint(point);
140 return svgFrame->GetFrameForPoint(point);
143 return nullptr;
146 static bool shouldReflowSVGTextFrameInside(nsIFrame* aFrame) {
147 return aFrame->IsFrameOfType(nsIFrame::eSVG | nsIFrame::eSVGContainer) ||
148 aFrame->IsSVGForeignObjectFrame() ||
149 !aFrame->IsFrameOfType(nsIFrame::eSVG);
152 void SVGSwitchFrame::AlwaysReflowSVGTextFrameDoForOneKid(nsIFrame* aKid) {
153 if (!aKid->IsSubtreeDirty()) {
154 return;
157 if (aKid->IsSVGTextFrame()) {
158 MOZ_ASSERT(!aKid->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
159 "A non-display SVGTextFrame directly contained in a display "
160 "container?");
161 static_cast<SVGTextFrame*>(aKid)->ReflowSVG();
162 } else if (shouldReflowSVGTextFrameInside(aKid)) {
163 if (!aKid->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY)) {
164 for (nsIFrame* kid : aKid->PrincipalChildList()) {
165 AlwaysReflowSVGTextFrameDoForOneKid(kid);
167 } else {
168 // This child is in a nondisplay context, something like:
169 // <switch>
170 // ...
171 // <g><mask><text></text></mask></g>
172 // </switch>
173 // We should not call ReflowSVG on it.
174 SVGContainerFrame::ReflowSVGNonDisplayText(aKid);
179 void SVGSwitchFrame::ReflowAllSVGTextFramesInsideNonActiveChildren(
180 nsIFrame* aActiveChild) {
181 for (nsIFrame* kid = mFrames.FirstChild(); kid; kid = kid->GetNextSibling()) {
182 if (aActiveChild == kid) {
183 continue;
186 AlwaysReflowSVGTextFrameDoForOneKid(kid);
190 void SVGSwitchFrame::ReflowSVG() {
191 NS_ASSERTION(SVGUtils::OuterSVGIsCallingReflowSVG(this),
192 "This call is probably a wasteful mistake");
194 MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
195 "ReflowSVG mechanism not designed for this");
197 if (!SVGUtils::NeedsReflowSVG(this)) {
198 return;
201 // If the NS_FRAME_FIRST_REFLOW bit has been removed from our parent frame,
202 // then our outer-<svg> has previously had its initial reflow. In that case
203 // we need to make sure that that bit has been removed from ourself _before_
204 // recursing over our children to ensure that they know too. Otherwise, we
205 // need to remove it _after_ recursing over our children so that they know
206 // the initial reflow is currently underway.
208 bool isFirstReflow = HasAnyStateBits(NS_FRAME_FIRST_REFLOW);
210 bool outerSVGHasHadFirstReflow =
211 !GetParent()->HasAnyStateBits(NS_FRAME_FIRST_REFLOW);
213 if (outerSVGHasHadFirstReflow) {
214 RemoveStateBits(NS_FRAME_FIRST_REFLOW); // tell our children
217 OverflowAreas overflowRects;
219 nsIFrame* child = GetActiveChildFrame();
220 ReflowAllSVGTextFramesInsideNonActiveChildren(child);
222 ISVGDisplayableFrame* svgChild = do_QueryFrame(child);
223 if (svgChild) {
224 MOZ_ASSERT(!child->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
225 "Check for this explicitly in the |if|, then");
226 svgChild->ReflowSVG();
228 // We build up our child frame overflows here instead of using
229 // nsLayoutUtils::UnionChildOverflow since SVG frame's all use the same
230 // frame list, and we're iterating over that list now anyway.
231 ConsiderChildOverflow(overflowRects, child);
232 } else if (child && shouldReflowSVGTextFrameInside(child)) {
233 MOZ_ASSERT(child->HasAnyStateBits(NS_FRAME_IS_NONDISPLAY) ||
234 !child->IsFrameOfType(nsIFrame::eSVG),
235 "Check for this explicitly in the |if|, then");
236 ReflowSVGNonDisplayText(child);
239 if (isFirstReflow) {
240 // Make sure we have our filter property (if any) before calling
241 // FinishAndStoreOverflow (subsequent filter changes are handled off
242 // nsChangeHint_UpdateEffects):
243 SVGObserverUtils::UpdateEffects(this);
246 FinishAndStoreOverflow(overflowRects, mRect.Size());
248 // Remove state bits after FinishAndStoreOverflow so that it doesn't
249 // invalidate on first reflow:
250 RemoveStateBits(NS_FRAME_FIRST_REFLOW | NS_FRAME_IS_DIRTY |
251 NS_FRAME_HAS_DIRTY_CHILDREN);
254 SVGBBox SVGSwitchFrame::GetBBoxContribution(const Matrix& aToBBoxUserspace,
255 uint32_t aFlags) {
256 nsIFrame* kid = GetActiveChildFrame();
257 ISVGDisplayableFrame* svgKid = do_QueryFrame(kid);
258 if (svgKid) {
259 nsIContent* content = kid->GetContent();
260 gfxMatrix transform = ThebesMatrix(aToBBoxUserspace);
261 if (content->IsSVGElement()) {
262 transform = static_cast<SVGElement*>(content)->PrependLocalTransformsTo(
263 {}, eChildToUserSpace) *
264 SVGUtils::GetTransformMatrixInUserSpace(kid) * transform;
266 return svgKid->GetBBoxContribution(ToMatrix(transform), aFlags);
268 return SVGBBox();
271 nsIFrame* SVGSwitchFrame::GetActiveChildFrame() {
272 nsIContent* activeChild =
273 static_cast<dom::SVGSwitchElement*>(GetContent())->GetActiveChild();
275 if (activeChild) {
276 for (nsIFrame* kid = mFrames.FirstChild(); kid;
277 kid = kid->GetNextSibling()) {
278 if (activeChild == kid->GetContent()) {
279 return kid;
283 return nullptr;
286 } // namespace mozilla