summaryrefslogtreecommitdiffstats
path: root/layout/svg/SVGGeometryFrame.cpp
blob: 0e00d60a318b87c88a3c9cb705461e84ed052dce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Main header first:
#include "SVGGeometryFrame.h"

// Keep others in (case-insensitive) order:
#include "gfx2DGlue.h"
#include "gfxContext.h"
#include "gfxPlatform.h"
#include "gfxUtils.h"
#include "mozilla/dom/SVGGeometryElement.h"
#include "mozilla/dom/SVGGraphicsElement.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/Helpers.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/PresShell.h"
#include "mozilla/RefPtr.h"
#include "mozilla/SVGContextPaint.h"
#include "mozilla/SVGContentUtils.h"
#include "mozilla/SVGObserverUtils.h"
#include "mozilla/SVGUtils.h"
#include "nsGkAtoms.h"
#include "nsLayoutUtils.h"
#include "SVGAnimatedTransformList.h"
#include "SVGMarkerFrame.h"

using namespace mozilla::dom;
using namespace mozilla::gfx;
using namespace mozilla::image;

//----------------------------------------------------------------------
// Implementation

nsIFrame* NS_NewSVGGeometryFrame(mozilla::PresShell* aPresShell,
                                 mozilla::ComputedStyle* aStyle) {
  return new (aPresShell)
      mozilla::SVGGeometryFrame(aStyle, aPresShell->GetPresContext());
}

namespace mozilla {

NS_IMPL_FRAMEARENA_HELPERS(SVGGeometryFrame)

//----------------------------------------------------------------------
// nsQueryFrame methods

NS_QUERYFRAME_HEAD(SVGGeometryFrame)
  NS_QUERYFRAME_ENTRY(ISVGDisplayableFrame)
  NS_QUERYFRAME_ENTRY(SVGGeometryFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsIFrame)

//----------------------------------------------------------------------
// nsIFrame methods

void SVGGeometryFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
                            nsIFrame* aPrevInFlow) {
  AddStateBits(aParent->GetStateBits() & NS_STATE_SVG_CLIPPATH_CHILD);
  nsIFrame::Init(aContent, aParent, aPrevInFlow);
}

nsresult SVGGeometryFrame::AttributeChanged(int32_t aNameSpaceID,
                                            nsAtom* aAttribute,
                                            int32_t aModType) {
  // We don't invalidate for transform changes (the layers code does that).
  // Also note that SVGTransformableElement::GetAttributeChangeHint will
  // return nsChangeHint_UpdateOverflow for "transform" attribute changes
  // and cause DoApplyRenderingChangeToTree to make the SchedulePaint call.

  if (aNameSpaceID == kNameSpaceID_None &&
      (static_cast<SVGGeometryElement*>(GetContent())
           ->AttributeDefinesGeometry(aAttribute))) {
    nsLayoutUtils::PostRestyleEvent(mContent->AsElement(), RestyleHint{0},
                                    nsChangeHint_InvalidateRenderingObservers);
    SVGUtils::ScheduleReflowSVG(this);
  }
  return NS_OK;
}

/* virtual */
void SVGGeometryFrame::DidSetComputedStyle(ComputedStyle* aOldComputedStyle) {
  nsIFrame::DidSetComputedStyle(aOldComputedStyle);
  auto* element = static_cast<SVGGeometryElement*>(GetContent());
  if (!aOldComputedStyle) {
    element->ClearAnyCachedPath();
    return;
  }

  const auto* oldStyleSVG = aOldComputedStyle->StyleSVG();
  if (!SVGContentUtils::ShapeTypeHasNoCorners(GetContent())) {
    if (StyleSVG()->mStrokeLinecap != oldStyleSVG->mStrokeLinecap &&
        element->IsSVGElement(nsGkAtoms::path)) {
      // If the stroke-linecap changes to or from "butt" then our element
      // needs to update its cached Moz2D Path, since SVGPathData::BuildPath
      // decides whether or not to insert little lines into the path for zero
      // length subpaths base on that property.
      element->ClearAnyCachedPath();
    } else if (HasAnyStateBits(NS_STATE_SVG_CLIPPATH_CHILD)) {
      if (StyleSVG()->mClipRule != oldStyleSVG->mClipRule) {
        // Moz2D Path objects are fill-rule specific.
        // For clipPath we use clip-rule as the path's fill-rule.
        element->ClearAnyCachedPath();
      }
    } else {
      if (StyleSVG()->mFillRule != oldStyleSVG->mFillRule) {
        // Moz2D Path objects are fill-rule specific.
        element->ClearAnyCachedPath();
      }
    }
  }

  if (element->IsGeometryChangedViaCSS(*Style(), *aOldComputedStyle)) {
    element->ClearAnyCachedPath();
    SVGObserverUtils::InvalidateRenderingObservers(this);
  }
}

