Add GrBackendTexture & GrBackendRenderTarget access methods to GrTexture and GrRenderTarget

Change-Id: I627fcc2cab1d04169f49e33a6c17e161e9a9772a
Reviewed-on: https://skia-review.googlesource.com/84621
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/include/gpu/GrRenderTarget.h b/include/gpu/GrRenderTarget.h
index ff5eb03..93501db 100644
--- a/include/gpu/GrRenderTarget.h
+++ b/include/gpu/GrRenderTarget.h
@@ -15,6 +15,7 @@
 class GrRenderTargetOpList;
 class GrRenderTargetPriv;
 class GrStencilAttachment;
+class GrBackendRenderTarget;
 
 /**
  * GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
@@ -104,6 +105,8 @@
      */
     virtual GrBackendObject getRenderTargetHandle() const = 0;
 
+    virtual GrBackendRenderTarget getBackendRenderTarget() const = 0;
+
     // Checked when this object is asked to attach a stencil buffer.
     virtual bool canAttemptStencilAttachment() const = 0;
 
diff --git a/include/gpu/GrTexture.h b/include/gpu/GrTexture.h
index c32c0e9..28f0d4b 100644
--- a/include/gpu/GrTexture.h
+++ b/include/gpu/GrTexture.h
@@ -30,6 +30,8 @@
      */
     virtual GrBackendObject getTextureHandle() const = 0;
 
