[graphite] Instantiate non-budgeted TextureProxies in Make()

Also removes the non-TextureInfo factories since it's simple enough to
just call Caps to get the default TextureInfo in those few places.

This changes it so that client-triggered copies and texture-backed
images will have an actual Texture upon return, matching the behavior
for graphite surfaces. The code to instantiate surface texture proxies
is now redundant for non-budgeted cases, but there are some internal
call sites that use budgeted surfaces and require that they are
instantiated.

Bug: b/336779772
Change-Id: I78f45ed350205b433767096f0947b4d37166c58c
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/845436
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
Reviewed-by: James Godfrey-Kittle <jamesgk@google.com>
diff --git a/fuzz/FuzzPrecompile.cpp b/fuzz/FuzzPrecompile.cpp
index 76f7acd..4efacd1 100644
--- a/fuzz/FuzzPrecompile.cpp
+++ b/fuzz/FuzzPrecompile.cpp
@@ -318,13 +318,17 @@
                                     /* dstTexture= */ nullptr,
                                     /* dstOffset= */ {0, 0});
 
+    auto dstTexInfo = recorder->priv().caps()->getDefaultSampledTextureInfo(kRGBA_8888_SkColorType,
+                                                                            skgpu::Mipmapped::kNo,
+                                                                            skgpu::Protected::kNo,
+                                                                            skgpu::Renderable::kNo);
+    // Use Budgeted::kYes to avoid immediately instantiating the TextureProxy. This test doesn't
+    // require full resources.
     sk_sp<TextureProxy> fakeDstTexture = TextureProxy::Make(recorder->priv().caps(),
+                                                            recorder->priv().resourceProvider(),
                                                             SkISize::Make(1, 1),
-                                                            kRGBA_8888_SkColorType,
-                                                            skgpu::Mipmapped::kNo,
-                                                            skgpu::Protected::kNo,
-                                                            skgpu::Renderable::kYes,
-                                                            skgpu::Budgeted::kNo);
+                                                            dstTexInfo,
+                                                            skgpu::Budgeted::kYes);
     constexpr SkIPoint fakeDstOffset = SkIPoint::Make(0, 0);
 
     DrawTypeFlags kDrawType = DrawTypeFlags::kShape;
diff --git a/src/gpu/graphite/AtlasProvider.cpp b/src/gpu/graphite/AtlasProvider.cpp
index 4f11c8f..7e6741b 100644
--- a/src/gpu/graphite/AtlasProvider.cpp
+++ b/src/gpu/graphite/AtlasProvider.cpp
@@ -59,24 +59,21 @@
         return iter->second;
     }
 
-    sk_sp<TextureProxy> proxy;
-    if (requireStorageUsage) {
-        proxy = TextureProxy::MakeStorage(recorder->priv().caps(),
-                                          SkISize::Make(int32_t(width), int32_t(height)),
-                                          colorType,
-                                          skgpu::Budgeted::kYes);
-    } else {
-        // We currently only make the distinction between a storage texture (written by a
-        // compute pass) and a plain sampleable texture (written via upload) that won't be
-        // used as a render attachment.
-        proxy = TextureProxy::Make(recorder->priv().caps(),
-                                   SkISize::Make(int32_t(width), int32_t(height)),
-                                   colorType,
-                                   skgpu::Mipmapped::kNo,
-                                   recorder->priv().isProtected(),
-                                   skgpu::Renderable::kNo,
-                                   skgpu::Budgeted::kYes);
-    }
+    // We currently only make the distinction between a storage texture (written by a
+    // compute pass) and a plain sampleable texture (written via upload) that won't be
+    // used as a render attachment.
+    const Caps* caps = recorder->priv().caps();
+    auto textureInfo = requireStorageUsage
+            ? caps->getDefaultStorageTextureInfo(colorType)
+            : caps->getDefaultSampledTextureInfo(colorType,
+                                                 Mipmapped::kNo,
+                                                 recorder->priv().isProtected(),
+                                                 Renderable::kNo);
+    sk_sp<TextureProxy> proxy = TextureProxy::Make(caps,
+                                                   recorder->priv().resourceProvider(),
+                                                   SkISize::Make((int32_t) width, (int32_t) height),
+                                                   textureInfo,
+                                                   Budgeted::kYes);
     if (!proxy) {
         return nullptr;
     }
