Miscellaneous GrSurfaceProxy-related cleanup

This is pulled out of: https://skia-review.googlesource.com/c/10284/ (Remove GrSurface-derived classes from ops)

Change-Id: I083c0beefe4899b3517d0b0569bb25096809f410
Reviewed-on: https://skia-review.googlesource.com/10483
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/src/core/SkSpecialImage.cpp b/src/core/SkSpecialImage.cpp
index 339341a..fad5d14 100644
--- a/src/core/SkSpecialImage.cpp
+++ b/src/core/SkSpecialImage.cpp
@@ -17,6 +17,7 @@
 
 #if SK_SUPPORT_GPU
 #include "GrContext.h"
+#include "GrContextPriv.h"
 #include "GrResourceProvider.h"
 #include "GrSurfaceContext.h"
 #include "GrSurfaceProxyPriv.h"
@@ -416,14 +417,13 @@
             return false;
         }
 
-        // Reading back to an SkBitmap ends deferral
-        GrTexture* texture = fTextureProxy->instantiate(fContext->resourceProvider());
-        if (!texture) {
+        sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext(
+                                                                          fTextureProxy, nullptr);
+        if (!sContext) {
             return false;
         }
 
-        if (!texture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig,
-                                 dst->getPixels(), dst->rowBytes())) {
+        if (!sContext->readPixels(info, dst->getPixels(), dst->rowBytes(), 0, 0)) {
             return false;
         }
 
diff --git a/src/core/SkSpecialSurface.cpp b/src/core/SkSpecialSurface.cpp
index 381e3d8..1f396a7 100644
--- a/src/core/SkSpecialSurface.cpp
+++ b/src/core/SkSpecialSurface.cpp
@@ -136,7 +136,7 @@
     ~SkSpecialSurface_Gpu() override { }
 
     sk_sp<SkSpecialImage> onMakeImageSnapshot() override {
-        if (!fRenderTargetContext->asTexture()) {
+        if (!fRenderTargetContext->asTextureProxy()) {
             return nullptr;
         }
         sk_sp<SkSpecialImage> tmp(SkSpecialImage::MakeDeferredFromGpu(
diff --git a/src/gpu/GrXferProcessor.cpp b/src/gpu/GrXferProcessor.cpp
index 45bcc2d..f206028 100644
--- a/src/gpu/GrXferProcessor.cpp
+++ b/src/gpu/GrXferProcessor.cpp
@@ -71,7 +71,7 @@
         SkASSERT(caps.textureBarrierSupport());
         return kTexture_GrXferBarrierType;
     }
-    return this->onXferBarrier(rt, caps);
+    return this->onXferBarrier(caps);
 }
 
 #ifdef SK_DEBUG
diff --git a/src/gpu/GrXferProcessor.h b/src/gpu/GrXferProcessor.h
index 54630af..2898d7f 100644
--- a/src/gpu/GrXferProcessor.h
+++ b/src/gpu/GrXferProcessor.h
@@ -209,7 +209,7 @@
      * that a kTexture type barrier is required is handled by the base class and need not be
      * considered by subclass overrides of this function.
      */
-    virtual GrXferBarrierType onXferBarrier(const GrRenderTarget*, const GrCaps&) const {
+    virtual GrXferBarrierType onXferBarrier(const GrCaps&) const {
         return kNone_GrXferBarrierType;
     }
 
diff --git a/src/gpu/effects/GrConfigConversionEffect.cpp b/src/gpu/effects/GrConfigConversionEffect.cpp
index 2c6589d..790a474 100644
--- a/src/gpu/effects/GrConfigConversionEffect.cpp
+++ b/src/gpu/effects/GrConfigConversionEffect.cpp
@@ -220,7 +220,7 @@
         // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
         // We then verify that two reads produced the same values.
 
-        if (!readRTC->asTexture()) {
+        if (!readRTC->asTextureProxy()) {
             continue;
         }
         GrPaint paint1;
diff --git a/src/gpu/effects/GrCustomXfermode.cpp b/src/gpu/effects/GrCustomXfermode.cpp
index 2e6d27c..55b73e0 100644
--- a/src/gpu/effects/GrCustomXfermode.cpp
+++ b/src/gpu/effects/GrCustomXfermode.cpp
@@ -100,7 +100,7 @@
 private:
     void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
 
-    GrXferBarrierType onXferBarrier(const GrRenderTarget*, const GrCaps&) const override;
+    GrXferBarrierType onXferBarrier(const GrCaps&) const override;
 
     void onGetBlendInfo(BlendInfo*) const override;
 
@@ -187,7 +187,7 @@
     return fMode == s.fMode && fHWBlendEquation == s.fHWBlendEquation;
 }
 
-GrXferBarrierType CustomXP::onXferBarrier(const GrRenderTarget* rt, const GrCaps& caps) const {
+GrXferBarrierType CustomXP::onXferBarrier(const GrCaps& caps) const {
     if (this->hasHWBlendEquation() && !caps.advancedCoherentBlendEquationSupport()) {
         return kBlend_GrXferBarrierType;
     }
diff --git a/tests/ClearTest.cpp b/tests/ClearTest.cpp
index d4b8587..843c4c3 100644
--- a/tests/ClearTest.cpp
+++ b/tests/ClearTest.cpp
@@ -17,12 +17,17 @@
 
 static bool check_rect(GrRenderTargetContext* rtc, const SkIRect& rect, uint32_t expectedValue,
                        uint32_t* actualValue, int* failX, int* failY) {
-    GrRenderTarget* rt = rtc->accessRenderTarget();
     int w = rect.width();
     int h = rect.height();
     std::unique_ptr<uint32_t[]> pixels(new uint32_t[w * h]);
     memset(pixels.get(), ~expectedValue, sizeof(uint32_t) * w * h);
-    rt->readPixels(rect.fLeft, rect.fTop, w, h, kRGBA_8888_GrPixelConfig, pixels.get());
+
+    SkImageInfo dstInfo = SkImageInfo::Make(w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
+
+    if (!rtc->readPixels(dstInfo, pixels.get(), 0, rect.fLeft, rect.fTop)) {
+        return false;
+    }
+
     for (int y = 0; y < h; ++y) {
         for (int x = 0; x < w; ++x) {
             uint32_t pixel = pixels.get()[y * w + x];
diff --git a/tests/PackedConfigsTextureTest.cpp b/tests/PackedConfigsTextureTest.cpp
index 4b8e3cd..7a51651 100644
--- a/tests/PackedConfigsTextureTest.cpp
+++ b/tests/PackedConfigsTextureTest.cpp
@@ -99,7 +99,7 @@
 void runTest(skiatest::Reporter* reporter, GrContext* context,
              T val1, T val2, int arraySize, GrPixelConfig config) {
     SkTDArray<T> controlPixelData;
-    // We will read back into an 8888 buffer since 565/4444 read backes aren't supported
+    // We will read back into an 8888 buffer since 565/4444 read backs aren't supported
     SkTDArray<GrColor> readBuffer;
     controlPixelData.setCount(arraySize);
     readBuffer.setCount(arraySize);
@@ -115,8 +115,7 @@
         desc.fWidth = DEV_W;
         desc.fHeight = DEV_H;
         desc.fConfig = config;
-        desc.fOrigin = 0 == origin ?
-            kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
+        desc.fOrigin = 0 == origin ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
         sk_sp<GrTexture> fpTexture(context->resourceProvider()->createTexture(
             desc, SkBudgeted::kNo, controlPixelData.begin(), 0));
         SkASSERT(fpTexture);
diff --git a/tests/SRGBMipMapTest.cpp b/tests/SRGBMipMapTest.cpp
index 89a47c9..54c68ae 100644
--- a/tests/SRGBMipMapTest.cpp
+++ b/tests/SRGBMipMapTest.cpp
@@ -13,6 +13,7 @@
 #include "GrRenderTargetContext.h"
 #include "GrResourceProvider.h"
 #include "SkCanvas.h"
+#include "SkGr.h"
 #include "SkSurface.h"
 #include "gl/GrGLGpu.h"
 
@@ -45,16 +46,19 @@
     }
 }
 
-void read_and_check_pixels(skiatest::Reporter* reporter, GrTexture* texture, U8CPU expected,
+void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context,
+                           U8CPU expected, const SkImageInfo& dstInfo,
                            U8CPU error, const char* subtestName) {
-    int w = texture->width();
-    int h = texture->height();
+    int w = dstInfo.width();
+    int h = dstInfo.height();
     SkAutoTMalloc<uint32_t> readData(w * h);
     memset(readData.get(), 0, sizeof(uint32_t) * w * h);
-    if (!texture->readPixels(0, 0, w, h, texture->config(), readData.get())) {
+
+    if (!context->readPixels(dstInfo, readData.get(), 0, 0, 0)) {
         ERRORF(reporter, "Could not read pixels for %s.", subtestName);
         return;
     }
+
     for (int j = 0; j < h; ++j) {
         for (int i = 0; i < w; ++i) {
             uint32_t read = readData[j * w + i];
@@ -112,6 +116,12 @@
     const U8CPU expectedLinear = srgb60 / 2;
     const U8CPU error = 10;
 
+    const SkImageInfo iiSRGBA = SkImageInfo::Make(rtS, rtS, kRGBA_8888_SkColorType,
+                                                  kPremul_SkAlphaType,
+                                                  SkColorSpace::MakeSRGB());
+    const SkImageInfo iiRGBA = SkImageInfo::Make(rtS, rtS, kRGBA_8888_SkColorType,
+                                                 kPremul_SkAlphaType);
+
     // Create our test texture
     GrSurfaceDesc desc;
     desc.fFlags = kNone_GrSurfaceFlags;
@@ -142,7 +152,7 @@
     // 1) Draw texture to S32 surface (should generate/use sRGB mips)
     paint.setGammaCorrect(true);
     s32RenderTargetContext->drawRect(noClip, GrPaint(paint), GrAA::kNo, SkMatrix::I(), rect);
-    read_and_check_pixels(reporter, s32RenderTargetContext->asTexture().get(), expectedSRGB, error,
+    read_and_check_pixels(reporter, s32RenderTargetContext.get(), expectedSRGB, iiSRGBA, error,
                           "first render of sRGB");
 
     // 2) Draw texture to L32 surface (should generate/use linear mips)
@@ -161,14 +171,14 @@
     GrGLGpu* glGpu = static_cast<GrGLGpu*>(context->getGpu());
     if (glGpu->glCaps().srgbDecodeDisableSupport() &&
         glGpu->glCaps().srgbDecodeDisableAffectsMipmaps()) {
-        read_and_check_pixels(reporter, l32RenderTargetContext->asTexture().get(), expectedLinear,
+        read_and_check_pixels(reporter, l32RenderTargetContext.get(), expectedLinear, iiRGBA,
                               error, "re-render as linear");
     }
 
     // 3) Go back to sRGB
     paint.setGammaCorrect(true);
     s32RenderTargetContext->drawRect(noClip, std::move(paint), GrAA::kNo, SkMatrix::I(), rect);
-    read_and_check_pixels(reporter, s32RenderTargetContext->asTexture().get(), expectedSRGB, error,
+    read_and_check_pixels(reporter, s32RenderTargetContext.get(), expectedSRGB, iiSRGBA, error,
                           "re-render as sRGB");
 }
 #endif
diff --git a/tests/SRGBReadWritePixelsTest.cpp b/tests/SRGBReadWritePixelsTest.cpp
index 5addc2e..9729774 100644
--- a/tests/SRGBReadWritePixelsTest.cpp
+++ b/tests/SRGBReadWritePixelsTest.cpp
@@ -9,8 +9,11 @@
 #if SK_SUPPORT_GPU
 #include "GrCaps.h"
 #include "GrContext.h"
+#include "GrContextPriv.h"
 #include "GrResourceProvider.h"
+#include "GrSurfaceContext.h"
 #include "SkCanvas.h"
+#include "SkGr.h"
 #include "SkSurface.h"
 
 // using anonymous namespace because these functions are used as template params.
@@ -112,17 +115,20 @@
 
 typedef bool (*CheckFn) (uint32_t orig, uint32_t actual, float error);
 
-void read_and_check_pixels(skiatest::Reporter* reporter, GrTexture* texture, uint32_t* origData,
-                           GrPixelConfig readConfig, CheckFn checker, float error,
+void read_and_check_pixels(skiatest::Reporter* reporter, GrSurfaceContext* context,
+                           uint32_t* origData,
+                           const SkImageInfo& dstInfo, CheckFn checker, float error,
                            const char* subtestName) {
-    int w = texture->width();
-    int h = texture->height();
+    int w = dstInfo.width();
+    int h = dstInfo.height();
     SkAutoTMalloc<uint32_t> readData(w * h);
     memset(readData.get(), 0, sizeof(uint32_t) * w * h);
-    if (!texture->readPixels(0, 0, w, h, readConfig, readData.get())) {
+
+    if (!context->readPixels(dstInfo, readData.get(), 0, 0, 0)) {
         ERRORF(reporter, "Could not read pixels for %s.", subtestName);
         return;
     }
+
     for (int j = 0; j < h; ++j) {
         for (int i = 0; i < w; ++i) {
             uint32_t orig = origData[j * w + i];
@@ -156,6 +162,11 @@
         }
     }
 
+    const SkImageInfo iiSRGBA = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType,
+                                                  kPremul_SkAlphaType,
+                                                  SkColorSpace::MakeSRGB());
+    const SkImageInfo iiRGBA = SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType,
+                                                 kPremul_SkAlphaType);
     GrSurfaceDesc desc;
     desc.fFlags = kRenderTarget_GrSurfaceFlag;
     desc.fWidth = kW;
@@ -163,25 +174,27 @@
     desc.fConfig = kSRGBA_8888_GrPixelConfig;
     if (context->caps()->isConfigRenderable(desc.fConfig, false) &&
         context->caps()->isConfigTexturable(desc.fConfig)) {
-        sk_sp<GrTexture> tex(context->resourceProvider()->createTexture(desc, SkBudgeted::kNo));
-        if (!tex) {
-            ERRORF(reporter, "Could not create SRGBA texture.");
+
+        sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext(
+                                                                    desc, SkBackingFit::kExact,
+                                                                    SkBudgeted::kNo);
+        if (!sContext) {
+            ERRORF(reporter, "Could not create SRGBA surface context.");
             return;
         }
 
         float error = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.2f  : 0.5f;
 
         // Write srgba data and read as srgba and then as rgba
-        if (tex->writePixels(0, 0, kW, kH, kSRGBA_8888_GrPixelConfig, origData)) {
+        if (sContext->writePixels(iiSRGBA, origData, 0, 0, 0)) {
             // For the all-srgba case, we allow a small error only for devices that have
             // precision variation because the srgba data gets converted to linear and back in
             // the shader.
-            float smallError = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.f :
-                    0.0f;
-            read_and_check_pixels(reporter, tex.get(), origData, kSRGBA_8888_GrPixelConfig,
+            float smallError = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.f : 0.0f;
+            read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
                                   check_srgb_to_linear_to_srgb_conversion, smallError,
                                   "write/read srgba to srgba texture");
-            read_and_check_pixels(reporter, tex.get(), origData, kRGBA_8888_GrPixelConfig,
+            read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA,
                                   check_srgb_to_linear_conversion, error,
                                   "write srgba/read rgba with srgba texture");
         } else {
@@ -189,12 +202,12 @@
         }
 
         // Now verify that we can write linear data
-        if (tex->writePixels(0, 0, kW, kH, kRGBA_8888_GrPixelConfig, origData)) {
+        if (sContext->writePixels(iiRGBA, origData, 0, 0, 0)) {
             // We allow more error on GPUs with lower precision shader variables.
-            read_and_check_pixels(reporter, tex.get(), origData, kSRGBA_8888_GrPixelConfig,
+            read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
                                   check_linear_to_srgb_conversion, error,
                                   "write rgba/read srgba with srgba texture");
-            read_and_check_pixels(reporter, tex.get(), origData, kRGBA_8888_GrPixelConfig,
+            read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA,
                                   check_linear_to_srgb_to_linear_conversion, error,
                                   "write/read rgba with srgba texture");
         } else {
@@ -202,18 +215,19 @@
         }
 
         desc.fConfig = kRGBA_8888_GrPixelConfig;
-        tex.reset(context->resourceProvider()->createTexture(desc, SkBudgeted::kNo));
-        if (!tex) {
-            ERRORF(reporter, "Could not create RGBA texture.");
+        sContext = context->contextPriv().makeDeferredSurfaceContext(desc, SkBackingFit::kExact,
+                                                                     SkBudgeted::kNo);
+        if (!sContext) {
+            ERRORF(reporter, "Could not create RGBA surface context.");
             return;
         }
 
         // Write srgba data to a rgba texture and read back as srgba and rgba
-        if (tex->writePixels(0, 0, kW, kH, kSRGBA_8888_GrPixelConfig, origData)) {
-            read_and_check_pixels(reporter, tex.get(), origData, kSRGBA_8888_GrPixelConfig,
+        if (sContext->writePixels(iiSRGBA, origData, 0, 0, 0)) {
+            read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
                                   check_srgb_to_linear_to_srgb_conversion, error,
                                   "write/read srgba to rgba texture");
-            read_and_check_pixels(reporter, tex.get(), origData, kRGBA_8888_GrPixelConfig,
+            read_and_check_pixels(reporter, sContext.get(), origData, iiRGBA,
                                   check_srgb_to_linear_conversion, error,
                                   "write srgba/read rgba to rgba texture");
         } else {
@@ -221,8 +235,8 @@
         }
 
         // Write rgba data to a rgba texture and read back as srgba
-        if (tex->writePixels(0, 0, kW, kH, kRGBA_8888_GrPixelConfig, origData)) {
-            read_and_check_pixels(reporter, tex.get(), origData, kSRGBA_8888_GrPixelConfig,
+        if (sContext->writePixels(iiRGBA, origData, 0, 0, 0)) {
+            read_and_check_pixels(reporter, sContext.get(), origData, iiSRGBA,
                                   check_linear_to_srgb_conversion, 1.2f,
                                   "write rgba/read srgba to rgba texture");
         } else {