bool SVGGeometryFrame::IsSVGTransformed(
    gfx::Matrix* aOwnTransform, gfx::Matrix* aFromParentTransform) const {
  return SVGUtils::IsSVGTransformed(this, aOwnTransform, aFromParentTransform);
}

void SVGGeometryFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
                                        const nsDisplayListSet& aLists) {
  if (!static_cast<const SVGElement*>(GetContent())->HasValidDimensions()) {
    return;
  }

  if (aBuilder->IsForPainting()) {
    if (!IsVisibleForPainting()) {
      return;
    }
    if (StyleEffects()->IsTransparent()) {
      return;
    }
    const auto* styleSVG = StyleSVG();
    if (styleSVG->mFill.kind.IsNone() && styleSVG->mStroke.kind.IsNone() &&
        !styleSVG->HasMarker()) {
      return;
    }

    aBuilder->BuildCompositorHitTestInfoIfNeeded(this,
                                                 aLists.BorderBackground());
  }

  DisplayOutline(aBuilder, aLists);
  aLists.Content()->AppendNewToTop<DisplaySVGGeometry>(aBuilder, this);
}

//----------------------------------------------------------------------
// ISVGDisplayableFrame methods

void SVGGeometryFrame::PaintSVG(gfxContext& aContext,
                                const gfxMatrix& aTransform,
                                imgDrawingParams& aImgParams) {
  if (!StyleVisibility()->IsVisible()) {
    return;
  }

  // Matrix to the geometry's user space:
  gfxMatrix newMatrix =
      aContext.CurrentMatrixDouble().PreMultiply(aTransform).NudgeToIntegers();
  if (newMatrix.IsSingular()) {
    return;
  }

  uint32_t paintOrder = StyleSVG()->mPaintOrder;
  if (!paintOrder) {
    Render(&aContext, eRenderFill | eRenderStroke, newMatrix, aImgParams);
    PaintMarkers(aContext, aTransform, aImgParams);
  } else {
    while (paintOrder) {
      auto component = StylePaintOrder(paintOrder & kPaintOrderMask);
      switch (component) {
        case StylePaintOrder::Fill:
          Render(&aContext, eRenderFill, newMatrix, aImgParams);
          break;
        case StylePaintOrder::Stroke:
          Render(&aContext, eRenderStroke, newMatrix, aImgParams);
          break;
        case StylePaintOrder::Markers:
          PaintMarkers(aContext, aTransform, aImgParams);
          break;
        default:
          MOZ_FALLTHROUGH_ASSERT("Unknown paint-order variant, how?");
        case StylePaintOrder::Normal:
          break;
      }
      paintOrder >>= kPaintOrderShift;
    }
  }
}

nsIFrame* SVGGeometryFrame::GetFrameForPoint(const gfxPoint& aPoint) {
  FillRule fillRule;
  uint16_t hitTestFlags;
  if (HasAnyStateBits(NS_STATE_SVG_CLIPPATH_CHILD)) {
    hitTestFlags = SVG_HIT_TEST_FILL;
    fillRule = SVGUtils::ToFillRule(StyleSVG()->mClipRule);
  } else {
    hitTestFlags = SVGUtils::GetGeometryHitTestFlags(this);
    if (!hitTestFlags) {
      return nullptr;
    }
    fillRule = SVGUtils::ToFillRule(StyleSVG()->mFillRule);
  }

  bool isHit = false;

  SVGGeometryElement* content = static_cast<SVGGeometryElement*>(GetContent());

  // Using ScreenReferenceDrawTarget() opens us to Moz2D backend specific hit-
  // testing bugs. Maybe we should use a BackendType::CAIRO DT for hit-testing
  // so that we get more consistent/backwards compatible results?
  RefPtr<DrawTarget> drawTarget =
      gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget();
  RefPtr<Path> path = content->GetOrBuildPath(drawTarget, fillRule);
  if (!path) {
    return nullptr;  // no path, so we don't paint anything that can be hit
  }

  if (hitTestFlags & SVG_HIT_TEST_FILL) {
    isHit = path->ContainsPoint(ToPoint(aPoint), {});
  }
  if (!isHit && (hitTestFlags & SVG_HIT_TEST_STROKE)) {
    Point point = ToPoint(aPoint);
    SVGContentUtils::AutoStrokeOptions stroke;
    SVGContentUtils::GetStrokeOptions(&stroke, content, Style(), nullptr);
    gfxMatrix userToOuterSVG;
    if (SVGUtils::GetNonScalingStrokeTransform(this, &userToOuterSVG)) {
      // We need to transform the path back into the appropriate ancestor
      // coordinate system in order for non-scaled stroke to be correct.
      // Naturally we also need to transform the point into the same
      // coordinate system in order to hit-test against the path.
      point = ToMatrix(userToOuterSVG).TransformPoint(point);
      RefPtr<PathBuilder> builder =
          path->TransformedCopyToBuilder(ToMatrix(userToOuterSVG), fillRule);
      path = builder->Finish();
    }
    isHit = path->StrokeContainsPoint(stroke, point, {});
  }

  if (isHit && SVGUtils::HitTestClip(this, aPoint)) {
    return this;
  }

  return nullptr;
}

