Start removal of tool usage of SkSurface::MakeFromBackendTextureAsRenderTarget.

Adds a new helper that creates a GrBackendRenderTarget using
GrGpu and then wraps it in a SkSurface. Uses the SkSurface
release proc to delete the BERT using GrGpu.

Upgrades GrGpu::createTestingOnlyBackendRenderTarget to create MSAA
buffers.

Updates many tests/tool to call sites to use the helper instead of
SkSurface::MakeFromBackendTextureAsRenderTarget.

Adds syncToCpu bool to SkSurface:: and GrContext::flushAndSubmit.

Bug: skia:9832

Change-Id: I73a8f0ce09dc6523729af0814464c6b6657fda06
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/293683
Commit-Queue: Brian Salomon <bsalomon@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
Reviewed-by: Jim Van Verth <jvanverth@google.com>
Reviewed-by: Kevin Lubick <kjlubick@google.com>
diff --git a/src/gpu/GrSurfaceContext.cpp b/src/gpu/GrSurfaceContext.cpp
index e7303c0..d24d7af 100644
--- a/src/gpu/GrSurfaceContext.cpp
+++ b/src/gpu/GrSurfaceContext.cpp
@@ -207,45 +207,75 @@
     }
 
     if (readFlag == GrCaps::SurfaceReadPixelsSupport::kCopyToTexture2D || canvas2DFastPath) {
-        GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
-                                    ? GrColorType::kRGBA_8888 : this->colorInfo().colorType();
-        sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorInfo().refColorSpace();
+        std::unique_ptr<GrSurfaceContext> tempCtx;
+        if (this->asTextureProxy()) {
+            GrColorType colorType = (canvas2DFastPath || srcIsCompressed)
+                                            ? GrColorType::kRGBA_8888
+                                            : this->colorInfo().colorType();
+            sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorInfo().refColorSpace();
 
-        auto tempCtx = GrRenderTargetContext::Make(
-                dContext, colorType, std::move(cs), SkBackingFit::kApprox, dstInfo.dimensions(),
-                1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
-        if (!tempCtx) {
-            return false;
-        }
-
-        std::unique_ptr<GrFragmentProcessor> fp;
-        if (canvas2DFastPath) {
-            fp = dContext->priv().createPMToUPMEffect(
-                    GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType()));
-            if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
-                fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
-                dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
+            tempCtx = GrRenderTargetContext::Make(
+                    dContext, colorType, std::move(cs), SkBackingFit::kApprox, dstInfo.dimensions(),
+                    1, GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
+            if (!tempCtx) {
+                return false;
             }
-            // The render target context is incorrectly tagged as kPremul even though we're writing
-            // unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type so we don't
-            // double unpremul.
-            dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
+
+            std::unique_ptr<GrFragmentProcessor> fp;
+            if (canvas2DFastPath) {
+                fp = dContext->priv().createPMToUPMEffect(GrTextureEffect::Make(
+                        this->readSurfaceView(), this->colorInfo().alphaType()));
+                if (dstInfo.colorType() == GrColorType::kBGRA_8888) {
+                    fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
+                    dstInfo = dstInfo.makeColorType(GrColorType::kRGBA_8888);
+                }
+                // The render target context is incorrectly tagged as kPremul even though we're
+                // writing unpremul data thanks to the PMToUPM effect. Fake out the dst alpha type
+                // so we don't double unpremul.
+                dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
+            } else {
+                fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
+            }
+            if (!fp) {
+                return false;
+            }
+            GrPaint paint;
+            paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
+            paint.setColorFragmentProcessor(std::move(fp));
+
+            tempCtx->asRenderTargetContext()->fillRectToRect(
+                    nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
+                    SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
+                    SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
+            pt = {0, 0};
         } else {
-            fp = GrTextureEffect::Make(this->readSurfaceView(), this->colorInfo().alphaType());
+            auto restrictions = this->caps()->getDstCopyRestrictions(this->asRenderTargetProxy(),
+                                                                     this->colorInfo().colorType());
+            sk_sp<GrSurfaceProxy> copy;
+            static constexpr auto kFit = SkBackingFit::kExact;
+            static constexpr auto kBudgeted = SkBudgeted::kYes;
+            static constexpr auto kMipMapped = GrMipMapped::kNo;
+            if (restrictions.fMustCopyWholeSrc) {
+                copy = GrSurfaceProxy::Copy(fContext, srcProxy, this->origin(), kMipMapped, kFit,
+                                            kBudgeted);
+            } else {
+                auto srcRect = SkIRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height());
+                copy = GrSurfaceProxy::Copy(fContext, srcProxy, this->origin(), kMipMapped, srcRect,
+                                            kFit, kBudgeted, restrictions.fRectsMustMatch);
+                pt = {0, 0};
+            }
+            if (!copy) {
+                return false;
+            }
+            GrSurfaceProxyView view{std::move(copy), this->origin(), this->readSwizzle()};
+            tempCtx = GrSurfaceContext::Make(dContext,
+                                             std::move(view),
+                                             this->colorInfo().colorType(),
+                                             this->colorInfo().alphaType(),
+                                             this->colorInfo().refColorSpace());
+            SkASSERT(tempCtx);
         }
-        if (!fp) {
-            return false;
-        }
-        GrPaint paint;
-        paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
-        paint.setColorFragmentProcessor(std::move(fp));
-
-        tempCtx->asRenderTargetContext()->fillRectToRect(
-                nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(),
-                SkRect::MakeWH(dstInfo.width(), dstInfo.height()),
-                SkRect::MakeXYWH(pt.fX, pt.fY, dstInfo.width(), dstInfo.height()));
-
-        return tempCtx->readPixels(dContext, dstInfo, dst, rowBytes, {0, 0});
+        return tempCtx->readPixels(dContext, dstInfo, dst, rowBytes, pt);
     }
 
     bool flip = this->origin() == kBottomLeft_GrSurfaceOrigin;