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
8 #include "tools/SkMetaData.h"
9
10 #include "include/private/SkMalloc.h"
11 #include "include/private/SkTo.h"
12
reset()13 void SkMetaData::reset()
14 {
15 Rec* rec = fRec;
16 while (rec) {
17 Rec* next = rec->fNext;
18 Rec::Free(rec);
19 rec = next;
20 }
21 fRec = nullptr;
22 }
23
setS32(const char name[],int32_t value)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
setScalar(const char name[],SkScalar value)29 void SkMetaData::setScalar(const char name[], SkScalar value)
30 {
31 (void)this->set(name, &value, sizeof(SkScalar), kScalar_Type, 1);
32 }
33
setScalars(const char name[],int count,const SkScalar values[])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);
39 return nullptr;
40 }
41
setPtr(const char name[],void * ptr)42 void SkMetaData::setPtr(const char name[], void* ptr) {
43 (void)this->set(name, &ptr, sizeof(void*), kPtr_Type, 1);
44 }
45
setBool(const char name[],bool value)46 void SkMetaData::setBool(const char name[], bool value)
47 {
48 (void)this->set(name, &value, sizeof(bool), kBool_Type, 1);
49 }
50
set(const char name[],const void * data,size_t dataSize,Type type,int count)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
73 rec->fNext = fRec;
74 fRec = rec;
75 return rec->data();
76 }
77
findS32(const char name[],int32_t * value) const78 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
findScalar(const char name[],SkScalar * value) const91 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
findScalars(const char name[],int * count,SkScalar values[]) const104 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 }
115 return nullptr;
116 }
117
findPtr(const char name[],void ** ptr) const118 bool SkMetaData::findPtr(const char name[], void** ptr) const {
119 const Rec* rec = this->find(name, kPtr_Type);
120 if (rec) {
121 SkASSERT(rec->fDataCount == 1);
122 void** found = (void**)rec->data();
123 if (ptr) {
124 *ptr = *found;
125 }
126 return true;
127 }
128 return false;
129 }
130
findBool(const char name[],bool * value) const131 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
find(const char name[],Type type) const144 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 }
153 return nullptr;
154 }
155
remove(const char name[],Type type)156 bool SkMetaData::remove(const char name[], Type type) {
157 Rec* rec = fRec;
158 Rec* prev = nullptr;
159 while (rec) {
160 Rec* next = rec->fNext;
161 if (rec->fType == type && !strcmp(rec->name(), name)) {
162 if (prev) {
163 prev->fNext = next;
164 } else {
165 fRec = next;
166 }
167 Rec::Free(rec);
168 return true;
169 }
170 prev = rec;
171 rec = next;
172 }
173 return false;
174 }
175
removeS32(const char name[])176 bool SkMetaData::removeS32(const char name[])
177 {
178 return this->remove(name, kS32_Type);
179 }
180
removeScalar(const char name[])181 bool SkMetaData::removeScalar(const char name[])
182 {
183 return this->remove(name, kScalar_Type);
184 }
185
removePtr(const char name[])186 bool SkMetaData::removePtr(const char name[])
187 {
188 return this->remove(name, kPtr_Type);
189 }
190
removeBool(const char name[])191 bool SkMetaData::removeBool(const char name[])
192 {
193 return this->remove(name, kBool_Type);
194 }
195
196 ///////////////////////////////////////////////////////////////////////////////
197
Iter(const SkMetaData & metadata)198 SkMetaData::Iter::Iter(const SkMetaData& metadata) {
199 fRec = metadata.fRec;
200 }
201
reset(const SkMetaData & metadata)202 void SkMetaData::Iter::reset(const SkMetaData& metadata) {
203 fRec = metadata.fRec;
204 }
205
next(SkMetaData::Type * t,int * count)206 const char* SkMetaData::Iter::next(SkMetaData::Type* t, int* count) {
207 const char* name = nullptr;
208
209 if (fRec) {
210 if (t) {
211 *t = (SkMetaData::Type)fRec->fType;
212 }
213 if (count) {
214 *count = fRec->fDataCount;
215 }
216 name = fRec->name();
217
218 fRec = fRec->fNext;
219 }
220 return name;
221 }
222
223 ///////////////////////////////////////////////////////////////////////////////
224
Alloc(size_t size)225 SkMetaData::Rec* SkMetaData::Rec::Alloc(size_t size) {
226 return (Rec*)sk_malloc_throw(size);
227 }
228
Free(Rec * rec)229 void SkMetaData::Rec::Free(Rec* rec) {
230 sk_free(rec);
231 }
232