Add utilities to create GrSurfaceProxy & GrSurfaceContext from backend descs

Change-Id: Iff2278de8ddd6c0dff74e5cf1996702bad31217b
Reviewed-on: https://skia-review.googlesource.com/7647
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/include/private/GrSurfaceProxy.h b/include/private/GrSurfaceProxy.h
index 6f2a751..a08cb26 100644
--- a/include/private/GrSurfaceProxy.h
+++ b/include/private/GrSurfaceProxy.h
@@ -178,6 +178,11 @@
                                               const GrSurfaceDesc&, SkBudgeted,
                                               const void* srcData, size_t rowBytes);
 
+    static sk_sp<GrSurfaceProxy> MakeWrappedBackend(
+                                            GrContext*,
+                                            GrBackendTextureDesc&,
+                                            GrWrapOwnership ownership = kBorrow_GrWrapOwnership);
+
     const GrSurfaceDesc& desc() const { return fDesc; }
 
     GrSurfaceOrigin origin() const {
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index db8863f..88b14f3 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -628,6 +628,21 @@
     return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
 }
 
+sk_sp<GrSurfaceContext> GrContextPriv::makeBackendSurfaceContext(const GrBackendTextureDesc& desc,
+                                                                 sk_sp<SkColorSpace> colorSpace,
+                                                                 GrWrapOwnership ownership) {
+    ASSERT_SINGLE_OWNER_PRIV
+
+    sk_sp<GrSurface> surface(fContext->textureProvider()->wrapBackendTexture(desc, ownership));
+    if (!surface) {
+        return nullptr;
+    }
+
+    sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
+
+    return this->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace));
+}
+
 sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
                                                                    const GrBackendTextureDesc& desc,
                                                                    sk_sp<SkColorSpace> colorSpace,
diff --git a/src/gpu/GrContextPriv.h b/src/gpu/GrContextPriv.h
index ed9691c..8ad3483 100644
--- a/src/gpu/GrContextPriv.h
+++ b/src/gpu/GrContextPriv.h
@@ -35,11 +35,11 @@
                                                        SkBackingFit dstFit,
                                                        SkBudgeted isDstBudgeted);
 
-    // TODO: add:
-    // sk_sp<GrSurfaceContext> makeBackendSurfaceContext(const GrBackendTextureDesc& desc,
-    //                                                   sk_sp<SkColorSpace> colorSpace,
-    //                                                   GrWrapOwnership = kBorrow_GrWrapOwnership);
-    // Maybe add a 'surfaceProps' param that is ignored for non-RTs?
+    // TODO: Maybe add a 'surfaceProps' param (that is ignored for non-RTs) and remove
+    // makeBackendTextureRenderTargetContext & makeBackendTextureAsRenderTargetRenderTargetContext
+    sk_sp<GrSurfaceContext> makeBackendSurfaceContext(const GrBackendTextureDesc& desc,
+                                                      sk_sp<SkColorSpace> colorSpace,
+                                                      GrWrapOwnership = kBorrow_GrWrapOwnership);
 
     sk_sp<GrRenderTargetContext> makeBackendTextureRenderTargetContext(
                                                          const GrBackendTextureDesc& desc,
diff --git a/src/gpu/GrSurfaceProxy.cpp b/src/gpu/GrSurfaceProxy.cpp
index 4cf4611..1e9ac9b 100644
--- a/src/gpu/GrSurfaceProxy.cpp
+++ b/src/gpu/GrSurfaceProxy.cpp
@@ -159,6 +159,13 @@
     return GrSurfaceProxy::MakeDeferred(caps, desc, SkBackingFit::kExact, budgeted);
 }
 
+sk_sp<GrSurfaceProxy> GrSurfaceProxy::MakeWrappedBackend(GrContext* context,
+                                                         GrBackendTextureDesc& desc,
+                                                         GrWrapOwnership ownership) {
+    sk_sp<GrTexture> tex(context->textureProvider()->wrapBackendTexture(desc, ownership));
+    return GrSurfaceProxy::MakeWrapped(std::move(tex));
+}
+
 #ifdef SK_DEBUG
 void GrSurfaceProxy::validate(GrContext* context) const {
     if (fTarget) {
diff --git a/src/image/SkImage_Gpu.cpp b/src/image/SkImage_Gpu.cpp
index ec1968f..8a27836 100644
--- a/src/image/SkImage_Gpu.cpp
+++ b/src/image/SkImage_Gpu.cpp
@@ -271,14 +271,8 @@
     uDesc.fWidth = yuvSizes[1].fWidth;
     uDesc.fHeight = yuvSizes[1].fHeight;
 
-    sk_sp<GrTexture> yTex(
-        ctx->textureProvider()->wrapBackendTexture(yDesc, kBorrow_GrWrapOwnership));
-    sk_sp<GrSurfaceProxy> yProxy = GrSurfaceProxy::MakeWrapped(std::move(yTex));
-
-    sk_sp<GrTexture> uTex(
-        ctx->textureProvider()->wrapBackendTexture(uDesc, kBorrow_GrWrapOwnership));
-    sk_sp<GrSurfaceProxy> uProxy = GrSurfaceProxy::MakeWrapped(std::move(uTex));
-
+    sk_sp<GrSurfaceProxy> yProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, yDesc);
+    sk_sp<GrSurfaceProxy> uProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, uDesc);
     sk_sp<GrSurfaceProxy> vProxy;
 
     if (nv12) {
@@ -292,9 +286,7 @@
         vDesc.fWidth = yuvSizes[2].fWidth;
         vDesc.fHeight = yuvSizes[2].fHeight;
 
-        sk_sp<GrTexture> vTex = sk_sp<GrTexture>(
-            ctx->textureProvider()->wrapBackendTexture(vDesc, kBorrow_GrWrapOwnership));
-        vProxy = GrSurfaceProxy::MakeWrapped(std::move(vTex));
+        vProxy = GrSurfaceProxy::MakeWrappedBackend(ctx, vDesc);
     }
     if (!yProxy || !uProxy || !vProxy) {
         return nullptr;
diff --git a/tests/EGLImageTest.cpp b/tests/EGLImageTest.cpp
index 02ba4a8..3011355 100644
--- a/tests/EGLImageTest.cpp
+++ b/tests/EGLImageTest.cpp
@@ -135,27 +135,22 @@
     externalDesc.fHeight = kSize;
     externalDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&externalTexture);
 
