Remove GrResourceProvider & GrGpu pointers from GrRenderTargetOpList

Additional shrinking of GrRenderTargetOpList since there will soon be more of them.

Change-Id: Ib3e68fdf3462838baf7a5a2644e418be066cf79c
Reviewed-on: https://skia-review.googlesource.com/14363
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrDrawingManager.cpp b/src/gpu/GrDrawingManager.cpp
index 2258c9e..9cc5845 100644
--- a/src/gpu/GrDrawingManager.cpp
+++ b/src/gpu/GrDrawingManager.cpp
@@ -25,7 +25,8 @@
 
 void GrDrawingManager::cleanup() {
     for (int i = 0; i < fOpLists.count(); ++i) {
-        fOpLists[i]->makeClosed();  // no opList should receive a new command after this
+        // no opList should receive a new command after this
+        fOpLists[i]->makeClosed(*fContext->caps());
         fOpLists[i]->clearTarget();
 
         // We shouldn't need to do this, but it turns out some clients still hold onto opLists
@@ -91,7 +92,7 @@
         // 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
         // GrOpLists will be created to replace them if the SkGpuDevice(s) write to them again.
-        fOpLists[i]->makeClosed();
+        fOpLists[i]->makeClosed(*fContext->caps());
     }
 
 #ifdef ENABLE_MDB
@@ -223,7 +224,6 @@
 
     sk_sp<GrRenderTargetOpList> opList(new GrRenderTargetOpList(rtp,
                                                                 fContext->getGpu(),
-                                                                fContext->resourceProvider(),
                                                                 fContext->getAuditTrail()));
     SkASSERT(rtp->getLastOpList() == opList.get());
 
diff --git a/src/gpu/GrOpList.cpp b/src/gpu/GrOpList.cpp
index 91c8c79..3781213 100644
--- a/src/gpu/GrOpList.cpp
+++ b/src/gpu/GrOpList.cpp
@@ -48,7 +48,7 @@
 }
 
 // Convert from a GrSurface-based dependency to a GrOpList one
-void GrOpList::addDependency(GrSurfaceProxy* dependedOn) {
+void GrOpList::addDependency(GrSurfaceProxy* dependedOn, const GrCaps& caps) {
     if (dependedOn->getLastOpList()) {
         // If it is still receiving dependencies, this GrOpList shouldn't be closed
         SkASSERT(!this->isClosed());
@@ -60,7 +60,7 @@
             this->addDependency(opList);
 
             // Can't make it closed in the self-read case
-            opList->makeClosed();
+            opList->makeClosed(caps);
         }
     }
 }
diff --git a/src/gpu/GrOpList.h b/src/gpu/GrOpList.h
index b7dd7fa..b354abe 100644
--- a/src/gpu/GrOpList.h
+++ b/src/gpu/GrOpList.h
@@ -14,6 +14,7 @@
 //#define ENABLE_MDB 1
 
 class GrAuditTrail;
+class GrCaps;
 class GrOpFlushState;
 class GrRenderTargetOpList;
 class GrSurfaceProxy;
@@ -28,7 +29,7 @@
     virtual void prepareOps(GrOpFlushState* flushState) = 0;
     virtual bool executeOps(GrOpFlushState* flushState) = 0;
 
