Debian Codegraph @

src/core/SkMessageBus.h

raw
1 /*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkMessageBus_DEFINED
9 #define SkMessageBus_DEFINED
10
11 #include <type_traits>
12
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkTypes.h"
15 #include "include/private/base/SkMutex.h"
16 #include "include/private/base/SkNoncopyable.h"
17 #include "include/private/base/SkTArray.h"
18 #include "include/private/base/SkTDArray.h"
19
20 /**
21 * The following method must have a specialization for type 'Message':
22 *
23 * bool SkShouldPostMessageToBus(const Message&, IDType msgBusUniqueID)
24 *
25 * We may want to consider providing a default template implementation, to avoid this requirement by
26 * sending to all inboxes when the specialization for type 'Message' is not present.
27 */
28 template <typename Message, typename IDType, bool AllowCopyableMessage = true>
29 class SkMessageBus : SkNoncopyable {
30 public:
31 template <typename T> struct is_sk_sp : std::false_type {};
32 template <typename T> struct is_sk_sp<sk_sp<T>> : std::true_type {};
33
34 // We want to make sure the caller of Post() method will not keep a ref or copy of the message,
35 // so the message type must be sk_sp or non copyable.
36 static_assert(AllowCopyableMessage || is_sk_sp<Message>::value ||
37 !std::is_copy_constructible<Message>::value,
38 "The message type must be sk_sp or non copyable.");
39
40 // Post a message to be received by Inboxes for this Message type. Checks
41 // SkShouldPostMessageToBus() for each inbox. Threadsafe.
42 static void Post(Message m);
43
44 class Inbox {
45 public:
46 Inbox(IDType uniqueID);
47 ~Inbox();
48
49 IDType uniqueID() const { return fUniqueID; }
50
51 // Overwrite out with all the messages we've received since the last call. Threadsafe.
52 void poll(skia_private::TArray<Message>* out);
53
54 private:
55 skia_private::TArray<Message> fMessages;
56 SkMutex fMessagesMutex;
57 const IDType fUniqueID;
58
59 friend class SkMessageBus;
60 void receive(Message m); // SkMessageBus is a friend only to call this.
61 };
62
63 private:
64 SkMessageBus();
65 static SkMessageBus* Get();
66
67 SkTDArray<Inbox*> fInboxes;
68 SkMutex fInboxesMutex;
69 };
70
71 // This must go in a single .cpp file, not some .h, or we risk creating more than one global
72 // SkMessageBus per type when using shared libraries. NOTE: at most one per file will compile.
73 #define DECLARE_SKMESSAGEBUS_MESSAGE(Message, IDType, AllowCopyableMessage) \
74 template <> \
75 SkMessageBus<Message, IDType, AllowCopyableMessage>* \
76 SkMessageBus<Message, IDType, AllowCopyableMessage>::Get() { \
77 static auto* bus = new SkMessageBus<Message, IDType, AllowCopyableMessage>(); \
78 return bus; \
79 }
80
81 // ----------------------- Implementation of SkMessageBus::Inbox -----------------------
82
83 template <typename Message, typename IDType, bool AllowCopyableMessage>
84 SkMessageBus<Message, IDType, AllowCopyableMessage>::Inbox::Inbox(IDType uniqueID)
85 : fUniqueID(uniqueID) {
86 // Register ourselves with the corresponding message bus.
87 auto* bus = SkMessageBus<Message, IDType, AllowCopyableMessage>::Get();
88 SkAutoMutexExclusive lock(bus->fInboxesMutex);
89 bus->fInboxes.push_back(this);
90 }
91
92 template <typename Message, typename IDType, bool AllowCopyableMessage>
93 SkMessageBus<Message, IDType, AllowCopyableMessage>::Inbox::~Inbox() {
94 // Remove ourselves from the corresponding message bus.
95 auto* bus = SkMessageBus<Message, IDType, AllowCopyableMessage>::Get();
96 SkAutoMutexExclusive lock(bus->fInboxesMutex);
97 // This is a cheaper fInboxes.remove(fInboxes.find(this)) when order doesn't matter.
98 for (int i = 0; i < bus->fInboxes.size(); i++) {
99 if (this == bus->fInboxes[i]) {
100 bus->fInboxes.removeShuffle(i);
101 break;
102 }
103 }
104 }
105
106 template <typename Message, typename IDType, bool AllowCopyableMessage>
107 void SkMessageBus<Message, IDType, AllowCopyableMessage>::Inbox::receive(Message m) {
108 SkAutoMutexExclusive lock(fMessagesMutex);
109 fMessages.push_back(std::move(m));
110 }
111
112 template <typename Message, typename IDType, bool AllowCopyableMessage>
113 void SkMessageBus<Message, IDType, AllowCopyableMessage>::Inbox::poll(
114 skia_private::TArray<Message>* messages) {
115 SkASSERT(messages);
116 messages->clear();
117 SkAutoMutexExclusive lock(fMessagesMutex);
118 fMessages.swap(*messages);
119 }
120
121 // ----------------------- Implementation of SkMessageBus -----------------------
122
123 template <typename Message, typename IDType, bool AllowCopyableMessage>
124 SkMessageBus<Message, IDType, AllowCopyableMessage>::SkMessageBus() = default;
125
126 template <typename Message, typename IDType, bool AllowCopyableMessage>
127 /*static*/ void SkMessageBus<Message, IDType, AllowCopyableMessage>::Post(Message m) {
128 auto* bus = SkMessageBus<Message, IDType, AllowCopyableMessage>::Get();
129 SkAutoMutexExclusive lock(bus->fInboxesMutex);
130 for (int i = 0; i < bus->fInboxes.size(); i++) {
131 if (SkShouldPostMessageToBus(m, bus->fInboxes[i]->fUniqueID)) {
132 if constexpr (AllowCopyableMessage) {
133 bus->fInboxes[i]->receive(m);
134 } else {
135 if constexpr (is_sk_sp<Message>::value) {
136 SkASSERT(m->unique());
137 }
138 bus->fInboxes[i]->receive(std::move(m));
139 break;
140 }
141 }
142 }
143
144 if constexpr (is_sk_sp<Message>::value && !AllowCopyableMessage) {
145 // Make sure sk_sp has been sent to an inbox.
146 SkASSERT(!m); // NOLINT(bugprone-use-after-move)
147 }
148 }
149
150 #endif // SkMessageBus_DEFINED