Bug 1083597 - Port the code that uses nsRenderingContext::DrawRect() to Moz2D. r=mattwoodrow
authorJonathan Watt <jwatt@jwatt.org>
Fri, 17 Oct 2014 15:06:34 +0100
changeset 211043 4714ac6ccbfe6d715edaf82ac1e6f71cecafd13e
parent 211042 01662ca4cb404b0c718eeab29261b992f3321b65
child 211044 78f725c6441db73e22b886040a0782f11478bbe0
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersmattwoodrow
bugs1083597
milestone36.0a1
Bug 1083597 - Port the code that uses nsRenderingContext::DrawRect() to Moz2D. r=mattwoodrow
gfx/src/nsRenderingContext.cpp
gfx/src/nsRenderingContext.h
layout/generic/nsFrame.cpp
layout/mathml/nsMathMLFrame.cpp
layout/tables/nsTableCellFrame.cpp
layout/xul/nsBoxFrame.cpp
--- a/gfx/src/nsRenderingContext.cpp
+++ b/gfx/src/nsRenderingContext.cpp
@@ -158,31 +158,16 @@ nsRenderingContext::DrawLine(nscoord aX0
         mThebes->SetMatrix(savedMatrix);
     } else {
         mThebes->NewPath();
         mThebes->Line(p0, p1);
         mThebes->Stroke();
     }
 }
 
-void
-nsRenderingContext::DrawRect(const nsRect& aRect)
-{
-    mThebes->NewPath();
-    mThebes->Rectangle(GFX_RECT_FROM_TWIPS_RECT(aRect), true);
-    mThebes->Stroke();
-}
-
-void
-nsRenderingContext::DrawRect(nscoord aX, nscoord aY,
-                             nscoord aWidth, nscoord aHeight)
-{
-    DrawRect(nsRect(aX, aY, aWidth, aHeight));
-}
-
 
 /* Clamp r to (0,0) (2^23,2^23)
  * these are to be device coordinates.
  *
  * Returns false if the rectangle is completely out of bounds,
  * true otherwise.
  *
  * This function assumes that it will be called with a rectangle being
--- a/gfx/src/nsRenderingContext.h
+++ b/gfx/src/nsRenderingContext.h
@@ -51,19 +51,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 DrawRect(const nsRect& aRect);
-    void DrawRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
-
     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
 
--- a/layout/generic/nsFrame.cpp
+++ b/layout/generic/nsFrame.cpp
@@ -6,18 +6,20 @@
 
 /* base class of all rendering objects */
 
 #include "nsFrame.h"
 
 #include <stdarg.h>
 #include <algorithm>
 
+#include "gfxUtils.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/DebugOnly.h"
+#include "mozilla/gfx/2D.h"
 
 #include "nsCOMPtr.h"
 #include "nsFrameList.h"
 #include "nsPlaceholderFrame.h"
 #include "nsIContent.h"
 #include "nsContentUtils.h"
 #include "nsIAtom.h"
 #include "nsString.h"