diff --git a/src/gpu/graphite/Device.cpp b/src/gpu/graphite/Device.cpp
index 8f2fcde..b0387d8 100644
--- a/src/gpu/graphite/Device.cpp
+++ b/src/gpu/graphite/Device.cpp
@@ -247,17 +247,17 @@
         return nullptr;
     }
 
-    Protected isProtected = Protected(recorder->priv().caps()->protectedSupport());
+    const Caps* caps = recorder->priv().caps();
     SkISize backingDimensions = backingFit == SkBackingFit::kApprox ? GetApproxSize(ii.dimensions())
                                                                     : ii.dimensions();
+    auto textureInfo = caps->getDefaultSampledTextureInfo(ii.colorType(),
+                                                          mipmapped,
+                                                          recorder->priv().isProtected(),
+                                                          Renderable::kYes);
+
     return Make(recorder,
-                TextureProxy::Make(recorder->priv().caps(),
-                                   backingDimensions,
-                                   ii.colorType(),
-                                   mipmapped,
-                                   isProtected,
-                                   Renderable::kYes,
-                                   budgeted),
+                TextureProxy::Make(caps, recorder->priv().resourceProvider(),
+                                   backingDimensions, textureInfo, budgeted),
                 ii.dimensions(),
                 ii.colorInfo(),
                 props,
diff --git a/src/gpu/graphite/DrawAtlas.cpp b/src/gpu/graphite/DrawAtlas.cpp
index 8875178..71a1be9 100644
--- a/src/gpu/graphite/DrawAtlas.cpp
+++ b/src/gpu/graphite/DrawAtlas.cpp
@@ -454,11 +454,14 @@
 
     const Caps* caps = recorder->priv().caps();
     auto textureInfo = caps->getDefaultSampledTextureInfo(fColorType,
-                                                          /*mipmapped=*/Mipmapped::kNo,
+                                                          Mipmapped::kNo,
                                                           recorder->priv().isProtected(),
                                                           Renderable::kNo);
-    fProxies[fNumActivePages] = TextureProxy::Make(
-            caps, {fTextureWidth, fTextureHeight}, textureInfo, skgpu::Budgeted::kYes);
+    fProxies[fNumActivePages] = TextureProxy::Make(caps,
+                                                   recorder->priv().resourceProvider(),
+                                                   {fTextureWidth, fTextureHeight},
+                                                   textureInfo,
+                                                   skgpu::Budgeted::kYes);
     if (!fProxies[fNumActivePages]) {
         return false;
     }
diff --git a/src/gpu/graphite/DrawPass.cpp b/src/gpu/graphite/DrawPass.cpp
index ae096f1..b405456 100644
--- a/src/gpu/graphite/DrawPass.cpp
+++ b/src/gpu/graphite/DrawPass.cpp
@@ -419,6 +419,7 @@
     SkASSERT(recorder->priv().caps()->isTexturable(target->textureInfo()));
     SkIRect dstSrcRect = SkIRect::MakePtSize(targetOffset, targetInfo.dimensions());
     sk_sp<TextureProxy> copy = TextureProxy::Make(recorder->priv().caps(),
+                                                  recorder->priv().resourceProvider(),
                                                   targetInfo.dimensions(),
                                                   target->textureInfo(),
                                                   skgpu::Budgeted::kYes);
diff --git a/src/gpu/graphite/Image_Graphite.cpp b/src/gpu/graphite/Image_Graphite.cpp
index 31de893..44064f1 100644
--- a/src/gpu/graphite/Image_Graphite.cpp
+++ b/src/gpu/graphite/Image_Graphite.cpp
@@ -86,6 +86,7 @@
 
     sk_sp<TextureProxy> dst = TextureProxy::Make(
             recorder->priv().caps(),
+            recorder->priv().resourceProvider(),
             backingFit == SkBackingFit::kApprox ? GetApproxSize(subset.size()) : subset.size(),
             textureInfo,
             budgeted);
diff --git a/src/gpu/graphite/Surface_Graphite.cpp b/src/gpu/graphite/Surface_Graphite.cpp
index 46bddbf..6c5c052 100644
--- a/src/gpu/graphite/Surface_Graphite.cpp
+++ b/src/gpu/graphite/Surface_Graphite.cpp
@@ -135,6 +135,9 @@
     if (!device) {
         return nullptr;
     }
+    // TODO: This instantiation isn't necessary anymore when budgeted == kNo; there are some callers
+    // that pass in kYes that still rely on this instantiation for Surface objects and need to have
+    // their logic updated to work correctly with truly scratch textures.
     if (!device->target()->instantiate(recorder->priv().resourceProvider())) {
         return nullptr;
     }
diff --git a/src/gpu/graphite/TextureProxy.cpp b/src/gpu/graphite/TextureProxy.cpp
index 30955c7..c448201 100644
--- a/src/gpu/graphite/TextureProxy.cpp
+++ b/src/gpu/graphite/TextureProxy.cpp
@@ -7,8 +7,10 @@
 
 #include "src/gpu/graphite/TextureProxy.h"
 
+#include "include/gpu/graphite/Recorder.h"
 #include "src/core/SkMipmap.h"
 #include "src/gpu/graphite/Caps.h"
+#include "src/gpu/graphite/RecorderPriv.h"
 #include "src/gpu/graphite/ResourceProvider.h"
 #include "src/gpu/graphite/Texture.h"
 #include "src/gpu/graphite/TextureUtils.h"
@@ -129,6 +131,7 @@
 }
 
 sk_sp<TextureProxy> TextureProxy::Make(const Caps* caps,
+                                       ResourceProvider* resourceProvider,
                                        SkISize dimensions,
                                        const TextureInfo& textureInfo,
                                        skgpu::Budgeted budgeted) {
@@ -139,22 +142,15 @@
         return nullptr;
     }
 
