Revert "Revert "Reuse GrTexture instances when the same GrBackendTexture is used to""

This reverts commit 426ba463d54c7fdd6f3b94f88b66c10c0212dafd.

Bug: skia:8613
Change-Id: Iacaf40549369110aa95015e8d4579ec41db22d13
Reviewed-on: https://skia-review.googlesource.com/c/182963
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
diff --git a/tools/DDLPromiseImageHelper.cpp b/tools/DDLPromiseImageHelper.cpp
index 2586835..fa72265 100644
--- a/tools/DDLPromiseImageHelper.cpp
+++ b/tools/DDLPromiseImageHelper.cpp
@@ -17,13 +17,30 @@
 #include "SkYUVASizeInfo.h"
 
 DDLPromiseImageHelper::PromiseImageCallbackContext::~PromiseImageCallbackContext() {
+    SkASSERT(fDoneCnt == fNumImages);
+    SkASSERT(!fUnreleasedFulfills);
+    SkASSERT(fTotalReleases == fTotalFulfills);
+    SkASSERT(!fTotalFulfills || fDoneCnt);
     GrGpu* gpu = fContext->contextPriv().getGpu();
 
-    if (fBackendTexture.isValid()) {
-        gpu->deleteTestingOnlyBackendTexture(fBackendTexture);
+    if (fPromiseImageTexture.isValid()) {
+        gpu->deleteTestingOnlyBackendTexture(fPromiseImageTexture.backendTexture());
     }
 }
 
+void DDLPromiseImageHelper::PromiseImageCallbackContext::setBackendTexture(
+        const GrBackendTexture& backendTexture) {
+    SkASSERT(!fUnreleasedFulfills);
+    if (fPromiseImageTexture.isValid()) {
+        GrGpu* gpu = fContext->contextPriv().getGpu();
+
+        if (fPromiseImageTexture.isValid()) {
+            gpu->deleteTestingOnlyBackendTexture(fPromiseImageTexture.backendTexture());
+        }
+    }
+    fPromiseImageTexture = SkPromiseImageTexture{backendTexture};
+}
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 DDLPromiseImageHelper::~DDLPromiseImageHelper() {}
@@ -111,7 +128,7 @@
 
                 callbackContext->setBackendTexture(create_yuva_texture(gpu, yuvPixmap,
                                                                        info.yuvaIndices(), j));
-                SkAssertResult(callbackContext->backendTexture().isValid());
+                SkASSERT(callbackContext->promiseImageTexture()->isValid());
 
                 fImageInfo[i].setCallbackContext(j, std::move(callbackContext));
             }
@@ -133,7 +150,34 @@
 
             fImageInfo[i].setCallbackContext(0, std::move(callbackContext));
         }
+    }
+}
 
+void DDLPromiseImageHelper::replaceEveryOtherPromiseTexture(GrContext* context) {
+    GrGpu* gpu = context->contextPriv().getGpu();
+    SkASSERT(gpu);
+
+    for (int i = 0; i < fImageInfo.count(); i += 2) {
+        PromiseImageInfo& info = fImageInfo[i];
+
+        // DDL TODO: how can we tell if we need mipmapping!
+        if (info.isYUV()) {
+            int numPixmaps;
+            SkAssertResult(SkYUVAIndex::AreValidIndices(info.yuvaIndices(), &numPixmaps));
+            for (int j = 0; j < numPixmaps; ++j) {
+                const SkPixmap& yuvPixmap = info.yuvPixmap(j);
+                info.callbackContext(j)->setBackendTexture(
+                        create_yuva_texture(gpu, yuvPixmap, info.yuvaIndices(), j));
+                SkASSERT(info.callbackContext(j)->promiseImageTexture()->isValid());
+            }
+        } else {
+            const SkBitmap& bm = info.normalBitmap();
+            info.callbackContext(0)->setBackendTexture(gpu->createTestingOnlyBackendTexture(
+                    bm.getPixels(), bm.width(), bm.height(), bm.colorType(), false,
+                    GrMipMapped::kNo, bm.rowBytes()));
+            // The GMs sometimes request too large an image
+            // SkAssertResult(callbackContext->backendTexture().isValid());
+        }
     }
 }
 
@@ -206,7 +250,9 @@
                                                  DDLPromiseImageHelper::PromiseImageReleaseProc,
                                                  DDLPromiseImageHelper::PromiseImageDoneProc,
                                                  contexts);
-
+        for (int i = 0; i < textureCount; ++i) {
+            curImage.callbackContext(i)->wasAddedToImage();
+        }
     } else {
         const GrBackendTexture& backendTex = curImage.backendTexture(0);
         GrBackendFormat backendFormat = backendTex.getBackendFormat();
@@ -227,6 +273,7 @@
                                              DDLPromiseImageHelper::PromiseImageReleaseProc,
                                              DDLPromiseImageHelper::PromiseImageDoneProc,
                                              (void*) curImage.refCallbackContext(0).release());
+        curImage.callbackContext(0)->wasAddedToImage();
     }
     perRecorderContext->fPromiseImages->push_back(image);
     SkASSERT(image);