Begin switching GrRecordingContext over to having the GrDrawingManager

The change forces the GrDrawingManager to only being able to access a GrRecordingContext.

Note that, like the ProxyProvider, the drawingManager still behaves differently if it is being used to directly render. In this case, the biggest difference is that the flush methods are disabled when DDL recording.

This pulls as much as possible out of https://skia-review.googlesource.com/c/skia/+/191287 (Move DrawingManager to RecordingContext) while keeping the drawingManager in the GrContext.

Change-Id: I1e5305fe0cb17ee0b243bfb8622f652310fc0507
Reviewed-on: https://skia-review.googlesource.com/c/192881
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrBlurUtils.cpp b/src/gpu/GrBlurUtils.cpp
index 9ff53b6..103fa73 100644
--- a/src/gpu/GrBlurUtils.cpp
+++ b/src/gpu/GrBlurUtils.cpp
@@ -8,6 +8,7 @@
 #include "GrBlurUtils.h"
 
 #include "GrCaps.h"
+#include "GrContextPriv.h"
 #include "GrFixedClip.h"
 #include "GrProxyProvider.h"
 #include "GrRecordingContext.h"
@@ -447,7 +448,7 @@
                                           const SkPaint& paint,
                                           const SkMatrix& viewMatrix,
                                           const GrShape& shape) {
-    if (context->abandoned()) {
+    if (context->priv().abandoned()) {
         return;
     }
 
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 7f7e4d4..1ccbc6b 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -55,8 +55,8 @@
 GrContext::~GrContext() {
     ASSERT_SINGLE_OWNER
 
-    if (fDrawingManager) {
-        fDrawingManager->cleanup();
+    if (this->drawingManager()) {
+        this->drawingManager()->cleanup();
     }
     delete fResourceProvider;
     delete fResourceCache;
@@ -103,7 +103,6 @@
         // Disable the small path renderer bc of the proxies in the atlas. They need to be
         // unified when the opLists are added back to the destination drawing manager.
         prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
-        prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kStencilAndCover;
     }
 
     GrTextContext::Options textContextOptions;
@@ -158,7 +157,7 @@
 
     // Need to cleanup the drawing manager first so all the render targets
     // will be released/forgotten before they too are abandoned.
-    fDrawingManager->cleanup();
+    this->drawingManager()->cleanup();
 
     // abandon first to so destructors
     // don't try to free the resources in the API.
@@ -168,7 +167,6 @@
 
     fGlyphCache->freeAll();
     fTextBlobCache->freeAll();
-
 }
 
 void GrContext::releaseResourcesAndAbandonContext() {
@@ -182,7 +180,7 @@
 
     // Need to cleanup the drawing manager first so all the render targets
     // will be released/forgotten before they too are abandoned.
-    fDrawingManager->cleanup();
+    this->drawingManager()->cleanup();
 
     // Release all resources in the backend 3D API.
     fResourceCache->releaseAll();
@@ -210,7 +208,7 @@
 
     fGlyphCache->freeAll();
 
-    fDrawingManager->freeGpuResources();
+    this->drawingManager()->freeGpuResources();
 
     fResourceCache->purgeAllUnlocked();
 }
@@ -230,7 +228,7 @@
     fResourceCache->purgeAsNeeded();
     fResourceCache->purgeResourcesNotUsedSince(purgeTime);
 
-    if (auto ccpr = fDrawingManager->getCoverageCountingPathRenderer()) {
+    if (auto ccpr = this->drawingManager()->getCoverageCountingPathRenderer()) {
         ccpr->purgeCacheEntriesOlderThan(this->proxyProvider(), purgeTime);
     }
 
@@ -292,7 +290,7 @@
     ASSERT_SINGLE_OWNER
     RETURN_IF_ABANDONED
 
-    fDrawingManager->flush(nullptr);
+    this->drawingManager()->flush(nullptr);
 }
 
 GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
@@ -302,7 +300,7 @@
         return GrSemaphoresSubmitted::kNo;
     }
 
-    return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
+    return this->drawingManager()->flush(nullptr, numSemaphores, signalSemaphores);
 }
 
 ////////////////////////////////////////////////////////////////////////////////
diff --git a/src/gpu/GrContextPriv.cpp b/src/gpu/GrContextPriv.cpp
index 81f7dfc..ea6c7a0 100644
--- a/src/gpu/GrContextPriv.cpp
+++ b/src/gpu/GrContextPriv.cpp
@@ -676,7 +676,7 @@
 
 void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
                                        GrRenderTargetProxy* newDest) {
-    fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest);
+    fContext->drawingManager()->copyOpListsFromDDL(ddl, newDest);
 }
 
 //////////////////////////////////////////////////////////////////////////////