-    return sk_sp<TextureProxy>(new TextureProxy(dimensions, textureInfo, budgeted));
-}
-
-sk_sp<TextureProxy> TextureProxy::Make(const Caps* caps,
-                                       SkISize dimensions,
-                                       SkColorType colorType,
-                                       Mipmapped mipmapped,
-                                       Protected isProtected,
-                                       Renderable renderable,
-                                       skgpu::Budgeted budgeted) {
-    TextureInfo textureInfo = caps->getDefaultSampledTextureInfo(colorType,
-                                                                 mipmapped,
-                                                                 isProtected,
-                                                                 renderable);
-
-    return Make(caps, dimensions, textureInfo, budgeted);
+    sk_sp<TextureProxy> proxy{new TextureProxy(dimensions, textureInfo, budgeted)};
+    if (budgeted == Budgeted::kNo) {
+        // Instantiate immediately to avoid races later on if the client starts to use the wrapping
+        // object on multiple threads.
+        if (!proxy->instantiate(resourceProvider)) {
+            return nullptr;
+        }
+    }
+    return proxy;
 }
 
 sk_sp<TextureProxy> TextureProxy::MakeLazy(const Caps* caps,
@@ -184,15 +180,6 @@
             SkISize::Make(-1, -1), textureInfo, budgeted, isVolatile, std::move(callback)));
 }
 
-sk_sp<TextureProxy> TextureProxy::MakeStorage(const Caps* caps,
-                                              SkISize dimensions,
-                                              SkColorType colorType,
-                                              skgpu::Budgeted budgeted) {
-    TextureInfo textureInfo = caps->getDefaultStorageTextureInfo(colorType);
-
-    return Make(caps, dimensions, textureInfo, budgeted);
-}
-
 sk_sp<TextureProxy> TextureProxy::Wrap(sk_sp<Texture> texture) {
     return sk_sp<TextureProxy>(new TextureProxy(std::move(texture)));
 }
