Make GrClientMappedBufferManager use GrDirectContextID for messages

Change-Id: Iace947384fbe426915e7a9d426dc7ac60fb3883b
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/383700
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/src/gpu/GrClientMappedBufferManager.cpp b/src/gpu/GrClientMappedBufferManager.cpp
index 10fa4d5..316e47e 100644
--- a/src/gpu/GrClientMappedBufferManager.cpp
+++ b/src/gpu/GrClientMappedBufferManager.cpp
@@ -9,8 +9,10 @@
 
 #include <algorithm>
 
-GrClientMappedBufferManager::GrClientMappedBufferManager(uint32_t contextID)
-        : fFinishedBufferInbox(contextID) {}
+GrClientMappedBufferManager::GrClientMappedBufferManager(
+            GrDirectContext::DirectContextID owningDirectContext)
+        : fFinishedBufferInbox(owningDirectContext) {
+}
 
 GrClientMappedBufferManager::~GrClientMappedBufferManager() {
     this->process();
@@ -63,9 +65,11 @@
 
 //////////////////////////////////////////////////////////////////////////////
 
-DECLARE_SKMESSAGEBUS_MESSAGE(GrClientMappedBufferManager::BufferFinishedMessage, uint32_t, false)
+DECLARE_SKMESSAGEBUS_MESSAGE(GrClientMappedBufferManager::BufferFinishedMessage,
+                             GrDirectContext::DirectContextID,
+                             false)
 
 bool SkShouldPostMessageToBus(const GrClientMappedBufferManager::BufferFinishedMessage& m,
-                              uint32_t msgBusUniqueID) {
-    return m.fInboxID == msgBusUniqueID;
+                              GrDirectContext::DirectContextID potentialRecipient) {
+    return m.fIntendedRecipient == potentialRecipient;
 }
diff --git a/src/gpu/GrClientMappedBufferManager.h b/src/gpu/GrClientMappedBufferManager.h
index 9ba0954..a77e598 100644
--- a/src/gpu/GrClientMappedBufferManager.h
+++ b/src/gpu/GrClientMappedBufferManager.h
@@ -8,6 +8,7 @@
 #ifndef GrClientMappedBufferManager_DEFINED
 #define GrClientMappedBufferManager_DEFINED
 
+#include "include/gpu/GrDirectContext.h"
 #include "include/private/SkTArray.h"
 #include "src/core/SkMessageBus.h"
 #include "src/gpu/GrGpuBuffer.h"
@@ -16,11 +17,11 @@
 /**
  * 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.
+ * ready to be unmapped (on the GrDirectContext's thread). It also handles cleaning up mapped
+ * buffers if the GrDirectContext 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.
+ * called periodically on the GrDirectContext thread to poll for messages and process them.
  */
 class GrClientMappedBufferManager final {
 public:
@@ -29,19 +30,22 @@
      * Set fInboxID to inboxID(). fBuffer must have been previously passed to insert().
      */
     struct BufferFinishedMessage {
-        BufferFinishedMessage(sk_sp<GrGpuBuffer> buffer, uint32_t indexId)
-                : fBuffer(std::move(buffer)), fInboxID(indexId) {}
+        BufferFinishedMessage(sk_sp<GrGpuBuffer> buffer,
+                              GrDirectContext::DirectContextID intendedRecipient)
+                : fBuffer(std::move(buffer)), fIntendedRecipient(intendedRecipient) {}
         BufferFinishedMessage(BufferFinishedMessage&& other) {
             fBuffer = std::move(other.fBuffer);
-            fInboxID = other.fInboxID;
-            other.fInboxID = 0;
+            fIntendedRecipient = other.fIntendedRecipient;
+            other.fIntendedRecipient.makeInvalid();
         }
-        sk_sp<GrGpuBuffer> fBuffer;
-        uint32_t fInboxID;
+        sk_sp<GrGpuBuffer>               fBuffer;
+        GrDirectContext::DirectContextID fIntendedRecipient;
     };
-    using BufferFinishedMessageBus = SkMessageBus<BufferFinishedMessage, uint32_t, false>;
+    using BufferFinishedMessageBus = SkMessageBus<BufferFinishedMessage,
+                                                  GrDirectContext::DirectContextID,
+                                                  false>;
 
-    GrClientMappedBufferManager(uint32_t contextID);
+    GrClientMappedBufferManager(GrDirectContext::DirectContextID owningDirectContext);
     GrClientMappedBufferManager(const GrClientMappedBufferManager&) = delete;
     GrClientMappedBufferManager(GrClientMappedBufferManager&&) = delete;
 
@@ -50,8 +54,12 @@
     GrClientMappedBufferManager& operator=(const GrClientMappedBufferManager&) = delete;
     GrClientMappedBufferManager& operator=(GrClientMappedBufferManager&&) = delete;
 
-    /** Initialize BufferFinishedMessage::fInboxID to this value. */
-    uint32_t inboxID() const { return fFinishedBufferInbox.uniqueID(); }
+    /** Initialize BufferFinishedMessage::fIntendedRecipient to this value. It is the
+     *  unique ID of the GrDirectContext that owns this buffer manager.
+     */
+    GrDirectContext::DirectContextID owningDirectContext() const {
+        return fFinishedBufferInbox.uniqueID();
+    }
 
     /**
      * Let the manager know to expect a message with buffer 'b'. It's illegal for a buffer to be
@@ -74,6 +82,6 @@
 };
 
 bool SkShouldPostMessageToBus(const GrClientMappedBufferManager::BufferFinishedMessage&,
-                              uint32_t msgBusUniqueID);
+                              GrDirectContext::DirectContextID potentialRecipient);
 
 #endif
diff --git a/src/gpu/GrDirectContext.cpp b/src/gpu/GrDirectContext.cpp
index 19722bb..0d62b04 100644
--- a/src/gpu/GrDirectContext.cpp
+++ b/src/gpu/GrDirectContext.cpp
@@ -214,7 +214,7 @@
     fResourceCache->setThreadSafeCache(this->threadSafeCache());
     fResourceProvider = std::make_unique<GrResourceProvider>(fGpu.get(), fResourceCache.get(),
                                                              this->singleOwner());
-    fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->contextID());
+    fMappedBufferManager = std::make_unique<GrClientMappedBufferManager>(this->directContextID());
 
     fDidTestPMConversions = false;
 
diff --git a/src/gpu/GrSurfaceContext.cpp b/src/gpu/GrSurfaceContext.cpp
index 1adc7f8..7c84826 100644
--- a/src/gpu/GrSurfaceContext.cpp
+++ b/src/gpu/GrSurfaceContext.cpp
@@ -644,10 +644,13 @@
 
 class GrSurfaceContext::AsyncReadResult : public SkImage::AsyncReadResult {
 public:
-    AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
+    AsyncReadResult(GrDirectContext::DirectContextID intendedRecipient)
+        : fIntendedRecipient(intendedRecipient) {
+    }
+
     ~AsyncReadResult() override {
         for (int i = 0; i < fPlanes.count(); ++i) {
-            fPlanes[i].releaseMappedBuffer(fInboxID);
+            fPlanes[i].releaseMappedBuffer(fIntendedRecipient);
         }
     }
 
@@ -706,10 +709,10 @@
         Plane& operator=(const Plane&) = delete;
         Plane& operator=(Plane&&) = default;
 
-        void releaseMappedBuffer(uint32_t inboxID) {
+        void releaseMappedBuffer(GrDirectContext::DirectContextID intendedRecipient) {
             if (fMappedBuffer) {
                 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
-                        {std::move(fMappedBuffer), inboxID});
+                        {std::move(fMappedBuffer), intendedRecipient});
             }
         }
 
@@ -731,7 +734,7 @@
         size_t fRowBytes;
     };
     SkSTArray<3, Plane> fPlanes;
-    uint32_t fInboxID;
+    GrDirectContext::DirectContextID fIntendedRecipient;
 };
 
 void GrSurfaceContext::asyncReadPixels(GrDirectContext* dContext,
@@ -754,7 +757,8 @@
     if (!transferResult.fTransferBuffer) {
         auto ii = SkImageInfo::Make(rect.size(), colorType, this->colorInfo().alphaType(),
                                     this->colorInfo().refColorSpace());
-        auto result = std::make_unique<AsyncReadResult>(0);
+        static const GrDirectContext::DirectContextID kInvalid;
+        auto result = std::make_unique<AsyncReadResult>(kInvalid);
         GrPixmap pm = GrPixmap::Allocate(ii);
         result->addCpuPlane(pm.pixelStorage(), pm.rowBytes());
 
@@ -786,10 +790,11 @@
                                             std::move(transferResult)};
     auto finishCallback = [](GrGpuFinishedContext c) {
         const auto* context = reinterpret_cast<const FinishContext*>(c);
-        auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
+        auto manager = context->fMappedBufferManager;
+        auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
         size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
         if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
-                                       context->fMappedBufferManager)) {
+                                       manager)) {
             result.reset();
         }
         (*context->fClientCallback)(context->fClientContext, std::move(result));
@@ -1004,7 +1009,7 @@
             callback(callbackContext, nullptr);
             return;
         }
-        auto result = std::make_unique<AsyncReadResult>(dContext->priv().contextID());
+        auto result = std::make_unique<AsyncReadResult>(dContext->directContextID());
         result->addCpuPlane(yPmp.pixelStorage(), yPmp.rowBytes());
         result->addCpuPlane(uPmp.pixelStorage(), uPmp.rowBytes());
         result->addCpuPlane(vPmp.pixelStorage(), vPmp.rowBytes());
@@ -1033,8 +1038,8 @@
                                             std::move(vTransfer)};
     auto finishCallback = [](GrGpuFinishedContext c) {
         const auto* context = reinterpret_cast<const FinishContext*>(c);
-        auto result = std::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
         auto manager = context->fMappedBufferManager;
+        auto result = std::make_unique<AsyncReadResult>(manager->owningDirectContext());
         size_t rowBytes = SkToSizeT(context->fSize.width());
         if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
             (*context->fClientCallback)(context->fClientContext, nullptr);