1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
#include "SkFlattenable.h"
#include "SkTypeface.h"
void SkFlattenable::flatten(SkFlattenableWriteBuffer&)
{
/* we don't write anything at the moment, but this allows our subclasses
to not know that, since we want them to always call INHERITED::flatten()
in their code.
*/
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
SkFlattenableReadBuffer::SkFlattenableReadBuffer() {
fRCArray = NULL;
fRCCount = 0;
fTFArray = NULL;
fTFCount = 0;
fFactoryArray = NULL;
fFactoryCount = 0;
}
SkFlattenableReadBuffer::SkFlattenableReadBuffer(const void* data) :
INHERITED(data, 1024 * 1024) {
fRCArray = NULL;
fRCCount = 0;
fTFArray = NULL;
fTFCount = 0;
fFactoryArray = NULL;
fFactoryCount = 0;
}
SkFlattenableReadBuffer::SkFlattenableReadBuffer(const void* data, size_t size)
: INHERITED(data, size) {
fRCArray = NULL;
fRCCount = 0;
fTFArray = NULL;
fTFCount = 0;
fFactoryArray = NULL;
fFactoryCount = 0;
}
SkTypeface* SkFlattenableReadBuffer::readTypeface() {
uint32_t index = this->readU32();
if (0 == index || index > (unsigned)fTFCount) {
if (index) {
SkDebugf("====== typeface index %d\n", index);
}
return NULL;
} else {
SkASSERT(fTFArray);
return fTFArray[index - 1];
}
}
SkRefCnt* SkFlattenableReadBuffer::readRefCnt() {
uint32_t index = this->readU32();
if (0 == index || index > (unsigned)fRCCount) {
return NULL;
} else {
SkASSERT(fRCArray);
return fRCArray[index - 1];
}
}
SkFlattenable* SkFlattenableReadBuffer::readFlattenable() {
SkFlattenable::Factory factory = NULL;
if (fFactoryCount > 0) {
uint32_t index = this->readU32();
if (index > 0) {
index -= 1;
SkASSERT(index < (unsigned)fFactoryCount);
factory = fFactoryArray[index];
// if we recorded an index, but failed to get a factory, we need
// to skip the flattened data in the buffer
if (NULL == factory) {
uint32_t size = this->readU32();
this->skip(size);
// fall through and return NULL for the object
}
}
} else {
factory = (SkFlattenable::Factory)readFunctionPtr();
}
SkFlattenable* obj = NULL;
if (factory) {
uint32_t sizeRecorded = this->readU32();
uint32_t offset = this->offset();
obj = (*factory)(*this);
// check that we read the amount we expected
uint32_t sizeRead = this->offset() - offset;
if (sizeRecorded != sizeRead) {
// we could try to fix up the offset...
sk_throw();
}
}
return obj;
}
void* SkFlattenableReadBuffer::readFunctionPtr() {
void* proc;
this->read(&proc, sizeof(proc));
return proc;
}
///////////////////////////////////////////////////////////////////////////////
SkFlattenableWriteBuffer::SkFlattenableWriteBuffer(size_t minSize) :
INHERITED(minSize) {
fFlags = (Flags)0;
fRCRecorder = NULL;
fTFRecorder = NULL;
fFactoryRecorder = NULL;
}
SkFlattenableWriteBuffer::~SkFlattenableWriteBuffer() {
fRCRecorder->safeUnref();
fTFRecorder->safeUnref();
fFactoryRecorder->safeUnref();
}
SkRefCntRecorder* SkFlattenableWriteBuffer::setRefCntRecorder(
SkRefCntRecorder* rec) {
SkRefCnt_SafeAssign(fRCRecorder, rec);
return rec;
}
SkRefCntRecorder* SkFlattenableWriteBuffer::setTypefaceRecorder(
SkRefCntRecorder* rec) {
SkRefCnt_SafeAssign(fTFRecorder, rec);
return rec;
}
SkFactoryRecorder* SkFlattenableWriteBuffer::setFactoryRecorder(
SkFactoryRecorder* rec) {
SkRefCnt_SafeAssign(fFactoryRecorder, rec);
return rec;
}
void SkFlattenableWriteBuffer::writeTypeface(SkTypeface* obj) {
if (NULL == obj || NULL == fTFRecorder) {
this->write32(0);
} else {
this->write32(fTFRecorder->record(obj));
}
}
void SkFlattenableWriteBuffer::writeRefCnt(SkRefCnt* obj) {
if (NULL == obj || NULL == fRCRecorder) {
this->write32(0);
} else {
this->write32(fRCRecorder->record(obj));
}
}
void SkFlattenableWriteBuffer::writeFlattenable(SkFlattenable* flattenable) {
SkFlattenable::Factory factory = NULL;
if (flattenable) {
factory = flattenable->getFactory();
}
if (fFactoryRecorder) {
this->write32(fFactoryRecorder->record(factory));
} else {
this->writeFunctionPtr((void*)factory);
}
if (factory) {
// make room for the size of the flatttened object
(void)this->reserve(sizeof(uint32_t));
// record the current size, so we can subtract after the object writes.
uint32_t offset = this->size();
// now flatten the object
flattenable->flatten(*this);
uint32_t objSize = this->size() - offset;
// record the obj's size
*this->peek32(offset - sizeof(uint32_t)) = objSize;
}
}
void SkFlattenableWriteBuffer::writeFunctionPtr(void* proc) {
*(void**)this->reserve(sizeof(void*)) = proc;
}
///////////////////////////////////////////////////////////////////////////////
SkRefCntRecorder::~SkRefCntRecorder() {
// call this now, while our decPtr() is sill in scope
this->reset();
}
void SkRefCntRecorder::incPtr(void* ptr) {
((SkRefCnt*)ptr)->ref();
}
void SkRefCntRecorder::decPtr(void* ptr) {
((SkRefCnt*)ptr)->unref();
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#define MAX_PAIR_COUNT 64
struct Pair {
const char* fName;
SkFlattenable::Factory fFactory;
};
static int gCount;
static Pair gPairs[MAX_PAIR_COUNT];
void SkFlattenable::Register(const char name[], Factory factory) {
SkASSERT(name);
SkASSERT(factory);
static bool gOnce;
if (!gOnce) {
gCount = 0;
gOnce = true;
}
SkASSERT(gCount < MAX_PAIR_COUNT);
gPairs[gCount].fName = name;
gPairs[gCount].fFactory = factory;
gCount += 1;
}
SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) {
const Pair* pairs = gPairs;
for (int i = gCount - 1; i >= 0; --i) {
if (strcmp(pairs[i].fName, name) == 0) {
return pairs[i].fFactory;
}
}
return NULL;
}
const char* SkFlattenable::FactoryToName(Factory fact) {
const Pair* pairs = gPairs;
for (int i = gCount - 1; i >= 0; --i) {
if (pairs[i].fFactory == fact) {
return pairs[i].fName;
}
}
return NULL;
}
bool SkFlattenable::toDumpString(SkString* str) const {
return false;
}
|