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 "SkMetaData.h" |
Herb Derby | d7b34a5 | 2017-03-20 11:19:23 -0400 | [diff] [blame] | 10 | |
Herb Derby | b549cc3 | 2017-03-27 13:35:15 -0400 | [diff] [blame] | 11 | #include "SkMalloc.h" |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 12 | #include "SkRefCnt.h" |
| 13 | |
| 14 | struct PtrPair { |
| 15 | void* fPtr; |
| 16 | SkMetaData::PtrProc fProc; |
| 17 | }; |
| 18 | |
| 19 | void* SkMetaData::RefCntProc(void* ptr, bool doRef) { |
| 20 | SkASSERT(ptr); |
| 21 | SkRefCnt* refcnt = reinterpret_cast<SkRefCnt*>(ptr); |
| 22 | |
| 23 | if (doRef) { |
| 24 | refcnt->ref(); |
| 25 | } else { |
| 26 | refcnt->unref(); |
| 27 | } |
| 28 | return ptr; |
| 29 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 31 | SkMetaData::SkMetaData() : fRec(nullptr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | { |
| 33 | } |
| 34 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 35 | SkMetaData::SkMetaData(const SkMetaData& src) : fRec(nullptr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | { |
| 37 | *this = src; |
| 38 | } |
| 39 | |
| 40 | SkMetaData::~SkMetaData() |
| 41 | { |
| 42 | this->reset(); |
| 43 | } |
| 44 | |
| 45 | void SkMetaData::reset() |
| 46 | { |
| 47 | Rec* rec = fRec; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 48 | while (rec) { |
| 49 | if (kPtr_Type == rec->fType) { |
| 50 | PtrPair* pair = (PtrPair*)rec->data(); |
| 51 | if (pair->fProc && pair->fPtr) { |
| 52 | pair->fPtr = pair->fProc(pair->fPtr, false); |
| 53 | } |
| 54 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | Rec* next = rec->fNext; |
| 56 | Rec::Free(rec); |
| 57 | rec = next; |
| 58 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 59 | fRec = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | SkMetaData& SkMetaData::operator=(const SkMetaData& src) |
| 63 | { |
| 64 | this->reset(); |
| 65 | |
| 66 | const Rec* rec = src.fRec; |
| 67 | while (rec) |
| 68 | { |
| 69 | this->set(rec->name(), rec->data(), rec->fDataLen, (Type)rec->fType, rec->fDataCount); |
| 70 | rec = rec->fNext; |
| 71 | } |
| 72 | return *this; |
| 73 | } |
| 74 | |
| 75 | void SkMetaData::setS32(const char name[], int32_t value) |
| 76 | { |
| 77 | (void)this->set(name, &value, sizeof(int32_t), kS32_Type, 1); |
| 78 | } |
| 79 | |
| 80 | void SkMetaData::setScalar(const char name[], SkScalar value) |
| 81 | { |
| 82 | (void)this->set(name, &value, sizeof(SkScalar), kScalar_Type, 1); |
| 83 | } |
| 84 | |
| 85 | SkScalar* SkMetaData::setScalars(const char name[], int count, const SkScalar values[]) |
| 86 | { |
| 87 | SkASSERT(count > 0); |
| 88 | if (count > 0) |
| 89 | return (SkScalar*)this->set(name, values, sizeof(SkScalar), kScalar_Type, count); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 90 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void SkMetaData::setString(const char name[], const char value[]) |
| 94 | { |
commit-bot@chromium.org | a8c7f77 | 2014-01-24 21:46:29 +0000 | [diff] [blame] | 95 | (void)this->set(name, value, sizeof(char), kString_Type, SkToInt(strlen(value) + 1)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | } |
| 97 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 98 | void SkMetaData::setPtr(const char name[], void* ptr, PtrProc proc) { |
| 99 | PtrPair pair = { ptr, proc }; |
| 100 | (void)this->set(name, &pair, sizeof(PtrPair), kPtr_Type, 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void SkMetaData::setBool(const char name[], bool value) |
| 104 | { |
| 105 | (void)this->set(name, &value, sizeof(bool), kBool_Type, 1); |
| 106 | } |
| 107 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 108 | void SkMetaData::setData(const char name[], const void* data, size_t byteCount) { |
commit-bot@chromium.org | a8c7f77 | 2014-01-24 21:46:29 +0000 | [diff] [blame] | 109 | (void)this->set(name, data, sizeof(char), kData_Type, SkToInt(byteCount)); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 110 | } |
| 111 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | void* SkMetaData::set(const char name[], const void* data, size_t dataSize, Type type, int count) |
| 113 | { |
| 114 | SkASSERT(name); |
| 115 | SkASSERT(dataSize); |
| 116 | SkASSERT(count > 0); |
| 117 | |
| 118 | (void)this->remove(name, type); |
| 119 | |
| 120 | size_t len = strlen(name); |
| 121 | Rec* rec = Rec::Alloc(sizeof(Rec) + dataSize * count + len + 1); |
| 122 | |
| 123 | #ifndef SK_DEBUG |
| 124 | rec->fType = SkToU8(type); |
| 125 | #else |
| 126 | rec->fType = type; |
| 127 | #endif |
| 128 | rec->fDataLen = SkToU8(dataSize); |
| 129 | rec->fDataCount = SkToU16(count); |
| 130 | if (data) |
| 131 | memcpy(rec->data(), data, dataSize * count); |
| 132 | memcpy(rec->name(), name, len + 1); |
| 133 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 134 | if (kPtr_Type == type) { |
| 135 | PtrPair* pair = (PtrPair*)rec->data(); |
| 136 | if (pair->fProc && pair->fPtr) { |
| 137 | pair->fPtr = pair->fProc(pair->fPtr, true); |
| 138 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | |
| 141 | rec->fNext = fRec; |
| 142 | fRec = rec; |
| 143 | return rec->data(); |
| 144 | } |
| 145 | |
| 146 | bool SkMetaData::findS32(const char name[], int32_t* value) const |
| 147 | { |
| 148 | const Rec* rec = this->find(name, kS32_Type); |
| 149 | if (rec) |
| 150 | { |
| 151 | SkASSERT(rec->fDataCount == 1); |
| 152 | if (value) |
| 153 | *value = *(const int32_t*)rec->data(); |
| 154 | return true; |
| 155 | } |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | bool SkMetaData::findScalar(const char name[], SkScalar* value) const |
| 160 | { |
| 161 | const Rec* rec = this->find(name, kScalar_Type); |
| 162 | if (rec) |
| 163 | { |
| 164 | SkASSERT(rec->fDataCount == 1); |
| 165 | if (value) |
| 166 | *value = *(const SkScalar*)rec->data(); |
| 167 | return true; |
| 168 | } |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | const SkScalar* SkMetaData::findScalars(const char name[], int* count, SkScalar values[]) const |
| 173 | { |
| 174 | const Rec* rec = this->find(name, kScalar_Type); |
| 175 | if (rec) |
| 176 | { |
| 177 | if (count) |
| 178 | *count = rec->fDataCount; |
| 179 | if (values) |
| 180 | memcpy(values, rec->data(), rec->fDataCount * rec->fDataLen); |
| 181 | return (const SkScalar*)rec->data(); |
| 182 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 183 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | } |
| 185 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 186 | bool SkMetaData::findPtr(const char name[], void** ptr, PtrProc* proc) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 187 | const Rec* rec = this->find(name, kPtr_Type); |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 188 | if (rec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 189 | SkASSERT(rec->fDataCount == 1); |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 190 | const PtrPair* pair = (const PtrPair*)rec->data(); |
| 191 | if (ptr) { |
| 192 | *ptr = pair->fPtr; |
| 193 | } |
| 194 | if (proc) { |
| 195 | *proc = pair->fProc; |
| 196 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | return true; |
| 198 | } |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | const char* SkMetaData::findString(const char name[]) const |
| 203 | { |
| 204 | const Rec* rec = this->find(name, kString_Type); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 205 | SkASSERT(rec == nullptr || rec->fDataLen == sizeof(char)); |
| 206 | return rec ? (const char*)rec->data() : nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | bool SkMetaData::findBool(const char name[], bool* value) const |
| 210 | { |
| 211 | const Rec* rec = this->find(name, kBool_Type); |
| 212 | if (rec) |
| 213 | { |
| 214 | SkASSERT(rec->fDataCount == 1); |
| 215 | if (value) |
| 216 | *value = *(const bool*)rec->data(); |
| 217 | return true; |
| 218 | } |
| 219 | return false; |
| 220 | } |
| 221 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 222 | const void* SkMetaData::findData(const char name[], size_t* length) const { |
| 223 | const Rec* rec = this->find(name, kData_Type); |
| 224 | if (rec) { |
| 225 | SkASSERT(rec->fDataLen == sizeof(char)); |
| 226 | if (length) { |
| 227 | *length = rec->fDataCount; |
| 228 | } |
| 229 | return rec->data(); |
| 230 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 231 | return nullptr; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 232 | } |
| 233 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 234 | const SkMetaData::Rec* SkMetaData::find(const char name[], Type type) const |
| 235 | { |
| 236 | const Rec* rec = fRec; |
| 237 | while (rec) |
| 238 | { |
| 239 | if (rec->fType == type && !strcmp(rec->name(), name)) |
| 240 | return rec; |
| 241 | rec = rec->fNext; |
| 242 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 243 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 244 | } |
| 245 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 246 | bool SkMetaData::remove(const char name[], Type type) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 247 | Rec* rec = fRec; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 248 | Rec* prev = nullptr; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 249 | while (rec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 250 | Rec* next = rec->fNext; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 251 | if (rec->fType == type && !strcmp(rec->name(), name)) { |
| 252 | if (prev) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 253 | prev->fNext = next; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 254 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 255 | fRec = next; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | if (kPtr_Type == type) { |
| 259 | PtrPair* pair = (PtrPair*)rec->data(); |
| 260 | if (pair->fProc && pair->fPtr) { |
| 261 | (void)pair->fProc(pair->fPtr, false); |
| 262 | } |
| 263 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 264 | Rec::Free(rec); |
| 265 | return true; |
| 266 | } |
| 267 | prev = rec; |
| 268 | rec = next; |
| 269 | } |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | bool SkMetaData::removeS32(const char name[]) |
| 274 | { |
| 275 | return this->remove(name, kS32_Type); |
| 276 | } |
| 277 | |
| 278 | bool SkMetaData::removeScalar(const char name[]) |
| 279 | { |
| 280 | return this->remove(name, kScalar_Type); |
| 281 | } |
| 282 | |
| 283 | bool SkMetaData::removeString(const char name[]) |
| 284 | { |
| 285 | return this->remove(name, kString_Type); |
| 286 | } |
| 287 | |
| 288 | bool SkMetaData::removePtr(const char name[]) |
| 289 | { |
| 290 | return this->remove(name, kPtr_Type); |
| 291 | } |
| 292 | |
| 293 | bool SkMetaData::removeBool(const char name[]) |
| 294 | { |
| 295 | return this->remove(name, kBool_Type); |
| 296 | } |
| 297 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 298 | bool SkMetaData::removeData(const char name[]) { |
| 299 | return this->remove(name, kData_Type); |
| 300 | } |
| 301 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 302 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 303 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 304 | SkMetaData::Iter::Iter(const SkMetaData& metadata) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 305 | fRec = metadata.fRec; |
| 306 | } |
| 307 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 308 | void SkMetaData::Iter::reset(const SkMetaData& metadata) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 309 | fRec = metadata.fRec; |
| 310 | } |
| 311 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 312 | const char* SkMetaData::Iter::next(SkMetaData::Type* t, int* count) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 313 | const char* name = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 314 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 315 | if (fRec) { |
| 316 | if (t) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 317 | *t = (SkMetaData::Type)fRec->fType; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 318 | } |
| 319 | if (count) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 320 | *count = fRec->fDataCount; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 321 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 322 | name = fRec->name(); |
| 323 | |
| 324 | fRec = fRec->fNext; |
| 325 | } |
| 326 | return name; |
| 327 | } |
| 328 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 329 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 330 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 331 | SkMetaData::Rec* SkMetaData::Rec::Alloc(size_t size) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 332 | return (Rec*)sk_malloc_throw(size); |
| 333 | } |
| 334 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 335 | void SkMetaData::Rec::Free(Rec* rec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 336 | sk_free(rec); |
| 337 | } |