Update createWrappedRenderTargetProxy with BackendRT to work with lazy proxies.

This change also triggered a bunch of side changes thanks to WrappedProxyTest
fixing/improving how we handle wrapped proxies in genernal.

Bug: skia:
Change-Id: I743a458923cff1c2e947627d0e9154a4c808a668
Reviewed-on: https://skia-review.googlesource.com/108102
Commit-Queue: Greg Daniel <egdaniel@google.com>
Reviewed-by: Chris Dalton <csmartdalton@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
diff --git a/tests/ProxyTest.cpp b/tests/ProxyTest.cpp
index dff230c..3a46438 100644
--- a/tests/ProxyTest.cpp
+++ b/tests/ProxyTest.cpp
@@ -20,6 +20,7 @@
 #include "GrSurfaceProxyPriv.h"
 #include "GrTexture.h"
 #include "GrTextureProxy.h"
+#include "SkGr.h"
 
 // Check that the surface proxy's member vars are set as expected
 static void check_surface(skiatest::Reporter* reporter,
@@ -42,8 +43,7 @@
                                GrRenderTargetProxy* rtProxy,
                                int numSamples,
                                SkBackingFit fit,
-                               int expectedMaxWindowRects,
-                               bool wasWrapped) {
+                               int expectedMaxWindowRects) {
     REPORTER_ASSERT(reporter, rtProxy->maxWindowRectangles(caps) == expectedMaxWindowRects);
     REPORTER_ASSERT(reporter, rtProxy->numStencilSamples() == numSamples);
 
@@ -52,13 +52,8 @@
     GrRenderTarget* rt = rtProxy->priv().peekRenderTarget();
 
     REPORTER_ASSERT(reporter, rtProxy->uniqueID() == idBefore);
-    if (wasWrapped) {
-        // Wrapped resources share their uniqueID with the wrapping RenderTargetProxy
-        REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() == rt->uniqueID().asUInt());
-    } else {
-        // Deferred resources should always have a different ID from their instantiated rendertarget
-        REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() != rt->uniqueID().asUInt());
-    }
+    // Deferred resources should always have a different ID from their instantiated rendertarget
+    REPORTER_ASSERT(reporter, rtProxy->uniqueID().asUInt() != rt->uniqueID().asUInt());
 
     if (SkBackingFit::kExact == fit) {
         REPORTER_ASSERT(reporter, rt->width() == rtProxy->width());
@@ -78,21 +73,15 @@
 static void check_texture(skiatest::Reporter* reporter,
                           GrResourceProvider* provider,
                           GrTextureProxy* texProxy,
-                          SkBackingFit fit,
-                          bool wasWrapped) {
+                          SkBackingFit fit) {
     GrSurfaceProxy::UniqueID idBefore = texProxy->uniqueID();
 
     REPORTER_ASSERT(reporter, texProxy->instantiate(provider));
     GrTexture* tex = texProxy->priv().peekTexture();
 
     REPORTER_ASSERT(reporter, texProxy->uniqueID() == idBefore);
-    if (wasWrapped) {
-        // Wrapped resources share their uniqueID with the wrapping TextureProxy
-        REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() == tex->uniqueID().asUInt());
-    } else {
-        // Deferred resources should always have a different ID from their instantiated texture
-        REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() != tex->uniqueID().asUInt());
-    }
+    // Deferred resources should always have a different ID from their instantiated texture
+    REPORTER_ASSERT(reporter, texProxy->uniqueID().asUInt() != tex->uniqueID().asUInt());
 
     if (SkBackingFit::kExact == fit) {
         REPORTER_ASSERT(reporter, tex->width() == texProxy->width());
@@ -154,7 +143,7 @@
                                     check_rendertarget(reporter, caps, resourceProvider,
                                                        proxy->asRenderTargetProxy(),
                                                        supportedSamples,
-                                                       fit, caps.maxWindowRectangles(), false);
+                                                       fit, caps.maxWindowRectangles());
                                 }
                             }
 
@@ -182,7 +171,7 @@
                                     check_surface(reporter, proxy.get(), origin,
                                                   widthHeight, widthHeight, config, budgeted);
                                     check_texture(reporter, resourceProvider,
-                                                  proxy->asTextureProxy(), fit, false);
+                                                  proxy->asTextureProxy(), fit);
                                 }
                             }
 