void SVGGeometryFrame::ReflowSVG() {
  NS_ASSERTION(SVGUtils::OuterSVGIsCallingReflowSVG(this),
               "This call is probably a wasteful mistake");

  MOZ_ASSERT(!HasAnyStateBits(NS_FRAME_IS_NONDISPLAY),
             "ReflowSVG mechanism not designed for this");

  if (!SVGUtils::NeedsReflowSVG(this)) {
    return;
  }

  uint32_t flags = SVGUtils::eBBoxIncludeFill | SVGUtils::eBBoxIncludeStroke |
                   SVGUtils::eBBoxIncludeMarkers;
  // Our "visual" overflow rect needs to be valid for building display lists
  // for hit testing, which means that for certain values of 'pointer-events'
  // it needs to include the geometry of the fill or stroke even when the fill/
  // stroke don't actually render (e.g. when stroke="none" or
  // stroke-opacity="0"). GetGeometryHitTestFlags() accounts for
  // 'pointer-events'.
  uint16_t hitTestFlags = SVGUtils::GetGeometryHitTestFlags(this);
  if (hitTestFlags & SVG_HIT_TEST_FILL) {
    flags |= SVGUtils::eBBoxIncludeFillGeometry;
  }
  if (hitTestFlags & SVG_HIT_TEST_STROKE) {
    flags |= SVGUtils::eBBoxIncludeStrokeGeometry;
  }

  gfxRect extent = GetBBoxContribution({}, flags).ToThebesRect();
  mRect = nsLayoutUtils::RoundGfxRectToAppRect(extent, AppUnitsPerCSSPixel());

  if (HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
    // Make sure we have our filter property (if any) before calling
    // FinishAndStoreOverflow (subsequent filter changes are handled off
    // nsChangeHint_UpdateEffects):
    SVGObserverUtils::UpdateEffects(this);
  }

  nsRect overflow = nsRect(nsPoint(0, 0), mRect.Size());
  OverflowAreas overflowAreas(overflow, overflow);
  FinishAndStoreOverflow(overflowAreas, mRect.Size());

  RemoveStateBits(NS_FRAME_FIRST_REFLOW | NS_FRAME_IS_DIRTY |
                  NS_FRAME_HAS_DIRTY_CHILDREN);

  // Invalidate, but only if this is not our first reflow (since if it is our
  // first reflow then we haven't had our first paint yet).
  if (!GetParent()->HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
    InvalidateFrame();
  }
}

