Patch up ref counting of proxies

Bug: skia:
Change-Id: If746283d788368bf7aad6d285f181d8531768e61
Reviewed-on: https://skia-review.googlesource.com/70024
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/include/private/GrSurfaceProxy.h b/include/private/GrSurfaceProxy.h
index 23f6219..f99e199 100644
--- a/include/private/GrSurfaceProxy.h
+++ b/include/private/GrSurfaceProxy.h
@@ -131,7 +131,7 @@
         SkASSERT(fTarget);
 
         SkASSERT(fTarget->fRefCnt > 0);
-        fTarget->fRefCnt += (fRefCnt-1); // don't xfer the proxy's creation ref
+        fTarget->fRefCnt += SkTMax(0, fRefCnt-1); // don't xfer the proxy's creation ref
         fTarget->fPendingReads += fPendingReads;
         fTarget->fPendingWrites += fPendingWrites;
     }
diff --git a/src/gpu/GrOnFlushResourceProvider.cpp b/src/gpu/GrOnFlushResourceProvider.cpp
index 42237a7..c9fc77e 100644
--- a/src/gpu/GrOnFlushResourceProvider.cpp
+++ b/src/gpu/GrOnFlushResourceProvider.cpp
@@ -39,6 +39,14 @@
         return nullptr;
     }
 
+    // Since this is at flush time and these won't be allocated for us by the GrResourceAllocator
+    // we have to manually ensure it is allocated here. The proxy had best have been created
+    // with the kNoPendingIO flag!
+    if (!renderTargetContext->asSurfaceProxy()->instantiate(
+                                                fDrawingMgr->getContext()->resourceProvider())) {
+        return nullptr;
+    }
+
     renderTargetContext->discard();
 
     return renderTargetContext;
@@ -59,6 +67,14 @@
         return nullptr;
     }
 
+    // Since this is at flush time and these won't be allocated for us by the GrResourceAllocator
+    // we have to manually ensure it is allocated here. The proxy had best have been created
+    // with the kNoPendingIO flag!
+    if (!renderTargetContext->asSurfaceProxy()->instantiate(
+                                                fDrawingMgr->getContext()->resourceProvider())) {
+        return nullptr;
+    }
+
     renderTargetContext->discard();
 
     return renderTargetContext;
diff --git a/tests/GrMipMappedTest.cpp b/tests/GrMipMappedTest.cpp
index f023018..c2a0df2 100644
--- a/tests/GrMipMappedTest.cpp
+++ b/tests/GrMipMappedTest.cpp
@@ -53,7 +53,7 @@
                                                                        mipMapped,
                                                                        backendHandle);
 
-            GrTextureProxy* proxy;
+            sk_sp<GrTextureProxy> proxy;
             sk_sp<SkImage> image;
             if (isRT) {
                 sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(
@@ -65,12 +65,12 @@
                                                                            nullptr);
 
                 SkGpuDevice* device = ((SkSurface_Gpu*)surface.get())->getDevice();
-                proxy = device->accessRenderTargetContext()->asTextureProxy();
+                proxy = device->accessRenderTargetContext()->asTextureProxyRef();
             } else {
                 image = SkImage::MakeFromTexture(context, backendTex,
                                                  kTopLeft_GrSurfaceOrigin,
                                                  kPremul_SkAlphaType, nullptr);
-                proxy = as_IB(image)->peekProxy();
+                proxy = as_IB(image)->asTextureProxyRef();
             }
             REPORTER_ASSERT(reporter, proxy);
             if (!proxy) {
diff --git a/tests/OnFlushCallbackTest.cpp b/tests/OnFlushCallbackTest.cpp
index 0c2edfb..c7af59a 100644
--- a/tests/OnFlushCallbackTest.cpp
+++ b/tests/OnFlushCallbackTest.cpp
@@ -415,8 +415,6 @@
     for (int i = 0; i < 3; ++i) {
         SkRect r = SkRect::MakeXYWH(i*kDrawnTileSize, 0, kDrawnTileSize, kDrawnTileSize);
 
-        // TODO: here is the blocker for deferring creation of the atlas. The TextureSamplers
-        // created here currently require a hard GrTexture.
         auto fp = GrSimpleTextureEffect::Make(fakeAtlas, SkMatrix::I());
         GrPaint paint;
         paint.addColorFragmentProcessor(std::move(fp));
@@ -481,12 +479,10 @@
     desc.fWidth = 32;
     desc.fHeight = 16;
     desc.fConfig = kSkia8888_GrPixelConfig;
-    sk_sp<GrSurfaceProxy> atlasDest = GrSurfaceProxy::MakeDeferred(
-                                                            context->resourceProvider(),
-                                                            desc, SkBackingFit::kExact,
-                                                            SkBudgeted::kYes,
-                                                            GrResourceProvider::kNoPendingIO_Flag);
-    return sk_ref_sp(atlasDest->asTextureProxy());
+    return GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
+                                        desc, SkBackingFit::kExact,
+                                        SkBudgeted::kYes,
+                                        GrResourceProvider::kNoPendingIO_Flag);
 }
 #endif