Make GrSurfaceProxy constructors take arg that determines participation in
GrResourceAllocator.

Removes LazyInstantiationType. All callbacks can be invoked one time (if successful).
Lazy callbacks indicate whether their lifetime should be extended, which is used by
promise image proxy callbacks.

Promise image proxies are no longer deinstantiated at the end of flush and
GrDeinstantiateProxyTracker is removed. They will be instantiated the first time
they are encountered in GrResourceAllocator::addInterval (without actually adding
an interval) and then remain instantiated.

Also removes some "helper" versions of proxy factory functions that took fewer
params. They weren't much used outside of test code and as the total number of params
has grown their relative utility has diminished. We could consider a params struct
or radically simpler helpers that take only a few params if desired.

Change-Id: Ic6b09e7b807b66cb9fcbb7a67ae0f9faf345485f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/238216
Reviewed-by: Robert Phillips <robertphillips@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
diff --git a/tests/LazyProxyTest.cpp b/tests/LazyProxyTest.cpp
index c7e5ffc..106a51a 100644
--- a/tests/LazyProxyTest.cpp
+++ b/tests/LazyProxyTest.cpp
@@ -84,8 +84,8 @@
                 ctx->priv().caps()->getDefaultBackendFormat(GrColorType::kBGR_565,
                                                             GrRenderable::kNo);
             fProxy = GrProxyProvider::MakeFullyLazyProxy(
-                    [this, format, nullTexture](
-                            GrResourceProvider* rp) -> GrSurfaceProxy::LazyInstantiationResult {
+                    [this, format,
+                     nullTexture](GrResourceProvider* rp) -> GrSurfaceProxy::LazyCallbackResult {
                         REPORTER_ASSERT(fTest->fReporter, !fTest->fHasOpTexture);
                         fTest->fHasOpTexture = true;
                         if (nullTexture) {
@@ -103,7 +103,8 @@
                         }
                     },
                     format, GrRenderable::kNo, 1, GrProtected::kNo, kTopLeft_GrSurfaceOrigin,
-                    kRGB_565_GrPixelConfig, *proxyProvider->caps());
+                    kRGB_565_GrPixelConfig, *proxyProvider->caps(),
+                    GrSurfaceProxy::UseAllocator::kYes);
 
             this->setBounds(SkRectPriv::MakeLargest(), GrOp::HasAABloat::kNo,
                             GrOp::IsZeroArea::kNo);
@@ -134,14 +135,15 @@
                 ctx->priv().caps()->getDefaultBackendFormat(GrColorType::kAlpha_F16,
                                                             GrRenderable::kYes);
             fLazyProxy = GrProxyProvider::MakeFullyLazyProxy(
-                    [this](GrResourceProvider* rp) -> GrSurfaceProxy::LazyInstantiationResult {
+                    [this](GrResourceProvider* rp) -> GrSurfaceProxy::LazyCallbackResult {
                         REPORTER_ASSERT(fTest->fReporter, !fTest->fHasClipTexture);
                         fTest->fHasClipTexture = true;
                         fAtlas->instantiate(rp);
                         return sk_ref_sp(fAtlas->peekTexture());
                     },
                     format, GrRenderable::kYes, 1, GrProtected::kNo, kBottomLeft_GrSurfaceOrigin,
-                    kAlpha_half_GrPixelConfig, *proxyProvider->caps());
+                    kAlpha_half_GrPixelConfig, *proxyProvider->caps(),
+                    GrSurfaceProxy::UseAllocator::kYes);
             fAccess.reset(fLazyProxy, GrSamplerState::Filter::kNearest,
                           GrSamplerState::WrapMode::kClamp);
             this->setTextureSamplerCnt(1);
@@ -236,19 +238,26 @@
     GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
                                                            GrRenderable::kNo);
 
