Move proxyProvider and singleOwner to GrImageContext

This starts to beef up the capabilities of the GrImageContext in preparation for its future responsibilities (i.e., creating promise images w/o a recordingContext).

Note that the proxyProvider still has different behavior if it has a full context vs. a reduced context. I intend to just let this behavior remain as is.

Change-Id: Idb9d99a548ef928fc1b9dc1e5a34f74343bb0b4b
Reviewed-on: https://skia-review.googlesource.com/c/189490
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrProxyProvider.cpp b/src/gpu/GrProxyProvider.cpp
index 5afd87d..a108569 100644
--- a/src/gpu/GrProxyProvider.cpp
+++ b/src/gpu/GrProxyProvider.cpp
@@ -8,6 +8,10 @@
 #include "GrProxyProvider.h"
 
 #include "GrCaps.h"
+#include "GrContext.h"
+#include "GrContextPriv.h"
+#include "GrImageContext.h"
+#include "GrImageContextPriv.h"
 #include "GrRenderTarget.h"
 #include "GrResourceKey.h"
 #include "GrResourceProvider.h"
@@ -28,46 +32,15 @@
 #include "SkTraceEvent.h"
 
 #define ASSERT_SINGLE_OWNER \
-    SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
+    SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());)
 
-GrProxyProvider::GrProxyProvider(GrResourceProvider* resourceProvider,
-                                 GrResourceCache* resourceCache,
-                                 sk_sp<const GrCaps> caps,
-                                 GrSingleOwner* owner)
-        : fResourceProvider(resourceProvider)
-        , fResourceCache(resourceCache)
-        , fAbandoned(false)
-        , fCaps(caps)
-        , fContextUniqueID(resourceCache->contextUniqueID())
-#ifdef SK_DEBUG
-        , fSingleOwner(owner)
-#endif
-{
-    SkASSERT(fResourceProvider);
-    SkASSERT(fResourceCache);
-    SkASSERT(fCaps);
-    SkASSERT(fSingleOwner);
-}
-
-GrProxyProvider::GrProxyProvider(uint32_t contextUniqueID,
-                                 sk_sp<const GrCaps> caps,
-                                 GrSingleOwner* owner)
-        : fResourceProvider(nullptr)
-        , fResourceCache(nullptr)
-        , fAbandoned(false)
-        , fCaps(caps)
-        , fContextUniqueID(contextUniqueID)
-#ifdef SK_DEBUG
-        , fSingleOwner(owner)
-#endif
-{
-    SkASSERT(fContextUniqueID != SK_InvalidUniqueID);
-    SkASSERT(fCaps);
-    SkASSERT(fSingleOwner);
+GrProxyProvider::GrProxyProvider(GrImageContext* imageContext)
+        : fImageContext(imageContext)
+        , fAbandoned(false) {
 }
 
 GrProxyProvider::~GrProxyProvider() {
-    if (fResourceCache) {
+    if (this->renderingDirectly()) {
         // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
         // they need their unique keys to, potentially, find a cached resource when the
         // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
@@ -82,10 +55,18 @@
         return false;
     }
 
-    // If there is already a GrResource with this key then the caller has violated the normal
-    // usage pattern of uniquely keyed resources (e.g., they have created one w/o first seeing
-    // if it already existed in the cache).
-    SkASSERT(!fResourceCache || !fResourceCache->findAndRefUniqueResource(key));
+#ifdef SK_DEBUG
+    {
+        GrContext* direct = fImageContext->priv().asDirectContext();
+        if (direct) {
+            GrResourceCache* resourceCache = direct->priv().getResourceCache();
+            // If there is already a GrResource with this key then the caller has violated the
+            // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
+            // first seeing if it already existed in the cache).
+            SkASSERT(!resourceCache->findAndRefUniqueResource(key));
+        }
+    }
+#endif
 
     SkASSERT(!fUniquelyKeyedProxies.find(key));     // multiple proxies can't get the same key
 
@@ -131,6 +112,37 @@
     return result;
 }
 
+///////////////////////////////////////////////////////////////////////////////
+
+#if GR_TEST_UTILS
+sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
+        const GrSurfaceDesc& desc, GrSurfaceOrigin origin, SkBackingFit fit, SkBudgeted budgeted) {
+    GrContext* direct = fImageContext->priv().asDirectContext();
+    if (!direct) {
+        return nullptr;
+    }
+
+    GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
+    sk_sp<GrTexture> tex;
+
+    if (SkBackingFit::kApprox == fit) {
+        tex = resourceProvider->createApproxTexture(desc, GrResourceProvider::Flags::kNone);
+    } else {
+        tex = resourceProvider->createTexture(desc, budgeted, GrResourceProvider::Flags::kNone);
+    }
+    if (!tex) {
+        return nullptr;
+    }
+
+    return this->createWrapped(std::move(tex), origin);
+}
+
+sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
+                                                                 GrSurfaceOrigin origin) {
+    return this->createWrapped(std::move(tex), origin);
+}
+#endif
+
 sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
 #ifdef SK_DEBUG
     if (tex->getUniqueKey().isValid()) {
@@ -158,11 +170,14 @@
         return result;
     }
 