@@ -1715,29 +1717,31 @@ ApplyOverflowClipping(nsDisplayListBuild
   }
   aClipState.ClipContainingBlockDescendantsExtra(clipRect, haveRadii ? radii : nullptr);
 }
 
 #ifdef DEBUG
 static void PaintDebugBorder(nsIFrame* aFrame, nsRenderingContext* aCtx,
      const nsRect& aDirtyRect, nsPoint aPt) {
   nsRect r(aPt, aFrame->GetSize());
-  if (aFrame->HasView()) {
-    aCtx->SetColor(NS_RGB(0,0,255));
-  } else {
-    aCtx->SetColor(NS_RGB(255,0,0));
-  }
-  aCtx->DrawRect(r);
+  DrawTarget* drawTarget = aCtx->GetDrawTarget();
+  int32_t appUnitsPerDevPixel = aFrame->PresContext()->AppUnitsPerDevPixel();
+  Color blueOrRed(aFrame->HasView() ? Color(0.f, 0.f, 1.f, 1.f) :
+                                      Color(1.f, 0.f, 0.f, 1.f));
+  drawTarget->StrokeRect(NSRectToRect(r, appUnitsPerDevPixel),
+                         ColorPattern(ToDeviceColor(blueOrRed)));
 }
 
 static void PaintEventTargetBorder(nsIFrame* aFrame, nsRenderingContext* aCtx,
      const nsRect& aDirtyRect, nsPoint aPt) {
   nsRect r(aPt, aFrame->GetSize());
-  aCtx->SetColor(NS_RGB(128,0,128));
-  aCtx->DrawRect(r);
+  DrawTarget* drawTarget = aCtx->GetDrawTarget();
+  int32_t appUnitsPerDevPixel = aFrame->PresContext()->AppUnitsPerDevPixel();
+  ColorPattern purple(ToDeviceColor(Color(.5f, 0.f, .5f, 1.f)));
+  drawTarget->StrokeRect(NSRectToRect(r, appUnitsPerDevPixel), purple);
 }
 
 static void
 DisplayDebugBorders(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame,
                     const nsDisplayListSet& aLists) {
   // Draw a border around the child
   // REVIEW: From nsContainerFrame::PaintChild
   if (nsFrame::GetShowFrameBorders() && !aFrame->GetRect().IsEmpty()) {
--- a/layout/mathml/nsMathMLFrame.cpp
+++ b/layout/mathml/nsMathMLFrame.cpp
@@ -1,25 +1,32 @@
 /* -*- 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 "nsMathMLFrame.h"
+
+#include "gfxUtils.h"
+#include "mozilla/gfx/2D.h"
+#include "nsLayoutUtils.h"
 #include "nsNameSpaceManager.h"
 #include "nsMathMLChar.h"
 #include "nsCSSPseudoElements.h"
 #include "nsMathMLElement.h"
 
 // used to map attributes into CSS rules
 #include "nsStyleSet.h"
 #include "nsAutoPtr.h"
 #include "nsDisplayList.h"
 #include "nsRenderingContext.h"
 
+using namespace mozilla;
+using namespace mozilla::gfx;
+
 eMathMLFrameType
 nsMathMLFrame::GetMathMLFrameType()
 {
   // see if it is an embellished operator (mapped to 'Op' in TeX)
   if (mEmbellishData.coreFrame)
     return GetMathMLFrameTypeFor(mEmbellishData.coreFrame);
 
   // if it has a prescribed base, fetch the type from there
@@ -304,18 +311,21 @@ public:
   NS_DISPLAY_DECL_NAME("MathMLBoundingMetrics", TYPE_MATHML_BOUNDING_METRICS)
 private:
   nsRect    mRect;
 };
 
 void nsDisplayMathMLBoundingMetrics::Paint(nsDisplayListBuilder* aBuilder,
                                            nsRenderingContext* aCtx)
 {
-  aCtx->SetColor(NS_RGB(0,0,255));
-  aCtx->DrawRect(mRect + ToReferenceFrame());
+  DrawTarget* drawTarget = aRenderingContext->GetDrawTarget();
+  Rect r = NSRectToRect(mRect + ToReferenceFrame(),
+                        mFrame->PresContext()->AppUnitsPerDevPixel());
+  ColorPattern blue(ToDeviceColor(Color(0.f, 0.f, 1.f, 1.f)));
+  drawTarget->StrokeRect(r, blue);
 }
 
 nsresult
 nsMathMLFrame::DisplayBoundingMetrics(nsDisplayListBuilder* aBuilder,
                                       nsIFrame* aFrame, const nsPoint& aPt,
                                       const nsBoundingMetrics& aMetrics,
                                       const nsDisplayListSet& aLists) {
   if (!NS_MATHML_PAINT_BOUNDING_METRICS(mPresentationData.flags))
--- a/layout/tables/nsTableCellFrame.cpp
+++ b/layout/tables/nsTableCellFrame.cpp
@@ -1,15 +1,20 @@
 /* -*- 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 "nsTableCellFrame.h"
+
+#include "gfxUtils.h"
+#include "mozilla/gfx/2D.h"
+#include "mozilla/gfx/Helpers.h"
 #include "nsTableFrame.h"
 #include "nsTableColFrame.h"
-#include "nsTableCellFrame.h"
 #include "nsTableRowFrame.h"
 #include "nsTableRowGroupFrame.h"
 #include "nsTablePainter.h"
 #include "nsStyleContext.h"
 #include "nsStyleConsts.h"
 #include "nsPresContext.h"
 #include "nsRenderingContext.h"
 #include "nsCSSRendering.h"
@@ -29,17 +34,17 @@
 #include "FrameLayerBuilder.h"
 #include <algorithm>
 
 //TABLECELL SELECTION
 #include "nsFrameSelection.h"
 #include "mozilla/LookAndFeel.h"
 
 using namespace mozilla;
-
+using namespace mozilla::gfx;
 
 nsTableCellFrame::nsTableCellFrame(nsStyleContext* aContext) :
   nsContainerFrame(aContext)
   , mDesiredSize(GetWritingMode())
 {
   mColIndex = 0;
   mPriorAvailWidth = 0;
 
@@ -313,35 +318,37 @@ nsTableCellFrame::DecorateForSelection(n
       }
       nscoord threePx = nsPresContext::CSSPixelsToAppUnits(3);
       if ((mRect.width > threePx) && (mRect.height > threePx))
       {
         //compare bordercolor to ((nsStyleColor *)myColor)->mBackgroundColor)
         bordercolor = EnsureDifferentColors(bordercolor,
                                             StyleBackground()->mBackgroundColor);
 
-        gfxContext* ctx = aRenderingContext.ThebesContext();
+        int32_t appUnitsPerDevPixel = PresContext()->AppUnitsPerDevPixel();
+        Point devPixelOffset = NSPointToPoint(aPt, appUnitsPerDevPixel);
 
-        gfxPoint devPixelOffset =
-          nsLayoutUtils::PointToGfxPoint(aPt,
-                                         PresContext()->AppUnitsPerDevPixel());
+        DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
+        AutoRestoreTransform autoRestoreTransform(drawTarget);
+        drawTarget->SetTransform(
+          drawTarget->GetTransform().PreTranslate(devPixelOffset));
 
-        gfxContextMatrixAutoSaveRestore autoSR(ctx);
-        ctx->SetMatrix(ctx->CurrentMatrix().Translate(devPixelOffset));
+        ColorPattern color(ToDeviceColor(bordercolor));
 
         nscoord onePixel = nsPresContext::CSSPixelsToAppUnits(1);
 
         aRenderingContext.SetColor(bordercolor);
         aRenderingContext.DrawLine(onePixel, 0, mRect.width, 0);
         aRenderingContext.DrawLine(0, onePixel, 0, mRect.height);
         aRenderingContext.DrawLine(onePixel, mRect.height, mRect.width, mRect.height);
         aRenderingContext.DrawLine(mRect.width, onePixel, mRect.width, mRect.height);
         //middle
-        aRenderingContext.DrawRect(onePixel, onePixel, mRect.width-onePixel,
-                                   mRect.height-onePixel);
+        nsRect r(onePixel, onePixel,
+                 mRect.width - onePixel, mRect.height - onePixel);
+        drawTarget->StrokeRect(NSRectToRect(r, appUnitsPerDevPixel), color);
         //shading
         aRenderingContext.DrawLine(2*onePixel, mRect.height-2*onePixel,
                                    mRect.width-onePixel, mRect.height- (2*onePixel));
         aRenderingContext.DrawLine(mRect.width - (2*onePixel), 2*onePixel,
                                    mRect.width - (2*onePixel), mRect.height-onePixel);
       }
     }
   }
--- a/layout/xul/nsBoxFrame.cpp
+++ b/layout/xul/nsBoxFrame.cpp
@@ -26,18 +26,21 @@
 
 // Boxes and Incremental Reflow
 // ----------------------------
 // Boxes layout out top down by adding up their children's min, max, and preferred sizes. Only problem is if a incremental
 // reflow occurs. The preferred size of a child deep in the hierarchy could change. And this could change
 // any number of syblings around the box. Basically any children in the reflow chain must have their caches cleared
 // so when asked for there current size they can relayout themselves. 
 
+#include "nsBoxFrame.h"
+
+#include "gfxUtils.h"
+#include "mozilla/gfx/2D.h"
 #include "nsBoxLayoutState.h"
-#include "nsBoxFrame.h"
 #include "mozilla/dom/Touch.h"
 #include "nsStyleContext.h"
 #include "nsPlaceholderFrame.h"
 #include "nsPresContext.h"
 #include "nsCOMPtr.h"
 #include "nsNameSpaceManager.h"
 #include "nsGkAtoms.h"
 #include "nsIContent.h"
@@ -66,16 +69,17 @@
 
 // Needed for Print Preview
 #include "nsIURI.h"
 
 #include "mozilla/TouchEvents.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
+using namespace mozilla::gfx;
 
 //define DEBUG_REDRAW
 
 #define DEBUG_SPRING_SIZE 8
 #define DEBUG_BORDER_SIZE 2
 #define COIL_SIZE 8
 
 //#define TEST_SANITY
@@ -1424,55 +1428,51 @@ nsBoxFrame::PaintXULDebugBackground(nsRe
   PixelMarginToTwips(GetPresContext(), debugPadding);
 
   nsRect inner(mRect);
   inner.MoveTo(aPt);
   inner.Deflate(debugMargin);
   inner.Deflate(border);
   //nsRect borderRect(inner);
 
-  nscolor color;
-  if (isHorizontal) {
-    color = NS_RGB(0,0,255);
-  } else {
-    color = NS_RGB(255,0,0);
-  }
+  int32_t appUnitsPerDevPixel = PresContext()->AppUnitsPerDevPixel();
 
-  aRenderingContext.SetColor(color);
+  ColorPattern color(ToDeviceColor(isHorizontal ? Color(0.f, 0.f, 1.f, 1.f) :
+                                                  Color(1.f, 0.f, 0.f, 1.f));
+
+  DrawTarget* drawTarget = aRenderingContext.GetDrawTarget();
 
   //left
   nsRect r(inner);
   r.width = debugBorder.left;
-  aRenderingContext.FillRect(r);
+  drawTarget->FillRect(NSRectToRect(r, appUnitsPerDevPixel), color);
 
   // top
   r = inner;
   r.height = debugBorder.top;
-  aRenderingContext.FillRect(r);
+  drawTarget->FillRect(NSRectToRect(r, appUnitsPerDevPixel), color);
 
   //right
   r = inner;
   r.x = r.x + r.width - debugBorder.right;
   r.width = debugBorder.right;
-  aRenderingContext.FillRect(r);
+  drawTarget->FillRect(NSRectToRect(r, appUnitsPerDevPixel), color);
 
   //bottom
   r = inner;
   r.y = r.y + r.height - debugBorder.bottom;
   r.height = debugBorder.bottom;
-  aRenderingContext.FillRect(r);
-
+  drawTarget->FillRect(NSRectToRect(r, appUnitsPerDevPixel), color);
   
   // if we have dirty children or we are dirty 
   // place a green border around us.
   if (NS_SUBTREE_DIRTY(this)) {
-     nsRect dirtyr(inner);
-     aRenderingContext.SetColor(NS_RGB(0,255,0));
-     aRenderingContext.DrawRect(dirtyr);
-     aRenderingContext.SetColor(color);
+    nsRect dirty(inner);
+    ColorPattern green(ToDeviceColor(Color0.f, 1.f, 0.f, 1.f)));
+    drawTarget->StrokeRect(NSRectToRect(dirty, appUnitsPerDevPixel), green);
   }
 }
 
 void
 nsBoxFrame::PaintXULDebugOverlay(nsRenderingContext& aRenderingContext,
                                  nsPoint aPt)
   nsMargin border;
   GetBorder(border);