Bug 579276. Part 11: Move ClipToRegion to gfxUtils. r=vlad
authorRobert O'Callahan <robert@ocallahan.org>
Fri, 03 Sep 2010 14:31:42 +1200
changeset 51956 e3d31ba6030cdd06c7d91ccac5ad301a2a7a0e6c
parent 51955 2a3bda7f342e65b16ba8cd2e323d5485d85ddf52
child 51957 c888cdffd617d4e698f05180e31effa78353289b
push idunknown
push userunknown
push dateunknown
reviewersvlad
bugs579276
milestone2.0b6pre
Bug 579276. Part 11: Move ClipToRegion to gfxUtils. r=vlad
gfx/layers/ThebesLayerBuffer.cpp
gfx/layers/ThebesLayerBuffer.h
gfx/layers/basic/BasicLayers.cpp
gfx/thebes/gfxUtils.cpp
gfx/thebes/gfxUtils.h
--- a/gfx/layers/ThebesLayerBuffer.cpp
+++ b/gfx/layers/ThebesLayerBuffer.cpp
@@ -34,33 +34,21 @@
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "ThebesLayerBuffer.h"
 #include "Layers.h"
 #include "gfxContext.h"
 #include "gfxPlatform.h"
+#include "gfxUtils.h"
 
 namespace mozilla {
 namespace layers {
 
-/*static*/ void
-ThebesLayerBuffer::ClipToRegion(gfxContext* aContext,
-                                const nsIntRegion& aRegion)
-{
-  aContext->NewPath();
-  nsIntRegionRectIterator iter(aRegion);
-  const nsIntRect* r;
-  while ((r = iter.Next()) != nsnull) {
-    aContext->Rectangle(gfxRect(r->x, r->y, r->width, r->height));
-  }
-  aContext->Clip();
-}
-
 nsIntRect
 ThebesLayerBuffer::GetQuadrantRectangle(XSide aXSide, YSide aYSide)
 {
   // quadrantTranslation is the amount we translate the top-left
   // of the quadrant by to get coordinates relative to the layer
   nsIntPoint quadrantTranslation = -mBufferRotation;
   quadrantTranslation.x += aXSide == LEFT ? mBufferRect.width : 0;
   quadrantTranslation.y += aYSide == TOP ? mBufferRect.height : 0;
@@ -233,17 +221,17 @@ ThebesLayerBuffer::BeginPaint(ThebesLaye
   PRInt32 xBoundary = mBufferRect.XMost() - mBufferRotation.x;
   PRInt32 yBoundary = mBufferRect.YMost() - mBufferRotation.y;
   XSide sideX = drawBounds.XMost() <= xBoundary ? RIGHT : LEFT;
   YSide sideY = drawBounds.YMost() <= yBoundary ? BOTTOM : TOP;
   nsIntRect quadrantRect = GetQuadrantRectangle(sideX, sideY);
   NS_ASSERTION(quadrantRect.Contains(drawBounds), "Messed up quadrants");
   result.mContext->Translate(-gfxPoint(quadrantRect.x, quadrantRect.y));
 
-  ClipToRegion(result.mContext, result.mRegionToDraw);
+  gfxUtils::ClipToRegion(result.mContext, result.mRegionToDraw);
   if (aContentType == gfxASurface::CONTENT_COLOR_ALPHA && !isClear) {
     result.mContext->SetOperator(gfxContext::OPERATOR_CLEAR);
     result.mContext->Paint();
     result.mContext->SetOperator(gfxContext::OPERATOR_OVER);
   }
   return result;
 }
 
--- a/gfx/layers/ThebesLayerBuffer.h
+++ b/gfx/layers/ThebesLayerBuffer.h
@@ -135,19 +135,16 @@ public:
   /**
    * Get the underlying buffer, if any. This is useful because we can pass
    * in the buffer as the default "reference surface" if there is one.
    * Don't use it for anything else!
    */
   gfxASurface* GetBuffer() { return mBuffer; }
 
 protected:
-  // XXX make me a general utility
-  static void ClipToRegion(gfxContext* aContext, const nsIntRegion& aRegion);
-
   enum XSide {
     LEFT, RIGHT
   };
   enum YSide {
     TOP, BOTTOM
   };
   nsIntRect GetQuadrantRectangle(XSide aXSide, YSide aYSide);
   void DrawBufferQuadrant(gfxContext* aTarget, XSide aXSide, YSide aYSide, float aOpacity);
--- a/gfx/layers/basic/BasicLayers.cpp
+++ b/gfx/layers/basic/BasicLayers.cpp
@@ -449,17 +449,17 @@ BasicThebesLayer::Paint(gfxContext* aCon
 
 void
 BasicThebesLayerBuffer::DrawTo(ThebesLayer* aLayer,
                                PRBool aIsOpaqueContent,
                                gfxContext* aTarget,
                                float aOpacity)
 {
   aTarget->Save();
-  ClipToRegion(aTarget, aLayer->GetVisibleRegion());
+  gfxUtils::ClipToRegion(aTarget, aLayer->GetVisibleRegion());
   if (aIsOpaqueContent) {
     aTarget->SetOperator(gfxContext::OPERATOR_SOURCE);
   }
   DrawBufferWithRotation(aTarget, aOpacity);
   aTarget->Restore();
 }
 
 already_AddRefed<gfxASurface>
--- a/gfx/thebes/gfxUtils.cpp
+++ b/gfx/thebes/gfxUtils.cpp
@@ -34,16 +34,17 @@
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "gfxUtils.h"
 #include "gfxContext.h"
 #include "gfxPlatform.h"
 #include "gfxDrawable.h"
+#include "nsRegion.h"
 
 #if defined(XP_WIN) || defined(WINCE)
 #include "gfxWindowsPlatform.h"
 #endif
 
 static PRUint8 sUnpremultiplyTable[256*256];
 static PRUint8 sPremultiplyTable[256*256];
 static PRBool sTablesInitialized = PR_FALSE;
@@ -413,8 +414,32 @@ gfxUtils::DrawPixelSnapped(gfxContext*  
         aContext->SetOperator(OptimalFillOperator());
     }
 
     drawable->Draw(aContext, aFill, doTile, aFilter, aUserSpaceToImageSpace);
 
     aContext->SetOperator(op);
 }
 
+static void
+ClipToRegionInternal(gfxContext* aContext, const nsIntRegion& aRegion,
+                     PRBool aSnap)
+{
+  aContext->NewPath();
+  nsIntRegionRectIterator iter(aRegion);
+  const nsIntRect* r;
+  while ((r = iter.Next()) != nsnull) {
+    aContext->Rectangle(gfxRect(r->x, r->y, r->width, r->height), aSnap);
+  }
+  aContext->Clip();
+}
+
+/*static*/ void
+gfxUtils::ClipToRegion(gfxContext* aContext, const nsIntRegion& aRegion)
+{
+  ClipToRegionInternal(aContext, aRegion, PR_FALSE);
+}
+
+/*static*/ void
+gfxUtils::ClipToRegionSnapped(gfxContext* aContext, const nsIntRegion& aRegion)
+{
+  ClipToRegionInternal(aContext, aRegion, PR_TRUE);
+}
--- a/gfx/thebes/gfxUtils.h
+++ b/gfx/thebes/gfxUtils.h
@@ -38,16 +38,17 @@
 #ifndef GFX_UTILS_H
 #define GFX_UTILS_H
 
 #include "gfxTypes.h"
 #include "gfxPattern.h"
 #include "gfxImageSurface.h"
 
 class gfxDrawable;
+class nsIntRegion;
 
 class THEBES_API gfxUtils {
 public:
     /*
      * Premultiply or Unpremultiply aSourceSurface, writing the result
      * to aDestSurface or back into aSourceSurface if aDestSurface is null.
      *
      * If aDestSurface is given, it must have identical format, dimensions, and
@@ -78,11 +79,21 @@ public:
                                  gfxDrawable*     aDrawable,
                                  const gfxMatrix& aUserSpaceToImageSpace,
                                  const gfxRect&   aSubimage,
                                  const gfxRect&   aSourceRect,
                                  const gfxRect&   aImageRect,
                                  const gfxRect&   aFill,
                                  const gfxImageSurface::gfxImageFormat aFormat,
                                  const gfxPattern::GraphicsFilter& aFilter);
+
+    /**
+     * Clip aContext to the region aRegion.
+     */
+    static void ClipToRegion(gfxContext* aContext, const nsIntRegion& aRegion);
+
+    /**
+     * Clip aContext to the region aRegion, snapping the rectangles.
+     */
+    static void ClipToRegionSnapped(gfxContext* aContext, const nsIntRegion& aRegion);
 };
 
 #endif