diff --git a/src/gpu/graphite/TextureProxy.h b/src/gpu/graphite/TextureProxy.h
index b5eee38..01a0c3b 100644
--- a/src/gpu/graphite/TextureProxy.h
+++ b/src/gpu/graphite/TextureProxy.h
@@ -20,6 +20,7 @@
 namespace skgpu::graphite {
 
 class Caps;
+class Recorder;
 class ResourceProvider;
 class Texture;
 
@@ -64,14 +65,9 @@
     const Texture* texture() const;
     Texture* texture() { return fTexture.get(); }
 
+    // Make() will immediately instantiate non-budgeted proxies.
     static sk_sp<TextureProxy> Make(const Caps*,
-                                    SkISize dimensions,
-                                    SkColorType,
-                                    Mipmapped,
-                                    Protected,
-                                    Renderable,
-                                    skgpu::Budgeted);
-    static sk_sp<TextureProxy> Make(const Caps*,
+                                    ResourceProvider*,
                                     SkISize dimensions,
                                     const TextureInfo&,
                                     skgpu::Budgeted);
@@ -89,11 +85,6 @@
                                              Volatile,
                                              LazyInstantiateCallback&&);
 
-    static sk_sp<TextureProxy> MakeStorage(const Caps*,
-                                           SkISize dimensions,
-                                           SkColorType,
-                                           skgpu::Budgeted);
-
     static sk_sp<TextureProxy> Wrap(sk_sp<Texture>);
 
 private:
diff --git a/src/gpu/graphite/TextureUtils.cpp b/src/gpu/graphite/TextureUtils.cpp
index 03b29e0..efc06b1 100644
--- a/src/gpu/graphite/TextureUtils.cpp
+++ b/src/gpu/graphite/TextureUtils.cpp
@@ -445,8 +445,8 @@
     }
 
     // Create proxy
-    sk_sp<TextureProxy> proxy =
-            TextureProxy::Make(caps, bmpToUpload.dimensions(), textureInfo, budgeted);
+    sk_sp<TextureProxy> proxy = TextureProxy::Make(caps, recorder->priv().resourceProvider(),
+                                                   bmpToUpload.dimensions(), textureInfo, budgeted);
     if (!proxy) {
         return {};
     }
diff --git a/src/gpu/graphite/compute/DispatchGroup.cpp b/src/gpu/graphite/compute/DispatchGroup.cpp
index 4de55d5..8e81fa8 100644
--- a/src/gpu/graphite/compute/DispatchGroup.cpp
+++ b/src/gpu/graphite/compute/DispatchGroup.cpp
@@ -362,8 +362,10 @@
             SkASSERT(!size.isEmpty());
             SkASSERT(colorType != kUnknown_SkColorType);
 
-            sk_sp<TextureProxy> texture = TextureProxy::MakeStorage(
-                    fRecorder->priv().caps(), size, colorType, skgpu::Budgeted::kYes);
+            auto textureInfo = fRecorder->priv().caps()->getDefaultStorageTextureInfo(colorType);
+            sk_sp<TextureProxy> texture = TextureProxy::Make(
+                    fRecorder->priv().caps(), fRecorder->priv().resourceProvider(),
+                    size, textureInfo, skgpu::Budgeted::kYes);
             if (texture) {
                 fObj->fTextures.push_back(std::move(texture));
                 result = TextureIndex{fObj->fTextures.size() - 1u};
diff --git a/tests/graphite/ComputeTest.cpp b/tests/graphite/ComputeTest.cpp
index 407d064..bf0430f 100644
--- a/tests/graphite/ComputeTest.cpp
+++ b/tests/graphite/ComputeTest.cpp
@@ -937,12 +937,14 @@
         }
     }
 