-    if (!fResourceCache) {
+    GrContext* direct = fImageContext->priv().asDirectContext();
+    if (!direct) {
         return nullptr;
     }
 
-    GrGpuResource* resource = fResourceCache->findAndRefUniqueResource(key);
+    GrResourceCache* resourceCache = direct->priv().getResourceCache();
+
+    GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
     if (!resource) {
         return nullptr;
     }
@@ -197,7 +212,7 @@
         return nullptr;
     }
 
-    GrBackendFormat format = fCaps->getBackendFormatFromColorType(info.colorType());
+    GrBackendFormat format = this->caps()->getBackendFormatFromColorType(info.colorType());
     if (!format.isValid()) {
         return nullptr;
     }
@@ -221,7 +236,7 @@
     }
 
     if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) {
-        if (fCaps->usesMixedSamples() && sampleCnt > 1) {
+        if (this->caps()->usesMixedSamples() && sampleCnt > 1) {
             surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled;
         }
     }
@@ -257,10 +272,13 @@
         return nullptr;
     }
 
-    if (fResourceProvider) {
+    GrContext* direct = fImageContext->priv().asDirectContext();
+    if (direct) {
+        GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
+
         // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
         // we're better off instantiating the proxy immediately here.
-        if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
+        if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
             return nullptr;
         }
     }
@@ -300,8 +318,8 @@
     // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
     // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
     // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
-    SkCopyPixelsMode copyMode = this->recordingDDL() ? kIfMutable_SkCopyPixelsMode
-                                                     : kNever_SkCopyPixelsMode;
+    SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
+                                                          : kIfMutable_SkCopyPixelsMode;
     sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
     if (!baseLevel) {
         return nullptr;
@@ -313,7 +331,8 @@
                                         SkBackingFit::kExact);
     }
 
-    const GrBackendFormat format = fCaps->getBackendFormatFromColorType(bitmap.info().colorType());
+    const GrBackendFormat format =
+                            this->caps()->getBackendFormatFromColorType(bitmap.info().colorType());
     if (!format.isValid()) {
         return nullptr;
     }
@@ -372,10 +391,12 @@
         return nullptr;
     }
 
-    if (fResourceProvider) {
+    GrContext* direct = fImageContext->priv().asDirectContext();
+    if (direct) {
+        GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
         // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
         // we're better off instantiating the proxy immediately here.
-        if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
+        if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
             return nullptr;
         }
     }
@@ -424,7 +445,8 @@
     }
 
     const GrColorType ct = GrPixelConfigToColorType(desc.fConfig);
-    const GrBackendFormat format = fCaps->getBackendFormatFromGrColorType(ct, GrSRGBEncoded::kNo);
+    const GrBackendFormat format =
+                            this->caps()->getBackendFormatFromGrColorType(ct, GrSRGBEncoded::kNo);
 
     sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
         [desc, data](GrResourceProvider* resourceProvider) {
@@ -444,10 +466,12 @@
         return nullptr;
     }
 
-    if (fResourceProvider) {
+    GrContext* direct = fImageContext->priv().asDirectContext();
+    if (direct) {
+        GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
         // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
         // we're better off instantiating the proxy immediately here.
-        if (!proxy->priv().doLazyInstantiation(fResourceProvider)) {
+        if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
             return nullptr;
         }
     }
@@ -467,12 +491,15 @@
     }
 
     // This is only supported on a direct GrContext.
-    if (!fResourceProvider) {
+    GrContext* direct = fImageContext->priv().asDirectContext();
+    if (!direct) {
         return nullptr;
     }
 
+    GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
+
     sk_sp<GrTexture> tex =
-            fResourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
+            resourceProvider->wrapBackendTexture(backendTex, ownership, cacheable, ioType);
     if (!tex) {
         return nullptr;
     }
@@ -500,17 +527,20 @@
     }
 
     // This is only supported on a direct GrContext.
-    if (!fResourceProvider) {
+    GrContext* direct = fImageContext->priv().asDirectContext();
+    if (!direct) {
         return nullptr;
     }
 
+    GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
+
     sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
     if (!sampleCnt) {
         return nullptr;
     }
 
-    sk_sp<GrTexture> tex = fResourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
-                                                                           ownership, cacheable);
+    sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
+                                                                          ownership, cacheable);
     if (!tex) {
         return nullptr;
     }