void SVGGeometryFrame::NotifySVGChanged(uint32_t aFlags) {
  MOZ_ASSERT(aFlags & (TRANSFORM_CHANGED | COORD_CONTEXT_CHANGED),
             "Invalidation logic may need adjusting");

  // Changes to our ancestors may affect how we render when we are rendered as
  // part of our ancestor (specifically, if our coordinate context changes size
  // and we have percentage lengths defining our geometry, then we need to be
  // reflowed). However, ancestor changes cannot affect how we render when we
  // are rendered as part of any rendering observers that we may have.
  // Therefore no need to notify rendering observers here.

  // Don't try to be too smart trying to avoid the ScheduleReflowSVG calls
  // for the stroke properties examined below. Checking HasStroke() is not
  // enough, since what we care about is whether we include the stroke in our
  // overflow rects or not, and we sometimes deliberately include stroke
  // when it's not visible. See the complexities of GetBBoxContribution.

  if (aFlags & COORD_CONTEXT_CHANGED) {
    auto* geom = static_cast<SVGGeometryElement*>(GetContent());
    // Stroke currently contributes to our mRect, which is why we have to take
    // account of stroke-width here. Note that we do not need to take account
    // of stroke-dashoffset since, although that can have a percentage value
    // that is resolved against our coordinate context, it does not affect our
    // mRect.
    const auto& strokeWidth = StyleSVG()->mStrokeWidth;
    if (geom->GeometryDependsOnCoordCtx() ||
        (strokeWidth.IsLengthPercentage() &&
         strokeWidth.AsLengthPercentage().HasPercent())) {
      geom->ClearAnyCachedPath();
      SVGUtils::ScheduleReflowSVG(this);
    }
  }

  if ((aFlags & TRANSFORM_CHANGED) && StyleSVGReset()->HasNonScalingStroke()) {
    // Stroke currently contributes to our mRect, and our stroke depends on
    // the transform to our outer-<svg> if |vector-effect:non-scaling-stroke|.
    SVGUtils::ScheduleReflowSVG(this);
  }
}