+    auto texInfo = context->priv().caps()->getDefaultSampledTextureInfo(kRGBA_8888_SkColorType,
+                                                                        skgpu::Mipmapped::kNo,
+                                                                        skgpu::Protected::kNo,
+                                                                        skgpu::Renderable::kNo);
     sk_sp<TextureProxy> srcProxy = TextureProxy::Make(context->priv().caps(),
+                                                      recorder->priv().resourceProvider(),
                                                       {kDim, kDim},
-                                                      kRGBA_8888_SkColorType,
-                                                      skgpu::Mipmapped::kNo,
-                                                      skgpu::Protected::kNo,
-                                                      skgpu::Renderable::kNo,
+                                                      texInfo,
                                                       skgpu::Budgeted::kNo);
     MipLevel mipLevel;
     mipLevel.fPixels = srcPixels.addr();
diff --git a/tests/graphite/DrawPassTest.cpp b/tests/graphite/DrawPassTest.cpp
index b07ade5..7c18a53 100644
--- a/tests/graphite/DrawPassTest.cpp
+++ b/tests/graphite/DrawPassTest.cpp
@@ -46,6 +46,7 @@
     const SkImageInfo targetInfo = SkImageInfo::Make(targetSize, targetColorType, targetAlphaType);
     sk_sp<TextureProxy> target = TextureProxy::Make(
             caps,
+            recorder->priv().resourceProvider(),
             targetSize,
             caps->getDefaultSampledTextureInfo(
                     targetColorType, Mipmapped::kNo, Protected::kNo, Renderable::kYes),
diff --git a/tests/graphite/PaintParamsKeyTest.cpp b/tests/graphite/PaintParamsKeyTest.cpp
index 3b8d269..e4fd0ce 100644
--- a/tests/graphite/PaintParamsKeyTest.cpp
+++ b/tests/graphite/PaintParamsKeyTest.cpp
@@ -1327,13 +1327,18 @@
 
     std::unique_ptr<RuntimeEffectDictionary> rtDict = std::make_unique<RuntimeEffectDictionary>();
 
+    auto dstTexInfo = context->priv().caps()->getDefaultSampledTextureInfo(
+            kRGBA_8888_SkColorType,
+            skgpu::Mipmapped::kNo,
+            skgpu::Protected::kNo,
+            skgpu::Renderable::kNo);
+    // Use Budgeted::kYes to avoid instantiating the proxy immediately; this test doesn't need
+    // a full resource.
     sk_sp<TextureProxy> fakeDstTexture = TextureProxy::Make(context->priv().caps(),
+                                                            context->priv().resourceProvider(),
                                                             SkISize::Make(1, 1),
-                                                            kRGBA_8888_SkColorType,
-                                                            skgpu::Mipmapped::kNo,
-                                                            skgpu::Protected::kNo,
-                                                            skgpu::Renderable::kYes,
-                                                            skgpu::Budgeted::kNo);
+                                                            dstTexInfo,
+                                                            skgpu::Budgeted::kYes);
     constexpr SkIPoint kFakeDstOffset = SkIPoint::Make(0, 0);
 
     KeyContext precompileKeyContext(context->priv().caps(),
diff --git a/tests/graphite/TextureProxyTest.cpp b/tests/graphite/TextureProxyTest.cpp
index 010f30a..9d3a915 100644
--- a/tests/graphite/TextureProxyTest.cpp
+++ b/tests/graphite/TextureProxyTest.cpp
@@ -41,6 +41,14 @@
     BackendTexture backendTexture = recorder->createBackendTexture(kValidSize, textureInfo);
     sk_sp<Texture> texture = resourceProvider->createWrappedTexture(backendTexture);
 
+    auto makeProxy = [&](SkISize dimensions, SkColorType colorType, Mipmapped mipmapped,
+                         Protected isProtected, Renderable renderable, Budgeted budgeted) {
+        auto textureInfo = caps->getDefaultSampledTextureInfo(colorType, mipmapped,
+                                                              isProtected, renderable);
+        return TextureProxy::Make(caps, recorder->priv().resourceProvider(),
+                                  dimensions, textureInfo, budgeted);
+    };
+
     auto nullCallback = [](ResourceProvider*) -> sk_sp<Texture> { return nullptr; };
     auto callback = [texture](ResourceProvider*) -> sk_sp<Texture> { return texture; };
 
@@ -52,31 +60,41 @@
 
     // Invalid parameters.
     sk_sp<TextureProxy> textureProxy;
-    textureProxy = TextureProxy::Make(caps,
-                                      kInvalidSize,
-                                      kValidColorType,
-                                      Mipmapped::kNo,
-                                      isProtected,
-                                      Renderable::kNo,
-                                      skgpu::Budgeted::kNo);
+    textureProxy = makeProxy(kInvalidSize,
+                             kValidColorType,
+                             Mipmapped::kNo,
+                             isProtected,
+                             Renderable::kNo,
+                             skgpu::Budgeted::kNo);
     REPORTER_ASSERT(reporter, textureProxy == nullptr);
-    textureProxy = TextureProxy::Make(caps,
-                                      kValidSize,
-                                      kInvalidColorType,
-                                      Mipmapped::kNo,
-                                      isProtected,
-                                      Renderable::kNo,
-                                      skgpu::Budgeted::kNo);
+    textureProxy = makeProxy(kValidSize,
+                             kInvalidColorType,
+                             Mipmapped::kNo,
+                             isProtected,
+                             Renderable::kNo,
+                             skgpu::Budgeted::kNo);
     REPORTER_ASSERT(reporter, textureProxy == nullptr);
 
-    // Non-lazy TextureProxy, successful instantiation.
-    textureProxy = TextureProxy::Make(caps,
-                                      kValidSize,
-                                      kValidColorType,
-                                      Mipmapped::kNo,
-                                      isProtected,
-                                      Renderable::kNo,
-                                      skgpu::Budgeted::kNo);
+    // Non-budgeted, non-lazy TextureProxy is instantiated on return
+    textureProxy = makeProxy(kValidSize,
+                             kValidColorType,
+                             Mipmapped::kNo,
+                             isProtected,
+                             Renderable::kNo,
+                             skgpu::Budgeted::kNo);
+    REPORTER_ASSERT(reporter, !textureProxy->isLazy());
+    REPORTER_ASSERT(reporter, !textureProxy->isFullyLazy());
+    REPORTER_ASSERT(reporter, !textureProxy->isVolatile());
+    REPORTER_ASSERT(reporter, textureProxy->isInstantiated());
+    REPORTER_ASSERT(reporter, textureProxy->dimensions() == kValidSize);
+
+    // Budgeted, non-lazy TextureProxy, successful instantiation later on
+    textureProxy = makeProxy(kValidSize,
+                             kValidColorType,
+                             Mipmapped::kNo,
+                             isProtected,
+                             Renderable::kNo,
+                             skgpu::Budgeted::kYes);
     REPORTER_ASSERT(reporter, !textureProxy->isLazy());
     REPORTER_ASSERT(reporter, !textureProxy->isFullyLazy());
     REPORTER_ASSERT(reporter, !textureProxy->isVolatile());
@@ -161,13 +179,12 @@
     REPORTER_ASSERT(reporter, textureProxy->dimensions() == kLargerSize);
 
     // InstantiateIfNotLazy tests.
-    textureProxy = TextureProxy::Make(caps,
-                                      kValidSize,
-                                      kValidColorType,
-                                      Mipmapped::kNo,
-                                      isProtected,
-                                      Renderable::kNo,
-                                      skgpu::Budgeted::kNo);
+    textureProxy = makeProxy(kValidSize,
+                             kValidColorType,
+                             Mipmapped::kNo,
+                             isProtected,
+                             Renderable::kNo,
+                             skgpu::Budgeted::kYes);
     instantiateSuccess = TextureProxy::InstantiateIfNotLazy(resourceProvider, textureProxy.get());
     REPORTER_ASSERT(reporter, instantiateSuccess);