Bug 1083753, part 1 - Port the code that uses nsRenderingContext::FillRect(nscoord,...) to Moz2D. r=mattwoodrow
authorJonathan Watt <jwatt@jwatt.org>
Sun, 19 Oct 2014 11:55:36 +0100
changeset 211146 045556326c7cf9c0dff9430aaa286f0feda8cee6
parent 211145 2dbb3fbd7385ae8a8e0e29d9d1dc00829eaee2d9
child 211147 2cb4b5b083746d01421f6c38fa71d6212b02ec2d
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersmattwoodrow
bugs1083753
milestone36.0a1
Bug 1083753, part 1 - Port the code that uses nsRenderingContext::FillRect(nscoord,...) to Moz2D. r=mattwoodrow
gfx/src/nsRenderingContext.cpp
gfx/src/nsRenderingContext.h
layout/generic/nsBulletFrame.cpp
layout/generic/nsPlaceholderFrame.cpp
layout/inspector/inFlasher.cpp
layout/inspector/inFlasher.h
layout/xul/nsTextBoxFrame.cpp
layout/xul/tree/nsTreeBodyFrame.cpp
--- a/gfx/src/nsRenderingContext.cpp
+++ b/gfx/src/nsRenderingContext.cpp
@@ -254,23 +254,16 @@ nsRenderingContext::FillRect(const nsRec
         mThebes->SetMatrix(mat);
     }
 
     mThebes->NewPath();
     mThebes->Rectangle(r, true);
     mThebes->Fill();
 }
 
-void
-nsRenderingContext::FillRect(nscoord aX, nscoord aY,
-                             nscoord aWidth, nscoord aHeight)
-{
-    FillRect(nsRect(aX, aY, aWidth, aHeight));
-}
-
 //
 // text
 //
 
 void
 nsRenderingContext::SetTextRunRTL(bool aIsRTL)
 {
     mFontMetrics->SetTextRunRTL(aIsRTL);
--- a/gfx/src/nsRenderingContext.h
+++ b/gfx/src/nsRenderingContext.h
@@ -52,17 +52,16 @@ public:
     void IntersectClip(const nsRect& aRect);
     void SetColor(nscolor aColor);
 
     // Shapes
 
     void DrawLine(const nsPoint& aStartPt, const nsPoint& aEndPt);
     void DrawLine(nscoord aX0, nscoord aY0, nscoord aX1, nscoord aY1);
     void FillRect(const nsRect& aRect);
-    void FillRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
 
     // Text
 
     void SetFont(nsFontMetrics *aFontMetrics);
     nsFontMetrics *FontMetrics() { return mFontMetrics; } // may be null
 
     void SetTextRunRTL(bool aIsRTL);
 
--- a/layout/generic/nsBulletFrame.cpp
+++ b/layout/generic/nsBulletFrame.cpp
@@ -316,31 +316,32 @@ nsBulletFrame::PaintBullet(nsRenderingCo
     }
   }
 
   nsRefPtr<nsFontMetrics> fm;
   ColorPattern color(ToDeviceColor(
                        nsLayoutUtils::GetColor(this, eCSSProperty_color)));
   aRenderingContext.SetColor(nsLayoutUtils::GetColor(this, eCSSProperty_color));
 
