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 | |
Hal Canary | e574f1e | 2019-07-15 14:01:37 -0400 | [diff] [blame] | 8 | #include "tools/SkMetaData.h" |
Herb Derby | d7b34a5 | 2017-03-20 11:19:23 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/private/SkMalloc.h" |
| 11 | #include "include/private/SkTo.h" |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 12 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | void SkMetaData::reset() |
| 14 | { |
| 15 | Rec* rec = fRec; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 16 | while (rec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | Rec* next = rec->fNext; |
| 18 | Rec::Free(rec); |
| 19 | rec = next; |
| 20 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 21 | fRec = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | } |
| 23 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | void SkMetaData::setS32(const char name[], int32_t value) |
| 25 | { |
| 26 | (void)this->set(name, &value, sizeof(int32_t), kS32_Type, 1); |
| 27 | } |
| 28 | |
| 29 | void SkMetaData::setScalar(const char name[], SkScalar value) |
| 30 | { |
| 31 | (void)this->set(name, &value, sizeof(SkScalar), kScalar_Type, 1); |
| 32 | } |
| 33 | |
| 34 | SkScalar* SkMetaData::setScalars(const char name[], int count, const SkScalar values[]) |
| 35 | { |
| 36 | SkASSERT(count > 0); |
| 37 | if (count > 0) |
| 38 | return (SkScalar*)this->set(name, values, sizeof(SkScalar), kScalar_Type, count); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 39 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Hal Canary | ca9e187 | 2019-07-25 10:36:30 -0400 | [diff] [blame] | 42 | void SkMetaData::setPtr(const char name[], void* ptr) { |
| 43 | (void)this->set(name, &ptr, sizeof(void*), kPtr_Type, 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void SkMetaData::setBool(const char name[], bool value) |
| 47 | { |
| 48 | (void)this->set(name, &value, sizeof(bool), kBool_Type, 1); |
| 49 | } |
| 50 | |
| 51 | void* SkMetaData::set(const char name[], const void* data, size_t dataSize, Type type, int count) |
| 52 | { |
| 53 | SkASSERT(name); |
| 54 | SkASSERT(dataSize); |
| 55 | SkASSERT(count > 0); |
| 56 | |
Ben Wagner | 1968473 | 2020-08-21 16:52:48 -0400 | [diff] [blame] | 57 | FindResult result = this->findWithPrev(name, type); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | |
Ben Wagner | 1968473 | 2020-08-21 16:52:48 -0400 | [diff] [blame] | 59 | Rec* rec; |
| 60 | bool reuseRec = result.rec && |
| 61 | result.rec->fDataLen == dataSize && |
| 62 | result.rec->fDataCount == count; |
| 63 | if (reuseRec) { |
| 64 | rec = result.rec; |
| 65 | } else { |
| 66 | size_t len = strlen(name); |
| 67 | rec = Rec::Alloc(sizeof(Rec) + dataSize * count + len + 1); |
| 68 | rec->fType = SkToU8(type); |
| 69 | rec->fDataLen = SkToU8(dataSize); |
| 70 | rec->fDataCount = SkToU16(count); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | |
Ben Wagner | 1968473 | 2020-08-21 16:52:48 -0400 | [diff] [blame] | 72 | memcpy(rec->name(), name, len + 1); |
| 73 | } |
| 74 | if (data) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | memcpy(rec->data(), data, dataSize * count); |
Ben Wagner | 1968473 | 2020-08-21 16:52:48 -0400 | [diff] [blame] | 76 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | |
Ben Wagner | 1968473 | 2020-08-21 16:52:48 -0400 | [diff] [blame] | 78 | if (reuseRec) { |
| 79 | // Do nothing, reused |
| 80 | } else if (result.rec) { |
| 81 | // Had one, but had to create a new one. Invalidates iterators. |
| 82 | // Delayed removal since name or data may have been in the result.rec. |
| 83 | this->remove(result); |
| 84 | if (result.prev) { |
| 85 | rec->fNext = result.prev->fNext; |
| 86 | result.prev->fNext = rec; |
| 87 | } |
| 88 | } else { |
| 89 | // Adding a new one, stick it at head. |
| 90 | rec->fNext = fRec; |
| 91 | fRec = rec; |
| 92 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | return rec->data(); |
| 94 | } |
| 95 | |
| 96 | bool SkMetaData::findS32(const char name[], int32_t* value) const |
| 97 | { |
| 98 | const Rec* rec = this->find(name, kS32_Type); |
| 99 | if (rec) |
| 100 | { |
| 101 | SkASSERT(rec->fDataCount == 1); |
| 102 | if (value) |
| 103 | *value = *(const int32_t*)rec->data(); |
| 104 | return true; |
| 105 | } |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | bool SkMetaData::findScalar(const char name[], SkScalar* value) const |
| 110 | { |
| 111 | const Rec* rec = this->find(name, kScalar_Type); |
| 112 | if (rec) |
| 113 | { |
| 114 | SkASSERT(rec->fDataCount == 1); |
| 115 | if (value) |
| 116 | *value = *(const SkScalar*)rec->data(); |
| 117 | return true; |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | const SkScalar* SkMetaData::findScalars(const char name[], int* count, SkScalar values[]) const |
| 123 | { |
| 124 | const Rec* rec = this->find(name, kScalar_Type); |
| 125 | if (rec) |
| 126 | { |
| 127 | if (count) |
| 128 | *count = rec->fDataCount; |
| 129 | if (values) |
| 130 | memcpy(values, rec->data(), rec->fDataCount * rec->fDataLen); |
| 131 | return (const SkScalar*)rec->data(); |
| 132 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 133 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Hal Canary | ca9e187 | 2019-07-25 10:36:30 -0400 | [diff] [blame] | 136 | bool SkMetaData::findPtr(const char name[], void** ptr) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | const Rec* rec = this->find(name, kPtr_Type); |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 138 | if (rec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | SkASSERT(rec->fDataCount == 1); |
Hal Canary | ca9e187 | 2019-07-25 10:36:30 -0400 | [diff] [blame] | 140 | void** found = (void**)rec->data(); |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 141 | if (ptr) { |
Hal Canary | ca9e187 | 2019-07-25 10:36:30 -0400 | [diff] [blame] | 142 | *ptr = *found; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 143 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | return false; |
| 147 | } |
| 148 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | bool SkMetaData::findBool(const char name[], bool* value) const |
| 150 | { |
| 151 | const Rec* rec = this->find(name, kBool_Type); |
| 152 | if (rec) |
| 153 | { |
| 154 | SkASSERT(rec->fDataCount == 1); |
| 155 | if (value) |
| 156 | *value = *(const bool*)rec->data(); |
| 157 | return true; |
| 158 | } |
| 159 | return false; |
| 160 | } |
| 161 | |
Ben Wagner | 1968473 | 2020-08-21 16:52:48 -0400 | [diff] [blame] | 162 | SkMetaData::FindResult SkMetaData::findWithPrev(const char name[], Type type) const { |
| 163 | FindResult current { fRec, nullptr }; |
| 164 | while (current.rec) { |
| 165 | if (current.rec->fType == type && !strcmp(current.rec->name(), name)) |
| 166 | return current; |
| 167 | current.prev = current.rec; |
| 168 | current.rec = current.rec->fNext; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | } |
Ben Wagner | 1968473 | 2020-08-21 16:52:48 -0400 | [diff] [blame] | 170 | return current; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | const SkMetaData::Rec* SkMetaData::find(const char name[], Type type) const { |
| 175 | return this->findWithPrev(name, type).rec; |
| 176 | } |
| 177 | |
| 178 | void SkMetaData::remove(FindResult result) { |
| 179 | SkASSERT(result.rec); |
| 180 | if (result.prev) { |
| 181 | result.prev->fNext = result.rec->fNext; |
| 182 | } else { |
| 183 | fRec = result.rec->fNext; |
| 184 | } |
| 185 | Rec::Free(result.rec); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 186 | } |
| 187 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 188 | bool SkMetaData::remove(const char name[], Type type) { |
Ben Wagner | 1968473 | 2020-08-21 16:52:48 -0400 | [diff] [blame] | 189 | FindResult result = this->findWithPrev(name, type); |
| 190 | if (!result.rec) { |
| 191 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 192 | } |
Ben Wagner | 1968473 | 2020-08-21 16:52:48 -0400 | [diff] [blame] | 193 | this->remove(result); |
| 194 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | bool SkMetaData::removeS32(const char name[]) |
| 198 | { |
| 199 | return this->remove(name, kS32_Type); |
| 200 | } |
| 201 | |
| 202 | bool SkMetaData::removeScalar(const char name[]) |
| 203 | { |
| 204 | return this->remove(name, kScalar_Type); |
| 205 | } |
| 206 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | bool SkMetaData::removePtr(const char name[]) |
| 208 | { |
| 209 | return this->remove(name, kPtr_Type); |
| 210 | } |
| 211 | |
| 212 | bool SkMetaData::removeBool(const char name[]) |
| 213 | { |
| 214 | return this->remove(name, kBool_Type); |
| 215 | } |
| 216 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 217 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 219 | SkMetaData::Iter::Iter(const SkMetaData& metadata) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 220 | fRec = metadata.fRec; |
| 221 | } |
| 222 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 223 | void SkMetaData::Iter::reset(const SkMetaData& metadata) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 224 | fRec = metadata.fRec; |
| 225 | } |
| 226 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 227 | const char* SkMetaData::Iter::next(SkMetaData::Type* t, int* count) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 228 | const char* name = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 229 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 230 | if (fRec) { |
| 231 | if (t) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 232 | *t = (SkMetaData::Type)fRec->fType; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 233 | } |
| 234 | if (count) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 235 | *count = fRec->fDataCount; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 236 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | name = fRec->name(); |
| 238 | |
| 239 | fRec = fRec->fNext; |
| 240 | } |
| 241 | return name; |
| 242 | } |
| 243 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 244 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 245 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 246 | SkMetaData::Rec* SkMetaData::Rec::Alloc(size_t size) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 247 | return (Rec*)sk_malloc_throw(size); |
| 248 | } |
| 249 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 250 | void SkMetaData::Rec::Free(Rec* rec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 251 | sk_free(rec); |
| 252 | } |