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>
This commit is contained in:
parent
c737a83709
commit
e7a959d8fe
@ -793,7 +793,7 @@ public:
|
||||
|
||||
class DirectContextID {
|
||||
public:
|
||||
static GrDirectContext::DirectContextID NextID();
|
||||
static GrDirectContext::DirectContextID Next();
|
||||
|
||||
DirectContextID() : fID(SK_InvalidUniqueID) {}
|
||||
|
||||
|
@ -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 @@ public:
|
||||
|
||||
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 @@ private:
|
||||
|
||||
// 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 @@ SkMessageBus<Message, AllowCopyableMessage>::Inbox::~Inbox() {
|
||||
}
|
||||
}
|
||||
|
||||
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 @@ void SkMessageBus<Message, AllowCopyableMessage>::Inbox::poll(SkTArray<Message>*
|
||||
|
||||
// ----------------------- 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)) {
|
||||
|
@ -20,7 +20,7 @@ SkPromiseImageTexture::SkPromiseImageTexture(const GrBackendTexture& backendText
|
||||
|
||||
SkPromiseImageTexture::~SkPromiseImageTexture() {
|
||||
for (const auto& msg : fMessages) {
|
||||
SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(msg);
|
||||
SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 @@ void SkResourceCache::init() {
|
||||
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::VisitAll(Visitor visitor, void* context) {
|
||||
|
||||
void SkResourceCache::PostPurgeSharedID(uint64_t sharedID) {
|
||||
if (sharedID) {
|
||||
SkMessageBus<PurgeSharedIDMessage>::Post(PurgeSharedIDMessage(sharedID));
|
||||
SkMessageBus<PurgeSharedIDMessage, uint32_t>::Post(PurgeSharedIDMessage(sharedID));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -271,7 +271,7 @@ private:
|
||||
size_t fSingleAllocationByteLimit;
|
||||
int fCount;
|
||||
|
||||
SkMessageBus<PurgeSharedIDMessage>::Inbox fPurgeSharedIDInbox;
|
||||
SkMessageBus<PurgeSharedIDMessage, uint32_t>::Inbox fPurgeSharedIDInbox;
|
||||
|
||||
void checkMessages();
|
||||
void purgeAsNeeded(bool forcePurge = false);
|
||||
|
@ -37,7 +37,7 @@ GrBackendTextureImageGenerator::RefHelper::~RefHelper() {
|
||||
// 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>
|
||||
|
@ -63,7 +63,7 @@ void GrClientMappedBufferManager::remove(const sk_sp<GrGpuBuffer>& b) {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
DECLARE_SKMESSAGEBUS_MESSAGE(GrClientMappedBufferManager::BufferFinishedMessage, false)
|
||||
DECLARE_SKMESSAGEBUS_MESSAGE(GrClientMappedBufferManager::BufferFinishedMessage, uint32_t, false)
|
||||
|
||||
bool SkShouldPostMessageToBus(const GrClientMappedBufferManager::BufferFinishedMessage& m,
|
||||
uint32_t msgBusUniqueID) {
|
||||
|
@ -39,7 +39,7 @@ public:
|
||||
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;
|
||||
|
@ -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::DirectContextID GrDirectContext::DirectContextID::NextID() {
|
||||
|
||||
GrDirectContext::GrDirectContext(GrBackendApi backend, const GrContextOptions& options)
|
||||
: INHERITED(GrContextThreadSafeProxyPriv::Make(backend, options))
|
||||
, fDirectContextID(DirectContextID::NextID()) {
|
||||
, fDirectContextID(DirectContextID::Next()) {
|
||||
}
|
||||
|
||||
GrDirectContext::~GrDirectContext() {
|
||||
|
@ -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)
|
||||
|
||||
|
@ -321,8 +321,8 @@ private:
|
||||
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;
|
||||
|
||||
|
@ -69,7 +69,9 @@ sk_sp<SkIDChangeListener> GrMakeUniqueKeyInvalidationListener(GrUniqueKey* key,
|
||||
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;
|
||||
|
@ -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 @@ uint32_t* GrCCPathCache::Key::data() {
|
||||
|
||||
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 @@ GrCCPathCache::~GrCCPathCache() {
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
|
@ -172,7 +172,7 @@ private:
|
||||
|
||||
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
|
||||
|
@ -89,7 +89,7 @@ public:
|
||||
private:
|
||||
GrUniqueKeyInvalidatedMessage fMsg;
|
||||
|
||||
void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
|
||||
void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(fMsg); }
|
||||
};
|
||||
|
||||
class StaticVertexAllocator : public GrEagerVertexAllocator {
|
||||
|
@ -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::freeAll() {
|
||||
|
||||
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() {
|
||||
|
@ -89,7 +89,7 @@ private:
|
||||
|
||||
// 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
|
||||
|
@ -259,7 +259,7 @@ sk_sp<GrTextureProxy> SkImage_GpuBase::MakePromiseImageLazyProxy(
|
||||
// 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});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1431,7 +1431,7 @@ static void test_15(GrDirectContext* dContext, skiatest::Reporter* reporter,
|
||||
GrUniqueKeyInvalidatedMessage msg(key, dContext->priv().contextID(),
|
||||
/* inThreadSafeCache */ true);
|
||||
|
||||
SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(msg);
|
||||
SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(msg);
|
||||
|
||||
// This purge call is needed to process the invalidation messages
|
||||
dContext->purgeUnlockedResources(/* scratchResourcesOnly */ true);
|
||||
|
@ -5,6 +5,7 @@
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "include/gpu/GrDirectContext.h"
|
||||
#include "src/core/SkMessageBus.h"
|
||||
#include "tests/Test.h"
|
||||
|
||||
@ -23,12 +24,12 @@ static inline bool SkShouldPostMessageToBus(const TestMessage&, uint32_t) {
|
||||
|
||||
} // namespace
|
||||
|
||||
DECLARE_SKMESSAGEBUS_MESSAGE(TestMessage, true)
|
||||
DECLARE_SKMESSAGEBUS_MESSAGE(TestMessage, uint32_t, true)
|
||||
|
||||
DEF_TEST(MessageBus, r) {
|
||||
using TestMessageBus = SkMessageBus<TestMessage>;
|
||||
using TestMessageBus = SkMessageBus<TestMessage, uint32_t>;
|
||||
// Register two inboxes to receive all TestMessages.
|
||||
TestMessageBus::Inbox inbox1, inbox2;
|
||||
TestMessageBus::Inbox inbox1(0), inbox2(0);
|
||||
|
||||
// Send two messages.
|
||||
const TestMessage m1 = { 5, 4.2f };
|
||||
@ -77,12 +78,12 @@ static inline bool SkShouldPostMessageToBus(const sk_sp<TestMessageRefCnt>&, uin
|
||||
|
||||
} // namespace
|
||||
|
||||
DECLARE_SKMESSAGEBUS_MESSAGE(sk_sp<TestMessageRefCnt>, false)
|
||||
DECLARE_SKMESSAGEBUS_MESSAGE(sk_sp<TestMessageRefCnt>, uint32_t, false)
|
||||
|
||||
DEF_TEST(MessageBusSp, r) {
|
||||
// Register two inboxes to receive all TestMessages.
|
||||
using TestMessageBus = SkMessageBus<sk_sp<TestMessageRefCnt>, false>;
|
||||
TestMessageBus::Inbox inbox1;
|
||||
using TestMessageBus = SkMessageBus<sk_sp<TestMessageRefCnt>, uint32_t, false>;
|
||||
TestMessageBus::Inbox inbox1(0);
|
||||
|
||||
// Send two messages.
|
||||
auto m1 = sk_make_sp<TestMessageRefCnt>(5, 4.2f);
|
||||
@ -119,12 +120,13 @@ DEF_TEST(MessageBusSp, r) {
|
||||
namespace {
|
||||
|
||||
struct AddressedMessage {
|
||||
uint32_t fInboxID;
|
||||
GrDirectContext::DirectContextID fInboxID;
|
||||
};
|
||||
|
||||
static inline bool SkShouldPostMessageToBus(const AddressedMessage& msg, uint32_t msgBusUniqueID) {
|
||||
SkASSERT(msgBusUniqueID);
|
||||
if (!msg.fInboxID) {
|
||||
static inline bool SkShouldPostMessageToBus(const AddressedMessage& msg,
|
||||
GrDirectContext::DirectContextID msgBusUniqueID) {
|
||||
SkASSERT(msgBusUniqueID.isValid());
|
||||
if (!msg.fInboxID.isValid()) {
|
||||
return true;
|
||||
}
|
||||
return msgBusUniqueID == msg.fInboxID;
|
||||
@ -132,29 +134,36 @@ static inline bool SkShouldPostMessageToBus(const AddressedMessage& msg, uint32_
|
||||
|
||||
} // namespace
|
||||
|
||||
DECLARE_SKMESSAGEBUS_MESSAGE(AddressedMessage, true)
|
||||
DECLARE_SKMESSAGEBUS_MESSAGE(AddressedMessage, GrDirectContext::DirectContextID, true)
|
||||
|
||||
DEF_TEST(MessageBus_SkShouldPostMessageToBus, r) {
|
||||
using AddressedMessageBus = SkMessageBus<AddressedMessage>;
|
||||
AddressedMessageBus::Inbox inbox1(1), inbox2(2);
|
||||
using ID = GrDirectContext::DirectContextID;
|
||||
using AddressedMessageBus = SkMessageBus<AddressedMessage, ID>;
|
||||
|
||||
AddressedMessageBus::Post({0}); // Should go to both
|
||||
AddressedMessageBus::Post({1}); // Should go to inbox1
|
||||
AddressedMessageBus::Post({2}); // Should go to inbox2
|
||||
AddressedMessageBus::Post({3}); // Should go nowhere
|
||||
ID idInvalid;
|
||||
ID id1 = ID::Next(),
|
||||
id2 = ID::Next(),
|
||||
id3 = ID::Next();
|
||||
|
||||
AddressedMessageBus::Inbox inbox1(id1), inbox2(id2);
|
||||
|
||||
AddressedMessageBus::Post({idInvalid}); // Should go to both
|
||||
AddressedMessageBus::Post({id1}); // Should go to inbox1
|
||||
AddressedMessageBus::Post({id2}); // Should go to inbox2
|
||||
AddressedMessageBus::Post({id3}); // Should go nowhere
|
||||
|
||||
SkTArray<AddressedMessage> messages;
|
||||
inbox1.poll(&messages);
|
||||
REPORTER_ASSERT(r, messages.count() == 2);
|
||||
if (messages.count() == 2) {
|
||||
REPORTER_ASSERT(r, messages[0].fInboxID == 0);
|
||||
REPORTER_ASSERT(r, messages[1].fInboxID == 1);
|
||||
REPORTER_ASSERT(r, !messages[0].fInboxID.isValid());
|
||||
REPORTER_ASSERT(r, messages[1].fInboxID == id1);
|
||||
}
|
||||
inbox2.poll(&messages);
|
||||
REPORTER_ASSERT(r, messages.count() == 2);
|
||||
if (messages.count() == 2) {
|
||||
REPORTER_ASSERT(r, messages[0].fInboxID == 0);
|
||||
REPORTER_ASSERT(r, messages[1].fInboxID == 2);
|
||||
REPORTER_ASSERT(r, !messages[0].fInboxID.isValid());
|
||||
REPORTER_ASSERT(r, messages[1].fInboxID == id2);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1080,7 +1080,7 @@ static void test_purge_invalidated(skiatest::Reporter* reporter) {
|
||||
REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive());
|
||||
|
||||
typedef GrUniqueKeyInvalidatedMessage Msg;
|
||||
typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus;
|
||||
typedef SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t> Bus;
|
||||
|
||||
// Invalidate two of the three, they should be purged and no longer accessible via their keys.
|
||||
Bus::Post(Msg(key1, dContext->priv().contextID()));
|
||||
@ -1562,14 +1562,14 @@ static void test_free_texture_messages(skiatest::Reporter* reporter) {
|
||||
|
||||
// Send message to free the first resource
|
||||
GrTextureFreedMessage msg1{wrapped[0], dContext->priv().contextID()};
|
||||
SkMessageBus<GrTextureFreedMessage>::Post(msg1);
|
||||
SkMessageBus<GrTextureFreedMessage, uint32_t>::Post(msg1);
|
||||
cache->purgeAsNeeded();
|
||||
|
||||
REPORTER_ASSERT(reporter, 1 == (freed[0] + freed[1] + freed[2]));
|
||||
REPORTER_ASSERT(reporter, 1 == freed[0]);
|
||||
|
||||
GrTextureFreedMessage msg2{wrapped[2], dContext->priv().contextID()};
|
||||
SkMessageBus<GrTextureFreedMessage>::Post(msg2);
|
||||
SkMessageBus<GrTextureFreedMessage, uint32_t>::Post(msg2);
|
||||
cache->purgeAsNeeded();
|
||||
|
||||
REPORTER_ASSERT(reporter, 2 == (freed[0] + freed[1] + freed[2]));
|
||||
@ -1645,7 +1645,7 @@ DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ResourceMessagesAfterAbandon, reporter, ctxIn
|
||||
// In the past, creating this message could cause an exception due to
|
||||
// an un-safe downcast from GrTexture to GrGpuResource
|
||||
GrTextureFreedMessage msg{tex, context->priv().contextID()};
|
||||
SkMessageBus<GrTextureFreedMessage>::Post(msg);
|
||||
SkMessageBus<GrTextureFreedMessage, uint32_t>::Post(msg);
|
||||
|
||||
// This doesn't actually do anything but it does trigger us to read messages
|
||||
context->purgeUnlockedResources(false);
|
||||
|
@ -215,7 +215,7 @@ static void basic_test(GrDirectContext* dContext,
|
||||
if (expectResourceToOutliveProxy) {
|
||||
proxy.reset();
|
||||
GrUniqueKeyInvalidatedMessage msg(texKey, dContext->priv().contextID());
|
||||
SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(msg);
|
||||
SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(msg);
|
||||
cache->purgeAsNeeded();
|
||||
expectedCacheCount -= cacheEntriesPerProxy;
|
||||
proxy = proxyProvider->findOrCreateProxyByUniqueKey(key);
|
||||
@ -312,7 +312,7 @@ static void invalidation_and_instantiation_test(GrDirectContext* dContext,
|
||||
SkAssertResult(proxyProvider->assignUniqueKeyToProxy(key, proxy.get()));
|
||||
|
||||
// Send an invalidation message, which will be sitting in the cache's inbox
|
||||
SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(
|
||||
SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(
|
||||
GrUniqueKeyInvalidatedMessage(key, dContext->priv().contextID()));
|
||||
|
||||
REPORTER_ASSERT(reporter, 1 == proxyProvider->numUniqueKeyProxies_TestOnly());
|
||||
|
Loading…
Reference in New Issue
Block a user