layout/base/nsCSSRendering.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/base/nsCSSRendering.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,5012 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +// vim:cindent:ts=2:et:sw=2:
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/* utility functions for drawing borders and backgrounds */
    1.11 +
    1.12 +#include <ctime>
    1.13 +
    1.14 +#include "mozilla/DebugOnly.h"
    1.15 +#include "mozilla/HashFunctions.h"
    1.16 +#include "mozilla/MathAlgorithms.h"
    1.17 +
    1.18 +#include "nsStyleConsts.h"
    1.19 +#include "nsPresContext.h"
    1.20 +#include "nsIFrame.h"
    1.21 +#include "nsPoint.h"
    1.22 +#include "nsRect.h"
    1.23 +#include "nsIPresShell.h"
    1.24 +#include "nsFrameManager.h"
    1.25 +#include "nsStyleContext.h"
    1.26 +#include "nsGkAtoms.h"
    1.27 +#include "nsCSSAnonBoxes.h"
    1.28 +#include "nsIContent.h"
    1.29 +#include "nsIDocumentInlines.h"
    1.30 +#include "nsIScrollableFrame.h"
    1.31 +#include "imgIRequest.h"
    1.32 +#include "imgIContainer.h"
    1.33 +#include "ImageOps.h"
    1.34 +#include "nsCSSRendering.h"
    1.35 +#include "nsCSSColorUtils.h"
    1.36 +#include "nsITheme.h"
    1.37 +#include "nsLayoutUtils.h"
    1.38 +#include "nsBlockFrame.h"
    1.39 +#include "gfxContext.h"
    1.40 +#include "nsRenderingContext.h"
    1.41 +#include "nsStyleStructInlines.h"
    1.42 +#include "nsCSSFrameConstructor.h"
    1.43 +#include "nsCSSProps.h"
    1.44 +#include "nsContentUtils.h"
    1.45 +#include "nsSVGEffects.h"
    1.46 +#include "nsSVGIntegrationUtils.h"
    1.47 +#include "gfxDrawable.h"
    1.48 +#include "GeckoProfiler.h"
    1.49 +#include "nsCSSRenderingBorders.h"
    1.50 +#include "mozilla/css/ImageLoader.h"
    1.51 +#include "ImageContainer.h"
    1.52 +#include "mozilla/Telemetry.h"
    1.53 +#include "gfxUtils.h"
    1.54 +#include "gfxColor.h"
    1.55 +#include "gfxGradientCache.h"
    1.56 +#include "GraphicsFilter.h"
    1.57 +#include <algorithm>
    1.58 +
    1.59 +using namespace mozilla;
    1.60 +using namespace mozilla::css;
    1.61 +using namespace mozilla::gfx;
    1.62 +using mozilla::image::ImageOps;
    1.63 +using mozilla::CSSSizeOrRatio;
    1.64 +
    1.65 +static int gFrameTreeLockCount = 0;
    1.66 +
    1.67 +// To avoid storing this data on nsInlineFrame (bloat) and to avoid
    1.68 +// recalculating this for each frame in a continuation (perf), hold
    1.69 +// a cache of various coordinate information that we need in order
    1.70 +// to paint inline backgrounds.
    1.71 +struct InlineBackgroundData
    1.72 +{
    1.73 +  InlineBackgroundData()
    1.74 +      : mFrame(nullptr), mBlockFrame(nullptr)
    1.75 +  {
    1.76 +  }
    1.77 +
    1.78 +  ~InlineBackgroundData()
    1.79 +  {
    1.80 +  }
    1.81 +
    1.82 +  void Reset()
    1.83 +  {
    1.84 +    mBoundingBox.SetRect(0,0,0,0);
    1.85 +    mContinuationPoint = mLineContinuationPoint = mUnbrokenWidth = 0;
    1.86 +    mFrame = mBlockFrame = nullptr;
    1.87 +  }
    1.88 +
    1.89 +  nsRect GetContinuousRect(nsIFrame* aFrame)
    1.90 +  {
    1.91 +    SetFrame(aFrame);
    1.92 +
    1.93 +    nscoord x;
    1.94 +    if (mBidiEnabled) {
    1.95 +      x = mLineContinuationPoint;
    1.96 +
    1.97 +      // Scan continuations on the same line as aFrame and accumulate the widths
    1.98 +      // of frames that are to the left (if this is an LTR block) or right
    1.99 +      // (if it's RTL) of the current one.
   1.100 +      bool isRtlBlock = (mBlockFrame->StyleVisibility()->mDirection ==
   1.101 +                           NS_STYLE_DIRECTION_RTL);
   1.102 +      nscoord curOffset = aFrame->GetOffsetTo(mBlockFrame).x;
   1.103 +
   1.104 +      // No need to use our GetPrevContinuation/GetNextContinuation methods
   1.105 +      // here, since ib-split siblings are certainly not on the same line.
   1.106 +
   1.107 +      nsIFrame* inlineFrame = aFrame->GetPrevContinuation();
   1.108 +      // If the continuation is fluid we know inlineFrame is not on the same line.
   1.109 +      // If it's not fluid, we need to test further to be sure.
   1.110 +      while (inlineFrame && !inlineFrame->GetNextInFlow() &&
   1.111 +             AreOnSameLine(aFrame, inlineFrame)) {
   1.112 +        nscoord frameXOffset = inlineFrame->GetOffsetTo(mBlockFrame).x;
   1.113 +        if(isRtlBlock == (frameXOffset >= curOffset)) {
   1.114 +          x += inlineFrame->GetSize().width;
   1.115 +        }
   1.116 +        inlineFrame = inlineFrame->GetPrevContinuation();
   1.117 +      }
   1.118 +
   1.119 +      inlineFrame = aFrame->GetNextContinuation();
   1.120 +      while (inlineFrame && !inlineFrame->GetPrevInFlow() &&
   1.121 +             AreOnSameLine(aFrame, inlineFrame)) {
   1.122 +        nscoord frameXOffset = inlineFrame->GetOffsetTo(mBlockFrame).x;
   1.123 +        if(isRtlBlock == (frameXOffset >= curOffset)) {
   1.124 +          x += inlineFrame->GetSize().width;
   1.125 +        }
   1.126 +        inlineFrame = inlineFrame->GetNextContinuation();
   1.127 +      }
   1.128 +      if (isRtlBlock) {
   1.129 +        // aFrame itself is also to the right of its left edge, so add its width.
   1.130 +        x += aFrame->GetSize().width;
   1.131 +        // x is now the distance from the left edge of aFrame to the right edge
   1.132 +        // of the unbroken content. Change it to indicate the distance from the
   1.133 +        // left edge of the unbroken content to the left edge of aFrame.
   1.134 +        x = mUnbrokenWidth - x;
   1.135 +      }
   1.136 +    } else {
   1.137 +      x = mContinuationPoint;
   1.138 +    }
   1.139 +
   1.140 +    // Assume background-origin: border and return a rect with offsets
   1.141 +    // relative to (0,0).  If we have a different background-origin,
   1.142 +    // then our rect should be deflated appropriately by our caller.
   1.143 +    return nsRect(-x, 0, mUnbrokenWidth, mFrame->GetSize().height);
   1.144 +  }
   1.145 +
   1.146 +  nsRect GetBoundingRect(nsIFrame* aFrame)
   1.147 +  {
   1.148 +    SetFrame(aFrame);
   1.149 +
   1.150 +    // Move the offsets relative to (0,0) which puts the bounding box into
   1.151 +    // our coordinate system rather than our parent's.  We do this by
   1.152 +    // moving it the back distance from us to the bounding box.
   1.153 +    // This also assumes background-origin: border, so our caller will
   1.154 +    // need to deflate us if needed.
   1.155 +    nsRect boundingBox(mBoundingBox);
   1.156 +    nsPoint point = mFrame->GetPosition();
   1.157 +    boundingBox.MoveBy(-point.x, -point.y);
   1.158 +
   1.159 +    return boundingBox;
   1.160 +  }
   1.161 +
   1.162 +protected:
   1.163 +  nsIFrame*     mFrame;
   1.164 +  nsBlockFrame* mBlockFrame;
   1.165 +  nsRect        mBoundingBox;
   1.166 +  nscoord       mContinuationPoint;
   1.167 +  nscoord       mUnbrokenWidth;
   1.168 +  nscoord       mLineContinuationPoint;
   1.169 +  bool          mBidiEnabled;
   1.170 +
   1.171 +  void SetFrame(nsIFrame* aFrame)
   1.172 +  {
   1.173 +    NS_PRECONDITION(aFrame, "Need a frame");
   1.174 +    NS_ASSERTION(gFrameTreeLockCount > 0,
   1.175 +                 "Can't call this when frame tree is not locked");
   1.176 +
   1.177 +    if (aFrame == mFrame) {
   1.178 +      return;
   1.179 +    }
   1.180 +
   1.181 +    nsIFrame *prevContinuation = GetPrevContinuation(aFrame);
   1.182 +
   1.183 +    if (!prevContinuation || mFrame != prevContinuation) {
   1.184 +      // Ok, we've got the wrong frame.  We have to start from scratch.
   1.185 +      Reset();
   1.186 +      Init(aFrame);
   1.187 +      return;
   1.188 +    }
   1.189 +
   1.190 +    // Get our last frame's size and add its width to our continuation
   1.191 +    // point before we cache the new frame.
   1.192 +    mContinuationPoint += mFrame->GetSize().width;
   1.193 +
   1.194 +    // If this a new line, update mLineContinuationPoint.
   1.195 +    if (mBidiEnabled &&
   1.196 +        (aFrame->GetPrevInFlow() || !AreOnSameLine(mFrame, aFrame))) {
   1.197 +       mLineContinuationPoint = mContinuationPoint;
   1.198 +    }
   1.199 +
   1.200 +    mFrame = aFrame;
   1.201 +  }
   1.202 +
   1.203 +  nsIFrame* GetPrevContinuation(nsIFrame* aFrame)
   1.204 +  {
   1.205 +    nsIFrame* prevCont = aFrame->GetPrevContinuation();
   1.206 +    if (!prevCont &&
   1.207 +        (aFrame->GetStateBits() & NS_FRAME_PART_OF_IBSPLIT)) {
   1.208 +      nsIFrame* block = static_cast<nsIFrame*>
   1.209 +        (aFrame->Properties().Get(nsIFrame::IBSplitPrevSibling()));
   1.210 +      if (block) {
   1.211 +        // The {ib} properties are only stored on first continuations
   1.212 +        NS_ASSERTION(!block->GetPrevContinuation(),
   1.213 +                     "Incorrect value for IBSplitPrevSibling");
   1.214 +        prevCont = static_cast<nsIFrame*>
   1.215 +          (block->Properties().Get(nsIFrame::IBSplitPrevSibling()));
   1.216 +        NS_ASSERTION(prevCont, "How did that happen?");
   1.217 +      }
   1.218 +    }
   1.219 +    return prevCont;
   1.220 +  }
   1.221 +
   1.222 +  nsIFrame* GetNextContinuation(nsIFrame* aFrame)
   1.223 +  {
   1.224 +    nsIFrame* nextCont = aFrame->GetNextContinuation();
   1.225 +    if (!nextCont &&
   1.226 +        (aFrame->GetStateBits() & NS_FRAME_PART_OF_IBSPLIT)) {
   1.227 +      // The {ib} properties are only stored on first continuations
   1.228 +      aFrame = aFrame->FirstContinuation();
   1.229 +      nsIFrame* block = static_cast<nsIFrame*>
   1.230 +        (aFrame->Properties().Get(nsIFrame::IBSplitSibling()));
   1.231 +      if (block) {
   1.232 +        nextCont = static_cast<nsIFrame*>
   1.233 +          (block->Properties().Get(nsIFrame::IBSplitSibling()));
   1.234 +        NS_ASSERTION(nextCont, "How did that happen?");
   1.235 +      }
   1.236 +    }
   1.237 +    return nextCont;
   1.238 +  }
   1.239 +
   1.240 +  void Init(nsIFrame* aFrame)
   1.241 +  {
   1.242 +    mBidiEnabled = aFrame->PresContext()->BidiEnabled();
   1.243 +    if (mBidiEnabled) {
   1.244 +      // Find the containing block frame
   1.245 +      nsIFrame* frame = aFrame;
   1.246 +      do {
   1.247 +        frame = frame->GetParent();
   1.248 +        mBlockFrame = do_QueryFrame(frame);
   1.249 +      }
   1.250 +      while (frame && frame->IsFrameOfType(nsIFrame::eLineParticipant));
   1.251 +
   1.252 +      NS_ASSERTION(mBlockFrame, "Cannot find containing block.");
   1.253 +    }
   1.254 +
   1.255 +    // Start with the previous flow frame as our continuation point
   1.256 +    // is the total of the widths of the previous frames.
   1.257 +    nsIFrame* inlineFrame = GetPrevContinuation(aFrame);
   1.258 +
   1.259 +    while (inlineFrame) {
   1.260 +      nsRect rect = inlineFrame->GetRect();
   1.261 +      mContinuationPoint += rect.width;
   1.262 +      if (mBidiEnabled && !AreOnSameLine(aFrame, inlineFrame)) {
   1.263 +        mLineContinuationPoint += rect.width;
   1.264 +      }
   1.265 +      mUnbrokenWidth += rect.width;
   1.266 +      mBoundingBox.UnionRect(mBoundingBox, rect);
   1.267 +      inlineFrame = GetPrevContinuation(inlineFrame);
   1.268 +    }
   1.269 +
   1.270 +    // Next add this frame and subsequent frames to the bounding box and
   1.271 +    // unbroken width.
   1.272 +    inlineFrame = aFrame;
   1.273 +    while (inlineFrame) {
   1.274 +      nsRect rect = inlineFrame->GetRect();
   1.275 +      mUnbrokenWidth += rect.width;
   1.276 +      mBoundingBox.UnionRect(mBoundingBox, rect);
   1.277 +      inlineFrame = GetNextContinuation(inlineFrame);
   1.278 +    }
   1.279 +
   1.280 +    mFrame = aFrame;
   1.281 +  }
   1.282 +
   1.283 +  bool AreOnSameLine(nsIFrame* aFrame1, nsIFrame* aFrame2) {
   1.284 +    bool isValid1, isValid2;
   1.285 +    nsBlockInFlowLineIterator it1(mBlockFrame, aFrame1, &isValid1);
   1.286 +    nsBlockInFlowLineIterator it2(mBlockFrame, aFrame2, &isValid2);
   1.287 +    return isValid1 && isValid2 &&
   1.288 +      // Make sure aFrame1 and aFrame2 are in the same continuation of
   1.289 +      // mBlockFrame.
   1.290 +      it1.GetContainer() == it2.GetContainer() &&
   1.291 +      // And on the same line in it
   1.292 +      it1.GetLine() == it2.GetLine();
   1.293 +  }
   1.294 +};
   1.295 +
   1.296 +// A resolved color stop --- with a specific position along the gradient line,
   1.297 +// and a Thebes color
   1.298 +struct ColorStop {
   1.299 +  ColorStop(double aPosition, gfxRGBA aColor) :
   1.300 +    mPosition(aPosition), mColor(aColor) {}
   1.301 +  double mPosition; // along the gradient line; 0=start, 1=end
   1.302 +  gfxRGBA mColor;
   1.303 +};
   1.304 +
   1.305 +/* Local functions */
   1.306 +static void DrawBorderImage(nsPresContext* aPresContext,
   1.307 +                            nsRenderingContext& aRenderingContext,
   1.308 +                            nsIFrame* aForFrame,
   1.309 +                            const nsRect& aBorderArea,
   1.310 +                            const nsStyleBorder& aStyleBorder,
   1.311 +                            const nsRect& aDirtyRect);
   1.312 +
   1.313 +static nscolor MakeBevelColor(mozilla::css::Side whichSide, uint8_t style,
   1.314 +                              nscolor aBackgroundColor,
   1.315 +                              nscolor aBorderColor);
   1.316 +
   1.317 +static InlineBackgroundData* gInlineBGData = nullptr;
   1.318 +
   1.319 +// Initialize any static variables used by nsCSSRendering.
   1.320 +void nsCSSRendering::Init()
   1.321 +{
   1.322 +  NS_ASSERTION(!gInlineBGData, "Init called twice");
   1.323 +  gInlineBGData = new InlineBackgroundData();
   1.324 +}
   1.325 +
   1.326 +// Clean up any global variables used by nsCSSRendering.
   1.327 +void nsCSSRendering::Shutdown()
   1.328 +{
   1.329 +  delete gInlineBGData;
   1.330 +  gInlineBGData = nullptr;
   1.331 +}
   1.332 +
   1.333 +/**
   1.334 + * Make a bevel color
   1.335 + */
   1.336 +static nscolor
   1.337 +MakeBevelColor(mozilla::css::Side whichSide, uint8_t style,
   1.338 +               nscolor aBackgroundColor, nscolor aBorderColor)
   1.339 +{
   1.340 +
   1.341 +  nscolor colors[2];
   1.342 +  nscolor theColor;
   1.343 +
   1.344 +  // Given a background color and a border color
   1.345 +  // calculate the color used for the shading
   1.346 +  NS_GetSpecial3DColors(colors, aBackgroundColor, aBorderColor);
   1.347 +
   1.348 +  if ((style == NS_STYLE_BORDER_STYLE_OUTSET) ||
   1.349 +      (style == NS_STYLE_BORDER_STYLE_RIDGE)) {
   1.350 +    // Flip colors for these two border styles
   1.351 +    switch (whichSide) {
   1.352 +    case NS_SIDE_BOTTOM: whichSide = NS_SIDE_TOP;    break;
   1.353 +    case NS_SIDE_RIGHT:  whichSide = NS_SIDE_LEFT;   break;
   1.354 +    case NS_SIDE_TOP:    whichSide = NS_SIDE_BOTTOM; break;
   1.355 +    case NS_SIDE_LEFT:   whichSide = NS_SIDE_RIGHT;  break;
   1.356 +    }
   1.357 +  }
   1.358 +
   1.359 +  switch (whichSide) {
   1.360 +  case NS_SIDE_BOTTOM:
   1.361 +    theColor = colors[1];
   1.362 +    break;
   1.363 +  case NS_SIDE_RIGHT:
   1.364 +    theColor = colors[1];
   1.365 +    break;
   1.366 +  case NS_SIDE_TOP:
   1.367 +    theColor = colors[0];
   1.368 +    break;
   1.369 +  case NS_SIDE_LEFT:
   1.370 +  default:
   1.371 +    theColor = colors[0];
   1.372 +    break;
   1.373 +  }
   1.374 +  return theColor;
   1.375 +}
   1.376 +
   1.377 +//----------------------------------------------------------------------
   1.378 +// Thebes Border Rendering Code Start
   1.379 +
   1.380 +/*
   1.381 + * Compute the float-pixel radii that should be used for drawing
   1.382 + * this border/outline, given the various input bits.
   1.383 + */
   1.384 +/* static */ void
   1.385 +nsCSSRendering::ComputePixelRadii(const nscoord *aAppUnitsRadii,
   1.386 +                                  nscoord aAppUnitsPerPixel,
   1.387 +                                  gfxCornerSizes *oBorderRadii)
   1.388 +{
   1.389 +  gfxFloat radii[8];
   1.390 +  NS_FOR_CSS_HALF_CORNERS(corner)
   1.391 +    radii[corner] = gfxFloat(aAppUnitsRadii[corner]) / aAppUnitsPerPixel;
   1.392 +
   1.393 +  (*oBorderRadii)[C_TL] = gfxSize(radii[NS_CORNER_TOP_LEFT_X],
   1.394 +                                  radii[NS_CORNER_TOP_LEFT_Y]);
   1.395 +  (*oBorderRadii)[C_TR] = gfxSize(radii[NS_CORNER_TOP_RIGHT_X],
   1.396 +                                  radii[NS_CORNER_TOP_RIGHT_Y]);
   1.397 +  (*oBorderRadii)[C_BR] = gfxSize(radii[NS_CORNER_BOTTOM_RIGHT_X],
   1.398 +                                  radii[NS_CORNER_BOTTOM_RIGHT_Y]);
   1.399 +  (*oBorderRadii)[C_BL] = gfxSize(radii[NS_CORNER_BOTTOM_LEFT_X],
   1.400 +                                  radii[NS_CORNER_BOTTOM_LEFT_Y]);
   1.401 +}
   1.402 +
   1.403 +void
   1.404 +nsCSSRendering::PaintBorder(nsPresContext* aPresContext,
   1.405 +                            nsRenderingContext& aRenderingContext,
   1.406 +                            nsIFrame* aForFrame,
   1.407 +                            const nsRect& aDirtyRect,
   1.408 +                            const nsRect& aBorderArea,
   1.409 +                            nsStyleContext* aStyleContext,
   1.410 +                            int aSkipSides)
   1.411 +{
   1.412 +  PROFILER_LABEL("nsCSSRendering", "PaintBorder");
   1.413 +  nsStyleContext *styleIfVisited = aStyleContext->GetStyleIfVisited();
   1.414 +  const nsStyleBorder *styleBorder = aStyleContext->StyleBorder();
   1.415 +  // Don't check RelevantLinkVisited here, since we want to take the
   1.416 +  // same amount of time whether or not it's true.
   1.417 +  if (!styleIfVisited) {
   1.418 +    PaintBorderWithStyleBorder(aPresContext, aRenderingContext, aForFrame,
   1.419 +                               aDirtyRect, aBorderArea, *styleBorder,
   1.420 +                               aStyleContext, aSkipSides);
   1.421 +    return;
   1.422 +  }
   1.423 +
   1.424 +  nsStyleBorder newStyleBorder(*styleBorder);
   1.425 +  // We could do something fancy to avoid the TrackImage/UntrackImage
   1.426 +  // work, but it doesn't seem worth it.  (We need to call TrackImage
   1.427 +  // since we're not going through nsRuleNode::ComputeBorderData.)
   1.428 +  newStyleBorder.TrackImage(aPresContext);
   1.429 +
   1.430 +  NS_FOR_CSS_SIDES(side) {
   1.431 +    newStyleBorder.SetBorderColor(side,
   1.432 +      aStyleContext->GetVisitedDependentColor(
   1.433 +        nsCSSProps::SubpropertyEntryFor(eCSSProperty_border_color)[side]));
   1.434 +  }
   1.435 +  PaintBorderWithStyleBorder(aPresContext, aRenderingContext, aForFrame,
   1.436 +                             aDirtyRect, aBorderArea, newStyleBorder,
   1.437 +                             aStyleContext, aSkipSides);
   1.438 +
   1.439 +  // We could do something fancy to avoid the TrackImage/UntrackImage
   1.440 +  // work, but it doesn't seem worth it.  (We need to call UntrackImage
   1.441 +  // since we're not going through nsStyleBorder::Destroy.)
   1.442 +  newStyleBorder.UntrackImage(aPresContext);
   1.443 +}
   1.444 +
   1.445 +void
   1.446 +nsCSSRendering::PaintBorderWithStyleBorder(nsPresContext* aPresContext,
   1.447 +                                           nsRenderingContext& aRenderingContext,
   1.448 +                                           nsIFrame* aForFrame,
   1.449 +                                           const nsRect& aDirtyRect,
   1.450 +                                           const nsRect& aBorderArea,
   1.451 +                                           const nsStyleBorder& aStyleBorder,
   1.452 +                                           nsStyleContext* aStyleContext,
   1.453 +                                           int aSkipSides)
   1.454 +{
   1.455 +  nsMargin            border;
   1.456 +  nscoord             twipsRadii[8];
   1.457 +  nsCompatibility     compatMode = aPresContext->CompatibilityMode();
   1.458 +
   1.459 +  SN("++ PaintBorder");
   1.460 +
   1.461 +  // Check to see if we have an appearance defined.  If so, we let the theme
   1.462 +  // renderer draw the border.  DO not get the data from aForFrame, since the passed in style context
   1.463 +  // may be different!  Always use |aStyleContext|!
   1.464 +  const nsStyleDisplay* displayData = aStyleContext->StyleDisplay();
   1.465 +  if (displayData->mAppearance) {
   1.466 +    nsITheme *theme = aPresContext->GetTheme();
   1.467 +    if (theme && theme->ThemeSupportsWidget(aPresContext, aForFrame, displayData->mAppearance))
   1.468 +      return; // Let the theme handle it.
   1.469 +  }
   1.470 +
   1.471 +  if (aStyleBorder.IsBorderImageLoaded()) {
   1.472 +    DrawBorderImage(aPresContext, aRenderingContext, aForFrame,
   1.473 +                    aBorderArea, aStyleBorder, aDirtyRect);
   1.474 +    return;
   1.475 +  }
   1.476 +
   1.477 +  // Get our style context's color struct.
   1.478 +  const nsStyleColor* ourColor = aStyleContext->StyleColor();
   1.479 +
   1.480 +  // in NavQuirks mode we want to use the parent's context as a starting point
   1.481 +  // for determining the background color
   1.482 +  nsIFrame* bgFrame = nsCSSRendering::FindNonTransparentBackgroundFrame
   1.483 +    (aForFrame, compatMode == eCompatibility_NavQuirks ? true : false);
   1.484 +  nsStyleContext* bgContext = bgFrame->StyleContext();
   1.485 +  nscolor bgColor =
   1.486 +    bgContext->GetVisitedDependentColor(eCSSProperty_background_color);
   1.487 +
   1.488 +  border = aStyleBorder.GetComputedBorder();
   1.489 +  if ((0 == border.left) && (0 == border.right) &&
   1.490 +      (0 == border.top) && (0 == border.bottom)) {
   1.491 +    // Empty border area
   1.492 +    return;
   1.493 +  }
   1.494 +
   1.495 +  nsSize frameSize = aForFrame->GetSize();
   1.496 +  if (&aStyleBorder == aForFrame->StyleBorder() &&
   1.497 +      frameSize == aBorderArea.Size()) {
   1.498 +    aForFrame->GetBorderRadii(twipsRadii);
   1.499 +  } else {
   1.500 +    nsIFrame::ComputeBorderRadii(aStyleBorder.mBorderRadius, frameSize,
   1.501 +                                 aBorderArea.Size(), aSkipSides, twipsRadii);
   1.502 +  }
   1.503 +
   1.504 +  // Turn off rendering for all of the zero sized sides
   1.505 +  if (aSkipSides & SIDE_BIT_TOP) border.top = 0;
   1.506 +  if (aSkipSides & SIDE_BIT_RIGHT) border.right = 0;
   1.507 +  if (aSkipSides & SIDE_BIT_BOTTOM) border.bottom = 0;
   1.508 +  if (aSkipSides & SIDE_BIT_LEFT) border.left = 0;
   1.509 +
   1.510 +  // get the inside and outside parts of the border
   1.511 +  nsRect outerRect(aBorderArea);
   1.512 +
   1.513 +  SF(" outerRect: %d %d %d %d\n", outerRect.x, outerRect.y, outerRect.width, outerRect.height);
   1.514 +
   1.515 +  // we can assume that we're already clipped to aDirtyRect -- I think? (!?)
   1.516 +
   1.517 +  // Get our conversion values
   1.518 +  nscoord twipsPerPixel = aPresContext->DevPixelsToAppUnits(1);
   1.519 +
   1.520 +  // convert outer and inner rects
   1.521 +  gfxRect oRect(nsLayoutUtils::RectToGfxRect(outerRect, twipsPerPixel));
   1.522 +
   1.523 +  // convert the border widths
   1.524 +  gfxFloat borderWidths[4] = { gfxFloat(border.top / twipsPerPixel),
   1.525 +                               gfxFloat(border.right / twipsPerPixel),
   1.526 +                               gfxFloat(border.bottom / twipsPerPixel),
   1.527 +                               gfxFloat(border.left / twipsPerPixel) };
   1.528 +
   1.529 +  // convert the radii
   1.530 +  gfxCornerSizes borderRadii;
   1.531 +  ComputePixelRadii(twipsRadii, twipsPerPixel, &borderRadii);
   1.532 +
   1.533 +  uint8_t borderStyles[4];
   1.534 +  nscolor borderColors[4];
   1.535 +  nsBorderColors *compositeColors[4];
   1.536 +
   1.537 +  // pull out styles, colors, composite colors
   1.538 +  NS_FOR_CSS_SIDES (i) {
   1.539 +    bool foreground;
   1.540 +    borderStyles[i] = aStyleBorder.GetBorderStyle(i);
   1.541 +    aStyleBorder.GetBorderColor(i, borderColors[i], foreground);
   1.542 +    aStyleBorder.GetCompositeColors(i, &compositeColors[i]);
   1.543 +
   1.544 +    if (foreground)
   1.545 +      borderColors[i] = ourColor->mColor;
   1.546 +  }
   1.547 +
   1.548 +  SF(" borderStyles: %d %d %d %d\n", borderStyles[0], borderStyles[1], borderStyles[2], borderStyles[3]);
   1.549 +
   1.550 +  // start drawing
   1.551 +  gfxContext *ctx = aRenderingContext.ThebesContext();
   1.552 +
   1.553 +  ctx->Save();
   1.554 +
   1.555 +#if 0
   1.556 +  // this will draw a transparent red backround underneath the oRect area
   1.557 +  ctx->Save();
   1.558 +  ctx->Rectangle(oRect);
   1.559 +  ctx->SetColor(gfxRGBA(1.0, 0.0, 0.0, 0.5));
   1.560 +  ctx->Fill();
   1.561 +  ctx->Restore();
   1.562 +#endif
   1.563 +
   1.564 +  //SF ("borderRadii: %f %f %f %f\n", borderRadii[0], borderRadii[1], borderRadii[2], borderRadii[3]);
   1.565 +
   1.566 +  nsCSSBorderRenderer br(twipsPerPixel,
   1.567 +                         ctx,
   1.568 +                         oRect,
   1.569 +                         borderStyles,
   1.570 +                         borderWidths,
   1.571 +                         borderRadii,
   1.572 +                         borderColors,
   1.573 +                         compositeColors,
   1.574 +                         aSkipSides,
   1.575 +                         bgColor);
   1.576 +  br.DrawBorders();
   1.577 +
   1.578 +  ctx->Restore();
   1.579 +
   1.580 +  SN();
   1.581 +}
   1.582 +
   1.583 +static nsRect
   1.584 +GetOutlineInnerRect(nsIFrame* aFrame)
   1.585 +{
   1.586 +  nsRect* savedOutlineInnerRect = static_cast<nsRect*>
   1.587 +    (aFrame->Properties().Get(nsIFrame::OutlineInnerRectProperty()));
   1.588 +  if (savedOutlineInnerRect)
   1.589 +    return *savedOutlineInnerRect;
   1.590 +  NS_NOTREACHED("we should have saved a frame property");
   1.591 +  return nsRect(nsPoint(0, 0), aFrame->GetSize());
   1.592 +}
   1.593 +
   1.594 +void
   1.595 +nsCSSRendering::PaintOutline(nsPresContext* aPresContext,
   1.596 +                             nsRenderingContext& aRenderingContext,
   1.597 +                             nsIFrame* aForFrame,
   1.598 +                             const nsRect& aDirtyRect,
   1.599 +                             const nsRect& aBorderArea,
   1.600 +                             nsStyleContext* aStyleContext)
   1.601 +{
   1.602 +  nscoord             twipsRadii[8];
   1.603 +
   1.604 +  // Get our style context's color struct.
   1.605 +  const nsStyleOutline* ourOutline = aStyleContext->StyleOutline();
   1.606 +
   1.607 +  nscoord width;
   1.608 +  ourOutline->GetOutlineWidth(width);
   1.609 +
   1.610 +  if (width == 0) {
   1.611 +    // Empty outline
   1.612 +    return;
   1.613 +  }
   1.614 +
   1.615 +  nsIFrame* bgFrame = nsCSSRendering::FindNonTransparentBackgroundFrame
   1.616 +    (aForFrame, false);
   1.617 +  nsStyleContext* bgContext = bgFrame->StyleContext();
   1.618 +  nscolor bgColor =
   1.619 +    bgContext->GetVisitedDependentColor(eCSSProperty_background_color);
   1.620 +
   1.621 +  nsRect innerRect;
   1.622 +  if (
   1.623 +#ifdef MOZ_XUL
   1.624 +      aStyleContext->GetPseudoType() == nsCSSPseudoElements::ePseudo_XULTree
   1.625 +#else
   1.626 +      false
   1.627 +#endif
   1.628 +     ) {
   1.629 +    innerRect = aBorderArea;
   1.630 +  } else {
   1.631 +    innerRect = GetOutlineInnerRect(aForFrame) + aBorderArea.TopLeft();
   1.632 +  }
   1.633 +  nscoord offset = ourOutline->mOutlineOffset;
   1.634 +  innerRect.Inflate(offset, offset);
   1.635 +  // If the dirty rect is completely inside the border area (e.g., only the
   1.636 +  // content is being painted), then we can skip out now
   1.637 +  // XXX this isn't exactly true for rounded borders, where the inside curves may
   1.638 +  // encroach into the content area.  A safer calculation would be to
   1.639 +  // shorten insideRect by the radius one each side before performing this test.
   1.640 +  if (innerRect.Contains(aDirtyRect))
   1.641 +    return;
   1.642 +
   1.643 +  nsRect outerRect = innerRect;
   1.644 +  outerRect.Inflate(width, width);
   1.645 +
   1.646 +  // get the radius for our outline
   1.647 +  nsIFrame::ComputeBorderRadii(ourOutline->mOutlineRadius, aBorderArea.Size(),
   1.648 +                               outerRect.Size(), 0, twipsRadii);
   1.649 +
   1.650 +  // Get our conversion values
   1.651 +  nscoord twipsPerPixel = aPresContext->DevPixelsToAppUnits(1);
   1.652 +
   1.653 +  // get the outer rectangles
   1.654 +  gfxRect oRect(nsLayoutUtils::RectToGfxRect(outerRect, twipsPerPixel));
   1.655 +
   1.656 +  // convert the radii
   1.657 +  nsMargin outlineMargin(width, width, width, width);
   1.658 +  gfxCornerSizes outlineRadii;
   1.659 +  ComputePixelRadii(twipsRadii, twipsPerPixel, &outlineRadii);
   1.660 +
   1.661 +  uint8_t outlineStyle = ourOutline->GetOutlineStyle();
   1.662 +  uint8_t outlineStyles[4] = { outlineStyle,
   1.663 +                               outlineStyle,
   1.664 +                               outlineStyle,
   1.665 +                               outlineStyle };
   1.666 +
   1.667 +  // This handles treating the initial color as 'currentColor'; if we
   1.668 +  // ever want 'invert' back we'll need to do a bit of work here too.
   1.669 +  nscolor outlineColor =
   1.670 +    aStyleContext->GetVisitedDependentColor(eCSSProperty_outline_color);
   1.671 +  nscolor outlineColors[4] = { outlineColor,
   1.672 +                               outlineColor,
   1.673 +                               outlineColor,
   1.674 +                               outlineColor };
   1.675 +
   1.676 +  // convert the border widths
   1.677 +  gfxFloat outlineWidths[4] = { gfxFloat(width / twipsPerPixel),
   1.678 +                                gfxFloat(width / twipsPerPixel),
   1.679 +                                gfxFloat(width / twipsPerPixel),
   1.680 +                                gfxFloat(width / twipsPerPixel) };
   1.681 +
   1.682 +  // start drawing
   1.683 +  gfxContext *ctx = aRenderingContext.ThebesContext();
   1.684 +
   1.685 +  ctx->Save();
   1.686 +
   1.687 +  nsCSSBorderRenderer br(twipsPerPixel,
   1.688 +                         ctx,
   1.689 +                         oRect,
   1.690 +                         outlineStyles,
   1.691 +                         outlineWidths,
   1.692 +                         outlineRadii,
   1.693 +                         outlineColors,
   1.694 +                         nullptr, 0,
   1.695 +                         bgColor);
   1.696 +  br.DrawBorders();
   1.697 +
   1.698 +  ctx->Restore();
   1.699 +
   1.700 +  SN();
   1.701 +}
   1.702 +
   1.703 +void
   1.704 +nsCSSRendering::PaintFocus(nsPresContext* aPresContext,
   1.705 +                           nsRenderingContext& aRenderingContext,
   1.706 +                           const nsRect& aFocusRect,
   1.707 +                           nscolor aColor)
   1.708 +{
   1.709 +  nscoord oneCSSPixel = nsPresContext::CSSPixelsToAppUnits(1);
   1.710 +  nscoord oneDevPixel = aPresContext->DevPixelsToAppUnits(1);
   1.711 +
   1.712 +  gfxRect focusRect(nsLayoutUtils::RectToGfxRect(aFocusRect, oneDevPixel));
   1.713 +
   1.714 +  gfxCornerSizes focusRadii;
   1.715 +  {
   1.716 +    nscoord twipsRadii[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
   1.717 +    ComputePixelRadii(twipsRadii, oneDevPixel, &focusRadii);
   1.718 +  }
   1.719 +  gfxFloat focusWidths[4] = { gfxFloat(oneCSSPixel / oneDevPixel),
   1.720 +                              gfxFloat(oneCSSPixel / oneDevPixel),
   1.721 +                              gfxFloat(oneCSSPixel / oneDevPixel),
   1.722 +                              gfxFloat(oneCSSPixel / oneDevPixel) };
   1.723 +
   1.724 +  uint8_t focusStyles[4] = { NS_STYLE_BORDER_STYLE_DOTTED,
   1.725 +                             NS_STYLE_BORDER_STYLE_DOTTED,
   1.726 +                             NS_STYLE_BORDER_STYLE_DOTTED,
   1.727 +                             NS_STYLE_BORDER_STYLE_DOTTED };
   1.728 +  nscolor focusColors[4] = { aColor, aColor, aColor, aColor };
   1.729 +
   1.730 +  gfxContext *ctx = aRenderingContext.ThebesContext();
   1.731 +
   1.732 +  ctx->Save();
   1.733 +
   1.734 +  // Because this renders a dotted border, the background color
   1.735 +  // should not be used.  Therefore, we provide a value that will
   1.736 +  // be blatantly wrong if it ever does get used.  (If this becomes
   1.737 +  // something that CSS can style, this function will then have access
   1.738 +  // to a style context and can use the same logic that PaintBorder
   1.739 +  // and PaintOutline do.)
   1.740 +  nsCSSBorderRenderer br(oneDevPixel,
   1.741 +                         ctx,
   1.742 +                         focusRect,
   1.743 +                         focusStyles,
   1.744 +                         focusWidths,
   1.745 +                         focusRadii,
   1.746 +                         focusColors,
   1.747 +                         nullptr, 0,
   1.748 +                         NS_RGB(255, 0, 0));
   1.749 +  br.DrawBorders();
   1.750 +
   1.751 +  ctx->Restore();
   1.752 +
   1.753 +  SN();
   1.754 +}
   1.755 +
   1.756 +// Thebes Border Rendering Code End
   1.757 +//----------------------------------------------------------------------
   1.758 +
   1.759 +
   1.760 +//----------------------------------------------------------------------
   1.761 +
   1.762 +/**
   1.763 + * Computes the placement of a background image.
   1.764 + *
   1.765 + * @param aOriginBounds is the box to which the tiling position should be
   1.766 + * relative
   1.767 + * This should correspond to 'background-origin' for the frame,
   1.768 + * except when painting on the canvas, in which case the origin bounds
   1.769 + * should be the bounds of the root element's frame.
   1.770 + * @param aTopLeft the top-left corner where an image tile should be drawn
   1.771 + * @param aAnchorPoint a point which should be pixel-aligned by
   1.772 + * nsLayoutUtils::DrawImage. This is the same as aTopLeft, unless CSS
   1.773 + * specifies a percentage (including 'right' or 'bottom'), in which case
   1.774 + * it's that percentage within of aOriginBounds. So 'right' would set
   1.775 + * aAnchorPoint.x to aOriginBounds.XMost().
   1.776 + *
   1.777 + * Points are returned relative to aOriginBounds.
   1.778 + */
   1.779 +static void
   1.780 +ComputeBackgroundAnchorPoint(const nsStyleBackground::Layer& aLayer,
   1.781 +                             const nsSize& aOriginBounds,
   1.782 +                             const nsSize& aImageSize,
   1.783 +                             nsPoint* aTopLeft,
   1.784 +                             nsPoint* aAnchorPoint)
   1.785 +{
   1.786 +  double percentX = aLayer.mPosition.mXPosition.mPercent;
   1.787 +  nscoord lengthX = aLayer.mPosition.mXPosition.mLength;
   1.788 +  aAnchorPoint->x = lengthX + NSToCoordRound(percentX*aOriginBounds.width);
   1.789 +  aTopLeft->x = lengthX +
   1.790 +    NSToCoordRound(percentX*(aOriginBounds.width - aImageSize.width));
   1.791 +
   1.792 +  double percentY = aLayer.mPosition.mYPosition.mPercent;
   1.793 +  nscoord lengthY = aLayer.mPosition.mYPosition.mLength;
   1.794 +  aAnchorPoint->y = lengthY + NSToCoordRound(percentY*aOriginBounds.height);
   1.795 +  aTopLeft->y = lengthY +
   1.796 +    NSToCoordRound(percentY*(aOriginBounds.height - aImageSize.height));
   1.797 +}
   1.798 +
   1.799 +nsIFrame*
   1.800 +nsCSSRendering::FindNonTransparentBackgroundFrame(nsIFrame* aFrame,
   1.801 +                                                  bool aStartAtParent /*= false*/)
   1.802 +{
   1.803 +  NS_ASSERTION(aFrame, "Cannot find NonTransparentBackgroundFrame in a null frame");
   1.804 +
   1.805 +  nsIFrame* frame = nullptr;
   1.806 +  if (aStartAtParent) {
   1.807 +    frame = nsLayoutUtils::GetParentOrPlaceholderFor(aFrame);
   1.808 +  }
   1.809 +  if (!frame) {
   1.810 +    frame = aFrame;
   1.811 +  }
   1.812 +
   1.813 +  while (frame) {
   1.814 +    // No need to call GetVisitedDependentColor because it always uses
   1.815 +    // this alpha component anyway.
   1.816 +    if (NS_GET_A(frame->StyleBackground()->mBackgroundColor) > 0)
   1.817 +      break;
   1.818 +
   1.819 +    if (frame->IsThemed())
   1.820 +      break;
   1.821 +
   1.822 +    nsIFrame* parent = nsLayoutUtils::GetParentOrPlaceholderFor(frame);
   1.823 +    if (!parent)
   1.824 +      break;
   1.825 +
   1.826 +    frame = parent;
   1.827 +  }
   1.828 +  return frame;
   1.829 +}
   1.830 +
   1.831 +// Returns true if aFrame is a canvas frame.
   1.832 +// We need to treat the viewport as canvas because, even though
   1.833 +// it does not actually paint a background, we need to get the right
   1.834 +// background style so we correctly detect transparent documents.
   1.835 +bool
   1.836 +nsCSSRendering::IsCanvasFrame(nsIFrame* aFrame)
   1.837 +{
   1.838 +  nsIAtom* frameType = aFrame->GetType();
   1.839 +  return frameType == nsGkAtoms::canvasFrame ||
   1.840 +         frameType == nsGkAtoms::rootFrame ||
   1.841 +         frameType == nsGkAtoms::pageContentFrame ||
   1.842 +         frameType == nsGkAtoms::viewportFrame;
   1.843 +}
   1.844 +
   1.845 +nsIFrame*
   1.846 +nsCSSRendering::FindBackgroundStyleFrame(nsIFrame* aForFrame)
   1.847 +{
   1.848 +  const nsStyleBackground* result = aForFrame->StyleBackground();
   1.849 +
   1.850 +  // Check if we need to do propagation from BODY rather than HTML.
   1.851 +  if (!result->IsTransparent()) {
   1.852 +    return aForFrame;
   1.853 +  }
   1.854 +
   1.855 +  nsIContent* content = aForFrame->GetContent();
   1.856 +  // The root element content can't be null. We wouldn't know what
   1.857 +  // frame to create for aFrame.
   1.858 +  // Use |OwnerDoc| so it works during destruction.
   1.859 +  if (!content) {
   1.860 +    return aForFrame;
   1.861 +  }
   1.862 +
   1.863 +  nsIDocument* document = content->OwnerDoc();
   1.864 +
   1.865 +  dom::Element* bodyContent = document->GetBodyElement();
   1.866 +  // We need to null check the body node (bug 118829) since
   1.867 +  // there are cases, thanks to the fix for bug 5569, where we
   1.868 +  // will reflow a document with no body.  In particular, if a
   1.869 +  // SCRIPT element in the head blocks the parser and then has a
   1.870 +  // SCRIPT that does "document.location.href = 'foo'", then
   1.871 +  // nsParser::Terminate will call |DidBuildModel| methods
   1.872 +  // through to the content sink, which will call |StartLayout|
   1.873 +  // and thus |Initialize| on the pres shell.  See bug 119351
   1.874 +  // for the ugly details.
   1.875 +  if (!bodyContent) {
   1.876 +    return aForFrame;
   1.877 +  }
   1.878 +
   1.879 +  nsIFrame *bodyFrame = bodyContent->GetPrimaryFrame();
   1.880 +  if (!bodyFrame) {
   1.881 +    return aForFrame;
   1.882 +  }
   1.883 +
   1.884 +  return nsLayoutUtils::GetStyleFrame(bodyFrame);
   1.885 +}
   1.886 +
   1.887 +/**
   1.888 + * |FindBackground| finds the correct style data to use to paint the
   1.889 + * background.  It is responsible for handling the following two
   1.890 + * statements in section 14.2 of CSS2:
   1.891 + *
   1.892 + *   The background of the box generated by the root element covers the
   1.893 + *   entire canvas.
   1.894 + *
   1.895 + *   For HTML documents, however, we recommend that authors specify the
   1.896 + *   background for the BODY element rather than the HTML element. User
   1.897 + *   agents should observe the following precedence rules to fill in the
   1.898 + *   background: if the value of the 'background' property for the HTML
   1.899 + *   element is different from 'transparent' then use it, else use the
   1.900 + *   value of the 'background' property for the BODY element. If the
   1.901 + *   resulting value is 'transparent', the rendering is undefined.
   1.902 + *
   1.903 + * Thus, in our implementation, it is responsible for ensuring that:
   1.904 + *  + we paint the correct background on the |nsCanvasFrame|,
   1.905 + *    |nsRootBoxFrame|, or |nsPageFrame|,
   1.906 + *  + we don't paint the background on the root element, and
   1.907 + *  + we don't paint the background on the BODY element in *some* cases,
   1.908 + *    and for SGML-based HTML documents only.
   1.909 + *
   1.910 + * |FindBackground| returns true if a background should be painted, and
   1.911 + * the resulting style context to use for the background information
   1.912 + * will be filled in to |aBackground|.
   1.913 + */
   1.914 +nsStyleContext*
   1.915 +nsCSSRendering::FindRootFrameBackground(nsIFrame* aForFrame)
   1.916 +{
   1.917 +  return FindBackgroundStyleFrame(aForFrame)->StyleContext();
   1.918 +}
   1.919 +
   1.920 +inline bool
   1.921 +FindElementBackground(nsIFrame* aForFrame, nsIFrame* aRootElementFrame,
   1.922 +                      nsStyleContext** aBackgroundSC)
   1.923 +{
   1.924 +  if (aForFrame == aRootElementFrame) {
   1.925 +    // We must have propagated our background to the viewport or canvas. Abort.
   1.926 +    return false;
   1.927 +  }
   1.928 +
   1.929 +  *aBackgroundSC = aForFrame->StyleContext();
   1.930 +
   1.931 +  // Return true unless the frame is for a BODY element whose background
   1.932 +  // was propagated to the viewport.
   1.933 +
   1.934 +  nsIContent* content = aForFrame->GetContent();
   1.935 +  if (!content || content->Tag() != nsGkAtoms::body)
   1.936 +    return true; // not frame for a "body" element
   1.937 +  // It could be a non-HTML "body" element but that's OK, we'd fail the
   1.938 +  // bodyContent check below
   1.939 +
   1.940 +  if (aForFrame->StyleContext()->GetPseudo())
   1.941 +    return true; // A pseudo-element frame.
   1.942 +
   1.943 +  // We should only look at the <html> background if we're in an HTML document
   1.944 +  nsIDocument* document = content->OwnerDoc();
   1.945 +
   1.946 +  dom::Element* bodyContent = document->GetBodyElement();
   1.947 +  if (bodyContent != content)
   1.948 +    return true; // this wasn't the background that was propagated
   1.949 +
   1.950 +  // This can be called even when there's no root element yet, during frame
   1.951 +  // construction, via nsLayoutUtils::FrameHasTransparency and
   1.952 +  // nsContainerFrame::SyncFrameViewProperties.
   1.953 +  if (!aRootElementFrame)
   1.954 +    return true;
   1.955 +
   1.956 +  const nsStyleBackground* htmlBG = aRootElementFrame->StyleBackground();
   1.957 +  return !htmlBG->IsTransparent();
   1.958 +}
   1.959 +
   1.960 +bool
   1.961 +nsCSSRendering::FindBackground(nsIFrame* aForFrame,
   1.962 +                               nsStyleContext** aBackgroundSC)
   1.963 +{
   1.964 +  nsIFrame* rootElementFrame =
   1.965 +    aForFrame->PresContext()->PresShell()->FrameConstructor()->GetRootElementStyleFrame();
   1.966 +  if (IsCanvasFrame(aForFrame)) {
   1.967 +    *aBackgroundSC = FindCanvasBackground(aForFrame, rootElementFrame);
   1.968 +    return true;
   1.969 +  } else {
   1.970 +    return FindElementBackground(aForFrame, rootElementFrame, aBackgroundSC);
   1.971 +  }
   1.972 +}
   1.973 +
   1.974 +void
   1.975 +nsCSSRendering::BeginFrameTreesLocked()
   1.976 +{
   1.977 +  ++gFrameTreeLockCount;
   1.978 +}
   1.979 +
   1.980 +void
   1.981 +nsCSSRendering::EndFrameTreesLocked()
   1.982 +{
   1.983 +  NS_ASSERTION(gFrameTreeLockCount > 0, "Unbalanced EndFrameTreeLocked");
   1.984 +  --gFrameTreeLockCount;
   1.985 +  if (gFrameTreeLockCount == 0) {
   1.986 +    gInlineBGData->Reset();
   1.987 +  }
   1.988 +}
   1.989 +
   1.990 +void
   1.991 +nsCSSRendering::PaintBoxShadowOuter(nsPresContext* aPresContext,
   1.992 +                                    nsRenderingContext& aRenderingContext,
   1.993 +                                    nsIFrame* aForFrame,
   1.994 +                                    const nsRect& aFrameArea,
   1.995 +                                    const nsRect& aDirtyRect,
   1.996 +                                    float aOpacity)
   1.997 +{
   1.998 +  const nsStyleBorder* styleBorder = aForFrame->StyleBorder();
   1.999 +  nsCSSShadowArray* shadows = styleBorder->mBoxShadow;
  1.1000 +  if (!shadows)
  1.1001 +    return;
  1.1002 +  nscoord twipsPerPixel = aPresContext->DevPixelsToAppUnits(1);
  1.1003 +
  1.1004 +  bool hasBorderRadius;
  1.1005 +  bool nativeTheme; // mutually exclusive with hasBorderRadius
  1.1006 +  gfxCornerSizes borderRadii;
  1.1007 +
  1.1008 +  // Get any border radius, since box-shadow must also have rounded corners if the frame does
  1.1009 +  const nsStyleDisplay* styleDisplay = aForFrame->StyleDisplay();
  1.1010 +  nsITheme::Transparency transparency;
  1.1011 +  if (aForFrame->IsThemed(styleDisplay, &transparency)) {
  1.1012 +    // We don't respect border-radius for native-themed widgets
  1.1013 +    hasBorderRadius = false;
  1.1014 +    // For opaque (rectangular) theme widgets we can take the generic
  1.1015 +    // border-box path with border-radius disabled.
  1.1016 +    nativeTheme = transparency != nsITheme::eOpaque;
  1.1017 +  } else {
  1.1018 +    nativeTheme = false;
  1.1019 +    nscoord twipsRadii[8];
  1.1020 +    NS_ASSERTION(aFrameArea.Size() == aForFrame->VisualBorderRectRelativeToSelf().Size(),
  1.1021 +                 "unexpected size");
  1.1022 +    hasBorderRadius = aForFrame->GetBorderRadii(twipsRadii);
  1.1023 +    if (hasBorderRadius) {
  1.1024 +      ComputePixelRadii(twipsRadii, twipsPerPixel, &borderRadii);
  1.1025 +    }
  1.1026 +  }
  1.1027 +
  1.1028 +  nsRect frameRect =
  1.1029 +    nativeTheme ? aForFrame->GetVisualOverflowRectRelativeToSelf() + aFrameArea.TopLeft() : aFrameArea;
  1.1030 +  gfxRect frameGfxRect(nsLayoutUtils::RectToGfxRect(frameRect, twipsPerPixel));
  1.1031 +  frameGfxRect.Round();
  1.1032 +
  1.1033 +  // We don't show anything that intersects with the frame we're blurring on. So tell the
  1.1034 +  // blurrer not to do unnecessary work there.
  1.1035 +  gfxRect skipGfxRect = frameGfxRect;
  1.1036 +  bool useSkipGfxRect = true;
  1.1037 +  if (nativeTheme) {
  1.1038 +    // Optimize non-leaf native-themed frames by skipping computing pixels
  1.1039 +    // in the padding-box. We assume the padding-box is going to be painted
  1.1040 +    // opaquely for non-leaf frames.
  1.1041 +    // XXX this may not be a safe assumption; we should make this go away
  1.1042 +    // by optimizing box-shadow drawing more for the cases where we don't have a skip-rect.
  1.1043 +    useSkipGfxRect = !aForFrame->IsLeaf();
  1.1044 +    nsRect paddingRect =
  1.1045 +      aForFrame->GetPaddingRect() - aForFrame->GetPosition() + aFrameArea.TopLeft();
  1.1046 +    skipGfxRect = nsLayoutUtils::RectToGfxRect(paddingRect, twipsPerPixel);
  1.1047 +  } else if (hasBorderRadius) {
  1.1048 +    skipGfxRect.Deflate(gfxMargin(
  1.1049 +        std::max(borderRadii[C_TL].height, borderRadii[C_TR].height), 0,
  1.1050 +        std::max(borderRadii[C_BL].height, borderRadii[C_BR].height), 0));
  1.1051 +  }
  1.1052 +
  1.1053 +  for (uint32_t i = shadows->Length(); i > 0; --i) {
  1.1054 +    nsCSSShadowItem* shadowItem = shadows->ShadowAt(i - 1);
  1.1055 +    if (shadowItem->mInset)
  1.1056 +      continue;
  1.1057 +
  1.1058 +    nsRect shadowRect = frameRect;
  1.1059 +    shadowRect.MoveBy(shadowItem->mXOffset, shadowItem->mYOffset);
  1.1060 +    if (!nativeTheme) {
  1.1061 +      shadowRect.Inflate(shadowItem->mSpread, shadowItem->mSpread);
  1.1062 +    }
  1.1063 +
  1.1064 +    // shadowRect won't include the blur, so make an extra rect here that includes the blur
  1.1065 +    // for use in the even-odd rule below.
  1.1066 +    nsRect shadowRectPlusBlur = shadowRect;
  1.1067 +    nscoord blurRadius = shadowItem->mRadius;
  1.1068 +    shadowRectPlusBlur.Inflate(
  1.1069 +      nsContextBoxBlur::GetBlurRadiusMargin(blurRadius, twipsPerPixel));
  1.1070 +
  1.1071 +    gfxRect shadowGfxRectPlusBlur =
  1.1072 +      nsLayoutUtils::RectToGfxRect(shadowRectPlusBlur, twipsPerPixel);
  1.1073 +    shadowGfxRectPlusBlur.RoundOut();
  1.1074 +
  1.1075 +    // Set the shadow color; if not specified, use the foreground color
  1.1076 +    nscolor shadowColor;
  1.1077 +    if (shadowItem->mHasColor)
  1.1078 +      shadowColor = shadowItem->mColor;
  1.1079 +    else
  1.1080 +      shadowColor = aForFrame->StyleColor()->mColor;
  1.1081 +
  1.1082 +    gfxRGBA gfxShadowColor(shadowColor);
  1.1083 +    gfxShadowColor.a *= aOpacity;
  1.1084 +
  1.1085 +    gfxContext* renderContext = aRenderingContext.ThebesContext();
  1.1086 +    if (nativeTheme) {
  1.1087 +      nsContextBoxBlur blurringArea;
  1.1088 +
  1.1089 +      // When getting the widget shape from the native theme, we're going
  1.1090 +      // to draw the widget into the shadow surface to create a mask.
  1.1091 +      // We need to ensure that there actually *is* a shadow surface
  1.1092 +      // and that we're not going to draw directly into renderContext.
  1.1093 +      gfxContext* shadowContext =
  1.1094 +        blurringArea.Init(shadowRect, shadowItem->mSpread,
  1.1095 +                          blurRadius, twipsPerPixel, renderContext, aDirtyRect,
  1.1096 +                          useSkipGfxRect ? &skipGfxRect : nullptr,
  1.1097 +                          nsContextBoxBlur::FORCE_MASK);
  1.1098 +      if (!shadowContext)
  1.1099 +        continue;
  1.1100 +
  1.1101 +      // shadowContext is owned by either blurringArea or aRenderingContext.
  1.1102 +      MOZ_ASSERT(shadowContext == blurringArea.GetContext());
  1.1103 +
  1.1104 +      renderContext->Save();
  1.1105 +      renderContext->SetColor(gfxShadowColor);
  1.1106 +
  1.1107 +      // Draw the shape of the frame so it can be blurred. Recall how nsContextBoxBlur
  1.1108 +      // doesn't make any temporary surfaces if blur is 0 and it just returns the original
  1.1109 +      // surface? If we have no blur, we're painting this fill on the actual content surface
  1.1110 +      // (renderContext == shadowContext) which is why we set up the color and clip
  1.1111 +      // before doing this.
  1.1112 +
  1.1113 +      // We don't clip the border-box from the shadow, nor any other box.
  1.1114 +      // We assume that the native theme is going to paint over the shadow.
  1.1115 +
  1.1116 +      // Draw the widget shape
  1.1117 +      gfxContextMatrixAutoSaveRestore save(shadowContext);
  1.1118 +      nsRefPtr<nsRenderingContext> wrapperCtx = new nsRenderingContext();
  1.1119 +      wrapperCtx->Init(aPresContext->DeviceContext(), shadowContext);
  1.1120 +      wrapperCtx->Translate(nsPoint(shadowItem->mXOffset,
  1.1121 +                                    shadowItem->mYOffset));
  1.1122 +
  1.1123 +      nsRect nativeRect;
  1.1124 +      nativeRect.IntersectRect(frameRect, aDirtyRect);
  1.1125 +      aPresContext->GetTheme()->DrawWidgetBackground(wrapperCtx, aForFrame,
  1.1126 +          styleDisplay->mAppearance, aFrameArea, nativeRect);
  1.1127 +
  1.1128 +      blurringArea.DoPaint();
  1.1129 +      renderContext->Restore();
  1.1130 +    } else {
  1.1131 +      renderContext->Save();
  1.1132 +      // Clip out the area of the actual frame so the shadow is not shown within
  1.1133 +      // the frame
  1.1134 +      renderContext->NewPath();
  1.1135 +      renderContext->Rectangle(shadowGfxRectPlusBlur);
  1.1136 +      if (hasBorderRadius) {
  1.1137 +        renderContext->RoundedRectangle(frameGfxRect, borderRadii);
  1.1138 +      } else {
  1.1139 +        renderContext->Rectangle(frameGfxRect);
  1.1140 +      }
  1.1141 +
  1.1142 +      renderContext->SetFillRule(gfxContext::FILL_RULE_EVEN_ODD);
  1.1143 +      renderContext->Clip();
  1.1144 +
  1.1145 +      gfxCornerSizes clipRectRadii;
  1.1146 +      if (hasBorderRadius) {
  1.1147 +        gfxFloat spreadDistance = shadowItem->mSpread / twipsPerPixel;
  1.1148 +
  1.1149 +        gfxFloat borderSizes[4];
  1.1150 +
  1.1151 +        borderSizes[NS_SIDE_LEFT] = spreadDistance;
  1.1152 +        borderSizes[NS_SIDE_TOP] = spreadDistance;
  1.1153 +        borderSizes[NS_SIDE_RIGHT] = spreadDistance;
  1.1154 +        borderSizes[NS_SIDE_BOTTOM] = spreadDistance;
  1.1155 +
  1.1156 +        nsCSSBorderRenderer::ComputeOuterRadii(borderRadii, borderSizes,
  1.1157 +            &clipRectRadii);
  1.1158 +
  1.1159 +      }
  1.1160 +      nsContextBoxBlur::BlurRectangle(renderContext,
  1.1161 +                                      shadowRect,
  1.1162 +                                      twipsPerPixel,
  1.1163 +                                      hasBorderRadius ? &clipRectRadii : nullptr,
  1.1164 +                                      blurRadius,
  1.1165 +                                      gfxShadowColor,
  1.1166 +                                      aDirtyRect,
  1.1167 +                                      skipGfxRect);
  1.1168 +      renderContext->Restore();
  1.1169 +    }
  1.1170 +
  1.1171 +  }
  1.1172 +}
  1.1173 +
  1.1174 +void
  1.1175 +nsCSSRendering::PaintBoxShadowInner(nsPresContext* aPresContext,
  1.1176 +                                    nsRenderingContext& aRenderingContext,
  1.1177 +                                    nsIFrame* aForFrame,
  1.1178 +                                    const nsRect& aFrameArea,
  1.1179 +                                    const nsRect& aDirtyRect)
  1.1180 +{
  1.1181 +  const nsStyleBorder* styleBorder = aForFrame->StyleBorder();
  1.1182 +  nsCSSShadowArray* shadows = styleBorder->mBoxShadow;
  1.1183 +  if (!shadows)
  1.1184 +    return;
  1.1185 +  if (aForFrame->IsThemed() && aForFrame->GetContent() &&
  1.1186 +      !nsContentUtils::IsChromeDoc(aForFrame->GetContent()->GetCurrentDoc())) {
  1.1187 +    // There's no way of getting hold of a shape corresponding to a
  1.1188 +    // "padding-box" for native-themed widgets, so just don't draw
  1.1189 +    // inner box-shadows for them. But we allow chrome to paint inner
  1.1190 +    // box shadows since chrome can be aware of the platform theme.
  1.1191 +    return;
  1.1192 +  }
  1.1193 +
  1.1194 +  // Get any border radius, since box-shadow must also have rounded corners
  1.1195 +  // if the frame does.
  1.1196 +  nscoord twipsRadii[8];
  1.1197 +  NS_ASSERTION(aForFrame->GetType() == nsGkAtoms::fieldSetFrame ||
  1.1198 +               aFrameArea.Size() == aForFrame->GetSize(), "unexpected size");
  1.1199 +  bool hasBorderRadius = aForFrame->GetBorderRadii(twipsRadii);
  1.1200 +  nscoord twipsPerPixel = aPresContext->DevPixelsToAppUnits(1);
  1.1201 +
  1.1202 +  nsRect paddingRect = aFrameArea;
  1.1203 +  nsMargin border = aForFrame->GetUsedBorder();
  1.1204 +  aForFrame->ApplySkipSides(border);
  1.1205 +  paddingRect.Deflate(border);
  1.1206 +
  1.1207 +  gfxCornerSizes innerRadii;
  1.1208 +  if (hasBorderRadius) {
  1.1209 +    gfxCornerSizes borderRadii;
  1.1210 +
  1.1211 +    ComputePixelRadii(twipsRadii, twipsPerPixel, &borderRadii);
  1.1212 +    gfxFloat borderSizes[4] = {
  1.1213 +      gfxFloat(border.top / twipsPerPixel),
  1.1214 +      gfxFloat(border.right / twipsPerPixel),
  1.1215 +      gfxFloat(border.bottom / twipsPerPixel),
  1.1216 +      gfxFloat(border.left / twipsPerPixel)
  1.1217 +    };
  1.1218 +    nsCSSBorderRenderer::ComputeInnerRadii(borderRadii, borderSizes,
  1.1219 +                                           &innerRadii);
  1.1220 +  }
  1.1221 +
  1.1222 +  for (uint32_t i = shadows->Length(); i > 0; --i) {
  1.1223 +    nsCSSShadowItem* shadowItem = shadows->ShadowAt(i - 1);
  1.1224 +    if (!shadowItem->mInset)
  1.1225 +      continue;
  1.1226 +
  1.1227 +    /*
  1.1228 +     * shadowRect: the frame's padding rect
  1.1229 +     * shadowPaintRect: the area to paint on the temp surface, larger than shadowRect
  1.1230 +     *                  so that blurs still happen properly near the edges
  1.1231 +     * shadowClipRect: the area on the temporary surface within shadowPaintRect
  1.1232 +     *                 that we will NOT paint in
  1.1233 +     */
  1.1234 +    nscoord blurRadius = shadowItem->mRadius;
  1.1235 +    nsMargin blurMargin =
  1.1236 +      nsContextBoxBlur::GetBlurRadiusMargin(blurRadius, twipsPerPixel);
  1.1237 +    nsRect shadowPaintRect = paddingRect;
  1.1238 +    shadowPaintRect.Inflate(blurMargin);
  1.1239 +
  1.1240 +    nsRect shadowClipRect = paddingRect;
  1.1241 +    shadowClipRect.MoveBy(shadowItem->mXOffset, shadowItem->mYOffset);
  1.1242 +    shadowClipRect.Deflate(shadowItem->mSpread, shadowItem->mSpread);
  1.1243 +
  1.1244 +    gfxCornerSizes clipRectRadii;
  1.1245 +    if (hasBorderRadius) {
  1.1246 +      // Calculate the radii the inner clipping rect will have
  1.1247 +      gfxFloat spreadDistance = shadowItem->mSpread / twipsPerPixel;
  1.1248 +      gfxFloat borderSizes[4] = {0, 0, 0, 0};
  1.1249 +
  1.1250 +      // See PaintBoxShadowOuter and bug 514670
  1.1251 +      if (innerRadii[C_TL].width > 0 || innerRadii[C_BL].width > 0) {
  1.1252 +        borderSizes[NS_SIDE_LEFT] = spreadDistance;
  1.1253 +      }
  1.1254 +
  1.1255 +      if (innerRadii[C_TL].height > 0 || innerRadii[C_TR].height > 0) {
  1.1256 +        borderSizes[NS_SIDE_TOP] = spreadDistance;
  1.1257 +      }
  1.1258 +
  1.1259 +      if (innerRadii[C_TR].width > 0 || innerRadii[C_BR].width > 0) {
  1.1260 +        borderSizes[NS_SIDE_RIGHT] = spreadDistance;
  1.1261 +      }
  1.1262 +
  1.1263 +      if (innerRadii[C_BL].height > 0 || innerRadii[C_BR].height > 0) {
  1.1264 +        borderSizes[NS_SIDE_BOTTOM] = spreadDistance;
  1.1265 +      }
  1.1266 +
  1.1267 +      nsCSSBorderRenderer::ComputeInnerRadii(innerRadii, borderSizes,
  1.1268 +                                             &clipRectRadii);
  1.1269 +    }
  1.1270 +
  1.1271 +    // Set the "skip rect" to the area within the frame that we don't paint in,
  1.1272 +    // including after blurring.
  1.1273 +    nsRect skipRect = shadowClipRect;
  1.1274 +    skipRect.Deflate(blurMargin);
  1.1275 +    gfxRect skipGfxRect = nsLayoutUtils::RectToGfxRect(skipRect, twipsPerPixel);
  1.1276 +    if (hasBorderRadius) {
  1.1277 +      skipGfxRect.Deflate(gfxMargin(
  1.1278 +          std::max(clipRectRadii[C_TL].height, clipRectRadii[C_TR].height), 0,
  1.1279 +          std::max(clipRectRadii[C_BL].height, clipRectRadii[C_BR].height), 0));
  1.1280 +    }
  1.1281 +
  1.1282 +    // When there's a blur radius, gfxAlphaBoxBlur leaves the skiprect area
  1.1283 +    // unchanged. And by construction the gfxSkipRect is not touched by the
  1.1284 +    // rendered shadow (even after blurring), so those pixels must be completely
  1.1285 +    // transparent in the shadow, so drawing them changes nothing.
  1.1286 +    gfxContext* renderContext = aRenderingContext.ThebesContext();
  1.1287 +    nsContextBoxBlur blurringArea;
  1.1288 +    gfxContext* shadowContext =
  1.1289 +      blurringArea.Init(shadowPaintRect, 0, blurRadius, twipsPerPixel,
  1.1290 +                        renderContext, aDirtyRect, &skipGfxRect);
  1.1291 +    if (!shadowContext)
  1.1292 +      continue;
  1.1293 +
  1.1294 +    // shadowContext is owned by either blurringArea or aRenderingContext.
  1.1295 +    MOZ_ASSERT(shadowContext == renderContext ||
  1.1296 +               shadowContext == blurringArea.GetContext());
  1.1297 +
  1.1298 +    // Set the shadow color; if not specified, use the foreground color
  1.1299 +    nscolor shadowColor;
  1.1300 +    if (shadowItem->mHasColor)
  1.1301 +      shadowColor = shadowItem->mColor;
  1.1302 +    else
  1.1303 +      shadowColor = aForFrame->StyleColor()->mColor;
  1.1304 +
  1.1305 +    renderContext->Save();
  1.1306 +    renderContext->SetColor(gfxRGBA(shadowColor));
  1.1307 +
  1.1308 +    // Clip the context to the area of the frame's padding rect, so no part of the
  1.1309 +    // shadow is painted outside. Also cut out anything beyond where the inset shadow
  1.1310 +    // will be.
  1.1311 +    gfxRect shadowGfxRect =
  1.1312 +      nsLayoutUtils::RectToGfxRect(paddingRect, twipsPerPixel);
  1.1313 +    shadowGfxRect.Round();
  1.1314 +    renderContext->NewPath();
  1.1315 +    if (hasBorderRadius)
  1.1316 +      renderContext->RoundedRectangle(shadowGfxRect, innerRadii, false);
  1.1317 +    else
  1.1318 +      renderContext->Rectangle(shadowGfxRect);
  1.1319 +    renderContext->Clip();
  1.1320 +
  1.1321 +    // Fill the surface minus the area within the frame that we should
  1.1322 +    // not paint in, and blur and apply it.
  1.1323 +    gfxRect shadowPaintGfxRect =
  1.1324 +      nsLayoutUtils::RectToGfxRect(shadowPaintRect, twipsPerPixel);
  1.1325 +    shadowPaintGfxRect.RoundOut();
  1.1326 +    gfxRect shadowClipGfxRect =
  1.1327 +      nsLayoutUtils::RectToGfxRect(shadowClipRect, twipsPerPixel);
  1.1328 +    shadowClipGfxRect.Round();
  1.1329 +    shadowContext->NewPath();
  1.1330 +    shadowContext->Rectangle(shadowPaintGfxRect);
  1.1331 +    if (hasBorderRadius)
  1.1332 +      shadowContext->RoundedRectangle(shadowClipGfxRect, clipRectRadii, false);
  1.1333 +    else
  1.1334 +      shadowContext->Rectangle(shadowClipGfxRect);
  1.1335 +    shadowContext->SetFillRule(gfxContext::FILL_RULE_EVEN_ODD);
  1.1336 +    shadowContext->Fill();
  1.1337 +
  1.1338 +    blurringArea.DoPaint();
  1.1339 +    renderContext->Restore();
  1.1340 +  }
  1.1341 +}
  1.1342 +
  1.1343 +void
  1.1344 +nsCSSRendering::PaintBackground(nsPresContext* aPresContext,
  1.1345 +                                nsRenderingContext& aRenderingContext,
  1.1346 +                                nsIFrame* aForFrame,
  1.1347 +                                const nsRect& aDirtyRect,
  1.1348 +                                const nsRect& aBorderArea,
  1.1349 +                                uint32_t aFlags,
  1.1350 +                                nsRect* aBGClipRect,
  1.1351 +                                int32_t aLayer)
  1.1352 +{
  1.1353 +  PROFILER_LABEL("nsCSSRendering", "PaintBackground");
  1.1354 +  NS_PRECONDITION(aForFrame,
  1.1355 +                  "Frame is expected to be provided to PaintBackground");
  1.1356 +
  1.1357 +  nsStyleContext *sc;
  1.1358 +  if (!FindBackground(aForFrame, &sc)) {
  1.1359 +    // We don't want to bail out if moz-appearance is set on a root
  1.1360 +    // node. If it has a parent content node, bail because it's not
  1.1361 +    // a root, otherwise keep going in order to let the theme stuff
  1.1362 +    // draw the background. The canvas really should be drawing the
  1.1363 +    // bg, but there's no way to hook that up via css.
  1.1364 +    if (!aForFrame->StyleDisplay()->mAppearance) {
  1.1365 +      return;
  1.1366 +    }
  1.1367 +
  1.1368 +    nsIContent* content = aForFrame->GetContent();
  1.1369 +    if (!content || content->GetParent()) {
  1.1370 +      return;
  1.1371 +    }
  1.1372 +
  1.1373 +    sc = aForFrame->StyleContext();
  1.1374 +  }
  1.1375 +
  1.1376 +  PaintBackgroundWithSC(aPresContext, aRenderingContext, aForFrame,
  1.1377 +                        aDirtyRect, aBorderArea, sc,
  1.1378 +                        *aForFrame->StyleBorder(), aFlags,
  1.1379 +                        aBGClipRect, aLayer);
  1.1380 +}
  1.1381 +
  1.1382 +void
  1.1383 +nsCSSRendering::PaintBackgroundColor(nsPresContext* aPresContext,
  1.1384 +                                     nsRenderingContext& aRenderingContext,
  1.1385 +                                     nsIFrame* aForFrame,
  1.1386 +                                     const nsRect& aDirtyRect,
  1.1387 +                                     const nsRect& aBorderArea,
  1.1388 +                                     uint32_t aFlags)
  1.1389 +{
  1.1390 +  PROFILER_LABEL("nsCSSRendering", "PaintBackgroundColor");
  1.1391 +  NS_PRECONDITION(aForFrame,
  1.1392 +                  "Frame is expected to be provided to PaintBackground");
  1.1393 +
  1.1394 +  nsStyleContext *sc;
  1.1395 +  if (!FindBackground(aForFrame, &sc)) {
  1.1396 +    // We don't want to bail out if moz-appearance is set on a root
  1.1397 +    // node. If it has a parent content node, bail because it's not
  1.1398 +    // a root, other wise keep going in order to let the theme stuff
  1.1399 +    // draw the background. The canvas really should be drawing the
  1.1400 +    // bg, but there's no way to hook that up via css.
  1.1401 +    if (!aForFrame->StyleDisplay()->mAppearance) {
  1.1402 +      return;
  1.1403 +    }
  1.1404 +
  1.1405 +    nsIContent* content = aForFrame->GetContent();
  1.1406 +    if (!content || content->GetParent()) {
  1.1407 +      return;
  1.1408 +    }
  1.1409 +
  1.1410 +    sc = aForFrame->StyleContext();
  1.1411 +  }
  1.1412 +
  1.1413 +  PaintBackgroundColorWithSC(aPresContext, aRenderingContext, aForFrame,
  1.1414 +                             aDirtyRect, aBorderArea, sc,
  1.1415 +                             *aForFrame->StyleBorder(), aFlags);
  1.1416 +}
  1.1417 +
  1.1418 +static bool
  1.1419 +IsOpaqueBorderEdge(const nsStyleBorder& aBorder, mozilla::css::Side aSide)
  1.1420 +{
  1.1421 +  if (aBorder.GetComputedBorder().Side(aSide) == 0)
  1.1422 +    return true;
  1.1423 +  switch (aBorder.GetBorderStyle(aSide)) {
  1.1424 +  case NS_STYLE_BORDER_STYLE_SOLID:
  1.1425 +  case NS_STYLE_BORDER_STYLE_GROOVE:
  1.1426 +  case NS_STYLE_BORDER_STYLE_RIDGE:
  1.1427 +  case NS_STYLE_BORDER_STYLE_INSET:
  1.1428 +  case NS_STYLE_BORDER_STYLE_OUTSET:
  1.1429 +    break;
  1.1430 +  default:
  1.1431 +    return false;
  1.1432 +  }
  1.1433 +
  1.1434 +  // If we're using a border image, assume it's not fully opaque,
  1.1435 +  // because we may not even have the image loaded at this point, and
  1.1436 +  // even if we did, checking whether the relevant tile is fully
  1.1437 +  // opaque would be too much work.
  1.1438 +  if (aBorder.mBorderImageSource.GetType() != eStyleImageType_Null)
  1.1439 +    return false;
  1.1440 +
  1.1441 +  nscolor color;
  1.1442 +  bool isForeground;
  1.1443 +  aBorder.GetBorderColor(aSide, color, isForeground);
  1.1444 +
  1.1445 +  // We don't know the foreground color here, so if it's being used
  1.1446 +  // we must assume it might be transparent.
  1.1447 +  if (isForeground)
  1.1448 +    return false;
  1.1449 +
  1.1450 +  return NS_GET_A(color) == 255;
  1.1451 +}
  1.1452 +
  1.1453 +/**
  1.1454 + * Returns true if all border edges are either missing or opaque.
  1.1455 + */
  1.1456 +static bool
  1.1457 +IsOpaqueBorder(const nsStyleBorder& aBorder)
  1.1458 +{
  1.1459 +  if (aBorder.mBorderColors)
  1.1460 +    return false;
  1.1461 +  NS_FOR_CSS_SIDES(i) {
  1.1462 +    if (!IsOpaqueBorderEdge(aBorder, i))
  1.1463 +      return false;
  1.1464 +  }
  1.1465 +  return true;
  1.1466 +}
  1.1467 +
  1.1468 +static inline void
  1.1469 +SetupDirtyRects(const nsRect& aBGClipArea, const nsRect& aCallerDirtyRect,
  1.1470 +                nscoord aAppUnitsPerPixel,
  1.1471 +                /* OUT: */
  1.1472 +                nsRect* aDirtyRect, gfxRect* aDirtyRectGfx)
  1.1473 +{
  1.1474 +  aDirtyRect->IntersectRect(aBGClipArea, aCallerDirtyRect);
  1.1475 +
  1.1476 +  // Compute the Thebes equivalent of the dirtyRect.
  1.1477 +  *aDirtyRectGfx = nsLayoutUtils::RectToGfxRect(*aDirtyRect, aAppUnitsPerPixel);
  1.1478 +  NS_WARN_IF_FALSE(aDirtyRect->IsEmpty() || !aDirtyRectGfx->IsEmpty(),
  1.1479 +                   "converted dirty rect should not be empty");
  1.1480 +  NS_ABORT_IF_FALSE(!aDirtyRect->IsEmpty() || aDirtyRectGfx->IsEmpty(),
  1.1481 +                    "second should be empty if first is");
  1.1482 +}
  1.1483 +
  1.1484 +struct BackgroundClipState {
  1.1485 +  nsRect mBGClipArea;  // Affected by mClippedRadii
  1.1486 +  nsRect mAdditionalBGClipArea;  // Not affected by mClippedRadii
  1.1487 +  nsRect mDirtyRect;
  1.1488 +  gfxRect mDirtyRectGfx;
  1.1489 +
  1.1490 +  gfxCornerSizes mClippedRadii;
  1.1491 +  bool mRadiiAreOuter;
  1.1492 +  bool mHasAdditionalBGClipArea;
  1.1493 +
  1.1494 +  // Whether we are being asked to draw with a caller provided background
  1.1495 +  // clipping area. If this is true we also disable rounded corners.
  1.1496 +  bool mCustomClip;
  1.1497 +};
  1.1498 +
  1.1499 +static void
  1.1500 +GetBackgroundClip(gfxContext *aCtx, uint8_t aBackgroundClip,
  1.1501 +                  uint8_t aBackgroundAttachment,
  1.1502 +                  nsIFrame* aForFrame, const nsRect& aBorderArea,
  1.1503 +                  const nsRect& aCallerDirtyRect, bool aHaveRoundedCorners,
  1.1504 +                  const gfxCornerSizes& aBGRadii, nscoord aAppUnitsPerPixel,
  1.1505 +                  /* out */ BackgroundClipState* aClipState)
  1.1506 +{
  1.1507 +  aClipState->mBGClipArea = aBorderArea;
  1.1508 +  aClipState->mHasAdditionalBGClipArea = false;
  1.1509 +  aClipState->mCustomClip = false;
  1.1510 +  aClipState->mRadiiAreOuter = true;
  1.1511 +  aClipState->mClippedRadii = aBGRadii;
  1.1512 +  if (aForFrame->GetType() == nsGkAtoms::scrollFrame &&
  1.1513 +        NS_STYLE_BG_ATTACHMENT_LOCAL == aBackgroundAttachment) {
  1.1514 +    // As of this writing, this is still in discussion in the CSS Working Group
  1.1515 +    // http://lists.w3.org/Archives/Public/www-style/2013Jul/0250.html
  1.1516 +
  1.1517 +    // The rectangle for 'background-clip' scrolls with the content,
  1.1518 +    // but the background is also clipped at a non-scrolling 'padding-box'
  1.1519 +    // like the content. (See below.)
  1.1520 +    // Therefore, only 'content-box' makes a difference here.
  1.1521 +    if (aBackgroundClip == NS_STYLE_BG_CLIP_CONTENT) {
  1.1522 +      nsIScrollableFrame* scrollableFrame = do_QueryFrame(aForFrame);
  1.1523 +      // Clip at a rectangle attached to the scrolled content.
  1.1524 +      aClipState->mHasAdditionalBGClipArea = true;
  1.1525 +      aClipState->mAdditionalBGClipArea = nsRect(
  1.1526 +        aClipState->mBGClipArea.TopLeft()
  1.1527 +          + scrollableFrame->GetScrolledFrame()->GetPosition()
  1.1528 +          // For the dir=rtl case:
  1.1529 +          + scrollableFrame->GetScrollRange().TopLeft(),
  1.1530 +        scrollableFrame->GetScrolledRect().Size());
  1.1531 +      nsMargin padding = aForFrame->GetUsedPadding();
  1.1532 +      // padding-bottom is ignored on scrollable frames:
  1.1533 +      // https://bugzilla.mozilla.org/show_bug.cgi?id=748518
  1.1534 +      padding.bottom = 0;
  1.1535 +      aForFrame->ApplySkipSides(padding);
  1.1536 +      aClipState->mAdditionalBGClipArea.Deflate(padding);
  1.1537 +    }
  1.1538 +
  1.1539 +    // Also clip at a non-scrolling, rounded-corner 'padding-box',
  1.1540 +    // same as the scrolled content because of the 'overflow' property.
  1.1541 +    aBackgroundClip = NS_STYLE_BG_CLIP_PADDING;
  1.1542 +  }
  1.1543 +
  1.1544 +  if (aBackgroundClip != NS_STYLE_BG_CLIP_BORDER) {
  1.1545 +    nsMargin border = aForFrame->GetUsedBorder();
  1.1546 +    if (aBackgroundClip == NS_STYLE_BG_CLIP_MOZ_ALMOST_PADDING) {
  1.1547 +      // Reduce |border| by 1px (device pixels) on all sides, if
  1.1548 +      // possible, so that we don't get antialiasing seams between the
  1.1549 +      // background and border.
  1.1550 +      border.top = std::max(0, border.top - aAppUnitsPerPixel);
  1.1551 +      border.right = std::max(0, border.right - aAppUnitsPerPixel);
  1.1552 +      border.bottom = std::max(0, border.bottom - aAppUnitsPerPixel);
  1.1553 +      border.left = std::max(0, border.left - aAppUnitsPerPixel);
  1.1554 +    } else if (aBackgroundClip != NS_STYLE_BG_CLIP_PADDING) {
  1.1555 +      NS_ASSERTION(aBackgroundClip == NS_STYLE_BG_CLIP_CONTENT,
  1.1556 +                   "unexpected background-clip");
  1.1557 +      border += aForFrame->GetUsedPadding();
  1.1558 +    }
  1.1559 +    aForFrame->ApplySkipSides(border);
  1.1560 +    aClipState->mBGClipArea.Deflate(border);
  1.1561 +
  1.1562 +    if (aHaveRoundedCorners) {
  1.1563 +      gfxFloat borderSizes[4] = {
  1.1564 +        gfxFloat(border.top / aAppUnitsPerPixel),
  1.1565 +        gfxFloat(border.right / aAppUnitsPerPixel),
  1.1566 +        gfxFloat(border.bottom / aAppUnitsPerPixel),
  1.1567 +        gfxFloat(border.left / aAppUnitsPerPixel)
  1.1568 +      };
  1.1569 +      nsCSSBorderRenderer::ComputeInnerRadii(aBGRadii, borderSizes,
  1.1570 +                                             &aClipState->mClippedRadii);
  1.1571 +      aClipState->mRadiiAreOuter = false;
  1.1572 +    }
  1.1573 +  }
  1.1574 +
  1.1575 +  if (!aHaveRoundedCorners && aClipState->mHasAdditionalBGClipArea) {
  1.1576 +    // Do the intersection here to account for the fast path(?) below.
  1.1577 +    aClipState->mBGClipArea =
  1.1578 +      aClipState->mBGClipArea.Intersect(aClipState->mAdditionalBGClipArea);
  1.1579 +    aClipState->mHasAdditionalBGClipArea = false;
  1.1580 +  }
  1.1581 +
  1.1582 +  SetupDirtyRects(aClipState->mBGClipArea, aCallerDirtyRect, aAppUnitsPerPixel,
  1.1583 +                  &aClipState->mDirtyRect, &aClipState->mDirtyRectGfx);
  1.1584 +}
  1.1585 +
  1.1586 +static void
  1.1587 +SetupBackgroundClip(BackgroundClipState& aClipState, gfxContext *aCtx,
  1.1588 +                    bool aHaveRoundedCorners, nscoord aAppUnitsPerPixel,
  1.1589 +                    gfxContextAutoSaveRestore* aAutoSR)
  1.1590 +{
  1.1591 +  if (aClipState.mDirtyRectGfx.IsEmpty()) {
  1.1592 +    // Our caller won't draw anything under this condition, so no need
  1.1593 +    // to set more up.
  1.1594 +    return;
  1.1595 +  }
  1.1596 +
  1.1597 +  if (aClipState.mCustomClip) {
  1.1598 +    // We don't support custom clips and rounded corners, arguably a bug, but
  1.1599 +    // table painting seems to depend on it.
  1.1600 +    return;
  1.1601 +  }
  1.1602 +
  1.1603 +  // If we have rounded corners, clip all subsequent drawing to the
  1.1604 +  // rounded rectangle defined by bgArea and bgRadii (we don't know
  1.1605 +  // whether the rounded corners intrude on the dirtyRect or not).
  1.1606 +  // Do not do this if we have a caller-provided clip rect --
  1.1607 +  // as above with bgArea, arguably a bug, but table painting seems
  1.1608 +  // to depend on it.
  1.1609 +
  1.1610 +  if (aHaveRoundedCorners || aClipState.mHasAdditionalBGClipArea) {
  1.1611 +    aAutoSR->Reset(aCtx);
  1.1612 +  }
  1.1613 +
  1.1614 +  if (aClipState.mHasAdditionalBGClipArea) {
  1.1615 +    gfxRect bgAreaGfx = nsLayoutUtils::RectToGfxRect(
  1.1616 +      aClipState.mAdditionalBGClipArea, aAppUnitsPerPixel);
  1.1617 +    bgAreaGfx.Round();
  1.1618 +    bgAreaGfx.Condition();
  1.1619 +    aCtx->NewPath();
  1.1620 +    aCtx->Rectangle(bgAreaGfx, true);
  1.1621 +    aCtx->Clip();
  1.1622 +  }
  1.1623 +
  1.1624 +  if (aHaveRoundedCorners) {
  1.1625 +    gfxRect bgAreaGfx =
  1.1626 +      nsLayoutUtils::RectToGfxRect(aClipState.mBGClipArea, aAppUnitsPerPixel);
  1.1627 +    bgAreaGfx.Round();
  1.1628 +    bgAreaGfx.Condition();
  1.1629 +
  1.1630 +    if (bgAreaGfx.IsEmpty()) {
  1.1631 +      // I think it's become possible to hit this since
  1.1632 +      // http://hg.mozilla.org/mozilla-central/rev/50e934e4979b landed.
  1.1633 +      NS_WARNING("converted background area should not be empty");
  1.1634 +      // Make our caller not do anything.
  1.1635 +      aClipState.mDirtyRectGfx.SizeTo(gfxSize(0.0, 0.0));
  1.1636 +      return;
  1.1637 +    }
  1.1638 +
  1.1639 +    aCtx->NewPath();
  1.1640 +    aCtx->RoundedRectangle(bgAreaGfx, aClipState.mClippedRadii, aClipState.mRadiiAreOuter);
  1.1641 +    aCtx->Clip();
  1.1642 +  }
  1.1643 +}
  1.1644 +
  1.1645 +static void
  1.1646 +DrawBackgroundColor(BackgroundClipState& aClipState, gfxContext *aCtx,
  1.1647 +                    bool aHaveRoundedCorners, nscoord aAppUnitsPerPixel)
  1.1648 +{
  1.1649 +  if (aClipState.mDirtyRectGfx.IsEmpty()) {
  1.1650 +    // Our caller won't draw anything under this condition, so no need
  1.1651 +    // to set more up.
  1.1652 +    return;
  1.1653 +  }
  1.1654 +
  1.1655 +  // We don't support custom clips and rounded corners, arguably a bug, but
  1.1656 +  // table painting seems to depend on it.
  1.1657 +  if (!aHaveRoundedCorners || aClipState.mCustomClip) {
  1.1658 +    aCtx->NewPath();
  1.1659 +    aCtx->Rectangle(aClipState.mDirtyRectGfx, true);
  1.1660 +    aCtx->Fill();
  1.1661 +    return;
  1.1662 +  }
  1.1663 +
  1.1664 +  gfxRect bgAreaGfx =
  1.1665 +    nsLayoutUtils::RectToGfxRect(aClipState.mBGClipArea, aAppUnitsPerPixel);
  1.1666 +  bgAreaGfx.Round();
  1.1667 +  bgAreaGfx.Condition();
  1.1668 +
  1.1669 +  if (bgAreaGfx.IsEmpty()) {
  1.1670 +    // I think it's become possible to hit this since
  1.1671 +    // http://hg.mozilla.org/mozilla-central/rev/50e934e4979b landed.
  1.1672 +    NS_WARNING("converted background area should not be empty");
  1.1673 +    // Make our caller not do anything.
  1.1674 +    aClipState.mDirtyRectGfx.SizeTo(gfxSize(0.0, 0.0));
  1.1675 +    return;
  1.1676 +  }
  1.1677 +
  1.1678 +  aCtx->Save();
  1.1679 +  gfxRect dirty = bgAreaGfx.Intersect(aClipState.mDirtyRectGfx);
  1.1680 +
  1.1681 +  aCtx->NewPath();
  1.1682 +  aCtx->Rectangle(dirty, true);
  1.1683 +  aCtx->Clip();
  1.1684 +
  1.1685 +  if (aClipState.mHasAdditionalBGClipArea) {
  1.1686 +    gfxRect bgAdditionalAreaGfx = nsLayoutUtils::RectToGfxRect(
  1.1687 +      aClipState.mAdditionalBGClipArea, aAppUnitsPerPixel);
  1.1688 +    bgAdditionalAreaGfx.Round();
  1.1689 +    bgAdditionalAreaGfx.Condition();
  1.1690 +    aCtx->NewPath();
  1.1691 +    aCtx->Rectangle(bgAdditionalAreaGfx, true);
  1.1692 +    aCtx->Clip();
  1.1693 +  }
  1.1694 +
  1.1695 +  aCtx->NewPath();
  1.1696 +  aCtx->RoundedRectangle(bgAreaGfx, aClipState.mClippedRadii,
  1.1697 +                         aClipState.mRadiiAreOuter);
  1.1698 +
  1.1699 +  aCtx->Fill();
  1.1700 +  aCtx->Restore();
  1.1701 +}
  1.1702 +
  1.1703 +nscolor
  1.1704 +nsCSSRendering::DetermineBackgroundColor(nsPresContext* aPresContext,
  1.1705 +                                         nsStyleContext* aStyleContext,
  1.1706 +                                         nsIFrame* aFrame,
  1.1707 +                                         bool& aDrawBackgroundImage,
  1.1708 +                                         bool& aDrawBackgroundColor)
  1.1709 +{
  1.1710 +  aDrawBackgroundImage = true;
  1.1711 +  aDrawBackgroundColor = true;
  1.1712 +
  1.1713 +  if (aFrame->HonorPrintBackgroundSettings()) {
  1.1714 +    aDrawBackgroundImage = aPresContext->GetBackgroundImageDraw();
  1.1715 +    aDrawBackgroundColor = aPresContext->GetBackgroundColorDraw();
  1.1716 +  }
  1.1717 +
  1.1718 +  const nsStyleBackground *bg = aStyleContext->StyleBackground();
  1.1719 +  nscolor bgColor;
  1.1720 +  if (aDrawBackgroundColor) {
  1.1721 +    bgColor =
  1.1722 +      aStyleContext->GetVisitedDependentColor(eCSSProperty_background_color);
  1.1723 +    if (NS_GET_A(bgColor) == 0) {
  1.1724 +      aDrawBackgroundColor = false;
  1.1725 +    }
  1.1726 +  } else {
  1.1727 +    // If GetBackgroundColorDraw() is false, we are still expected to
  1.1728 +    // draw color in the background of any frame that's not completely
  1.1729 +    // transparent, but we are expected to use white instead of whatever
  1.1730 +    // color was specified.
  1.1731 +    bgColor = NS_RGB(255, 255, 255);
  1.1732 +    if (aDrawBackgroundImage || !bg->IsTransparent()) {
  1.1733 +      aDrawBackgroundColor = true;
  1.1734 +    } else {
  1.1735 +      bgColor = NS_RGBA(0,0,0,0);
  1.1736 +    }
  1.1737 +  }
  1.1738 +
  1.1739 +  // We can skip painting the background color if a background image is opaque.
  1.1740 +  if (aDrawBackgroundColor &&
  1.1741 +      bg->BottomLayer().mRepeat.mXRepeat == NS_STYLE_BG_REPEAT_REPEAT &&
  1.1742 +      bg->BottomLayer().mRepeat.mYRepeat == NS_STYLE_BG_REPEAT_REPEAT &&
  1.1743 +      bg->BottomLayer().mImage.IsOpaque() &&
  1.1744 +      bg->BottomLayer().mBlendMode == NS_STYLE_BLEND_NORMAL) {
  1.1745 +    aDrawBackgroundColor = false;
  1.1746 +  }
  1.1747 +
  1.1748 +  return bgColor;
  1.1749 +}
  1.1750 +
  1.1751 +static gfxFloat
  1.1752 +ConvertGradientValueToPixels(const nsStyleCoord& aCoord,
  1.1753 +                             gfxFloat aFillLength,
  1.1754 +                             int32_t aAppUnitsPerPixel)
  1.1755 +{
  1.1756 +  switch (aCoord.GetUnit()) {
  1.1757 +    case eStyleUnit_Percent:
  1.1758 +      return aCoord.GetPercentValue() * aFillLength;
  1.1759 +    case eStyleUnit_Coord:
  1.1760 +      return NSAppUnitsToFloatPixels(aCoord.GetCoordValue(), aAppUnitsPerPixel);
  1.1761 +    case eStyleUnit_Calc: {
  1.1762 +      const nsStyleCoord::Calc *calc = aCoord.GetCalcValue();
  1.1763 +      return calc->mPercent * aFillLength +
  1.1764 +             NSAppUnitsToFloatPixels(calc->mLength, aAppUnitsPerPixel);
  1.1765 +    }
  1.1766 +    default:
  1.1767 +      NS_WARNING("Unexpected coord unit");
  1.1768 +      return 0;
  1.1769 +  }
  1.1770 +}
  1.1771 +
  1.1772 +// Given a box with size aBoxSize and origin (0,0), and an angle aAngle,
  1.1773 +// and a starting point for the gradient line aStart, find the endpoint of
  1.1774 +// the gradient line --- the intersection of the gradient line with a line
  1.1775 +// perpendicular to aAngle that passes through the farthest corner in the
  1.1776 +// direction aAngle.
  1.1777 +static gfxPoint
  1.1778 +ComputeGradientLineEndFromAngle(const gfxPoint& aStart,
  1.1779 +                                double aAngle,
  1.1780 +                                const gfxSize& aBoxSize)
  1.1781 +{
  1.1782 +  double dx = cos(-aAngle);
  1.1783 +  double dy = sin(-aAngle);
  1.1784 +  gfxPoint farthestCorner(dx > 0 ? aBoxSize.width : 0,
  1.1785 +                          dy > 0 ? aBoxSize.height : 0);
  1.1786 +  gfxPoint delta = farthestCorner - aStart;
  1.1787 +  double u = delta.x*dy - delta.y*dx;
  1.1788 +  return farthestCorner + gfxPoint(-u*dy, u*dx);
  1.1789 +}
  1.1790 +
  1.1791 +// Compute the start and end points of the gradient line for a linear gradient.
  1.1792 +static void
  1.1793 +ComputeLinearGradientLine(nsPresContext* aPresContext,
  1.1794 +                          nsStyleGradient* aGradient,
  1.1795 +                          const gfxSize& aBoxSize,
  1.1796 +                          gfxPoint* aLineStart,
  1.1797 +                          gfxPoint* aLineEnd)
  1.1798 +{
  1.1799 +  if (aGradient->mBgPosX.GetUnit() == eStyleUnit_None) {
  1.1800 +    double angle;
  1.1801 +    if (aGradient->mAngle.IsAngleValue()) {
  1.1802 +      angle = aGradient->mAngle.GetAngleValueInRadians();
  1.1803 +      if (!aGradient->mLegacySyntax) {
  1.1804 +        angle = M_PI_2 - angle;
  1.1805 +      }
  1.1806 +    } else {
  1.1807 +      angle = -M_PI_2; // defaults to vertical gradient starting from top
  1.1808 +    }
  1.1809 +    gfxPoint center(aBoxSize.width/2, aBoxSize.height/2);
  1.1810 +    *aLineEnd = ComputeGradientLineEndFromAngle(center, angle, aBoxSize);
  1.1811 +    *aLineStart = gfxPoint(aBoxSize.width, aBoxSize.height) - *aLineEnd;
  1.1812 +  } else if (!aGradient->mLegacySyntax) {
  1.1813 +    float xSign = aGradient->mBgPosX.GetPercentValue() * 2 - 1;
  1.1814 +    float ySign = 1 - aGradient->mBgPosY.GetPercentValue() * 2;
  1.1815 +    double angle = atan2(ySign * aBoxSize.width, xSign * aBoxSize.height);
  1.1816 +    gfxPoint center(aBoxSize.width/2, aBoxSize.height/2);
  1.1817 +    *aLineEnd = ComputeGradientLineEndFromAngle(center, angle, aBoxSize);
  1.1818 +    *aLineStart = gfxPoint(aBoxSize.width, aBoxSize.height) - *aLineEnd;
  1.1819 +  } else {
  1.1820 +    int32_t appUnitsPerPixel = aPresContext->AppUnitsPerDevPixel();
  1.1821 +    *aLineStart = gfxPoint(
  1.1822 +      ConvertGradientValueToPixels(aGradient->mBgPosX, aBoxSize.width,
  1.1823 +                                   appUnitsPerPixel),
  1.1824 +      ConvertGradientValueToPixels(aGradient->mBgPosY, aBoxSize.height,
  1.1825 +                                   appUnitsPerPixel));
  1.1826 +    if (aGradient->mAngle.IsAngleValue()) {
  1.1827 +      MOZ_ASSERT(aGradient->mLegacySyntax);
  1.1828 +      double angle = aGradient->mAngle.GetAngleValueInRadians();
  1.1829 +      *aLineEnd = ComputeGradientLineEndFromAngle(*aLineStart, angle, aBoxSize);
  1.1830 +    } else {
  1.1831 +      // No angle, the line end is just the reflection of the start point
  1.1832 +      // through the center of the box
  1.1833 +      *aLineEnd = gfxPoint(aBoxSize.width, aBoxSize.height) - *aLineStart;
  1.1834 +    }
  1.1835 +  }
  1.1836 +}
  1.1837 +
  1.1838 +// Compute the start and end points of the gradient line for a radial gradient.
  1.1839 +// Also returns the horizontal and vertical radii defining the circle or
  1.1840 +// ellipse to use.
  1.1841 +static void
  1.1842 +ComputeRadialGradientLine(nsPresContext* aPresContext,
  1.1843 +                          nsStyleGradient* aGradient,
  1.1844 +                          const gfxSize& aBoxSize,
  1.1845 +                          gfxPoint* aLineStart,
  1.1846 +                          gfxPoint* aLineEnd,
  1.1847 +                          double* aRadiusX,
  1.1848 +                          double* aRadiusY)
  1.1849 +{
  1.1850 +  if (aGradient->mBgPosX.GetUnit() == eStyleUnit_None) {
  1.1851 +    // Default line start point is the center of the box
  1.1852 +    *aLineStart = gfxPoint(aBoxSize.width/2, aBoxSize.height/2);
  1.1853 +  } else {
  1.1854 +    int32_t appUnitsPerPixel = aPresContext->AppUnitsPerDevPixel();
  1.1855 +    *aLineStart = gfxPoint(
  1.1856 +      ConvertGradientValueToPixels(aGradient->mBgPosX, aBoxSize.width,
  1.1857 +                                   appUnitsPerPixel),
  1.1858 +      ConvertGradientValueToPixels(aGradient->mBgPosY, aBoxSize.height,
  1.1859 +                                   appUnitsPerPixel));
  1.1860 +  }
  1.1861 +
  1.1862 +  // Compute gradient shape: the x and y radii of an ellipse.
  1.1863 +  double radiusX, radiusY;
  1.1864 +  double leftDistance = Abs(aLineStart->x);
  1.1865 +  double rightDistance = Abs(aBoxSize.width - aLineStart->x);
  1.1866 +  double topDistance = Abs(aLineStart->y);
  1.1867 +  double bottomDistance = Abs(aBoxSize.height - aLineStart->y);
  1.1868 +  switch (aGradient->mSize) {
  1.1869 +  case NS_STYLE_GRADIENT_SIZE_CLOSEST_SIDE:
  1.1870 +    radiusX = std::min(leftDistance, rightDistance);
  1.1871 +    radiusY = std::min(topDistance, bottomDistance);
  1.1872 +    if (aGradient->mShape == NS_STYLE_GRADIENT_SHAPE_CIRCULAR) {
  1.1873 +      radiusX = radiusY = std::min(radiusX, radiusY);
  1.1874 +    }
  1.1875 +    break;
  1.1876 +  case NS_STYLE_GRADIENT_SIZE_CLOSEST_CORNER: {
  1.1877 +    // Compute x and y distances to nearest corner
  1.1878 +    double offsetX = std::min(leftDistance, rightDistance);
  1.1879 +    double offsetY = std::min(topDistance, bottomDistance);
  1.1880 +    if (aGradient->mShape == NS_STYLE_GRADIENT_SHAPE_CIRCULAR) {
  1.1881 +      radiusX = radiusY = NS_hypot(offsetX, offsetY);
  1.1882 +    } else {
  1.1883 +      // maintain aspect ratio
  1.1884 +      radiusX = offsetX*M_SQRT2;
  1.1885 +      radiusY = offsetY*M_SQRT2;
  1.1886 +    }
  1.1887 +    break;
  1.1888 +  }
  1.1889 +  case NS_STYLE_GRADIENT_SIZE_FARTHEST_SIDE:
  1.1890 +    radiusX = std::max(leftDistance, rightDistance);
  1.1891 +    radiusY = std::max(topDistance, bottomDistance);
  1.1892 +    if (aGradient->mShape == NS_STYLE_GRADIENT_SHAPE_CIRCULAR) {
  1.1893 +      radiusX = radiusY = std::max(radiusX, radiusY);
  1.1894 +    }
  1.1895 +    break;
  1.1896 +  case NS_STYLE_GRADIENT_SIZE_FARTHEST_CORNER: {
  1.1897 +    // Compute x and y distances to nearest corner
  1.1898 +    double offsetX = std::max(leftDistance, rightDistance);
  1.1899 +    double offsetY = std::max(topDistance, bottomDistance);
  1.1900 +    if (aGradient->mShape == NS_STYLE_GRADIENT_SHAPE_CIRCULAR) {
  1.1901 +      radiusX = radiusY = NS_hypot(offsetX, offsetY);
  1.1902 +    } else {
  1.1903 +      // maintain aspect ratio
  1.1904 +      radiusX = offsetX*M_SQRT2;
  1.1905 +      radiusY = offsetY*M_SQRT2;
  1.1906 +    }
  1.1907 +    break;
  1.1908 +  }
  1.1909 +  case NS_STYLE_GRADIENT_SIZE_EXPLICIT_SIZE: {
  1.1910 +    int32_t appUnitsPerPixel = aPresContext->AppUnitsPerDevPixel();
  1.1911 +    radiusX = ConvertGradientValueToPixels(aGradient->mRadiusX,
  1.1912 +                                           aBoxSize.width, appUnitsPerPixel);
  1.1913 +    radiusY = ConvertGradientValueToPixels(aGradient->mRadiusY,
  1.1914 +                                           aBoxSize.height, appUnitsPerPixel);
  1.1915 +    break;
  1.1916 +  }
  1.1917 +  default:
  1.1918 +    radiusX = radiusY = 0;
  1.1919 +    NS_ABORT_IF_FALSE(false, "unknown radial gradient sizing method");
  1.1920 +  }
  1.1921 +  *aRadiusX = radiusX;
  1.1922 +  *aRadiusY = radiusY;
  1.1923 +
  1.1924 +  double angle;
  1.1925 +  if (aGradient->mAngle.IsAngleValue()) {
  1.1926 +    angle = aGradient->mAngle.GetAngleValueInRadians();
  1.1927 +  } else {
  1.1928 +    // Default angle is 0deg
  1.1929 +    angle = 0.0;
  1.1930 +  }
  1.1931 +
  1.1932 +  // The gradient line end point is where the gradient line intersects
  1.1933 +  // the ellipse.
  1.1934 +  *aLineEnd = *aLineStart + gfxPoint(radiusX*cos(-angle), radiusY*sin(-angle));
  1.1935 +}
  1.1936 +
  1.1937 +// Returns aFrac*aC2 + (1 - aFrac)*C1. The interpolation is done
  1.1938 +// in unpremultiplied space, which is what SVG gradients and cairo
  1.1939 +// gradients expect.
  1.1940 +static gfxRGBA
  1.1941 +InterpolateColor(const gfxRGBA& aC1, const gfxRGBA& aC2, double aFrac)
  1.1942 +{
  1.1943 +  double other = 1 - aFrac;
  1.1944 +  return gfxRGBA(aC2.r*aFrac + aC1.r*other,
  1.1945 +                 aC2.g*aFrac + aC1.g*other,
  1.1946 +                 aC2.b*aFrac + aC1.b*other,
  1.1947 +                 aC2.a*aFrac + aC1.a*other);
  1.1948 +}
  1.1949 +
  1.1950 +static nscoord
  1.1951 +FindTileStart(nscoord aDirtyCoord, nscoord aTilePos, nscoord aTileDim)
  1.1952 +{
  1.1953 +  NS_ASSERTION(aTileDim > 0, "Non-positive tile dimension");
  1.1954 +  double multiples = floor(double(aDirtyCoord - aTilePos)/aTileDim);
  1.1955 +  return NSToCoordRound(multiples*aTileDim + aTilePos);
  1.1956 +}
  1.1957 +
  1.1958 +void
  1.1959 +nsCSSRendering::PaintGradient(nsPresContext* aPresContext,
  1.1960 +                              nsRenderingContext& aRenderingContext,
  1.1961 +                              nsStyleGradient* aGradient,
  1.1962 +                              const nsRect& aDirtyRect,
  1.1963 +                              const nsRect& aDest,
  1.1964 +                              const nsRect& aFillArea,
  1.1965 +                              const CSSIntRect& aSrc,
  1.1966 +                              const nsSize& aIntrinsicSize)
  1.1967 +{
  1.1968 +  PROFILER_LABEL("nsCSSRendering", "PaintGradient");
  1.1969 +  Telemetry::AutoTimer<Telemetry::GRADIENT_DURATION, Telemetry::Microsecond> gradientTimer;
  1.1970 +  if (aDest.IsEmpty() || aFillArea.IsEmpty()) {
  1.1971 +    return;
  1.1972 +  }
  1.1973 +
  1.1974 +  gfxContext *ctx = aRenderingContext.ThebesContext();
  1.1975 +  nscoord appUnitsPerDevPixel = aPresContext->AppUnitsPerDevPixel();
  1.1976 +  gfxSize srcSize = gfxSize(gfxFloat(aIntrinsicSize.width)/appUnitsPerDevPixel,
  1.1977 +                            gfxFloat(aIntrinsicSize.height)/appUnitsPerDevPixel);
  1.1978 +
  1.1979 +  bool cellContainsFill = aDest.Contains(aFillArea);
  1.1980 +
  1.1981 +  // Compute "gradient line" start and end relative to the intrinsic size of
  1.1982 +  // the gradient.
  1.1983 +  gfxPoint lineStart, lineEnd;
  1.1984 +  double radiusX = 0, radiusY = 0; // for radial gradients only
  1.1985 +  if (aGradient->mShape == NS_STYLE_GRADIENT_SHAPE_LINEAR) {
  1.1986 +    ComputeLinearGradientLine(aPresContext, aGradient, srcSize,
  1.1987 +                              &lineStart, &lineEnd);
  1.1988 +  } else {
  1.1989 +    ComputeRadialGradientLine(aPresContext, aGradient, srcSize,
  1.1990 +                              &lineStart, &lineEnd, &radiusX, &radiusY);
  1.1991 +  }
  1.1992 +  gfxFloat lineLength = NS_hypot(lineEnd.x - lineStart.x,
  1.1993 +                                  lineEnd.y - lineStart.y);
  1.1994 +
  1.1995 +  NS_ABORT_IF_FALSE(aGradient->mStops.Length() >= 2,
  1.1996 +                    "The parser should reject gradients with less than two stops");
  1.1997 +
  1.1998 +  // Build color stop array and compute stop positions
  1.1999 +  nsTArray<ColorStop> stops;
  1.2000 +  // If there is a run of stops before stop i that did not have specified
  1.2001 +  // positions, then this is the index of the first stop in that run, otherwise
  1.2002 +  // it's -1.
  1.2003 +  int32_t firstUnsetPosition = -1;
  1.2004 +  for (uint32_t i = 0; i < aGradient->mStops.Length(); ++i) {
  1.2005 +    const nsStyleGradientStop& stop = aGradient->mStops[i];
  1.2006 +    double position;
  1.2007 +    switch (stop.mLocation.GetUnit()) {
  1.2008 +    case eStyleUnit_None:
  1.2009 +      if (i == 0) {
  1.2010 +        // First stop defaults to position 0.0
  1.2011 +        position = 0.0;
  1.2012 +      } else if (i == aGradient->mStops.Length() - 1) {
  1.2013 +        // Last stop defaults to position 1.0
  1.2014 +        position = 1.0;
  1.2015 +      } else {
  1.2016 +        // Other stops with no specified position get their position assigned
  1.2017 +        // later by interpolation, see below.
  1.2018 +        // Remeber where the run of stops with no specified position starts,
  1.2019 +        // if it starts here.
  1.2020 +        if (firstUnsetPosition < 0) {
  1.2021 +          firstUnsetPosition = i;
  1.2022 +        }
  1.2023 +        stops.AppendElement(ColorStop(0, stop.mColor));
  1.2024 +        continue;
  1.2025 +      }
  1.2026 +      break;
  1.2027 +    case eStyleUnit_Percent:
  1.2028 +      position = stop.mLocation.GetPercentValue();
  1.2029 +      break;
  1.2030 +    case eStyleUnit_Coord:
  1.2031 +      position = lineLength < 1e-6 ? 0.0 :
  1.2032 +          stop.mLocation.GetCoordValue() / appUnitsPerDevPixel / lineLength;
  1.2033 +      break;
  1.2034 +    case eStyleUnit_Calc:
  1.2035 +      nsStyleCoord::Calc *calc;
  1.2036 +      calc = stop.mLocation.GetCalcValue();
  1.2037 +      position = calc->mPercent +
  1.2038 +          ((lineLength < 1e-6) ? 0.0 :
  1.2039 +          (NSAppUnitsToFloatPixels(calc->mLength, appUnitsPerDevPixel) / lineLength));
  1.2040 +      break;
  1.2041 +    default:
  1.2042 +      NS_ABORT_IF_FALSE(false, "Unknown stop position type");
  1.2043 +    }
  1.2044 +
  1.2045 +    if (i > 0) {
  1.2046 +      // Prevent decreasing stop positions by advancing this position
  1.2047 +      // to the previous stop position, if necessary
  1.2048 +      position = std::max(position, stops[i - 1].mPosition);
  1.2049 +    }
  1.2050 +    stops.AppendElement(ColorStop(position, stop.mColor));
  1.2051 +    if (firstUnsetPosition > 0) {
  1.2052 +      // Interpolate positions for all stops that didn't have a specified position
  1.2053 +      double p = stops[firstUnsetPosition - 1].mPosition;
  1.2054 +      double d = (stops[i].mPosition - p)/(i - firstUnsetPosition + 1);
  1.2055 +      for (uint32_t j = firstUnsetPosition; j < i; ++j) {
  1.2056 +        p += d;
  1.2057 +        stops[j].mPosition = p;
  1.2058 +      }
  1.2059 +      firstUnsetPosition = -1;
  1.2060 +    }
  1.2061 +  }
  1.2062 +
  1.2063 +  // Eliminate negative-position stops if the gradient is radial.
  1.2064 +  double firstStop = stops[0].mPosition;
  1.2065 +  if (aGradient->mShape != NS_STYLE_GRADIENT_SHAPE_LINEAR && firstStop < 0.0) {
  1.2066 +    if (aGradient->mRepeating) {
  1.2067 +      // Choose an instance of the repeated pattern that gives us all positive
  1.2068 +      // stop-offsets.
  1.2069 +      double lastStop = stops[stops.Length() - 1].mPosition;
  1.2070 +      double stopDelta = lastStop - firstStop;
  1.2071 +      // If all the stops are in approximately the same place then logic below
  1.2072 +      // will kick in that makes us draw just the last stop color, so don't
  1.2073 +      // try to do anything in that case. We certainly need to avoid
  1.2074 +      // dividing by zero.
  1.2075 +      if (stopDelta >= 1e-6) {
  1.2076 +        double instanceCount = ceil(-firstStop/stopDelta);
  1.2077 +        // Advance stops by instanceCount multiples of the period of the
  1.2078 +        // repeating gradient.
  1.2079 +        double offset = instanceCount*stopDelta;
  1.2080 +        for (uint32_t i = 0; i < stops.Length(); i++) {
  1.2081 +          stops[i].mPosition += offset;
  1.2082 +        }
  1.2083 +      }
  1.2084 +    } else {
  1.2085 +      // Move negative-position stops to position 0.0. We may also need
  1.2086 +      // to set the color of the stop to the color the gradient should have
  1.2087 +      // at the center of the ellipse.
  1.2088 +      for (uint32_t i = 0; i < stops.Length(); i++) {
  1.2089 +        double pos = stops[i].mPosition;
  1.2090 +        if (pos < 0.0) {
  1.2091 +          stops[i].mPosition = 0.0;
  1.2092 +          // If this is the last stop, we don't need to adjust the color,
  1.2093 +          // it will fill the entire area.
  1.2094 +          if (i < stops.Length() - 1) {
  1.2095 +            double nextPos = stops[i + 1].mPosition;
  1.2096 +            // If nextPos is approximately equal to pos, then we don't
  1.2097 +            // need to adjust the color of this stop because it's
  1.2098 +            // not going to be displayed.
  1.2099 +            // If nextPos is negative, we don't need to adjust the color of
  1.2100 +            // this stop since it's not going to be displayed because
  1.2101 +            // nextPos will also be moved to 0.0.
  1.2102 +            if (nextPos >= 0.0 && nextPos - pos >= 1e-6) {
  1.2103 +              // Compute how far the new position 0.0 is along the interval
  1.2104 +              // between pos and nextPos.
  1.2105 +              // XXX Color interpolation (in cairo, too) should use the
  1.2106 +              // CSS 'color-interpolation' property!
  1.2107 +              double frac = (0.0 - pos)/(nextPos - pos);
  1.2108 +              stops[i].mColor =
  1.2109 +                InterpolateColor(stops[i].mColor, stops[i + 1].mColor, frac);
  1.2110 +            }
  1.2111 +          }
  1.2112 +        }
  1.2113 +      }
  1.2114 +    }
  1.2115 +    firstStop = stops[0].mPosition;
  1.2116 +    NS_ABORT_IF_FALSE(firstStop >= 0.0, "Failed to fix stop offsets");
  1.2117 +  }
  1.2118 +
  1.2119 +  if (aGradient->mShape != NS_STYLE_GRADIENT_SHAPE_LINEAR && !aGradient->mRepeating) {
  1.2120 +    // Direct2D can only handle a particular class of radial gradients because
  1.2121 +    // of the way the it specifies gradients. Setting firstStop to 0, when we
  1.2122 +    // can, will help us stay on the fast path. Currently we don't do this
  1.2123 +    // for repeating gradients but we could by adjusting the stop collection
  1.2124 +    // to start at 0
  1.2125 +    firstStop = 0;
  1.2126 +  }
  1.2127 +
  1.2128 +  double lastStop = stops[stops.Length() - 1].mPosition;
  1.2129 +  // Cairo gradients must have stop positions in the range [0, 1]. So,
  1.2130 +  // stop positions will be normalized below by subtracting firstStop and then
  1.2131 +  // multiplying by stopScale.
  1.2132 +  double stopScale;
  1.2133 +  double stopOrigin = firstStop;
  1.2134 +  double stopEnd = lastStop;
  1.2135 +  double stopDelta = lastStop - firstStop;
  1.2136 +  bool zeroRadius = aGradient->mShape != NS_STYLE_GRADIENT_SHAPE_LINEAR &&
  1.2137 +                      (radiusX < 1e-6 || radiusY < 1e-6);
  1.2138 +  if (stopDelta < 1e-6 || lineLength < 1e-6 || zeroRadius) {
  1.2139 +    // Stops are all at the same place. Map all stops to 0.0.
  1.2140 +    // For repeating radial gradients, or for any radial gradients with
  1.2141 +    // a zero radius, we need to fill with the last stop color, so just set
  1.2142 +    // both radii to 0.
  1.2143 +    if (aGradient->mRepeating || zeroRadius) {
  1.2144 +      radiusX = radiusY = 0.0;
  1.2145 +    }
  1.2146 +    stopDelta = 0.0;
  1.2147 +    lastStop = firstStop;
  1.2148 +  }
  1.2149 +
  1.2150 +  // Don't normalize non-repeating or degenerate gradients below 0..1
  1.2151 +  // This keeps the gradient line as large as the box and doesn't
  1.2152 +  // lets us avoiding having to get padding correct for stops
  1.2153 +  // at 0 and 1
  1.2154 +  if (!aGradient->mRepeating || stopDelta == 0.0) {
  1.2155 +    stopOrigin = std::min(stopOrigin, 0.0);
  1.2156 +    stopEnd = std::max(stopEnd, 1.0);
  1.2157 +  }
  1.2158 +  stopScale = 1.0/(stopEnd - stopOrigin);
  1.2159 +
  1.2160 +  // Create the gradient pattern.
  1.2161 +  nsRefPtr<gfxPattern> gradientPattern;
  1.2162 +  bool forceRepeatToCoverTiles = false;
  1.2163 +  gfxMatrix matrix;
  1.2164 +  if (aGradient->mShape == NS_STYLE_GRADIENT_SHAPE_LINEAR) {
  1.2165 +    // Compute the actual gradient line ends we need to pass to cairo after
  1.2166 +    // stops have been normalized.
  1.2167 +    gfxPoint gradientStart = lineStart + (lineEnd - lineStart)*stopOrigin;
  1.2168 +    gfxPoint gradientEnd = lineStart + (lineEnd - lineStart)*stopEnd;
  1.2169 +    gfxPoint gradientStopStart = lineStart + (lineEnd - lineStart)*firstStop;
  1.2170 +    gfxPoint gradientStopEnd = lineStart + (lineEnd - lineStart)*lastStop;
  1.2171 +
  1.2172 +    if (stopDelta == 0.0) {
  1.2173 +      // Stops are all at the same place. For repeating gradients, this will
  1.2174 +      // just paint the last stop color. We don't need to do anything.
  1.2175 +      // For non-repeating gradients, this should render as two colors, one
  1.2176 +      // on each "side" of the gradient line segment, which is a point. All
  1.2177 +      // our stops will be at 0.0; we just need to set the direction vector
  1.2178 +      // correctly.
  1.2179 +      gradientEnd = gradientStart + (lineEnd - lineStart);
  1.2180 +      gradientStopEnd = gradientStopStart + (lineEnd - lineStart);
  1.2181 +    }
  1.2182 +
  1.2183 +    gradientPattern = new gfxPattern(gradientStart.x, gradientStart.y,
  1.2184 +                                      gradientEnd.x, gradientEnd.y);
  1.2185 +
  1.2186 +    // When the gradient line is parallel to the x axis from the left edge
  1.2187 +    // to the right edge of a tile, then we can repeat by just repeating the
  1.2188 +    // gradient.
  1.2189 +    if (!cellContainsFill &&
  1.2190 +        ((gradientStopStart.y == gradientStopEnd.y && gradientStopStart.x == 0 &&
  1.2191 +          gradientStopEnd.x == srcSize.width) ||
  1.2192 +          (gradientStopStart.x == gradientStopEnd.x && gradientStopStart.y == 0 &&
  1.2193 +          gradientStopEnd.y == srcSize.height))) {
  1.2194 +      forceRepeatToCoverTiles = true;
  1.2195 +    }
  1.2196 +  } else {
  1.2197 +    NS_ASSERTION(firstStop >= 0.0,
  1.2198 +                  "Negative stops not allowed for radial gradients");
  1.2199 +
  1.2200 +    // To form an ellipse, we'll stretch a circle vertically, if necessary.
  1.2201 +    // So our radii are based on radiusX.
  1.2202 +    double innerRadius = radiusX*stopOrigin;
  1.2203 +    double outerRadius = radiusX*stopEnd;
  1.2204 +    if (stopDelta == 0.0) {
  1.2205 +      // Stops are all at the same place.  See above (except we now have
  1.2206 +      // the inside vs. outside of an ellipse).
  1.2207 +      outerRadius = innerRadius + 1;
  1.2208 +    }
  1.2209 +    gradientPattern = new gfxPattern(lineStart.x, lineStart.y, innerRadius,
  1.2210 +                                     lineStart.x, lineStart.y, outerRadius);
  1.2211 +    if (radiusX != radiusY) {
  1.2212 +      // Stretch the circles into ellipses vertically by setting a transform
  1.2213 +      // in the pattern.
  1.2214 +      // Recall that this is the transform from user space to pattern space.
  1.2215 +      // So to stretch the ellipse by factor of P vertically, we scale
  1.2216 +      // user coordinates by 1/P.
  1.2217 +      matrix.Translate(lineStart);
  1.2218 +      matrix.Scale(1.0, radiusX/radiusY);
  1.2219 +      matrix.Translate(-lineStart);
  1.2220 +    }
  1.2221 +  }
  1.2222 +  // Use a pattern transform to take account of source and dest rects
  1.2223 +  matrix.Translate(gfxPoint(aPresContext->CSSPixelsToDevPixels(aSrc.x),
  1.2224 +                            aPresContext->CSSPixelsToDevPixels(aSrc.y)));
  1.2225 +  matrix.Scale(gfxFloat(aPresContext->CSSPixelsToAppUnits(aSrc.width))/aDest.width,
  1.2226 +               gfxFloat(aPresContext->CSSPixelsToAppUnits(aSrc.height))/aDest.height);
  1.2227 +  gradientPattern->SetMatrix(matrix);
  1.2228 +
  1.2229 +  if (gradientPattern->CairoStatus())
  1.2230 +    return;
  1.2231 +
  1.2232 +  if (stopDelta == 0.0) {
  1.2233 +    // Non-repeating gradient with all stops in same place -> just add
  1.2234 +    // first stop and last stop, both at position 0.
  1.2235 +    // Repeating gradient with all stops in the same place, or radial
  1.2236 +    // gradient with radius of 0 -> just paint the last stop color.
  1.2237 +    // We use firstStop offset to keep |stops| with same units (will later normalize to 0).
  1.2238 +    gfxRGBA firstColor(stops[0].mColor);
  1.2239 +    gfxRGBA lastColor(stops.LastElement().mColor);
  1.2240 +    stops.Clear();
  1.2241 +
  1.2242 +    if (!aGradient->mRepeating && !zeroRadius) {
  1.2243 +      stops.AppendElement(ColorStop(firstStop, firstColor));
  1.2244 +    }
  1.2245 +    stops.AppendElement(ColorStop(firstStop, lastColor));
  1.2246 +  }
  1.2247 +
  1.2248 +  bool isRepeat = aGradient->mRepeating || forceRepeatToCoverTiles;
  1.2249 +
  1.2250 +  // Now set normalized color stops in pattern.
  1.2251 +  if (!ctx->IsCairo()) {
  1.2252 +    // Offscreen gradient surface cache (not a tile):
  1.2253 +    // On some backends (e.g. D2D), the GradientStops object holds an offscreen surface
  1.2254 +    // which is a lookup table used to evaluate the gradient. This surface can use
  1.2255 +    // much memory (ram and/or GPU ram) and can be expensive to create. So we cache it.
  1.2256 +    // The cache key correlates 1:1 with the arguments for CreateGradientStops (also the implied backend type)
  1.2257 +    // Note that GradientStop is a simple struct with a stop value (while GradientStops has the surface).
  1.2258 +    nsTArray<gfx::GradientStop> rawStops(stops.Length());
  1.2259 +    rawStops.SetLength(stops.Length());
  1.2260 +    for(uint32_t i = 0; i < stops.Length(); i++) {
  1.2261 +      rawStops[i].color = gfx::Color(stops[i].mColor.r, stops[i].mColor.g, stops[i].mColor.b, stops[i].mColor.a);
  1.2262 +      rawStops[i].offset = stopScale * (stops[i].mPosition - stopOrigin);
  1.2263 +    }
  1.2264 +    mozilla::RefPtr<mozilla::gfx::GradientStops> gs =
  1.2265 +      gfxGradientCache::GetOrCreateGradientStops(ctx->GetDrawTarget(),
  1.2266 +                                                 rawStops,
  1.2267 +                                                 isRepeat ? gfx::ExtendMode::REPEAT : gfx::ExtendMode::CLAMP);
  1.2268 +    gradientPattern->SetColorStops(gs);
  1.2269 +  } else {
  1.2270 +    for (uint32_t i = 0; i < stops.Length(); i++) {
  1.2271 +      double pos = stopScale*(stops[i].mPosition - stopOrigin);
  1.2272 +      gradientPattern->AddColorStop(pos, stops[i].mColor);
  1.2273 +    }
  1.2274 +    // Set repeat mode. Default cairo extend mode is PAD.
  1.2275 +    if (isRepeat) {
  1.2276 +      gradientPattern->SetExtend(gfxPattern::EXTEND_REPEAT);
  1.2277 +    }
  1.2278 +  }
  1.2279 +
  1.2280 +  // Paint gradient tiles. This isn't terribly efficient, but doing it this
  1.2281 +  // way is simple and sure to get pixel-snapping right. We could speed things
  1.2282 +  // up by drawing tiles into temporary surfaces and copying those to the
  1.2283 +  // destination, but after pixel-snapping tiles may not all be the same size.
  1.2284 +  nsRect dirty;
  1.2285 +  if (!dirty.IntersectRect(aDirtyRect, aFillArea))
  1.2286 +    return;
  1.2287 +
  1.2288 +  gfxRect areaToFill =
  1.2289 +    nsLayoutUtils::RectToGfxRect(aFillArea, appUnitsPerDevPixel);
  1.2290 +  gfxMatrix ctm = ctx->CurrentMatrix();
  1.2291 +  bool isCTMPreservingAxisAlignedRectangles = ctm.PreservesAxisAlignedRectangles();
  1.2292 +
  1.2293 +  // xStart/yStart are the top-left corner of the top-left tile.
  1.2294 +  nscoord xStart = FindTileStart(dirty.x, aDest.x, aDest.width);
  1.2295 +  nscoord yStart = FindTileStart(dirty.y, aDest.y, aDest.height);
  1.2296 +  nscoord xEnd = forceRepeatToCoverTiles ? xStart + aDest.width : dirty.XMost();
  1.2297 +  nscoord yEnd = forceRepeatToCoverTiles ? yStart + aDest.height : dirty.YMost();
  1.2298 +
  1.2299 +  // x and y are the top-left corner of the tile to draw
  1.2300 +  for (nscoord y = yStart; y < yEnd; y += aDest.height) {
  1.2301 +    for (nscoord x = xStart; x < xEnd; x += aDest.width) {
  1.2302 +      // The coordinates of the tile
  1.2303 +      gfxRect tileRect = nsLayoutUtils::RectToGfxRect(
  1.2304 +                      nsRect(x, y, aDest.width, aDest.height),
  1.2305 +                      appUnitsPerDevPixel);
  1.2306 +      // The actual area to fill with this tile is the intersection of this
  1.2307 +      // tile with the overall area we're supposed to be filling
  1.2308 +      gfxRect fillRect =
  1.2309 +        forceRepeatToCoverTiles ? areaToFill : tileRect.Intersect(areaToFill);
  1.2310 +      // Try snapping the fill rect. Snap its top-left and bottom-right
  1.2311 +      // independently to preserve the orientation.
  1.2312 +      gfxPoint snappedFillRectTopLeft = fillRect.TopLeft();
  1.2313 +      gfxPoint snappedFillRectTopRight = fillRect.TopRight();
  1.2314 +      gfxPoint snappedFillRectBottomRight = fillRect.BottomRight();
  1.2315 +      // Snap three points instead of just two to ensure we choose the
  1.2316 +      // correct orientation if there's a reflection.
  1.2317 +      if (isCTMPreservingAxisAlignedRectangles &&
  1.2318 +          ctx->UserToDevicePixelSnapped(snappedFillRectTopLeft, true) &&
  1.2319 +          ctx->UserToDevicePixelSnapped(snappedFillRectBottomRight, true) &&
  1.2320 +          ctx->UserToDevicePixelSnapped(snappedFillRectTopRight, true)) {
  1.2321 +        if (snappedFillRectTopLeft.x == snappedFillRectBottomRight.x ||
  1.2322 +            snappedFillRectTopLeft.y == snappedFillRectBottomRight.y) {
  1.2323 +          // Nothing to draw; avoid scaling by zero and other weirdness that
  1.2324 +          // could put the context in an error state.
  1.2325 +          continue;
  1.2326 +        }
  1.2327 +        // Set the context's transform to the transform that maps fillRect to
  1.2328 +        // snappedFillRect. The part of the gradient that was going to
  1.2329 +        // exactly fill fillRect will fill snappedFillRect instead.
  1.2330 +        gfxMatrix transform = gfxUtils::TransformRectToRect(fillRect,
  1.2331 +            snappedFillRectTopLeft, snappedFillRectTopRight,
  1.2332 +            snappedFillRectBottomRight);
  1.2333 +        ctx->SetMatrix(transform);
  1.2334 +      }
  1.2335 +      ctx->NewPath();
  1.2336 +      ctx->Rectangle(fillRect);
  1.2337 +      ctx->Translate(tileRect.TopLeft());
  1.2338 +      ctx->SetPattern(gradientPattern);
  1.2339 +      ctx->Fill();
  1.2340 +      ctx->SetMatrix(ctm);
  1.2341 +    }
  1.2342 +  }
  1.2343 +}
  1.2344 +
  1.2345 +void
  1.2346 +nsCSSRendering::PaintBackgroundWithSC(nsPresContext* aPresContext,
  1.2347 +                                      nsRenderingContext& aRenderingContext,
  1.2348 +                                      nsIFrame* aForFrame,
  1.2349 +                                      const nsRect& aDirtyRect,
  1.2350 +                                      const nsRect& aBorderArea,
  1.2351 +                                      nsStyleContext* aBackgroundSC,
  1.2352 +                                      const nsStyleBorder& aBorder,
  1.2353 +                                      uint32_t aFlags,
  1.2354 +                                      nsRect* aBGClipRect,
  1.2355 +                                      int32_t aLayer)
  1.2356 +{
  1.2357 +  NS_PRECONDITION(aForFrame,
  1.2358 +                  "Frame is expected to be provided to PaintBackground");
  1.2359 +
  1.2360 +  // Check to see if we have an appearance defined.  If so, we let the theme
  1.2361 +  // renderer draw the background and bail out.
  1.2362 +  // XXXzw this ignores aBGClipRect.
  1.2363 +  const nsStyleDisplay* displayData = aForFrame->StyleDisplay();
  1.2364 +  if (displayData->mAppearance) {
  1.2365 +    nsITheme *theme = aPresContext->GetTheme();
  1.2366 +    if (theme && theme->ThemeSupportsWidget(aPresContext, aForFrame,
  1.2367 +                                            displayData->mAppearance)) {
  1.2368 +      nsRect drawing(aBorderArea);
  1.2369 +      theme->GetWidgetOverflow(aPresContext->DeviceContext(),
  1.2370 +                               aForFrame, displayData->mAppearance, &drawing);
  1.2371 +      drawing.IntersectRect(drawing, aDirtyRect);
  1.2372 +      theme->DrawWidgetBackground(&aRenderingContext, aForFrame,
  1.2373 +                                  displayData->mAppearance, aBorderArea,
  1.2374 +                                  drawing);
  1.2375 +      return;
  1.2376 +    }
  1.2377 +  }
  1.2378 +
  1.2379 +  // For canvas frames (in the CSS sense) we draw the background color using
  1.2380 +  // a solid color item that gets added in nsLayoutUtils::PaintFrame,
  1.2381 +  // or nsSubDocumentFrame::BuildDisplayList (bug 488242). (The solid
  1.2382 +  // color may be moved into nsDisplayCanvasBackground by
  1.2383 +  // nsPresShell::AddCanvasBackgroundColorItem, and painted by
  1.2384 +  // nsDisplayCanvasBackground directly.) Either way we don't need to
  1.2385 +  // paint the background color here.
  1.2386 +  bool isCanvasFrame = IsCanvasFrame(aForFrame);
  1.2387 +
  1.2388 +  // Determine whether we are drawing background images and/or
  1.2389 +  // background colors.
  1.2390 +  bool drawBackgroundImage;
  1.2391 +  bool drawBackgroundColor;
  1.2392 +
  1.2393 +  nscolor bgColor = DetermineBackgroundColor(aPresContext,
  1.2394 +                                             aBackgroundSC,
  1.2395 +                                             aForFrame,
  1.2396 +                                             drawBackgroundImage,
  1.2397 +                                             drawBackgroundColor);
  1.2398 +
  1.2399 +  // If we're drawing a specific layer, we don't want to draw the
  1.2400 +  // background color.
  1.2401 +  const nsStyleBackground *bg = aBackgroundSC->StyleBackground();
  1.2402 +  if (drawBackgroundColor && aLayer >= 0) {
  1.2403 +    drawBackgroundColor = false;
  1.2404 +  }
  1.2405 +
  1.2406 +  // At this point, drawBackgroundImage and drawBackgroundColor are
  1.2407 +  // true if and only if we are actually supposed to paint an image or
  1.2408 +  // color into aDirtyRect, respectively.
  1.2409 +  if (!drawBackgroundImage && !drawBackgroundColor)
  1.2410 +    return;
  1.2411 +
  1.2412 +  // Compute the outermost boundary of the area that might be painted.
  1.2413 +  gfxContext *ctx = aRenderingContext.ThebesContext();
  1.2414 +  nscoord appUnitsPerPixel = aPresContext->AppUnitsPerDevPixel();
  1.2415 +
  1.2416 +  // Same coordinate space as aBorderArea & aBGClipRect
  1.2417 +  gfxCornerSizes bgRadii;
  1.2418 +  bool haveRoundedCorners;
  1.2419 +  {
  1.2420 +    nscoord radii[8];
  1.2421 +    nsSize frameSize = aForFrame->GetSize();
  1.2422 +    if (&aBorder == aForFrame->StyleBorder() &&
  1.2423 +        frameSize == aBorderArea.Size()) {
  1.2424 +      haveRoundedCorners = aForFrame->GetBorderRadii(radii);
  1.2425 +    } else {
  1.2426 +      haveRoundedCorners = nsIFrame::ComputeBorderRadii(aBorder.mBorderRadius,
  1.2427 +                                   frameSize, aBorderArea.Size(),
  1.2428 +                                   aForFrame->GetSkipSides(), radii);
  1.2429 +    }
  1.2430 +    if (haveRoundedCorners)
  1.2431 +      ComputePixelRadii(radii, appUnitsPerPixel, &bgRadii);
  1.2432 +  }
  1.2433 +
  1.2434 +  // The 'bgClipArea' (used only by the image tiling logic, far below)
  1.2435 +  // is the caller-provided aBGClipRect if any, or else the area
  1.2436 +  // determined by the value of 'background-clip' in
  1.2437 +  // SetupCurrentBackgroundClip.  (Arguably it should be the
  1.2438 +  // intersection, but that breaks the table painter -- in particular,
  1.2439 +  // taking the intersection breaks reftests/bugs/403249-1[ab].)
  1.2440 +  BackgroundClipState clipState;
  1.2441 +  uint8_t currentBackgroundClip;
  1.2442 +  bool isSolidBorder;
  1.2443 +  if (aBGClipRect) {
  1.2444 +    clipState.mBGClipArea = *aBGClipRect;
  1.2445 +    clipState.mCustomClip = true;
  1.2446 +    SetupDirtyRects(clipState.mBGClipArea, aDirtyRect, appUnitsPerPixel,
  1.2447 +                    &clipState.mDirtyRect, &clipState.mDirtyRectGfx);
  1.2448 +  } else {
  1.2449 +    // The background is rendered over the 'background-clip' area,
  1.2450 +    // which is normally equal to the border area but may be reduced
  1.2451 +    // to the padding area by CSS.  Also, if the border is solid, we
  1.2452 +    // don't need to draw outside the padding area.  In either case,
  1.2453 +    // if the borders are rounded, make sure we use the same inner
  1.2454 +    // radii as the border code will.
  1.2455 +    // The background-color is drawn based on the bottom
  1.2456 +    // background-clip.
  1.2457 +    currentBackgroundClip = bg->BottomLayer().mClip;
  1.2458 +    isSolidBorder =
  1.2459 +      (aFlags & PAINTBG_WILL_PAINT_BORDER) && IsOpaqueBorder(aBorder);
  1.2460 +    if (isSolidBorder && currentBackgroundClip == NS_STYLE_BG_CLIP_BORDER) {
  1.2461 +      // If we have rounded corners, we need to inflate the background
  1.2462 +      // drawing area a bit to avoid seams between the border and
  1.2463 +      // background.
  1.2464 +      currentBackgroundClip = haveRoundedCorners ?
  1.2465 +        NS_STYLE_BG_CLIP_MOZ_ALMOST_PADDING : NS_STYLE_BG_CLIP_PADDING;
  1.2466 +    }
  1.2467 +
  1.2468 +    GetBackgroundClip(ctx, currentBackgroundClip, bg->BottomLayer().mAttachment,
  1.2469 +                      aForFrame, aBorderArea,
  1.2470 +                      aDirtyRect, haveRoundedCorners, bgRadii, appUnitsPerPixel,
  1.2471 +                      &clipState);
  1.2472 +  }
  1.2473 +
  1.2474 +  // If we might be using a background color, go ahead and set it now.
  1.2475 +  if (drawBackgroundColor && !isCanvasFrame)
  1.2476 +    ctx->SetColor(gfxRGBA(bgColor));
  1.2477 +
  1.2478 +  gfxContextAutoSaveRestore autoSR;
  1.2479 +
  1.2480 +  // If there is no background image, draw a color.  (If there is
  1.2481 +  // neither a background image nor a color, we wouldn't have gotten
  1.2482 +  // this far.)
  1.2483 +  if (!drawBackgroundImage) {
  1.2484 +    if (!isCanvasFrame) {
  1.2485 +      DrawBackgroundColor(clipState, ctx, haveRoundedCorners, appUnitsPerPixel);
  1.2486 +    }
  1.2487 +    return;
  1.2488 +  }
  1.2489 +
  1.2490 +  if (bg->mImageCount < 1) {
  1.2491 +    // Return if there are no background layers, all work from this point
  1.2492 +    // onwards happens iteratively on these.
  1.2493 +    return;
  1.2494 +  }
  1.2495 +
  1.2496 +  // Validate the layer range before we start iterating.
  1.2497 +  int32_t startLayer = aLayer;
  1.2498 +  int32_t nLayers = 1;
  1.2499 +  if (startLayer < 0) {
  1.2500 +    startLayer = (int32_t)bg->mImageCount - 1;
  1.2501 +    nLayers = bg->mImageCount;
  1.2502 +  }
  1.2503 +
  1.2504 +  // Ensure we get invalidated for loads of the image.  We need to do
  1.2505 +  // this here because this might be the only code that knows about the
  1.2506 +  // association of the style data with the frame.
  1.2507 +  if (aBackgroundSC != aForFrame->StyleContext()) {
  1.2508 +    NS_FOR_VISIBLE_BACKGROUND_LAYERS_BACK_TO_FRONT_WITH_RANGE(i, bg, startLayer, nLayers) {
  1.2509 +      aForFrame->AssociateImage(bg->mLayers[i].mImage, aPresContext);
  1.2510 +    }
  1.2511 +  }
  1.2512 +
  1.2513 +  // The background color is rendered over the entire dirty area,
  1.2514 +  // even if the image isn't.
  1.2515 +  if (drawBackgroundColor && !isCanvasFrame) {
  1.2516 +    DrawBackgroundColor(clipState, ctx, haveRoundedCorners, appUnitsPerPixel);
  1.2517 +  }
  1.2518 +
  1.2519 +  if (drawBackgroundImage) {
  1.2520 +    bool clipSet = false;
  1.2521 +    NS_FOR_VISIBLE_BACKGROUND_LAYERS_BACK_TO_FRONT_WITH_RANGE(i, bg, bg->mImageCount - 1,
  1.2522 +                                                              nLayers + (bg->mImageCount -
  1.2523 +                                                                         startLayer - 1)) {
  1.2524 +      const nsStyleBackground::Layer &layer = bg->mLayers[i];
  1.2525 +      if (!aBGClipRect) {
  1.2526 +        uint8_t newBackgroundClip = layer.mClip;
  1.2527 +        if (isSolidBorder && newBackgroundClip == NS_STYLE_BG_CLIP_BORDER) {
  1.2528 +          newBackgroundClip = haveRoundedCorners ?
  1.2529 +            NS_STYLE_BG_CLIP_MOZ_ALMOST_PADDING : NS_STYLE_BG_CLIP_PADDING;
  1.2530 +        }
  1.2531 +        if (currentBackgroundClip != newBackgroundClip || !clipSet) {
  1.2532 +          currentBackgroundClip = newBackgroundClip;
  1.2533 +          // If clipSet is false that means this is the bottom layer and we
  1.2534 +          // already called GetBackgroundClip above and it stored its results
  1.2535 +          // in clipState.
  1.2536 +          if (clipSet) {
  1.2537 +            GetBackgroundClip(ctx, currentBackgroundClip, layer.mAttachment, aForFrame,
  1.2538 +                              aBorderArea, aDirtyRect, haveRoundedCorners,
  1.2539 +                              bgRadii, appUnitsPerPixel, &clipState);
  1.2540 +          }
  1.2541 +          SetupBackgroundClip(clipState, ctx, haveRoundedCorners,
  1.2542 +                              appUnitsPerPixel, &autoSR);
  1.2543 +          clipSet = true;
  1.2544 +        }
  1.2545 +      }
  1.2546 +      if ((aLayer < 0 || i == (uint32_t)startLayer) &&
  1.2547 +          !clipState.mDirtyRectGfx.IsEmpty()) {
  1.2548 +        nsBackgroundLayerState state = PrepareBackgroundLayer(aPresContext, aForFrame,
  1.2549 +            aFlags, aBorderArea, clipState.mBGClipArea, *bg, layer);
  1.2550 +        if (!state.mFillArea.IsEmpty()) {
  1.2551 +          if (state.mCompositingOp != gfxContext::OPERATOR_OVER) {
  1.2552 +            NS_ASSERTION(ctx->CurrentOperator() == gfxContext::OPERATOR_OVER,
  1.2553 +                         "It is assumed the initial operator is OPERATOR_OVER, when it is restored later");
  1.2554 +            ctx->SetOperator(state.mCompositingOp);
  1.2555 +          }
  1.2556 +          state.mImageRenderer.DrawBackground(aPresContext, aRenderingContext,
  1.2557 +                                              state.mDestArea, state.mFillArea,
  1.2558 +                                              state.mAnchor + aBorderArea.TopLeft(),
  1.2559 +                                              clipState.mDirtyRect);
  1.2560 +          if (state.mCompositingOp != gfxContext::OPERATOR_OVER) {
  1.2561 +            ctx->SetOperator(gfxContext::OPERATOR_OVER);
  1.2562 +          }
  1.2563 +        }
  1.2564 +      }
  1.2565 +    }
  1.2566 +  }
  1.2567 +}
  1.2568 +
  1.2569 +void
  1.2570 +nsCSSRendering::PaintBackgroundColorWithSC(nsPresContext* aPresContext,
  1.2571 +                                           nsRenderingContext& aRenderingContext,
  1.2572 +                                           nsIFrame* aForFrame,
  1.2573 +                                           const nsRect& aDirtyRect,
  1.2574 +                                           const nsRect& aBorderArea,
  1.2575 +                                           nsStyleContext* aBackgroundSC,
  1.2576 +                                           const nsStyleBorder& aBorder,
  1.2577 +                                           uint32_t aFlags)
  1.2578 +{
  1.2579 +  NS_PRECONDITION(aForFrame,
  1.2580 +                  "Frame is expected to be provided to PaintBackground");
  1.2581 +
  1.2582 +  // Check to see if we have an appearance defined.  If so, we let the theme
  1.2583 +  // renderer draw the background and bail out.
  1.2584 +  const nsStyleDisplay* displayData = aForFrame->StyleDisplay();
  1.2585 +  if (displayData->mAppearance) {
  1.2586 +    nsITheme *theme = aPresContext->GetTheme();
  1.2587 +    if (theme && theme->ThemeSupportsWidget(aPresContext, aForFrame,
  1.2588 +                                            displayData->mAppearance)) {
  1.2589 +      NS_ERROR("Shouldn't be trying to paint a background color if we are themed!");
  1.2590 +      return;
  1.2591 +    }
  1.2592 +  }
  1.2593 +
  1.2594 +  NS_ASSERTION(!IsCanvasFrame(aForFrame), "Should not be trying to paint a background color for canvas frames!");
  1.2595 +
  1.2596 +  // Determine whether we are drawing background images and/or
  1.2597 +  // background colors.
  1.2598 +  bool drawBackgroundImage;
  1.2599 +  bool drawBackgroundColor;
  1.2600 +  nscolor bgColor = DetermineBackgroundColor(aPresContext,
  1.2601 +                                             aBackgroundSC,
  1.2602 +                                             aForFrame,
  1.2603 +                                             drawBackgroundImage,
  1.2604 +                                             drawBackgroundColor);
  1.2605 +
  1.2606 +  NS_ASSERTION(drawBackgroundImage || drawBackgroundColor,
  1.2607 +               "Should not be trying to paint a background if we don't have one");
  1.2608 +  if (!drawBackgroundColor) {
  1.2609 +    return;
  1.2610 +  }
  1.2611 +
  1.2612 +  // Compute the outermost boundary of the area that might be painted.
  1.2613 +  gfxContext *ctx = aRenderingContext.ThebesContext();
  1.2614 +  nscoord appUnitsPerPixel = aPresContext->AppUnitsPerDevPixel();
  1.2615 +
  1.2616 +  // Same coordinate space as aBorderArea
  1.2617 +  gfxCornerSizes bgRadii;
  1.2618 +  bool haveRoundedCorners;
  1.2619 +  {
  1.2620 +    nscoord radii[8];
  1.2621 +    nsSize frameSize = aForFrame->GetSize();
  1.2622 +    if (&aBorder == aForFrame->StyleBorder() &&
  1.2623 +        frameSize == aBorderArea.Size()) {
  1.2624 +      haveRoundedCorners = aForFrame->GetBorderRadii(radii);
  1.2625 +    } else {
  1.2626 +      haveRoundedCorners = nsIFrame::ComputeBorderRadii(aBorder.mBorderRadius,
  1.2627 +                                   frameSize, aBorderArea.Size(),
  1.2628 +                                   aForFrame->GetSkipSides(), radii);
  1.2629 +    }
  1.2630 +    if (haveRoundedCorners)
  1.2631 +      ComputePixelRadii(radii, appUnitsPerPixel, &bgRadii);
  1.2632 +  }
  1.2633 +
  1.2634 +  // The background is rendered over the 'background-clip' area,
  1.2635 +  // which is normally equal to the border area but may be reduced
  1.2636 +  // to the padding area by CSS.  Also, if the border is solid, we
  1.2637 +  // don't need to draw outside the padding area.  In either case,
  1.2638 +  // if the borders are rounded, make sure we use the same inner
  1.2639 +  // radii as the border code will.
  1.2640 +  // The background-color is drawn based on the bottom
  1.2641 +  // background-clip.
  1.2642 +  const nsStyleBackground *bg = aBackgroundSC->StyleBackground();
  1.2643 +  uint8_t currentBackgroundClip = bg->BottomLayer().mClip;
  1.2644 +  bool isSolidBorder =
  1.2645 +    (aFlags & PAINTBG_WILL_PAINT_BORDER) && IsOpaqueBorder(aBorder);
  1.2646 +  if (isSolidBorder && currentBackgroundClip == NS_STYLE_BG_CLIP_BORDER) {
  1.2647 +    // If we have rounded corners, we need to inflate the background
  1.2648 +    // drawing area a bit to avoid seams between the border and
  1.2649 +    // background.
  1.2650 +    currentBackgroundClip = haveRoundedCorners ?
  1.2651 +      NS_STYLE_BG_CLIP_MOZ_ALMOST_PADDING : NS_STYLE_BG_CLIP_PADDING;
  1.2652 +  }
  1.2653 +
  1.2654 +  BackgroundClipState clipState;
  1.2655 +  GetBackgroundClip(ctx, currentBackgroundClip, bg->BottomLayer().mAttachment,
  1.2656 +                    aForFrame, aBorderArea,
  1.2657 +                    aDirtyRect, haveRoundedCorners, bgRadii, appUnitsPerPixel,
  1.2658 +                    &clipState);
  1.2659 +
  1.2660 +  ctx->SetColor(gfxRGBA(bgColor));
  1.2661 +
  1.2662 +  gfxContextAutoSaveRestore autoSR;
  1.2663 +  DrawBackgroundColor(clipState, ctx, haveRoundedCorners, appUnitsPerPixel);
  1.2664 +}
  1.2665 +
  1.2666 +static inline bool
  1.2667 +IsTransformed(nsIFrame* aForFrame, nsIFrame* aTopFrame)
  1.2668 +{
  1.2669 +  for (nsIFrame* f = aForFrame; f != aTopFrame; f = f->GetParent()) {
  1.2670 +    if (f->IsTransformed()) {
  1.2671 +      return true;
  1.2672 +    }
  1.2673 +  }
  1.2674 +  return false;
  1.2675 +}
  1.2676 +
  1.2677 +nsRect
  1.2678 +nsCSSRendering::ComputeBackgroundPositioningArea(nsPresContext* aPresContext,
  1.2679 +                                                 nsIFrame* aForFrame,
  1.2680 +                                                 const nsRect& aBorderArea,
  1.2681 +                                                 const nsStyleBackground& aBackground,
  1.2682 +                                                 const nsStyleBackground::Layer& aLayer,
  1.2683 +                                                 nsIFrame** aAttachedToFrame)
  1.2684 +{
  1.2685 +  // Compute background origin area relative to aBorderArea now as we may need
  1.2686 +  // it to compute the effective image size for a CSS gradient.
  1.2687 +  nsRect bgPositioningArea(0, 0, 0, 0);
  1.2688 +
  1.2689 +  nsIAtom* frameType = aForFrame->GetType();
  1.2690 +  nsIFrame* geometryFrame = aForFrame;
  1.2691 +  if (frameType == nsGkAtoms::inlineFrame) {
  1.2692 +    // XXXjwalden Strictly speaking this is not quite faithful to how
  1.2693 +    // background-break is supposed to interact with background-origin values,
  1.2694 +    // but it's a non-trivial amount of work to make it fully conformant, and
  1.2695 +    // until the specification is more finalized (and assuming background-break
  1.2696 +    // even makes the cut) it doesn't make sense to hammer out exact behavior.
  1.2697 +    switch (aBackground.mBackgroundInlinePolicy) {
  1.2698 +    case NS_STYLE_BG_INLINE_POLICY_EACH_BOX:
  1.2699 +      bgPositioningArea = nsRect(nsPoint(0,0), aBorderArea.Size());
  1.2700 +      break;
  1.2701 +    case NS_STYLE_BG_INLINE_POLICY_BOUNDING_BOX:
  1.2702 +      bgPositioningArea = gInlineBGData->GetBoundingRect(aForFrame);
  1.2703 +      break;
  1.2704 +    default:
  1.2705 +      NS_ERROR("Unknown background-inline-policy value!  "
  1.2706 +               "Please, teach me what to do.");
  1.2707 +    case NS_STYLE_BG_INLINE_POLICY_CONTINUOUS:
  1.2708 +      bgPositioningArea = gInlineBGData->GetContinuousRect(aForFrame);
  1.2709 +      break;
  1.2710 +    }
  1.2711 +  } else if (frameType == nsGkAtoms::canvasFrame) {
  1.2712 +    geometryFrame = aForFrame->GetFirstPrincipalChild();
  1.2713 +    // geometryFrame might be null if this canvas is a page created
  1.2714 +    // as an overflow container (e.g. the in-flow content has already
  1.2715 +    // finished and this page only displays the continuations of
  1.2716 +    // absolutely positioned content).
  1.2717 +    if (geometryFrame) {
  1.2718 +      bgPositioningArea = geometryFrame->GetRect();
  1.2719 +    }
  1.2720 +  } else if (frameType == nsGkAtoms::scrollFrame &&
  1.2721 +             NS_STYLE_BG_ATTACHMENT_LOCAL == aLayer.mAttachment) {
  1.2722 +    nsIScrollableFrame* scrollableFrame = do_QueryFrame(aForFrame);
  1.2723 +    bgPositioningArea = nsRect(
  1.2724 +      scrollableFrame->GetScrolledFrame()->GetPosition()
  1.2725 +        // For the dir=rtl case:
  1.2726 +        + scrollableFrame->GetScrollRange().TopLeft(),
  1.2727 +      scrollableFrame->GetScrolledRect().Size());
  1.2728 +    // The ScrolledRect’s size does not include the borders or scrollbars,
  1.2729 +    // reverse the handling of background-origin
  1.2730 +    // compared to the common case below.
  1.2731 +    if (aLayer.mOrigin == NS_STYLE_BG_ORIGIN_BORDER) {
  1.2732 +      nsMargin border = geometryFrame->GetUsedBorder();
  1.2733 +      geometryFrame->ApplySkipSides(border);
  1.2734 +      bgPositioningArea.Inflate(border);
  1.2735 +      bgPositioningArea.Inflate(scrollableFrame->GetActualScrollbarSizes());
  1.2736 +    } else if (aLayer.mOrigin != NS_STYLE_BG_ORIGIN_PADDING) {
  1.2737 +      nsMargin padding = geometryFrame->GetUsedPadding();
  1.2738 +      geometryFrame->ApplySkipSides(padding);
  1.2739 +      bgPositioningArea.Deflate(padding);
  1.2740 +      NS_ASSERTION(aLayer.mOrigin == NS_STYLE_BG_ORIGIN_CONTENT,
  1.2741 +                   "unknown background-origin value");
  1.2742 +    }
  1.2743 +    *aAttachedToFrame = aForFrame;
  1.2744 +    return bgPositioningArea;
  1.2745 +  } else {
  1.2746 +    bgPositioningArea = nsRect(nsPoint(0,0), aBorderArea.Size());
  1.2747 +  }
  1.2748 +
  1.2749 +  // Background images are tiled over the 'background-clip' area
  1.2750 +  // but the origin of the tiling is based on the 'background-origin' area
  1.2751 +  if (aLayer.mOrigin != NS_STYLE_BG_ORIGIN_BORDER && geometryFrame) {
  1.2752 +    nsMargin border = geometryFrame->GetUsedBorder();
  1.2753 +    if (aLayer.mOrigin != NS_STYLE_BG_ORIGIN_PADDING) {
  1.2754 +      border += geometryFrame->GetUsedPadding();
  1.2755 +      NS_ASSERTION(aLayer.mOrigin == NS_STYLE_BG_ORIGIN_CONTENT,
  1.2756 +                   "unknown background-origin value");
  1.2757 +    }
  1.2758 +    geometryFrame->ApplySkipSides(border);
  1.2759 +    bgPositioningArea.Deflate(border);
  1.2760 +  }
  1.2761 +
  1.2762 +  nsIFrame* attachedToFrame = aForFrame;
  1.2763 +  if (NS_STYLE_BG_ATTACHMENT_FIXED == aLayer.mAttachment) {
  1.2764 +    // If it's a fixed background attachment, then the image is placed
  1.2765 +    // relative to the viewport, which is the area of the root frame
  1.2766 +    // in a screen context or the page content frame in a print context.
  1.2767 +    attachedToFrame = aPresContext->PresShell()->FrameManager()->GetRootFrame();
  1.2768 +    NS_ASSERTION(attachedToFrame, "no root frame");
  1.2769 +    nsIFrame* pageContentFrame = nullptr;
  1.2770 +    if (aPresContext->IsPaginated()) {
  1.2771 +      pageContentFrame =
  1.2772 +        nsLayoutUtils::GetClosestFrameOfType(aForFrame, nsGkAtoms::pageContentFrame);
  1.2773 +      if (pageContentFrame) {
  1.2774 +        attachedToFrame = pageContentFrame;
  1.2775 +      }
  1.2776 +      // else this is an embedded shell and its root frame is what we want
  1.2777 +    }
  1.2778 +
  1.2779 +    // Set the background positioning area to the viewport's area
  1.2780 +    // (relative to aForFrame)
  1.2781 +    bgPositioningArea =
  1.2782 +      nsRect(-aForFrame->GetOffsetTo(attachedToFrame), attachedToFrame->GetSize());
  1.2783 +
  1.2784 +    if (!pageContentFrame) {
  1.2785 +      // Subtract the size of scrollbars.
  1.2786 +      nsIScrollableFrame* scrollableFrame =
  1.2787 +        aPresContext->PresShell()->GetRootScrollFrameAsScrollable();
  1.2788 +      if (scrollableFrame) {
  1.2789 +        nsMargin scrollbars = scrollableFrame->GetActualScrollbarSizes();
  1.2790 +        bgPositioningArea.Deflate(scrollbars);
  1.2791 +      }
  1.2792 +    }
  1.2793 +  }
  1.2794 +  *aAttachedToFrame = attachedToFrame;
  1.2795 +
  1.2796 +  return bgPositioningArea;
  1.2797 +}
  1.2798 +
  1.2799 +// Apply the CSS image sizing algorithm as it applies to background images.
  1.2800 +// See http://www.w3.org/TR/css3-background/#the-background-size .
  1.2801 +// aIntrinsicSize is the size that the background image 'would like to be'.
  1.2802 +// It can be found by calling nsImageRenderer::ComputeIntrinsicSize.
  1.2803 +static nsSize
  1.2804 +ComputeDrawnSizeForBackground(const CSSSizeOrRatio& aIntrinsicSize,
  1.2805 +                              const nsSize& aBgPositioningArea,
  1.2806 +                              const nsStyleBackground::Size& aLayerSize)
  1.2807 +{
  1.2808 +  // Size is dictated by cover or contain rules.
  1.2809 +  if (aLayerSize.mWidthType == nsStyleBackground::Size::eContain ||
  1.2810 +      aLayerSize.mWidthType == nsStyleBackground::Size::eCover) {
  1.2811 +    nsImageRenderer::FitType fitType =
  1.2812 +      aLayerSize.mWidthType == nsStyleBackground::Size::eCover
  1.2813 +        ? nsImageRenderer::COVER
  1.2814 +        : nsImageRenderer::CONTAIN;
  1.2815 +    return nsImageRenderer::ComputeConstrainedSize(aBgPositioningArea,
  1.2816 +                                                   aIntrinsicSize.mRatio,
  1.2817 +                                                   fitType);
  1.2818 +  }
  1.2819 +
  1.2820 +  // No cover/contain constraint, use default algorithm.
  1.2821 +  CSSSizeOrRatio specifiedSize;
  1.2822 +  if (aLayerSize.mWidthType == nsStyleBackground::Size::eLengthPercentage) {
  1.2823 +    specifiedSize.SetWidth(
  1.2824 +      aLayerSize.ResolveWidthLengthPercentage(aBgPositioningArea));
  1.2825 +  }
  1.2826 +  if (aLayerSize.mHeightType == nsStyleBackground::Size::eLengthPercentage) {
  1.2827 +    specifiedSize.SetHeight(
  1.2828 +      aLayerSize.ResolveHeightLengthPercentage(aBgPositioningArea));
  1.2829 +  }
  1.2830 +
  1.2831 +  return nsImageRenderer::ComputeConcreteSize(specifiedSize,
  1.2832 +                                              aIntrinsicSize,
  1.2833 +                                              aBgPositioningArea);
  1.2834 +}
  1.2835 +
  1.2836 +nsBackgroundLayerState
  1.2837 +nsCSSRendering::PrepareBackgroundLayer(nsPresContext* aPresContext,
  1.2838 +                                       nsIFrame* aForFrame,
  1.2839 +                                       uint32_t aFlags,
  1.2840 +                                       const nsRect& aBorderArea,
  1.2841 +                                       const nsRect& aBGClipRect,
  1.2842 +                                       const nsStyleBackground& aBackground,
  1.2843 +                                       const nsStyleBackground::Layer& aLayer)
  1.2844 +{
  1.2845 +  /*
  1.2846 +   * The background properties we need to keep in mind when drawing background
  1.2847 +   * layers are:
  1.2848 +   *
  1.2849 +   *   background-image
  1.2850 +   *   background-repeat
  1.2851 +   *   background-attachment
  1.2852 +   *   background-position
  1.2853 +   *   background-clip
  1.2854 +   *   background-origin
  1.2855 +   *   background-size
  1.2856 +   *   background-break (-moz-background-inline-policy)
  1.2857 +   *   background-blend-mode
  1.2858 +   *
  1.2859 +   * (background-color applies to the entire element and not to individual
  1.2860 +   * layers, so it is irrelevant to this method.)
  1.2861 +   *
  1.2862 +   * These properties have the following dependencies upon each other when
  1.2863 +   * determining rendering:
  1.2864 +   *
  1.2865 +   *   background-image
  1.2866 +   *     no dependencies
  1.2867 +   *   background-repeat
  1.2868 +   *     no dependencies
  1.2869 +   *   background-attachment
  1.2870 +   *     no dependencies
  1.2871 +   *   background-position
  1.2872 +   *     depends upon background-size (for the image's scaled size) and
  1.2873 +   *     background-break (for the background positioning area)
  1.2874 +   *   background-clip
  1.2875 +   *     no dependencies
  1.2876 +   *   background-origin
  1.2877 +   *     depends upon background-attachment (only in the case where that value
  1.2878 +   *     is 'fixed')
  1.2879 +   *   background-size
  1.2880 +   *     depends upon background-break (for the background positioning area for
  1.2881 +   *     resolving percentages), background-image (for the image's intrinsic
  1.2882 +   *     size), background-repeat (if that value is 'round'), and
  1.2883 +   *     background-origin (for the background painting area, when
  1.2884 +   *     background-repeat is 'round')
  1.2885 +   *   background-break
  1.2886 +   *     depends upon background-origin (specifying how the boxes making up the
  1.2887 +   *     background positioning area are determined)
  1.2888 +   *
  1.2889 +   * As a result of only-if dependencies we don't strictly do a topological
  1.2890 +   * sort of the above properties when processing, but it's pretty close to one:
  1.2891 +   *
  1.2892 +   *   background-clip (by caller)
  1.2893 +   *   background-image
  1.2894 +   *   background-break, background-origin
  1.2895 +   *   background-attachment (postfix for background-{origin,break} if 'fixed')
  1.2896 +   *   background-size
  1.2897 +   *   background-position
  1.2898 +   *   background-repeat
  1.2899 +   */
  1.2900 +
  1.2901 +  uint32_t irFlags = 0;
  1.2902 +  if (aFlags & nsCSSRendering::PAINTBG_SYNC_DECODE_IMAGES) {
  1.2903 +    irFlags |= nsImageRenderer::FLAG_SYNC_DECODE_IMAGES;
  1.2904 +  }
  1.2905 +  if (aFlags & nsCSSRendering::PAINTBG_TO_WINDOW) {
  1.2906 +    irFlags |= nsImageRenderer::FLAG_PAINTING_TO_WINDOW;
  1.2907 +  }
  1.2908 +
  1.2909 +  nsBackgroundLayerState state(aForFrame, &aLayer.mImage, irFlags);
  1.2910 +  if (!state.mImageRenderer.PrepareImage()) {
  1.2911 +    // There's no image or it's not ready to be painted.
  1.2912 +    return state;
  1.2913 +  }
  1.2914 +
  1.2915 +  // The frame to which the background is attached
  1.2916 +  nsIFrame* attachedToFrame = aForFrame;
  1.2917 +  // Compute background origin area relative to aBorderArea now as we may need
  1.2918 +  // it to compute the effective image size for a CSS gradient.
  1.2919 +  nsRect bgPositioningArea =
  1.2920 +    ComputeBackgroundPositioningArea(aPresContext, aForFrame, aBorderArea,
  1.2921 +                                     aBackground, aLayer, &attachedToFrame);
  1.2922 +
  1.2923 +  // For background-attachment:fixed backgrounds, we'll limit the area
  1.2924 +  // where the background can be drawn to the viewport.
  1.2925 +  nsRect bgClipRect = aBGClipRect;
  1.2926 +
  1.2927 +  // Compute the anchor point.
  1.2928 +  //
  1.2929 +  // relative to aBorderArea.TopLeft() (which is where the top-left
  1.2930 +  // of aForFrame's border-box will be rendered)
  1.2931 +  nsPoint imageTopLeft;
  1.2932 +  if (NS_STYLE_BG_ATTACHMENT_FIXED == aLayer.mAttachment) {
  1.2933 +    if ((aFlags & nsCSSRendering::PAINTBG_TO_WINDOW) &&
  1.2934 +        !IsTransformed(aForFrame, attachedToFrame)) {
  1.2935 +      // Clip background-attachment:fixed backgrounds to the viewport, if we're
  1.2936 +      // painting to the screen and not transformed. This avoids triggering
  1.2937 +      // tiling in common cases, without affecting output since drawing is
  1.2938 +      // always clipped to the viewport when we draw to the screen. (But it's
  1.2939 +      // not a pure optimization since it can affect the values of pixels at the
  1.2940 +      // edge of the viewport --- whether they're sampled from a putative "next
  1.2941 +      // tile" or not.)
  1.2942 +      bgClipRect.IntersectRect(bgClipRect, bgPositioningArea + aBorderArea.TopLeft());
  1.2943 +    }
  1.2944 +  }
  1.2945 +
  1.2946 +  // Scale the image as specified for background-size and as required for
  1.2947 +  // proper background positioning when background-position is defined with
  1.2948 +  // percentages.
  1.2949 +  CSSSizeOrRatio intrinsicSize = state.mImageRenderer.ComputeIntrinsicSize();
  1.2950 +  nsSize bgPositionSize = bgPositioningArea.Size();
  1.2951 +  nsSize imageSize = ComputeDrawnSizeForBackground(intrinsicSize,
  1.2952 +                                                   bgPositionSize,
  1.2953 +                                                   aLayer.mSize);
  1.2954 +  if (imageSize.width <= 0 || imageSize.height <= 0)
  1.2955 +    return state;
  1.2956 +
  1.2957 +  state.mImageRenderer.SetPreferredSize(intrinsicSize,
  1.2958 +                                        imageSize);
  1.2959 +
  1.2960 +  // Compute the position of the background now that the background's size is
  1.2961 +  // determined.
  1.2962 +  ComputeBackgroundAnchorPoint(aLayer, bgPositionSize, imageSize,
  1.2963 +                               &imageTopLeft, &state.mAnchor);
  1.2964 +  imageTopLeft += bgPositioningArea.TopLeft();
  1.2965 +  state.mAnchor += bgPositioningArea.TopLeft();
  1.2966 +
  1.2967 +  state.mDestArea = nsRect(imageTopLeft + aBorderArea.TopLeft(), imageSize);
  1.2968 +  state.mFillArea = state.mDestArea;
  1.2969 +  int repeatX = aLayer.mRepeat.mXRepeat;
  1.2970 +  int repeatY = aLayer.mRepeat.mYRepeat;
  1.2971 +  if (repeatX == NS_STYLE_BG_REPEAT_REPEAT) {
  1.2972 +    state.mFillArea.x = bgClipRect.x;
  1.2973 +    state.mFillArea.width = bgClipRect.width;
  1.2974 +  }
  1.2975 +  if (repeatY == NS_STYLE_BG_REPEAT_REPEAT) {
  1.2976 +    state.mFillArea.y = bgClipRect.y;
  1.2977 +    state.mFillArea.height = bgClipRect.height;
  1.2978 +  }
  1.2979 +  state.mFillArea.IntersectRect(state.mFillArea, bgClipRect);
  1.2980 +
  1.2981 +  state.mCompositingOp = GetGFXBlendMode(aLayer.mBlendMode);
  1.2982 +
  1.2983 +  return state;
  1.2984 +}
  1.2985 +
  1.2986 +nsRect
  1.2987 +nsCSSRendering::GetBackgroundLayerRect(nsPresContext* aPresContext,
  1.2988 +                                       nsIFrame* aForFrame,
  1.2989 +                                       const nsRect& aBorderArea,
  1.2990 +                                       const nsRect& aClipRect,
  1.2991 +                                       const nsStyleBackground& aBackground,
  1.2992 +                                       const nsStyleBackground::Layer& aLayer,
  1.2993 +                                       uint32_t aFlags)
  1.2994 +{
  1.2995 +  nsBackgroundLayerState state =
  1.2996 +      PrepareBackgroundLayer(aPresContext, aForFrame, aFlags, aBorderArea,
  1.2997 +                             aClipRect, aBackground, aLayer);
  1.2998 +  return state.mFillArea;
  1.2999 +}
  1.3000 +
  1.3001 +/* static */ bool
  1.3002 +nsCSSRendering::IsBackgroundImageDecodedForStyleContextAndLayer(
  1.3003 +  const nsStyleBackground *aBackground, uint32_t aLayer)
  1.3004 +{
  1.3005 +  const nsStyleImage* image = &aBackground->mLayers[aLayer].mImage;
  1.3006 +  if (image->GetType() == eStyleImageType_Image) {
  1.3007 +    nsCOMPtr<imgIContainer> img;
  1.3008 +    if (NS_SUCCEEDED(image->GetImageData()->GetImage(getter_AddRefs(img)))) {
  1.3009 +      if (!img->IsDecoded()) {
  1.3010 +        return false;
  1.3011 +      }
  1.3012 +    }
  1.3013 +  }
  1.3014 +  return true;
  1.3015 +}
  1.3016 +
  1.3017 +/* static */ bool
  1.3018 +nsCSSRendering::AreAllBackgroundImagesDecodedForFrame(nsIFrame* aFrame)
  1.3019 +{
  1.3020 +  const nsStyleBackground *bg = aFrame->StyleContext()->StyleBackground();
  1.3021 +  NS_FOR_VISIBLE_BACKGROUND_LAYERS_BACK_TO_FRONT(i, bg) {
  1.3022 +    if (!IsBackgroundImageDecodedForStyleContextAndLayer(bg, i)) {
  1.3023 +      return false;
  1.3024 +    }
  1.3025 +  }
  1.3026 +  return true;
  1.3027 +}
  1.3028 +
  1.3029 +static void
  1.3030 +DrawBorderImage(nsPresContext*       aPresContext,
  1.3031 +                nsRenderingContext&  aRenderingContext,
  1.3032 +                nsIFrame*            aForFrame,
  1.3033 +                const nsRect&        aBorderArea,
  1.3034 +                const nsStyleBorder& aStyleBorder,
  1.3035 +                const nsRect&        aDirtyRect)
  1.3036 +{
  1.3037 +  NS_PRECONDITION(aStyleBorder.IsBorderImageLoaded(),
  1.3038 +                  "drawing border image that isn't successfully loaded");
  1.3039 +
  1.3040 +  if (aDirtyRect.IsEmpty())
  1.3041 +    return;
  1.3042 +
  1.3043 +  nsImageRenderer renderer(aForFrame, &aStyleBorder.mBorderImageSource, 0);
  1.3044 +
  1.3045 +  // Ensure we get invalidated for loads and animations of the image.
  1.3046 +  // We need to do this here because this might be the only code that
  1.3047 +  // knows about the association of the style data with the frame.
  1.3048 +  // XXX We shouldn't really... since if anybody is passing in a
  1.3049 +  // different style, they'll potentially have the wrong size for the
  1.3050 +  // border too.
  1.3051 +  aForFrame->AssociateImage(aStyleBorder.mBorderImageSource, aPresContext);
  1.3052 +
  1.3053 +  if (!renderer.PrepareImage()) {
  1.3054 +    return;
  1.3055 +  }
  1.3056 +
  1.3057 +  // Determine the border image area, which by default corresponds to the
  1.3058 +  // border box but can be modified by 'border-image-outset'.
  1.3059 +  nsRect borderImgArea(aBorderArea);
  1.3060 +  borderImgArea.Inflate(aStyleBorder.GetImageOutset());
  1.3061 +
  1.3062 +  // Calculate the image size used to compute slice points.
  1.3063 +  CSSSizeOrRatio intrinsicSize = renderer.ComputeIntrinsicSize();
  1.3064 +  nsSize imageSize = nsImageRenderer::ComputeConcreteSize(CSSSizeOrRatio(),
  1.3065 +                                                          intrinsicSize,
  1.3066 +                                                          borderImgArea.Size());
  1.3067 +  renderer.SetPreferredSize(intrinsicSize, imageSize);
  1.3068 +
  1.3069 +  // Compute the used values of 'border-image-slice' and 'border-image-width';
  1.3070 +  // we do them together because the latter can depend on the former.
  1.3071 +  nsMargin slice;
  1.3072 +  nsMargin border;
  1.3073 +  NS_FOR_CSS_SIDES(s) {
  1.3074 +    nsStyleCoord coord = aStyleBorder.mBorderImageSlice.Get(s);
  1.3075 +    int32_t imgDimension = NS_SIDE_IS_VERTICAL(s)
  1.3076 +                           ? imageSize.width : imageSize.height;
  1.3077 +    nscoord borderDimension = NS_SIDE_IS_VERTICAL(s)
  1.3078 +                           ? borderImgArea.width : borderImgArea.height;
  1.3079 +    double value;
  1.3080 +    switch (coord.GetUnit()) {
  1.3081 +      case eStyleUnit_Percent:
  1.3082 +        value = coord.GetPercentValue() * imgDimension;
  1.3083 +        break;
  1.3084 +      case eStyleUnit_Factor:
  1.3085 +        value = nsPresContext::CSSPixelsToAppUnits(
  1.3086 +          NS_lround(coord.GetFactorValue()));
  1.3087 +        break;
  1.3088 +      default:
  1.3089 +        NS_NOTREACHED("unexpected CSS unit for image slice");
  1.3090 +        value = 0;
  1.3091 +        break;
  1.3092 +    }
  1.3093 +    if (value < 0)
  1.3094 +      value = 0;
  1.3095 +    if (value > imgDimension)
  1.3096 +      value = imgDimension;
  1.3097 +    slice.Side(s) = value;
  1.3098 +
  1.3099 +    nsMargin borderWidths(aStyleBorder.GetComputedBorder());
  1.3100 +    coord = aStyleBorder.mBorderImageWidth.Get(s);
  1.3101 +    switch (coord.GetUnit()) {
  1.3102 +      case eStyleUnit_Coord: // absolute dimension
  1.3103 +        value = coord.GetCoordValue();
  1.3104 +        break;
  1.3105 +      case eStyleUnit_Percent:
  1.3106 +        value = coord.GetPercentValue() * borderDimension;
  1.3107 +        break;
  1.3108 +      case eStyleUnit_Factor:
  1.3109 +        value = coord.GetFactorValue() * borderWidths.Side(s);
  1.3110 +        break;
  1.3111 +      case eStyleUnit_Auto:  // same as the slice value, in CSS pixels
  1.3112 +        value = slice.Side(s);
  1.3113 +        break;
  1.3114 +      default:
  1.3115 +        NS_NOTREACHED("unexpected CSS unit for border image area division");
  1.3116 +        value = 0;
  1.3117 +        break;
  1.3118 +    }
  1.3119 +    // NSToCoordRoundWithClamp rounds towards infinity, but that's OK
  1.3120 +    // because we expect value to be non-negative.
  1.3121 +    MOZ_ASSERT(value >= 0);
  1.3122 +    border.Side(s) = NSToCoordRoundWithClamp(value);
  1.3123 +    MOZ_ASSERT(border.Side(s) >= 0);
  1.3124 +  }
  1.3125 +
  1.3126 +  // "If two opposite border-image-width offsets are large enough that they
  1.3127 +  // overlap, their used values are proportionately reduced until they no
  1.3128 +  // longer overlap."
  1.3129 +  uint32_t combinedBorderWidth = uint32_t(border.left) +
  1.3130 +                                 uint32_t(border.right);
  1.3131 +  double scaleX = combinedBorderWidth > uint32_t(borderImgArea.width)
  1.3132 +                  ? borderImgArea.width / double(combinedBorderWidth)
  1.3133 +                  : 1.0;
  1.3134 +  uint32_t combinedBorderHeight = uint32_t(border.top) +
  1.3135 +                                  uint32_t(border.bottom);
  1.3136 +  double scaleY = combinedBorderHeight > uint32_t(borderImgArea.height)
  1.3137 +                  ? borderImgArea.height / double(combinedBorderHeight)
  1.3138 +                  : 1.0;
  1.3139 +  double scale = std::min(scaleX, scaleY);
  1.3140 +  if (scale < 1.0) {
  1.3141 +    border.left *= scale;
  1.3142 +    border.right *= scale;
  1.3143 +    border.top *= scale;
  1.3144 +    border.bottom *= scale;
  1.3145 +    NS_ASSERTION(border.left + border.right <= borderImgArea.width &&
  1.3146 +                 border.top + border.bottom <= borderImgArea.height,
  1.3147 +                 "rounding error in width reduction???");
  1.3148 +  }
  1.3149 +
  1.3150 +  // These helper tables recharacterize the 'slice' and 'width' margins
  1.3151 +  // in a more convenient form: they are the x/y/width/height coords
  1.3152 +  // required for various bands of the border, and they have been transformed
  1.3153 +  // to be relative to the innerRect (for 'slice') or the page (for 'border').
  1.3154 +  enum {
  1.3155 +    LEFT, MIDDLE, RIGHT,
  1.3156 +    TOP = LEFT, BOTTOM = RIGHT
  1.3157 +  };
  1.3158 +  const nscoord borderX[3] = {
  1.3159 +    borderImgArea.x + 0,
  1.3160 +    borderImgArea.x + border.left,
  1.3161 +    borderImgArea.x + borderImgArea.width - border.right,
  1.3162 +  };
  1.3163 +  const nscoord borderY[3] = {
  1.3164 +    borderImgArea.y + 0,
  1.3165 +    borderImgArea.y + border.top,
  1.3166 +    borderImgArea.y + borderImgArea.height - border.bottom,
  1.3167 +  };
  1.3168 +  const nscoord borderWidth[3] = {
  1.3169 +    border.left,
  1.3170 +    borderImgArea.width - border.left - border.right,
  1.3171 +    border.right,
  1.3172 +  };
  1.3173 +  const nscoord borderHeight[3] = {
  1.3174 +    border.top,
  1.3175 +    borderImgArea.height - border.top - border.bottom,
  1.3176 +    border.bottom,
  1.3177 +  };
  1.3178 +  const int32_t sliceX[3] = {
  1.3179 +    0,
  1.3180 +    slice.left,
  1.3181 +    imageSize.width - slice.right,
  1.3182 +  };
  1.3183 +  const int32_t sliceY[3] = {
  1.3184 +    0,
  1.3185 +    slice.top,
  1.3186 +    imageSize.height - slice.bottom,
  1.3187 +  };
  1.3188 +  const int32_t sliceWidth[3] = {
  1.3189 +    slice.left,
  1.3190 +    std::max(imageSize.width - slice.left - slice.right, 0),
  1.3191 +    slice.right,
  1.3192 +  };
  1.3193 +  const int32_t sliceHeight[3] = {
  1.3194 +    slice.top,
  1.3195 +    std::max(imageSize.height - slice.top - slice.bottom, 0),
  1.3196 +    slice.bottom,
  1.3197 +  };
  1.3198 +
  1.3199 +  for (int i = LEFT; i <= RIGHT; i++) {
  1.3200 +    for (int j = TOP; j <= BOTTOM; j++) {
  1.3201 +      uint8_t fillStyleH, fillStyleV;
  1.3202 +      nsSize unitSize;
  1.3203 +
  1.3204 +      if (i == MIDDLE && j == MIDDLE) {
  1.3205 +        // Discard the middle portion unless set to fill.
  1.3206 +        if (NS_STYLE_BORDER_IMAGE_SLICE_NOFILL ==
  1.3207 +            aStyleBorder.mBorderImageFill) {
  1.3208 +          continue;
  1.3209 +        }
  1.3210 +
  1.3211 +        NS_ASSERTION(NS_STYLE_BORDER_IMAGE_SLICE_FILL ==
  1.3212 +                     aStyleBorder.mBorderImageFill,
  1.3213 +                     "Unexpected border image fill");
  1.3214 +
  1.3215 +        // css-background:
  1.3216 +        //     The middle image's width is scaled by the same factor as the
  1.3217 +        //     top image unless that factor is zero or infinity, in which
  1.3218 +        //     case the scaling factor of the bottom is substituted, and
  1.3219 +        //     failing that, the width is not scaled. The height of the
  1.3220 +        //     middle image is scaled by the same factor as the left image
  1.3221 +        //     unless that factor is zero or infinity, in which case the
  1.3222 +        //     scaling factor of the right image is substituted, and failing
  1.3223 +        //     that, the height is not scaled.
  1.3224 +        gfxFloat hFactor, vFactor;
  1.3225 +
  1.3226 +        if (0 < border.left && 0 < slice.left)
  1.3227 +          vFactor = gfxFloat(border.left)/slice.left;
  1.3228 +        else if (0 < border.right && 0 < slice.right)
  1.3229 +          vFactor = gfxFloat(border.right)/slice.right;
  1.3230 +        else
  1.3231 +          vFactor = 1;
  1.3232 +
  1.3233 +        if (0 < border.top && 0 < slice.top)
  1.3234 +          hFactor = gfxFloat(border.top)/slice.top;
  1.3235 +        else if (0 < border.bottom && 0 < slice.bottom)
  1.3236 +          hFactor = gfxFloat(border.bottom)/slice.bottom;
  1.3237 +        else
  1.3238 +          hFactor = 1;
  1.3239 +
  1.3240 +        unitSize.width = sliceWidth[i]*hFactor;
  1.3241 +        unitSize.height = sliceHeight[j]*vFactor;
  1.3242 +        fillStyleH = aStyleBorder.mBorderImageRepeatH;
  1.3243 +        fillStyleV = aStyleBorder.mBorderImageRepeatV;
  1.3244 +
  1.3245 +      } else if (i == MIDDLE) { // top, bottom
  1.3246 +        // Sides are always stretched to the thickness of their border,
  1.3247 +        // and stretched proportionately on the other axis.
  1.3248 +        gfxFloat factor;
  1.3249 +        if (0 < borderHeight[j] && 0 < sliceHeight[j])
  1.3250 +          factor = gfxFloat(borderHeight[j])/sliceHeight[j];
  1.3251 +        else
  1.3252 +          factor = 1;
  1.3253 +
  1.3254 +        unitSize.width = sliceWidth[i]*factor;
  1.3255 +        unitSize.height = borderHeight[j];
  1.3256 +        fillStyleH = aStyleBorder.mBorderImageRepeatH;
  1.3257 +        fillStyleV = NS_STYLE_BORDER_IMAGE_REPEAT_STRETCH;
  1.3258 +
  1.3259 +      } else if (j == MIDDLE) { // left, right
  1.3260 +        gfxFloat factor;
  1.3261 +        if (0 < borderWidth[i] && 0 < sliceWidth[i])
  1.3262 +          factor = gfxFloat(borderWidth[i])/sliceWidth[i];
  1.3263 +        else
  1.3264 +          factor = 1;
  1.3265 +
  1.3266 +        unitSize.width = borderWidth[i];
  1.3267 +        unitSize.height = sliceHeight[j]*factor;
  1.3268 +        fillStyleH = NS_STYLE_BORDER_IMAGE_REPEAT_STRETCH;
  1.3269 +        fillStyleV = aStyleBorder.mBorderImageRepeatV;
  1.3270 +
  1.3271 +      } else {
  1.3272 +        // Corners are always stretched to fit the corner.
  1.3273 +        unitSize.width = borderWidth[i];
  1.3274 +        unitSize.height = borderHeight[j];
  1.3275 +        fillStyleH = NS_STYLE_BORDER_IMAGE_REPEAT_STRETCH;
  1.3276 +        fillStyleV = NS_STYLE_BORDER_IMAGE_REPEAT_STRETCH;
  1.3277 +      }
  1.3278 +
  1.3279 +      nsRect destArea(borderX[i], borderY[j], borderWidth[i], borderHeight[j]);
  1.3280 +      nsRect subArea(sliceX[i], sliceY[j], sliceWidth[i], sliceHeight[j]);
  1.3281 +      nsIntRect intSubArea = subArea.ToOutsidePixels(nsPresContext::AppUnitsPerCSSPixel());
  1.3282 +
  1.3283 +      renderer.DrawBorderImageComponent(aPresContext,
  1.3284 +                                        aRenderingContext, aDirtyRect,
  1.3285 +                                        destArea, CSSIntRect(intSubArea.x,
  1.3286 +                                                             intSubArea.y,
  1.3287 +                                                             intSubArea.width,
  1.3288 +                                                             intSubArea.height),
  1.3289 +                                        fillStyleH, fillStyleV,
  1.3290 +                                        unitSize, j * (RIGHT + 1) + i);
  1.3291 +    }
  1.3292 +  }
  1.3293 +}
  1.3294 +
  1.3295 +// Begin table border-collapsing section
  1.3296 +// These functions were written to not disrupt the normal ones and yet satisfy some additional requirements
  1.3297 +// At some point, all functions should be unified to include the additional functionality that these provide
  1.3298 +
  1.3299 +static nscoord
  1.3300 +RoundIntToPixel(nscoord aValue,
  1.3301 +                nscoord aTwipsPerPixel,
  1.3302 +                bool    aRoundDown = false)
  1.3303 +{
  1.3304 +  if (aTwipsPerPixel <= 0)
  1.3305 +    // We must be rendering to a device that has a resolution greater than Twips!
  1.3306 +    // In that case, aValue is as accurate as it's going to get.
  1.3307 +    return aValue;
  1.3308 +
  1.3309 +  nscoord halfPixel = NSToCoordRound(aTwipsPerPixel / 2.0f);
  1.3310 +  nscoord extra = aValue % aTwipsPerPixel;
  1.3311 +  nscoord finalValue = (!aRoundDown && (extra >= halfPixel)) ? aValue + (aTwipsPerPixel - extra) : aValue - extra;
  1.3312 +  return finalValue;
  1.3313 +}
  1.3314 +
  1.3315 +static nscoord
  1.3316 +RoundFloatToPixel(float   aValue,
  1.3317 +                  nscoord aTwipsPerPixel,
  1.3318 +                  bool    aRoundDown = false)
  1.3319 +{
  1.3320 +  return RoundIntToPixel(NSToCoordRound(aValue), aTwipsPerPixel, aRoundDown);
  1.3321 +}
  1.3322 +
  1.3323 +static void
  1.3324 +SetPoly(const nsRect& aRect,
  1.3325 +        nsPoint*      poly)
  1.3326 +{
  1.3327 +  poly[0].x = aRect.x;
  1.3328 +  poly[0].y = aRect.y;
  1.3329 +  poly[1].x = aRect.x + aRect.width;
  1.3330 +  poly[1].y = aRect.y;
  1.3331 +  poly[2].x = aRect.x + aRect.width;
  1.3332 +  poly[2].y = aRect.y + aRect.height;
  1.3333 +  poly[3].x = aRect.x;
  1.3334 +  poly[3].y = aRect.y + aRect.height;
  1.3335 +  poly[4].x = aRect.x;
  1.3336 +  poly[4].y = aRect.y;
  1.3337 +}
  1.3338 +
  1.3339 +static void
  1.3340 +DrawSolidBorderSegment(nsRenderingContext& aContext,
  1.3341 +                       nsRect               aRect,
  1.3342 +                       nscoord              aTwipsPerPixel,
  1.3343 +                       uint8_t              aStartBevelSide = 0,
  1.3344 +                       nscoord              aStartBevelOffset = 0,
  1.3345 +                       uint8_t              aEndBevelSide = 0,
  1.3346 +                       nscoord              aEndBevelOffset = 0)
  1.3347 +{
  1.3348 +
  1.3349 +  if ((aRect.width == aTwipsPerPixel) || (aRect.height == aTwipsPerPixel) ||
  1.3350 +      ((0 == aStartBevelOffset) && (0 == aEndBevelOffset))) {
  1.3351 +    // simple line or rectangle
  1.3352 +    if ((NS_SIDE_TOP == aStartBevelSide) || (NS_SIDE_BOTTOM == aStartBevelSide)) {
  1.3353 +      if (1 == aRect.height)
  1.3354 +        aContext.DrawLine(aRect.TopLeft(), aRect.BottomLeft());
  1.3355 +      else
  1.3356 +        aContext.FillRect(aRect);
  1.3357 +    }
  1.3358 +    else {
  1.3359 +      if (1 == aRect.width)
  1.3360 +        aContext.DrawLine(aRect.TopLeft(), aRect.TopRight());
  1.3361 +      else
  1.3362 +        aContext.FillRect(aRect);
  1.3363 +    }
  1.3364 +  }
  1.3365 +  else {
  1.3366 +    // polygon with beveling
  1.3367 +    nsPoint poly[5];
  1.3368 +    SetPoly(aRect, poly);
  1.3369 +    switch(aStartBevelSide) {
  1.3370 +    case NS_SIDE_TOP:
  1.3371 +      poly[0].x += aStartBevelOffset;
  1.3372 +      poly[4].x = poly[0].x;
  1.3373 +      break;
  1.3374 +    case NS_SIDE_BOTTOM:
  1.3375 +      poly[3].x += aStartBevelOffset;
  1.3376 +      break;
  1.3377 +    case NS_SIDE_RIGHT:
  1.3378 +      poly[1].y += aStartBevelOffset;
  1.3379 +      break;
  1.3380 +    case NS_SIDE_LEFT:
  1.3381 +      poly[0].y += aStartBevelOffset;
  1.3382 +      poly[4].y = poly[0].y;
  1.3383 +    }
  1.3384 +
  1.3385 +    switch(aEndBevelSide) {
  1.3386 +    case NS_SIDE_TOP:
  1.3387 +      poly[1].x -= aEndBevelOffset;
  1.3388 +      break;
  1.3389 +    case NS_SIDE_BOTTOM:
  1.3390 +      poly[2].x -= aEndBevelOffset;
  1.3391 +      break;
  1.3392 +    case NS_SIDE_RIGHT:
  1.3393 +      poly[2].y -= aEndBevelOffset;
  1.3394 +      break;
  1.3395 +    case NS_SIDE_LEFT:
  1.3396 +      poly[3].y -= aEndBevelOffset;
  1.3397 +    }
  1.3398 +
  1.3399 +    aContext.FillPolygon(poly, 5);
  1.3400 +  }
  1.3401 +
  1.3402 +
  1.3403 +}
  1.3404 +
  1.3405 +static void
  1.3406 +GetDashInfo(nscoord  aBorderLength,
  1.3407 +            nscoord  aDashLength,
  1.3408 +            nscoord  aTwipsPerPixel,
  1.3409 +            int32_t& aNumDashSpaces,
  1.3410 +            nscoord& aStartDashLength,
  1.3411 +            nscoord& aEndDashLength)
  1.3412 +{
  1.3413 +  aNumDashSpaces = 0;
  1.3414 +  if (aStartDashLength + aDashLength + aEndDashLength >= aBorderLength) {
  1.3415 +    aStartDashLength = aBorderLength;
  1.3416 +    aEndDashLength = 0;
  1.3417 +  }
  1.3418 +  else {
  1.3419 +    aNumDashSpaces = (aBorderLength - aDashLength)/ (2 * aDashLength); // round down
  1.3420 +    nscoord extra = aBorderLength - aStartDashLength - aEndDashLength - (((2 * aNumDashSpaces) - 1) * aDashLength);
  1.3421 +    if (extra > 0) {
  1.3422 +      nscoord half = RoundIntToPixel(extra / 2, aTwipsPerPixel);
  1.3423 +      aStartDashLength += half;
  1.3424 +      aEndDashLength += (extra - half);
  1.3425 +    }
  1.3426 +  }
  1.3427 +}
  1.3428 +
  1.3429 +void
  1.3430 +nsCSSRendering::DrawTableBorderSegment(nsRenderingContext&     aContext,
  1.3431 +                                       uint8_t                  aBorderStyle,
  1.3432 +                                       nscolor                  aBorderColor,
  1.3433 +                                       const nsStyleBackground* aBGColor,
  1.3434 +                                       const nsRect&            aBorder,
  1.3435 +                                       int32_t                  aAppUnitsPerCSSPixel,
  1.3436 +                                       uint8_t                  aStartBevelSide,
  1.3437 +                                       nscoord                  aStartBevelOffset,
  1.3438 +                                       uint8_t                  aEndBevelSide,
  1.3439 +                                       nscoord                  aEndBevelOffset)
  1.3440 +{
  1.3441 +  aContext.SetColor (aBorderColor);
  1.3442 +
  1.3443 +  bool horizontal = ((NS_SIDE_TOP == aStartBevelSide) || (NS_SIDE_BOTTOM == aStartBevelSide));
  1.3444 +  nscoord twipsPerPixel = NSIntPixelsToAppUnits(1, aAppUnitsPerCSSPixel);
  1.3445 +  uint8_t ridgeGroove = NS_STYLE_BORDER_STYLE_RIDGE;
  1.3446 +
  1.3447 +  if ((twipsPerPixel >= aBorder.width) || (twipsPerPixel >= aBorder.height) ||
  1.3448 +      (NS_STYLE_BORDER_STYLE_DASHED == aBorderStyle) || (NS_STYLE_BORDER_STYLE_DOTTED == aBorderStyle)) {
  1.3449 +    // no beveling for 1 pixel border, dash or dot
  1.3450 +    aStartBevelOffset = 0;
  1.3451 +    aEndBevelOffset = 0;
  1.3452 +  }
  1.3453 +
  1.3454 +  gfxContext *ctx = aContext.ThebesContext();
  1.3455 +  gfxContext::AntialiasMode oldMode = ctx->CurrentAntialiasMode();
  1.3456 +  ctx->SetAntialiasMode(gfxContext::MODE_ALIASED);
  1.3457 +
  1.3458 +  switch (aBorderStyle) {
  1.3459 +  case NS_STYLE_BORDER_STYLE_NONE:
  1.3460 +  case NS_STYLE_BORDER_STYLE_HIDDEN:
  1.3461 +    //NS_ASSERTION(false, "style of none or hidden");
  1.3462 +    break;
  1.3463 +  case NS_STYLE_BORDER_STYLE_DOTTED:
  1.3464 +  case NS_STYLE_BORDER_STYLE_DASHED:
  1.3465 +    {
  1.3466 +      nscoord dashLength = (NS_STYLE_BORDER_STYLE_DASHED == aBorderStyle) ? DASH_LENGTH : DOT_LENGTH;
  1.3467 +      // make the dash length proportional to the border thickness
  1.3468 +      dashLength *= (horizontal) ? aBorder.height : aBorder.width;
  1.3469 +      // make the min dash length for the ends 1/2 the dash length
  1.3470 +      nscoord minDashLength = (NS_STYLE_BORDER_STYLE_DASHED == aBorderStyle)
  1.3471 +                              ? RoundFloatToPixel(((float)dashLength) / 2.0f, twipsPerPixel) : dashLength;
  1.3472 +      minDashLength = std::max(minDashLength, twipsPerPixel);
  1.3473 +      nscoord numDashSpaces = 0;
  1.3474 +      nscoord startDashLength = minDashLength;
  1.3475 +      nscoord endDashLength   = minDashLength;
  1.3476 +      if (horizontal) {
  1.3477 +        GetDashInfo(aBorder.width, dashLength, twipsPerPixel, numDashSpaces, startDashLength, endDashLength);
  1.3478 +        nsRect rect(aBorder.x, aBorder.y, startDashLength, aBorder.height);
  1.3479 +        DrawSolidBorderSegment(aContext, rect, twipsPerPixel);
  1.3480 +        for (int32_t spaceX = 0; spaceX < numDashSpaces; spaceX++) {
  1.3481 +          rect.x += rect.width + dashLength;
  1.3482 +          rect.width = (spaceX == (numDashSpaces - 1)) ? endDashLength : dashLength;
  1.3483 +          DrawSolidBorderSegment(aContext, rect, twipsPerPixel);
  1.3484 +        }
  1.3485 +      }
  1.3486 +      else {
  1.3487 +        GetDashInfo(aBorder.height, dashLength, twipsPerPixel, numDashSpaces, startDashLength, endDashLength);
  1.3488 +        nsRect rect(aBorder.x, aBorder.y, aBorder.width, startDashLength);
  1.3489 +        DrawSolidBorderSegment(aContext, rect, twipsPerPixel);
  1.3490 +        for (int32_t spaceY = 0; spaceY < numDashSpaces; spaceY++) {
  1.3491 +          rect.y += rect.height + dashLength;
  1.3492 +          rect.height = (spaceY == (numDashSpaces - 1)) ? endDashLength : dashLength;
  1.3493 +          DrawSolidBorderSegment(aContext, rect, twipsPerPixel);
  1.3494 +        }
  1.3495 +      }
  1.3496 +    }
  1.3497 +    break;
  1.3498 +  case NS_STYLE_BORDER_STYLE_GROOVE:
  1.3499 +    ridgeGroove = NS_STYLE_BORDER_STYLE_GROOVE; // and fall through to ridge
  1.3500 +  case NS_STYLE_BORDER_STYLE_RIDGE:
  1.3501 +    if ((horizontal && (twipsPerPixel >= aBorder.height)) ||
  1.3502 +        (!horizontal && (twipsPerPixel >= aBorder.width))) {
  1.3503 +      // a one pixel border
  1.3504 +      DrawSolidBorderSegment(aContext, aBorder, twipsPerPixel, aStartBevelSide, aStartBevelOffset,
  1.3505 +                             aEndBevelSide, aEndBevelOffset);
  1.3506 +    }
  1.3507 +    else {
  1.3508 +      nscoord startBevel = (aStartBevelOffset > 0)
  1.3509 +                            ? RoundFloatToPixel(0.5f * (float)aStartBevelOffset, twipsPerPixel, true) : 0;
  1.3510 +      nscoord endBevel =   (aEndBevelOffset > 0)
  1.3511 +                            ? RoundFloatToPixel(0.5f * (float)aEndBevelOffset, twipsPerPixel, true) : 0;
  1.3512 +      mozilla::css::Side ridgeGrooveSide = (horizontal) ? NS_SIDE_TOP : NS_SIDE_LEFT;
  1.3513 +      // FIXME: In theory, this should use the visited-dependent
  1.3514 +      // background color, but I don't care.
  1.3515 +      aContext.SetColor (
  1.3516 +        MakeBevelColor(ridgeGrooveSide, ridgeGroove, aBGColor->mBackgroundColor, aBorderColor));
  1.3517 +      nsRect rect(aBorder);
  1.3518 +      nscoord half;
  1.3519 +      if (horizontal) { // top, bottom
  1.3520 +        half = RoundFloatToPixel(0.5f * (float)aBorder.height, twipsPerPixel);
  1.3521 +        rect.height = half;
  1.3522 +        if (NS_SIDE_TOP == aStartBevelSide) {
  1.3523 +          rect.x += startBevel;
  1.3524 +          rect.width -= startBevel;
  1.3525 +        }
  1.3526 +        if (NS_SIDE_TOP == aEndBevelSide) {
  1.3527 +          rect.width -= endBevel;
  1.3528 +        }
  1.3529 +        DrawSolidBorderSegment(aContext, rect, twipsPerPixel, aStartBevelSide,
  1.3530 +                               startBevel, aEndBevelSide, endBevel);
  1.3531 +      }
  1.3532 +      else { // left, right
  1.3533 +        half = RoundFloatToPixel(0.5f * (float)aBorder.width, twipsPerPixel);
  1.3534 +        rect.width = half;
  1.3535 +        if (NS_SIDE_LEFT == aStartBevelSide) {
  1.3536 +          rect.y += startBevel;
  1.3537 +          rect.height -= startBevel;
  1.3538 +        }
  1.3539 +        if (NS_SIDE_LEFT == aEndBevelSide) {
  1.3540 +          rect.height -= endBevel;
  1.3541 +        }
  1.3542 +        DrawSolidBorderSegment(aContext, rect, twipsPerPixel, aStartBevelSide,
  1.3543 +                               startBevel, aEndBevelSide, endBevel);
  1.3544 +      }
  1.3545 +
  1.3546 +      rect = aBorder;
  1.3547 +      ridgeGrooveSide = (NS_SIDE_TOP == ridgeGrooveSide) ? NS_SIDE_BOTTOM : NS_SIDE_RIGHT;
  1.3548 +      // FIXME: In theory, this should use the visited-dependent
  1.3549 +      // background color, but I don't care.
  1.3550 +      aContext.SetColor (
  1.3551 +        MakeBevelColor(ridgeGrooveSide, ridgeGroove, aBGColor->mBackgroundColor, aBorderColor));
  1.3552 +      if (horizontal) {
  1.3553 +        rect.y = rect.y + half;
  1.3554 +        rect.height = aBorder.height - half;
  1.3555 +        if (NS_SIDE_BOTTOM == aStartBevelSide) {
  1.3556 +          rect.x += startBevel;
  1.3557 +          rect.width -= startBevel;
  1.3558 +        }
  1.3559 +        if (NS_SIDE_BOTTOM == aEndBevelSide) {
  1.3560 +          rect.width -= endBevel;
  1.3561 +        }
  1.3562 +        DrawSolidBorderSegment(aContext, rect, twipsPerPixel, aStartBevelSide,
  1.3563 +                               startBevel, aEndBevelSide, endBevel);
  1.3564 +      }
  1.3565 +      else {
  1.3566 +        rect.x = rect.x + half;
  1.3567 +        rect.width = aBorder.width - half;
  1.3568 +        if (NS_SIDE_RIGHT == aStartBevelSide) {
  1.3569 +          rect.y += aStartBevelOffset - startBevel;
  1.3570 +          rect.height -= startBevel;
  1.3571 +        }
  1.3572 +        if (NS_SIDE_RIGHT == aEndBevelSide) {
  1.3573 +          rect.height -= endBevel;
  1.3574 +        }
  1.3575 +        DrawSolidBorderSegment(aContext, rect, twipsPerPixel, aStartBevelSide,
  1.3576 +                               startBevel, aEndBevelSide, endBevel);
  1.3577 +      }
  1.3578 +    }
  1.3579 +    break;
  1.3580 +  case NS_STYLE_BORDER_STYLE_DOUBLE:
  1.3581 +    // We can only do "double" borders if the thickness of the border
  1.3582 +    // is more than 2px.  Otherwise, we fall through to painting a
  1.3583 +    // solid border.
  1.3584 +    if ((aBorder.width > 2*twipsPerPixel || horizontal) &&
  1.3585 +        (aBorder.height > 2*twipsPerPixel || !horizontal)) {
  1.3586 +      nscoord startBevel = (aStartBevelOffset > 0)
  1.3587 +                            ? RoundFloatToPixel(0.333333f * (float)aStartBevelOffset, twipsPerPixel) : 0;
  1.3588 +      nscoord endBevel =   (aEndBevelOffset > 0)
  1.3589 +                            ? RoundFloatToPixel(0.333333f * (float)aEndBevelOffset, twipsPerPixel) : 0;
  1.3590 +      if (horizontal) { // top, bottom
  1.3591 +        nscoord thirdHeight = RoundFloatToPixel(0.333333f * (float)aBorder.height, twipsPerPixel);
  1.3592 +
  1.3593 +        // draw the top line or rect
  1.3594 +        nsRect topRect(aBorder.x, aBorder.y, aBorder.width, thirdHeight);
  1.3595 +        if (NS_SIDE_TOP == aStartBevelSide) {
  1.3596 +          topRect.x += aStartBevelOffset - startBevel;
  1.3597 +          topRect.width -= aStartBevelOffset - startBevel;
  1.3598 +        }
  1.3599 +        if (NS_SIDE_TOP == aEndBevelSide) {
  1.3600 +          topRect.width -= aEndBevelOffset - endBevel;
  1.3601 +        }
  1.3602 +        DrawSolidBorderSegment(aContext, topRect, twipsPerPixel, aStartBevelSide,
  1.3603 +                               startBevel, aEndBevelSide, endBevel);
  1.3604 +
  1.3605 +        // draw the botom line or rect
  1.3606 +        nscoord heightOffset = aBorder.height - thirdHeight;
  1.3607 +        nsRect bottomRect(aBorder.x, aBorder.y + heightOffset, aBorder.width, aBorder.height - heightOffset);
  1.3608 +        if (NS_SIDE_BOTTOM == aStartBevelSide) {
  1.3609 +          bottomRect.x += aStartBevelOffset - startBevel;
  1.3610 +          bottomRect.width -= aStartBevelOffset - startBevel;
  1.3611 +        }
  1.3612 +        if (NS_SIDE_BOTTOM == aEndBevelSide) {
  1.3613 +          bottomRect.width -= aEndBevelOffset - endBevel;
  1.3614 +        }
  1.3615 +        DrawSolidBorderSegment(aContext, bottomRect, twipsPerPixel, aStartBevelSide,
  1.3616 +                               startBevel, aEndBevelSide, endBevel);
  1.3617 +      }
  1.3618 +      else { // left, right
  1.3619 +        nscoord thirdWidth = RoundFloatToPixel(0.333333f * (float)aBorder.width, twipsPerPixel);
  1.3620 +
  1.3621 +        nsRect leftRect(aBorder.x, aBorder.y, thirdWidth, aBorder.height);
  1.3622 +        if (NS_SIDE_LEFT == aStartBevelSide) {
  1.3623 +          leftRect.y += aStartBevelOffset - startBevel;
  1.3624 +          leftRect.height -= aStartBevelOffset - startBevel;
  1.3625 +        }
  1.3626 +        if (NS_SIDE_LEFT == aEndBevelSide) {
  1.3627 +          leftRect.height -= aEndBevelOffset - endBevel;
  1.3628 +        }
  1.3629 +        DrawSolidBorderSegment(aContext, leftRect, twipsPerPixel, aStartBevelSide,
  1.3630 +                               startBevel, aEndBevelSide, endBevel);
  1.3631 +
  1.3632 +        nscoord widthOffset = aBorder.width - thirdWidth;
  1.3633 +        nsRect rightRect(aBorder.x + widthOffset, aBorder.y, aBorder.width - widthOffset, aBorder.height);
  1.3634 +        if (NS_SIDE_RIGHT == aStartBevelSide) {
  1.3635 +          rightRect.y += aStartBevelOffset - startBevel;
  1.3636 +          rightRect.height -= aStartBevelOffset - startBevel;
  1.3637 +        }
  1.3638 +        if (NS_SIDE_RIGHT == aEndBevelSide) {
  1.3639 +          rightRect.height -= aEndBevelOffset - endBevel;
  1.3640 +        }
  1.3641 +        DrawSolidBorderSegment(aContext, rightRect, twipsPerPixel, aStartBevelSide,
  1.3642 +                               startBevel, aEndBevelSide, endBevel);
  1.3643 +      }
  1.3644 +      break;
  1.3645 +    }
  1.3646 +    // else fall through to solid
  1.3647 +  case NS_STYLE_BORDER_STYLE_SOLID:
  1.3648 +    DrawSolidBorderSegment(aContext, aBorder, twipsPerPixel, aStartBevelSide,
  1.3649 +                           aStartBevelOffset, aEndBevelSide, aEndBevelOffset);
  1.3650 +    break;
  1.3651 +  case NS_STYLE_BORDER_STYLE_OUTSET:
  1.3652 +  case NS_STYLE_BORDER_STYLE_INSET:
  1.3653 +    NS_ASSERTION(false, "inset, outset should have been converted to groove, ridge");
  1.3654 +    break;
  1.3655 +  case NS_STYLE_BORDER_STYLE_AUTO:
  1.3656 +    NS_ASSERTION(false, "Unexpected 'auto' table border");
  1.3657 +    break;
  1.3658 +  }
  1.3659 +
  1.3660 +  ctx->SetAntialiasMode(oldMode);
  1.3661 +}
  1.3662 +
  1.3663 +// End table border-collapsing section
  1.3664 +
  1.3665 +gfxRect
  1.3666 +nsCSSRendering::ExpandPaintingRectForDecorationLine(nsIFrame* aFrame,
  1.3667 +                                                    const uint8_t aStyle,
  1.3668 +                                                    const gfxRect& aClippedRect,
  1.3669 +                                                    const gfxFloat aXInFrame,
  1.3670 +                                                    const gfxFloat aCycleLength)
  1.3671 +{
  1.3672 +  switch (aStyle) {
  1.3673 +    case NS_STYLE_TEXT_DECORATION_STYLE_DOTTED:
  1.3674 +    case NS_STYLE_TEXT_DECORATION_STYLE_DASHED:
  1.3675 +    case NS_STYLE_TEXT_DECORATION_STYLE_WAVY:
  1.3676 +      break;
  1.3677 +    default:
  1.3678 +      NS_ERROR("Invalid style was specified");
  1.3679 +      return aClippedRect;
  1.3680 +  }
  1.3681 +
  1.3682 +  nsBlockFrame* block = nullptr;
  1.3683 +  // Note that when we paint the decoration lines in relative positioned
  1.3684 +  // box, we should paint them like all of the boxes are positioned as static.
  1.3685 +  nscoord frameXInBlockAppUnits = 0;
  1.3686 +  for (nsIFrame* f = aFrame; f; f = f->GetParent()) {
  1.3687 +    block = do_QueryFrame(f);
  1.3688 +    if (block) {
  1.3689 +      break;
  1.3690 +    }
  1.3691 +    frameXInBlockAppUnits += f->GetNormalPosition().x;
  1.3692 +  }
  1.3693 +
  1.3694 +  NS_ENSURE_TRUE(block, aClippedRect);
  1.3695 +
  1.3696 +  nsPresContext *pc = aFrame->PresContext();
  1.3697 +  gfxFloat frameXInBlock = pc->AppUnitsToGfxUnits(frameXInBlockAppUnits);
  1.3698 +  int32_t rectXInBlock = int32_t(NS_round(frameXInBlock + aXInFrame));
  1.3699 +  int32_t extraLeft =
  1.3700 +    rectXInBlock - (rectXInBlock / int32_t(aCycleLength) * aCycleLength);
  1.3701 +  gfxRect rect(aClippedRect);
  1.3702 +  rect.x -= extraLeft;
  1.3703 +  rect.width += extraLeft;
  1.3704 +  return rect;
  1.3705 +}
  1.3706 +
  1.3707 +void
  1.3708 +nsCSSRendering::PaintDecorationLine(nsIFrame* aFrame,
  1.3709 +                                    gfxContext* aGfxContext,
  1.3710 +                                    const gfxRect& aDirtyRect,
  1.3711 +                                    const nscolor aColor,
  1.3712 +                                    const gfxPoint& aPt,
  1.3713 +                                    const gfxFloat aXInFrame,
  1.3714 +                                    const gfxSize& aLineSize,
  1.3715 +                                    const gfxFloat aAscent,
  1.3716 +                                    const gfxFloat aOffset,
  1.3717 +                                    const uint8_t aDecoration,
  1.3718 +                                    const uint8_t aStyle,
  1.3719 +                                    const gfxFloat aDescentLimit)
  1.3720 +{
  1.3721 +  NS_ASSERTION(aStyle != NS_STYLE_TEXT_DECORATION_STYLE_NONE, "aStyle is none");
  1.3722 +
  1.3723 +  gfxRect rect =
  1.3724 +    GetTextDecorationRectInternal(aPt, aLineSize, aAscent, aOffset,
  1.3725 +                                  aDecoration, aStyle, aDescentLimit);
  1.3726 +  if (rect.IsEmpty() || !rect.Intersects(aDirtyRect)) {
  1.3727 +    return;
  1.3728 +  }
  1.3729 +
  1.3730 +  if (aDecoration != NS_STYLE_TEXT_DECORATION_LINE_UNDERLINE &&
  1.3731 +      aDecoration != NS_STYLE_TEXT_DECORATION_LINE_OVERLINE &&
  1.3732 +      aDecoration != NS_STYLE_TEXT_DECORATION_LINE_LINE_THROUGH) {
  1.3733 +    NS_ERROR("Invalid decoration value!");
  1.3734 +    return;
  1.3735 +  }
  1.3736 +
  1.3737 +  gfxFloat lineHeight = std::max(NS_round(aLineSize.height), 1.0);
  1.3738 +  bool contextIsSaved = false;
  1.3739 +
  1.3740 +  gfxFloat oldLineWidth;
  1.3741 +  nsRefPtr<gfxPattern> oldPattern;
  1.3742 +
  1.3743 +  switch (aStyle) {
  1.3744 +    case NS_STYLE_TEXT_DECORATION_STYLE_SOLID:
  1.3745 +    case NS_STYLE_TEXT_DECORATION_STYLE_DOUBLE:
  1.3746 +      oldLineWidth = aGfxContext->CurrentLineWidth();
  1.3747 +      oldPattern = aGfxContext->GetPattern();
  1.3748 +      break;
  1.3749 +    case NS_STYLE_TEXT_DECORATION_STYLE_DASHED: {
  1.3750 +      aGfxContext->Save();
  1.3751 +      contextIsSaved = true;
  1.3752 +      aGfxContext->Clip(rect);
  1.3753 +      gfxFloat dashWidth = lineHeight * DOT_LENGTH * DASH_LENGTH;
  1.3754 +      gfxFloat dash[2] = { dashWidth, dashWidth };
  1.3755 +      aGfxContext->SetLineCap(gfxContext::LINE_CAP_BUTT);
  1.3756 +      aGfxContext->SetDash(dash, 2, 0.0);
  1.3757 +      rect = ExpandPaintingRectForDecorationLine(aFrame, aStyle, rect,
  1.3758 +                                                 aXInFrame, dashWidth * 2);
  1.3759 +      // We should continue to draw the last dash even if it is not in the rect.
  1.3760 +      rect.width += dashWidth;
  1.3761 +      break;
  1.3762 +    }
  1.3763 +    case NS_STYLE_TEXT_DECORATION_STYLE_DOTTED: {
  1.3764 +      aGfxContext->Save();
  1.3765 +      contextIsSaved = true;
  1.3766 +      aGfxContext->Clip(rect);
  1.3767 +      gfxFloat dashWidth = lineHeight * DOT_LENGTH;
  1.3768 +      gfxFloat dash[2];
  1.3769 +      if (lineHeight > 2.0) {
  1.3770 +        dash[0] = 0.0;
  1.3771 +        dash[1] = dashWidth * 2.0;
  1.3772 +        aGfxContext->SetLineCap(gfxContext::LINE_CAP_ROUND);
  1.3773 +      } else {
  1.3774 +        dash[0] = dashWidth;
  1.3775 +        dash[1] = dashWidth;
  1.3776 +      }
  1.3777 +      aGfxContext->SetDash(dash, 2, 0.0);
  1.3778 +      rect = ExpandPaintingRectForDecorationLine(aFrame, aStyle, rect,
  1.3779 +                                                 aXInFrame, dashWidth * 2);
  1.3780 +      // We should continue to draw the last dot even if it is not in the rect.
  1.3781 +      rect.width += dashWidth;
  1.3782 +      break;
  1.3783 +    }
  1.3784 +    case NS_STYLE_TEXT_DECORATION_STYLE_WAVY:
  1.3785 +      aGfxContext->Save();
  1.3786 +      contextIsSaved = true;
  1.3787 +      aGfxContext->Clip(rect);
  1.3788 +      if (lineHeight > 2.0) {
  1.3789 +        aGfxContext->SetAntialiasMode(gfxContext::MODE_COVERAGE);
  1.3790 +      } else {
  1.3791 +        // Don't use anti-aliasing here.  Because looks like lighter color wavy
  1.3792 +        // line at this case.  And probably, users don't think the
  1.3793 +        // non-anti-aliased wavy line is not pretty.
  1.3794 +        aGfxContext->SetAntialiasMode(gfxContext::MODE_ALIASED);
  1.3795 +      }
  1.3796 +      break;
  1.3797 +    default:
  1.3798 +      NS_ERROR("Invalid style value!");
  1.3799 +      return;
  1.3800 +  }
  1.3801 +
  1.3802 +  // The y position should be set to the middle of the line.
  1.3803 +  rect.y += lineHeight / 2;
  1.3804 +
  1.3805 +  aGfxContext->SetColor(gfxRGBA(aColor));
  1.3806 +  aGfxContext->SetLineWidth(lineHeight);
  1.3807 +  switch (aStyle) {
  1.3808 +    case NS_STYLE_TEXT_DECORATION_STYLE_SOLID:
  1.3809 +      aGfxContext->NewPath();
  1.3810 +      aGfxContext->MoveTo(rect.TopLeft());
  1.3811 +      aGfxContext->LineTo(rect.TopRight());
  1.3812 +      aGfxContext->Stroke();
  1.3813 +      break;
  1.3814 +    case NS_STYLE_TEXT_DECORATION_STYLE_DOUBLE:
  1.3815 +      /**
  1.3816 +       *  We are drawing double line as:
  1.3817 +       *
  1.3818 +       * +-------------------------------------------+
  1.3819 +       * |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| ^
  1.3820 +       * |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| | lineHeight
  1.3821 +       * |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| v
  1.3822 +       * |                                           |
  1.3823 +       * |                                           |
  1.3824 +       * |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| ^
  1.3825 +       * |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| | lineHeight
  1.3826 +       * |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| v
  1.3827 +       * +-------------------------------------------+
  1.3828 +       */
  1.3829 +      aGfxContext->NewPath();
  1.3830 +      aGfxContext->MoveTo(rect.TopLeft());
  1.3831 +      aGfxContext->LineTo(rect.TopRight());
  1.3832 +      rect.height -= lineHeight;
  1.3833 +      aGfxContext->MoveTo(rect.BottomLeft());
  1.3834 +      aGfxContext->LineTo(rect.BottomRight());
  1.3835 +      aGfxContext->Stroke();
  1.3836 +      break;
  1.3837 +    case NS_STYLE_TEXT_DECORATION_STYLE_DOTTED:
  1.3838 +    case NS_STYLE_TEXT_DECORATION_STYLE_DASHED:
  1.3839 +      aGfxContext->NewPath();
  1.3840 +      aGfxContext->MoveTo(rect.TopLeft());
  1.3841 +      aGfxContext->LineTo(rect.TopRight());
  1.3842 +      aGfxContext->Stroke();
  1.3843 +      break;
  1.3844 +    case NS_STYLE_TEXT_DECORATION_STYLE_WAVY: {
  1.3845 +      /**
  1.3846 +       *  We are drawing wavy line as:
  1.3847 +       *
  1.3848 +       *  P: Path, X: Painted pixel
  1.3849 +       *
  1.3850 +       *     +---------------------------------------+
  1.3851 +       *   XX|X            XXXXXX            XXXXXX  |
  1.3852 +       *   PP|PX          XPPPPPPX          XPPPPPPX |    ^
  1.3853 +       *   XX|XPX        XPXXXXXXPX        XPXXXXXXPX|    |
  1.3854 +       *     | XPX      XPX      XPX      XPX      XP|X   |adv
  1.3855 +       *     |  XPXXXXXXPX        XPXXXXXXPX        X|PX  |
  1.3856 +       *     |   XPPPPPPX          XPPPPPPX          |XPX v
  1.3857 +       *     |    XXXXXX            XXXXXX           | XX
  1.3858 +       *     +---------------------------------------+
  1.3859 +       *      <---><--->                                ^
  1.3860 +       *      adv  flatLengthAtVertex                   rightMost
  1.3861 +       *
  1.3862 +       *  1. Always starts from top-left of the drawing area, however, we need
  1.3863 +       *     to draw  the line from outside of the rect.  Because the start
  1.3864 +       *     point of the line is not good style if we draw from inside it.
  1.3865 +       *  2. First, draw horizontal line from outside the rect to top-left of
  1.3866 +       *     the rect;
  1.3867 +       *  3. Goes down to bottom of the area at 45 degrees.
  1.3868 +       *  4. Slides to right horizontaly, see |flatLengthAtVertex|.
  1.3869 +       *  5. Goes up to top of the area at 45 degrees.
  1.3870 +       *  6. Slides to right horizontaly.
  1.3871 +       *  7. Repeat from 2 until reached to right-most edge of the area.
  1.3872 +       */
  1.3873 +
  1.3874 +      gfxFloat adv = rect.Height() - lineHeight;
  1.3875 +      gfxFloat flatLengthAtVertex = std::max((lineHeight - 1.0) * 2.0, 1.0);
  1.3876 +
  1.3877 +      // Align the start of wavy lines to the nearest ancestor block.
  1.3878 +      gfxFloat cycleLength = 2 * (adv + flatLengthAtVertex);
  1.3879 +      rect = ExpandPaintingRectForDecorationLine(aFrame, aStyle, rect,
  1.3880 +                                                 aXInFrame, cycleLength);
  1.3881 +      // figure out if we can trim whole cycles from the left and right edges
  1.3882 +      // of the line, to try and avoid creating an unnecessarily long and
  1.3883 +      // complex path
  1.3884 +      int32_t skipCycles = floor((aDirtyRect.x - rect.x) / cycleLength);
  1.3885 +      if (skipCycles > 0) {
  1.3886 +        rect.x += skipCycles * cycleLength;
  1.3887 +        rect.width -= skipCycles * cycleLength;
  1.3888 +      }
  1.3889 +
  1.3890 +      rect.x += lineHeight / 2.0;
  1.3891 +      gfxPoint pt(rect.TopLeft());
  1.3892 +      gfxFloat rightMost = pt.x + rect.Width() + lineHeight;
  1.3893 +
  1.3894 +      skipCycles = floor((rightMost - aDirtyRect.XMost()) / cycleLength);
  1.3895 +      if (skipCycles > 0) {
  1.3896 +        rightMost -= skipCycles * cycleLength;
  1.3897 +      }
  1.3898 +
  1.3899 +      aGfxContext->NewPath();
  1.3900 +
  1.3901 +      pt.x -= lineHeight;
  1.3902 +      aGfxContext->MoveTo(pt); // 1
  1.3903 +
  1.3904 +      pt.x = rect.X();
  1.3905 +      aGfxContext->LineTo(pt); // 2
  1.3906 +
  1.3907 +      bool goDown = true;
  1.3908 +      uint32_t iter = 0;
  1.3909 +      while (pt.x < rightMost) {
  1.3910 +        if (++iter > 1000) {
  1.3911 +          // stroke the current path and start again, to avoid pathological
  1.3912 +          // behavior in cairo with huge numbers of path segments
  1.3913 +          aGfxContext->Stroke();
  1.3914 +          aGfxContext->NewPath();
  1.3915 +          aGfxContext->MoveTo(pt);
  1.3916 +          iter = 0;
  1.3917 +        }
  1.3918 +        pt.x += adv;
  1.3919 +        pt.y += goDown ? adv : -adv;
  1.3920 +
  1.3921 +        aGfxContext->LineTo(pt); // 3 and 5
  1.3922 +
  1.3923 +        pt.x += flatLengthAtVertex;
  1.3924 +        aGfxContext->LineTo(pt); // 4 and 6
  1.3925 +
  1.3926 +        goDown = !goDown;
  1.3927 +      }
  1.3928 +      aGfxContext->Stroke();
  1.3929 +      break;
  1.3930 +    }
  1.3931 +    default:
  1.3932 +      NS_ERROR("Invalid style value!");
  1.3933 +      break;
  1.3934 +  }
  1.3935 +
  1.3936 +  if (contextIsSaved) {
  1.3937 +    aGfxContext->Restore();
  1.3938 +  } else {
  1.3939 +    aGfxContext->SetPattern(oldPattern);
  1.3940 +    aGfxContext->SetLineWidth(oldLineWidth);
  1.3941 +  }
  1.3942 +}
  1.3943 +
  1.3944 +void
  1.3945 +nsCSSRendering::DecorationLineToPath(nsIFrame* aFrame,
  1.3946 +                                     gfxContext* aGfxContext,
  1.3947 +                                     const gfxRect& aDirtyRect,
  1.3948 +                                     const nscolor aColor,
  1.3949 +                                     const gfxPoint& aPt,
  1.3950 +                                     const gfxFloat aXInFrame,
  1.3951 +                                     const gfxSize& aLineSize,
  1.3952 +                                     const gfxFloat aAscent,
  1.3953 +                                     const gfxFloat aOffset,
  1.3954 +                                     const uint8_t aDecoration,
  1.3955 +                                     const uint8_t aStyle,
  1.3956 +                                     const gfxFloat aDescentLimit)
  1.3957 +{
  1.3958 +  NS_ASSERTION(aStyle != NS_STYLE_TEXT_DECORATION_STYLE_NONE, "aStyle is none");
  1.3959 +
  1.3960 +  aGfxContext->NewPath();
  1.3961 +
  1.3962 +  gfxRect rect =
  1.3963 +    GetTextDecorationRectInternal(aPt, aLineSize, aAscent, aOffset,
  1.3964 +                                  aDecoration, aStyle, aDescentLimit);
  1.3965 +  if (rect.IsEmpty() || !rect.Intersects(aDirtyRect)) {
  1.3966 +    return;
  1.3967 +  }
  1.3968 +
  1.3969 +  if (aDecoration != NS_STYLE_TEXT_DECORATION_LINE_UNDERLINE &&
  1.3970 +      aDecoration != NS_STYLE_TEXT_DECORATION_LINE_OVERLINE &&
  1.3971 +      aDecoration != NS_STYLE_TEXT_DECORATION_LINE_LINE_THROUGH) {
  1.3972 +    NS_ERROR("Invalid decoration value!");
  1.3973 +    return;
  1.3974 +  }
  1.3975 +
  1.3976 +  if (aStyle != NS_STYLE_TEXT_DECORATION_STYLE_SOLID) {
  1.3977 +    // For the moment, we support only solid text decorations.
  1.3978 +    return;
  1.3979 +  }
  1.3980 +
  1.3981 +  gfxFloat lineHeight = std::max(NS_round(aLineSize.height), 1.0);
  1.3982 +
  1.3983 +  // The y position should be set to the middle of the line.
  1.3984 +  rect.y += lineHeight / 2;
  1.3985 +
  1.3986 +  aGfxContext->Rectangle
  1.3987 +    (gfxRect(gfxPoint(rect.TopLeft() - gfxPoint(0.0, lineHeight / 2)),
  1.3988 +             gfxSize(rect.Width(), lineHeight)));
  1.3989 +}
  1.3990 +
  1.3991 +nsRect
  1.3992 +nsCSSRendering::GetTextDecorationRect(nsPresContext* aPresContext,
  1.3993 +                                      const gfxSize& aLineSize,
  1.3994 +                                      const gfxFloat aAscent,
  1.3995 +                                      const gfxFloat aOffset,
  1.3996 +                                      const uint8_t aDecoration,
  1.3997 +                                      const uint8_t aStyle,
  1.3998 +                                      const gfxFloat aDescentLimit)
  1.3999 +{
  1.4000 +  NS_ASSERTION(aPresContext, "aPresContext is null");
  1.4001 +  NS_ASSERTION(aStyle != NS_STYLE_TEXT_DECORATION_STYLE_NONE, "aStyle is none");
  1.4002 +
  1.4003 +  gfxRect rect =
  1.4004 +    GetTextDecorationRectInternal(gfxPoint(0, 0), aLineSize, aAscent, aOffset,
  1.4005 +                                  aDecoration, aStyle, aDescentLimit);
  1.4006 +  // The rect values are already rounded to nearest device pixels.
  1.4007 +  nsRect r;
  1.4008 +  r.x = aPresContext->GfxUnitsToAppUnits(rect.X());
  1.4009 +  r.y = aPresContext->GfxUnitsToAppUnits(rect.Y());
  1.4010 +  r.width = aPresContext->GfxUnitsToAppUnits(rect.Width());
  1.4011 +  r.height = aPresContext->GfxUnitsToAppUnits(rect.Height());
  1.4012 +  return r;
  1.4013 +}
  1.4014 +
  1.4015 +gfxRect
  1.4016 +nsCSSRendering::GetTextDecorationRectInternal(const gfxPoint& aPt,
  1.4017 +                                              const gfxSize& aLineSize,
  1.4018 +                                              const gfxFloat aAscent,
  1.4019 +                                              const gfxFloat aOffset,
  1.4020 +                                              const uint8_t aDecoration,
  1.4021 +                                              const uint8_t aStyle,
  1.4022 +                                              const gfxFloat aDescentLimit)
  1.4023 +{
  1.4024 +  NS_ASSERTION(aStyle <= NS_STYLE_TEXT_DECORATION_STYLE_WAVY,
  1.4025 +               "Invalid aStyle value");
  1.4026 +
  1.4027 +  if (aStyle == NS_STYLE_TEXT_DECORATION_STYLE_NONE)
  1.4028 +    return gfxRect(0, 0, 0, 0);
  1.4029 +
  1.4030 +  bool canLiftUnderline = aDescentLimit >= 0.0;
  1.4031 +
  1.4032 +  const gfxFloat left  = floor(aPt.x + 0.5),
  1.4033 +                 right = floor(aPt.x + aLineSize.width + 0.5);
  1.4034 +  gfxRect r(left, 0, right - left, 0);
  1.4035 +
  1.4036 +  gfxFloat lineHeight = NS_round(aLineSize.height);
  1.4037 +  lineHeight = std::max(lineHeight, 1.0);
  1.4038 +
  1.4039 +  gfxFloat ascent = NS_round(aAscent);
  1.4040 +  gfxFloat descentLimit = floor(aDescentLimit);
  1.4041 +
  1.4042 +  gfxFloat suggestedMaxRectHeight = std::max(std::min(ascent, descentLimit), 1.0);
  1.4043 +  r.height = lineHeight;
  1.4044 +  if (aStyle == NS_STYLE_TEXT_DECORATION_STYLE_DOUBLE) {
  1.4045 +    /**
  1.4046 +     *  We will draw double line as:
  1.4047 +     *
  1.4048 +     * +-------------------------------------------+
  1.4049 +     * |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| ^
  1.4050 +     * |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| | lineHeight
  1.4051 +     * |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| v
  1.4052 +     * |                                           | ^
  1.4053 +     * |                                           | | gap
  1.4054 +     * |                                           | v
  1.4055 +     * |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| ^
  1.4056 +     * |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| | lineHeight
  1.4057 +     * |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| v
  1.4058 +     * +-------------------------------------------+
  1.4059 +     */
  1.4060 +    gfxFloat gap = NS_round(lineHeight / 2.0);
  1.4061 +    gap = std::max(gap, 1.0);
  1.4062 +    r.height = lineHeight * 2.0 + gap;
  1.4063 +    if (canLiftUnderline) {
  1.4064 +      if (r.Height() > suggestedMaxRectHeight) {
  1.4065 +        // Don't shrink the line height, because the thickness has some meaning.
  1.4066 +        // We can just shrink the gap at this time.
  1.4067 +        r.height = std::max(suggestedMaxRectHeight, lineHeight * 2.0 + 1.0);
  1.4068 +      }
  1.4069 +    }
  1.4070 +  } else if (aStyle == NS_STYLE_TEXT_DECORATION_STYLE_WAVY) {
  1.4071 +    /**
  1.4072 +     *  We will draw wavy line as:
  1.4073 +     *
  1.4074 +     * +-------------------------------------------+
  1.4075 +     * |XXXXX            XXXXXX            XXXXXX  | ^
  1.4076 +     * |XXXXXX          XXXXXXXX          XXXXXXXX | | lineHeight
  1.4077 +     * |XXXXXXX        XXXXXXXXXX        XXXXXXXXXX| v
  1.4078 +     * |     XXX      XXX      XXX      XXX      XX|
  1.4079 +     * |      XXXXXXXXXX        XXXXXXXXXX        X|
  1.4080 +     * |       XXXXXXXX          XXXXXXXX          |
  1.4081 +     * |        XXXXXX            XXXXXX           |
  1.4082 +     * +-------------------------------------------+
  1.4083 +     */
  1.4084 +    r.height = lineHeight > 2.0 ? lineHeight * 4.0 : lineHeight * 3.0;
  1.4085 +    if (canLiftUnderline) {
  1.4086 +      if (r.Height() > suggestedMaxRectHeight) {
  1.4087 +        // Don't shrink the line height even if there is not enough space,
  1.4088 +        // because the thickness has some meaning.  E.g., the 1px wavy line and
  1.4089 +        // 2px wavy line can be used for different meaning in IME selections
  1.4090 +        // at same time.
  1.4091 +        r.height = std::max(suggestedMaxRectHeight, lineHeight * 2.0);
  1.4092 +      }
  1.4093 +    }
  1.4094 +  }
  1.4095 +
  1.4096 +  gfxFloat baseline = floor(aPt.y + aAscent + 0.5);
  1.4097 +  gfxFloat offset = 0.0;
  1.4098 +  switch (aDecoration) {
  1.4099 +    case NS_STYLE_TEXT_DECORATION_LINE_UNDERLINE:
  1.4100 +      offset = aOffset;
  1.4101 +      if (canLiftUnderline) {
  1.4102 +        if (descentLimit < -offset + r.Height()) {
  1.4103 +          // If we can ignore the offset and the decoration line is overflowing,
  1.4104 +          // we should align the bottom edge of the decoration line rect if it's
  1.4105 +          // possible.  Otherwise, we should lift up the top edge of the rect as
  1.4106 +          // far as possible.
  1.4107 +          gfxFloat offsetBottomAligned = -descentLimit + r.Height();
  1.4108 +          gfxFloat offsetTopAligned = 0.0;
  1.4109 +          offset = std::min(offsetBottomAligned, offsetTopAligned);
  1.4110 +        }
  1.4111 +      }
  1.4112 +      break;
  1.4113 +    case NS_STYLE_TEXT_DECORATION_LINE_OVERLINE:
  1.4114 +      offset = aOffset - lineHeight + r.Height();
  1.4115 +      break;
  1.4116 +    case NS_STYLE_TEXT_DECORATION_LINE_LINE_THROUGH: {
  1.4117 +      gfxFloat extra = floor(r.Height() / 2.0 + 0.5);
  1.4118 +      extra = std::max(extra, lineHeight);
  1.4119 +      offset = aOffset - lineHeight + extra;
  1.4120 +      break;
  1.4121 +    }
  1.4122 +    default:
  1.4123 +      NS_ERROR("Invalid decoration value!");
  1.4124 +  }
  1.4125 +  r.y = baseline - floor(offset + 0.5);
  1.4126 +  return r;
  1.4127 +}
  1.4128 +
  1.4129 +// ------------------
  1.4130 +// ImageRenderer
  1.4131 +// ------------------
  1.4132 +nsImageRenderer::nsImageRenderer(nsIFrame* aForFrame,
  1.4133 +                                 const nsStyleImage* aImage,
  1.4134 +                                 uint32_t aFlags)
  1.4135 +  : mForFrame(aForFrame)
  1.4136 +  , mImage(aImage)
  1.4137 +  , mType(aImage->GetType())
  1.4138 +  , mImageContainer(nullptr)
  1.4139 +  , mGradientData(nullptr)
  1.4140 +  , mPaintServerFrame(nullptr)
  1.4141 +  , mIsReady(false)
  1.4142 +  , mSize(0, 0)
  1.4143 +  , mFlags(aFlags)
  1.4144 +{
  1.4145 +}
  1.4146 +
  1.4147 +nsImageRenderer::~nsImageRenderer()
  1.4148 +{
  1.4149 +}
  1.4150 +
  1.4151 +bool
  1.4152 +nsImageRenderer::PrepareImage()
  1.4153 +{
  1.4154 +  if (mImage->IsEmpty())
  1.4155 +    return false;
  1.4156 +
  1.4157 +  if (!mImage->IsComplete()) {
  1.4158 +    // Make sure the image is actually decoding
  1.4159 +    mImage->StartDecoding();
  1.4160 +
  1.4161 +    // check again to see if we finished
  1.4162 +    if (!mImage->IsComplete()) {
  1.4163 +      // We can not prepare the image for rendering if it is not fully loaded.
  1.4164 +      //
  1.4165 +      // Special case: If we requested a sync decode and we have an image, push
  1.4166 +      // on through because the Draw() will do a sync decode then
  1.4167 +      nsCOMPtr<imgIContainer> img;
  1.4168 +      if (!((mFlags & FLAG_SYNC_DECODE_IMAGES) &&
  1.4169 +            (mType == eStyleImageType_Image) &&
  1.4170 +            (NS_SUCCEEDED(mImage->GetImageData()->GetImage(getter_AddRefs(img))))))
  1.4171 +        return false;
  1.4172 +    }
  1.4173 +  }
  1.4174 +
  1.4175 +  switch (mType) {
  1.4176 +    case eStyleImageType_Image:
  1.4177 +    {
  1.4178 +      nsCOMPtr<imgIContainer> srcImage;
  1.4179 +      DebugOnly<nsresult> rv =
  1.4180 +        mImage->GetImageData()->GetImage(getter_AddRefs(srcImage));
  1.4181 +      NS_ABORT_IF_FALSE(NS_SUCCEEDED(rv) && srcImage,
  1.4182 +                        "If GetImage() is failing, mImage->IsComplete() "
  1.4183 +                        "should have returned false");
  1.4184 +
  1.4185 +      if (!mImage->GetCropRect()) {
  1.4186 +        mImageContainer.swap(srcImage);
  1.4187 +      } else {
  1.4188 +        nsIntRect actualCropRect;
  1.4189 +        bool isEntireImage;
  1.4190 +        bool success =
  1.4191 +          mImage->ComputeActualCropRect(actualCropRect, &isEntireImage);
  1.4192 +        NS_ASSERTION(success, "ComputeActualCropRect() should not fail here");
  1.4193 +        if (!success || actualCropRect.IsEmpty()) {
  1.4194 +          // The cropped image has zero size
  1.4195 +          return false;
  1.4196 +        }
  1.4197 +        if (isEntireImage) {
  1.4198 +          // The cropped image is identical to the source image
  1.4199 +          mImageContainer.swap(srcImage);
  1.4200 +        } else {
  1.4201 +          nsCOMPtr<imgIContainer> subImage = ImageOps::Clip(srcImage, actualCropRect);
  1.4202 +          mImageContainer.swap(subImage);
  1.4203 +        }
  1.4204 +      }
  1.4205 +      mIsReady = true;
  1.4206 +      break;
  1.4207 +    }
  1.4208 +    case eStyleImageType_Gradient:
  1.4209 +      mGradientData = mImage->GetGradientData();
  1.4210 +      mIsReady = true;
  1.4211 +      break;
  1.4212 +    case eStyleImageType_Element:
  1.4213 +    {
  1.4214 +      nsAutoString elementId =
  1.4215 +        NS_LITERAL_STRING("#") + nsDependentString(mImage->GetElementId());
  1.4216 +      nsCOMPtr<nsIURI> targetURI;
  1.4217 +      nsCOMPtr<nsIURI> base = mForFrame->GetContent()->GetBaseURI();
  1.4218 +      nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(targetURI), elementId,
  1.4219 +                                                mForFrame->GetContent()->GetCurrentDoc(), base);
  1.4220 +      nsSVGPaintingProperty* property = nsSVGEffects::GetPaintingPropertyForURI(
  1.4221 +          targetURI, mForFrame->FirstContinuation(),
  1.4222 +          nsSVGEffects::BackgroundImageProperty());
  1.4223 +      if (!property)
  1.4224 +        return false;
  1.4225 +      mPaintServerFrame = property->GetReferencedFrame();
  1.4226 +
  1.4227 +      // If the referenced element doesn't have a frame we might still be able
  1.4228 +      // to paint it if it's an <img>, <canvas>, or <video> element.
  1.4229 +      if (!mPaintServerFrame) {
  1.4230 +        mImageElementSurface =
  1.4231 +          nsLayoutUtils::SurfaceFromElement(property->GetReferencedElement());
  1.4232 +        if (!mImageElementSurface.mSourceSurface)
  1.4233 +          return false;
  1.4234 +      }
  1.4235 +      mIsReady = true;
  1.4236 +      break;
  1.4237 +    }
  1.4238 +    case eStyleImageType_Null:
  1.4239 +    default:
  1.4240 +      break;
  1.4241 +  }
  1.4242 +
  1.4243 +  return mIsReady;
  1.4244 +}
  1.4245 +
  1.4246 +nsSize
  1.4247 +CSSSizeOrRatio::ComputeConcreteSize() const
  1.4248 +{
  1.4249 +  NS_ASSERTION(CanComputeConcreteSize(), "Cannot compute");
  1.4250 +  if (mHasWidth && mHasHeight) {
  1.4251 +    return nsSize(mWidth, mHeight);
  1.4252 +  }
  1.4253 +  if (mHasWidth) {
  1.4254 +    nscoord height = NSCoordSaturatingNonnegativeMultiply(
  1.4255 +      mWidth,
  1.4256 +      double(mRatio.height) / mRatio.width);
  1.4257 +    return nsSize(mWidth, height);
  1.4258 +  }
  1.4259 +
  1.4260 +  MOZ_ASSERT(mHasHeight);
  1.4261 +  nscoord width = NSCoordSaturatingNonnegativeMultiply(
  1.4262 +    mHeight,
  1.4263 +    double(mRatio.width) / mRatio.height);
  1.4264 +  return nsSize(width, mHeight);
  1.4265 +}
  1.4266 +
  1.4267 +CSSSizeOrRatio
  1.4268 +nsImageRenderer::ComputeIntrinsicSize()
  1.4269 +{
  1.4270 +  NS_ASSERTION(mIsReady, "Ensure PrepareImage() has returned true "
  1.4271 +                         "before calling me");
  1.4272 +
  1.4273 +  CSSSizeOrRatio result;
  1.4274 +  switch (mType) {
  1.4275 +    case eStyleImageType_Image:
  1.4276 +    {
  1.4277 +      bool haveWidth, haveHeight;
  1.4278 +      nsIntSize imageIntSize;
  1.4279 +      nsLayoutUtils::ComputeSizeForDrawing(mImageContainer, imageIntSize,
  1.4280 +                                           result.mRatio, haveWidth, haveHeight);
  1.4281 +      if (haveWidth) {
  1.4282 +        result.SetWidth(nsPresContext::CSSPixelsToAppUnits(imageIntSize.width));
  1.4283 +      }
  1.4284 +      if (haveHeight) {
  1.4285 +        result.SetHeight(nsPresContext::CSSPixelsToAppUnits(imageIntSize.height));
  1.4286 +      }
  1.4287 +      break;
  1.4288 +    }
  1.4289 +    case eStyleImageType_Element:
  1.4290 +    {
  1.4291 +      // XXX element() should have the width/height of the referenced element,
  1.4292 +      //     and that element's ratio, if it matches.  If it doesn't match, it
  1.4293 +      //     should have no width/height or ratio.  See element() in CSS images:
  1.4294 +      //     <http://dev.w3.org/csswg/css-images-4/#element-notation>.
  1.4295 +      //     Make sure to change nsStyleBackground::Size::DependsOnFrameSize
  1.4296 +      //     when fixing this!
  1.4297 +      if (mPaintServerFrame) {
  1.4298 +        // SVG images have no intrinsic size
  1.4299 +        if (!mPaintServerFrame->IsFrameOfType(nsIFrame::eSVG)) {
  1.4300 +          // The intrinsic image size for a generic nsIFrame paint server is
  1.4301 +          // the union of the border-box rects of all of its continuations,
  1.4302 +          // rounded to device pixels.
  1.4303 +          int32_t appUnitsPerDevPixel =
  1.4304 +            mForFrame->PresContext()->AppUnitsPerDevPixel();
  1.4305 +          result.SetSize(
  1.4306 +            nsSVGIntegrationUtils::GetContinuationUnionSize(mPaintServerFrame).
  1.4307 +              ToNearestPixels(appUnitsPerDevPixel).
  1.4308 +              ToAppUnits(appUnitsPerDevPixel));
  1.4309 +        }
  1.4310 +      } else {
  1.4311 +        NS_ASSERTION(mImageElementSurface.mSourceSurface, "Surface should be ready.");
  1.4312 +        gfxIntSize surfaceSize = mImageElementSurface.mSize;
  1.4313 +        result.SetSize(
  1.4314 +          nsSize(nsPresContext::CSSPixelsToAppUnits(surfaceSize.width),
  1.4315 +                 nsPresContext::CSSPixelsToAppUnits(surfaceSize.height)));
  1.4316 +      }
  1.4317 +      break;
  1.4318 +    }
  1.4319 +    case eStyleImageType_Gradient:
  1.4320 +      // Per <http://dev.w3.org/csswg/css3-images/#gradients>, gradients have no
  1.4321 +      // intrinsic dimensions.
  1.4322 +    case eStyleImageType_Null:
  1.4323 +    default:
  1.4324 +      break;
  1.4325 +  }
  1.4326 +
  1.4327 +  return result;
  1.4328 +}
  1.4329 +
  1.4330 +/* static */ nsSize
  1.4331 +nsImageRenderer::ComputeConcreteSize(const CSSSizeOrRatio& aSpecifiedSize,
  1.4332 +                                     const CSSSizeOrRatio& aIntrinsicSize,
  1.4333 +                                     const nsSize& aDefaultSize)
  1.4334 +{
  1.4335 +  // The specified size is fully specified, just use that
  1.4336 +  if (aSpecifiedSize.IsConcrete()) {
  1.4337 +    return aSpecifiedSize.ComputeConcreteSize();
  1.4338 +  }
  1.4339 +
  1.4340 +  MOZ_ASSERT(!aSpecifiedSize.mHasWidth || !aSpecifiedSize.mHasHeight);
  1.4341 +
  1.4342 +  if (!aSpecifiedSize.mHasWidth && !aSpecifiedSize.mHasHeight) {
  1.4343 +    // no specified size, try using the intrinsic size
  1.4344 +    if (aIntrinsicSize.CanComputeConcreteSize()) {
  1.4345 +      return aIntrinsicSize.ComputeConcreteSize();
  1.4346 +    }
  1.4347 +
  1.4348 +    if (aIntrinsicSize.mHasWidth) {
  1.4349 +      return nsSize(aIntrinsicSize.mWidth, aDefaultSize.height);
  1.4350 +    }
  1.4351 +    if (aIntrinsicSize.mHasHeight) {
  1.4352 +      return nsSize(aDefaultSize.width, aIntrinsicSize.mHeight);
  1.4353 +    }
  1.4354 +
  1.4355 +    // couldn't use the intrinsic size either, revert to using the default size
  1.4356 +    return ComputeConstrainedSize(aDefaultSize,
  1.4357 +                                  aIntrinsicSize.mRatio,
  1.4358 +                                  CONTAIN);
  1.4359 +  }
  1.4360 +
  1.4361 +  MOZ_ASSERT(aSpecifiedSize.mHasWidth || aSpecifiedSize.mHasHeight);
  1.4362 +
  1.4363 +  // The specified height is partial, try to compute the missing part.
  1.4364 +  if (aSpecifiedSize.mHasWidth) {
  1.4365 +    nscoord height;
  1.4366 +    if (aIntrinsicSize.HasRatio()) {
  1.4367 +      height = NSCoordSaturatingNonnegativeMultiply(
  1.4368 +        aSpecifiedSize.mWidth,
  1.4369 +        double(aIntrinsicSize.mRatio.height) / aIntrinsicSize.mRatio.width);
  1.4370 +    } else if (aIntrinsicSize.mHasHeight) {
  1.4371 +      height = aIntrinsicSize.mHeight;
  1.4372 +    } else {
  1.4373 +      height = aDefaultSize.height;
  1.4374 +    }
  1.4375 +    return nsSize(aSpecifiedSize.mWidth, height);
  1.4376 +  }
  1.4377 +
  1.4378 +  MOZ_ASSERT(aSpecifiedSize.mHasHeight);
  1.4379 +  nscoord width;
  1.4380 +  if (aIntrinsicSize.HasRatio()) {
  1.4381 +    width = NSCoordSaturatingNonnegativeMultiply(
  1.4382 +      aSpecifiedSize.mHeight,
  1.4383 +      double(aIntrinsicSize.mRatio.width) / aIntrinsicSize.mRatio.height);
  1.4384 +  } else if (aIntrinsicSize.mHasWidth) {
  1.4385 +    width = aIntrinsicSize.mWidth;
  1.4386 +  } else {
  1.4387 +    width = aDefaultSize.width;
  1.4388 +  }
  1.4389 +  return nsSize(width, aSpecifiedSize.mHeight);
  1.4390 +}
  1.4391 +
  1.4392 +/* static */ nsSize
  1.4393 +nsImageRenderer::ComputeConstrainedSize(const nsSize& aConstrainingSize,
  1.4394 +                                        const nsSize& aIntrinsicRatio,
  1.4395 +                                        FitType aFitType)
  1.4396 +{
  1.4397 +  if (aIntrinsicRatio.width <= 0 && aIntrinsicRatio.height <= 0) {
  1.4398 +    return aConstrainingSize;
  1.4399 +  }
  1.4400 +
  1.4401 +  float scaleX = double(aConstrainingSize.width) / aIntrinsicRatio.width;
  1.4402 +  float scaleY = double(aConstrainingSize.height) / aIntrinsicRatio.height;
  1.4403 +  nsSize size;
  1.4404 +  if ((aFitType == CONTAIN) == (scaleX < scaleY)) {
  1.4405 +    size.width = aConstrainingSize.width;
  1.4406 +    size.height = NSCoordSaturatingNonnegativeMultiply(
  1.4407 +                    aIntrinsicRatio.height, scaleX);
  1.4408 +  } else {
  1.4409 +    size.width = NSCoordSaturatingNonnegativeMultiply(
  1.4410 +                   aIntrinsicRatio.width, scaleY);
  1.4411 +    size.height = aConstrainingSize.height;
  1.4412 +  }
  1.4413 +  return size;
  1.4414 +}
  1.4415 +
  1.4416 +/**
  1.4417 + * mSize is the image's "preferred" size for this particular rendering, while
  1.4418 + * the drawn (aka concrete) size is the actual rendered size after accounting
  1.4419 + * for background-size etc..  The preferred size is most often the image's
  1.4420 + * intrinsic dimensions.  But for images with incomplete intrinsic dimensions,
  1.4421 + * the preferred size varies, depending on the specified and default sizes, see
  1.4422 + * nsImageRenderer::Compute*Size.
  1.4423 + *
  1.4424 + * This distinction is necessary because the components of a vector image are
  1.4425 + * specified with respect to its preferred size for a rendering situation, not
  1.4426 + * to its actual rendered size.  For example, consider a 4px wide background
  1.4427 + * vector image with no height which contains a left-aligned
  1.4428 + * 2px wide black rectangle with height 100%.  If the background-size width is
  1.4429 + * auto (or 4px), the vector image will render 4px wide, and the black rectangle
  1.4430 + * will be 2px wide.  If the background-size width is 8px, the vector image will
  1.4431 + * render 8px wide, and the black rectangle will be 4px wide -- *not* 2px wide.
  1.4432 + * In both cases mSize.width will be 4px; but in the first case the returned
  1.4433 + * width will be 4px, while in the second case the returned width will be 8px.
  1.4434 + */
  1.4435 +void
  1.4436 +nsImageRenderer::SetPreferredSize(const CSSSizeOrRatio& aIntrinsicSize,
  1.4437 +                                  const nsSize& aDefaultSize)
  1.4438 +{
  1.4439 +  mSize.width = aIntrinsicSize.mHasWidth
  1.4440 +                  ? aIntrinsicSize.mWidth
  1.4441 +                  : aDefaultSize.width;
  1.4442 +  mSize.height = aIntrinsicSize.mHasHeight
  1.4443 +                  ? aIntrinsicSize.mHeight
  1.4444 +                  : aDefaultSize.height;
  1.4445 +}
  1.4446 +
  1.4447 +// Convert from nsImageRenderer flags to the flags we want to use for drawing in
  1.4448 +// the imgIContainer namespace.
  1.4449 +static uint32_t
  1.4450 +ConvertImageRendererToDrawFlags(uint32_t aImageRendererFlags)
  1.4451 +{
  1.4452 +  uint32_t drawFlags = imgIContainer::FLAG_NONE;
  1.4453 +  if (aImageRendererFlags & nsImageRenderer::FLAG_SYNC_DECODE_IMAGES) {
  1.4454 +    drawFlags |= imgIContainer::FLAG_SYNC_DECODE;
  1.4455 +  }
  1.4456 +  if (aImageRendererFlags & nsImageRenderer::FLAG_PAINTING_TO_WINDOW) {
  1.4457 +    drawFlags |= imgIContainer::FLAG_HIGH_QUALITY_SCALING;
  1.4458 +  }
  1.4459 +  return drawFlags;
  1.4460 +}
  1.4461 +
  1.4462 +void
  1.4463 +nsImageRenderer::Draw(nsPresContext*       aPresContext,
  1.4464 +                      nsRenderingContext&  aRenderingContext,
  1.4465 +                      const nsRect&        aDirtyRect,
  1.4466 +                      const nsRect&        aFill,
  1.4467 +                      const nsRect&        aDest,
  1.4468 +                      const CSSIntRect&    aSrc)
  1.4469 +{
  1.4470 +  if (!mIsReady) {
  1.4471 +    NS_NOTREACHED("Ensure PrepareImage() has returned true before calling me");
  1.4472 +    return;
  1.4473 +  }
  1.4474 +  if (aDest.IsEmpty() || aFill.IsEmpty() ||
  1.4475 +      mSize.width <= 0 || mSize.height <= 0) {
  1.4476 +    return;
  1.4477 +  }
  1.4478 +
  1.4479 +  GraphicsFilter graphicsFilter =
  1.4480 +    nsLayoutUtils::GetGraphicsFilterForFrame(mForFrame);
  1.4481 +
  1.4482 +  switch (mType) {
  1.4483 +    case eStyleImageType_Image:
  1.4484 +    {
  1.4485 +      nsLayoutUtils::DrawSingleImage(&aRenderingContext, mImageContainer,
  1.4486 +                                     graphicsFilter, aFill, aDirtyRect,
  1.4487 +                                     nullptr,
  1.4488 +                                     ConvertImageRendererToDrawFlags(mFlags));
  1.4489 +      return;
  1.4490 +    }
  1.4491 +    case eStyleImageType_Gradient:
  1.4492 +    {
  1.4493 +      nsCSSRendering::PaintGradient(aPresContext, aRenderingContext,
  1.4494 +                                    mGradientData, aDirtyRect,
  1.4495 +                                    aDest, aFill, aSrc, mSize);
  1.4496 +      return;
  1.4497 +    }
  1.4498 +    case eStyleImageType_Element:
  1.4499 +    {
  1.4500 +      nsRefPtr<gfxDrawable> drawable = DrawableForElement(aDest,
  1.4501 +                                                          aRenderingContext);
  1.4502 +      if (!drawable) {
  1.4503 +        NS_WARNING("Could not create drawable for element");
  1.4504 +        return;
  1.4505 +      }
  1.4506 +      nsLayoutUtils::DrawPixelSnapped(&aRenderingContext, drawable, graphicsFilter,
  1.4507 +                                      aDest, aFill, aDest.TopLeft(), aDirtyRect);
  1.4508 +      return;
  1.4509 +    }
  1.4510 +    case eStyleImageType_Null:
  1.4511 +    default:
  1.4512 +      return;
  1.4513 +  }
  1.4514 +}
  1.4515 +
  1.4516 +already_AddRefed<gfxDrawable>
  1.4517 +nsImageRenderer::DrawableForElement(const nsRect& aImageRect,
  1.4518 +                                    nsRenderingContext&  aRenderingContext)
  1.4519 +{
  1.4520 +  NS_ASSERTION(mType == eStyleImageType_Element,
  1.4521 +               "DrawableForElement only makes sense if backed by an element");
  1.4522 +  if (mPaintServerFrame) {
  1.4523 +    int32_t appUnitsPerDevPixel = mForFrame->PresContext()->AppUnitsPerDevPixel();
  1.4524 +    nsRect destRect = aImageRect - aImageRect.TopLeft();
  1.4525 +    nsIntSize roundedOut = destRect.ToOutsidePixels(appUnitsPerDevPixel).Size();
  1.4526 +    gfxIntSize imageSize(roundedOut.width, roundedOut.height);
  1.4527 +    nsRefPtr<gfxDrawable> drawable =
  1.4528 +      nsSVGIntegrationUtils::DrawableFromPaintServer(
  1.4529 +        mPaintServerFrame, mForFrame, mSize, imageSize,
  1.4530 +        aRenderingContext.ThebesContext()->CurrentMatrix(),
  1.4531 +        mFlags & FLAG_SYNC_DECODE_IMAGES
  1.4532 +          ? nsSVGIntegrationUtils::FLAG_SYNC_DECODE_IMAGES
  1.4533 +          : 0);
  1.4534 +
  1.4535 +    return drawable.forget();
  1.4536 +  }
  1.4537 +  NS_ASSERTION(mImageElementSurface.mSourceSurface, "Surface should be ready.");
  1.4538 +  nsRefPtr<gfxDrawable> drawable = new gfxSurfaceDrawable(
  1.4539 +                                mImageElementSurface.mSourceSurface,
  1.4540 +                                mImageElementSurface.mSize);
  1.4541 +  return drawable.forget();
  1.4542 +}
  1.4543 +
  1.4544 +void
  1.4545 +nsImageRenderer::DrawBackground(nsPresContext*       aPresContext,
  1.4546 +                                nsRenderingContext&  aRenderingContext,
  1.4547 +                                const nsRect&        aDest,
  1.4548 +                                const nsRect&        aFill,
  1.4549 +                                const nsPoint&       aAnchor,
  1.4550 +                                const nsRect&        aDirty)
  1.4551 +{
  1.4552 +  if (!mIsReady) {
  1.4553 +    NS_NOTREACHED("Ensure PrepareImage() has returned true before calling me");
  1.4554 +    return;
  1.4555 +  }
  1.4556 +  if (aDest.IsEmpty() || aFill.IsEmpty() ||
  1.4557 +      mSize.width <= 0 || mSize.height <= 0) {
  1.4558 +    return;
  1.4559 +  }
  1.4560 +
  1.4561 +  if (mType == eStyleImageType_Image) {
  1.4562 +    GraphicsFilter graphicsFilter =
  1.4563 +      nsLayoutUtils::GetGraphicsFilterForFrame(mForFrame);
  1.4564 +
  1.4565 +    nsLayoutUtils::DrawBackgroundImage(&aRenderingContext, mImageContainer,
  1.4566 +                nsIntSize(nsPresContext::AppUnitsToIntCSSPixels(mSize.width),
  1.4567 +                          nsPresContext::AppUnitsToIntCSSPixels(mSize.height)),
  1.4568 +                graphicsFilter,
  1.4569 +                aDest, aFill, aAnchor, aDirty,
  1.4570 +                ConvertImageRendererToDrawFlags(mFlags));
  1.4571 +    return;
  1.4572 +  }
  1.4573 +
  1.4574 +  Draw(aPresContext, aRenderingContext,
  1.4575 +       aDirty, aFill, aDest,
  1.4576 +       CSSIntRect(0, 0,
  1.4577 +                  nsPresContext::AppUnitsToIntCSSPixels(mSize.width),
  1.4578 +                  nsPresContext::AppUnitsToIntCSSPixels(mSize.height)));
  1.4579 +}
  1.4580 +
  1.4581 +/**
  1.4582 + * Compute the size and position of the master copy of the image. I.e., a single
  1.4583 + * tile used to fill the dest rect.
  1.4584 + * aFill The destination rect to be filled
  1.4585 + * aHFill and aVFill are the repeat patterns for the component -
  1.4586 + * NS_STYLE_BORDER_IMAGE_REPEAT_* - i.e., how a tiling unit is used to fill aFill
  1.4587 + * aUnitSize The size of the source rect in dest coords.
  1.4588 + */
  1.4589 +static nsRect
  1.4590 +ComputeTile(const nsRect&        aFill,
  1.4591 +            uint8_t              aHFill,
  1.4592 +            uint8_t              aVFill,
  1.4593 +            const nsSize&        aUnitSize)
  1.4594 +{
  1.4595 +  nsRect tile;
  1.4596 +  switch (aHFill) {
  1.4597 +  case NS_STYLE_BORDER_IMAGE_REPEAT_STRETCH:
  1.4598 +    tile.x = aFill.x;
  1.4599 +    tile.width = aFill.width;
  1.4600 +    break;
  1.4601 +  case NS_STYLE_BORDER_IMAGE_REPEAT_REPEAT:
  1.4602 +    tile.x = aFill.x + aFill.width/2 - aUnitSize.width/2;
  1.4603 +    tile.width = aUnitSize.width;
  1.4604 +    break;
  1.4605 +  case NS_STYLE_BORDER_IMAGE_REPEAT_ROUND:
  1.4606 +    tile.x = aFill.x;
  1.4607 +    tile.width = aFill.width / ceil(gfxFloat(aFill.width)/aUnitSize.width);
  1.4608 +    break;
  1.4609 +  default:
  1.4610 +    NS_NOTREACHED("unrecognized border-image fill style");
  1.4611 +  }
  1.4612 +
  1.4613 +  switch (aVFill) {
  1.4614 +  case NS_STYLE_BORDER_IMAGE_REPEAT_STRETCH:
  1.4615 +    tile.y = aFill.y;
  1.4616 +    tile.height = aFill.height;
  1.4617 +    break;
  1.4618 +  case NS_STYLE_BORDER_IMAGE_REPEAT_REPEAT:
  1.4619 +    tile.y = aFill.y + aFill.height/2 - aUnitSize.height/2;
  1.4620 +    tile.height = aUnitSize.height;
  1.4621 +    break;
  1.4622 +  case NS_STYLE_BORDER_IMAGE_REPEAT_ROUND:
  1.4623 +    tile.y = aFill.y;
  1.4624 +    tile.height = aFill.height/ceil(gfxFloat(aFill.height)/aUnitSize.height);
  1.4625 +    break;
  1.4626 +  default:
  1.4627 +    NS_NOTREACHED("unrecognized border-image fill style");
  1.4628 +  }
  1.4629 +
  1.4630 +  return tile;
  1.4631 +}
  1.4632 +
  1.4633 +/**
  1.4634 + * Returns true if the given set of arguments will require the tiles which fill
  1.4635 + * the dest rect to be scaled from the source tile. See comment on ComputeTile
  1.4636 + * for argument descriptions.
  1.4637 + */
  1.4638 +static bool
  1.4639 +RequiresScaling(const nsRect&        aFill,
  1.4640 +                uint8_t              aHFill,
  1.4641 +                uint8_t              aVFill,
  1.4642 +                const nsSize&        aUnitSize)
  1.4643 +{
  1.4644 +  // If we have no tiling in either direction, we can skip the intermediate
  1.4645 +  // scaling step.
  1.4646 +  return (aHFill != NS_STYLE_BORDER_IMAGE_REPEAT_STRETCH ||
  1.4647 +          aVFill != NS_STYLE_BORDER_IMAGE_REPEAT_STRETCH) &&
  1.4648 +         (aUnitSize.width != aFill.width ||
  1.4649 +          aUnitSize.height != aFill.height);
  1.4650 +}
  1.4651 +
  1.4652 +void
  1.4653 +nsImageRenderer::DrawBorderImageComponent(nsPresContext*       aPresContext,
  1.4654 +                                          nsRenderingContext&  aRenderingContext,
  1.4655 +                                          const nsRect&        aDirtyRect,
  1.4656 +                                          const nsRect&        aFill,
  1.4657 +                                          const CSSIntRect&    aSrc,
  1.4658 +                                          uint8_t              aHFill,
  1.4659 +                                          uint8_t              aVFill,
  1.4660 +                                          const nsSize&        aUnitSize,
  1.4661 +                                          uint8_t              aIndex)
  1.4662 +{
  1.4663 +  if (!mIsReady) {
  1.4664 +    NS_NOTREACHED("Ensure PrepareImage() has returned true before calling me");
  1.4665 +    return;
  1.4666 +  }
  1.4667 +  if (aFill.IsEmpty() || aSrc.IsEmpty()) {
  1.4668 +    return;
  1.4669 +  }
  1.4670 +
  1.4671 +  if (mType == eStyleImageType_Image) {
  1.4672 +    nsCOMPtr<imgIContainer> subImage;
  1.4673 +    if ((subImage = mImage->GetSubImage(aIndex)) == nullptr) {
  1.4674 +      subImage = ImageOps::Clip(mImageContainer, nsIntRect(aSrc.x,
  1.4675 +                                                           aSrc.y,
  1.4676 +                                                           aSrc.width,
  1.4677 +                                                           aSrc.height));
  1.4678 +      mImage->SetSubImage(aIndex, subImage);
  1.4679 +    }
  1.4680 +
  1.4681 +    GraphicsFilter graphicsFilter =
  1.4682 +      nsLayoutUtils::GetGraphicsFilterForFrame(mForFrame);
  1.4683 +
  1.4684 +    if (!RequiresScaling(aFill, aHFill, aVFill, aUnitSize)) {
  1.4685 +      nsLayoutUtils::DrawSingleImage(&aRenderingContext,
  1.4686 +                                     subImage,
  1.4687 +                                     graphicsFilter,
  1.4688 +                                     aFill, aDirtyRect,
  1.4689 +                                     nullptr,
  1.4690 +                                     imgIContainer::FLAG_NONE);
  1.4691 +      return;
  1.4692 +    }
  1.4693 +
  1.4694 +    nsRect tile = ComputeTile(aFill, aHFill, aVFill, aUnitSize);
  1.4695 +    nsLayoutUtils::DrawImage(&aRenderingContext,
  1.4696 +                             subImage,
  1.4697 +                             graphicsFilter,
  1.4698 +                             tile, aFill, tile.TopLeft(), aDirtyRect,
  1.4699 +                             imgIContainer::FLAG_NONE);
  1.4700 +    return;
  1.4701 +  }
  1.4702 +
  1.4703 +  nsRect destTile = RequiresScaling(aFill, aHFill, aVFill, aUnitSize)
  1.4704 +                  ? ComputeTile(aFill, aHFill, aVFill, aUnitSize)
  1.4705 +                  : aFill;
  1.4706 +
  1.4707 +  if (mType == eStyleImageType_Element) {
  1.4708 +    // This path is horribly slow - we read and copy the source nine times(!)
  1.4709 +    // It could be easily optimised by only reading the source once and caching
  1.4710 +    // it. It could be further optimised by caching the sub-images between draws
  1.4711 +    // but that would be a bit harder because you would have to know when to
  1.4712 +    // invalidate the cache. A special case optimisation would be when
  1.4713 +    // border-image-slice is proportional to the border widths, in which case
  1.4714 +    // the subimages do not need to be independently scaled, then we don't need
  1.4715 +    // subimages at all.
  1.4716 +    // In any case, such optimisations are probably not worth doing because it
  1.4717 +    // seems unlikely anyone would use -moz-element as the source for a border
  1.4718 +    // image.
  1.4719 +
  1.4720 +    // draw the source image slice into an intermediate surface
  1.4721 +    nsPresContext* presContext = mForFrame->PresContext();
  1.4722 +    gfxRect srcRect = gfxRect(presContext->CSSPixelsToDevPixels(aSrc.x),
  1.4723 +                              presContext->CSSPixelsToDevPixels(aSrc.y),
  1.4724 +                              presContext->CSSPixelsToDevPixels(aSrc.width),
  1.4725 +                              presContext->CSSPixelsToDevPixels(aSrc.height));
  1.4726 +    RefPtr<DrawTarget> srcSlice = gfxPlatform::GetPlatform()->
  1.4727 +      CreateOffscreenContentDrawTarget(IntSize(srcRect.width, srcRect.height),
  1.4728 +                             SurfaceFormat::B8G8R8A8);
  1.4729 +    nsRefPtr<gfxContext> ctx = new gfxContext(srcSlice);
  1.4730 +
  1.4731 +    // grab the entire source
  1.4732 +    nsRefPtr<gfxDrawable> drawable = DrawableForElement(nsRect(nsPoint(), mSize),
  1.4733 +                                                        aRenderingContext);
  1.4734 +    if (!drawable) {
  1.4735 +      NS_WARNING("Could not create drawable for element");
  1.4736 +      return;
  1.4737 +    }
  1.4738 +
  1.4739 +    // draw the source into our intermediate surface
  1.4740 +    GraphicsFilter graphicsFilter =
  1.4741 +      nsLayoutUtils::GetGraphicsFilterForFrame(mForFrame);
  1.4742 +    gfxMatrix transform;
  1.4743 +    transform.Translate(gfxPoint(srcRect.x, srcRect.y));
  1.4744 +    bool success = drawable->Draw(ctx,
  1.4745 +                                  gfxRect(0, 0, srcRect.width, srcRect.height),
  1.4746 +                                  false,
  1.4747 +                                  graphicsFilter,
  1.4748 +                                  transform);
  1.4749 +    if (!success) {
  1.4750 +      NS_WARNING("Could not copy element image");
  1.4751 +      return;
  1.4752 +    }
  1.4753 +
  1.4754 +    // Ensure that drawing the image gets flushed to the target.
  1.4755 +    ctx = nullptr;
  1.4756 +
  1.4757 +    // draw the slice
  1.4758 +    nsRefPtr<gfxSurfaceDrawable> srcSliceDrawable =
  1.4759 +      new gfxSurfaceDrawable(srcSlice,
  1.4760 +                             gfxIntSize(srcRect.width, srcRect.height));
  1.4761 +    nsPoint anchor(nsPresContext::CSSPixelsToAppUnits(aSrc.x),
  1.4762 +                   nsPresContext::CSSPixelsToAppUnits(aSrc.y));
  1.4763 +    nsLayoutUtils::DrawPixelSnapped(&aRenderingContext, srcSliceDrawable,
  1.4764 +                                    graphicsFilter, destTile, aFill,
  1.4765 +                                    anchor, aDirtyRect);
  1.4766 +
  1.4767 +    return;
  1.4768 +  }
  1.4769 +
  1.4770 +  Draw(aPresContext, aRenderingContext, aDirtyRect, aFill, destTile, aSrc);
  1.4771 +}
  1.4772 +
  1.4773 +bool
  1.4774 +nsImageRenderer::IsRasterImage()
  1.4775 +{
  1.4776 +  if (mType != eStyleImageType_Image || !mImageContainer)
  1.4777 +    return false;
  1.4778 +  return mImageContainer->GetType() == imgIContainer::TYPE_RASTER;
  1.4779 +}
  1.4780 +
  1.4781 +bool
  1.4782 +nsImageRenderer::IsAnimatedImage()
  1.4783 +{
  1.4784 +  if (mType != eStyleImageType_Image || !mImageContainer)
  1.4785 +    return false;
  1.4786 +  bool animated = false;
  1.4787 +  if (NS_SUCCEEDED(mImageContainer->GetAnimated(&animated)) && animated)
  1.4788 +    return true;
  1.4789 +
  1.4790 +  return false;
  1.4791 +}
  1.4792 +
  1.4793 +already_AddRefed<mozilla::layers::ImageContainer>
  1.4794 +nsImageRenderer::GetContainer(LayerManager* aManager)
  1.4795 +{
  1.4796 +  if (mType != eStyleImageType_Image || !mImageContainer)
  1.4797 +    return nullptr;
  1.4798 +
  1.4799 +  nsRefPtr<ImageContainer> container;
  1.4800 +  nsresult rv = mImageContainer->GetImageContainer(aManager, getter_AddRefs(container));
  1.4801 +  NS_ENSURE_SUCCESS(rv, nullptr);
  1.4802 +  return container.forget();
  1.4803 +}
  1.4804 +
  1.4805 +#define MAX_BLUR_RADIUS 300
  1.4806 +#define MAX_SPREAD_RADIUS 50
  1.4807 +
  1.4808 +static inline gfxPoint ComputeBlurStdDev(nscoord aBlurRadius,
  1.4809 +                                         int32_t aAppUnitsPerDevPixel,
  1.4810 +                                         gfxFloat aScaleX,
  1.4811 +                                         gfxFloat aScaleY)
  1.4812 +{
  1.4813 +  // http://dev.w3.org/csswg/css3-background/#box-shadow says that the
  1.4814 +  // standard deviation of the blur should be half the given blur value.
  1.4815 +  gfxFloat blurStdDev = gfxFloat(aBlurRadius) / gfxFloat(aAppUnitsPerDevPixel);
  1.4816 +
  1.4817 +  return gfxPoint(std::min((blurStdDev * aScaleX),
  1.4818 +                           gfxFloat(MAX_BLUR_RADIUS)) / 2.0,
  1.4819 +                  std::min((blurStdDev * aScaleY),
  1.4820 +                           gfxFloat(MAX_BLUR_RADIUS)) / 2.0);
  1.4821 +}
  1.4822 +
  1.4823 +static inline gfxIntSize
  1.4824 +ComputeBlurRadius(nscoord aBlurRadius,
  1.4825 +                  int32_t aAppUnitsPerDevPixel,
  1.4826 +                  gfxFloat aScaleX = 1.0,
  1.4827 +                  gfxFloat aScaleY = 1.0)
  1.4828 +{
  1.4829 +  gfxPoint scaledBlurStdDev = ComputeBlurStdDev(aBlurRadius, aAppUnitsPerDevPixel,
  1.4830 +                                                aScaleX, aScaleY);
  1.4831 +  return
  1.4832 +    gfxAlphaBoxBlur::CalculateBlurRadius(scaledBlurStdDev);
  1.4833 +}
  1.4834 +
  1.4835 +// -----
  1.4836 +// nsContextBoxBlur
  1.4837 +// -----
  1.4838 +gfxContext*
  1.4839 +nsContextBoxBlur::Init(const nsRect& aRect, nscoord aSpreadRadius,
  1.4840 +                       nscoord aBlurRadius,
  1.4841 +                       int32_t aAppUnitsPerDevPixel,
  1.4842 +                       gfxContext* aDestinationCtx,
  1.4843 +                       const nsRect& aDirtyRect,
  1.4844 +                       const gfxRect* aSkipRect,
  1.4845 +                       uint32_t aFlags)
  1.4846 +{
  1.4847 +  if (aRect.IsEmpty()) {
  1.4848 +    mContext = nullptr;
  1.4849 +    return nullptr;
  1.4850 +  }
  1.4851 +
  1.4852 +  gfxFloat scaleX = 1;
  1.4853 +  gfxFloat scaleY = 1;
  1.4854 +
  1.4855 +  // Do blurs in device space when possible.
  1.4856 +  // Chrome/Skia always does the blurs in device space
  1.4857 +  // and will sometimes get incorrect results (e.g. rotated blurs)
  1.4858 +  gfxMatrix transform = aDestinationCtx->CurrentMatrix();
  1.4859 +  // XXX: we could probably handle negative scales but for now it's easier just to fallback
  1.4860 +  if (transform.HasNonAxisAlignedTransform() || transform.xx <= 0.0 || transform.yy <= 0.0) {
  1.4861 +    transform = gfxMatrix();
  1.4862 +  } else {
  1.4863 +    scaleX = transform.xx;
  1.4864 +    scaleY = transform.yy;
  1.4865 +  }
  1.4866 +
  1.4867 +  // compute a large or smaller blur radius
  1.4868 +  gfxIntSize blurRadius = ComputeBlurRadius(aBlurRadius, aAppUnitsPerDevPixel, scaleX, scaleY);
  1.4869 +  gfxIntSize spreadRadius = gfxIntSize(std::min(int32_t(aSpreadRadius * scaleX / aAppUnitsPerDevPixel),
  1.4870 +                                              int32_t(MAX_SPREAD_RADIUS)),
  1.4871 +                                       std::min(int32_t(aSpreadRadius * scaleY / aAppUnitsPerDevPixel),
  1.4872 +                                              int32_t(MAX_SPREAD_RADIUS)));
  1.4873 +  mDestinationCtx = aDestinationCtx;
  1.4874 +
  1.4875 +  // If not blurring, draw directly onto the destination device
  1.4876 +  if (blurRadius.width <= 0 && blurRadius.height <= 0 &&
  1.4877 +      spreadRadius.width <= 0 && spreadRadius.height <= 0 &&
  1.4878 +      !(aFlags & FORCE_MASK)) {
  1.4879 +    mContext = aDestinationCtx;
  1.4880 +    return mContext;
  1.4881 +  }
  1.4882 +
  1.4883 +  // Convert from app units to device pixels
  1.4884 +  gfxRect rect = nsLayoutUtils::RectToGfxRect(aRect, aAppUnitsPerDevPixel);
  1.4885 +
  1.4886 +  gfxRect dirtyRect =
  1.4887 +    nsLayoutUtils::RectToGfxRect(aDirtyRect, aAppUnitsPerDevPixel);
  1.4888 +  dirtyRect.RoundOut();
  1.4889 +
  1.4890 +  rect = transform.TransformBounds(rect);
  1.4891 +
  1.4892 +  mPreTransformed = !transform.IsIdentity();
  1.4893 +
  1.4894 +  // Create the temporary surface for blurring
  1.4895 +  dirtyRect = transform.TransformBounds(dirtyRect);
  1.4896 +  if (aSkipRect) {
  1.4897 +    gfxRect skipRect = transform.TransformBounds(*aSkipRect);
  1.4898 +    mContext = blur.Init(rect, spreadRadius,
  1.4899 +                         blurRadius, &dirtyRect, &skipRect);
  1.4900 +  } else {
  1.4901 +    mContext = blur.Init(rect, spreadRadius,
  1.4902 +                         blurRadius, &dirtyRect, nullptr);
  1.4903 +  }
  1.4904 +
  1.4905 +  if (mContext) {
  1.4906 +    // we don't need to blur if skipRect is equal to rect
  1.4907 +    // and mContext will be nullptr
  1.4908 +    mContext->Multiply(transform);
  1.4909 +  }
  1.4910 +  return mContext;
  1.4911 +}
  1.4912 +
  1.4913 +void
  1.4914 +nsContextBoxBlur::DoPaint()
  1.4915 +{
  1.4916 +  if (mContext == mDestinationCtx)
  1.4917 +    return;
  1.4918 +
  1.4919 +  gfxContextMatrixAutoSaveRestore saveMatrix(mDestinationCtx);
  1.4920 +
  1.4921 +  if (mPreTransformed) {
  1.4922 +    mDestinationCtx->IdentityMatrix();
  1.4923 +  }
  1.4924 +
  1.4925 +  blur.Paint(mDestinationCtx);
  1.4926 +}
  1.4927 +
  1.4928 +gfxContext*
  1.4929 +nsContextBoxBlur::GetContext()
  1.4930 +{
  1.4931 +  return mContext;
  1.4932 +}
  1.4933 +
  1.4934 +/* static */ nsMargin
  1.4935 +nsContextBoxBlur::GetBlurRadiusMargin(nscoord aBlurRadius,
  1.4936 +                                      int32_t aAppUnitsPerDevPixel)
  1.4937 +{
  1.4938 +  gfxIntSize blurRadius = ComputeBlurRadius(aBlurRadius, aAppUnitsPerDevPixel);
  1.4939 +
  1.4940 +  nsMargin result;
  1.4941 +  result.top    = blurRadius.height * aAppUnitsPerDevPixel;
  1.4942 +  result.right  = blurRadius.width  * aAppUnitsPerDevPixel;
  1.4943 +  result.bottom = blurRadius.height * aAppUnitsPerDevPixel;
  1.4944 +  result.left   = blurRadius.width  * aAppUnitsPerDevPixel;
  1.4945 +  return result;
  1.4946 +}
  1.4947 +
  1.4948 +/* static */ void
  1.4949 +nsContextBoxBlur::BlurRectangle(gfxContext* aDestinationCtx,
  1.4950 +                                const nsRect& aRect,
  1.4951 +                                int32_t aAppUnitsPerDevPixel,
  1.4952 +                                gfxCornerSizes* aCornerRadii,
  1.4953 +                                nscoord aBlurRadius,
  1.4954 +                                const gfxRGBA& aShadowColor,
  1.4955 +                                const nsRect& aDirtyRect,
  1.4956 +                                const gfxRect& aSkipRect)
  1.4957 +{
  1.4958 +  if (aRect.IsEmpty()) {
  1.4959 +    return;
  1.4960 +  }
  1.4961 +
  1.4962 +  gfxRect shadowGfxRect =
  1.4963 +    nsLayoutUtils::RectToGfxRect(aRect, aAppUnitsPerDevPixel);
  1.4964 +
  1.4965 +  if (aBlurRadius <= 0) {
  1.4966 +    aDestinationCtx->SetColor(aShadowColor);
  1.4967 +    aDestinationCtx->NewPath();
  1.4968 +    if (aCornerRadii) {
  1.4969 +      aDestinationCtx->RoundedRectangle(shadowGfxRect, *aCornerRadii);
  1.4970 +    } else {
  1.4971 +      aDestinationCtx->Rectangle(shadowGfxRect);
  1.4972 +    }
  1.4973 +
  1.4974 +    aDestinationCtx->Fill();
  1.4975 +    return;
  1.4976 +  }
  1.4977 +
  1.4978 +  gfxFloat scaleX = 1;
  1.4979 +  gfxFloat scaleY = 1;
  1.4980 +
  1.4981 +  // Do blurs in device space when possible.
  1.4982 +  // Chrome/Skia always does the blurs in device space
  1.4983 +  // and will sometimes get incorrect results (e.g. rotated blurs)
  1.4984 +  gfxMatrix transform = aDestinationCtx->CurrentMatrix();
  1.4985 +  // XXX: we could probably handle negative scales but for now it's easier just to fallback
  1.4986 +  if (!transform.HasNonAxisAlignedTransform() && transform.xx > 0.0 && transform.yy > 0.0) {
  1.4987 +    scaleX = transform.xx;
  1.4988 +    scaleY = transform.yy;
  1.4989 +    aDestinationCtx->IdentityMatrix();
  1.4990 +  } else {
  1.4991 +    transform = gfxMatrix();
  1.4992 +  }
  1.4993 +
  1.4994 +  gfxPoint blurStdDev = ComputeBlurStdDev(aBlurRadius, aAppUnitsPerDevPixel, scaleX, scaleY);
  1.4995 +
  1.4996 +  gfxRect dirtyRect =
  1.4997 +    nsLayoutUtils::RectToGfxRect(aDirtyRect, aAppUnitsPerDevPixel);
  1.4998 +  dirtyRect.RoundOut();
  1.4999 +
  1.5000 +  shadowGfxRect = transform.TransformBounds(shadowGfxRect);
  1.5001 +  dirtyRect = transform.TransformBounds(dirtyRect);
  1.5002 +  gfxRect skipRect = transform.TransformBounds(aSkipRect);
  1.5003 +
  1.5004 +  if (aCornerRadii) {
  1.5005 +    aCornerRadii->Scale(scaleX, scaleY);
  1.5006 +  }
  1.5007 +
  1.5008 +  gfxAlphaBoxBlur::BlurRectangle(aDestinationCtx,
  1.5009 +                                 shadowGfxRect,
  1.5010 +                                 aCornerRadii,
  1.5011 +                                 blurStdDev,
  1.5012 +                                 aShadowColor,
  1.5013 +                                 dirtyRect,
  1.5014 +                                 skipRect);
  1.5015 +}

mercurial