SVGBBox SVGGeometryFrame::GetBBoxContribution(const Matrix& aToBBoxUserspace,
                                              uint32_t aFlags) {
  SVGBBox bbox;

  if (aToBBoxUserspace.IsSingular()) {
    // XXX ReportToConsole
    return bbox;
  }

  if ((aFlags & SVGUtils::eForGetClientRects) &&
      aToBBoxUserspace.PreservesAxisAlignedRectangles()) {
    Rect rect = NSRectToRect(mRect, AppUnitsPerCSSPixel());
    bbox = aToBBoxUserspace.TransformBounds(rect);
    return bbox;
  }

  SVGGeometryElement* element = static_cast<SVGGeometryElement*>(GetContent());

  const bool getFill = (aFlags & SVGUtils::eBBoxIncludeFillGeometry) ||
                       ((aFlags & SVGUtils::eBBoxIncludeFill) &&
                        !StyleSVG()->mFill.kind.IsNone());

  const bool getStroke =
      ((aFlags & SVGUtils::eBBoxIncludeStrokeGeometry) ||
       ((aFlags & SVGUtils::eBBoxIncludeStroke) &&
        SVGUtils::HasStroke(this))) &&
      // If this frame has non-scaling-stroke and we would like to compute its
      // stroke, it may cause a potential cyclical dependency if the caller is
      // for transform. In this case, we have to fall back to fill-box, so make
      // |getStroke| be false.
      // https://github.com/w3c/csswg-drafts/issues/9640
      //
      // Note:
      // 1. We don't care about the computation of the markers below in this
      //    function because we know the callers don't set
      //    SVGUtils::eBBoxIncludeMarkers.
      //    See nsStyleTransformMatrix::GetSVGBox() and
      //    MotionPathUtils::GetRayContainReferenceSize() for more details.
      // 2. We have to break the dependency here *again* because the geometry
      //    frame may be in the subtree of a SVGContainerFrame, which may not
      //    set non-scaling-stroke.
      !(StyleSVGReset()->HasNonScalingStroke() &&
        (aFlags & SVGUtils::eAvoidCycleIfNonScalingStroke));

  SVGContentUtils::AutoStrokeOptions strokeOptions;
  if (getStroke) {
    SVGContentUtils::GetStrokeOptions(&strokeOptions, element, Style(), nullptr,
                                      SVGContentUtils::eIgnoreStrokeDashing);
  } else {
    // Override the default line width of 1.f so that when we call
    // GetGeometryBounds below the result doesn't include stroke bounds.
    strokeOptions.mLineWidth = 0.f;
  }

  Rect simpleBounds;
  bool gotSimpleBounds = false;
  gfxMatrix userToOuterSVG;
  if (getStroke &&
      SVGUtils::GetNonScalingStrokeTransform(this, &userToOuterSVG)) {
    Matrix moz2dUserToOuterSVG = ToMatrix(userToOuterSVG);
    if (moz2dUserToOuterSVG.IsSingular()) {
      return bbox;
    }
    gotSimpleBounds = element->GetGeometryBounds(
        &simpleBounds, strokeOptions, aToBBoxUserspace, &moz2dUserToOuterSVG);
  } else {
    gotSimpleBounds = element->GetGeometryBounds(&simpleBounds, strokeOptions,
                                                 aToBBoxUserspace);
  }

  if (gotSimpleBounds) {
    bbox = simpleBounds;
  } else {
    // Get the bounds using a Moz2D Path object (more expensive):
    RefPtr<DrawTarget> tmpDT;
    tmpDT = gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget();

    FillRule fillRule = SVGUtils::ToFillRule(
        HasAnyStateBits(NS_STATE_SVG_CLIPPATH_CHILD) ? StyleSVG()->mClipRule
                                                     : StyleSVG()->mFillRule);
    RefPtr<Path> pathInUserSpace = element->GetOrBuildPath(tmpDT, fillRule);
    if (!pathInUserSpace) {
      return bbox;
    }
    RefPtr<Path> pathInBBoxSpace;
    if (aToBBoxUserspace.IsIdentity()) {
      pathInBBoxSpace = pathInUserSpace;
    } else {
      RefPtr<PathBuilder> builder =
          pathInUserSpace->TransformedCopyToBuilder(aToBBoxUserspace, fillRule);
      pathInBBoxSpace = builder->Finish();
      if (!pathInBBoxSpace) {
        return bbox;
      }
    }

    // Be careful when replacing the following logic to get the fill and stroke
    // extents independently (instead of computing the stroke extents from the
    // path extents). You may think that you can just use the stroke extents if
    // there is both a fill and a stroke. In reality it's necessary to
    // calculate both the fill and stroke extents, and take the union of the
    // two. There are two reasons for this:
    //
    // # Due to stroke dashing, in certain cases the fill extents could
    //   actually extend outside the stroke extents.
    // # If the stroke is very thin, cairo won't paint any stroke, and so the
    //   stroke bounds that it will return will be empty.

    Rect pathBBoxExtents = pathInBBoxSpace->GetBounds();
    if (!pathBBoxExtents.IsFinite()) {
      // This can happen in the case that we only have a move-to command in the
      // path commands, in which case we know nothing gets rendered.
      return bbox;
    }

    // Account for fill:
    if (getFill) {
      bbox = pathBBoxExtents;
    }

    // Account for stroke:
    if (getStroke) {
#if 0
      // This disabled code is how we would calculate the stroke bounds using
      // Moz2D Path::GetStrokedBounds(). Unfortunately at the time of writing
      // it there are two problems that prevent us from using it.
      //
      // First, it seems that some of the Moz2D backends are really dumb. Not
      // only do some GetStrokeOptions() implementations sometimes
      // significantly overestimate the stroke bounds, but if an argument is
      // passed for the aTransform parameter then they just return bounds-of-
      // transformed-bounds.  These two things combined can lead the bounds to
      // be unacceptably oversized, leading to massive over-invalidation.
      //
      // Second, the way we account for non-scaling-stroke by transforming the
      // path using the transform to the outer-<svg> element is not compatible
      // with the way that SVGGeometryFrame::Reflow() inserts a scale
      // into aToBBoxUserspace and then scales the bounds that we return.
      SVGContentUtils::AutoStrokeOptions strokeOptions;
      SVGContentUtils::GetStrokeOptions(&strokeOptions, element,
                                        Style(), nullptr,
                                        SVGContentUtils::eIgnoreStrokeDashing);
      Rect strokeBBoxExtents;
      gfxMatrix userToOuterSVG;
      if (SVGUtils::GetNonScalingStrokeTransform(this, &userToOuterSVG)) {
        Matrix outerSVGToUser = ToMatrix(userToOuterSVG);
        outerSVGToUser.Invert();
        Matrix outerSVGToBBox = aToBBoxUserspace * outerSVGToUser;
        RefPtr<PathBuilder> builder =
          pathInUserSpace->TransformedCopyToBuilder(ToMatrix(userToOuterSVG));
        RefPtr<Path> pathInOuterSVGSpace = builder->Finish();
        strokeBBoxExtents =
          pathInOuterSVGSpace->GetStrokedBounds(strokeOptions, outerSVGToBBox);
      } else {
        strokeBBoxExtents =
          pathInUserSpace->GetStrokedBounds(strokeOptions, aToBBoxUserspace);
      }
      MOZ_ASSERT(strokeBBoxExtents.IsFinite(), "bbox is about to go bad");
      bbox.UnionEdges(strokeBBoxExtents);
#else
      // For now we just use SVGUtils::PathExtentsToMaxStrokeExtents:
      gfxRect strokeBBoxExtents = SVGUtils::PathExtentsToMaxStrokeExtents(
          ThebesRect(pathBBoxExtents), this, ThebesMatrix(aToBBoxUserspace));
      MOZ_ASSERT(ToRect(strokeBBoxExtents).IsFinite(),
                 "bbox is about to go bad");
      bbox.UnionEdges(strokeBBoxExtents);
#endif
    }
  }

  // Account for markers:
  if ((aFlags & SVGUtils::eBBoxIncludeMarkers) && element->IsMarkable()) {
    SVGMarkerFrame* markerFrames[SVGMark::eTypeCount];
    if (SVGObserverUtils::GetAndObserveMarkers(this, &markerFrames)) {
      nsTArray<SVGMark> marks;
      element->GetMarkPoints(&marks);
      if (uint32_t num = marks.Length()) {
        float strokeWidth = SVGUtils::GetStrokeWidth(this);
        for (uint32_t i = 0; i < num; i++) {
          const SVGMark& mark = marks[i];
          SVGMarkerFrame* frame = markerFrames[mark.type];
          if (frame) {
            SVGBBox mbbox = frame->GetMarkBBoxContribution(
                aToBBoxUserspace, aFlags, this, mark, strokeWidth);
            MOZ_ASSERT(mbbox.IsFinite(), "bbox is about to go bad");
            bbox.UnionEdges(mbbox);
          }
        }
      }
    }
  }

  return bbox;
}