-    virtual void makeClosed() {
+    virtual void makeClosed(const GrCaps&) {
         // We only close GrOpLists when MDB is enabled. When MDB is disabled there is only
         // ever one GrOpLists and all calls will be funnelled into it.
 #ifdef ENABLE_MDB
@@ -53,7 +54,7 @@
     /*
      * Notify this GrOpList that it relies on the contents of 'dependedOn'
      */
-    void addDependency(GrSurfaceProxy* dependedOn);
+    void addDependency(GrSurfaceProxy* dependedOn, const GrCaps& caps);
 
     /*
      * Does this opList depend on 'dependedOn'?
diff --git a/src/gpu/GrPreFlushResourceProvider.cpp b/src/gpu/GrPreFlushResourceProvider.cpp
index 01d948a..43037be 100644
--- a/src/gpu/GrPreFlushResourceProvider.cpp
+++ b/src/gpu/GrPreFlushResourceProvider.cpp
@@ -35,7 +35,6 @@
     sk_sp<GrRenderTargetOpList> opList(new GrRenderTargetOpList(
                                                     sk_ref_sp(proxy->asRenderTargetProxy()),
                                                     fDrawingMgr->fContext->getGpu(),
-                                                    fDrawingMgr->fContext->resourceProvider(),
                                                     fDrawingMgr->fContext->getAuditTrail()));
     proxy->setLastOpList(opList.get());
 
@@ -64,7 +63,6 @@
     sk_sp<GrRenderTargetOpList> opList(new GrRenderTargetOpList(
                                                     sk_ref_sp(proxy->asRenderTargetProxy()),
                                                     fDrawingMgr->fContext->getGpu(),
-                                                    fDrawingMgr->fContext->resourceProvider(),
                                                     fDrawingMgr->fContext->getAuditTrail()));
     proxy->setLastOpList(opList.get());
 
diff --git a/src/gpu/GrRenderTargetOpList.cpp b/src/gpu/GrRenderTargetOpList.cpp
index a7e109d..3abc21c 100644
--- a/src/gpu/GrRenderTargetOpList.cpp
+++ b/src/gpu/GrRenderTargetOpList.cpp
@@ -26,21 +26,17 @@
 static const int kMaxOpLookahead = 10;
 
 GrRenderTargetOpList::GrRenderTargetOpList(sk_sp<GrRenderTargetProxy> proxy, GrGpu* gpu,
-                                           GrResourceProvider* resourceProvider,
                                            GrAuditTrail* auditTrail)
         : INHERITED(std::move(proxy), auditTrail)
-        , fGpu(SkRef(gpu))
-        , fResourceProvider(resourceProvider)
         , fLastClipStackGenID(SK_InvalidUniqueID)
         , fClipAllocator(fClipAllocatorStorage, sizeof(fClipAllocatorStorage),
                          sizeof(fClipAllocatorStorage)) {
-    if (GrCaps::InstancedSupport::kNone != this->caps()->instancedSupport()) {
-        fInstancedRendering.reset(fGpu->createInstancedRendering());
+    if (GrCaps::InstancedSupport::kNone != gpu->caps()->instancedSupport()) {
+        fInstancedRendering.reset(gpu->createInstancedRendering());
     }
 }
 
 GrRenderTargetOpList::~GrRenderTargetOpList() {
-    fGpu->unref();
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -161,10 +157,10 @@
             finish_command_buffer(commandBuffer.get());
             currentRenderTarget = fRecordedOps[i].fRenderTarget.get();
 
-            commandBuffer = create_command_buffer(fGpu);
+            commandBuffer = create_command_buffer(flushState->gpu());
             flushState->setCommandBuffer(commandBuffer.get());
         } else if (!commandBuffer) {
-            commandBuffer = create_command_buffer(fGpu);
+            commandBuffer = create_command_buffer(flushState->gpu());
             flushState->setCommandBuffer(commandBuffer.get());
         }
 
@@ -182,7 +178,7 @@
     finish_command_buffer(commandBuffer.get());
     flushState->setCommandBuffer(nullptr);
 
-    fGpu->finishOpList();
+    flushState->gpu()->finishOpList();
     return true;
 }
 
@@ -197,16 +193,14 @@
 }
 
 void GrRenderTargetOpList::abandonGpuResources() {
-    if (GrCaps::InstancedSupport::kNone != this->caps()->instancedSupport()) {
-        InstancedRendering* ir = this->instancedRendering();
-        ir->resetGpuResources(InstancedRendering::ResetType::kAbandon);
+    if (fInstancedRendering) {
+        fInstancedRendering->resetGpuResources(InstancedRendering::ResetType::kAbandon);
     }
 }
 
 void GrRenderTargetOpList::freeGpuResources() {
-    if (GrCaps::InstancedSupport::kNone != this->caps()->instancedSupport()) {
-        InstancedRendering* ir = this->instancedRendering();
-        ir->resetGpuResources(InstancedRendering::ResetType::kDestroy);
+    if (fInstancedRendering) {
+        fInstancedRendering->resetGpuResources(InstancedRendering::ResetType::kDestroy);
     }
 }
 
@@ -278,7 +272,8 @@
 
 bool GrRenderTargetOpList::combineIfPossible(const RecordedOp& a, GrOp* b,
                                              const GrAppliedClip* bClip,
-                                             const DstTexture* bDstTexture) {
+                                             const DstTexture* bDstTexture,
+                                             const GrCaps& caps) {
     if (a.fAppliedClip) {
         if (!bClip) {
             return false;
@@ -296,7 +291,7 @@
     } else if (a.fDstTexture.texture()) {
         return false;
     }
-    return a.fOp->combineIfPossible(b, *this->caps());
+    return a.fOp->combineIfPossible(b, caps);
 }
 
 GrOp* GrRenderTargetOpList::recordOp(std::unique_ptr<GrOp> op,
@@ -309,6 +304,8 @@
         return nullptr;
     }
 
+    const GrCaps* caps = renderTargetContext->caps();
+
     // A closed GrOpList should never receive new/more ops
     SkASSERT(!this->isClosed());
 
@@ -340,7 +337,7 @@
                           candidate.fOp->uniqueID());
                 break;
             }
-            if (this->combineIfPossible(candidate, op.get(), clip, dstTexture)) {
+            if (this->combineIfPossible(candidate, op.get(), clip, dstTexture, *caps)) {
                 GrOP_INFO("\t\tBackward: Combining with (%s, opID: %u)\n", candidate.fOp->name(),
                           candidate.fOp->uniqueID());
                 GrOP_INFO("\t\t\tBackward: Combined op info:\n");
@@ -375,7 +372,7 @@
     return fRecordedOps.back().fOp.get();
 }
 
-void GrRenderTargetOpList::forwardCombine() {
+void GrRenderTargetOpList::forwardCombine(const GrCaps& caps) {
     SkASSERT(!this->isClosed());
 
     for (int i = 0; i < fRecordedOps.count() - 1; ++i) {
@@ -398,7 +395,7 @@
                 break;
             }
             if (this->combineIfPossible(fRecordedOps[i], candidate.fOp.get(),
-                                        candidate.fAppliedClip, &candidate.fDstTexture)) {
+                                        candidate.fAppliedClip, &candidate.fDstTexture, caps)) {
                 GrOP_INFO("\t\tForward: Combining with (%s, opID: %u)\n", candidate.fOp->name(),
                           candidate.fOp->uniqueID());
                 GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(fAuditTrail, op, candidate.fOp.get());
diff --git a/src/gpu/GrRenderTargetOpList.h b/src/gpu/GrRenderTargetOpList.h
index 88bb6fd..4b3f0d0 100644
--- a/src/gpu/GrRenderTargetOpList.h
+++ b/src/gpu/GrRenderTargetOpList.h
@@ -33,19 +33,19 @@
     using DstTexture = GrXferProcessor::DstTexture;
 
 public:
-    GrRenderTargetOpList(sk_sp<GrRenderTargetProxy>, GrGpu*, GrResourceProvider*, GrAuditTrail*);
+    GrRenderTargetOpList(sk_sp<GrRenderTargetProxy>, GrGpu*, GrAuditTrail*);
 
     ~GrRenderTargetOpList() override;
 
-    void makeClosed() override {
+    void makeClosed(const GrCaps& caps) override {
         if (this->isClosed()) {
             return;
         }
 
         fLastFullClearOp = nullptr;
-        this->forwardCombine();
+        this->forwardCombine(caps);
 
-        INHERITED::makeClosed();
+        INHERITED::makeClosed(caps);
     }
 
     bool isEmpty() const { return fRecordedOps.empty(); }
@@ -65,11 +65,6 @@
     void prepareOps(GrOpFlushState* flushState) override;
     bool executeOps(GrOpFlushState* flushState) override;
 
-    /**
-     * Gets the capabilities of the draw target.
-     */
-    const GrCaps* caps() const { return fGpu->caps(); }
-
     uint32_t addOp(std::unique_ptr<GrOp> op, GrRenderTargetContext* renderTargetContext) {
         this->recordOp(std::move(op), renderTargetContext, nullptr, nullptr);
         return this->uniqueID();
@@ -138,19 +133,16 @@
     GrOp* recordOp(std::unique_ptr<GrOp>, GrRenderTargetContext*, GrAppliedClip* = nullptr,
                    const DstTexture* = nullptr);
 
-    void forwardCombine();
+    void forwardCombine(const GrCaps&);
 
     // If this returns true then b has been merged into a's op.
     bool combineIfPossible(const RecordedOp& a, GrOp* b, const GrAppliedClip* bClip,
-                           const DstTexture* bDstTexture);
+                           const DstTexture* bDstTexture, const GrCaps&);
 
     GrClearOp* fLastFullClearOp = nullptr;
     GrGpuResource::UniqueID fLastFullClearResourceID = GrGpuResource::UniqueID::InvalidID();
     GrSurfaceProxy::UniqueID fLastFullClearProxyID = GrSurfaceProxy::UniqueID::InvalidID();
 
-    GrGpu* fGpu;
-    GrResourceProvider* fResourceProvider;
-
     std::unique_ptr<gr_instanced::InstancedRendering> fInstancedRendering;
 
     int32_t fLastClipStackGenID;
diff --git a/src/gpu/instanced/InstancedOp.cpp b/src/gpu/instanced/InstancedOp.cpp
index 5880721..1ebb0b5 100644
--- a/src/gpu/instanced/InstancedOp.cpp
+++ b/src/gpu/instanced/InstancedOp.cpp
@@ -163,7 +163,7 @@
     fIsTracked = true;
 }
 
-bool InstancedOp::onCombineIfPossible(GrOp* other, const GrCaps& caps) {
+bool InstancedOp::onCombineIfPossible(GrOp* other, const GrCaps&) {
     InstancedOp* that = static_cast<InstancedOp*>(other);
     SkASSERT(!that->fInstancedRendering || (fInstancedRendering == that->fInstancedRendering));
     SkASSERT(fTailDraw);