-    sk_sp<GrSurfaceContext> externalTextureContext;
+    sk_sp<GrSurfaceContext> surfaceContext = context0->contextPriv().makeBackendSurfaceContext(
+                                                                           externalDesc, nullptr);
 
-    {
-        sk_sp<GrTexture> externalTextureObj(
-            context0->textureProvider()->wrapBackendTexture(externalDesc));
-        if (!externalTextureObj) {
-            ERRORF(reporter, "Error wrapping external texture in GrTexture.");
-            cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
-            return;
-        }
-
-        externalTextureContext = context0->contextPriv().makeWrappedSurfaceContext(
-                                                                    std::move(externalTextureObj));
+    if (!surfaceContext) {
+        ERRORF(reporter, "Error wrapping external texture in GrSurfaceContext.");
+        cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
+        return;
     }
 
     // Should not be able to wrap as a RT
     {
         externalDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
-        sk_sp<GrTexture> externalTextureRTObj(
-            context0->textureProvider()->wrapBackendTexture(externalDesc));
-        if (externalTextureRTObj) {
+
+        sk_sp<GrSurfaceContext> temp = context0->contextPriv().makeBackendSurfaceContext(
+                                                                           externalDesc, nullptr);
+        if (temp) {
             ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture as a RT.");
         }
         externalDesc.fFlags = kNone_GrBackendTextureFlag;
@@ -164,24 +159,24 @@
     // Should not be able to wrap with a sample count
     {
         externalDesc.fSampleCnt = 4;
-        sk_sp<GrTexture> externalTextureMSAAObj(
-            context0->textureProvider()->wrapBackendTexture(externalDesc));
-        if (externalTextureMSAAObj) {
+        sk_sp<GrSurfaceContext> temp = context0->contextPriv().makeBackendSurfaceContext(
+                                                                           externalDesc, nullptr);
+        if (temp) {
             ERRORF(reporter, "Should not be able to wrap an EXTERNAL texture with MSAA.");
         }
         externalDesc.fSampleCnt = 0;
     }
 
-    test_read_pixels(reporter, context0, externalTextureContext.get(), pixels.get(),
+    test_read_pixels(reporter, context0, surfaceContext.get(), pixels.get(),
                      "EGLImageTest-read");
 
     // We should not be able to write to a EXTERNAL texture
-    test_write_pixels(reporter, context0, externalTextureContext.get(), false,
+    test_write_pixels(reporter, context0, surfaceContext.get(), false,
                       "EGLImageTest-write");
 
     // Only test RT-config
     // TODO: why do we always need to draw to copy from an external texture?
-    test_copy_from_surface(reporter, context0, externalTextureContext->asDeferredSurface(),
+    test_copy_from_surface(reporter, context0, surfaceContext->asDeferredSurface(),
                            pixels.get(), true, "EGLImageTest-copy");
 
     cleanup(glCtx0, externalTexture.fID, glCtx1.get(), context1, backendTexture1, image);
diff --git a/tests/RectangleTextureTest.cpp b/tests/RectangleTextureTest.cpp
index d55b882..ef5f2d3 100644
--- a/tests/RectangleTextureTest.cpp
+++ b/tests/RectangleTextureTest.cpp
@@ -133,23 +133,12 @@
             }
         }
 
-        sk_sp<GrSurfaceProxy> rectProxy;
-
-        {
-            sk_sp<GrTexture> rectangleTexture(
-                context->textureProvider()->wrapBackendTexture(rectangleDesc));
-            if (!rectangleTexture) {
-                ERRORF(reporter, "Error wrapping rectangle texture in GrTexture.");
-                GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
-                continue;
-            }
-
-            rectProxy = GrSurfaceProxy::MakeWrapped(std::move(rectangleTexture));
-            if (!rectProxy) {
-                ERRORF(reporter, "Error creating proxy for rectangle texture.");
-                GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
-                continue;
-            }
+        sk_sp<GrSurfaceProxy> rectProxy = GrSurfaceProxy::MakeWrappedBackend(context,
+                                                                             rectangleDesc);
+        if (!rectProxy) {
+            ERRORF(reporter, "Error creating proxy for rectangle texture.");
+            GR_GL_CALL(glContext->gl(), DeleteTextures(1, &rectTexID));
+            continue;
         }
 
         test_basic_draw_as_src(reporter, context, rectProxy, refPixels);