Add GrProxyProvider

This pulls all the proxy tracking & creation functionality out of the GrResourceCache and GrResourceProvider and consolidates it in the GrProxyProvider.

Change-Id: I7256f7c544319a70c1bd93dd5a9ccbe5fa0a544f
Reviewed-on: https://skia-review.googlesource.com/91501
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
diff --git a/tests/TextureProxyTest.cpp b/tests/TextureProxyTest.cpp
index 393e8c6..425fab2 100644
--- a/tests/TextureProxyTest.cpp
+++ b/tests/TextureProxyTest.cpp
@@ -14,6 +14,7 @@
 #include "GrBackendSurface.h"
 #include "GrContextPriv.h"
 #include "GrResourceCache.h"
+#include "GrProxyProvider.h"
 #include "GrResourceProvider.h"
 #include "GrTest.h"
 #include "GrTexture.h"
@@ -22,7 +23,7 @@
 #include "SkGr.h"
 #include "SkImage.h"
 
-int GrResourceCache::numUniqueKeyProxies_TestOnly() const {
+int GrProxyProvider::numUniqueKeyProxies_TestOnly() const {
     return fUniquelyKeyedProxies.count();
 }
 
@@ -42,37 +43,41 @@
 // Basic test
 
 static sk_sp<GrTextureProxy> deferred_tex(skiatest::Reporter* reporter,
-                                          GrResourceProvider* provider, SkBackingFit fit) {
+                                          GrContext* context, SkBackingFit fit) {
+    GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
     GrSurfaceDesc desc = make_desc(kNone_GrSurfaceFlags);
 
     // Only budgeted & wrapped external proxies get to carry uniqueKeys
-    sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(provider, desc, fit,
+    sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(proxyProvider, desc, fit,
                                                                SkBudgeted::kYes);
     REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid());
     return proxy;
 }
 
 static sk_sp<GrTextureProxy> deferred_texRT(skiatest::Reporter* reporter,
-                                            GrResourceProvider* provider, SkBackingFit fit) {
+                                            GrContext* context, SkBackingFit fit) {
+    GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
     GrSurfaceDesc desc = make_desc(kRenderTarget_GrSurfaceFlag);
 
     // Only budgeted & wrapped external proxies get to carry uniqueKeys
-    sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(provider, desc, fit,
+    sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(proxyProvider, desc, fit,
                                                                SkBudgeted::kYes);
     REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid());
     return proxy;
 }
 
 static sk_sp<GrTextureProxy> wrapped(skiatest::Reporter* reporter,
-                                     GrResourceProvider* provider, SkBackingFit fit) {
+                                     GrContext* context, SkBackingFit fit) {
+    GrResourceProvider* resourceProvider = context->resourceProvider();
+
     GrSurfaceDesc desc = make_desc(kNone_GrSurfaceFlags);
 
     sk_sp<GrTexture> tex;
     if (SkBackingFit::kApprox == fit) {
-        tex = sk_sp<GrTexture>(provider->createApproxTexture(desc, 0));
+        tex = sk_sp<GrTexture>(resourceProvider->createApproxTexture(desc, 0));
     } else {
         // Only budgeted & wrapped external proxies get to carry uniqueKeys
-        tex = provider->createTexture(desc, SkBudgeted::kYes);
+        tex = resourceProvider->createTexture(desc, SkBudgeted::kYes);
     }
 
     sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(tex),
@@ -82,7 +87,9 @@
 }
 
 static sk_sp<GrTextureProxy> wrapped_with_key(skiatest::Reporter* reporter,
-                                              GrResourceProvider* provider, SkBackingFit fit) {
+                                              GrContext* context, SkBackingFit fit) {
+    GrResourceProvider* resourceProvider = context->resourceProvider();
+
     static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain();
     static int kUniqueKeyData = 0;
 
@@ -96,10 +103,10 @@
 
     sk_sp<GrTexture> tex;
     if (SkBackingFit::kApprox == fit) {
-        tex = sk_sp<GrTexture>(provider->createApproxTexture(desc, 0));
+        tex = sk_sp<GrTexture>(resourceProvider->createApproxTexture(desc, 0));
     } else {
         // Only budgeted & wrapped external proxies get to carry uniqueKeys
-        tex = provider->createTexture(desc, SkBudgeted::kYes);
+        tex = resourceProvider->createTexture(desc, SkBudgeted::kYes);
     }
 
     tex->resourcePriv().setUniqueKey(key);
@@ -134,7 +141,8 @@
                        sk_sp<GrTextureProxy> proxy, bool proxyIsCached) {
     static int id = 1;
 
-    GrResourceProvider* provider = context->resourceProvider();
+    GrResourceProvider* resourceProvider = context->resourceProvider();
+    GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
     GrResourceCache* cache = context->getResourceCache();
 
     int startCacheCount = cache->getResourceCount();
@@ -147,23 +155,23 @@
         ++id;
 
         // Assigning the uniqueKey adds the proxy to the hash but doesn't force instantiation
-        REPORTER_ASSERT(reporter, !cache->numUniqueKeyProxies_TestOnly());
-        provider->assignUniqueKeyToProxy(key, proxy.get());
+        REPORTER_ASSERT(reporter, !proxyProvider->numUniqueKeyProxies_TestOnly());
+        proxyProvider->assignUniqueKeyToProxy(key, proxy.get());
     }
 
-    REPORTER_ASSERT(reporter, 1 == cache->numUniqueKeyProxies_TestOnly());
+    REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly());
     REPORTER_ASSERT(reporter, startCacheCount == cache->getResourceCount());
 
     // setUniqueKey had better stick
     REPORTER_ASSERT(reporter, key == proxy->getUniqueKey());
 
     // We just added it, surely we can find it
