Make GrIODB record and play back copySurface.
Review URL: https://codereview.chromium.org/13581003

git-svn-id: http://skia.googlecode.com/svn/trunk@8574 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/GrDrawTarget.cpp b/src/gpu/GrDrawTarget.cpp
index a0c15dd..d07d625 100644
--- a/src/gpu/GrDrawTarget.cpp
+++ b/src/gpu/GrDrawTarget.cpp
@@ -751,52 +751,76 @@
     target->setClip(&fReplacementClip);
 }
 
-bool GrDrawTarget::copySurface(GrSurface* dst,
-                               GrSurface* src,
+namespace {
+// returns true if the read/written rect intersects the src/dst and false if not.
+bool clip_srcrect_and_dstpoint(const GrSurface* dst,
+                               const GrSurface* src,
                                const SkIRect& srcRect,
-                               const SkIPoint& dstPoint) {
-    SkIRect clippedSrcRect(srcRect);
-    SkIPoint clippedDstPoint(dstPoint);
+                               const SkIPoint& dstPoint,
+                               SkIRect* clippedSrcRect,
+                               SkIPoint* clippedDstPoint) {
+    *clippedSrcRect = srcRect;
+    *clippedDstPoint = dstPoint;
 
-    // clip the left edge to src and dst bounds, adjusting dstPoint if neceessary
-    if (clippedSrcRect.fLeft < 0) {
-        clippedDstPoint.fX -= clippedSrcRect.fLeft;
-        clippedSrcRect.fLeft = 0;
+    // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
+    if (clippedSrcRect->fLeft < 0) {
+        clippedDstPoint->fX -= clippedSrcRect->fLeft;
+        clippedSrcRect->fLeft = 0;
     }
-    if (clippedDstPoint.fX < 0) {
-        clippedSrcRect.fLeft -= clippedDstPoint.fX;
-        clippedDstPoint.fX = 0;
+    if (clippedDstPoint->fX < 0) {
+        clippedSrcRect->fLeft -= clippedDstPoint->fX;
+        clippedDstPoint->fX = 0;
     }
 
-    // clip the top edge to src and dst bounds, adjusting dstPoint if neceessary
-    if (clippedSrcRect.fTop < 0) {
-        clippedDstPoint.fY -= clippedSrcRect.fTop;
-        clippedSrcRect.fTop = 0;
+    // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
+    if (clippedSrcRect->fTop < 0) {
+        clippedDstPoint->fY -= clippedSrcRect->fTop;
+        clippedSrcRect->fTop = 0;
     }
-    if (clippedDstPoint.fY < 0) {
-        clippedSrcRect.fTop -= clippedDstPoint.fY;
-        clippedDstPoint.fY = 0;
+    if (clippedDstPoint->fY < 0) {
+        clippedSrcRect->fTop -= clippedDstPoint->fY;
+        clippedDstPoint->fY = 0;
     }
 
     // clip the right edge to the src and dst bounds.
-    if (clippedSrcRect.fRight > src->width()) {
-        clippedSrcRect.fRight = src->width();
+    if (clippedSrcRect->fRight > src->width()) {
+        clippedSrcRect->fRight = src->width();
     }
-    if (clippedDstPoint.fX + clippedSrcRect.width() > dst->width()) {
-        clippedSrcRect.fRight = clippedSrcRect.fLeft + dst->width() - clippedDstPoint.fX;
+    if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
+        clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
     }
 
     // clip the bottom edge to the src and dst bounds.
-    if (clippedSrcRect.fBottom > src->height()) {
-        clippedSrcRect.fBottom = src->height();
+    if (clippedSrcRect->fBottom > src->height()) {
+        clippedSrcRect->fBottom = src->height();
     }
-    if (clippedDstPoint.fY + clippedSrcRect.height() > dst->height()) {
-        clippedSrcRect.fBottom = clippedSrcRect.fTop + dst->height() - clippedDstPoint.fY;
+    if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
+        clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
     }
 
     // The above clipping steps may have inverted the rect if it didn't intersect either the src or
     // dst bounds.
-    if (clippedSrcRect.isEmpty()) {
+    return !clippedSrcRect->isEmpty();
+}
+}
+
+bool GrDrawTarget::copySurface(GrSurface* dst,
+                               GrSurface* src,
+                               const SkIRect& srcRect,
+                               const SkIPoint& dstPoint) {
+    GrAssert(NULL != dst);
+    GrAssert(NULL != src);
+
+    SkIRect clippedSrcRect;
+    SkIPoint clippedDstPoint;
+    // If the rect is outside the src or dst then we've already succeeded.
+    if (!clip_srcrect_and_dstpoint(dst,
+                                   src,
+                                   srcRect,
+                                   dstPoint,
+                                   &clippedSrcRect,
+                                   &clippedDstPoint)) {
+        GrAssert(this->canCopySurface(dst, src, srcRect, dstPoint));
         return true;
     }
 
@@ -809,6 +833,27 @@
                                   GrSurface* src,
                                   const SkIRect& srcRect,
                                   const SkIPoint& dstPoint) {
+    GrAssert(NULL != dst);
+    GrAssert(NULL != src);
+
+    SkIRect clippedSrcRect;
+    SkIPoint clippedDstPoint;
+    // If the rect is outside the src or dst then we're guaranteed success
+    if (!clip_srcrect_and_dstpoint(dst,
+                                   src,
+                                   srcRect,
+                                   dstPoint,
+                                   &clippedSrcRect,
+                                   &clippedDstPoint)) {
+        return true;
+    }
+    return this->onCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
+}
+
+bool GrDrawTarget::onCanCopySurface(GrSurface* dst,
+                                    GrSurface* src,
+                                    const SkIRect& srcRect,
+                                    const SkIPoint& dstPoint) {
     // Check that the read/write rects are contained within the src/dst bounds.
     GrAssert(!srcRect.isEmpty());
     GrAssert(SkIRect::MakeWH(src->width(), src->height()).contains(srcRect));
@@ -816,14 +861,14 @@
     GrAssert(dstPoint.fX + srcRect.width() <= dst->width() &&
              dstPoint.fY + srcRect.height() <= dst->height());
 
-    return NULL != dst->asRenderTarget() && NULL != src->asTexture();
+    return !dst->isSameAs(src) && NULL != dst->asRenderTarget() && NULL != src->asTexture();
 }
 
 bool GrDrawTarget::onCopySurface(GrSurface* dst,
                                  GrSurface* src,
                                  const SkIRect& srcRect,
                                  const SkIPoint& dstPoint) {
-    if (!GrDrawTarget::canCopySurface(dst, src, srcRect, dstPoint)) {
+    if (!GrDrawTarget::onCanCopySurface(dst, src, srcRect, dstPoint)) {
         return false;
     }