//----------------------------------------------------------------------
// SVGGeometryFrame methods:

gfxMatrix SVGGeometryFrame::GetCanvasTM() {
  NS_ASSERTION(GetParent(), "null parent");

  auto* parent = static_cast<SVGContainerFrame*>(GetParent());
  auto* content = static_cast<SVGGraphicsElement*>(GetContent());

  return content->PrependLocalTransformsTo(parent->GetCanvasTM());
}

void SVGGeometryFrame::Render(gfxContext* aContext, uint32_t aRenderComponents,
                              const gfxMatrix& aTransform,
                              imgDrawingParams& aImgParams) {
  MOZ_ASSERT(!aTransform.IsSingular());

  DrawTarget* drawTarget = aContext->GetDrawTarget();

  MOZ_ASSERT(drawTarget);
  if (!drawTarget->IsValid()) {
    return;
  }

  FillRule fillRule = SVGUtils::ToFillRule(
      HasAnyStateBits(NS_STATE_SVG_CLIPPATH_CHILD) ? StyleSVG()->mClipRule
                                                   : StyleSVG()->mFillRule);

  SVGGeometryElement* element = static_cast<SVGGeometryElement*>(GetContent());

  AntialiasMode aaMode =
      (StyleSVG()->mShapeRendering == StyleShapeRendering::Optimizespeed ||
       StyleSVG()->mShapeRendering == StyleShapeRendering::Crispedges)
          ? AntialiasMode::NONE
          : AntialiasMode::SUBPIXEL;

  // We wait as late as possible before setting the transform so that we don't
  // set it unnecessarily if we return early (it's an expensive operation for
  // some backends).
  gfxContextMatrixAutoSaveRestore autoRestoreTransform(aContext);
  aContext->SetMatrixDouble(aTransform);

  if (HasAnyStateBits(NS_STATE_SVG_CLIPPATH_CHILD)) {
    // We don't complicate this code with GetAsSimplePath since the cost of
    // masking will dwarf Path creation overhead anyway.
    RefPtr<Path> path = element->GetOrBuildPath(drawTarget, fillRule);
    if (path) {
      ColorPattern white(ToDeviceColor(sRGBColor(1.0f, 1.0f, 1.0f, 1.0f)));
      drawTarget->Fill(path, white,
                       DrawOptions(1.0f, CompositionOp::OP_OVER, aaMode));
    }
    return;
  }

  SVGGeometryElement::SimplePath simplePath;
  RefPtr<Path> path;

  element->GetAsSimplePath(&simplePath);
  if (!simplePath.IsPath()) {
    path = element->GetOrBuildPath(drawTarget, fillRule);
    if (!path) {
      return;
    }
  }

  SVGContextPaint* contextPaint =
      SVGContextPaint::GetContextPaint(GetContent());

  if (aRenderComponents & eRenderFill) {
    GeneralPattern fillPattern;
    SVGUtils::MakeFillPatternFor(this, aContext, &fillPattern, aImgParams,
                                 contextPaint);

    if (fillPattern.GetPattern()) {
      DrawOptions drawOptions(1.0f, CompositionOp::OP_OVER, aaMode);
      if (simplePath.IsRect()) {
        drawTarget->FillRect(simplePath.AsRect(), fillPattern, drawOptions);
      } else if (path) {
        drawTarget->Fill(path, fillPattern, drawOptions);
      }
    }
  }

  if ((aRenderComponents & eRenderStroke) &&
      SVGUtils::HasStroke(this, contextPaint)) {
    // Account for vector-effect:non-scaling-stroke:
    gfxMatrix userToOuterSVG;
    if (SVGUtils::GetNonScalingStrokeTransform(this, &userToOuterSVG)) {
      // A simple Rect can't be transformed with rotate/skew, so let's switch
      // to using a real path:
      if (!path) {
        path = element->GetOrBuildPath(drawTarget, fillRule);
        if (!path) {
          return;
        }
        simplePath.Reset();
      }
      // We need to transform the path back into the appropriate ancestor
      // coordinate system, and paint it it that coordinate system, in order
      // for non-scaled stroke to paint correctly.
      gfxMatrix outerSVGToUser = userToOuterSVG;
      outerSVGToUser.Invert();
      aContext->Multiply(outerSVGToUser);
      RefPtr<PathBuilder> builder =
          path->TransformedCopyToBuilder(ToMatrix(userToOuterSVG), fillRule);
      path = builder->Finish();
    }
    GeneralPattern strokePattern;
    SVGUtils::MakeStrokePatternFor(this, aContext, &strokePattern, aImgParams,
                                   contextPaint);

    if (strokePattern.GetPattern()) {
      SVGContentUtils::AutoStrokeOptions strokeOptions;
      SVGContentUtils::GetStrokeOptions(&strokeOptions,
                                        static_cast<SVGElement*>(GetContent()),
                                        Style(), contextPaint);
      // GetStrokeOptions may set the line width to zero as an optimization
      if (strokeOptions.mLineWidth <= 0) {
        return;
      }
      DrawOptions drawOptions(1.0f, CompositionOp::OP_OVER, aaMode);
      if (simplePath.IsRect()) {
        drawTarget->StrokeRect(simplePath.AsRect(), strokePattern,
                               strokeOptions, drawOptions);
      } else if (simplePath.IsLine()) {
        drawTarget->StrokeLine(simplePath.Point1(), simplePath.Point2(),
                               strokePattern, strokeOptions, drawOptions);
      } else {
        drawTarget->Stroke(path, strokePattern, strokeOptions, drawOptions);
      }
    }
  }
}

