Make instantiate return a Boolean

From an off-line conversation:
The longer term idea will be to create a helper class isolates the
ability to instantiate proxies until flush time. The peek* methods
could then be moved to GrSurfaceProxy.

Change-Id: I8e8c02c098475b77d515791c0d6b81f7e4a327dd
Reviewed-on: https://skia-review.googlesource.com/18076
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/src/core/SkGpuBlurUtils.cpp b/src/core/SkGpuBlurUtils.cpp
index 359cbea..64bf203 100644
--- a/src/core/SkGpuBlurUtils.cpp
+++ b/src/core/SkGpuBlurUtils.cpp
@@ -198,11 +198,11 @@
     SkASSERT(context);
 
     {
+        // MDB TODO: remove this
         // Chrome is crashing with proxies when they need to be instantiated.
         // Force an instantiation here (where, in olden days, we used to require a GrTexture)
         // to see if the input is already un-instantiable.
-        GrTexture* temp = srcProxy->instantiateTexture(context->resourceProvider());
-        if (!temp) {
+        if (!srcProxy->instantiate(context->resourceProvider())) {
             return nullptr;
         }
     }
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 98abc22..17db956 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -308,11 +308,12 @@
     ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
     GR_AUDIT_TRAIL_AUTO_FRAME(&fContext->fAuditTrail, "GrContextPriv::writeSurfacePixels");
 
-    GrSurface* dstSurface = dst->asSurfaceProxy()->instantiate(fContext->resourceProvider());
-    if (!dstSurface) {
+    if (!dst->asSurfaceProxy()->instantiate(fContext->resourceProvider())) {
         return false;
     }
 
+    GrSurface* dstSurface = dst->asSurfaceProxy()->priv().peekSurface();
+
     // The src is unpremul but the dst is premul -> premul the src before or as part of the write
     const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
     if (!valid_pixel_conversion(srcConfig, dstSurface->config(), premul)) {
@@ -389,10 +390,10 @@
         if (tempProxy->priv().hasPendingIO()) {
             this->flush(tempProxy.get());
         }
-        GrTexture* texture = tempProxy->instantiateTexture(fContext->resourceProvider());
-        if (!texture) {
+        if (!tempProxy->instantiate(fContext->resourceProvider())) {
             return false;
         }
+        GrTexture* texture = tempProxy->priv().peekTexture();
         if (!fContext->fGpu->writePixels(texture, 0, 0, width, height, tempDrawInfo.fWriteConfig,
                                          buffer, rowBytes)) {
             return false;
@@ -435,11 +436,12 @@
     GR_AUDIT_TRAIL_AUTO_FRAME(&fContext->fAuditTrail, "GrContextPriv::readSurfacePixels");
 
     // MDB TODO: delay this instantiation until later in the method
-    GrSurface* srcSurface = src->asSurfaceProxy()->instantiate(fContext->resourceProvider());
-    if (!srcSurface) {
+    if (!src->asSurfaceProxy()->instantiate(fContext->resourceProvider())) {
         return false;
     }
 
+    GrSurface* srcSurface = src->asSurfaceProxy()->priv().peekSurface();
+
     // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
     bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
     if (!valid_pixel_conversion(srcSurface->config(), dstConfig, unpremul)) {
@@ -530,11 +532,12 @@
         return false;
     }
 
-    GrSurface* surfaceToRead = proxyToRead->instantiate(fContext->resourceProvider());
-    if (!surfaceToRead) {
+    if (!proxyToRead->instantiate(fContext->resourceProvider())) {
         return false;
     }
 
+    GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
+
     if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
         return false;
     }
diff --git a/src/gpu/GrDrawOpAtlas.cpp b/src/gpu/GrDrawOpAtlas.cpp
index be8258a..78f6ba4 100644
--- a/src/gpu/GrDrawOpAtlas.cpp
+++ b/src/gpu/GrDrawOpAtlas.cpp
@@ -207,10 +207,10 @@
 
         // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
         // Once it is deferred more care must be taken upon instantiation failure.
-        GrTexture* texture = fProxy->instantiateTexture(fContext->resourceProvider());
-        if (!texture) {
+        if (!fProxy->instantiate(fContext->resourceProvider())) {
             return false;
         }
+        GrTexture* texture = fProxy->priv().peekTexture();
 
         GrDrawOpUploadToken lastUploadToken = target->addAsapUpload(
             [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
@@ -286,10 +286,10 @@
     sk_sp<Plot> plotsp(SkRef(newPlot.get()));
     // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
     // Once it is deferred more care must be taken upon instantiation failure.
-    GrTexture* texture = fProxy->instantiateTexture(fContext->resourceProvider());
-    if (!texture) {
+    if (!fProxy->instantiate(fContext->resourceProvider())) {
         return false;
     }
+    GrTexture* texture = fProxy->priv().peekTexture();
 
     GrDrawOpUploadToken lastUploadToken = target->addInlineUpload(
         [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
diff --git a/src/gpu/GrDrawingManager.cpp b/src/gpu/GrDrawingManager.cpp
index bcd3e2b..67b5e8f 100644
--- a/src/gpu/GrDrawingManager.cpp
+++ b/src/gpu/GrDrawingManager.cpp
@@ -208,11 +208,12 @@
         this->flush(proxy);
     }
 
-    GrSurface* surface = proxy->instantiate(fContext->resourceProvider());
-    if (!surface) {
+    if (!proxy->instantiate(fContext->resourceProvider())) {
         return;
     }
 
+    GrSurface* surface = proxy->priv().peekSurface();
+
     if (fContext->getGpu() && surface->asRenderTarget()) {
         fContext->getGpu()->resolveRenderTarget(surface->asRenderTarget());
     }
@@ -327,12 +328,12 @@
     if (useDIF && fContext->caps()->shaderCaps()->pathRenderingSupport() &&
         GrFSAAType::kNone != rtp->fsaaType()) {
         // TODO: defer stencil buffer attachment for PathRenderingDrawContext
-        sk_sp<GrRenderTarget> rt(
-                sk_ref_sp(rtp->instantiateRenderTarget(fContext->resourceProvider())));
-        if (!rt) {
+        if (!rtp->instantiate(fContext->resourceProvider())) {
             return nullptr;
         }
-        GrStencilAttachment* sb = fContext->resourceProvider()->attachStencilAttachment(rt.get());
+        GrRenderTarget* rt = rtp->priv().peekRenderTarget();
+
+        GrStencilAttachment* sb = fContext->resourceProvider()->attachStencilAttachment(rt);
         if (sb) {
             return sk_sp<GrRenderTargetContext>(new GrPathRenderingRenderTargetContext(
                                                         fContext, this, std::move(rtp),
diff --git a/src/gpu/GrRenderTargetContext.h b/src/gpu/GrRenderTargetContext.h
index cb4d702..bcc5323 100644
--- a/src/gpu/GrRenderTargetContext.h
+++ b/src/gpu/GrRenderTargetContext.h
@@ -324,7 +324,10 @@
     GrRenderTarget* accessRenderTarget() {
         // TODO: usage of this entry point needs to be reduced and potentially eliminated
         // since it ends the deferral of the GrRenderTarget's allocation
-        return fRenderTargetProxy->instantiateRenderTarget(fContext->resourceProvider());
+        if (!fRenderTargetProxy->instantiate(fContext->resourceProvider())) {
+            return nullptr;
+        }
+        return fRenderTargetProxy->priv().peekRenderTarget();
     }
 
     GrSurfaceProxy* asSurfaceProxy() override { return fRenderTargetProxy.get(); }
diff --git a/src/gpu/GrRenderTargetProxy.cpp b/src/gpu/GrRenderTargetProxy.cpp
index 7cc51df..b582455 100644
--- a/src/gpu/GrRenderTargetProxy.cpp
+++ b/src/gpu/GrRenderTargetProxy.cpp
@@ -45,20 +45,19 @@
                    : 0;
 }
 
-GrSurface* GrRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
+bool GrRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
     static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
 
-    GrSurface* surf = this->instantiateImpl(resourceProvider, fSampleCnt, kFlags,
-                                            /* isMipped = */ false,
-                                            SkDestinationSurfaceColorMode::kLegacy);
-    if (!surf) {
-        return nullptr;
+    if (!this->instantiateImpl(resourceProvider, fSampleCnt, kFlags,
+                               /* isMipped = */ false,
+                               SkDestinationSurfaceColorMode::kLegacy)) {
+        return false;
     }
-    SkASSERT(surf->asRenderTarget());
+    SkASSERT(fTarget->asRenderTarget());
     // Check that our a priori computation matched the ultimate reality
-    SkASSERT(fRenderTargetFlags == surf->asRenderTarget()->renderTargetPriv().flags());
+    SkASSERT(fRenderTargetFlags == fTarget->asRenderTarget()->renderTargetPriv().flags());
 
-    return surf;
+    return true;
 }
 
 int GrRenderTargetProxy::worstCaseWidth() const {
diff --git a/src/gpu/GrResourceProvider.cpp b/src/gpu/GrResourceProvider.cpp
index 79fcd95..d8611fe 100644
--- a/src/gpu/GrResourceProvider.cpp
+++ b/src/gpu/GrResourceProvider.cpp
@@ -313,10 +313,10 @@
         return;
     }
 
-    GrTexture* texture = proxy->instantiateTexture(this);
-    if (!texture) {
+    if (!proxy->instantiate(this)) {
         return;
     }
+    GrTexture* texture = proxy->priv().peekTexture();
 
     this->assignUniqueKeyToResource(key, texture);
 }
diff --git a/src/gpu/GrSurfaceProxy.cpp b/src/gpu/GrSurfaceProxy.cpp
index 2577fc8..54bcf0b 100644
--- a/src/gpu/GrSurfaceProxy.cpp
+++ b/src/gpu/GrSurfaceProxy.cpp
@@ -40,11 +40,11 @@
     SkASSERT(!fLastOpList);
 }
 
-GrSurface* GrSurfaceProxy::instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt,
-                                           GrSurfaceFlags flags, bool isMipMapped,
-                                           SkDestinationSurfaceColorMode mipColorMode) {
+bool GrSurfaceProxy::instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt,
+                                     GrSurfaceFlags flags, bool isMipMapped,
+                                     SkDestinationSurfaceColorMode mipColorMode) {
     if (fTarget) {
-        return fTarget;
+        return true;
     }
     GrSurfaceDesc desc;
     desc.fConfig = fConfig;
@@ -64,7 +64,7 @@
         fTarget = resourceProvider->createTexture(desc, fBudgeted, fFlags).release();
     }
     if (!fTarget) {
-        return nullptr;
+        return false;
     }
 
     fTarget->asTexture()->texturePriv().setMipColorMode(mipColorMode);
@@ -76,7 +76,7 @@
     }
 #endif
 
-    return fTarget;
+    return true;
 }
 
 void GrSurfaceProxy::setLastOpList(GrOpList* opList) {
diff --git a/src/gpu/GrSurfaceProxyPriv.h b/src/gpu/GrSurfaceProxyPriv.h
index ea4d9b6..b10cb34 100644
--- a/src/gpu/GrSurfaceProxyPriv.h
+++ b/src/gpu/GrSurfaceProxyPriv.h
@@ -15,13 +15,21 @@
     data members or virtual methods. */
 class GrSurfaceProxyPriv {
 public:
+    // This should only be called after a successful call to instantiate
+    GrSurface* peekSurface() const {
+        SkASSERT(fProxy->fTarget);
+        return fProxy->fTarget;
+    }
+
     // If the proxy is already instantiated, return its backing GrTexture; if not,
     // return null
     GrTexture* peekTexture() const {
         return fProxy->fTarget ? fProxy->fTarget->asTexture() : nullptr;
     }
 
+    // This should only be called after a successful call to instantiate
     GrRenderTarget* peekRenderTarget() const {
+        SkASSERT(fProxy->fTarget && fProxy->fTarget->asRenderTarget());
         return fProxy->fTarget ? fProxy->fTarget->asRenderTarget() : nullptr;
     }
 
diff --git a/src/gpu/GrTextureProxy.cpp b/src/gpu/GrTextureProxy.cpp
index 7928164..98cc75b 100644
--- a/src/gpu/GrTextureProxy.cpp
+++ b/src/gpu/GrTextureProxy.cpp
@@ -21,17 +21,17 @@
 GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf)
         : INHERITED(std::move(surf), SkBackingFit::kExact)
         , fIsMipMapped(fTarget->asTexture()->texturePriv().hasMipMaps())
-        , fMipColorMode(fTarget->asTexture()->texturePriv().mipColorMode()) {}
+        , fMipColorMode(fTarget->asTexture()->texturePriv().mipColorMode()) {
+}
 
-GrSurface* GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
-    GrSurface* surf =
-            this->instantiateImpl(resourceProvider, 0, kNone_GrSurfaceFlags, fIsMipMapped,
-                                  fMipColorMode);
-    if (!surf) {
-        return nullptr;
+bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
+    if (!this->instantiateImpl(resourceProvider, 0, kNone_GrSurfaceFlags, fIsMipMapped,
+                               fMipColorMode)) {
+        return false;
     }
-    SkASSERT(surf->asTexture());
-    return surf;
+
+    SkASSERT(fTarget->asTexture());
+    return true;
 }
 
 void GrTextureProxy::setMipColorMode(SkDestinationSurfaceColorMode colorMode) {
diff --git a/src/gpu/GrTextureRenderTargetProxy.cpp b/src/gpu/GrTextureRenderTargetProxy.cpp
index 7058c83..3981f17 100644
--- a/src/gpu/GrTextureRenderTargetProxy.cpp
+++ b/src/gpu/GrTextureRenderTargetProxy.cpp
@@ -44,16 +44,15 @@
                                   SkBackingFit::kApprox == fFit);
 }
 
-GrSurface* GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
+bool GrTextureRenderTargetProxy::instantiate(GrResourceProvider* resourceProvider) {
     static constexpr GrSurfaceFlags kFlags = kRenderTarget_GrSurfaceFlag;
 
-    GrSurface* surf = this->instantiateImpl(resourceProvider, this->numStencilSamples(), kFlags,
-                                            this->isMipMapped(), this->mipColorMode());
-    if (!surf) {
-        return nullptr;
+    if (!this->instantiateImpl(resourceProvider, this->numStencilSamples(), kFlags,
+                               this->isMipMapped(), this->mipColorMode())) {
+        return false;
     }
-    SkASSERT(surf->asRenderTarget());
-    SkASSERT(surf->asTexture());
+    SkASSERT(fTarget->asRenderTarget());
+    SkASSERT(fTarget->asTexture());
 
-    return surf;
+    return true;
 }
diff --git a/src/gpu/GrTextureRenderTargetProxy.h b/src/gpu/GrTextureRenderTargetProxy.h
index 992e319..a9e8b52 100644
--- a/src/gpu/GrTextureRenderTargetProxy.h
+++ b/src/gpu/GrTextureRenderTargetProxy.h
@@ -32,7 +32,7 @@
     // Wrapped version
     GrTextureRenderTargetProxy(sk_sp<GrSurface>);
 
-    GrSurface* instantiate(GrResourceProvider*) override;
+    bool instantiate(GrResourceProvider*) override;
 
     size_t onUninstantiatedGpuMemorySize() const override;
 };
diff --git a/src/gpu/effects/GrSimpleTextureEffect.h b/src/gpu/effects/GrSimpleTextureEffect.h
index 8e0e4b0..0b01ac7 100644
--- a/src/gpu/effects/GrSimpleTextureEffect.h
+++ b/src/gpu/effects/GrSimpleTextureEffect.h
@@ -28,8 +28,7 @@
         // MDB TODO: remove this instantiation once instantiation is pushed past the
         // TextureSamplers. Instantiation failure in the TextureSampler is difficult to
         // recover from.
-        GrTexture* temp = proxy->instantiateTexture(resourceProvider);
-        if (!temp) {
+        if (!proxy->instantiate(resourceProvider)) {
             return nullptr;
         }
 
@@ -48,8 +47,7 @@
         // MDB TODO: remove this instantiation once instantiation is pushed past the
         // TextureSamplers. Instantiation failure in the TextureSampler is difficult to
         // recover from.
-        GrTexture* temp = proxy->instantiateTexture(resourceProvider);
-        if (!temp) {
+        if (!proxy->instantiate(resourceProvider)) {
             return nullptr;
         }
 
@@ -67,8 +65,7 @@
         // MDB TODO: remove this instantiation once instantiation is pushed past the
         // TextureSamplers. Instantiation failure in the TextureSampler is difficult to
         // recover from.
-        GrTexture* temp = proxy->instantiateTexture(resourceProvider);
-        if (!temp) {
+        if (!proxy->instantiate(resourceProvider)) {
             return nullptr;
         }
 
diff --git a/src/gpu/ops/GrCopySurfaceOp.h b/src/gpu/ops/GrCopySurfaceOp.h
index f1dac8f..6c80408 100644
--- a/src/gpu/ops/GrCopySurfaceOp.h
+++ b/src/gpu/ops/GrCopySurfaceOp.h
@@ -56,13 +56,13 @@
     void onExecute(GrOpFlushState* state) override {
         SkASSERT(!state->commandBuffer());
 
-        GrSurface* dst = fDst.get()->instantiate(state->resourceProvider());
-        GrSurface* src = fSrc.get()->instantiate(state->resourceProvider());
-        if (!dst || !src) {
+        if (!fDst.get()->instantiate(state->resourceProvider()) ||
+            !fSrc.get()->instantiate(state->resourceProvider())) {
             return;
         }
 
-        state->gpu()->copySurface(dst, src, fSrcRect, fDstPoint);
+        state->gpu()->copySurface(fDst.get()->priv().peekSurface(),
+                                  fSrc.get()->priv().peekSurface(), fSrcRect, fDstPoint);
     }
 
     // For RenderTargetContexts 'fDst' is redundant with the RenderTarget that will be passed
diff --git a/src/image/SkImage_Gpu.cpp b/src/image/SkImage_Gpu.cpp
index b1eac97..660c31c 100644
--- a/src/image/SkImage_Gpu.cpp
+++ b/src/image/SkImage_Gpu.cpp
@@ -155,15 +155,20 @@
                                                 GrSurfaceOrigin* origin) const {
     SkASSERT(fProxy);
 
-    GrSurface* surface = fProxy->instantiate(fContext->resourceProvider());
-    if (surface && surface->asTexture()) {
+    if (!fProxy->instantiate(fContext->resourceProvider())) {
+        return 0;
+    }
+
+    GrTexture* texture = fProxy->priv().peekTexture();
+
+    if (texture) {
         if (flushPendingGrContextIO) {
             fContext->contextPriv().prepareSurfaceForExternalIO(fProxy.get());
         }
         if (origin) {
             *origin = fProxy->origin();
         }
-        return surface->asTexture()->getTextureHandle();
+        return texture->getTextureHandle();
     }
     return 0;
 }
@@ -174,7 +179,11 @@
         return nullptr;
     }
 
-    return proxy->instantiateTexture(fContext->resourceProvider());
+    if (!proxy->instantiate(fContext->resourceProvider())) {
+        return nullptr;
+    }
+
+    return proxy->priv().peekTexture();
 }
 
 bool SkImage_Gpu::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
@@ -489,10 +498,10 @@
         return codecImage;
     }
 
-    sk_sp<GrTexture> texture(sk_ref_sp(proxy->instantiateTexture(context->resourceProvider())));
-    if (!texture) {
+    if (!proxy->instantiate(context->resourceProvider())) {
         return codecImage;
     }
+    sk_sp<GrTexture> texture = sk_ref_sp(proxy->priv().peekTexture());
 
     // Flush any writes or uploads
     context->contextPriv().prepareSurfaceForExternalIO(proxy.get());
diff --git a/src/image/SkImage_Gpu.h b/src/image/SkImage_Gpu.h
index 273f93e..fbe29d8 100644
--- a/src/image/SkImage_Gpu.h
+++ b/src/image/SkImage_Gpu.h
@@ -36,7 +36,11 @@
         return fProxy.get();
     }
     GrTexture* peekTexture() const override {
-        return fProxy->instantiateTexture(fContext->resourceProvider());
+        if (!fProxy->instantiate(fContext->resourceProvider())) {
+            return nullptr;
+        }
+
+        return fProxy->priv().peekTexture();
     }
     sk_sp<GrTextureProxy> asTextureProxyRef() const override {
         return fProxy;