-    using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
-    using LazyInstantiationResult = GrSurfaceProxy::LazyInstantiationResult;
+    auto tex = ctx->priv().resourceProvider()->createTexture(desc, format, GrRenderable::kNo, 1,
+                                                             SkBudgeted::kNo, GrProtected::kNo);
+    using LazyInstantiationResult = GrSurfaceProxy::LazyCallbackResult;
     for (bool doInstantiate : {true, false}) {
-        for (auto lazyType : {LazyInstantiationType::kSingleUse,
-                              LazyInstantiationType::kMultipleUse,
-                              LazyInstantiationType::kDeinstantiate}) {
+        for (bool releaseCallback : {false, true}) {
             int testCount = 0;
             // Sets an integer to 1 when the callback is called and -1 when it is deleted.
             class TestCallback {
             public:
-                TestCallback(int* value) : fValue(value) {}
+                TestCallback(int* value, bool releaseCallback, sk_sp<GrTexture> tex)
+                        : fValue(value)
+                        , fReleaseCallback(releaseCallback)
+                        , fTexture(std::move(tex)) {}
                 TestCallback(const TestCallback& that) { SkASSERT(0); }
-                TestCallback(TestCallback&& that) : fValue(that.fValue) { that.fValue = nullptr; }
+                TestCallback(TestCallback&& that)
+                        : fValue(that.fValue)
+                        , fReleaseCallback(that.fReleaseCallback)
+                        , fTexture(std::move(that.fTexture)) {
+                    that.fValue = nullptr;
+                }
 
                 ~TestCallback() { fValue ? (void)(*fValue = -1) : void(); }
 
@@ -260,25 +269,27 @@
 
                 LazyInstantiationResult operator()(GrResourceProvider* resourceProvider) const {
                     *fValue = 1;
-                    return {};
+                    return {fTexture, fReleaseCallback};
                 }
 
             private:
                 int* fValue = nullptr;
+                bool fReleaseCallback;
+                sk_sp<GrTexture> fTexture;
             };
             sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy(
-                    TestCallback(&testCount), format, desc, GrRenderable::kNo, 1,
-                    kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated,
+                    TestCallback(&testCount, releaseCallback, tex), format, desc, GrRenderable::kNo,
+                    1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, GrMipMapsStatus::kNotAllocated,
                     GrInternalSurfaceFlags::kNone, SkBackingFit::kExact, SkBudgeted::kNo,
-                    GrProtected::kNo, lazyType);
+                    GrProtected::kNo, GrSurfaceProxy::UseAllocator::kYes);
 
             REPORTER_ASSERT(reporter, proxy.get());
             REPORTER_ASSERT(reporter, 0 == testCount);
 
             if (doInstantiate) {
                 proxy->priv().doLazyInstantiation(ctx->priv().resourceProvider());
-                if (LazyInstantiationType::kSingleUse == proxy->priv().lazyInstantiationType()) {
-                    // In SingleUse we will call the cleanup and delete the callback in the
+                if (releaseCallback) {
+                    // We will call the cleanup and delete the callback in the
                     // doLazyInstantiationCall.
                     REPORTER_ASSERT(reporter, -1 == testCount);
                 } else {
@@ -330,7 +341,7 @@
 
         fLazyProxy = proxyProvider->createLazyProxy(
                 [testExecuteValue, shouldFailInstantiation, desc,
-                 format](GrResourceProvider* rp) -> GrSurfaceProxy::LazyInstantiationResult {
+                 format](GrResourceProvider* rp) -> GrSurfaceProxy::LazyCallbackResult {
                     if (shouldFailInstantiation) {
                         *testExecuteValue = 1;
                         return {};
@@ -338,11 +349,11 @@
                     return {rp->createTexture(desc, format, GrRenderable::kNo, 1, SkBudgeted::kNo,
                                               GrProtected::kNo,
                                               GrResourceProvider::Flags::kNoPendingIO),
-                            GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced};
+                            true, GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced};
                 },
                 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
-                GrMipMapsStatus::kNotAllocated, SkBackingFit::kExact, SkBudgeted::kNo,
-                GrProtected::kNo);
+                GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kNone, SkBackingFit::kExact,
+                SkBudgeted::kNo, GrProtected::kNo, GrSurfaceProxy::UseAllocator::kYes);
 
         SkASSERT(fLazyProxy.get());
 
@@ -393,131 +404,3 @@
         }
     }
 }
-
-class LazyDeinstantiateTestOp : public GrDrawOp {
-public:
-    DEFINE_OP_CLASS_ID
-
-    static std::unique_ptr<GrDrawOp> Make(GrContext* context, sk_sp<GrTextureProxy> proxy) {
-        GrOpMemoryPool* pool = context->priv().opMemoryPool();
-
-        return pool->allocate<LazyDeinstantiateTestOp>(std::move(proxy));
-    }
-
-    void visitProxies(const VisitProxyFunc& func) const override {
-        func(fLazyProxy.get(), GrMipMapped::kNo);
-    }
-
-private:
-    friend class GrOpMemoryPool; // for ctor
-
-    LazyDeinstantiateTestOp(sk_sp<GrTextureProxy> proxy)
-            : INHERITED(ClassID()), fLazyProxy(std::move(proxy)) {
-        this->setBounds(SkRect::MakeIWH(kSize, kSize), HasAABloat::kNo, IsZeroArea::kNo);
-    }
-
-    const char* name() const override { return "LazyDeinstantiateTestOp"; }
-    FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
-    GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
-                                      bool hasMixedSampledCoverage, GrClampType) override {
-        return GrProcessorSet::EmptySetAnalysis();
-    }
-    void onPrepare(GrOpFlushState*) override {}
-    void onExecute(GrOpFlushState* state, const SkRect& chainBounds) override {}
-
-    sk_sp<GrTextureProxy> fLazyProxy;
-
-    typedef GrDrawOp INHERITED;
-};
-
-static void DeinstantiateReleaseProc(void* releaseValue) { (*static_cast<int*>(releaseValue))++; }
-
-// Test that lazy proxies with the Deinstantiate LazyCallbackType are deinstantiated and released as
-// expected.
-DEF_GPUTEST(LazyProxyDeinstantiateTest, reporter, /* options */) {
-    GrMockOptions mockOptions;
-    sk_sp<GrContext> ctx = GrContext::MakeMock(&mockOptions, GrContextOptions());
-    GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
-
-    GrBackendFormat format =
-        ctx->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
-                                                    GrRenderable::kNo);
-
-    using LazyType = GrSurfaceProxy::LazyInstantiationType;
-    for (auto lazyType : {LazyType::kSingleUse, LazyType::kMultipleUse, LazyType::kDeinstantiate}) {
-        auto rtc = ctx->priv().makeDeferredRenderTargetContext(SkBackingFit::kExact, 100, 100,
-                                                               GrColorType::kRGBA_8888, nullptr);
-        REPORTER_ASSERT(reporter, rtc);
-
-        rtc->clear(nullptr, SkPMColor4f::FromBytes_RGBA(0xbaaaaaad),
-                   GrRenderTargetContext::CanClearFullscreen::kYes);
-
-        int instantiateTestValue = 0;
-        int releaseTestValue = 0;
-        int* instantiatePtr = &instantiateTestValue;
-        int* releasePtr = &releaseTestValue;
-        GrSurfaceDesc desc;
-        desc.fWidth = kSize;
-        desc.fHeight = kSize;
-        desc.fConfig = kRGBA_8888_GrPixelConfig;
-
-        GrBackendTexture backendTex = ctx->createBackendTexture(
-                kSize, kSize, kRGBA_8888_SkColorType, SkColors::kTransparent,
-                GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
-
-        sk_sp<GrTextureProxy> lazyProxy = proxyProvider->createLazyProxy(
-                [instantiatePtr, releasePtr,
-                 backendTex](GrResourceProvider* rp) -> GrSurfaceProxy::LazyInstantiationResult {
-                    sk_sp<GrTexture> texture =
-                            rp->wrapBackendTexture(backendTex, GrColorType::kRGBA_8888,
-                                                   kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
-                                                   kRead_GrIOType);
-                    if (!texture) {
-                        return {};
-                    }
-                    (*instantiatePtr)++;
-                    texture->setRelease(DeinstantiateReleaseProc, releasePtr);
-                    return texture;
-                },
-                format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
-                GrMipMapsStatus::kNotAllocated, GrInternalSurfaceFlags::kReadOnly,
-                SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo, lazyType);
-
-        REPORTER_ASSERT(reporter, lazyProxy.get());
-
-        rtc->priv().testingOnly_addDrawOp(LazyDeinstantiateTestOp::Make(ctx.get(), lazyProxy));
-
-        ctx->flush();
-
-        REPORTER_ASSERT(reporter, 1 == instantiateTestValue);
-        if (LazyType::kDeinstantiate == lazyType) {
-            REPORTER_ASSERT(reporter, 1 == releaseTestValue);
-        } else {
-            REPORTER_ASSERT(reporter, 0 == releaseTestValue);
-        }
-
-        // This should cause the uninstantiate proxies to be instantiated again but have no effect
-        // on the others
-        rtc->priv().testingOnly_addDrawOp(LazyDeinstantiateTestOp::Make(ctx.get(), lazyProxy));
-        // Add a second op to make sure we only instantiate once.
-        rtc->priv().testingOnly_addDrawOp(LazyDeinstantiateTestOp::Make(ctx.get(), lazyProxy));
-        ctx->flush();
-
-        if (LazyType::kDeinstantiate == lazyType) {
-            REPORTER_ASSERT(reporter, 2 == instantiateTestValue);
-            REPORTER_ASSERT(reporter, 2 == releaseTestValue);
-        } else {
-            REPORTER_ASSERT(reporter, 1 == instantiateTestValue);
-            REPORTER_ASSERT(reporter, 0 == releaseTestValue);
-        }
-
-        lazyProxy.reset();
-        if (LazyType::kDeinstantiate == lazyType) {
-            REPORTER_ASSERT(reporter, 2 == releaseTestValue);
-        } else {
-            REPORTER_ASSERT(reporter, 1 == releaseTestValue);
-        }
-
-        ctx->deleteBackendTexture(backendTex);
-    }
-}