Copy on write for wrapped backend texture surfaces.

Makes SkImage_Gpu backed by two proxies, an original and a copy. The
image uses the original until a new render task is bound to it at which
point further uses of the image will use the copy. If the image is ever
used off a GrDirectContext we fall over to the copy. If the copy is
never used and never can be used by the next flush then the render
task that populates it is marked "skipped" and we don't perform the
copy.

Bug: skia:11208

Change-Id: Id255f4a733acc608c8a53c1a5633207aeafc404b
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/366282
Commit-Queue: Brian Salomon <bsalomon@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrDrawingManager.cpp b/src/gpu/GrDrawingManager.cpp
index cc4f761..628d0fe 100644
--- a/src/gpu/GrDrawingManager.cpp
+++ b/src/gpu/GrDrawingManager.cpp
@@ -798,11 +798,11 @@
     SkDEBUGCODE(this->validate());
 }
 
-bool GrDrawingManager::newCopyRenderTask(sk_sp<GrSurfaceProxy> src,
-                                         SkIRect srcRect,
-                                         sk_sp<GrSurfaceProxy> dst,
-                                         SkIPoint dstPoint,
-                                         GrSurfaceOrigin origin) {
+sk_sp<GrRenderTask> GrDrawingManager::newCopyRenderTask(sk_sp<GrSurfaceProxy> src,
+                                                        SkIRect srcRect,
+                                                        sk_sp<GrSurfaceProxy> dst,
+                                                        SkIPoint dstPoint,
+                                                        GrSurfaceOrigin origin) {
     SkDEBUGCODE(this->validate());
     SkASSERT(fContext);
 
@@ -813,21 +813,23 @@
     // task, then fail to make a copy task, the next active ops task may target the same proxy. This
     // will trip an assert related to unnecessary ops task splitting.
     if (src->framebufferOnly()) {
-        return false;
+        return nullptr;
     }
 
     this->closeActiveOpsTask();
 
-    GrRenderTask* task = this->appendTask(GrCopyRenderTask::Make(this,
-                                                                 src,
-                                                                 srcRect,
-                                                                 std::move(dst),
-                                                                 dstPoint,
-                                                                 origin));
+    sk_sp<GrRenderTask> task = GrCopyRenderTask::Make(this,
+                                                      src,
+                                                      srcRect,
+                                                      std::move(dst),
+                                                      dstPoint,
+                                                      origin);
     if (!task) {
-        return false;
+        return nullptr;
     }
 
+    this->appendTask(task);
+
     const GrCaps& caps = *fContext->priv().caps();
     // We always say GrMipmapped::kNo here since we are always just copying from the base layer to
     // another base layer. We don't need to make sure the whole mip map chain is valid.
@@ -838,7 +840,7 @@
     // shouldn't be an active one.
     SkASSERT(!fActiveOpsTask);
     SkDEBUGCODE(this->validate());
-    return true;
+    return task;
 }
 
 bool GrDrawingManager::newWritePixelsTask(sk_sp<GrSurfaceProxy> dst,