-    REPORTER_ASSERT(reporter, provider->findOrCreateProxyByUniqueKey(key,
-                                                                     kBottomLeft_GrSurfaceOrigin));
-    REPORTER_ASSERT(reporter, 1 == cache->numUniqueKeyProxies_TestOnly());
+    REPORTER_ASSERT(reporter, proxyProvider->findOrCreateProxyByUniqueKey(
+                                                                key, kBottomLeft_GrSurfaceOrigin));
+    REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly());
 
     // Once instantiated, the backing resource should have the same key
-    SkAssertResult(proxy->instantiate(provider));
+    SkAssertResult(proxy->instantiate(resourceProvider));
     const GrUniqueKey& texKey = proxy->priv().peekSurface()->getUniqueKey();
     REPORTER_ASSERT(reporter, texKey.isValid());
     REPORTER_ASSERT(reporter, key == texKey);
@@ -173,17 +181,17 @@
 
     // deleting the proxy should delete it from the hash but not the cache
     proxy = nullptr;
-    REPORTER_ASSERT(reporter, 0 == cache->numUniqueKeyProxies_TestOnly());
+    REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
     REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
 
     // If the proxy was cached refinding it should bring it back to life
-    proxy = provider->findOrCreateProxyByUniqueKey(key, kBottomLeft_GrSurfaceOrigin);
+    proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, kBottomLeft_GrSurfaceOrigin);
     if (proxyIsCached) {
         REPORTER_ASSERT(reporter, proxy);
-        REPORTER_ASSERT(reporter, 1 == cache->numUniqueKeyProxies_TestOnly());
+        REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly());
     } else {
         REPORTER_ASSERT(reporter, !proxy);
-        REPORTER_ASSERT(reporter, 0 == cache->numUniqueKeyProxies_TestOnly());
+        REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
     }
     REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
 
@@ -197,7 +205,7 @@
     }
 
     // We can bring neither the texture nor proxy back from perma-death
-    proxy = provider->findOrCreateProxyByUniqueKey(key, kBottomLeft_GrSurfaceOrigin);
+    proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, kBottomLeft_GrSurfaceOrigin);
     REPORTER_ASSERT(reporter, !proxy);
     if (proxyIsCached) {
         REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
@@ -212,6 +220,7 @@
 // Test if invalidating unique ids operates as expected for texture proxies.
 static void invalidation_test(GrContext* context, skiatest::Reporter* reporter) {
 
+    GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
     GrResourceCache* cache = context->getResourceCache();
     REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
 
@@ -224,12 +233,12 @@
         bm.allocPixels(ii);
 
         rasterImg = SkImage::MakeFromBitmap(bm);
-        REPORTER_ASSERT(reporter, 0 == cache->numUniqueKeyProxies_TestOnly());
+        REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
         REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
     }
 
     sk_sp<SkImage> textureImg = rasterImg->makeTextureImage(context, nullptr);
-    REPORTER_ASSERT(reporter, 1 == cache->numUniqueKeyProxies_TestOnly());
+    REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly());
     REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
 
     rasterImg = nullptr;        // this invalidates the uniqueKey