bool SVGGeometryFrame::IsInvisible() const {
  if (!StyleVisibility()->IsVisible()) {
    return true;
  }

  // Anything below will round to zero later down the pipeline.
  constexpr float opacity_threshold = 1.0 / 128.0;

  if (StyleEffects()->mOpacity <= opacity_threshold) {
    return true;
  }

  const nsStyleSVG* style = StyleSVG();
  SVGContextPaint* contextPaint =
      SVGContextPaint::GetContextPaint(GetContent());

  if (!style->mFill.kind.IsNone()) {
    float opacity = SVGUtils::GetOpacity(style->mFillOpacity, contextPaint);
    if (opacity > opacity_threshold) {
      return false;
    }
  }

  if (!style->mStroke.kind.IsNone()) {
    float opacity = SVGUtils::GetOpacity(style->mStrokeOpacity, contextPaint);
    if (opacity > opacity_threshold) {
      return false;
    }
  }

  if (style->HasMarker()) {
    return false;
  }

  return true;
}

bool SVGGeometryFrame::CreateWebRenderCommands(
    mozilla::wr::DisplayListBuilder& aBuilder,
    mozilla::wr::IpcResourceUpdateQueue& aResources,
    const mozilla::layers::StackingContextHelper& aSc,
    mozilla::layers::RenderRootStateManager* aManager,
    nsDisplayListBuilder* aDisplayListBuilder, DisplaySVGGeometry* aItem,
    bool aDryRun) {
  if (!StyleVisibility()->IsVisible()) {
    return true;
  }

  SVGGeometryElement* element = static_cast<SVGGeometryElement*>(GetContent());

  SVGGeometryElement::SimplePath simplePath;
  element->GetAsSimplePath(&simplePath);

  if (!simplePath.IsRect()) {
    return false;
  }

  const nsStyleSVG* style = StyleSVG();
  MOZ_ASSERT(style);

  if (!style->mFill.kind.IsColor()) {
    return false;
  }

  switch (style->mFill.kind.tag) {
    case StyleSVGPaintKind::Tag::Color:
      break;
    default:
      return false;
  }

  if (!style->mStroke.kind.IsNone()) {
    return false;
  }

  if (StyleEffects()->HasMixBlendMode()) {
    // FIXME: not implemented
    return false;
  }

  if (style->HasMarker() && element->IsMarkable()) {
    // Markers aren't suppported yet.
    return false;
  }

  if (!aDryRun) {
    auto appUnitsPerDevPx = PresContext()->AppUnitsPerDevPixel();
    float scale = (float)AppUnitsPerCSSPixel() / (float)appUnitsPerDevPx;

    auto rect = simplePath.AsRect();
    rect.Scale(scale);

    auto offset = LayoutDevicePoint::FromAppUnits(
        aItem->ToReferenceFrame() - GetPosition(), appUnitsPerDevPx);
    rect.MoveBy(offset.x, offset.y);

    auto wrRect = wr::ToLayoutRect(rect);

    SVGContextPaint* contextPaint =
        SVGContextPaint::GetContextPaint(GetContent());
    // At the moment this code path doesn't support strokes so it fine to
    // combine the rectangle's opacity (which has to be applied on the result)
    // of (filling + stroking) with the fill opacity.

    float elemOpacity = 1.0f;
    if (SVGUtils::CanOptimizeOpacity(this)) {
      elemOpacity = StyleEffects()->mOpacity;
    }

    float fillOpacity = SVGUtils::GetOpacity(style->mFillOpacity, contextPaint);
    float opacity = elemOpacity * fillOpacity;

    auto c = nsLayoutUtils::GetColor(this, &nsStyleSVG::mFill);
    wr::ColorF color{
        ((float)NS_GET_R(c)) / 255.0f, ((float)NS_GET_G(c)) / 255.0f,
        ((float)NS_GET_B(c)) / 255.0f, ((float)NS_GET_A(c)) / 255.0f * opacity};

    aBuilder.PushRect(wrRect, wrRect, !aItem->BackfaceIsHidden(), true, false,
                      color);
  }

  return true;
}