diff --git a/src/gpu/GrContextPriv.h b/src/gpu/GrContextPriv.h
index ddf257f..e902ff7 100644
--- a/src/gpu/GrContextPriv.h
+++ b/src/gpu/GrContextPriv.h
@@ -53,6 +53,8 @@
     GrProxyProvider* proxyProvider() { return fContext->proxyProvider(); }
     const GrProxyProvider* proxyProvider() const { return fContext->proxyProvider(); }
 
+    bool abandoned() const { return fContext->abandoned(); }
+
     /** This is only useful for debug purposes */
     SkDEBUGCODE(GrSingleOwner* singleOwner() const { return fContext->singleOwner(); } )
 
diff --git a/src/gpu/GrDrawingManager.cpp b/src/gpu/GrDrawingManager.cpp
index 9db2864..e3fcdf6 100644
--- a/src/gpu/GrDrawingManager.cpp
+++ b/src/gpu/GrDrawingManager.cpp
@@ -186,7 +186,7 @@
 }
 
 bool GrDrawingManager::wasAbandoned() const {
-    return fContext->abandoned();
+    return fContext->priv().abandoned();
 }
 
 void GrDrawingManager::freeGpuResources() {
@@ -213,12 +213,20 @@
     }
     SkDEBUGCODE(this->validate());
 
-    GrGpu* gpu = fContext->priv().getGpu();
+    auto direct = fContext->priv().asDirectContext();
+    if (!direct) {
+        return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
+    }
+
+    GrGpu* gpu = direct->priv().getGpu();
     if (!gpu) {
         return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
     }
     fFlushing = true;
 
+    auto resourceProvider = direct->priv().resourceProvider();
+    auto resourceCache = direct->priv().getResourceCache();
+
     // Semi-usually the GrOpLists are already closed at this point, but sometimes Ganesh
     // needs to flush mid-draw. In that case, the SkGpuDevice's GrOpLists won't be closed
     // but need to be flushed anyway. Closing such GrOpLists here will mean new
@@ -235,8 +243,7 @@
         fCpuBufferCache = GrBufferAllocPool::CpuBufferCache::Make(maxCachedBuffers);
     }
 
-    GrOpFlushState flushState(gpu, fContext->priv().resourceProvider(), &fTokenTracker,
-                              fCpuBufferCache);
+    GrOpFlushState flushState(gpu, resourceProvider, &fTokenTracker, fCpuBufferCache);
 
     GrOnFlushResourceProvider onFlushProvider(this);
     // TODO: AFAICT the only reason fFlushState is on GrDrawingManager rather than on the
@@ -338,7 +345,7 @@
 
     // Give the cache a chance to purge resources that become purgeable due to flushing.
     if (flushed) {
-        fContext->priv().getResourceCache()->purgeAsNeeded();
+        resourceCache->purgeAsNeeded();
     }
     for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
         onFlushCBObject->postFlush(fTokenTracker.nextTokenToFlush(), fFlushingOpListIDs.begin(),
@@ -364,7 +371,12 @@
     }
 #endif
 
-    GrResourceProvider* resourceProvider = fContext->priv().resourceProvider();
+    auto direct = fContext->priv().asDirectContext();
+    if (!direct) {
+        return false;
+    }
+
+    GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
     bool anyOpListsExecuted = false;
 
     for (int i = startIndex; i < stopIndex; ++i) {
@@ -389,7 +401,7 @@
 
         // TODO: handle this instantiation via lazy surface proxies?
         // Instantiate all deferred proxies (being built on worker threads) so we can upload them
-        opList->instantiateDeferredProxies(fContext->priv().resourceProvider());
+        opList->instantiateDeferredProxies(resourceProvider);
         opList->prepare(flushState);
     }
 
@@ -455,17 +467,24 @@
     SkDEBUGCODE(this->validate());
     SkASSERT(proxy);
 
-    GrGpu* gpu = fContext->priv().getGpu();
+    auto direct = fContext->priv().asDirectContext();
+    if (!direct) {
+        return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
+    }
+
+    GrGpu* gpu = direct->priv().getGpu();
     if (!gpu) {
         return GrSemaphoresSubmitted::kNo; // Can't flush while DDL recording
     }
 
+    auto resourceProvider = direct->priv().resourceProvider();
+
     GrSemaphoresSubmitted result = GrSemaphoresSubmitted::kNo;
     if (proxy->priv().hasPendingIO() || numSemaphores) {
         result = this->flush(proxy, numSemaphores, backendSemaphores);
     }
 
-    if (!proxy->instantiate(fContext->priv().resourceProvider())) {
+    if (!proxy->instantiate(resourceProvider)) {
         return result;
     }
 
@@ -590,7 +609,13 @@
         fActiveOpList = nullptr;
     }
 
