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.h b/src/gpu/GrClientMappedBufferManager.h
new file mode 100644
index 0000000..3f3b067
--- /dev/null
+++ b/src/gpu/GrClientMappedBufferManager.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef GrClientMappedBufferManager_DEFINED
+#define GrClientMappedBufferManager_DEFINED
+
+#include "include/private/SkTArray.h"
+#include "src/core/SkMessageBus.h"
+#include "src/gpu/GrGpuBuffer.h"
+#include <forward_list>
+
+/**
+ * We sometimes hand clients objects that contain mapped GrGpuBuffers. The client may consume
+ * the mapped buffer on another thread. This object manages receiving messages that buffers are
+ * ready to be unmapped (on the direct GrContext's thread). It also handles cleaning up mapped
+ * buffers if the GrContext is destroyed before the client has finished with the buffer.
+ *
+ * Buffers are first registered using insert() before being passed the client. process() should be
+ * called periodically on the direct GrContext thread to poll for messages and process them.
+ */
+class GrClientMappedBufferManager final {
+public:
+ /**
+ * The message type that internal users of this should post to unmap the buffer.
+ * Set fInboxID to inboxID(). fBuffer must have been previously passed to insert().
+ */
+ struct BufferFinishedMessage {
+ sk_sp<GrGpuBuffer> fBuffer;
+ uint32_t fInboxID;
+ };
+ using BufferFinishedMessageBus = SkMessageBus<BufferFinishedMessage>;
+
+ GrClientMappedBufferManager(uint32_t contextID);
+ GrClientMappedBufferManager(const GrClientMappedBufferManager&) = delete;
+ GrClientMappedBufferManager(GrClientMappedBufferManager&&) = delete;
+
+ ~GrClientMappedBufferManager();
+
+ GrClientMappedBufferManager& operator=(const GrClientMappedBufferManager&) = delete;
+ GrClientMappedBufferManager& operator=(GrClientMappedBufferManager&&) = delete;
+
+ /** Initialize BufferFinishedMessage::fInboxID to this value. */
+ uint32_t inboxID() const { return fFinishedBufferInbox.uniqueID(); }
+
+ /**
+ * Let the manager know to expect a message with buffer 'b'. It's illegal for a buffer to be
+ * inserted again before it is unmapped by process().
+ */
+ void insert(sk_sp<GrGpuBuffer> b);
+
+ /** Poll for messages and unmap any incoming buffers. */
+ void process();
+
+ /** Notifies the manager that the context has been abandoned. No more unmaps() will occur.*/
+ void abandon();
+
+private:
+ BufferFinishedMessageBus::Inbox fFinishedBufferInbox;
+ std::forward_list<sk_sp<GrGpuBuffer>> fClientHeldBuffers;
+ bool fAbandoned = false;
+
+ void remove(const sk_sp<GrGpuBuffer>& b);
+};
+
+bool SkShouldPostMessageToBus(const GrClientMappedBufferManager::BufferFinishedMessage&,
+ uint32_t msgBusUniqueID);
+
+#endif