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 | |
| 57 | (void)this->remove(name, type); |
| 58 | |
| 59 | size_t len = strlen(name); |
| 60 | Rec* rec = Rec::Alloc(sizeof(Rec) + dataSize * count + len + 1); |
| 61 | |
| 62 | #ifndef SK_DEBUG |
| 63 | rec->fType = SkToU8(type); |
| 64 | #else |
| 65 | rec->fType = type; |
| 66 | #endif |
| 67 | rec->fDataLen = SkToU8(dataSize); |
| 68 | rec->fDataCount = SkToU16(count); |
| 69 | if (data) |
| 70 | memcpy(rec->data(), data, dataSize * count); |
| 71 | memcpy(rec->name(), name, len + 1); |
| 72 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | rec->fNext = fRec; |
| 74 | fRec = rec; |
| 75 | return rec->data(); |
| 76 | } |
| 77 | |
| 78 | bool SkMetaData::findS32(const char name[], int32_t* value) const |
| 79 | { |
| 80 | const Rec* rec = this->find(name, kS32_Type); |
| 81 | if (rec) |
| 82 | { |
| 83 | SkASSERT(rec->fDataCount == 1); |
| 84 | if (value) |
| 85 | *value = *(const int32_t*)rec->data(); |
| 86 | return true; |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | bool SkMetaData::findScalar(const char name[], SkScalar* value) const |
| 92 | { |
| 93 | const Rec* rec = this->find(name, kScalar_Type); |
| 94 | if (rec) |
| 95 | { |
| 96 | SkASSERT(rec->fDataCount == 1); |
| 97 | if (value) |
| 98 | *value = *(const SkScalar*)rec->data(); |
| 99 | return true; |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | const SkScalar* SkMetaData::findScalars(const char name[], int* count, SkScalar values[]) const |
| 105 | { |
| 106 | const Rec* rec = this->find(name, kScalar_Type); |
| 107 | if (rec) |
| 108 | { |
| 109 | if (count) |
| 110 | *count = rec->fDataCount; |
| 111 | if (values) |
| 112 | memcpy(values, rec->data(), rec->fDataCount * rec->fDataLen); |
| 113 | return (const SkScalar*)rec->data(); |
| 114 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 115 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Hal Canary | ca9e187 | 2019-07-25 10:36:30 -0400 | [diff] [blame] | 118 | bool SkMetaData::findPtr(const char name[], void** ptr) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | const Rec* rec = this->find(name, kPtr_Type); |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 120 | if (rec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | SkASSERT(rec->fDataCount == 1); |
Hal Canary | ca9e187 | 2019-07-25 10:36:30 -0400 | [diff] [blame] | 122 | void** found = (void**)rec->data(); |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 123 | if (ptr) { |
Hal Canary | ca9e187 | 2019-07-25 10:36:30 -0400 | [diff] [blame] | 124 | *ptr = *found; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 125 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | bool SkMetaData::findBool(const char name[], bool* value) const |
| 132 | { |
| 133 | const Rec* rec = this->find(name, kBool_Type); |
| 134 | if (rec) |
| 135 | { |
| 136 | SkASSERT(rec->fDataCount == 1); |
| 137 | if (value) |
| 138 | *value = *(const bool*)rec->data(); |
| 139 | return true; |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | const SkMetaData::Rec* SkMetaData::find(const char name[], Type type) const |
| 145 | { |
| 146 | const Rec* rec = fRec; |
| 147 | while (rec) |
| 148 | { |
| 149 | if (rec->fType == type && !strcmp(rec->name(), name)) |
| 150 | return rec; |
| 151 | rec = rec->fNext; |
| 152 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 153 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 154 | } |
| 155 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 156 | bool SkMetaData::remove(const char name[], Type type) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | Rec* rec = fRec; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 158 | Rec* prev = nullptr; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 159 | while (rec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | Rec* next = rec->fNext; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 161 | if (rec->fType == type && !strcmp(rec->name(), name)) { |
| 162 | if (prev) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | prev->fNext = next; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 164 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | fRec = next; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 166 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | Rec::Free(rec); |
| 168 | return true; |
| 169 | } |
| 170 | prev = rec; |
| 171 | rec = next; |
| 172 | } |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | bool SkMetaData::removeS32(const char name[]) |
| 177 | { |
| 178 | return this->remove(name, kS32_Type); |
| 179 | } |
| 180 | |
| 181 | bool SkMetaData::removeScalar(const char name[]) |
| 182 | { |
| 183 | return this->remove(name, kScalar_Type); |
| 184 | } |
| 185 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 186 | bool SkMetaData::removePtr(const char name[]) |
| 187 | { |
| 188 | return this->remove(name, kPtr_Type); |
| 189 | } |
| 190 | |
| 191 | bool SkMetaData::removeBool(const char name[]) |
| 192 | { |
| 193 | return this->remove(name, kBool_Type); |
| 194 | } |
| 195 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 196 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 198 | SkMetaData::Iter::Iter(const SkMetaData& metadata) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 199 | fRec = metadata.fRec; |
| 200 | } |
| 201 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 202 | void SkMetaData::Iter::reset(const SkMetaData& metadata) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | fRec = metadata.fRec; |
| 204 | } |
| 205 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 206 | const char* SkMetaData::Iter::next(SkMetaData::Type* t, int* count) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 207 | const char* name = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 208 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 209 | if (fRec) { |
| 210 | if (t) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 211 | *t = (SkMetaData::Type)fRec->fType; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 212 | } |
| 213 | if (count) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | *count = fRec->fDataCount; |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 215 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | name = fRec->name(); |
| 217 | |
| 218 | fRec = fRec->fNext; |
| 219 | } |
| 220 | return name; |
| 221 | } |
| 222 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 223 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 224 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 225 | SkMetaData::Rec* SkMetaData::Rec::Alloc(size_t size) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 226 | return (Rec*)sk_malloc_throw(size); |
| 227 | } |
| 228 | |
reed@google.com | e733071 | 2011-03-30 18:23:21 +0000 | [diff] [blame] | 229 | void SkMetaData::Rec::Free(Rec* rec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 230 | sk_free(rec); |
| 231 | } |