Flutter Engine
The Flutter Engine
SkMetaData.cpp
Go to the documentation of this file.
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
12
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
24void SkMetaData::setS32(const char name[], int32_t value)
25{
26 (void)this->set(name, &value, sizeof(int32_t), kS32_Type, 1);
27}
28
30{
31 (void)this->set(name, &value, sizeof(SkScalar), kScalar_Type, 1);
32}
33
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
42void SkMetaData::setPtr(const char name[], void* ptr) {
43 (void)this->set(name, &ptr, sizeof(void*), kPtr_Type, 1);
44}
45
46void SkMetaData::setBool(const char name[], bool value)
47{
48 (void)this->set(name, &value, sizeof(bool), kBool_Type, 1);
49}
50
51void* SkMetaData::set(const char name[], const void* data, size_t dataSize, Type type, int count)
52{
54 SkASSERT(dataSize);
55 SkASSERT(count > 0);
56
57 FindResult result = this->findWithPrev(name, type);
58
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);
71
72 memcpy(rec->name(), name, len + 1);
73 }
74 if (data) {
75 memcpy(rec->data(), data, dataSize * count);
76 }
77
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 }
93 return rec->data();
94}
95
96bool 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
109bool 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
122const 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 }
133 return nullptr;
134}
135
136bool SkMetaData::findPtr(const char name[], void** ptr) const {
137 const Rec* rec = this->find(name, kPtr_Type);
138 if (rec) {
139 SkASSERT(rec->fDataCount == 1);
140 void* const* found = (void* const*)rec->data();
141 if (ptr) {
142 *ptr = *found;
143 }
144 return true;
145 }
146 return false;
147}
148
149bool 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
162SkMetaData::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;
169 }
170 return current;
171}
172
173
174const SkMetaData::Rec* SkMetaData::find(const char name[], Type type) const {
175 return this->findWithPrev(name, type).rec;
176}
177
178void 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);
186}
187
188bool SkMetaData::remove(const char name[], Type type) {
189 FindResult result = this->findWithPrev(name, type);
190 if (!result.rec) {
191 return false;
192 }
193 this->remove(result);
194 return true;
195}
196
197bool SkMetaData::removeS32(const char name[])
198{
199 return this->remove(name, kS32_Type);
200}
201
203{
204 return this->remove(name, kScalar_Type);
205}
206
207bool SkMetaData::removePtr(const char name[])
208{
209 return this->remove(name, kPtr_Type);
210}
211
212bool SkMetaData::removeBool(const char name[])
213{
214 return this->remove(name, kBool_Type);
215}
216
217///////////////////////////////////////////////////////////////////////////////
218
220 fRec = metadata.fRec;
221}
222
223void SkMetaData::Iter::reset(const SkMetaData& metadata) {
224 fRec = metadata.fRec;
225}
226
228 const char* name = nullptr;
229
230 if (fRec) {
231 if (t) {
233 }
234 if (count) {
236 }
237 name = fRec->name();
238
239 fRec = fRec->fNext;
240 }
241 return name;
242}
243
244///////////////////////////////////////////////////////////////////////////////
245
247 return (Rec*)sk_malloc_throw(size);
248}
249
251 sk_free(rec);
252}
int count
Definition: FontMgrTest.cpp:50
static float next(float f)
#define SkASSERT(cond)
Definition: SkAssert.h:116
SK_API void sk_free(void *)
static void * sk_malloc_throw(size_t size)
Definition: SkMalloc.h:67
constexpr uint16_t SkToU16(S x)
Definition: SkTo.h:24
constexpr uint8_t SkToU8(S x)
Definition: SkTo.h:22
GLenum type
const char * next(Type *, int *count)
Definition: SkMetaData.cpp:227
void reset(const SkMetaData &)
Definition: SkMetaData.cpp:223
const SkScalar * findScalars(const char name[], int *count, SkScalar values[]=nullptr) const
Definition: SkMetaData.cpp:122
bool removeBool(const char name[])
Definition: SkMetaData.cpp:212
void reset()
Definition: SkMetaData.cpp:13
void * set(const char name[], const void *data, size_t len, Type, int count)
Definition: SkMetaData.cpp:51
bool removePtr(const char name[])
Definition: SkMetaData.cpp:207
bool findScalar(const char name[], SkScalar *value=nullptr) const
Definition: SkMetaData.cpp:109
@ kScalar_Type
Definition: SkMetaData.h:58
void setS32(const char name[], int32_t value)
Definition: SkMetaData.cpp:24
void setPtr(const char name[], void *value)
Definition: SkMetaData.cpp:42
bool removeScalar(const char name[])
Definition: SkMetaData.cpp:202
SkScalar * setScalars(const char name[], int count, const SkScalar values[]=nullptr)
Definition: SkMetaData.cpp:34
bool removeS32(const char name[])
Definition: SkMetaData.cpp:197
void setBool(const char name[], bool value)
Definition: SkMetaData.cpp:46
bool remove(const char name[], Type)
Definition: SkMetaData.cpp:188
void setScalar(const char name[], SkScalar value)
Definition: SkMetaData.cpp:29
bool findBool(const char name[], bool *value=nullptr) const
Definition: SkMetaData.cpp:149
Rec * fRec
Definition: SkMetaData.h:105
bool findS32(const char name[], int32_t *value=nullptr) const
Definition: SkMetaData.cpp:96
bool findPtr(const char name[], void **value=nullptr) const
Definition: SkMetaData.cpp:136
const Rec * find(const char name[], Type) const
Definition: SkMetaData.cpp:174
float SkScalar
Definition: extension.cpp:12
uint8_t value
GAsyncResult * result
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
const char * name() const
Definition: SkMetaData.h:99
uint8_t fType
Definition: SkMetaData.h:95
const void * data() const
Definition: SkMetaData.h:97
static Rec * Alloc(size_t)
Definition: SkMetaData.cpp:246
uint16_t fDataCount
Definition: SkMetaData.h:93
static void Free(Rec *)
Definition: SkMetaData.cpp:250
uint8_t fDataLen
Definition: SkMetaData.h:94
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63