@@ -198,71 +187,87 @@
 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WrappedProxyTest, reporter, ctxInfo) {
     GrProxyProvider* proxyProvider = ctxInfo.grContext()->contextPriv().proxyProvider();
     GrResourceProvider* resourceProvider = ctxInfo.grContext()->contextPriv().resourceProvider();
+    GrGpu* gpu = ctxInfo.grContext()->contextPriv().getGpu();
     const GrCaps& caps = *ctxInfo.grContext()->caps();
 
     static const int kWidthHeight = 100;
 
+    if (kOpenGL_GrBackend != ctxInfo.backend()) {
+        return;
+    }
     for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
-        for (auto config : { kAlpha_8_GrPixelConfig, kRGBA_8888_GrPixelConfig }) {
-            for (auto budgeted : { SkBudgeted::kYes, SkBudgeted::kNo }) {
-                for (auto numSamples : {1, 4}) {
-                    int supportedNumSamples = caps.getRenderTargetSampleCount(numSamples, config);
+        for (auto colorType : { kAlpha_8_SkColorType, kRGBA_8888_SkColorType }) {
+            for (auto numSamples : {1, 4}) {
+                GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, caps);
+                int supportedNumSamples = caps.getRenderTargetSampleCount(numSamples, config);
 
-                    GrSurfaceDesc desc;
-                    desc.fOrigin = origin;
-                    desc.fWidth = kWidthHeight;
-                    desc.fHeight = kWidthHeight;
-                    desc.fConfig = config;
-                    desc.fSampleCnt = supportedNumSamples;
+                if (!supportedNumSamples) {
+                    continue;
+                }
 
-                    // External on-screen render target.
-                    if (supportedNumSamples && kOpenGL_GrBackend == ctxInfo.backend()) {
-                        GrGLFramebufferInfo fboInfo;
-                        fboInfo.fFBOID = 0;
-                        GrBackendRenderTarget backendRT(kWidthHeight, kWidthHeight, numSamples, 8,
-                                                        config, fboInfo);
+                // External on-screen render target.
+                {
+                    GrGLFramebufferInfo fboInfo;
+                    fboInfo.fFBOID = 0;
+                    GrBackendRenderTarget backendRT(kWidthHeight, kWidthHeight, numSamples, 8,
+                                                    config, fboInfo);
 
-                        sk_sp<GrSurfaceProxy> sProxy(proxyProvider->createWrappedRenderTargetProxy(
-                                                                                backendRT, origin));
-                        check_surface(reporter, sProxy.get(), origin,
-                                      kWidthHeight, kWidthHeight, config, SkBudgeted::kNo);
-                        check_rendertarget(reporter, caps, resourceProvider,
-                                           sProxy->asRenderTargetProxy(),
-                                           supportedNumSamples, SkBackingFit::kExact, 0, true);
+                    sk_sp<GrSurfaceProxy> sProxy(proxyProvider->createWrappedRenderTargetProxy(
+                            backendRT, origin));
+                    check_surface(reporter, sProxy.get(), origin,
+                                  kWidthHeight, kWidthHeight,
+                                  backendRT.testingOnly_getPixelConfig(), SkBudgeted::kNo);
+                    check_rendertarget(reporter, caps, resourceProvider,
+                                       sProxy->asRenderTargetProxy(),
+                                       supportedNumSamples, SkBackingFit::kExact, 0);
+                }
+
+                {
+                    GrBackendTexture backendTex =
+                            gpu->createTestingOnlyBackendTexture(nullptr, kWidthHeight,
+                                                                 kWidthHeight, colorType, true,
+                                                                 GrMipMapped::kNo);
+
+                    sk_sp<GrSurfaceProxy> sProxy =
+                            proxyProvider->createWrappedTextureProxy(backendTex, origin,
+                                                                     supportedNumSamples);
+                    if (!sProxy) {
+                        continue;  // This can fail on Mesa
                     }
 
-                    if (supportedNumSamples) {
-                        // Internal offscreen render target.
-                        desc.fFlags = kRenderTarget_GrSurfaceFlag;
+                    check_surface(reporter, sProxy.get(), origin,
+                                  kWidthHeight, kWidthHeight,
+                                  backendTex.testingOnly_getPixelConfig(), SkBudgeted::kNo);
+                    check_rendertarget(reporter, caps, resourceProvider,
+                                       sProxy->asRenderTargetProxy(),
+                                       supportedNumSamples, SkBackingFit::kExact,
+                                       caps.maxWindowRectangles());
 
-                        sk_sp<GrSurfaceProxy> sProxy = proxyProvider->createInstantiatedProxy(
-                                                        desc, SkBackingFit::kExact, budgeted);
-                        if (!sProxy) {
-                            continue;  // This can fail on Mesa
-                        }
+                    gpu->deleteTestingOnlyBackendTexture(&backendTex);
+                }
 
-                        check_surface(reporter, sProxy.get(), origin,
-                                      kWidthHeight, kWidthHeight, config, budgeted);
-                        check_rendertarget(reporter, caps, resourceProvider,
-                                           sProxy->asRenderTargetProxy(),
-                                           supportedNumSamples, SkBackingFit::kExact,
-                                           caps.maxWindowRectangles(), true);
-                    } else {
-                        // Internal offscreen texture
-                        SkASSERT(kNone_GrSurfaceFlags == desc.fFlags );
-                        desc.fSampleCnt = 1;
+                {
+                    // Internal offscreen texture
+                    GrBackendTexture backendTex =
+                            gpu->createTestingOnlyBackendTexture(nullptr, kWidthHeight,
+                                                                 kWidthHeight, colorType, false,
+                                                                 GrMipMapped::kNo);
 
-                        sk_sp<GrSurfaceProxy> sProxy = proxyProvider->createInstantiatedProxy(
-                                                          desc, SkBackingFit::kExact, budgeted);
-                        if (!sProxy) {
-                            continue;
-                        }
-
-                        check_surface(reporter, sProxy.get(), origin,
-                                      kWidthHeight, kWidthHeight, config, budgeted);
-                        check_texture(reporter, resourceProvider, sProxy->asTextureProxy(),
-                                      SkBackingFit::kExact, true);
+                    sk_sp<GrSurfaceProxy> sProxy =
+                            proxyProvider->createWrappedTextureProxy(backendTex, origin,
+                                                                     kBorrow_GrWrapOwnership,
+                                                                     nullptr, nullptr);
+                    if (!sProxy) {
+                        continue;
                     }
+
+                    check_surface(reporter, sProxy.get(), origin,
+                                  kWidthHeight, kWidthHeight,
+                                  backendTex.testingOnly_getPixelConfig(), SkBudgeted::kNo);
+                    check_texture(reporter, resourceProvider, sProxy->asTextureProxy(),
+                                  SkBackingFit::kExact);
+
+                    gpu->deleteTestingOnlyBackendTexture(&backendTex);
                 }
             }
         }