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 "SkEvent.h" |
| 10 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 11 | void SkEvent::initialize(const char* type, size_t typeLen, |
| 12 | SkEventSinkID targetID) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 13 | fType = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | setType(type, typeLen); |
| 15 | f32 = 0; |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 16 | fTargetID = targetID; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 17 | fTargetProc = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | #ifdef SK_DEBUG |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | fTime = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 20 | fNextEvent = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | SkEvent::SkEvent() |
| 25 | { |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 26 | initialize("", 0, 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | SkEvent::SkEvent(const SkEvent& src) |
| 30 | { |
| 31 | *this = src; |
| 32 | if (((size_t) fType & 1) == 0) |
| 33 | setType(src.fType); |
| 34 | } |
| 35 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 36 | SkEvent::SkEvent(const SkString& type, SkEventSinkID targetID) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | { |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 38 | initialize(type.c_str(), type.size(), targetID); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | } |
| 40 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 41 | SkEvent::SkEvent(const char type[], SkEventSinkID targetID) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | { |
| 43 | SkASSERT(type); |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 44 | initialize(type, strlen(type), targetID); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | SkEvent::~SkEvent() |
| 48 | { |
| 49 | if (((size_t) fType & 1) == 0) |
| 50 | sk_free((void*) fType); |
| 51 | } |
| 52 | |
| 53 | static size_t makeCharArray(char* buffer, size_t compact) |
| 54 | { |
| 55 | size_t bits = (size_t) compact >> 1; |
| 56 | memcpy(buffer, &bits, sizeof(compact)); |
| 57 | buffer[sizeof(compact)] = 0; |
| 58 | return strlen(buffer); |
| 59 | } |
| 60 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 61 | void SkEvent::getType(SkString* str) const |
| 62 | { |
| 63 | if (str) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | { |
| 65 | if ((size_t) fType & 1) // not a pointer |
| 66 | { |
| 67 | char chars[sizeof(size_t) + 1]; |
| 68 | size_t len = makeCharArray(chars, (size_t) fType); |
| 69 | str->set(chars, len); |
| 70 | } |
| 71 | else |
| 72 | str->set(fType); |
| 73 | } |
| 74 | } |
| 75 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 76 | bool SkEvent::isType(const SkString& str) const |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 78 | return this->isType(str.c_str(), str.size()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | } |
| 80 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 81 | bool SkEvent::isType(const char type[], size_t typeLen) const |
| 82 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | if (typeLen == 0) |
| 84 | typeLen = strlen(type); |
| 85 | if ((size_t) fType & 1) { // not a pointer |
| 86 | char chars[sizeof(size_t) + 1]; |
| 87 | size_t len = makeCharArray(chars, (size_t) fType); |
| 88 | return len == typeLen && strncmp(chars, type, typeLen) == 0; |
| 89 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 90 | return strncmp(fType, type, typeLen) == 0 && fType[typeLen] == 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void SkEvent::setType(const char type[], size_t typeLen) |
| 94 | { |
| 95 | if (typeLen == 0) |
| 96 | typeLen = strlen(type); |
| 97 | if (typeLen <= sizeof(fType)) { |
| 98 | size_t slot = 0; |
| 99 | memcpy(&slot, type, typeLen); |
| 100 | if (slot << 1 >> 1 != slot) |
| 101 | goto useCharStar; |
| 102 | slot <<= 1; |
| 103 | slot |= 1; |
| 104 | fType = (char*) slot; |
| 105 | } else { |
| 106 | useCharStar: |
| 107 | fType = (char*) sk_malloc_throw(typeLen + 1); |
| 108 | SkASSERT(((size_t) fType & 1) == 0); |
| 109 | memcpy(fType, type, typeLen); |
| 110 | fType[typeLen] = 0; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void SkEvent::setType(const SkString& type) |
| 115 | { |
| 116 | setType(type.c_str()); |
| 117 | } |
| 118 | |
| 119 | //////////////////////////////////////////////////////////////////////////// |
| 120 | |
| 121 | #include "SkParse.h" |
| 122 | |
| 123 | void SkEvent::inflate(const SkDOM& dom, const SkDOM::Node* node) |
| 124 | { |
| 125 | const char* name = dom.findAttr(node, "type"); |
| 126 | if (name) |
| 127 | this->setType(name); |
| 128 | |
| 129 | const char* value; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 130 | if ((value = dom.findAttr(node, "fast32")) != nullptr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | { |
| 132 | int32_t n; |
| 133 | if (SkParse::FindS32(value, &n)) |
| 134 | this->setFast32(n); |
| 135 | } |
| 136 | |
| 137 | for (node = dom.getFirstChild(node); node; node = dom.getNextSibling(node)) |
| 138 | { |
| 139 | if (strcmp(dom.getName(node), "data")) |
| 140 | { |
| 141 | SkDEBUGCODE(SkDebugf("SkEvent::inflate unrecognized subelement <%s>\n", dom.getName(node));) |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | name = dom.findAttr(node, "name"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 146 | if (name == nullptr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | { |
| 148 | SkDEBUGCODE(SkDebugf("SkEvent::inflate missing required \"name\" attribute in <data> subelement\n");) |
| 149 | continue; |
| 150 | } |
| 151 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 152 | if ((value = dom.findAttr(node, "s32")) != nullptr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | { |
| 154 | int32_t n; |
| 155 | if (SkParse::FindS32(value, &n)) |
| 156 | this->setS32(name, n); |
| 157 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 158 | else if ((value = dom.findAttr(node, "scalar")) != nullptr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | { |
| 160 | SkScalar x; |
| 161 | if (SkParse::FindScalar(value, &x)) |
| 162 | this->setScalar(name, x); |
| 163 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 164 | else if ((value = dom.findAttr(node, "string")) != nullptr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | this->setString(name, value); |
| 166 | #ifdef SK_DEBUG |
| 167 | else |
| 168 | { |
| 169 | SkDebugf("SkEvent::inflate <data name=\"%s\"> subelement missing required type attribute [S32 | scalar | string]\n", name); |
| 170 | } |
| 171 | #endif |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | #ifdef SK_DEBUG |
| 176 | |
| 177 | #ifndef SkScalarToFloat |
| 178 | #define SkScalarToFloat(x) ((x) / 65536.f) |
| 179 | #endif |
| 180 | |
| 181 | void SkEvent::dump(const char title[]) |
| 182 | { |
| 183 | if (title) |
| 184 | SkDebugf("%s ", title); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 185 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 186 | SkString etype; |
| 187 | this->getType(&etype); |
| 188 | SkDebugf("event<%s> fast32=%d", etype.c_str(), this->getFast32()); |
| 189 | |
| 190 | const SkMetaData& md = this->getMetaData(); |
| 191 | SkMetaData::Iter iter(md); |
| 192 | SkMetaData::Type mtype; |
| 193 | int count; |
| 194 | const char* name; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 195 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 196 | while ((name = iter.next(&mtype, &count)) != nullptr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | { |
| 198 | SkASSERT(count > 0); |
| 199 | |
| 200 | SkDebugf(" <%s>=", name); |
| 201 | switch (mtype) { |
| 202 | case SkMetaData::kS32_Type: // vector version??? |
| 203 | { |
| 204 | int32_t value; |
| 205 | md.findS32(name, &value); |
| 206 | SkDebugf("%d ", value); |
| 207 | } |
| 208 | break; |
| 209 | case SkMetaData::kScalar_Type: |
| 210 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 211 | const SkScalar* values = md.findScalars(name, &count, nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | SkDebugf("%f", SkScalarToFloat(values[0])); |
| 213 | for (int i = 1; i < count; i++) |
| 214 | SkDebugf(", %f", SkScalarToFloat(values[i])); |
| 215 | SkDebugf(" "); |
| 216 | } |
| 217 | break; |
| 218 | case SkMetaData::kString_Type: |
| 219 | { |
| 220 | const char* value = md.findString(name); |
| 221 | SkASSERT(value); |
| 222 | SkDebugf("<%s> ", value); |
| 223 | } |
| 224 | break; |
| 225 | case SkMetaData::kPtr_Type: // vector version??? |
| 226 | { |
| 227 | void* value; |
| 228 | md.findPtr(name, &value); |
| 229 | SkDebugf("%p ", value); |
| 230 | } |
| 231 | break; |
| 232 | case SkMetaData::kBool_Type: // vector version??? |
| 233 | { |
| 234 | bool value; |
| 235 | md.findBool(name, &value); |
| 236 | SkDebugf("%s ", value ? "true" : "false"); |
| 237 | } |
| 238 | break; |
| 239 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 240 | SkDEBUGFAIL("unknown metadata type returned from iterator"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 241 | break; |
| 242 | } |
| 243 | } |
| 244 | SkDebugf("\n"); |
| 245 | } |
| 246 | #endif |
| 247 | |
| 248 | /////////////////////////////////////////////////////////////////////////////////////// |
| 249 | |
| 250 | #ifdef SK_DEBUG |
| 251 | // #define SK_TRACE_EVENTSx |
| 252 | #endif |
| 253 | |
| 254 | #ifdef SK_TRACE_EVENTS |
| 255 | static void event_log(const char s[]) |
| 256 | { |
| 257 | SkDEBUGF(("%s\n", s)); |
| 258 | } |
| 259 | |
| 260 | #define EVENT_LOG(s) event_log(s) |
| 261 | #define EVENT_LOGN(s, n) do { SkString str(s); str.append(" "); str.appendS32(n); event_log(str.c_str()); } while (0) |
| 262 | #else |
| 263 | #define EVENT_LOG(s) |
| 264 | #define EVENT_LOGN(s, n) |
| 265 | #endif |
| 266 | |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 267 | #include "SkMutex.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 268 | #include "SkTime.h" |
| 269 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 270 | class SkEvent_Globals { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 271 | public: |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 272 | SkEvent_Globals() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 273 | fEventQHead = nullptr; |
| 274 | fEventQTail = nullptr; |
| 275 | fDelayQHead = nullptr; |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 276 | SkDEBUGCODE(fEventCounter = 0;) |
| 277 | } |
| 278 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 279 | SkMutex fEventMutex; |
| 280 | SkEvent* fEventQHead, *fEventQTail; |
| 281 | SkEvent* fDelayQHead; |
| 282 | SkDEBUGCODE(int fEventCounter;) |
| 283 | }; |
| 284 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 285 | static SkEvent_Globals& getGlobals() { |
| 286 | // leak this, so we don't incure any shutdown perf hit |
| 287 | static SkEvent_Globals* gGlobals = new SkEvent_Globals; |
| 288 | return *gGlobals; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 289 | } |
| 290 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 291 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 292 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 293 | void SkEvent::postDelay(SkMSec delay) { |
| 294 | if (!fTargetID && !fTargetProc) { |
reed@google.com | c514dde | 2011-08-03 19:41:24 +0000 | [diff] [blame] | 295 | delete this; |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 296 | return; |
| 297 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 298 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 299 | if (delay) { |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 300 | this->postTime(GetMSecsSinceStartup() + delay); |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 301 | return; |
| 302 | } |
| 303 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 304 | SkEvent_Globals& globals = getGlobals(); |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 305 | |
| 306 | globals.fEventMutex.acquire(); |
| 307 | bool wasEmpty = SkEvent::Enqueue(this); |
| 308 | globals.fEventMutex.release(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 309 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 310 | // call outside of us holding the mutex |
| 311 | if (wasEmpty) { |
| 312 | SkEvent::SignalNonEmptyQueue(); |
reed@google.com | c514dde | 2011-08-03 19:41:24 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 316 | void SkEvent::postTime(SkMSec time) { |
| 317 | if (!fTargetID && !fTargetProc) { |
| 318 | delete this; |
| 319 | return; |
| 320 | } |
| 321 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 322 | SkEvent_Globals& globals = getGlobals(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 323 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 324 | globals.fEventMutex.acquire(); |
| 325 | SkMSec queueDelay = SkEvent::EnqueueTime(this, time); |
| 326 | globals.fEventMutex.release(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 327 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 328 | // call outside of us holding the mutex |
| 329 | if ((int32_t)queueDelay != ~0) { |
| 330 | SkEvent::SignalQueueTimer(queueDelay); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | bool SkEvent::Enqueue(SkEvent* evt) { |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 335 | SkEvent_Globals& globals = getGlobals(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 336 | // gEventMutex acquired by caller |
| 337 | |
| 338 | SkASSERT(evt); |
| 339 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 340 | bool wasEmpty = globals.fEventQHead == nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 341 | |
| 342 | if (globals.fEventQTail) |
| 343 | globals.fEventQTail->fNextEvent = evt; |
| 344 | globals.fEventQTail = evt; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 345 | if (globals.fEventQHead == nullptr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 346 | globals.fEventQHead = evt; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 347 | evt->fNextEvent = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 348 | |
| 349 | SkDEBUGCODE(++globals.fEventCounter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 350 | |
| 351 | return wasEmpty; |
| 352 | } |
| 353 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 354 | SkEvent* SkEvent::Dequeue() { |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 355 | SkEvent_Globals& globals = getGlobals(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 356 | globals.fEventMutex.acquire(); |
| 357 | |
| 358 | SkEvent* evt = globals.fEventQHead; |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 359 | if (evt) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 360 | SkDEBUGCODE(--globals.fEventCounter); |
| 361 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 362 | globals.fEventQHead = evt->fNextEvent; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 363 | if (globals.fEventQHead == nullptr) { |
| 364 | globals.fEventQTail = nullptr; |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 365 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 366 | } |
| 367 | globals.fEventMutex.release(); |
| 368 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 369 | return evt; |
| 370 | } |
| 371 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 372 | bool SkEvent::QHasEvents() { |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 373 | SkEvent_Globals& globals = getGlobals(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 374 | |
| 375 | // this is not thread accurate, need a semaphore for that |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 376 | return globals.fEventQHead != nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | #ifdef SK_TRACE_EVENTS |
| 380 | static int gDelayDepth; |
| 381 | #endif |
| 382 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 383 | SkMSec SkEvent::EnqueueTime(SkEvent* evt, SkMSec time) { |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 384 | SkEvent_Globals& globals = getGlobals(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 385 | // gEventMutex acquired by caller |
| 386 | |
| 387 | SkEvent* curr = globals.fDelayQHead; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 388 | SkEvent* prev = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 389 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 390 | while (curr) { |
| 391 | if (SkMSec_LT(time, curr->fTime)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 392 | break; |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 393 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 394 | prev = curr; |
| 395 | curr = curr->fNextEvent; |
| 396 | } |
| 397 | |
| 398 | evt->fTime = time; |
| 399 | evt->fNextEvent = curr; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 400 | if (prev == nullptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 401 | globals.fDelayQHead = evt; |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 402 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 403 | prev->fNextEvent = evt; |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 404 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 405 | |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 406 | SkMSec delay = globals.fDelayQHead->fTime - GetMSecsSinceStartup(); |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 407 | if ((int32_t)delay <= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 408 | delay = 1; |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 409 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 410 | return delay; |
| 411 | } |
| 412 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 413 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 414 | |
| 415 | #include "SkEventSink.h" |
| 416 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 417 | bool SkEvent::ProcessEvent() { |
| 418 | SkEvent* evt = SkEvent::Dequeue(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 419 | SkAutoTDelete<SkEvent> autoDelete(evt); |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 420 | bool again = false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 421 | |
| 422 | EVENT_LOGN("ProcessEvent", (int32_t)evt); |
| 423 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 424 | if (evt) { |
| 425 | (void)SkEventSink::DoEvent(*evt); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 426 | again = SkEvent::QHasEvents(); |
| 427 | } |
| 428 | return again; |
| 429 | } |
| 430 | |
| 431 | void SkEvent::ServiceQueueTimer() |
| 432 | { |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 433 | SkEvent_Globals& globals = getGlobals(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 434 | |
| 435 | globals.fEventMutex.acquire(); |
| 436 | |
| 437 | bool wasEmpty = false; |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 438 | SkMSec now = GetMSecsSinceStartup(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 439 | SkEvent* evt = globals.fDelayQHead; |
| 440 | |
| 441 | while (evt) |
| 442 | { |
| 443 | if (SkMSec_LT(now, evt->fTime)) |
| 444 | break; |
| 445 | |
| 446 | #ifdef SK_TRACE_EVENTS |
| 447 | --gDelayDepth; |
| 448 | SkDebugf("dequeue-delay %s (%d)", evt->getType(), gDelayDepth); |
| 449 | const char* idStr = evt->findString("id"); |
| 450 | if (idStr) |
| 451 | SkDebugf(" (%s)", idStr); |
| 452 | SkDebugf("\n"); |
| 453 | #endif |
| 454 | |
| 455 | SkEvent* next = evt->fNextEvent; |
| 456 | if (SkEvent::Enqueue(evt)) |
| 457 | wasEmpty = true; |
| 458 | evt = next; |
| 459 | } |
| 460 | globals.fDelayQHead = evt; |
| 461 | |
| 462 | SkMSec time = evt ? evt->fTime - now : 0; |
| 463 | |
| 464 | globals.fEventMutex.release(); |
| 465 | |
| 466 | if (wasEmpty) |
| 467 | SkEvent::SignalNonEmptyQueue(); |
| 468 | |
| 469 | SkEvent::SignalQueueTimer(time); |
| 470 | } |
| 471 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 472 | int SkEvent::CountEventsOnQueue() { |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 473 | SkEvent_Globals& globals = getGlobals(); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 474 | globals.fEventMutex.acquire(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 475 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 476 | int count = 0; |
| 477 | const SkEvent* evt = globals.fEventQHead; |
| 478 | while (evt) { |
| 479 | count += 1; |
| 480 | evt = evt->fNextEvent; |
| 481 | } |
| 482 | globals.fEventMutex.release(); |
| 483 | |
| 484 | return count; |
| 485 | } |
| 486 | |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 487 | SkMSec SkEvent::GetMSecsSinceStartup() { |
| 488 | static const double kEpoch = SkTime::GetMSecs(); |
| 489 | return static_cast<SkMSec>(SkTime::GetMSecs() - kEpoch); |
| 490 | } |
| 491 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 492 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 493 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 494 | void SkEvent::Init() {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 495 | |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 496 | void SkEvent::Term() { |
| 497 | SkEvent_Globals& globals = getGlobals(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 498 | |
| 499 | SkEvent* evt = globals.fEventQHead; |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 500 | while (evt) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 501 | SkEvent* next = evt->fNextEvent; |
| 502 | delete evt; |
| 503 | evt = next; |
| 504 | } |
| 505 | |
| 506 | evt = globals.fDelayQHead; |
reed@google.com | 9998c66 | 2011-11-17 22:09:47 +0000 | [diff] [blame] | 507 | while (evt) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 508 | SkEvent* next = evt->fNextEvent; |
| 509 | delete evt; |
| 510 | evt = next; |
| 511 | } |
| 512 | } |