Cherry-pick of https://skia.googlesource.com/skia.git/+/96c118edff293af93db0a2b1b6775428117924b1 to m39 branch

Change GrContext::copyTexture to go through GrDrawTarget

BUG=crbug.com/415100
R=bsalomon@google.com, robertphillips@google.com

Author: junov@chromium.org

Review URL: https://codereview.chromium.org/607993002

Conflicts:
	src/gpu/GrContext.cpp

Review URL: https://codereview.chromium.org/608353002
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 091c4a8..9628645 100755
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -1556,17 +1556,6 @@
     }
     ASSERT_OWNED_RESOURCE(src);
 
-    // Writes pending to the source texture are not tracked, so a flush
-    // is required to ensure that the copy captures the most recent contents
-    // of the source texture. See similar behavior in
-    // GrContext::resolveRenderTarget.
-    this->flush();
-
-    GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
-    GrDrawState* drawState = fGpu->drawState();
-    drawState->setRenderTarget(dst);
-    SkMatrix sampleM;
-    sampleM.setIDiv(src->width(), src->height());
     SkIRect srcRect = SkIRect::MakeWH(dst->width(), dst->height());
     if (NULL != topLeft) {
         srcRect.offset(*topLeft);
@@ -1575,10 +1564,14 @@
     if (!srcRect.intersect(srcBounds)) {
         return;
     }
-    sampleM.preTranslate(SkIntToScalar(srcRect.fLeft), SkIntToScalar(srcRect.fTop));
-    drawState->addColorTextureEffect(src, sampleM);
-    SkRect dstR = SkRect::MakeWH(SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height()));
-    fGpu->drawSimpleRect(dstR);
+
+    GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, NULL, NULL);
+    if (NULL == target) {
+        return;
+    }
+    SkIPoint dstPoint;
+    dstPoint.setZero();
+    target->copySurface(dst, src, srcRect, dstPoint);
 }
 
 bool GrContext::writeRenderTargetPixels(GrRenderTarget* target,
diff --git a/src/gpu/SkGrPixelRef.cpp b/src/gpu/SkGrPixelRef.cpp
index 2131f41..b064b7e 100644
--- a/src/gpu/SkGrPixelRef.cpp
+++ b/src/gpu/SkGrPixelRef.cpp
@@ -51,7 +51,7 @@
 
 ///////////////////////////////////////////////////////////////////////////////
 
-static SkGrPixelRef* copyToTexturePixelRef(GrTexture* texture, SkColorType dstCT,
+static SkGrPixelRef* copy_to_new_texture_pixelref(GrTexture* texture, SkColorType dstCT,
                                            const SkIRect* subset) {
     if (NULL == texture || kUnknown_SkColorType == dstCT) {
         return NULL;
@@ -86,15 +86,10 @@
 
     context->copyTexture(texture, dst->asRenderTarget(), topLeft);
 
-    // TODO: figure out if this is responsible for Chrome canvas errors
-#if 0
-    // The render texture we have created (to perform the copy) isn't fully
-    // functional (since it doesn't have a stencil buffer). Release it here
-    // so the caller doesn't try to render to it.
-    // TODO: we can undo this release when dynamic stencil buffer attach/
-    // detach has been implemented
-    dst->releaseRenderTarget();
-#endif
+    // Blink is relying on the above copy being sent to GL immediately in the case when the source
+    // is a WebGL canvas backing store. We could have a TODO to remove this flush, but we have a
+    // larger TODO to remove SkGrPixelRef entirely.
+    context->flush();
 
     SkImageInfo info = SkImageInfo::Make(desc.fWidth, desc.fHeight, dstCT, kPremul_SkAlphaType);
     SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (info, dst));
@@ -156,7 +151,7 @@
     // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live
     // independently of that texture.  Texture-backed pixel refs, on the other
     // hand, own their GrTextures, and are thus self-contained.
-    return copyToTexturePixelRef(fSurface->asTexture(), dstCT, subset);
+    return copy_to_new_texture_pixelref(fSurface->asTexture(), dstCT, subset);
 }
 
 bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) {