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/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 {}