+  DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
+  int32_t appUnitsPerDevPixel = PresContext()->AppUnitsPerDevPixel();
+
   nsAutoString text;
   switch (listStyleType->GetStyle()) {
   case NS_STYLE_LIST_STYLE_NONE:
     break;
 
   case NS_STYLE_LIST_STYLE_DISC:
   case NS_STYLE_LIST_STYLE_CIRCLE:
     {
       nsRect rect(padding.left + aPt.x,
                   padding.top + aPt.y,
                   mRect.width - (padding.left + padding.right),
                   mRect.height - (padding.top + padding.bottom));
-      Rect devPxRect =
-        ToRect(nsLayoutUtils::RectToGfxRect(rect, PresContext()->AppUnitsPerDevPixel()));
-      DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
+      Rect devPxRect = NSRectToRect(rect, appUnitsPerDevPixel, *drawTarget);
       RefPtr<PathBuilder> builder = drawTarget->CreatePathBuilder();
       AppendEllipseToPath(builder, devPxRect.Center(), devPxRect.Size());
       RefPtr<Path> ellipse = builder->Finish();
       if (listStyleType->GetStyle() == NS_STYLE_LIST_STYLE_DISC) {
         drawTarget->Fill(ellipse, color);
       } else {
         drawTarget->Stroke(ellipse, color);
       }
@@ -359,18 +360,18 @@ nsBulletFrame::PaintBullet(nsRenderingCo
       // FIXME: We should really only do this if we're not transformed
       // (like gfxContext::UserToDevicePixelSnapped does).
       nsPresContext *pc = PresContext();
       nsRect snapRect(rect.x, rect.y, 
                       pc->RoundAppUnitsToNearestDevPixels(rect.width),
                       pc->RoundAppUnitsToNearestDevPixels(rect.height));
       snapRect.MoveBy((rect.width - snapRect.width) / 2,
                       (rect.height - snapRect.height) / 2);
-      aRenderingContext.FillRect(snapRect.x, snapRect.y,
-                                 snapRect.width, snapRect.height);
+      Rect devPxRect = NSRectToRect(snapRect, appUnitsPerDevPixel, *drawTarget);
+      drawTarget->FillRect(devPxRect, color);
     }
     break;
 
   case NS_STYLE_LIST_STYLE_DISCLOSURE_CLOSED:
   case NS_STYLE_LIST_STYLE_DISCLOSURE_OPEN:
     {
       nsRect rect(aPt, mRect.Size());
       rect.Deflate(padding);
@@ -386,19 +387,17 @@ nsBulletFrame::PaintBullet(nsRenderingCo
         rect.height -= diff * 2;
       } else {
         rect.Deflate(diff, 0);
       }
       nsPresContext *pc = PresContext();
       rect.x = pc->RoundAppUnitsToNearestDevPixels(rect.x);
       rect.y = pc->RoundAppUnitsToNearestDevPixels(rect.y);
 
-      DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
       RefPtr<PathBuilder> builder = drawTarget->CreatePathBuilder();
-      int32_t appUnitsPerDevPixel = pc->AppUnitsPerDevPixel();
       if (isDown) {
         // to bottom
         builder->MoveTo(NSPointToPoint(rect.TopLeft(), appUnitsPerDevPixel));
         builder->LineTo(NSPointToPoint(rect.TopRight(), appUnitsPerDevPixel));
         builder->LineTo(NSPointToPoint((rect.BottomLeft() + rect.BottomRight()) / 2,
                                        appUnitsPerDevPixel));
       } else {
         bool isLR = isVertical ? wm.IsVerticalLR() : wm.IsBidiLTR();
--- a/layout/generic/nsPlaceholderFrame.cpp
+++ b/layout/generic/nsPlaceholderFrame.cpp
@@ -5,23 +5,28 @@
 
 /*
  * rendering object for the point that anchors out-of-flow rendering
  * objects such as floats and absolutely positioned elements
  */
 
 #include "nsPlaceholderFrame.h"
 
+#include "gfxUtils.h"
+#include "mozilla/gfx/2D.h"
 #include "nsDisplayList.h"
 #include "nsFrameManager.h"
 #include "nsLayoutUtils.h"
 #include "nsPresContext.h"
 #include "nsRenderingContext.h"
 #include "nsIFrameInlines.h"
 
+using namespace mozilla;
+using namespace mozilla::gfx;
+
 nsIFrame*
 NS_NewPlaceholderFrame(nsIPresShell* aPresShell, nsStyleContext* aContext,
                        nsFrameState aTypeBit)
 {
   return new (aPresShell) nsPlaceholderFrame(aContext, aTypeBit);
 }
 
 NS_IMPL_FRAMEARENA_HELPERS(nsPlaceholderFrame)
@@ -196,25 +201,31 @@ nsPlaceholderFrame::GetParentStyleContex
 }
 
 
 #ifdef DEBUG
 static void
 PaintDebugPlaceholder(nsIFrame* aFrame, nsRenderingContext* aCtx,
                       const nsRect& aDirtyRect, nsPoint aPt)
 {
-  aCtx->SetColor(NS_RGB(0, 255, 255));
+  ColorPattern cyan(ToDeviceColor(Color(0.f, 1.f, 1.f, 1.f)));
+  DrawTarget* drawTarget = aCtx->GetDrawTarget();
+  int32_t appUnitsPerDevPixel = aFrame->PresContext()->AppUnitsPerDevPixel();
+
   nscoord x = nsPresContext::CSSPixelsToAppUnits(-5);
-  aCtx->FillRect(aPt.x + x, aPt.y,
-                 nsPresContext::CSSPixelsToAppUnits(13),
-                 nsPresContext::CSSPixelsToAppUnits(3));
+  nsRect r(aPt.x + x, aPt.y,
+           nsPresContext::CSSPixelsToAppUnits(13),
+           nsPresContext::CSSPixelsToAppUnits(3));
+  drawTarget->FillRect(NSRectToRect(r, appUnitsPerDevPixel), cyan);
+
   nscoord y = nsPresContext::CSSPixelsToAppUnits(-10);
-  aCtx->FillRect(aPt.x, aPt.y + y,
-                 nsPresContext::CSSPixelsToAppUnits(3),
-                 nsPresContext::CSSPixelsToAppUnits(10));
+  r = nsRect(aPt.x, aPt.y + y,
+             nsPresContext::CSSPixelsToAppUnits(3),
+             nsPresContext::CSSPixelsToAppUnits(10));
+  drawTarget->FillRect(NSRectToRect(r, appUnitsPerDevPixel), cyan);
 }
 #endif // DEBUG
 
 #if defined(DEBUG) || (defined(MOZ_REFLOW_PERF_DSP) && defined(MOZ_REFLOW_PERF))
 
 void
 nsPlaceholderFrame::BuildDisplayList(nsDisplayListBuilder*   aBuilder,
                                      const nsRect&           aDirtyRect,
--- a/layout/inspector/inFlasher.cpp
+++ b/layout/inspector/inFlasher.cpp
@@ -129,41 +129,8 @@ inFlasher::ScrollElementIntoView(nsIDOME
   nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
   presShell->ScrollContentIntoView(content,
                                    nsIPresShell::ScrollAxis(),
                                    nsIPresShell::ScrollAxis(),
                                    nsIPresShell::SCROLL_OVERFLOW_HIDDEN);
 
   return NS_OK;
 }
-
-///////////////////////////////////////////////////////////////////////////////
-// inFlasher
-
-void
-inFlasher::DrawOutline(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight,
-                       nsRenderingContext* aRenderContext,
-                       bool aDrawBegin, bool aDrawEnd)
-{
-  aRenderContext->SetColor(mColor);
-
-  DrawLine(aX, aY, aWidth, DIR_HORIZONTAL, BOUND_OUTER, aRenderContext);
-  if (aDrawBegin) {
-    DrawLine(aX, aY, aHeight, DIR_VERTICAL, BOUND_OUTER, aRenderContext);
-  }
-  DrawLine(aX, aY+aHeight, aWidth, DIR_HORIZONTAL, BOUND_INNER, aRenderContext);
-  if (aDrawEnd) {
-    DrawLine(aX+aWidth, aY, aHeight, DIR_VERTICAL, BOUND_INNER, aRenderContext);
-  }
-}
-
-void
-inFlasher::DrawLine(nscoord aX, nscoord aY, nscoord aLength,
-                    bool aDir, bool aBounds,
-                    nsRenderingContext* aRenderContext)
-{
-  nscoord thickTwips = nsPresContext::CSSPixelsToAppUnits(mThickness);
-  if (aDir) { // horizontal
-    aRenderContext->FillRect(aX, aY+(aBounds?0:-thickTwips), aLength, thickTwips);
-  } else { // vertical
-    aRenderContext->FillRect(aX+(aBounds?0:-thickTwips), aY, thickTwips, aLength);
-  }
-}
--- a/layout/inspector/inFlasher.h
+++ b/layout/inspector/inFlasher.h
@@ -23,23 +23,16 @@ public:
   NS_DECL_ISUPPORTS
   NS_DECL_INIFLASHER
 
   inFlasher();
 
 protected:
   virtual ~inFlasher();
 
-  void DrawOutline(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight,
-                   nsRenderingContext* aRenderContext,
-                   bool aDrawBegin, bool aDrawEnd);
-  void DrawLine(nscoord aX, nscoord aY, nscoord aLength,
-                bool aDir, bool aBounds,
-                nsRenderingContext* aRenderContext);
-
   nscolor mColor;
 
   uint16_t mThickness;
   bool mInvert;
 };
 
 // {9286E71A-621A-4b91-851E-9984C1A2E81A}
 #define IN_FLASHER_CID \
--- a/layout/xul/nsTextBoxFrame.cpp
+++ b/layout/xul/nsTextBoxFrame.cpp
@@ -1,15 +1,17 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsTextBoxFrame.h"
 
+#include "gfxUtils.h"
+#include "mozilla/gfx/2D.h"
 #include "nsReadableUtils.h"
 #include "nsCOMPtr.h"
 #include "nsGkAtoms.h"
 #include "nsPresContext.h"
 #include "nsRenderingContext.h"
 #include "nsStyleContext.h"
 #include "nsIContent.h"
 #include "nsNameSpaceManager.h"
@@ -34,16 +36,17 @@
 #ifdef ACCESSIBILITY
 #include "nsAccessibilityService.h"
 #endif
 
 #include "nsBidiUtils.h"
 #include "nsBidiPresUtils.h"
 
 using namespace mozilla;
+using namespace mozilla::gfx;
 
 class nsAccessKeyInfo
 {
 public:
     int32_t mAccesskeyIndex;
     nscoord mBeforeWidth, mAccessWidth, mAccessUnderlineSize, mAccessOffset;
 };
 
@@ -380,16 +383,18 @@ nsTextBoxFrame::PaintTitle(nsRenderingCo
 
 void
 nsTextBoxFrame::DrawText(nsRenderingContext& aRenderingContext,
                          const nsRect&       aDirtyRect,
                          const nsRect&       aTextRect,
                          const nscolor*      aOverrideColor)
 {
     nsPresContext* presContext = PresContext();
+    int32_t appUnitsPerDevPixel = presContext->AppUnitsPerDevPixel();
+    DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
 
     // paint the title
     nscolor overColor;
     nscolor underColor;
     nscolor strikeColor;
     uint8_t overStyle;
     uint8_t underStyle;
     uint8_t strikeStyle;
@@ -495,17 +500,19 @@ nsTextBoxFrame::DrawText(nsRenderingCont
     nsRefPtr<nsRenderingContext> refContext =
         PresContext()->PresShell()->CreateReferenceRenderingContext();
 
     aRenderingContext.SetFont(fontMet);
     refContext->SetFont(fontMet);
 
     CalculateUnderline(*refContext);
 
-    aRenderingContext.SetColor(aOverrideColor ? *aOverrideColor : StyleColor()->mColor);
+    nscolor c = aOverrideColor ? *aOverrideColor : StyleColor()->mColor;
+    ColorPattern color(ToDeviceColor(c));
+    aRenderingContext.SetColor(c);
 
     nsresult rv = NS_ERROR_FAILURE;
 
     if (mState & NS_FRAME_IS_BIDI) {
       presContext->SetBidiEnabled();
       nsBidiLevel level = nsBidiPresUtils::BidiLevelFromStyle(StyleContext());
       if (mAccessKeyInfo && mAccessKeyInfo->mAccesskeyIndex != kNotFound) {
           // We let the RenderText function calculate the mnemonic's
@@ -545,20 +552,22 @@ nsTextBoxFrame::DrawText(nsRenderingCont
        }
 
        fontMet->DrawString(mCroppedTitle.get(), mCroppedTitle.Length(),
                            aTextRect.x, baseline, &aRenderingContext,
                            refContext.get());
     }
 
     if (mAccessKeyInfo && mAccessKeyInfo->mAccesskeyIndex != kNotFound) {
-        aRenderingContext.FillRect(aTextRect.x + mAccessKeyInfo->mBeforeWidth,
-                                   aTextRect.y + mAccessKeyInfo->mAccessOffset,
-                                   mAccessKeyInfo->mAccessWidth,
-                                   mAccessKeyInfo->mAccessUnderlineSize);
+      nsRect r(aTextRect.x + mAccessKeyInfo->mBeforeWidth,
+               aTextRect.y + mAccessKeyInfo->mAccessOffset,
+               mAccessKeyInfo->mAccessWidth,
+               mAccessKeyInfo->mAccessUnderlineSize);
+      Rect devPxRect = NSRectToRect(r, appUnitsPerDevPixel, *drawTarget);
+      drawTarget->FillRect(devPxRect, color);
     }
 
     // Strikeout is drawn on top of the text, per
     // http://www.w3.org/TR/CSS21/zindex.html point 7.2.1.4.1.1.
     if ((decorations & NS_FONT_DECORATION_LINE_THROUGH) &&
         strikeStyle != NS_STYLE_TEXT_DECORATION_STYLE_NONE) {
       fontMet->GetStrikeout(offset, size);
       gfxFloat offsetPixel = presContext->AppUnitsToGfxUnits(offset);
--- a/layout/xul/tree/nsTreeBodyFrame.cpp
+++ b/layout/xul/tree/nsTreeBodyFrame.cpp
@@ -3555,16 +3555,19 @@ nsTreeBodyFrame::PaintText(int32_t      
   mView->GetCellText(aRowIndex, aColumn, text);
   // We're going to paint this text so we need to ensure bidi is enabled if
   // necessary
   CheckTextForBidi(text);
 
   if (text.Length() == 0)
     return; // Don't paint an empty string. XXX What about background/borders? Still paint?
 
+  int32_t appUnitsPerDevPixel = PresContext()->AppUnitsPerDevPixel();
+  DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
+
   // Resolve style for the text.  It contains all the info we need to lay ourselves
   // out and to paint.
   nsStyleContext* textContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecelltext);
 
   // Obtain opacity value for the image.
   float opacity = textContext->StyleDisplay()->mOpacity;
 
   // Obtain the margins for the text and then deflate our rect by that 
@@ -3604,33 +3607,43 @@ nsTreeBodyFrame::PaintText(int32_t      
     aCurrX += textRect.width + textMargin.LeftRight();
 
   PaintBackgroundLayer(textContext, aPresContext, aRenderingContext, textRect, aDirtyRect);
 
   // Time to paint our text.
   textRect.Deflate(bp);
 
   // Set our color.
+  ColorPattern color(ToDeviceColor(textContext->StyleColor()->mColor));
   aRenderingContext.SetColor(textContext->StyleColor()->mColor);
 
   // Draw decorations.
   uint8_t decorations = textContext->StyleTextReset()->mTextDecorationLine;
 
   nscoord offset;
   nscoord size;
   if (decorations & (NS_FONT_DECORATION_OVERLINE | NS_FONT_DECORATION_UNDERLINE)) {
     fontMet->GetUnderline(offset, size);
-    if (decorations & NS_FONT_DECORATION_OVERLINE)
-      aRenderingContext.FillRect(textRect.x, textRect.y, textRect.width, size);
-    if (decorations & NS_FONT_DECORATION_UNDERLINE)
-      aRenderingContext.FillRect(textRect.x, textRect.y + baseline - offset, textRect.width, size);
+    if (decorations & NS_FONT_DECORATION_OVERLINE) {
+      nsRect r(textRect.x, textRect.y, textRect.width, size);
+      Rect devPxRect = NSRectToRect(r, appUnitsPerDevPixel, *drawTarget);
+      drawTarget->FillRect(devPxRect, color);
+    }
+    if (decorations & NS_FONT_DECORATION_UNDERLINE) {
+      nsRect r(textRect.x, textRect.y + baseline - offset,
+               textRect.width, size);
+      Rect devPxRect = NSRectToRect(r, appUnitsPerDevPixel, *drawTarget);
+      drawTarget->FillRect(devPxRect, color);
+    }
   }
   if (decorations & NS_FONT_DECORATION_LINE_THROUGH) {
     fontMet->GetStrikeout(offset, size);
-    aRenderingContext.FillRect(textRect.x, textRect.y + baseline - offset, textRect.width, size);
+    nsRect r(textRect.x, textRect.y + baseline - offset, textRect.width, size);
+    Rect devPxRect = NSRectToRect(r, appUnitsPerDevPixel, *drawTarget);
+    drawTarget->FillRect(devPxRect, color);
   }
   nsStyleContext* cellContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecell);
 
   gfxContext* ctx = aRenderingContext.ThebesContext();
   if (opacity != 1.0f) {
     ctx->PushGroup(gfxContentType::COLOR_ALPHA);
   }