@@ -537,11 +567,14 @@
     }
 
     // This is only supported on a direct GrContext.
-    if (!fResourceProvider) {
+    GrContext* direct = fImageContext->priv().asDirectContext();
+    if (!direct) {
         return nullptr;
     }
 
-    sk_sp<GrRenderTarget> rt = fResourceProvider->wrapBackendRenderTarget(backendRT);
+    GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
+
+    sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT);
     if (!rt) {
         return nullptr;
     }
@@ -568,12 +601,15 @@
     }
 
     // This is only supported on a direct GrContext.
-    if (!fResourceProvider) {
+    GrContext* direct = fImageContext->priv().asDirectContext();
+    if (!direct) {
         return nullptr;
     }
 
+    GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
+
     sk_sp<GrRenderTarget> rt =
-            fResourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
+            resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt);
     if (!rt) {
         return nullptr;
     }
@@ -592,16 +628,19 @@
     }
 
     // This is only supported on a direct GrContext.
-    if (!fResourceProvider) {
+    GrContext* direct = fImageContext->priv().asDirectContext();
+    if (!direct) {
         return nullptr;
     }
 
-    sk_sp<GrRenderTarget> rt = fResourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
-                                                                                      vkInfo);
+    GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
 
+    sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
+                                                                                     vkInfo);
     if (!rt) {
         return nullptr;
     }
+
     SkASSERT(!rt->asTexture());  // A GrRenderTarget that's not textureable
     SkASSERT(!rt->getUniqueKey().isValid());
     // This proxy should be unbudgeted because we're just wrapping an external resource
@@ -634,8 +673,8 @@
                                                        SkBackingFit fit,
                                                        SkBudgeted budgeted) {
     // For non-ddl draws always make lazy proxy's single use.
-    LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse
-                                                       : LazyInstantiationType::kMultipleUse;
+    LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
+                                                               : LazyInstantiationType::kMultipleUse;
     return this->createLazyProxy(std::move(callback), format, desc, origin, mipMapped, surfaceFlags,
                                  fit, budgeted, lazyType);
 }
@@ -652,7 +691,8 @@
     SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
              (desc.fWidth > 0 && desc.fHeight > 0));
 
-    if (desc.fWidth > fCaps->maxTextureSize() || desc.fHeight > fCaps->maxTextureSize()) {
+    if (desc.fWidth > this->caps()->maxTextureSize() ||
+        desc.fHeight > this->caps()->maxTextureSize()) {
         return nullptr;
     }
 
@@ -660,7 +700,7 @@
 #ifdef SK_DEBUG
     if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) {
         if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
-            SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1);
+            SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
         }
     }
 #endif
@@ -680,7 +720,8 @@
     SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
              (desc.fWidth > 0 && desc.fHeight > 0));
 
-    if (desc.fWidth > fCaps->maxRenderTargetSize() || desc.fHeight > fCaps->maxRenderTargetSize()) {
+    if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
+        desc.fHeight > this->caps()->maxRenderTargetSize()) {
         return nullptr;
     }
 
@@ -688,14 +729,14 @@
 
 #ifdef SK_DEBUG
     if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) {
-        SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1);
+        SkASSERT(this->caps()->usesMixedSamples() && desc.fSampleCnt > 1);
     }
 #endif
 
     using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
     // For non-ddl draws always make lazy proxy's single use.
-    LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse
-                                                       : LazyInstantiationType::kMultipleUse;
+    LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
+                                                               : LazyInstantiationType::kMultipleUse;
 
     if (textureInfo) {
         return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
@@ -762,8 +803,12 @@
         if (proxy && proxy->isInstantiated()) {
             invalidGpuResource = sk_ref_sp(proxy->peekSurface());
         }
-        if (!invalidGpuResource && fResourceProvider) {
-            invalidGpuResource = fResourceProvider->findByUniqueKey<GrGpuResource>(key);
+        if (!invalidGpuResource) {
+            GrContext* direct = fImageContext->priv().asDirectContext();
+            if (direct) {
+                GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
+                invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
+            }
         }
         SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
     }
@@ -780,6 +825,18 @@
     }
 }
 
+uint32_t GrProxyProvider::contextID() const {
+    return fImageContext->priv().contextID();
+}
+
+const GrCaps* GrProxyProvider::caps() const {
+    return fImageContext->priv().caps();
+}
+
+sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
+    return fImageContext->priv().refCaps();
+}
+
 void GrProxyProvider::orphanAllUniqueKeys() {
     UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
     for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
@@ -798,3 +855,7 @@
     }
     SkASSERT(!fUniquelyKeyedProxies.count());
 }
+
+bool GrProxyProvider::renderingDirectly() const {
+    return fImageContext->priv().asDirectContext();
+}