+    virtual GrBackendTexture getBackendTexture() const = 0;
+
     /**
      * This function indicates that the texture parameters (wrap mode, filtering, ...) have been
      * changed externally to Skia.
diff --git a/src/gpu/GrBackendTextureImageGenerator.cpp b/src/gpu/GrBackendTextureImageGenerator.cpp
index 58dacf6..186ca52 100644
--- a/src/gpu/GrBackendTextureImageGenerator.cpp
+++ b/src/gpu/GrBackendTextureImageGenerator.cpp
@@ -30,32 +30,6 @@
     SkMessageBus<GrGpuResourceFreedMessage>::Post(msg);
 }
 
-// TODO: I copied this from SkImage_Gpu, perhaps we put a version of this somewhere else?
-static GrBackendTexture make_backend_texture_from_handle(GrBackend backend,
-                                                         int width, int height,
-                                                         GrPixelConfig config,
-                                                         GrMipMapped mipMapped,
-                                                         GrBackendObject handle) {
-    switch (backend) {
-        case kOpenGL_GrBackend: {
-            const GrGLTextureInfo* glInfo = (const GrGLTextureInfo*)(handle);
-            return GrBackendTexture(width, height, config, mipMapped, *glInfo);
-        }
-#ifdef SK_VULKAN
-        case kVulkan_GrBackend: {
-            const GrVkImageInfo* vkInfo = (const GrVkImageInfo*)(handle);
-            return GrBackendTexture(width, height, *vkInfo);
-        }
-#endif
-        case kMock_GrBackend: {
-            const GrMockTextureInfo* mockInfo = (const GrMockTextureInfo*)(handle);
-            return GrBackendTexture(width, height, config, mipMapped, *mockInfo);
-        }
-        default:
-            return GrBackendTexture();
-    }
-}
-
 std::unique_ptr<SkImageGenerator>
 GrBackendTextureImageGenerator::Make(sk_sp<GrTexture> texture, GrSurfaceOrigin origin,
                                      sk_sp<GrSemaphore> semaphore,
@@ -76,14 +50,7 @@
     // this point. That ref will be released when the generator's RefHelper is freed.
     context->getResourceCache()->insertCrossContextGpuResource(texture.get());
 
-    GrBackend backend = context->contextPriv().getBackend();
-    GrMipMapped mipMapped = texture->texturePriv().mipMapped();
-    GrBackendTexture backendTexture = make_backend_texture_from_handle(backend,
-                                                                       texture->width(),
-                                                                       texture->height(),
-                                                                       texture->config(),
-                                                                       mipMapped,
-                                                                       texture->getTextureHandle());
+    GrBackendTexture backendTexture = texture->getBackendTexture();
 
     SkImageInfo info = SkImageInfo::Make(texture->width(), texture->height(), colorType, alphaType,
                                          std::move(colorSpace));
diff --git a/src/gpu/gl/GrGLRenderTarget.h b/src/gpu/gl/GrGLRenderTarget.h
index d38c557..28c6e3d 100644
--- a/src/gpu/gl/GrGLRenderTarget.h
+++ b/src/gpu/gl/GrGLRenderTarget.h
@@ -9,6 +9,7 @@
 #ifndef GrGLRenderTarget_DEFINED
 #define GrGLRenderTarget_DEFINED
 
+#include "GrBackendSurface.h"
 #include "GrGLIRect.h"
 #include "GrRenderTarget.h"
 #include "SkScalar.h"
@@ -62,6 +63,14 @@
 
     GrBackendObject getRenderTargetHandle() const override { return fRTFBOID; }
 
+    GrBackendRenderTarget getBackendRenderTarget() const override {
+        GrGLFramebufferInfo fbi;
+        fbi.fFBOID = fRTFBOID;
+
+        return GrBackendRenderTarget(this->width(), this->height(), this->numColorSamples(),
+                                     this->numStencilSamples(), this->config(), fbi);
+    }
+
     bool canAttemptStencilAttachment() const override;
 
     // GrGLRenderTarget overrides dumpMemoryStatistics so it can log its texture and renderbuffer
diff --git a/src/gpu/gl/GrGLTexture.cpp b/src/gpu/gl/GrGLTexture.cpp
index 4a4f085..597c213 100644
--- a/src/gpu/gl/GrGLTexture.cpp
+++ b/src/gpu/gl/GrGLTexture.cpp
@@ -9,6 +9,7 @@
 #include "GrGLGpu.h"
 #include "GrSemaphore.h"
 #include "GrShaderCaps.h"
+#include "GrTexturePriv.h"
 #include "SkTraceMemoryDump.h"
 
 #define GPUGL static_cast<GrGLGpu*>(this->getGpu())
@@ -105,6 +106,11 @@
     return reinterpret_cast<GrBackendObject>(&fInfo);
 }
 
+GrBackendTexture GrGLTexture::getBackendTexture() const {
+    return GrBackendTexture(this->width(), this->height(), this->config(),
+                            this->texturePriv().mipMapped(), fInfo);
+}
+
 void GrGLTexture::setMemoryBacking(SkTraceMemoryDump* traceMemoryDump,
                                    const SkString& dumpName) const {
     SkString texture_id;
diff --git a/src/gpu/gl/GrGLTexture.h b/src/gpu/gl/GrGLTexture.h
index dfce602..b7c8716 100644
--- a/src/gpu/gl/GrGLTexture.h
+++ b/src/gpu/gl/GrGLTexture.h
@@ -40,6 +40,7 @@
     }
 
     GrBackendObject getTextureHandle() const override;
+    GrBackendTexture getBackendTexture() const override;
 
     void textureParamsModified() override { fTexParams.invalidate(); }
 
diff --git a/src/gpu/mock/GrMockTexture.h b/src/gpu/mock/GrMockTexture.h
index 0469d4f..a25ff9b 100644
--- a/src/gpu/mock/GrMockTexture.h
+++ b/src/gpu/mock/GrMockTexture.h
@@ -28,6 +28,11 @@
     GrBackendObject getTextureHandle() const override {
         return reinterpret_cast<GrBackendObject>(&fInfo);
     }
+    GrBackendTexture getBackendTexture() const override {
+        return GrBackendTexture(this->width(), this->height(), this->config(),
+                                this->texturePriv().mipMapped(), fInfo);
+    }
+
     void textureParamsModified() override {}
     void setRelease(ReleaseProc proc, ReleaseCtx ctx) override {
         fReleaseProc = proc;
@@ -68,6 +73,11 @@
     }
     ResolveType getResolveType() const override { return kCanResolve_ResolveType; }
     GrBackendObject getRenderTargetHandle() const override { return 0; }
+
+    GrBackendRenderTarget getBackendRenderTarget() const override {
+        return GrBackendRenderTarget(); // invalid
+    }
+
     bool canAttemptStencilAttachment() const override { return true; }
     bool completeStencilAttachment() override { return true; }
     GrTexture* asTexture() override { return this; }
diff --git a/src/gpu/mtl/GrMtlRenderTarget.h b/src/gpu/mtl/GrMtlRenderTarget.h
index 186f703..4dac306 100644
--- a/src/gpu/mtl/GrMtlRenderTarget.h
+++ b/src/gpu/mtl/GrMtlRenderTarget.h
@@ -10,6 +10,8 @@
 
 #include "GrRenderTarget.h"
 
+#include "GrBackendSurface.h"
+
 #import <Metal/Metal.h>
 
 class GrMtlGpu;
@@ -40,6 +42,10 @@
 
     GrBackendObject getRenderTargetHandle() const override;
 
+    GrBackendRenderTarget getBackendRenderTarget() const override {
+        return GrBackendRenderTarget(); // invalid
+    }
+
 protected:
     GrMtlRenderTarget(GrMtlGpu* gpu,
                       const GrSurfaceDesc& desc,
diff --git a/src/gpu/mtl/GrMtlTexture.h b/src/gpu/mtl/GrMtlTexture.h
index 3f223d5..2d7c301 100644
--- a/src/gpu/mtl/GrMtlTexture.h
+++ b/src/gpu/mtl/GrMtlTexture.h
@@ -27,6 +27,7 @@
     id<MTLTexture> mtlTexture() const { return fTexture; }
 
     GrBackendObject getTextureHandle() const override;
+    GrBackendTexture getBackendTexture() const override;
 
     void textureParamsModified() override {}
 
diff --git a/src/gpu/mtl/GrMtlTexture.mm b/src/gpu/mtl/GrMtlTexture.mm
index 0bd50ed..26e5918 100644
--- a/src/gpu/mtl/GrMtlTexture.mm
+++ b/src/gpu/mtl/GrMtlTexture.mm
@@ -79,3 +79,7 @@
     void* voidTex = (__bridge_retained void*)fTexture;
     return (GrBackendObject)voidTex;
 }
+
+GrBackendTexture GrMtlTexture::getBackendTexture() const {
+    return GrBackendTexture(); // invalid
+}
diff --git a/src/gpu/vk/GrVkRenderTarget.cpp b/src/gpu/vk/GrVkRenderTarget.cpp
index 27cb119..c5a8628 100644
--- a/src/gpu/vk/GrVkRenderTarget.cpp
+++ b/src/gpu/vk/GrVkRenderTarget.cpp
@@ -7,6 +7,7 @@
 
 #include "GrVkRenderTarget.h"
 
+#include "GrBackendSurface.h"
 #include "GrRenderTargetPriv.h"
 #include "GrVkCommandBuffer.h"
 #include "GrVkFramebuffer.h"
@@ -347,6 +348,11 @@
     return (GrBackendObject)&fInfo;
 }
 
+GrBackendRenderTarget GrVkRenderTarget::getBackendRenderTarget() const {
+    return GrBackendRenderTarget(this->width(), this->height(), this->numColorSamples(),
+                                 this->numStencilSamples(), fInfo);
+}
+
 const GrVkResource* GrVkRenderTarget::stencilImageResource() const {
     const GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment();
     if (stencil) {
diff --git a/src/gpu/vk/GrVkRenderTarget.h b/src/gpu/vk/GrVkRenderTarget.h
index 18a0bd3..eb297a8 100644
--- a/src/gpu/vk/GrVkRenderTarget.h
+++ b/src/gpu/vk/GrVkRenderTarget.h
@@ -70,6 +70,7 @@
     }
 
     GrBackendObject getRenderTargetHandle() const override;
+    GrBackendRenderTarget getBackendRenderTarget() const override;
 
     void getAttachmentsDescriptor(GrVkRenderPass::AttachmentsDescriptor* desc,
                                   GrVkRenderPass::AttachmentFlags* flags) const;
diff --git a/src/gpu/vk/GrVkTexture.cpp b/src/gpu/vk/GrVkTexture.cpp
index c4f55d7..e696dd1 100644
--- a/src/gpu/vk/GrVkTexture.cpp
+++ b/src/gpu/vk/GrVkTexture.cpp
@@ -165,6 +165,10 @@
     return (GrBackendObject)&fInfo;
 }
 
+GrBackendTexture GrVkTexture::getBackendTexture() const {
+    return GrBackendTexture(this->width(), this->height(), fInfo);
+}
+
 GrVkGpu* GrVkTexture::getVkGpu() const {
     SkASSERT(!this->wasDestroyed());
     return static_cast<GrVkGpu*>(this->getGpu());
diff --git a/src/gpu/vk/GrVkTexture.h b/src/gpu/vk/GrVkTexture.h
index 0749f6b..a2a357a 100644
--- a/src/gpu/vk/GrVkTexture.h
+++ b/src/gpu/vk/GrVkTexture.h
@@ -29,6 +29,7 @@
     ~GrVkTexture() override;
 
     GrBackendObject getTextureHandle() const override;
+    GrBackendTexture getBackendTexture() const override;
 
     void textureParamsModified() override {}
 
diff --git a/tests/BlendTest.cpp b/tests/BlendTest.cpp
index 354bb3c..52f41dc 100644
--- a/tests/BlendTest.cpp
+++ b/tests/BlendTest.cpp
@@ -100,13 +100,8 @@
         return nullptr;
     }
 
-    GrBackendTexture backendTex =
-            GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
-                                         width,
-                                         height,
-                                         config,
-                                         GrMipMapped::kNo,
-                                         (*backingSurface)->getTextureHandle());
+    GrBackendTexture backendTex = (*backingSurface)->getBackendTexture();
+
     sk_sp<SkSurface> surface =
             SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex, origin,
                                                             sampleCnt, nullptr, nullptr);
diff --git a/tests/GrMipMappedTest.cpp b/tests/GrMipMappedTest.cpp
index 7caa4bb..58faec4 100644
--- a/tests/GrMipMappedTest.cpp
+++ b/tests/GrMipMappedTest.cpp
@@ -159,21 +159,19 @@
                 return;
             }
 
-            GrBackendObject genBackendObject = genTexture->getTextureHandle();
+            GrBackendTexture genBackendTex = genTexture->getBackendTexture();
 
-            if (kOpenGL_GrBackend == context->contextPriv().getBackend()) {
+            if (const GrGLTextureInfo* genTexInfo = genBackendTex.getGLTextureInfo()) {
                 const GrGLTextureInfo* origTexInfo = backendTex.getGLTextureInfo();
-                GrGLTextureInfo* genTexInfo = (GrGLTextureInfo*)genBackendObject;
                 if (willUseMips && GrMipMapped::kNo == mipMapped) {
                     // We did a copy so the texture IDs should be different
                     REPORTER_ASSERT(reporter, origTexInfo->fID != genTexInfo->fID);
                 } else {
                     REPORTER_ASSERT(reporter, origTexInfo->fID == genTexInfo->fID);
                 }
-            } else if (kVulkan_GrBackend == context->contextPriv().getBackend()) {
 #ifdef SK_VULKAN
+            } else if (const GrVkImageInfo* genImageInfo = genBackendTex.getVkImageInfo()) {
                 const GrVkImageInfo* origImageInfo = backendTex.getVkImageInfo();
-                GrVkImageInfo* genImageInfo = (GrVkImageInfo*)genBackendObject;
                 if (willUseMips && GrMipMapped::kNo == mipMapped) {
                     // We did a copy so the texture IDs should be different
                     REPORTER_ASSERT(reporter, origImageInfo->fImage != genImageInfo->fImage);
@@ -181,8 +179,6 @@
                     REPORTER_ASSERT(reporter, origImageInfo->fImage == genImageInfo->fImage);
                 }
 #endif
-            } else if (kMetal_GrBackend == context->contextPriv().getBackend()) {
-                REPORTER_ASSERT(reporter, false);
             } else {
                 REPORTER_ASSERT(reporter, false);
             }
diff --git a/tests/ImageFilterCacheTest.cpp b/tests/ImageFilterCacheTest.cpp
index 449b252..bcd4511 100644
--- a/tests/ImageFilterCacheTest.cpp
+++ b/tests/ImageFilterCacheTest.cpp
@@ -215,12 +215,8 @@
     }
     GrTexture* tex = srcProxy->priv().peekTexture();
 
-    GrBackendTexture backendTex = GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
-                                                               kFullSize,
-                                                               kFullSize,
-                                                               kRGBA_8888_GrPixelConfig,
-                                                               GrMipMapped::kNo,
-                                                               tex->getTextureHandle());
+    GrBackendTexture backendTex = tex->getBackendTexture();
+
     GrSurfaceOrigin texOrigin = kTopLeft_GrSurfaceOrigin;
     sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context,
                                                      backendTex,
diff --git a/tests/TextureProxyTest.cpp b/tests/TextureProxyTest.cpp
index a4b93c8..393e8c6 100644
--- a/tests/TextureProxyTest.cpp
+++ b/tests/TextureProxyTest.cpp
@@ -114,19 +114,14 @@
                                                     sk_sp<GrTexture>* backingSurface) {
     GrResourceProvider* provider = context->resourceProvider();
 
-    GrSurfaceDesc desc = make_desc(kNone_GrSurfaceFlags);
+    const GrSurfaceDesc desc = make_desc(kNone_GrSurfaceFlags);
 
     *backingSurface = provider->createTexture(desc, SkBudgeted::kNo);
     if (!(*backingSurface)) {
         return nullptr;
     }
 
-    GrBackendTexture backendTex =
-            GrTest::CreateBackendTexture(context->contextPriv().getBackend(),
-                                         64, 64,
-                                         kRGBA_8888_GrPixelConfig,
-                                         GrMipMapped::kNo,
-                                         (*backingSurface)->getTextureHandle());
+    GrBackendTexture backendTex = (*backingSurface)->getBackendTexture();
 
     return GrSurfaceProxy::MakeWrappedBackend(context, backendTex, kBottomLeft_GrSurfaceOrigin);
 }
diff --git a/tools/fiddle/fiddle_main.cpp b/tools/fiddle/fiddle_main.cpp
index 776e9d1..4c7a087 100644
--- a/tools/fiddle/fiddle_main.cpp
+++ b/tools/fiddle/fiddle_main.cpp
@@ -124,15 +124,12 @@
         return false;
     }
 
-    GrBackend backend = context->contextPriv().getBackend();
-    const GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig;
-
     GrSurfaceDesc backingDesc;
     backingDesc.fFlags = kNone_GrSurfaceFlags;
     backingDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
     backingDesc.fWidth = bm.width();
     backingDesc.fHeight = bm.height();
-    backingDesc.fConfig = kConfig;
+    backingDesc.fConfig = kRGBA_8888_GrPixelConfig;
     backingDesc.fSampleCnt = 0;
 
     if (!bm.empty()) {
@@ -173,12 +170,7 @@
             return false;
         }
 
-        backEndTexture = GrTest::CreateBackendTexture(backend,
-                                                      backingDesc.fWidth,
-                                                      backingDesc.fHeight,
-                                                      kConfig,
-                                                      options.fMipMapping,
-                                                      backingTexture->getTextureHandle());
+        backEndTexture = backingTexture->getBackendTexture();
         if (!backEndTexture.isValid()) {
             return false;
         }
@@ -209,13 +201,7 @@
 
         backingRenderTarget = sk_ref_sp(tmp->asRenderTarget());
 
-        backEndRenderTarget = GrTest::CreateBackendRenderTarget(
-                                                    backend,
-                                                    backingDesc.fWidth,
-                                                    backingDesc.fHeight,
-                                                    backingDesc.fSampleCnt, 0,
-                                                    kConfig,
-                                                    backingRenderTarget->getRenderTargetHandle());
+        backEndRenderTarget = backingRenderTarget->getBackendRenderTarget();
         if (!backEndRenderTarget.isValid()) {
             return false;
         }
@@ -243,13 +229,7 @@
             return false;
         }
 
-        backEndTextureRenderTarget = GrTest::CreateBackendTexture(
-                                                    backend,
-                                                    backingDesc.fWidth,
-                                                    backingDesc.fHeight,
-                                                    kConfig,
-                                                    options.fOffScreenMipMapping,
-                                                    backingTextureRenderTarget->getTextureHandle());
+        backEndTextureRenderTarget = backingTextureRenderTarget->getBackendTexture();
         if (!backEndTextureRenderTarget.isValid()) {
             return false;
         }
diff --git a/tools/gpu/GrTest.cpp b/tools/gpu/GrTest.cpp
index 0001d1d..552dd8a 100644
--- a/tools/gpu/GrTest.cpp
+++ b/tools/gpu/GrTest.cpp
@@ -78,28 +78,6 @@
     }
 }
 
-GrBackendRenderTarget CreateBackendRenderTarget(GrBackend backend, int width, int height,
-                                                int sampleCnt, int stencilBits,
-                                                GrPixelConfig config,
-                                                GrBackendObject handle) {
-    switch (backend) {
-#ifdef SK_VULKAN
-        case kVulkan_GrBackend: {
-            GrVkImageInfo* vkInfo = (GrVkImageInfo*)(handle);
-            return GrBackendRenderTarget(width, height, sampleCnt, stencilBits, *vkInfo);
-        }
-#endif
-        case kOpenGL_GrBackend: {
-            GrGLFramebufferInfo glInfo;
-            glInfo.fFBOID = handle;
-            return GrBackendRenderTarget(width, height, sampleCnt, stencilBits, config, glInfo);
-        }
-        case kMock_GrBackend: // fall through
-        default:
-            return GrBackendRenderTarget();
-    }
-}
-
 }  // namespace GrTest
 
 bool GrSurfaceProxy::isWrapped_ForTesting() const {
diff --git a/tools/gpu/GrTest.h b/tools/gpu/GrTest.h
index 1134932..5d988c7 100644
--- a/tools/gpu/GrTest.h
+++ b/tools/gpu/GrTest.h
@@ -18,12 +18,9 @@
      */
     void SetupAlwaysEvictAtlas(GrContext*);
 
+    // TODO: remove this. It is only used in the SurfaceSemaphores Test.
     GrBackendTexture CreateBackendTexture(GrBackend, int width, int height,
                                           GrPixelConfig, GrMipMapped, GrBackendObject);
-
-    GrBackendRenderTarget CreateBackendRenderTarget(GrBackend, int width, int height,
-                                                    int sampleCnt, int stencilBits,
-                                                    GrPixelConfig, GrBackendObject);
 };
 
 #endif