Switch ImageStorageAccess over to GrTextureProxies

Split out of: https://skia-review.googlesource.com/c/10484/ (Omnibus: Push instantiation of GrTextures later (post TextureSampler))

Change-Id: I341de6ae121620d30e50bff21450878a18bdf4f2
Reviewed-on: https://skia-review.googlesource.com/16714
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrProcessor.cpp b/src/gpu/GrProcessor.cpp
index 57a8008..1533257 100644
--- a/src/gpu/GrProcessor.cpp
+++ b/src/gpu/GrProcessor.cpp
@@ -129,6 +129,8 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 void GrResourceIOProcessor::addTextureSampler(const TextureSampler* access) {
+    // MDB TODO: this 'isBad' call checks to ensure the underlying texture exists. It needs to
+    // be moved later.
     if (access->isBad()) {
         this->markAsBad();
     }
@@ -140,7 +142,13 @@
     fBufferAccesses.push_back(access);
 }
 
-void GrResourceIOProcessor::addImageStorageAccess(const ImageStorageAccess* access) {
+void GrResourceIOProcessor::addImageStorageAccess(GrResourceProvider* resourceProvider,
+                                                  const ImageStorageAccess* access) {
+    // MDB TODO: this 'isBad' call attempts to instantiate 'access'. It needs to be moved later.
+    if (access->isBad(resourceProvider)) {
+        this->markAsBad();
+    }
+
     fImageStorageAccesses.push_back(access);
 }
 
@@ -152,7 +160,7 @@
         buffer->programBuffer()->markPendingIO();
     }
     for (const auto& imageStorage : fImageStorageAccesses) {
-        imageStorage->programTexture()->markPendingIO();
+        imageStorage->programProxy()->markPendingIO();
     }
 }
 
@@ -164,7 +172,7 @@
         buffer->programBuffer()->removeRef();
     }
     for (const auto& imageStorage : fImageStorageAccesses) {
-        imageStorage->programTexture()->removeRef();
+        imageStorage->programProxy()->removeRef();
     }
 }
 
@@ -176,7 +184,7 @@
         buffer->programBuffer()->pendingIOComplete();
     }
     for (const auto& imageStorage : fImageStorageAccesses) {
-        imageStorage->programTexture()->pendingIOComplete();
+        imageStorage->programProxy()->pendingIOComplete();
     }
 }
 
