OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkEventSink.h" | 10 #include "SkEventSink.h" |
11 #include "SkMutex.h" | 11 #include "SkMutex.h" |
12 #include "SkTagList.h" | 12 #include "SkTagList.h" |
13 #include "SkTime.h" | 13 #include "SkTime.h" |
14 | 14 |
15 class SkEventSink_Globals { | 15 class SkEventSink_Globals { |
16 public: | 16 public: |
17 SkEventSink_Globals() { | 17 SkEventSink_Globals() { |
18 fNextSinkID = 0; | 18 fNextSinkID = 0; |
19 fSinkHead = NULL; | 19 fSinkHead = nullptr; |
20 } | 20 } |
21 | 21 |
22 SkMutex fSinkMutex; | 22 SkMutex fSinkMutex; |
23 SkEventSinkID fNextSinkID; | 23 SkEventSinkID fNextSinkID; |
24 SkEventSink* fSinkHead; | 24 SkEventSink* fSinkHead; |
25 }; | 25 }; |
26 | 26 |
27 static SkEventSink_Globals& getGlobals() { | 27 static SkEventSink_Globals& getGlobals() { |
28 // leak this, so we don't incur any shutdown perf hit | 28 // leak this, so we don't incur any shutdown perf hit |
29 static SkEventSink_Globals* gGlobals = new SkEventSink_Globals; | 29 static SkEventSink_Globals* gGlobals = new SkEventSink_Globals; |
30 return *gGlobals; | 30 return *gGlobals; |
31 } | 31 } |
32 | 32 |
33 SkEventSink::SkEventSink() : fTagHead(NULL) { | 33 SkEventSink::SkEventSink() : fTagHead(nullptr) { |
34 SkEventSink_Globals& globals = getGlobals(); | 34 SkEventSink_Globals& globals = getGlobals(); |
35 | 35 |
36 globals.fSinkMutex.acquire(); | 36 globals.fSinkMutex.acquire(); |
37 | 37 |
38 fID = ++globals.fNextSinkID; | 38 fID = ++globals.fNextSinkID; |
39 fNextSink = globals.fSinkHead; | 39 fNextSink = globals.fSinkHead; |
40 globals.fSinkHead = this; | 40 globals.fSinkHead = this; |
41 | 41 |
42 globals.fSinkMutex.release(); | 42 globals.fSinkMutex.release(); |
43 } | 43 } |
44 | 44 |
45 SkEventSink::~SkEventSink() { | 45 SkEventSink::~SkEventSink() { |
46 SkEventSink_Globals& globals = getGlobals(); | 46 SkEventSink_Globals& globals = getGlobals(); |
47 | 47 |
48 if (fTagHead) | 48 if (fTagHead) |
49 SkTagList::DeleteAll(fTagHead); | 49 SkTagList::DeleteAll(fTagHead); |
50 | 50 |
51 globals.fSinkMutex.acquire(); | 51 globals.fSinkMutex.acquire(); |
52 | 52 |
53 SkEventSink* sink = globals.fSinkHead; | 53 SkEventSink* sink = globals.fSinkHead; |
54 SkEventSink* prev = NULL; | 54 SkEventSink* prev = nullptr; |
55 | 55 |
56 for (;;) { | 56 for (;;) { |
57 SkEventSink* next = sink->fNextSink; | 57 SkEventSink* next = sink->fNextSink; |
58 if (sink == this) { | 58 if (sink == this) { |
59 if (prev) { | 59 if (prev) { |
60 prev->fNextSink = next; | 60 prev->fNextSink = next; |
61 } else { | 61 } else { |
62 globals.fSinkHead = next; | 62 globals.fSinkHead = next; |
63 } | 63 } |
64 break; | 64 break; |
(...skipping 17 matching lines...) Expand all Loading... |
82 return false; | 82 return false; |
83 } | 83 } |
84 | 84 |
85 bool SkEventSink::onQuery(SkEvent*) { | 85 bool SkEventSink::onQuery(SkEvent*) { |
86 return false; | 86 return false; |
87 } | 87 } |
88 | 88 |
89 /////////////////////////////////////////////////////////////////////////////// | 89 /////////////////////////////////////////////////////////////////////////////// |
90 | 90 |
91 SkTagList* SkEventSink::findTagList(U8CPU tag) const { | 91 SkTagList* SkEventSink::findTagList(U8CPU tag) const { |
92 return fTagHead ? SkTagList::Find(fTagHead, tag) : NULL; | 92 return fTagHead ? SkTagList::Find(fTagHead, tag) : nullptr; |
93 } | 93 } |
94 | 94 |
95 void SkEventSink::addTagList(SkTagList* rec) { | 95 void SkEventSink::addTagList(SkTagList* rec) { |
96 SkASSERT(rec); | 96 SkASSERT(rec); |
97 SkASSERT(fTagHead == NULL || SkTagList::Find(fTagHead, rec->fTag) == NULL); | 97 SkASSERT(fTagHead == nullptr || SkTagList::Find(fTagHead, rec->fTag) == null
ptr); |
98 | 98 |
99 rec->fNext = fTagHead; | 99 rec->fNext = fTagHead; |
100 fTagHead = rec; | 100 fTagHead = rec; |
101 } | 101 } |
102 | 102 |
103 void SkEventSink::removeTagList(U8CPU tag) { | 103 void SkEventSink::removeTagList(U8CPU tag) { |
104 if (fTagHead) { | 104 if (fTagHead) { |
105 SkTagList::DeleteTag(&fTagHead, tag); | 105 SkTagList::DeleteTag(&fTagHead, tag); |
106 } | 106 } |
107 } | 107 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 memcpy(next->fIDs, prev->fIDs, count * sizeof(SkEventSinkID)); | 155 memcpy(next->fIDs, prev->fIDs, count * sizeof(SkEventSinkID)); |
156 this->removeTagList(kListeners_SkTagList); | 156 this->removeTagList(kListeners_SkTagList); |
157 } | 157 } |
158 next->fIDs[count] = id; | 158 next->fIDs[count] = id; |
159 this->addTagList(next); | 159 this->addTagList(next); |
160 } | 160 } |
161 | 161 |
162 void SkEventSink::copyListeners(const SkEventSink& sink) | 162 void SkEventSink::copyListeners(const SkEventSink& sink) |
163 { | 163 { |
164 SkListenersTagList* sinkList = (SkListenersTagList*)sink.findTagList(kListen
ers_SkTagList); | 164 SkListenersTagList* sinkList = (SkListenersTagList*)sink.findTagList(kListen
ers_SkTagList); |
165 if (sinkList == NULL) | 165 if (sinkList == nullptr) |
166 return; | 166 return; |
167 SkASSERT(sinkList->countListners() > 0); | 167 SkASSERT(sinkList->countListners() > 0); |
168 const SkEventSinkID* iter = sinkList->fIDs; | 168 const SkEventSinkID* iter = sinkList->fIDs; |
169 const SkEventSinkID* stop = iter + sinkList->countListners(); | 169 const SkEventSinkID* stop = iter + sinkList->countListners(); |
170 while (iter < stop) | 170 while (iter < stop) |
171 addListenerID(*iter++); | 171 addListenerID(*iter++); |
172 } | 172 } |
173 | 173 |
174 void SkEventSink::removeListenerID(SkEventSinkID id) | 174 void SkEventSink::removeListenerID(SkEventSinkID id) |
175 { | 175 { |
176 if (id == 0) | 176 if (id == 0) |
177 return; | 177 return; |
178 | 178 |
179 SkListenersTagList* list = (SkListenersTagList*)this->findTagList(kListeners
_SkTagList); | 179 SkListenersTagList* list = (SkListenersTagList*)this->findTagList(kListeners
_SkTagList); |
180 | 180 |
181 if (list == NULL) | 181 if (list == nullptr) |
182 return; | 182 return; |
183 | 183 |
184 int index = list->find(id); | 184 int index = list->find(id); |
185 if (index >= 0) | 185 if (index >= 0) |
186 { | 186 { |
187 int count = list->countListners(); | 187 int count = list->countListners(); |
188 SkASSERT(count > 0); | 188 SkASSERT(count > 0); |
189 if (count == 1) | 189 if (count == 1) |
190 this->removeTagList(kListeners_SkTagList); | 190 this->removeTagList(kListeners_SkTagList); |
191 else | 191 else |
192 { | 192 { |
193 // overwrite without resize/reallocating our struct (for speed) | 193 // overwrite without resize/reallocating our struct (for speed) |
194 list->fIDs[index] = list->fIDs[count - 1]; | 194 list->fIDs[index] = list->fIDs[count - 1]; |
195 list->fExtra16 = SkToU16(count - 1); | 195 list->fExtra16 = SkToU16(count - 1); |
196 } | 196 } |
197 } | 197 } |
198 } | 198 } |
199 | 199 |
200 bool SkEventSink::hasListeners() const | 200 bool SkEventSink::hasListeners() const |
201 { | 201 { |
202 return this->findTagList(kListeners_SkTagList) != NULL; | 202 return this->findTagList(kListeners_SkTagList) != nullptr; |
203 } | 203 } |
204 | 204 |
205 void SkEventSink::postToListeners(const SkEvent& evt, SkMSec delay) { | 205 void SkEventSink::postToListeners(const SkEvent& evt, SkMSec delay) { |
206 SkListenersTagList* list = (SkListenersTagList*)this->findTagList(kListeners
_SkTagList); | 206 SkListenersTagList* list = (SkListenersTagList*)this->findTagList(kListeners
_SkTagList); |
207 if (list) { | 207 if (list) { |
208 SkASSERT(list->countListners() > 0); | 208 SkASSERT(list->countListners() > 0); |
209 const SkEventSinkID* iter = list->fIDs; | 209 const SkEventSinkID* iter = list->fIDs; |
210 const SkEventSinkID* stop = iter + list->countListners(); | 210 const SkEventSinkID* stop = iter + list->countListners(); |
211 while (iter < stop) { | 211 while (iter < stop) { |
212 SkEvent* copy = new SkEvent(evt); | 212 SkEvent* copy = new SkEvent(evt); |
(...skipping 26 matching lines...) Expand all Loading... |
239 SkEventSink_Globals& globals = getGlobals(); | 239 SkEventSink_Globals& globals = getGlobals(); |
240 SkAutoMutexAcquire ac(globals.fSinkMutex); | 240 SkAutoMutexAcquire ac(globals.fSinkMutex); |
241 SkEventSink* sink = globals.fSinkHead; | 241 SkEventSink* sink = globals.fSinkHead; |
242 | 242 |
243 while (sink) | 243 while (sink) |
244 { | 244 { |
245 if (sink->getSinkID() == sinkID) | 245 if (sink->getSinkID() == sinkID) |
246 return sink; | 246 return sink; |
247 sink = sink->fNextSink; | 247 sink = sink->fNextSink; |
248 } | 248 } |
249 return NULL; | 249 return nullptr; |
250 } | 250 } |
251 | 251 |
252 ////////////////////////////////////////////////////////////////////////////////
//////// | 252 ////////////////////////////////////////////////////////////////////////////////
//////// |
253 ////////////////////////////////////////////////////////////////////////////////
//////// | 253 ////////////////////////////////////////////////////////////////////////////////
//////// |
254 | 254 |
255 #if 0 // experimental, not tested | 255 #if 0 // experimental, not tested |
256 | 256 |
257 #include "SkMutex.h" | 257 #include "SkMutex.h" |
258 #include "SkTDict.h" | 258 #include "SkTDict.h" |
259 | 259 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 /** Remove all name/id pairs from the system. This is call internally | 292 /** Remove all name/id pairs from the system. This is call internally |
293 on shutdown, to ensure no memory leaks. It should not be called | 293 on shutdown, to ensure no memory leaks. It should not be called |
294 before shutdown. | 294 before shutdown. |
295 */ | 295 */ |
296 void SkEventSink::RemoveAllNamedSinkIDs() | 296 void SkEventSink::RemoveAllNamedSinkIDs() |
297 { | 297 { |
298 SkAutoMutexAcquire ac(gNamedSinkMutex); | 298 SkAutoMutexAcquire ac(gNamedSinkMutex); |
299 (void)gNamedSinkIDs.reset(); | 299 (void)gNamedSinkIDs.reset(); |
300 } | 300 } |
301 #endif | 301 #endif |
OLD | NEW |