epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | |
| 9 | #include "SkEventSink.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 10 | #include "SkMutex.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkTagList.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkTime.h" |
| 13 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 14 | class SkEventSink_Globals { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | public: |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 16 | SkEventSink_Globals() { |
| 17 | fNextSinkID = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 18 | fSinkHead = nullptr; |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 19 | } |
| 20 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | SkMutex fSinkMutex; |
| 22 | SkEventSinkID fNextSinkID; |
| 23 | SkEventSink* fSinkHead; |
| 24 | }; |
| 25 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 26 | static SkEventSink_Globals& getGlobals() { |
| 27 | // leak this, so we don't incur any shutdown perf hit |
| 28 | static SkEventSink_Globals* gGlobals = new SkEventSink_Globals; |
| 29 | return *gGlobals; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | } |
| 31 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 32 | SkEventSink::SkEventSink() : fTagHead(nullptr) { |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 33 | SkEventSink_Globals& globals = getGlobals(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | |
| 35 | globals.fSinkMutex.acquire(); |
| 36 | |
| 37 | fID = ++globals.fNextSinkID; |
| 38 | fNextSink = globals.fSinkHead; |
| 39 | globals.fSinkHead = this; |
| 40 | |
| 41 | globals.fSinkMutex.release(); |
| 42 | } |
| 43 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 44 | SkEventSink::~SkEventSink() { |
| 45 | SkEventSink_Globals& globals = getGlobals(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | |
| 47 | if (fTagHead) |
| 48 | SkTagList::DeleteAll(fTagHead); |
| 49 | |
| 50 | globals.fSinkMutex.acquire(); |
| 51 | |
| 52 | SkEventSink* sink = globals.fSinkHead; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | SkEventSink* prev = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 55 | for (;;) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | SkEventSink* next = sink->fNextSink; |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 57 | if (sink == this) { |
| 58 | if (prev) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | prev->fNextSink = next; |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 60 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | globals.fSinkHead = next; |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 62 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | break; |
| 64 | } |
| 65 | prev = sink; |
| 66 | sink = next; |
| 67 | } |
| 68 | globals.fSinkMutex.release(); |
| 69 | } |
| 70 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 71 | bool SkEventSink::doEvent(const SkEvent& evt) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | return this->onEvent(evt); |
| 73 | } |
| 74 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 75 | bool SkEventSink::doQuery(SkEvent* evt) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | SkASSERT(evt); |
| 77 | return this->onQuery(evt); |
| 78 | } |
| 79 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 80 | bool SkEventSink::onEvent(const SkEvent&) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 84 | bool SkEventSink::onQuery(SkEvent*) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | return false; |
| 86 | } |
| 87 | |
| 88 | /////////////////////////////////////////////////////////////////////////////// |
| 89 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 90 | SkTagList* SkEventSink::findTagList(U8CPU tag) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 91 | return fTagHead ? SkTagList::Find(fTagHead, tag) : nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | } |
| 93 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 94 | void SkEventSink::addTagList(SkTagList* rec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | SkASSERT(rec); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 96 | SkASSERT(fTagHead == nullptr || SkTagList::Find(fTagHead, rec->fTag) == nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | |
| 98 | rec->fNext = fTagHead; |
| 99 | fTagHead = rec; |
| 100 | } |
| 101 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 102 | void SkEventSink::removeTagList(U8CPU tag) { |
| 103 | if (fTagHead) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | SkTagList::DeleteTag(&fTagHead, tag); |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 105 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /////////////////////////////////////////////////////////////////////////////// |
| 109 | |
| 110 | struct SkListenersTagList : SkTagList { |
| 111 | SkListenersTagList(U16CPU count) : SkTagList(kListeners_SkTagList) |
| 112 | { |
| 113 | fExtra16 = SkToU16(count); |
| 114 | fIDs = (SkEventSinkID*)sk_malloc_throw(count * sizeof(SkEventSinkID)); |
| 115 | } |
| 116 | virtual ~SkListenersTagList() |
| 117 | { |
| 118 | sk_free(fIDs); |
| 119 | } |
| 120 | |
| 121 | int countListners() const { return fExtra16; } |
| 122 | |
| 123 | int find(SkEventSinkID id) const |
| 124 | { |
| 125 | const SkEventSinkID* idptr = fIDs; |
| 126 | for (int i = fExtra16 - 1; i >= 0; --i) |
| 127 | if (idptr[i] == id) |
| 128 | return i; |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | SkEventSinkID* fIDs; |
| 133 | }; |
| 134 | |
| 135 | void SkEventSink::addListenerID(SkEventSinkID id) |
| 136 | { |
| 137 | if (id == 0) |
| 138 | return; |
| 139 | |
| 140 | SkListenersTagList* prev = (SkListenersTagList*)this->findTagList(kListeners_SkTagList); |
| 141 | int count = 0; |
| 142 | |
| 143 | if (prev) |
| 144 | { |
| 145 | if (prev->find(id) >= 0) |
| 146 | return; |
| 147 | count = prev->countListners(); |
| 148 | } |
| 149 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 150 | SkListenersTagList* next = new SkListenersTagList(count + 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | |
| 152 | if (prev) |
| 153 | { |
| 154 | memcpy(next->fIDs, prev->fIDs, count * sizeof(SkEventSinkID)); |
| 155 | this->removeTagList(kListeners_SkTagList); |
| 156 | } |
| 157 | next->fIDs[count] = id; |
| 158 | this->addTagList(next); |
| 159 | } |
| 160 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 161 | void SkEventSink::copyListeners(const SkEventSink& sink) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | { |
| 163 | SkListenersTagList* sinkList = (SkListenersTagList*)sink.findTagList(kListeners_SkTagList); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 164 | if (sinkList == nullptr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | return; |
| 166 | SkASSERT(sinkList->countListners() > 0); |
| 167 | const SkEventSinkID* iter = sinkList->fIDs; |
| 168 | const SkEventSinkID* stop = iter + sinkList->countListners(); |
| 169 | while (iter < stop) |
| 170 | addListenerID(*iter++); |
| 171 | } |
| 172 | |
| 173 | void SkEventSink::removeListenerID(SkEventSinkID id) |
| 174 | { |
| 175 | if (id == 0) |
| 176 | return; |
| 177 | |
| 178 | SkListenersTagList* list = (SkListenersTagList*)this->findTagList(kListeners_SkTagList); |
| 179 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 180 | if (list == nullptr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | return; |
| 182 | |
| 183 | int index = list->find(id); |
| 184 | if (index >= 0) |
| 185 | { |
| 186 | int count = list->countListners(); |
| 187 | SkASSERT(count > 0); |
| 188 | if (count == 1) |
| 189 | this->removeTagList(kListeners_SkTagList); |
| 190 | else |
| 191 | { |
| 192 | // overwrite without resize/reallocating our struct (for speed) |
| 193 | list->fIDs[index] = list->fIDs[count - 1]; |
| 194 | list->fExtra16 = SkToU16(count - 1); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | bool SkEventSink::hasListeners() const |
| 200 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 201 | return this->findTagList(kListeners_SkTagList) != nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 202 | } |
| 203 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 204 | void SkEventSink::postToListeners(const SkEvent& evt, SkMSec delay) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 205 | SkListenersTagList* list = (SkListenersTagList*)this->findTagList(kListeners_SkTagList); |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 206 | if (list) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | SkASSERT(list->countListners() > 0); |
| 208 | const SkEventSinkID* iter = list->fIDs; |
| 209 | const SkEventSinkID* stop = iter + list->countListners(); |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 210 | while (iter < stop) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 211 | SkEvent* copy = new SkEvent(evt); |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 212 | copy->setTargetID(*iter++)->postDelay(delay); |
| 213 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | /////////////////////////////////////////////////////////////////////////////// |
| 218 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 219 | SkEventSink::EventResult SkEventSink::DoEvent(const SkEvent& evt) { |
| 220 | SkEvent::Proc proc = evt.getTargetProc(); |
| 221 | if (proc) { |
| 222 | return proc(evt) ? kHandled_EventResult : kNotHandled_EventResult; |
| 223 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 224 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 225 | SkEventSink* sink = SkEventSink::FindSink(evt.getTargetID()); |
| 226 | if (sink) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | return sink->doEvent(evt) ? kHandled_EventResult : kNotHandled_EventResult; |
| 228 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 229 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 230 | return kSinkNotFound_EventResult; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | SkEventSink* SkEventSink::FindSink(SkEventSinkID sinkID) |
| 234 | { |
| 235 | if (sinkID == 0) |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 236 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 238 | SkEventSink_Globals& globals = getGlobals(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | SkAutoMutexAcquire ac(globals.fSinkMutex); |
| 240 | SkEventSink* sink = globals.fSinkHead; |
| 241 | |
| 242 | while (sink) |
| 243 | { |
| 244 | if (sink->getSinkID() == sinkID) |
| 245 | return sink; |
| 246 | sink = sink->fNextSink; |
| 247 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 248 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 249 | } |