Change UnrefDDLTask to just be the DDLTask (take 2)

This includes:

https://skia-review.googlesource.com/c/skia/+/333576 (Fix UMR (valgrind) bug in GrDrawingManager)

Change-Id: I162aba033e75b47a96b4dfdd840ba2cb1f70e42c
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/334422
Reviewed-by: Adlai Holler <adlai@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrDrawingManager.cpp b/src/gpu/GrDrawingManager.cpp
index 68b4690..5961715 100644
--- a/src/gpu/GrDrawingManager.cpp
+++ b/src/gpu/GrDrawingManager.cpp
@@ -19,6 +19,7 @@
 #include "src/gpu/GrAuditTrail.h"
 #include "src/gpu/GrClientMappedBufferManager.h"
 #include "src/gpu/GrCopyRenderTask.h"
+#include "src/gpu/GrDDLTask.h"
 #include "src/gpu/GrDirectContextPriv.h"
 #include "src/gpu/GrGpu.h"
 #include "src/gpu/GrMemoryPool.h"
@@ -38,7 +39,6 @@
 #include "src/gpu/GrTextureResolveRenderTask.h"
 #include "src/gpu/GrTracing.h"
 #include "src/gpu/GrTransferFromRenderTask.h"
-#include "src/gpu/GrUnrefDDLTask.h"
 #include "src/gpu/GrWaitRenderTask.h"
 #include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
 #include "src/gpu/text/GrSDFTOptions.h"
@@ -149,10 +149,10 @@
 
     // Prepare any onFlush op lists (e.g. atlases).
     if (!fOnFlushCBObjects.empty()) {
-        fFlushingRenderTaskIDs.reset(fDAG.count());
-        for (int i = 0; i < fDAG.count(); i++) {
-            if (fDAG[i]) {
-                fFlushingRenderTaskIDs[i] = fDAG[i]->uniqueID();
+        fFlushingRenderTaskIDs.reserve_back(fDAG.count());
+        for (const auto& task : fDAG) {
+            if (task) {
+                task->gatherIDs(&fFlushingRenderTaskIDs);
             }
         }
 
@@ -376,8 +376,10 @@
         if (!task) {
             continue;
         }
-        if (!task->unique()) {
-            // TODO: Eventually this should be guaranteed unique: http://skbug.com/7111
+        if (!task->unique() || task->requiresExplicitCleanup()) {
+            // TODO: Eventually uniqueness should be guaranteed: http://skbug.com/7111.
+            // DDLs, however, will always require an explicit notification for when they
+            // can clean up resources.
             task->endFlush(this);
         }
         task->disown(this);
@@ -535,8 +537,8 @@
 
 void GrDrawingManager::setLastRenderTask(const GrSurfaceProxy* proxy, GrRenderTask* task) {
 #ifdef SK_DEBUG
-    if (GrRenderTask* prior = this->getLastRenderTask(proxy)) {
-        SkASSERT(prior->isClosed());
+    if (auto prior = this->getLastRenderTask(proxy)) {
+        SkASSERT(prior->isClosed() || prior == task);
     }
 #endif
     uint32_t key = proxy->uniqueID().asUInt();
@@ -565,6 +567,8 @@
     this->closeAllTasks();
     fActiveOpsTask = nullptr;
 
+    this->sortTasks();
+
     fDAG.swap(ddl->fRenderTasks);
     SkASSERT(fDAG.empty());
 
@@ -586,8 +590,8 @@
     SkDEBUGCODE(this->validate());
 }
 
-void GrDrawingManager::copyRenderTasksFromDDL(sk_sp<const SkDeferredDisplayList> ddl,
-                                              GrRenderTargetProxy* newDest) {
+void GrDrawingManager::createDDLTask(sk_sp<const SkDeferredDisplayList> ddl,
+                                     GrRenderTargetProxy* newDest) {
     SkDEBUGCODE(this->validate());
 
     if (fActiveOpsTask) {
@@ -599,7 +603,7 @@
         fActiveOpsTask = nullptr;
     }
 
-    // Propagate the DDL proxy's state information to the replaying DDL.
+    // Propagate the DDL proxy's state information to the replay target.
     if (ddl->priv().targetProxy()->isMSAADirty()) {
         newDest->markMSAADirty(ddl->priv().targetProxy()->msaaDirtyRect(),
                                ddl->characterization().origin());
@@ -612,7 +616,7 @@
     this->addDDLTarget(newDest, ddl->priv().targetProxy());
 
     // Here we jam the proxy that backs the current replay SkSurface into the LazyProxyData.
-    // The lazy proxy that references it (in the copied opsTasks) will steal its GrTexture.
+    // The lazy proxy that references it (in the DDL opsTasks) will then steal its GrTexture.
     ddl->fLazyProxyData->fReplayDest = newDest;
 
     if (ddl->fPendingPaths.size()) {
@@ -621,11 +625,11 @@
         ccpr->mergePendingPaths(ddl->fPendingPaths);
     }
 
-    this->appendTasks(ddl->fRenderTasks);
-
-    // Add a task to unref the DDL after flush.
-    GrRenderTask* unrefTask = this->appendTask(sk_make_sp<GrUnrefDDLTask>(std::move(ddl)));
-    unrefTask->makeClosed(*fContext->priv().caps());
+    // Add a task to handle drawing and lifetime management of the DDL.
+    SkDEBUGCODE(auto ddlTask =) this->appendTask(sk_make_sp<GrDDLTask>(this,
+                                                                       sk_ref_sp(newDest),
+                                                                       std::move(ddl)));
+    SkASSERT(ddlTask->isClosed());
 
     SkDEBUGCODE(this->validate());
 }
@@ -917,4 +921,3 @@
         resourceCache->purgeAsNeeded();
     }
 }
-