@@ -272,19 +280,20 @@
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-GrResourceIOProcessor::ImageStorageAccess::ImageStorageAccess(sk_sp<GrTexture> texture,
+GrResourceIOProcessor::ImageStorageAccess::ImageStorageAccess(sk_sp<GrTextureProxy> proxy,
                                                               GrIOType ioType,
                                                               GrSLMemoryModel memoryModel,
                                                               GrSLRestrict restrict,
-                                                              GrShaderFlags visibility) {
-    SkASSERT(texture);
-    fTexture.set(texture.release(), ioType);
+                                                              GrShaderFlags visibility)
+        : fProxyRef(std::move(proxy), ioType) {
+    SkASSERT(fProxyRef.getProxy());
+
     fMemoryModel = memoryModel;
     fRestrict = restrict;
     fVisibility = visibility;
     // We currently infer this from the config. However, we could allow the client to specify
     // a format that is different but compatible with the config.
-    switch (fTexture.get()->config()) {
+    switch (fProxyRef.getProxy()->config()) {
         case kRGBA_8888_GrPixelConfig:
             fFormat = GrImageStorageFormat::kRGBA8;
             break;
diff --git a/src/gpu/GrProcessor.h b/src/gpu/GrProcessor.h
index 7011267..027a34e 100644
--- a/src/gpu/GrProcessor.h
+++ b/src/gpu/GrProcessor.h
@@ -15,6 +15,7 @@
 #include "GrProgramElement.h"
 #include "GrSamplerParams.h"
 #include "GrShaderVar.h"
+#include "GrSurfaceProxyPriv.h"
 #include "SkMath.h"
 #include "SkString.h"
 #include "../private/SkAtomics.h"
@@ -189,7 +190,7 @@
      */
     void addTextureSampler(const TextureSampler*);
     void addBufferAccess(const BufferAccess*);
-    void addImageStorageAccess(const ImageStorageAccess*);
+    void addImageStorageAccess(GrResourceProvider* resourceProvider, const ImageStorageAccess*);
 
     bool hasSameSamplersAndAccesses(const GrResourceIOProcessor&) const;
 
@@ -324,33 +325,39 @@
  */
 class GrResourceIOProcessor::ImageStorageAccess : public SkNoncopyable {
 public:
-    ImageStorageAccess(sk_sp<GrTexture> texture, GrIOType ioType, GrSLMemoryModel, GrSLRestrict,
+    ImageStorageAccess(sk_sp<GrTextureProxy>, GrIOType, GrSLMemoryModel, GrSLRestrict,
                        GrShaderFlags visibility = kFragment_GrShaderFlag);
 
     bool operator==(const ImageStorageAccess& that) const {
-        return this->texture() == that.texture() && fVisibility == that.fVisibility;
+        return this->proxy() == that.proxy() && fVisibility == that.fVisibility;
     }
 
     bool operator!=(const ImageStorageAccess& that) const { return !(*this == that); }
 
-    GrTexture* texture() const { return fTexture.get(); }
+    GrTexture* texture() const { return fProxyRef.getProxy()->priv().peekTexture(); }
+    GrTextureProxy* proxy() const { return fProxyRef.getProxy()->asTextureProxy(); }
     GrShaderFlags visibility() const { return fVisibility; }
-    GrIOType ioType() const { return fTexture.ioType(); }
+    GrIOType ioType() const { return fProxyRef.ioType(); }
     GrImageStorageFormat format() const { return fFormat; }
     GrSLMemoryModel memoryModel() const { return fMemoryModel; }
     GrSLRestrict restrict() const { return fRestrict; }
 
+    // MDB: In the future this should be renamed instantiate
+    bool isBad(GrResourceProvider* resourceProvider) const {
+        return SkToBool(!fProxyRef.getProxy()->instantiate(resourceProvider));
+    }
+
     /**
      * For internal use by GrProcessor.
      */
-    const GrGpuResourceRef* programTexture() const { return &fTexture; }
+    const GrSurfaceProxyRef* programProxy() const { return &fProxyRef; }
 
 private:
-    GrTGpuResourceRef<GrTexture> fTexture;
-    GrShaderFlags fVisibility;
+    GrSurfaceProxyRef    fProxyRef;
+    GrShaderFlags        fVisibility;
     GrImageStorageFormat fFormat;
-    GrSLMemoryModel fMemoryModel;
-    GrSLRestrict fRestrict;
+    GrSLMemoryModel      fMemoryModel;
+    GrSLRestrict         fRestrict;
     typedef SkNoncopyable INHERITED;
 };
 
diff --git a/src/gpu/GrProgramDesc.cpp b/src/gpu/GrProgramDesc.cpp
index 86c653d..41993e1 100644
--- a/src/gpu/GrProgramDesc.cpp
+++ b/src/gpu/GrProgramDesc.cpp
@@ -62,7 +62,7 @@
 }
 
 static uint16_t storage_image_key(const GrResourceIOProcessor::ImageStorageAccess& imageAccess) {
-    GrSLType type = imageAccess.texture()->texturePriv().imageStorageType();
+    GrSLType type = imageAccess.proxy()->imageStorageType();
     return image_storage_or_sampler_uniform_type_key(type) |
            (int)imageAccess.format() << kSamplerOrImageTypeKeyBits;
 }
diff --git a/src/gpu/GrSurfaceProxyPriv.h b/src/gpu/GrSurfaceProxyPriv.h
index 24c6993..0d0da4e 100644
--- a/src/gpu/GrSurfaceProxyPriv.h
+++ b/src/gpu/GrSurfaceProxyPriv.h
@@ -17,7 +17,7 @@
 public:
     // If the proxy is already instantiated, return its backing GrTexture; if not,
     // return null
-    const GrTexture* peekTexture() const {
+    GrTexture* peekTexture() const {
         return fProxy->fTarget ? fProxy->fTarget->asTexture() : nullptr;
     }
 
diff --git a/src/gpu/glsl/GrGLSLProgramBuilder.cpp b/src/gpu/glsl/GrGLSLProgramBuilder.cpp
index 5c3bee1..96b3359 100644
--- a/src/gpu/glsl/GrGLSLProgramBuilder.cpp
+++ b/src/gpu/glsl/GrGLSLProgramBuilder.cpp
@@ -358,7 +358,7 @@
     if (access.visibility() & kFragment_GrShaderFlag) {
         ++fNumFragmentImageStorages;
     }
-    GrSLType uniformType = access.texture()->texturePriv().imageStorageType();
+    GrSLType uniformType = access.proxy()->imageStorageType();
     return this->uniformHandler()->addImageStorage(access.visibility(), uniformType,
                                                    access.format(), access.memoryModel(),
                                                    access.restrict(), access.ioType(), name);