Reland "Reland "SkSurface asynchronous read APIs allow client to extend pixel lifetime""

This is a reland of 6fc04f88a89ed2c9a1b4aa48bcd28602a69a457b

Original change's description:
> Reland "SkSurface asynchronous read APIs allow client to extend pixel lifetime"
> 
> This is a reland of ce240cc6fd8ec95bd051c7df2173dad2ae8f6ad6
> 
> Original change's description:
> > SkSurface asynchronous read APIs allow client to extend pixel lifetime
> > 
> > Previously the pixel data passed to the client was only valid during
> > the client's callback. This meant if the client wanted to defer
> > processing of the data a copy was necessary.
> > 
> > Now we pass an object to the callback and the pixel lifetime is tied
> > to the lifetime of that object.
> > 
> > The object may be holding a GPU transfer buffer mapped. We don't assume
> > that the object will be released on the direct GrContext thread. So
> > when the object is destroyed it posts a message to a new type,
> > GrClientMappedBufferManager, hanging off the direct context. The direct
> > context will periodically check for messages and unmap and then unref
> > buffers so that they can be reused. Currently this is done in
> > GrContext::performDeferredCleanup() and GrDrawingManager::flush().
> > 
> > The old API is kept around for backwards compatibility but it is
> > reimplemented as a bridge on top of the new mechanism.
> > 
> > Also a utility function to SkImageInfo is added to directly make a new
> > info with a specified dimensions rather than passing the width and
> > height separately to makeWH().
> > 
> > Bug: chromium:973403
> > Bug: skia:8962
> > 
> > Change-Id: Id5cf04235376170142a48e90d3ecd13fd021a2a6
> > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/245457
> > Reviewed-by: Brian Osman <brianosman@google.com>
> > Commit-Queue: Brian Salomon <bsalomon@google.com>
> 
> Bug: chromium:973403, skia:8962
> Change-Id: I5cecd36276c8b6dc942cf549c7095db2df88530c
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/245678
> Reviewed-by: Brian Salomon <bsalomon@google.com>
> Commit-Queue: Brian Salomon <bsalomon@google.com>

Bug: chromium:973403, skia:8962
Change-Id: Ie584c1c3ef8021c976f71b708e53871c693cc450
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/246057
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
diff --git a/src/gpu/GrClientMappedBufferManager.cpp b/src/gpu/GrClientMappedBufferManager.cpp
new file mode 100644
index 0000000..b927726
--- /dev/null
+++ b/src/gpu/GrClientMappedBufferManager.cpp
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "src/gpu/GrClientMappedBufferManager.h"
+
+#include <algorithm>
+
+GrClientMappedBufferManager::GrClientMappedBufferManager(uint32_t contextID)
+        : fFinishedBufferInbox(contextID) {}
+
+GrClientMappedBufferManager::~GrClientMappedBufferManager() {
+    this->process();
+    if (!fAbandoned) {
+        // If we're going down before we got the messages we go ahead and unmap all the buffers.
+        // It's up to the client to ensure that they aren't being accessed on another thread while
+        // this is happening (or afterwards on any thread).
+        for (auto& b : fClientHeldBuffers) {
+            b->unmap();
+        }
+    }
+}
+
+void GrClientMappedBufferManager::insert(sk_sp<GrGpuBuffer> b) {
+    SkDEBUGCODE(auto end = fClientHeldBuffers.end());
+    SkASSERT(std::find(fClientHeldBuffers.begin(), end, b) == end);
+    fClientHeldBuffers.emplace_front(std::move(b));
+}
+
+void GrClientMappedBufferManager::process() {
+    SkSTArray<4, BufferFinishedMessage> messages;
+    fFinishedBufferInbox.poll(&messages);
+    if (!fAbandoned) {
+        for (auto& m : messages) {
+            this->remove(m.fBuffer);
+            m.fBuffer->unmap();
+        }
+    }
+}
+
+void GrClientMappedBufferManager::abandon() {
+    fAbandoned = true;
+    fClientHeldBuffers.clear();
+}
+
+void GrClientMappedBufferManager::remove(const sk_sp<GrGpuBuffer>& b) {
+    // There is no convenient remove only the first element that equals a value functionality in
+    // std::forward_list.
+    auto prev = fClientHeldBuffers.before_begin();
+    auto end = fClientHeldBuffers.end();
+    SkASSERT(std::find(fClientHeldBuffers.begin(), end, b) != end);
+    for (auto cur = fClientHeldBuffers.begin(); cur != end; prev = cur++) {
+        if (*cur == b) {
+            fClientHeldBuffers.erase_after(prev);
+            break;
+        }
+    }
+    SkASSERT(std::find(fClientHeldBuffers.begin(), end, b) == end);
+}
+
+//////////////////////////////////////////////////////////////////////////////
+
+DECLARE_SKMESSAGEBUS_MESSAGE(GrClientMappedBufferManager::BufferFinishedMessage)
+
+bool SkShouldPostMessageToBus(const GrClientMappedBufferManager::BufferFinishedMessage& m,
+                              uint32_t msgBusUniqueID) {
+    return m.fInboxID == msgBusUniqueID;
+}