ccpr: Use lazy proxies with GrCCAtlas

Bug: skia:
Change-Id: I576d9303d451352778de0425e3ecbc561331cd09
Reviewed-on: https://skia-review.googlesource.com/135362
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Chris Dalton <csmartdalton@google.com>
diff --git a/src/gpu/ccpr/GrCCAtlas.cpp b/src/gpu/ccpr/GrCCAtlas.cpp
index cbf6993..94a37e8 100644
--- a/src/gpu/ccpr/GrCCAtlas.cpp
+++ b/src/gpu/ccpr/GrCCAtlas.cpp
@@ -9,8 +9,10 @@
 
 #include "GrCaps.h"
 #include "GrOnFlushResourceProvider.h"
+#include "GrProxyProvider.h"
 #include "GrRectanizer_skyline.h"
 #include "GrRenderTargetContext.h"
+#include "GrTexture.h"
 #include "GrTextureProxy.h"
 #include "SkMakeUnique.h"
 #include "SkMathPriv.h"
@@ -44,9 +46,12 @@
     GrRectanizerSkyline fRectanizer;
 };
 
-GrCCAtlas::GrCCAtlas(const Specs& specs)
+GrCCAtlas::GrCCAtlas(GrPixelConfig pixelConfig, const Specs& specs, const GrCaps& caps)
         : fMaxTextureSize(SkTMax(SkTMax(specs.fMinHeight, specs.fMinWidth),
                                  specs.fMaxPreferredTextureSize)) {
+    // Caller should have cropped any paths to the destination render target instead of asking for
+    // an atlas larger than maxRenderTargetSize.
+    SkASSERT(fMaxTextureSize <= caps.maxTextureSize());
     SkASSERT(specs.fMaxPreferredTextureSize > 0);
 
     // Begin with the first pow2 dimensions whose area is theoretically large enough to contain the
@@ -66,14 +71,28 @@
     }
 
     fTopNode = skstd::make_unique<Node>(nullptr, 0, 0, fWidth, fHeight);
+
+    fTextureProxy = GrProxyProvider::MakeFullyLazyProxy(
+            [this, pixelConfig](GrResourceProvider* resourceProvider) {
+                    if (!resourceProvider) {
+                        return sk_sp<GrTexture>();
+                    }
+                    GrSurfaceDesc desc;
+                    desc.fFlags = kRenderTarget_GrSurfaceFlag;
+                    desc.fWidth = fWidth;
+                    desc.fHeight = fHeight;
+                    desc.fConfig = pixelConfig;
+                    return resourceProvider->createTexture(desc, SkBudgeted::kYes);
+            },
+            GrProxyProvider::Renderable::kYes, kTextureOrigin, pixelConfig, caps);
 }
 
 GrCCAtlas::~GrCCAtlas() {
 }
 
 bool GrCCAtlas::addRect(const SkIRect& devIBounds, SkIVector* offset) {
-    // This can't be called anymore once makeClearedTextureProxy() has been called.
-    SkASSERT(!fTextureProxy);
+    // This can't be called anymore once makeRenderTargetContext() has been called.
+    SkASSERT(!fTextureProxy->priv().isInstantiated());
 
     SkIPoint16 location;
     if (!this->internalPlaceRect(devIBounds.width(), devIBounds.height(), &location)) {
@@ -112,21 +131,19 @@
     return true;
 }
 
-sk_sp<GrRenderTargetContext> GrCCAtlas::initInternalTextureProxy(
-        GrOnFlushResourceProvider* onFlushRP, GrPixelConfig config) {
-    SkASSERT(!fTextureProxy);
-    // Caller should have cropped any paths to the destination render target instead of asking for
-    // an atlas larger than maxRenderTargetSize.
-    SkASSERT(SkTMax(fHeight, fWidth) <= fMaxTextureSize);
+void GrCCAtlas::setUserBatchID(int id) {
+    // This can't be called anymore once makeRenderTargetContext() has been called.
+    SkASSERT(!fTextureProxy->priv().isInstantiated());
+    fUserBatchID = id;
+}
+
+sk_sp<GrRenderTargetContext> GrCCAtlas::makeRenderTargetContext(
+        GrOnFlushResourceProvider* onFlushRP) {
+    SkASSERT(!fTextureProxy->priv().isInstantiated());  // This method should only be called once.
     SkASSERT(fMaxTextureSize <= onFlushRP->caps()->maxRenderTargetSize());
 
-    GrSurfaceDesc desc;
-    desc.fFlags = kRenderTarget_GrSurfaceFlag;
-    desc.fWidth = fWidth;
-    desc.fHeight = fHeight;
-    desc.fConfig = config;
     sk_sp<GrRenderTargetContext> rtc =
-            onFlushRP->makeRenderTargetContext(desc, kTextureOrigin, nullptr, nullptr);
+            onFlushRP->makeRenderTargetContext(fTextureProxy, nullptr, nullptr);
     if (!rtc) {
         SkDebugf("WARNING: failed to allocate a %ix%i atlas. Some paths will not be drawn.\n",
                  fWidth, fHeight);
@@ -135,20 +152,18 @@
 
     SkIRect clearRect = SkIRect::MakeSize(fDrawBounds);
     rtc->clear(&clearRect, 0, GrRenderTargetContext::CanClearFullscreen::kYes);
-
-    fTextureProxy = sk_ref_sp(rtc->asTextureProxy());
     return rtc;
 }
 
-GrCCAtlas* GrCCAtlasStack::addRect(const SkIRect& devIBounds, SkIVector* offset) {
+GrCCAtlas* GrCCAtlasStack::addRect(const SkIRect& devIBounds, SkIVector* devToAtlasOffset) {
     GrCCAtlas* retiredAtlas = nullptr;
-    if (fAtlases.empty() || !fAtlases.back().addRect(devIBounds, offset)) {
+    if (fAtlases.empty() || !fAtlases.back().addRect(devIBounds, devToAtlasOffset)) {
         // The retired atlas is out of room and can't grow any bigger.
         retiredAtlas = !fAtlases.empty() ? &fAtlases.back() : nullptr;
-        fAtlases.emplace_back(fSpecs);
+        fAtlases.emplace_back(fPixelConfig, fSpecs, *fCaps);
         SkASSERT(devIBounds.width() <= fSpecs.fMinWidth);
         SkASSERT(devIBounds.height() <= fSpecs.fMinHeight);
-        SkAssertResult(fAtlases.back().addRect(devIBounds, offset));
+        SkAssertResult(fAtlases.back().addRect(devIBounds, devToAtlasOffset));
     }
     return retiredAtlas;
 }