Expand SkMessageBus to support different unique key types

Bug: skia:11728
Change-Id: I16fb8250fa5c04ce3fe369a50d0c61a0bee46811
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/383696
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/src/core/SkMessageBus.h b/src/core/SkMessageBus.h
index c6fe957..bbbf06a 100644
--- a/src/core/SkMessageBus.h
+++ b/src/core/SkMessageBus.h
@@ -21,12 +21,13 @@
 /**
  * The following method must have a specialization for type 'Message':
  *
- *     bool SkShouldPostMessageToBus(const Message&, uint32_t msgBusUniqueID)
+ *     bool SkShouldPostMessageToBus(const Message&, IDType msgBusUniqueID)
  *
  * We may want to consider providing a default template implementation, to avoid this requirement by
  * sending to all inboxes when the specialization for type 'Message' is not present.
  */
-template <typename Message, bool AllowCopyableMessage = true> class SkMessageBus : SkNoncopyable {
+template <typename Message, typename IDType, bool AllowCopyableMessage = true>
+class SkMessageBus : SkNoncopyable {
 public:
     template <typename T> struct is_sk_sp : std::false_type {};
     template <typename T> struct is_sk_sp<sk_sp<T>> : std::true_type {};
@@ -43,18 +44,18 @@
 
     class Inbox {
     public:
-        Inbox(uint32_t uniqueID = SK_InvalidUniqueID);
+        Inbox(IDType uniqueID);
         ~Inbox();
 
-        uint32_t uniqueID() const { return fUniqueID; }
+        IDType uniqueID() const { return fUniqueID; }
 
         // Overwrite out with all the messages we've received since the last call.  Threadsafe.
         void poll(SkTArray<Message>* out);
 
     private:
-        SkTArray<Message>  fMessages;
-        SkMutex            fMessagesMutex;
-        const uint32_t fUniqueID;
+        SkTArray<Message> fMessages;
+        SkMutex           fMessagesMutex;
+        const IDType      fUniqueID;
 
         friend class SkMessageBus;
         void receive(Message m);  // SkMessageBus is a friend only to call this.
@@ -70,30 +71,31 @@
 
 // This must go in a single .cpp file, not some .h, or we risk creating more than one global
 // SkMessageBus per type when using shared libraries.  NOTE: at most one per file will compile.
-#define DECLARE_SKMESSAGEBUS_MESSAGE(Message, AllowCopyableMessage)            \
-    template <>                                                                \
-    SkMessageBus<Message, AllowCopyableMessage>*                               \
-    SkMessageBus<Message, AllowCopyableMessage>::Get() {                       \
-        static SkOnce once;                                                    \
-        static SkMessageBus<Message, AllowCopyableMessage>* bus;               \
-        once([] { bus = new SkMessageBus<Message, AllowCopyableMessage>(); }); \
-        return bus;                                                            \
+#define DECLARE_SKMESSAGEBUS_MESSAGE(Message, IDType, AllowCopyableMessage)            \
+    template <>                                                                        \
+    SkMessageBus<Message, IDType, AllowCopyableMessage>*                               \
+    SkMessageBus<Message, IDType, AllowCopyableMessage>::Get() {                       \
+        static SkOnce once;                                                            \
+        static SkMessageBus<Message, IDType, AllowCopyableMessage>* bus;               \
+        once([] { bus = new SkMessageBus<Message, IDType, AllowCopyableMessage>(); }); \
+        return bus;                                                                    \
     }
 
 //   ----------------------- Implementation of SkMessageBus::Inbox -----------------------
 
-template <typename Message, bool AllowCopyableMessage>
-SkMessageBus<Message, AllowCopyableMessage>::Inbox::Inbox(uint32_t uniqueID) : fUniqueID(uniqueID) {
+template <typename Message, typename IDType, bool AllowCopyableMessage>
+SkMessageBus<Message, IDType, AllowCopyableMessage>::Inbox::Inbox(IDType uniqueID)
+        : fUniqueID(uniqueID) {
     // Register ourselves with the corresponding message bus.
-    auto* bus = SkMessageBus<Message, AllowCopyableMessage>::Get();
+    auto* bus = SkMessageBus<Message, IDType, AllowCopyableMessage>::Get();
     SkAutoMutexExclusive lock(bus->fInboxesMutex);
     bus->fInboxes.push_back(this);
 }
 
-template <typename Message, bool AllowCopyableMessage>
-SkMessageBus<Message, AllowCopyableMessage>::Inbox::~Inbox() {
+template <typename Message, typename IDType, bool AllowCopyableMessage>
+SkMessageBus<Message, IDType, AllowCopyableMessage>::Inbox::~Inbox() {
     // Remove ourselves from the corresponding message bus.
-    auto* bus = SkMessageBus<Message, AllowCopyableMessage>::Get();
+    auto* bus = SkMessageBus<Message, IDType, AllowCopyableMessage>::Get();
     SkAutoMutexExclusive lock(bus->fInboxesMutex);
     // This is a cheaper fInboxes.remove(fInboxes.find(this)) when order doesn't matter.
     for (int i = 0; i < bus->fInboxes.count(); i++) {
@@ -104,14 +106,14 @@
     }
 }
 
-template <typename Message, bool AllowCopyableMessage>
-void SkMessageBus<Message, AllowCopyableMessage>::Inbox::receive(Message m) {
+template <typename Message, typename IDType, bool AllowCopyableMessage>
+void SkMessageBus<Message, IDType, AllowCopyableMessage>::Inbox::receive(Message m) {
     SkAutoMutexExclusive lock(fMessagesMutex);
     fMessages.push_back(std::move(m));
 }
 
-template <typename Message, bool AllowCopyableMessage>
-void SkMessageBus<Message, AllowCopyableMessage>::Inbox::poll(SkTArray<Message>* messages) {
+template <typename Message, typename IDType, bool AllowCopyableMessage>
+void SkMessageBus<Message, IDType, AllowCopyableMessage>::Inbox::poll(SkTArray<Message>* messages) {
     SkASSERT(messages);
     messages->reset();
     SkAutoMutexExclusive lock(fMessagesMutex);
@@ -120,12 +122,12 @@
 
 //   ----------------------- Implementation of SkMessageBus -----------------------
 
-template <typename Message, bool AllowCopyableMessage>
-SkMessageBus<Message, AllowCopyableMessage>::SkMessageBus() = default;
+template <typename Message, typename IDType, bool AllowCopyableMessage>
+SkMessageBus<Message, IDType, AllowCopyableMessage>::SkMessageBus() = default;
 
-template <typename Message, bool AllowCopyableMessage>
-/*static*/ void SkMessageBus<Message, AllowCopyableMessage>::Post(Message m) {
-    auto* bus = SkMessageBus<Message, AllowCopyableMessage>::Get();
+template <typename Message, typename IDType, bool AllowCopyableMessage>
+/*static*/ void SkMessageBus<Message, IDType, AllowCopyableMessage>::Post(Message m) {
+    auto* bus = SkMessageBus<Message, IDType, AllowCopyableMessage>::Get();
     SkAutoMutexExclusive lock(bus->fInboxesMutex);
     for (int i = 0; i < bus->fInboxes.count(); i++) {
         if (SkShouldPostMessageToBus(m, bus->fInboxes[i]->fUniqueID)) {
diff --git a/src/core/SkPromiseImageTexture.cpp b/src/core/SkPromiseImageTexture.cpp
index 6d627f5..e14f217 100644
--- a/src/core/SkPromiseImageTexture.cpp
+++ b/src/core/SkPromiseImageTexture.cpp
@@ -20,7 +20,7 @@
 
 SkPromiseImageTexture::~SkPromiseImageTexture() {
     for (const auto& msg : fMessages) {
-        SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(msg);
+        SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(msg);
     }
 }
 
diff --git a/src/core/SkResourceCache.cpp b/src/core/SkResourceCache.cpp
index ae100a4..016c38c 100644
--- a/src/core/SkResourceCache.cpp
+++ b/src/core/SkResourceCache.cpp
@@ -19,7 +19,7 @@
 #include <stddef.h>
 #include <stdlib.h>
 
-DECLARE_SKMESSAGEBUS_MESSAGE(SkResourceCache::PurgeSharedIDMessage, true)
+DECLARE_SKMESSAGEBUS_MESSAGE(SkResourceCache::PurgeSharedIDMessage, uint32_t, true)
 
 static inline bool SkShouldPostMessageToBus(
         const SkResourceCache::PurgeSharedIDMessage&, uint32_t) {
@@ -91,12 +91,14 @@
     fDiscardableFactory = nullptr;
 }
 
-SkResourceCache::SkResourceCache(DiscardableFactory factory) {
+SkResourceCache::SkResourceCache(DiscardableFactory factory)
+        : fPurgeSharedIDInbox(SK_InvalidUniqueID) {
     this->init();
     fDiscardableFactory = factory;
 }
 
-SkResourceCache::SkResourceCache(size_t byteLimit) {
+SkResourceCache::SkResourceCache(size_t byteLimit)
+        : fPurgeSharedIDInbox(SK_InvalidUniqueID) {
     this->init();
     fTotalByteLimit = byteLimit;
 }
@@ -542,7 +544,7 @@
 
 void SkResourceCache::PostPurgeSharedID(uint64_t sharedID) {
     if (sharedID) {
-        SkMessageBus<PurgeSharedIDMessage>::Post(PurgeSharedIDMessage(sharedID));
+        SkMessageBus<PurgeSharedIDMessage, uint32_t>::Post(PurgeSharedIDMessage(sharedID));
     }
 }
 
diff --git a/src/core/SkResourceCache.h b/src/core/SkResourceCache.h
index c59e95d..a0cc754 100644
--- a/src/core/SkResourceCache.h
+++ b/src/core/SkResourceCache.h
@@ -271,7 +271,7 @@
     size_t  fSingleAllocationByteLimit;
     int     fCount;
 
-    SkMessageBus<PurgeSharedIDMessage>::Inbox fPurgeSharedIDInbox;
+    SkMessageBus<PurgeSharedIDMessage, uint32_t>::Inbox fPurgeSharedIDInbox;
 
     void checkMessages();
     void purgeAsNeeded(bool forcePurge = false);
diff --git a/src/gpu/GrBackendTextureImageGenerator.cpp b/src/gpu/GrBackendTextureImageGenerator.cpp
index 4e08ecc..620e18c 100644
--- a/src/gpu/GrBackendTextureImageGenerator.cpp
+++ b/src/gpu/GrBackendTextureImageGenerator.cpp
@@ -37,7 +37,7 @@
     // Generator has been freed, and no one is borrowing the texture. Notify the original cache
     // that it can free the last ref, so it happens on the correct thread.
     GrTextureFreedMessage msg { fOriginalTexture, fOwningContextID };
-    SkMessageBus<GrTextureFreedMessage>::Post(msg);
+    SkMessageBus<GrTextureFreedMessage, uint32_t>::Post(msg);
 }
 
 std::unique_ptr<SkImageGenerator>
diff --git a/src/gpu/GrClientMappedBufferManager.cpp b/src/gpu/GrClientMappedBufferManager.cpp
index 1dd3860..10fa4d5 100644
--- a/src/gpu/GrClientMappedBufferManager.cpp
+++ b/src/gpu/GrClientMappedBufferManager.cpp
@@ -63,7 +63,7 @@
 
 //////////////////////////////////////////////////////////////////////////////
 
-DECLARE_SKMESSAGEBUS_MESSAGE(GrClientMappedBufferManager::BufferFinishedMessage, false)
+DECLARE_SKMESSAGEBUS_MESSAGE(GrClientMappedBufferManager::BufferFinishedMessage, uint32_t, false)
 
 bool SkShouldPostMessageToBus(const GrClientMappedBufferManager::BufferFinishedMessage& m,
                               uint32_t msgBusUniqueID) {
diff --git a/src/gpu/GrClientMappedBufferManager.h b/src/gpu/GrClientMappedBufferManager.h
index 20ef457..9ba0954 100644
--- a/src/gpu/GrClientMappedBufferManager.h
+++ b/src/gpu/GrClientMappedBufferManager.h
@@ -39,7 +39,7 @@
         sk_sp<GrGpuBuffer> fBuffer;
         uint32_t fInboxID;
     };
-    using BufferFinishedMessageBus = SkMessageBus<BufferFinishedMessage, false>;
+    using BufferFinishedMessageBus = SkMessageBus<BufferFinishedMessage, uint32_t, false>;
 
     GrClientMappedBufferManager(uint32_t contextID);
     GrClientMappedBufferManager(const GrClientMappedBufferManager&) = delete;
diff --git a/src/gpu/GrDirectContext.cpp b/src/gpu/GrDirectContext.cpp
index 535538d..19722bb 100644
--- a/src/gpu/GrDirectContext.cpp
+++ b/src/gpu/GrDirectContext.cpp
@@ -51,7 +51,7 @@
 
 #define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(this->singleOwner())
 
-GrDirectContext::DirectContextID GrDirectContext::DirectContextID::NextID() {
+GrDirectContext::DirectContextID GrDirectContext::DirectContextID::Next() {
     static std::atomic<uint32_t> nextID{1};
     uint32_t id;
     do {
@@ -62,7 +62,7 @@
 
 GrDirectContext::GrDirectContext(GrBackendApi backend, const GrContextOptions& options)
         : INHERITED(GrContextThreadSafeProxyPriv::Make(backend, options))
-        , fDirectContextID(DirectContextID::NextID()) {
+        , fDirectContextID(DirectContextID::Next()) {
 }
 
 GrDirectContext::~GrDirectContext() {
diff --git a/src/gpu/GrResourceCache.cpp b/src/gpu/GrResourceCache.cpp
index f0b09b9..ac22385 100644
--- a/src/gpu/GrResourceCache.cpp
+++ b/src/gpu/GrResourceCache.cpp
@@ -25,9 +25,9 @@
 #include "src/gpu/GrTracing.h"
 #include "src/gpu/SkGr.h"
 
-DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage, true);
+DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage, uint32_t, true);
 
-DECLARE_SKMESSAGEBUS_MESSAGE(GrTextureFreedMessage, true);
+DECLARE_SKMESSAGEBUS_MESSAGE(GrTextureFreedMessage, uint32_t, true);
 
 #define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(fSingleOwner)
 
diff --git a/src/gpu/GrResourceCache.h b/src/gpu/GrResourceCache.h
index 2c13917..e1706a3 100644
--- a/src/gpu/GrResourceCache.h
+++ b/src/gpu/GrResourceCache.h
@@ -321,8 +321,8 @@
         return res->cacheAccess().accessCacheIndex();
     }
 
-    typedef SkMessageBus<GrUniqueKeyInvalidatedMessage>::Inbox InvalidUniqueKeyInbox;
-    typedef SkMessageBus<GrTextureFreedMessage>::Inbox FreedTextureInbox;
+    typedef SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Inbox InvalidUniqueKeyInbox;
+    typedef SkMessageBus<GrTextureFreedMessage, uint32_t>::Inbox FreedTextureInbox;
     typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
     typedef SkTDArray<GrGpuResource*> ResourceArray;
 
diff --git a/src/gpu/SkGr.cpp b/src/gpu/SkGr.cpp
index 6d85242..89220b3 100644
--- a/src/gpu/SkGr.cpp
+++ b/src/gpu/SkGr.cpp
@@ -69,7 +69,9 @@
     public:
         Listener(const GrUniqueKey& key, uint32_t contextUniqueID) : fMsg(key, contextUniqueID) {}
 
-        void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
+        void changed() override {
+            SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(fMsg);
+        }
 
     private:
         GrUniqueKeyInvalidatedMessage fMsg;
diff --git a/src/gpu/ccpr/GrCCPathCache.cpp b/src/gpu/ccpr/GrCCPathCache.cpp
index 18c4e7f..4f16d80 100644
--- a/src/gpu/ccpr/GrCCPathCache.cpp
+++ b/src/gpu/ccpr/GrCCPathCache.cpp
@@ -13,7 +13,7 @@
 
 static constexpr int kMaxKeyDataCountU32 = 256;  // 1kB of uint32_t's.
 
-DECLARE_SKMESSAGEBUS_MESSAGE(sk_sp<GrCCPathCache::Key>, true);
+DECLARE_SKMESSAGEBUS_MESSAGE(sk_sp<GrCCPathCache::Key>, uint32_t, true);
 
 static inline uint32_t next_path_cache_id() {
     static std::atomic<uint32_t> gNextID(1);
@@ -89,7 +89,7 @@
 
 void GrCCPathCache::Key::changed() {
     // Our key's corresponding path was invalidated. Post a thread-safe eviction message.
-    SkMessageBus<sk_sp<Key>>::Post(sk_ref_sp(this));
+    SkMessageBus<sk_sp<Key>, uint32_t>::Post(sk_ref_sp(this));
 }
 
 GrCCPathCache::GrCCPathCache(uint32_t contextUniqueID)
@@ -107,11 +107,11 @@
     // Now take all the atlas textures we just invalidated and purge them from the GrResourceCache.
     // We just purge via message bus since we don't have any access to the resource cache right now.
     for (const sk_sp<GrTextureProxy>& proxy : fInvalidatedProxies) {
-        SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(
+        SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(
                 GrUniqueKeyInvalidatedMessage(proxy->getUniqueKey(), fContextUniqueID));
     }
     for (const GrUniqueKey& key : fInvalidatedProxyUniqueKeys) {
-        SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(
+        SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(
                 GrUniqueKeyInvalidatedMessage(key, fContextUniqueID));
     }
 }
diff --git a/src/gpu/ccpr/GrCCPathCache.h b/src/gpu/ccpr/GrCCPathCache.h
index ae9925c..6a521ae 100644
--- a/src/gpu/ccpr/GrCCPathCache.h
+++ b/src/gpu/ccpr/GrCCPathCache.h
@@ -172,7 +172,7 @@
 
     SkTHashTable<HashNode, const Key&> fHashTable;
     SkTInternalLList<GrCCPathCacheEntry> fLRU;
-    SkMessageBus<sk_sp<Key>>::Inbox fInvalidatedKeysInbox;
+    SkMessageBus<sk_sp<Key>, uint32_t>::Inbox fInvalidatedKeysInbox;
     sk_sp<Key> fScratchKey;  // Reused for creating a temporary key in the find() method.
 
     // We only read the clock once per flush, and cache it in this variable. This prevents us from
diff --git a/src/gpu/ops/GrTriangulatingPathRenderer.cpp b/src/gpu/ops/GrTriangulatingPathRenderer.cpp
index 495f96d..f3b9ef5 100644
--- a/src/gpu/ops/GrTriangulatingPathRenderer.cpp
+++ b/src/gpu/ops/GrTriangulatingPathRenderer.cpp
@@ -89,7 +89,7 @@
 private:
     GrUniqueKeyInvalidatedMessage fMsg;
 
-    void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
+    void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(fMsg); }
 };
 
 class StaticVertexAllocator : public GrEagerVertexAllocator {
diff --git a/src/gpu/text/GrTextBlobCache.cpp b/src/gpu/text/GrTextBlobCache.cpp
index de94501..e7cca26 100644
--- a/src/gpu/text/GrTextBlobCache.cpp
+++ b/src/gpu/text/GrTextBlobCache.cpp
@@ -7,7 +7,7 @@
 
 #include "src/gpu/text/GrTextBlobCache.h"
 
-DECLARE_SKMESSAGEBUS_MESSAGE(GrTextBlobCache::PurgeBlobMessage, true)
+DECLARE_SKMESSAGEBUS_MESSAGE(GrTextBlobCache::PurgeBlobMessage, uint32_t, true)
 
 // This function is captured by the above macro using implementations from SkMessageBus.h
 static inline bool SkShouldPostMessageToBus(
@@ -75,7 +75,7 @@
 
 void GrTextBlobCache::PostPurgeBlobMessage(uint32_t blobID, uint32_t cacheID) {
     SkASSERT(blobID != SK_InvalidGenID);
-    SkMessageBus<PurgeBlobMessage>::Post(PurgeBlobMessage(blobID, cacheID));
+    SkMessageBus<PurgeBlobMessage, uint32_t>::Post(PurgeBlobMessage(blobID, cacheID));
 }
 
 void GrTextBlobCache::purgeStaleBlobs() {
diff --git a/src/gpu/text/GrTextBlobCache.h b/src/gpu/text/GrTextBlobCache.h
index 8e86092d..adff3b9 100644
--- a/src/gpu/text/GrTextBlobCache.h
+++ b/src/gpu/text/GrTextBlobCache.h
@@ -89,7 +89,7 @@
 
     // In practice 'messageBusID' is always the unique ID of the owning GrContext
     const uint32_t fMessageBusID;
-    SkMessageBus<PurgeBlobMessage>::Inbox fPurgeBlobInbox SK_GUARDED_BY(fSpinLock);
+    SkMessageBus<PurgeBlobMessage, uint32_t>::Inbox fPurgeBlobInbox SK_GUARDED_BY(fSpinLock);
 };
 
 #endif
diff --git a/src/image/SkImage_GpuBase.cpp b/src/image/SkImage_GpuBase.cpp
index 840c6bc..ecaabbb 100644
--- a/src/image/SkImage_GpuBase.cpp
+++ b/src/image/SkImage_GpuBase.cpp
@@ -259,7 +259,7 @@
             // In the future the GrSurface class hierarchy refactoring should eliminate this
             // difficulty by removing the virtual inheritance.
             if (fTexture) {
-                SkMessageBus<GrTextureFreedMessage>::Post({fTexture, fTextureContextID});
+                SkMessageBus<GrTextureFreedMessage, uint32_t>::Post({fTexture, fTextureContextID});
             }
         }