void SVGGeometryFrame::PaintMarkers(gfxContext& aContext,
                                    const gfxMatrix& aTransform,
                                    imgDrawingParams& aImgParams) {
  auto* element = static_cast<SVGGeometryElement*>(GetContent());
  if (!element->IsMarkable()) {
    return;
  }
  SVGMarkerFrame* markerFrames[SVGMark::eTypeCount];
  if (!SVGObserverUtils::GetAndObserveMarkers(this, &markerFrames)) {
    return;
  }
  nsTArray<SVGMark> marks;
  element->GetMarkPoints(&marks);
  if (marks.IsEmpty()) {
    return;
  }
  float strokeWidth = GetStrokeWidthForMarkers();
  for (const SVGMark& mark : marks) {
    if (auto* frame = markerFrames[mark.type]) {
      frame->PaintMark(aContext, aTransform, this, mark, strokeWidth,
                       aImgParams);
    }
  }
}

float SVGGeometryFrame::GetStrokeWidthForMarkers() {
  float strokeWidth = SVGUtils::GetStrokeWidth(
      this, SVGContextPaint::GetContextPaint(GetContent()));
  gfxMatrix userToOuterSVG;
  if (SVGUtils::GetNonScalingStrokeTransform(this, &userToOuterSVG)) {
    // We're not interested in any translation here so we can treat this as
    // Singular Value Decomposition (SVD) of a 2 x 2 matrix. That would give us
    // sx and sy values as the X and Y scales. The value we want is the XY
    // scale i.e. the normalised hypotenuse, which is sqrt(sx^2 + sy^2) /
    // sqrt(2). If we use the formulae from
    // https://scicomp.stackexchange.com/a/14103, we discover that the
    // normalised hypotenuse is simply the square root of the sum of the squares
    // of all the 2D matrix elements divided by sqrt(2).
    //
    // Note that this may need adjusting to support 3D transforms properly.

    strokeWidth /= float(sqrt(userToOuterSVG._11 * userToOuterSVG._11 +
                              userToOuterSVG._12 * userToOuterSVG._12 +
                              userToOuterSVG._21 * userToOuterSVG._21 +
                              userToOuterSVG._22 * userToOuterSVG._22) /
                         M_SQRT2);
  }
  return strokeWidth;
}

}  // namespace mozilla