Bug 1858915 [wpt PR 42525] - Use taskcluster-run in infra tests, a=testonly
[gecko.git] / layout / svg / SVGViewportFrame.cpp
blobb21d9df67b8bd30f5f140f5e9cf08a9c0b7b719f
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 // Main header first:
8 #include "SVGViewportFrame.h"
10 // Keep others in (case-insensitive) order:
11 #include "gfx2DGlue.h"
12 #include "gfxContext.h"
13 #include "mozilla/ISVGDisplayableFrame.h"
14 #include "mozilla/SVGContainerFrame.h"
15 #include "mozilla/SVGUtils.h"
16 #include "mozilla/dom/SVGViewportElement.h"
17 #include "nsLayoutUtils.h"
19 using namespace mozilla::dom;
20 using namespace mozilla::gfx;
21 using namespace mozilla::image;
23 namespace mozilla {
25 //----------------------------------------------------------------------
26 // ISVGDisplayableFrame methods
28 void SVGViewportFrame::PaintSVG(gfxContext& aContext,
29 const gfxMatrix& aTransform,
30 imgDrawingParams& aImgParams) {
31 NS_ASSERTION(HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
32 "Only painting of non-display SVG should take this code path");
34 gfxClipAutoSaveRestore autoSaveClip(&aContext);
36 if (StyleDisplay()->IsScrollableOverflow()) {
37 float x, y, width, height;
38 static_cast<SVGViewportElement*>(GetContent())
39 ->GetAnimatedLengthValues(&x, &y, &width, &height, nullptr);
41 if (width <= 0 || height <= 0) {
42 return;
45 gfxRect clipRect = SVGUtils::GetClipRectForFrame(this, x, y, width, height);
46 autoSaveClip.TransformedClip(aTransform, clipRect);
49 SVGDisplayContainerFrame::PaintSVG(aContext, aTransform, aImgParams);
52 void SVGViewportFrame::ReflowSVG() {
53 // mRect must be set before FinishAndStoreOverflow is called in order
54 // for our overflow areas to be clipped correctly.
55 float x, y, width, height;
56 static_cast<SVGViewportElement*>(GetContent())
57 ->GetAnimatedLengthValues(&x, &y, &width, &height, nullptr);
58 mRect = nsLayoutUtils::RoundGfxRectToAppRect(gfxRect(x, y, width, height),
59 AppUnitsPerCSSPixel());
61 // If we have a filter, we need to invalidate ourselves because filter
62 // output can change even if none of our descendants need repainting.
63 if (StyleEffects()->HasFilters()) {
64 InvalidateFrame();
67 SVGDisplayContainerFrame::ReflowSVG();
70 void SVGViewportFrame::NotifySVGChanged(uint32_t aFlags) {
71 MOZ_ASSERT(aFlags & (TRANSFORM_CHANGED | COORD_CONTEXT_CHANGED),
72 "Invalidation logic may need adjusting");
74 if (aFlags & COORD_CONTEXT_CHANGED) {
75 SVGViewportElement* svg = static_cast<SVGViewportElement*>(GetContent());
77 bool xOrYIsPercentage =
78 svg->mLengthAttributes[SVGViewportElement::ATTR_X].IsPercentage() ||
79 svg->mLengthAttributes[SVGViewportElement::ATTR_Y].IsPercentage();
80 bool widthOrHeightIsPercentage =
81 svg->mLengthAttributes[SVGViewportElement::ATTR_WIDTH].IsPercentage() ||
82 svg->mLengthAttributes[SVGViewportElement::ATTR_HEIGHT].IsPercentage();
84 if (xOrYIsPercentage || widthOrHeightIsPercentage) {
85 // Ancestor changes can't affect how we render from the perspective of
86 // any rendering observers that we may have, so we don't need to
87 // invalidate them. We also don't need to invalidate ourself, since our
88 // changed ancestor will have invalidated its entire area, which includes
89 // our area.
90 // For perf reasons we call this before calling NotifySVGChanged() below.
91 SVGUtils::ScheduleReflowSVG(this);
94 // Coordinate context changes affect mCanvasTM if we have a
95 // percentage 'x' or 'y', or if we have a percentage 'width' or 'height' AND
96 // a 'viewBox'.
98 if (!(aFlags & TRANSFORM_CHANGED) &&
99 (xOrYIsPercentage ||
100 (widthOrHeightIsPercentage && svg->HasViewBox()))) {
101 aFlags |= TRANSFORM_CHANGED;
104 if (svg->HasViewBox() || !widthOrHeightIsPercentage) {
105 // Remove COORD_CONTEXT_CHANGED, since we establish the coordinate
106 // context for our descendants and this notification won't change its
107 // dimensions:
108 aFlags &= ~COORD_CONTEXT_CHANGED;
110 if (!aFlags) {
111 return; // No notification flags left
116 SVGDisplayContainerFrame::NotifySVGChanged(aFlags);
119 SVGBBox SVGViewportFrame::GetBBoxContribution(const Matrix& aToBBoxUserspace,
120 uint32_t aFlags) {
121 // XXXjwatt It seems like authors would want the result to be clipped by the
122 // viewport we establish if IsScrollableOverflow() is true. We should
123 // consider doing that. See bug 1350755.
125 SVGBBox bbox;
127 if (aFlags & SVGUtils::eForGetClientRects) {
128 // XXXjwatt For consistency with the old code this code includes the
129 // viewport we establish in the result, but only includes the bounds of our
130 // descendants if they are not clipped to that viewport. However, this is
131 // both inconsistent with Chrome and with the specs. See bug 1350755.
132 // Ideally getClientRects/getBoundingClientRect should be consistent with
133 // getBBox.
134 float x, y, w, h;
135 static_cast<SVGViewportElement*>(GetContent())
136 ->GetAnimatedLengthValues(&x, &y, &w, &h, nullptr);
137 if (w < 0.0f) w = 0.0f;
138 if (h < 0.0f) h = 0.0f;
139 Rect viewport(x, y, w, h);
140 bbox = aToBBoxUserspace.TransformBounds(viewport);
141 if (StyleDisplay()->IsScrollableOverflow()) {
142 return bbox;
144 // Else we're not clipping to our viewport so we fall through and include
145 // the bounds of our children.
148 SVGBBox descendantsBbox =
149 SVGDisplayContainerFrame::GetBBoxContribution(aToBBoxUserspace, aFlags);
151 bbox.UnionEdges(descendantsBbox);
153 return bbox;
156 nsresult SVGViewportFrame::AttributeChanged(int32_t aNameSpaceID,
157 nsAtom* aAttribute,
158 int32_t aModType) {
159 if (aNameSpaceID == kNameSpaceID_None &&
160 !HasAnyStateBits(NS_FRAME_IS_NONDISPLAY)) {
161 SVGViewportElement* content =
162 static_cast<SVGViewportElement*>(GetContent());
164 if (aAttribute == nsGkAtoms::width || aAttribute == nsGkAtoms::height) {
165 nsLayoutUtils::PostRestyleEvent(
166 mContent->AsElement(), RestyleHint{0},
167 nsChangeHint_InvalidateRenderingObservers);
168 SVGUtils::ScheduleReflowSVG(this);
170 if (content->HasViewBoxOrSyntheticViewBox()) {
171 // make sure our cached transform matrix gets (lazily) updated
172 mCanvasTM = nullptr;
173 content->ChildrenOnlyTransformChanged();
174 SVGUtils::NotifyChildrenOfSVGChange(this, TRANSFORM_CHANGED);
175 } else {
176 uint32_t flags = COORD_CONTEXT_CHANGED;
177 if (mCanvasTM && mCanvasTM->IsSingular()) {
178 mCanvasTM = nullptr;
179 flags |= TRANSFORM_CHANGED;
181 SVGUtils::NotifyChildrenOfSVGChange(this, flags);
184 } else if (aAttribute == nsGkAtoms::transform ||
185 aAttribute == nsGkAtoms::preserveAspectRatio ||
186 aAttribute == nsGkAtoms::viewBox || aAttribute == nsGkAtoms::x ||
187 aAttribute == nsGkAtoms::y) {
188 // make sure our cached transform matrix gets (lazily) updated
189 mCanvasTM = nullptr;
191 SVGUtils::NotifyChildrenOfSVGChange(
192 this, aAttribute == nsGkAtoms::viewBox
193 ? TRANSFORM_CHANGED | COORD_CONTEXT_CHANGED
194 : TRANSFORM_CHANGED);
196 // We don't invalidate for transform changes (the layers code does that).
197 // Also note that SVGTransformableElement::GetAttributeChangeHint will
198 // return nsChangeHint_UpdateOverflow for "transform" attribute changes
199 // and cause DoApplyRenderingChangeToTree to make the SchedulePaint call.
201 if (aAttribute == nsGkAtoms::x || aAttribute == nsGkAtoms::y) {
202 nsLayoutUtils::PostRestyleEvent(
203 mContent->AsElement(), RestyleHint{0},
204 nsChangeHint_InvalidateRenderingObservers);
205 SVGUtils::ScheduleReflowSVG(this);
206 } else if (aAttribute == nsGkAtoms::viewBox ||
207 (aAttribute == nsGkAtoms::preserveAspectRatio &&
208 content->HasViewBoxOrSyntheticViewBox())) {
209 content->ChildrenOnlyTransformChanged();
210 // SchedulePaint sets a global state flag so we only need to call it
211 // once (on ourself is fine), not once on each child (despite bug
212 // 828240).
213 SchedulePaint();
218 return NS_OK;
221 nsIFrame* SVGViewportFrame::GetFrameForPoint(const gfxPoint& aPoint) {
222 MOZ_ASSERT_UNREACHABLE("A clipPath cannot contain svg or symbol elements");
223 return nullptr;
226 //----------------------------------------------------------------------
227 // ISVGSVGFrame methods:
229 void SVGViewportFrame::NotifyViewportOrTransformChanged(uint32_t aFlags) {
230 // The dimensions of inner-<svg> frames are purely defined by their "width"
231 // and "height" attributes, and transform changes can only occur as a result
232 // of changes to their "width", "height", "viewBox" or "preserveAspectRatio"
233 // attributes. Changes to all of these attributes are handled in
234 // AttributeChanged(), so we should never be called.
235 NS_ERROR("Not called for SVGViewportFrame");
238 //----------------------------------------------------------------------
239 // SVGContainerFrame methods:
241 bool SVGViewportFrame::HasChildrenOnlyTransform(gfx::Matrix* aTransform) const {
242 SVGViewportElement* content = static_cast<SVGViewportElement*>(GetContent());
244 if (content->HasViewBoxOrSyntheticViewBox()) {
245 // XXX Maybe return false if the transform is the identity transform?
246 if (aTransform) {
247 *aTransform = content->GetViewBoxTransform();
249 return true;
251 return false;
254 } // namespace mozilla