Bug 1867190 - Initialise the PHC allocate delay later r=glandium
[gecko.git] / layout / svg / SVGPatternFrame.cpp
blob20863db17e80800247fa6335d5769e12b4cb8145
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 "SVGPatternFrame.h"
10 // Keep others in (case-insensitive) order:
11 #include "AutoReferenceChainGuard.h"
12 #include "gfx2DGlue.h"
13 #include "gfxContext.h"
14 #include "gfxMatrix.h"
15 #include "gfxPattern.h"
16 #include "gfxPlatform.h"
17 #include "mozilla/ComputedStyle.h"
18 #include "mozilla/ISVGDisplayableFrame.h"
19 #include "mozilla/PresShell.h"
20 #include "mozilla/SVGContentUtils.h"
21 #include "mozilla/SVGGeometryFrame.h"
22 #include "mozilla/SVGObserverUtils.h"
23 #include "mozilla/SVGUtils.h"
24 #include "mozilla/dom/SVGPatternElement.h"
25 #include "mozilla/dom/SVGUnitTypesBinding.h"
26 #include "mozilla/gfx/2D.h"
27 #include "nsGkAtoms.h"
28 #include "nsIFrameInlines.h"
29 #include "SVGAnimatedTransformList.h"
31 using namespace mozilla::dom;
32 using namespace mozilla::dom::SVGUnitTypes_Binding;
33 using namespace mozilla::gfx;
34 using namespace mozilla::image;
36 namespace mozilla {
38 //----------------------------------------------------------------------
39 // Implementation
41 SVGPatternFrame::SVGPatternFrame(ComputedStyle* aStyle,
42 nsPresContext* aPresContext)
43 : SVGPaintServerFrame(aStyle, aPresContext, kClassID),
44 mSource(nullptr),
45 mLoopFlag(false),
46 mNoHRefURI(false) {}
48 NS_IMPL_FRAMEARENA_HELPERS(SVGPatternFrame)
50 NS_QUERYFRAME_HEAD(SVGPatternFrame)
51 NS_QUERYFRAME_ENTRY(SVGPatternFrame)
52 NS_QUERYFRAME_TAIL_INHERITING(SVGPaintServerFrame)
54 //----------------------------------------------------------------------
55 // nsIFrame methods:
57 nsresult SVGPatternFrame::AttributeChanged(int32_t aNameSpaceID,
58 nsAtom* aAttribute,
59 int32_t aModType) {
60 if (aNameSpaceID == kNameSpaceID_None &&
61 (aAttribute == nsGkAtoms::patternUnits ||
62 aAttribute == nsGkAtoms::patternContentUnits ||
63 aAttribute == nsGkAtoms::patternTransform ||
64 aAttribute == nsGkAtoms::x || aAttribute == nsGkAtoms::y ||
65 aAttribute == nsGkAtoms::width || aAttribute == nsGkAtoms::height ||
66 aAttribute == nsGkAtoms::preserveAspectRatio ||
67 aAttribute == nsGkAtoms::viewBox)) {
68 SVGObserverUtils::InvalidateRenderingObservers(this);
71 if ((aNameSpaceID == kNameSpaceID_XLink ||
72 aNameSpaceID == kNameSpaceID_None) &&
73 aAttribute == nsGkAtoms::href) {
74 // Blow away our reference, if any
75 SVGObserverUtils::RemoveTemplateObserver(this);
76 mNoHRefURI = false;
77 // And update whoever references us
78 SVGObserverUtils::InvalidateRenderingObservers(this);
81 return SVGPaintServerFrame::AttributeChanged(aNameSpaceID, aAttribute,
82 aModType);
85 #ifdef DEBUG
86 void SVGPatternFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
87 nsIFrame* aPrevInFlow) {
88 NS_ASSERTION(aContent->IsSVGElement(nsGkAtoms::pattern),
89 "Content is not an SVG pattern");
91 SVGPaintServerFrame::Init(aContent, aParent, aPrevInFlow);
93 #endif /* DEBUG */
95 //----------------------------------------------------------------------
96 // SVGContainerFrame methods:
98 // If our GetCanvasTM is getting called, we
99 // need to return *our current* transformation
100 // matrix, which depends on our units parameters
101 // and X, Y, Width, and Height
102 gfxMatrix SVGPatternFrame::GetCanvasTM() {
103 if (mCTM) {
104 return *mCTM;
107 // Do we know our rendering parent?
108 if (mSource) {
109 // Yes, use it!
110 return mSource->GetCanvasTM();
113 // We get here when geometry in the <pattern> container is updated
114 return gfxMatrix();
117 // -------------------------------------------------------------------------
118 // Helper functions
119 // -------------------------------------------------------------------------
121 /** Calculate the maximum expansion of a matrix */
122 static float MaxExpansion(const Matrix& aMatrix) {
123 // maximum expansion derivation from
124 // http://lists.cairographics.org/archives/cairo/2004-October/001980.html
125 // and also implemented in cairo_matrix_transformed_circle_major_axis
126 double a = aMatrix._11;
127 double b = aMatrix._12;
128 double c = aMatrix._21;
129 double d = aMatrix._22;
130 double f = (a * a + b * b + c * c + d * d) / 2;
131 double g = (a * a + b * b - c * c - d * d) / 2;
132 double h = a * c + b * d;
133 return sqrt(f + sqrt(g * g + h * h));
136 // The SVG specification says that the 'patternContentUnits' attribute "has no
137 // effect if attribute ‘viewBox’ is specified". We still need to include a bbox
138 // scale if the viewBox is specified and _patternUnits_ is set to or defaults to
139 // objectBoundingBox though, since in that case the viewBox is relative to the
140 // bbox
141 static bool IncludeBBoxScale(const SVGAnimatedViewBox& aViewBox,
142 uint32_t aPatternContentUnits,
143 uint32_t aPatternUnits) {
144 return (!aViewBox.IsExplicitlySet() &&
145 aPatternContentUnits == SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) ||
146 (aViewBox.IsExplicitlySet() &&
147 aPatternUnits == SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
150 // Given the matrix for the pattern element's own transform, this returns a
151 // combined matrix including the transforms applicable to its target.
152 static Matrix GetPatternMatrix(nsIFrame* aSource,
153 const StyleSVGPaint nsStyleSVG::*aFillOrStroke,
154 uint16_t aPatternUnits,
155 const gfxMatrix& patternTransform,
156 const gfxRect& bbox, const gfxRect& callerBBox,
157 const Matrix& callerCTM) {
158 // We really want the pattern matrix to handle translations
159 gfxFloat minx = bbox.X();
160 gfxFloat miny = bbox.Y();
162 if (aPatternUnits == SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
163 minx += callerBBox.X();
164 miny += callerBBox.Y();
167 double scale = 1.0 / MaxExpansion(callerCTM);
168 auto patternMatrix = patternTransform;
169 patternMatrix.PreScale(scale, scale);
170 patternMatrix.PreTranslate(minx, miny);
172 // revert the vector effect transform so that the pattern appears unchanged
173 if (aFillOrStroke == &nsStyleSVG::mStroke) {
174 gfxMatrix userToOuterSVG;
175 if (SVGUtils::GetNonScalingStrokeTransform(aSource, &userToOuterSVG)) {
176 patternMatrix *= userToOuterSVG;
180 return ToMatrix(patternMatrix);
183 static nsresult GetTargetGeometry(gfxRect* aBBox,
184 const SVGAnimatedViewBox& aViewBox,
185 uint16_t aPatternContentUnits,
186 uint16_t aPatternUnits, nsIFrame* aTarget,
187 const Matrix& aContextMatrix,
188 const gfxRect* aOverrideBounds) {
189 *aBBox =
190 aOverrideBounds
191 ? *aOverrideBounds
192 : SVGUtils::GetBBox(aTarget, SVGUtils::eUseFrameBoundsForOuterSVG |
193 SVGUtils::eBBoxIncludeFillGeometry);
195 // Sanity check
196 if (IncludeBBoxScale(aViewBox, aPatternContentUnits, aPatternUnits) &&
197 (aBBox->Width() <= 0 || aBBox->Height() <= 0)) {
198 return NS_ERROR_FAILURE;
201 // OK, now fix up the bounding box to reflect user coordinates
202 // We handle device unit scaling in pattern matrix
203 float scale = MaxExpansion(aContextMatrix);
204 if (scale <= 0) {
205 return NS_ERROR_FAILURE;
207 aBBox->Scale(scale);
208 return NS_OK;
211 void SVGPatternFrame::PaintChildren(DrawTarget* aDrawTarget,
212 SVGPatternFrame* aPatternWithChildren,
213 nsIFrame* aSource, float aGraphicOpacity,
214 imgDrawingParams& aImgParams) {
215 gfxContext ctx(aDrawTarget);
216 gfxGroupForBlendAutoSaveRestore autoGroupForBlend(&ctx);
218 if (aGraphicOpacity != 1.0f) {
219 autoGroupForBlend.PushGroupForBlendBack(gfxContentType::COLOR_ALPHA,
220 aGraphicOpacity);
223 // OK, now render -- note that we use "firstKid", which
224 // we got at the beginning because it takes care of the
225 // referenced pattern situation for us
227 if (aSource->IsSVGGeometryFrame()) {
228 // Set the geometrical parent of the pattern we are rendering
229 aPatternWithChildren->mSource = static_cast<SVGGeometryFrame*>(aSource);
232 // Delay checking NS_FRAME_DRAWING_AS_PAINTSERVER bit until here so we can
233 // give back a clear surface if there's a loop
234 if (!aPatternWithChildren->HasAnyStateBits(NS_FRAME_DRAWING_AS_PAINTSERVER)) {
235 AutoSetRestorePaintServerState paintServer(aPatternWithChildren);
236 for (auto* kid : aPatternWithChildren->mFrames) {
237 gfxMatrix tm = *(aPatternWithChildren->mCTM);
239 // The CTM of each frame referencing us can be different
240 ISVGDisplayableFrame* SVGFrame = do_QueryFrame(kid);
241 if (SVGFrame) {
242 SVGFrame->NotifySVGChanged(ISVGDisplayableFrame::TRANSFORM_CHANGED);
243 tm = SVGUtils::GetTransformMatrixInUserSpace(kid) * tm;
246 SVGUtils::PaintFrameWithEffects(kid, ctx, tm, aImgParams);
250 aPatternWithChildren->mSource = nullptr;
253 already_AddRefed<SourceSurface> SVGPatternFrame::PaintPattern(
254 const DrawTarget* aDrawTarget, Matrix* patternMatrix,
255 const Matrix& aContextMatrix, nsIFrame* aSource,
256 StyleSVGPaint nsStyleSVG::*aFillOrStroke, float aGraphicOpacity,
257 const gfxRect* aOverrideBounds, imgDrawingParams& aImgParams) {
259 * General approach:
260 * Set the content geometry stuff
261 * Calculate our bbox (using x,y,width,height & patternUnits &
262 * patternTransform)
263 * Create the surface
264 * Calculate the content transformation matrix
265 * Get our children (we may need to get them from another Pattern)
266 * Call SVGPaint on all of our children
267 * Return
270 SVGPatternFrame* patternWithChildren = GetPatternWithChildren();
271 if (!patternWithChildren) {
272 // Either no kids or a bad reference
273 return nullptr;
276 const SVGAnimatedViewBox& viewBox = GetViewBox();
278 uint16_t patternContentUnits =
279 GetEnumValue(SVGPatternElement::PATTERNCONTENTUNITS);
280 uint16_t patternUnits = GetEnumValue(SVGPatternElement::PATTERNUNITS);
283 * Get the content geometry information. This is a little tricky --
284 * our parent is probably a <defs>, but we are rendering in the context
285 * of some geometry source. Our content geometry information needs to
286 * come from our rendering parent as opposed to our content parent. We
287 * get that information from aSource, which is passed to us from the
288 * backend renderer.
290 * There are three "geometries" that we need:
291 * 1) The bounding box for the pattern. We use this to get the
292 * width and height for the surface, and as the return to
293 * GetBBox.
294 * 2) The transformation matrix for the pattern. This is not *quite*
295 * the same as the canvas transformation matrix that we will
296 * provide to our rendering children since we "fudge" it a little
297 * to get the renderer to handle the translations correctly for us.
298 * 3) The CTM that we return to our children who make up the pattern.
301 // Get all of the information we need from our "caller" -- i.e.
302 // the geometry that is being rendered with a pattern
303 gfxRect callerBBox;
304 if (NS_FAILED(GetTargetGeometry(&callerBBox, viewBox, patternContentUnits,
305 patternUnits, aSource, aContextMatrix,
306 aOverrideBounds))) {
307 return nullptr;
310 // Construct the CTM that we will provide to our children when we
311 // render them into the tile.
312 gfxMatrix ctm = ConstructCTM(viewBox, patternContentUnits, patternUnits,
313 callerBBox, aContextMatrix, aSource);
314 if (ctm.IsSingular()) {
315 return nullptr;
318 if (patternWithChildren->mCTM) {
319 *patternWithChildren->mCTM = ctm;
320 } else {
321 patternWithChildren->mCTM = MakeUnique<gfxMatrix>(ctm);
324 // Get the bounding box of the pattern. This will be used to determine
325 // the size of the surface, and will also be used to define the bounding
326 // box for the pattern tile.
327 gfxRect bbox =
328 GetPatternRect(patternUnits, callerBBox, aContextMatrix, aSource);
329 if (bbox.Width() <= 0.0 || bbox.Height() <= 0.0) {
330 return nullptr;
333 // Get the pattern transform
334 auto patternTransform = GetPatternTransform();
336 // Get the transformation matrix that we will hand to the renderer's pattern
337 // routine.
338 *patternMatrix =
339 GetPatternMatrix(aSource, aFillOrStroke, patternUnits, patternTransform,
340 bbox, callerBBox, aContextMatrix);
341 if (patternMatrix->IsSingular()) {
342 return nullptr;
345 // Now that we have all of the necessary geometries, we can
346 // create our surface.
347 gfxSize scaledSize = bbox.Size() * MaxExpansion(ToMatrix(patternTransform));
349 bool resultOverflows;
350 IntSize surfaceSize =
351 SVGUtils::ConvertToSurfaceSize(scaledSize, &resultOverflows);
353 // 0 disables rendering, < 0 is an error
354 if (surfaceSize.width <= 0 || surfaceSize.height <= 0) {
355 return nullptr;
358 gfxFloat patternWidth = bbox.Width();
359 gfxFloat patternHeight = bbox.Height();
361 if (resultOverflows || patternWidth != surfaceSize.width ||
362 patternHeight != surfaceSize.height) {
363 // scale drawing to pattern surface size
364 patternWithChildren->mCTM->PostScale(surfaceSize.width / patternWidth,
365 surfaceSize.height / patternHeight);
367 // and rescale pattern to compensate
368 patternMatrix->PreScale(patternWidth / surfaceSize.width,
369 patternHeight / surfaceSize.height);
372 RefPtr<DrawTarget> dt = aDrawTarget->CreateSimilarDrawTargetWithBacking(
373 surfaceSize, SurfaceFormat::B8G8R8A8);
374 if (!dt || !dt->IsValid()) {
375 return nullptr;
377 dt->ClearRect(Rect(0, 0, surfaceSize.width, surfaceSize.height));
379 PaintChildren(dt, patternWithChildren, aSource, aGraphicOpacity, aImgParams);
381 // caller now owns the surface
382 return dt->GetBackingSurface();
385 /* Will probably need something like this... */
386 // How do we handle the insertion of a new frame?
387 // We really don't want to rerender this every time,
388 // do we?
389 SVGPatternFrame* SVGPatternFrame::GetPatternWithChildren() {
390 // Do we have any children ourselves?
391 if (!mFrames.IsEmpty()) {
392 return this;
395 // No, see if we chain to someone who does
397 // Before we recurse, make sure we'll break reference loops and over long
398 // reference chains:
399 static int16_t sRefChainLengthCounter = AutoReferenceChainGuard::noChain;
400 AutoReferenceChainGuard refChainGuard(this, &mLoopFlag,
401 &sRefChainLengthCounter);
402 if (MOZ_UNLIKELY(!refChainGuard.Reference())) {
403 // Break reference chain
404 return nullptr;
407 SVGPatternFrame* next = GetReferencedPattern();
408 if (!next) {
409 return nullptr;
412 return next->GetPatternWithChildren();
415 uint16_t SVGPatternFrame::GetEnumValue(uint32_t aIndex, nsIContent* aDefault) {
416 SVGAnimatedEnumeration& thisEnum =
417 static_cast<SVGPatternElement*>(GetContent())->mEnumAttributes[aIndex];
419 if (thisEnum.IsExplicitlySet()) {
420 return thisEnum.GetAnimValue();
423 // Before we recurse, make sure we'll break reference loops and over long
424 // reference chains:
425 static int16_t sRefChainLengthCounter = AutoReferenceChainGuard::noChain;
426 AutoReferenceChainGuard refChainGuard(this, &mLoopFlag,
427 &sRefChainLengthCounter);
428 if (MOZ_UNLIKELY(!refChainGuard.Reference())) {
429 // Break reference chain
430 return static_cast<SVGPatternElement*>(aDefault)
431 ->mEnumAttributes[aIndex]
432 .GetAnimValue();
435 SVGPatternFrame* next = GetReferencedPattern();
436 return next ? next->GetEnumValue(aIndex, aDefault)
437 : static_cast<SVGPatternElement*>(aDefault)
438 ->mEnumAttributes[aIndex]
439 .GetAnimValue();
442 SVGAnimatedTransformList* SVGPatternFrame::GetPatternTransformList(
443 nsIContent* aDefault) {
444 SVGAnimatedTransformList* thisTransformList =
445 static_cast<SVGPatternElement*>(GetContent())->GetAnimatedTransformList();
447 if (thisTransformList && thisTransformList->IsExplicitlySet())
448 return thisTransformList;
450 // Before we recurse, make sure we'll break reference loops and over long
451 // reference chains:
452 static int16_t sRefChainLengthCounter = AutoReferenceChainGuard::noChain;
453 AutoReferenceChainGuard refChainGuard(this, &mLoopFlag,
454 &sRefChainLengthCounter);
455 if (MOZ_UNLIKELY(!refChainGuard.Reference())) {
456 // Break reference chain
457 return static_cast<SVGPatternElement*>(aDefault)->mPatternTransform.get();
460 SVGPatternFrame* next = GetReferencedPattern();
461 return next ? next->GetPatternTransformList(aDefault)
462 : static_cast<SVGPatternElement*>(aDefault)
463 ->mPatternTransform.get();
466 gfxMatrix SVGPatternFrame::GetPatternTransform() {
467 SVGAnimatedTransformList* animTransformList =
468 GetPatternTransformList(GetContent());
469 if (!animTransformList) {
470 return gfxMatrix();
473 return animTransformList->GetAnimValue().GetConsolidationMatrix();
476 const SVGAnimatedViewBox& SVGPatternFrame::GetViewBox(nsIContent* aDefault) {
477 const SVGAnimatedViewBox& thisViewBox =
478 static_cast<SVGPatternElement*>(GetContent())->mViewBox;
480 if (thisViewBox.IsExplicitlySet()) {
481 return thisViewBox;
484 // Before we recurse, make sure we'll break reference loops and over long
485 // reference chains:
486 static int16_t sRefChainLengthCounter = AutoReferenceChainGuard::noChain;
487 AutoReferenceChainGuard refChainGuard(this, &mLoopFlag,
488 &sRefChainLengthCounter);
489 if (MOZ_UNLIKELY(!refChainGuard.Reference())) {
490 // Break reference chain
491 return static_cast<SVGPatternElement*>(aDefault)->mViewBox;
494 SVGPatternFrame* next = GetReferencedPattern();
495 return next ? next->GetViewBox(aDefault)
496 : static_cast<SVGPatternElement*>(aDefault)->mViewBox;
499 const SVGAnimatedPreserveAspectRatio& SVGPatternFrame::GetPreserveAspectRatio(
500 nsIContent* aDefault) {
501 const SVGAnimatedPreserveAspectRatio& thisPar =
502 static_cast<SVGPatternElement*>(GetContent())->mPreserveAspectRatio;
504 if (thisPar.IsExplicitlySet()) {
505 return thisPar;
508 // Before we recurse, make sure we'll break reference loops and over long
509 // reference chains:
510 static int16_t sRefChainLengthCounter = AutoReferenceChainGuard::noChain;
511 AutoReferenceChainGuard refChainGuard(this, &mLoopFlag,
512 &sRefChainLengthCounter);
513 if (MOZ_UNLIKELY(!refChainGuard.Reference())) {
514 // Break reference chain
515 return static_cast<SVGPatternElement*>(aDefault)->mPreserveAspectRatio;
518 SVGPatternFrame* next = GetReferencedPattern();
519 return next ? next->GetPreserveAspectRatio(aDefault)
520 : static_cast<SVGPatternElement*>(aDefault)->mPreserveAspectRatio;
523 const SVGAnimatedLength* SVGPatternFrame::GetLengthValue(uint32_t aIndex,
524 nsIContent* aDefault) {
525 const SVGAnimatedLength* thisLength =
526 &static_cast<SVGPatternElement*>(GetContent())->mLengthAttributes[aIndex];
528 if (thisLength->IsExplicitlySet()) {
529 return thisLength;
532 // Before we recurse, make sure we'll break reference loops and over long
533 // reference chains:
534 static int16_t sRefChainLengthCounter = AutoReferenceChainGuard::noChain;
535 AutoReferenceChainGuard refChainGuard(this, &mLoopFlag,
536 &sRefChainLengthCounter);
537 if (MOZ_UNLIKELY(!refChainGuard.Reference())) {
538 // Break reference chain
539 return &static_cast<SVGPatternElement*>(aDefault)
540 ->mLengthAttributes[aIndex];
543 SVGPatternFrame* next = GetReferencedPattern();
544 return next ? next->GetLengthValue(aIndex, aDefault)
545 : &static_cast<SVGPatternElement*>(aDefault)
546 ->mLengthAttributes[aIndex];
549 // Private (helper) methods
551 SVGPatternFrame* SVGPatternFrame::GetReferencedPattern() {
552 if (mNoHRefURI) {
553 return nullptr;
556 auto GetHref = [this](nsAString& aHref) {
557 SVGPatternElement* pattern =
558 static_cast<SVGPatternElement*>(this->GetContent());
559 if (pattern->mStringAttributes[SVGPatternElement::HREF].IsExplicitlySet()) {
560 pattern->mStringAttributes[SVGPatternElement::HREF].GetAnimValue(aHref,
561 pattern);
562 } else {
563 pattern->mStringAttributes[SVGPatternElement::XLINK_HREF].GetAnimValue(
564 aHref, pattern);
566 this->mNoHRefURI = aHref.IsEmpty();
569 // We don't call SVGObserverUtils::RemoveTemplateObserver and set
570 // `mNoHRefURI = false` on failure since we want to be invalidated if the ID
571 // specified by our href starts resolving to a different/valid element.
573 return do_QueryFrame(SVGObserverUtils::GetAndObserveTemplate(this, GetHref));
576 gfxRect SVGPatternFrame::GetPatternRect(uint16_t aPatternUnits,
577 const gfxRect& aTargetBBox,
578 const Matrix& aTargetCTM,
579 nsIFrame* aTarget) {
580 // We need to initialize our box
581 float x, y, width, height;
583 // Get the pattern x,y,width, and height
584 const SVGAnimatedLength *tmpX, *tmpY, *tmpHeight, *tmpWidth;
585 tmpX = GetLengthValue(SVGPatternElement::ATTR_X);
586 tmpY = GetLengthValue(SVGPatternElement::ATTR_Y);
587 tmpHeight = GetLengthValue(SVGPatternElement::ATTR_HEIGHT);
588 tmpWidth = GetLengthValue(SVGPatternElement::ATTR_WIDTH);
590 if (aPatternUnits == SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
591 x = SVGUtils::ObjectSpace(aTargetBBox, tmpX);
592 y = SVGUtils::ObjectSpace(aTargetBBox, tmpY);
593 width = SVGUtils::ObjectSpace(aTargetBBox, tmpWidth);
594 height = SVGUtils::ObjectSpace(aTargetBBox, tmpHeight);
595 } else {
596 if (aTarget->IsTextFrame()) {
597 aTarget = aTarget->GetParent();
599 float scale = MaxExpansion(aTargetCTM);
600 x = SVGUtils::UserSpace(aTarget, tmpX) * scale;
601 y = SVGUtils::UserSpace(aTarget, tmpY) * scale;
602 width = SVGUtils::UserSpace(aTarget, tmpWidth) * scale;
603 height = SVGUtils::UserSpace(aTarget, tmpHeight) * scale;
606 return gfxRect(x, y, width, height);
609 gfxMatrix SVGPatternFrame::ConstructCTM(const SVGAnimatedViewBox& aViewBox,
610 uint16_t aPatternContentUnits,
611 uint16_t aPatternUnits,
612 const gfxRect& callerBBox,
613 const Matrix& callerCTM,
614 nsIFrame* aTarget) {
615 if (aTarget->IsTextFrame()) {
616 aTarget = aTarget->GetParent();
618 nsIContent* targetContent = aTarget->GetContent();
619 SVGViewportElement* ctx = nullptr;
620 gfxFloat scaleX, scaleY;
622 // The objectBoundingBox conversion must be handled in the CTM:
623 if (IncludeBBoxScale(aViewBox, aPatternContentUnits, aPatternUnits)) {
624 scaleX = callerBBox.Width();
625 scaleY = callerBBox.Height();
626 } else {
627 if (targetContent->IsSVGElement()) {
628 ctx = static_cast<SVGElement*>(targetContent)->GetCtx();
630 scaleX = scaleY = MaxExpansion(callerCTM);
633 if (!aViewBox.IsExplicitlySet()) {
634 return gfxMatrix(scaleX, 0.0, 0.0, scaleY, 0.0, 0.0);
636 const SVGViewBox& viewBox = aViewBox.GetAnimValue();
638 if (viewBox.height <= 0.0f || viewBox.width <= 0.0f) {
639 return gfxMatrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0); // singular
642 float viewportWidth, viewportHeight;
643 if (targetContent->IsSVGElement()) {
644 // If we're dealing with an SVG target only retrieve the context once.
645 // Calling the nsIFrame* variant of GetAnimValue would look it up on
646 // every call.
647 viewportWidth =
648 GetLengthValue(SVGPatternElement::ATTR_WIDTH)->GetAnimValue(ctx);
649 viewportHeight =
650 GetLengthValue(SVGPatternElement::ATTR_HEIGHT)->GetAnimValue(ctx);
651 } else {
652 // No SVG target, call the nsIFrame* variant of GetAnimValue.
653 viewportWidth =
654 GetLengthValue(SVGPatternElement::ATTR_WIDTH)->GetAnimValue(aTarget);
655 viewportHeight =
656 GetLengthValue(SVGPatternElement::ATTR_HEIGHT)->GetAnimValue(aTarget);
659 if (viewportWidth <= 0.0f || viewportHeight <= 0.0f) {
660 return gfxMatrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0); // singular
663 Matrix tm = SVGContentUtils::GetViewBoxTransform(
664 viewportWidth * scaleX, viewportHeight * scaleY, viewBox.x, viewBox.y,
665 viewBox.width, viewBox.height, GetPreserveAspectRatio());
667 return ThebesMatrix(tm);
670 //----------------------------------------------------------------------
671 // SVGPaintServerFrame methods:
672 already_AddRefed<gfxPattern> SVGPatternFrame::GetPaintServerPattern(
673 nsIFrame* aSource, const DrawTarget* aDrawTarget,
674 const gfxMatrix& aContextMatrix, StyleSVGPaint nsStyleSVG::*aFillOrStroke,
675 float aGraphicOpacity, imgDrawingParams& aImgParams,
676 const gfxRect* aOverrideBounds) {
677 if (aGraphicOpacity == 0.0f) {
678 return do_AddRef(new gfxPattern(DeviceColor()));
681 // Paint it!
682 Matrix pMatrix;
683 RefPtr<SourceSurface> surface =
684 PaintPattern(aDrawTarget, &pMatrix, ToMatrix(aContextMatrix), aSource,
685 aFillOrStroke, aGraphicOpacity, aOverrideBounds, aImgParams);
687 if (!surface) {
688 return nullptr;
691 RefPtr<gfxPattern> pattern = new gfxPattern(surface, pMatrix);
693 if (!pattern) {
694 return nullptr;
697 pattern->SetExtend(ExtendMode::REPEAT);
698 return pattern.forget();
701 } // namespace mozilla
703 // -------------------------------------------------------------------------
704 // Public functions
705 // -------------------------------------------------------------------------
707 nsIFrame* NS_NewSVGPatternFrame(mozilla::PresShell* aPresShell,
708 mozilla::ComputedStyle* aStyle) {
709 return new (aPresShell)
710 mozilla::SVGPatternFrame(aStyle, aPresShell->GetPresContext());