@@ -240,20 +249,21 @@
     context->getResourceCacheLimits(&maxNum, &maxBytes);
     context->setResourceCacheLimits(maxNum-1, maxBytes);
 
-    REPORTER_ASSERT(reporter, 0 == cache->numUniqueKeyProxies_TestOnly());
+    REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
     REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
 
     textureImg = nullptr;
     context->purgeAllUnlockedResources();
 
-    REPORTER_ASSERT(reporter, 0 == cache->numUniqueKeyProxies_TestOnly());
+    REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
     REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
 }
 
 #ifndef SK_DISABLE_DEFERRED_PROXIES
 // Test if invalidating unique ids prior to instantiating operates as expected
 static void invalidation_and_instantiation_test(GrContext* context, skiatest::Reporter* reporter) {
-    GrResourceProvider* provider = context->resourceProvider();
+    GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
+    GrResourceProvider* resourceProvider = context->resourceProvider();
     GrResourceCache* cache = context->getResourceCache();
     REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
 
@@ -264,44 +274,44 @@
     builder.finish();
 
     // Create proxy, assign unique key
-    sk_sp<GrTextureProxy> proxy = deferred_tex(reporter, provider, SkBackingFit::kExact);
-    provider->assignUniqueKeyToProxy(key, proxy.get());
+    sk_sp<GrTextureProxy> proxy = deferred_tex(reporter, context, SkBackingFit::kExact);
+    proxyProvider->assignUniqueKeyToProxy(key, proxy.get());
 
     // Send an invalidation message, which will be sitting in the cache's inbox
     SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(GrUniqueKeyInvalidatedMessage(key));
 
-    REPORTER_ASSERT(reporter, 1 == cache->numUniqueKeyProxies_TestOnly());
+    REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly());
     REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
 
     // Instantiate the proxy. This will trigger the message to be processed, so the resulting
     // texture should *not* have the unique key on it!
-    SkAssertResult(proxy->instantiate(provider));
+    SkAssertResult(proxy->instantiate(resourceProvider));
 
     REPORTER_ASSERT(reporter, !proxy->getUniqueKey().isValid());
     REPORTER_ASSERT(reporter, !proxy->priv().peekTexture()->getUniqueKey().isValid());
-    REPORTER_ASSERT(reporter, 0 == cache->numUniqueKeyProxies_TestOnly());
+    REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
     REPORTER_ASSERT(reporter, 1 == cache->getResourceCount());
 
     proxy = nullptr;
     context->purgeAllUnlockedResources();
 
-    REPORTER_ASSERT(reporter, 0 == cache->numUniqueKeyProxies_TestOnly());
+    REPORTER_ASSERT(reporter, 0 == proxyProvider->numUniqueKeyProxies_TestOnly());
     REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
 }
 #endif
 
 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextureProxyTest, reporter, ctxInfo) {
     GrContext* context = ctxInfo.grContext();
-    GrResourceProvider* provider = context->resourceProvider();
+    GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
     GrResourceCache* cache = context->getResourceCache();
 
-    REPORTER_ASSERT(reporter, !cache->numUniqueKeyProxies_TestOnly());
+    REPORTER_ASSERT(reporter, !proxyProvider->numUniqueKeyProxies_TestOnly());
     REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
 
     for (auto fit : { SkBackingFit::kExact, SkBackingFit::kApprox }) {
         for (auto create : { deferred_tex, deferred_texRT, wrapped, wrapped_with_key }) {
             REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
-            basic_test(context, reporter, create(reporter, provider, fit), true);
+            basic_test(context, reporter, create(reporter, context, fit), true);
         }
 
         REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());