-    auto resourceProvider = fContext->priv().resourceProvider();
+    // MDB TODO: this is unfortunate. GrOpList only needs the resourceProvider here so that, when
+    // not explicitly allocating resources, it can immediately instantiate 'rtp' so that the use
+    // order matches the allocation order (see the comment in GrOpList's ctor).
+    GrResourceProvider* resourceProvider = nullptr;
+    if (fContext->priv().asDirectContext()) {
+        resourceProvider = fContext->priv().asDirectContext()->priv().resourceProvider();
+    }
 
     sk_sp<GrRenderTargetOpList> opList(new GrRenderTargetOpList(
                                                         resourceProvider,
@@ -633,7 +658,15 @@
         fActiveOpList = nullptr;
     }
 
-    sk_sp<GrTextureOpList> opList(new GrTextureOpList(fContext->priv().resourceProvider(),
+    // MDB TODO: this is unfortunate. GrOpList only needs the resourceProvider here so that, when
+    // not explicitly allocating resources, it can immediately instantiate 'texureProxy' so that
+    // the use order matches the allocation order (see the comment in GrOpList's ctor).
+    GrResourceProvider* resourceProvider = nullptr;
+    if (fContext->priv().asDirectContext()) {
+        resourceProvider = fContext->priv().asDirectContext()->priv().resourceProvider();
+    }
+
+    sk_sp<GrTextureOpList> opList(new GrTextureOpList(resourceProvider,
                                                       fContext->priv().refOpMemoryPool(),
                                                       textureProxy,
                                                       fContext->priv().auditTrail()));
@@ -700,7 +733,12 @@
 }
 
 void GrDrawingManager::flushIfNecessary() {
-    GrResourceCache* resourceCache = fContext->priv().getResourceCache();
+    auto direct = fContext->priv().asDirectContext();
+    if (!direct) {
+        return;
+    }
+
+    auto resourceCache = direct->priv().getResourceCache();
     if (resourceCache && resourceCache->requestsFlush()) {
         this->flush(nullptr, 0, nullptr);
         resourceCache->purgeAsNeeded();
diff --git a/src/gpu/GrOnFlushResourceProvider.cpp b/src/gpu/GrOnFlushResourceProvider.cpp
index 285e8e2..13fd60f 100644
--- a/src/gpu/GrOnFlushResourceProvider.cpp
+++ b/src/gpu/GrOnFlushResourceProvider.cpp
@@ -62,7 +62,13 @@
 }
 
 bool GrOnFlushResourceProvider::instatiateProxy(GrSurfaceProxy* proxy) {
-    auto resourceProvider = fDrawingMgr->getContext()->priv().resourceProvider();
+    // TODO: this class should probably just get a GrDirectContext
+    auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
+    if (!direct) {
+        return false;
+    }
+
+    auto resourceProvider = direct->priv().resourceProvider();
 
     if (GrSurfaceProxy::LazyState::kNot != proxy->lazyInstantiationState()) {
         // DDL TODO: Decide if we ever plan to have these proxies use the GrDeinstantiateTracker
@@ -75,14 +81,28 @@
 
 sk_sp<GrGpuBuffer> GrOnFlushResourceProvider::makeBuffer(GrGpuBufferType intendedType, size_t size,
                                                          const void* data) {
-    auto resourceProvider = fDrawingMgr->getContext()->priv().resourceProvider();
+    // TODO: this class should probably just get a GrDirectContext
+    auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
+    if (!direct) {
+        return nullptr;
+    }
+
+    auto resourceProvider = direct->priv().resourceProvider();
+
     return sk_sp<GrGpuBuffer>(
             resourceProvider->createBuffer(size, intendedType, kDynamic_GrAccessPattern, data));
 }
 
 sk_sp<const GrGpuBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(
         GrGpuBufferType intendedType, size_t size, const void* data, const GrUniqueKey& key) {
-    auto resourceProvider = fDrawingMgr->getContext()->priv().resourceProvider();
+    // TODO: class should probably just get a GrDirectContext
+    auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
+    if (!direct) {
+        return nullptr;
+    }
+
+    auto resourceProvider = direct->priv().resourceProvider();
+
     sk_sp<const GrGpuBuffer> buffer =
             resourceProvider->findOrMakeStaticBuffer(intendedType, size, data, key);
     // Static buffers should never have pending IO.
diff --git a/src/gpu/GrPathRendererChain.cpp b/src/gpu/GrPathRendererChain.cpp
index 64eb986..ca2a7f0 100644
--- a/src/gpu/GrPathRendererChain.cpp
+++ b/src/gpu/GrPathRendererChain.cpp
@@ -28,10 +28,15 @@
         fChain.push_back(sk_make_sp<GrDashLinePathRenderer>());
     }
     if (options.fGpuPathRenderers & GpuPathRenderers::kStencilAndCover) {
-        sk_sp<GrPathRenderer> pr(
-           GrStencilAndCoverPathRenderer::Create(context->priv().resourceProvider(), caps));
-        if (pr) {
-            fChain.push_back(std::move(pr));
+        auto direct = context->priv().asDirectContext();
+        if (direct) {
+            auto resourceProvider = direct->priv().resourceProvider();
+
+            sk_sp<GrPathRenderer> pr(
+               GrStencilAndCoverPathRenderer::Create(resourceProvider, caps));
+            if (pr) {
+                fChain.push_back(std::move(pr));
+            }
         }
     }
     if (options.fGpuPathRenderers & GpuPathRenderers::kAAConvex) {
diff --git a/src/gpu/GrRenderTargetContext.cpp b/src/gpu/GrRenderTargetContext.cpp
index 700e808..4a294e0 100644
--- a/src/gpu/GrRenderTargetContext.cpp
+++ b/src/gpu/GrRenderTargetContext.cpp
@@ -1183,7 +1183,7 @@
                                            const SkPath& path,
                                            const SkDrawShadowRec& rec) {
     ASSERT_SINGLE_OWNER
-    if (fContext->abandoned()) {
+    if (fContext->priv().abandoned()) {
         return true;
     }
     SkDEBUGCODE(this->validate();)
@@ -1653,7 +1653,7 @@
 GrSemaphoresSubmitted GrRenderTargetContext::prepareForExternalIO(
         int numSemaphores, GrBackendSemaphore backendSemaphores[]) {
     ASSERT_SINGLE_OWNER
-    if (fContext->abandoned()) {
+    if (fContext->priv().abandoned()) {
         return GrSemaphoresSubmitted::kNo;
     }
     SkDEBUGCODE(this->validate();)
@@ -1848,7 +1848,7 @@
 SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
     ASSERT_SINGLE_OWNER_PRIV
 
-    if (fRenderTargetContext->fContext->abandoned()) {
+    if (fRenderTargetContext->fContext->priv().abandoned()) {
         return SkBudgeted::kNo;
     }
 
diff --git a/src/gpu/GrSoftwarePathRenderer.cpp b/src/gpu/GrSoftwarePathRenderer.cpp
index d93492b..2264141 100644
--- a/src/gpu/GrSoftwarePathRenderer.cpp
+++ b/src/gpu/GrSoftwarePathRenderer.cpp
@@ -100,7 +100,7 @@
                                            const SkMatrix& viewMatrix,
                                            const SkRect& rect,
                                            const SkMatrix& localMatrix) {
-    GrContext* context = renderTargetContext->surfPriv().getContext();
+    auto context = renderTargetContext->surfPriv().getContext();
     renderTargetContext->addDrawOp(clip,
                                    GrFillRectOp::MakeWithLocalMatrix(
                                            context, std::move(paint), GrAAType::kNone, viewMatrix,
diff --git a/src/gpu/GrSurfaceContext.cpp b/src/gpu/GrSurfaceContext.cpp
index 1b5a54cd..a573a36 100644
--- a/src/gpu/GrSurfaceContext.cpp
+++ b/src/gpu/GrSurfaceContext.cpp
@@ -52,9 +52,15 @@
     if (GrColorType::kUnknown == colorType) {
         return false;
     }
-    return fContext->priv().readSurfacePixels(this, x, y, dstInfo.width(), dstInfo.height(),
-                                                     colorType, dstInfo.colorSpace(), dstBuffer,
-                                                     dstRowBytes, flags);
+
+    auto direct = fContext->priv().asDirectContext();
+    if (!direct) {
+        return false;
+    }
+
+    return direct->priv().readSurfacePixels(this, x, y, dstInfo.width(), dstInfo.height(),
+                                            colorType, dstInfo.colorSpace(), dstBuffer,
+                                            dstRowBytes, flags);
 }
 
 bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
@@ -71,9 +77,15 @@
     if (GrColorType::kUnknown == colorType) {
         return false;
     }
-    return fContext->priv().writeSurfacePixels(this, x, y, srcInfo.width(), srcInfo.height(),
-                                                      colorType, srcInfo.colorSpace(), srcBuffer,
-                                                      srcRowBytes, flags);
+
+    auto direct = fContext->priv().asDirectContext();
+    if (!direct) {
+        return false;
+    }
+
+    return direct->priv().writeSurfacePixels(this, x, y, srcInfo.width(), srcInfo.height(),
+                                             colorType, srcInfo.colorSpace(), srcBuffer,
+                                             srcRowBytes, flags);
 }
 
 bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {