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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkFlattenable.h"
#include "SkTypeface.h"
#include "SkMatrix.h"
#include "SkRegion.h"
void SkReadMatrix(SkReader32* reader, SkMatrix* matrix) {
size_t size = matrix->unflatten(reader->peek());
SkASSERT(SkAlign4(size) == size);
(void)reader->skip(size);
}
void SkWriteMatrix(SkWriter32* writer, const SkMatrix& matrix) {
size_t size = matrix.flatten(NULL);
SkASSERT(SkAlign4(size) == size);
matrix.flatten(writer->reserve(size));
}
void SkReadRegion(SkReader32* reader, SkRegion* rgn) {
size_t size = rgn->unflatten(reader->peek());
SkASSERT(SkAlign4(size) == size);
(void)reader->skip(size);
}
void SkWriteRegion(SkWriter32* writer, const SkRegion& rgn) {
size_t size = rgn.flatten(NULL);
SkASSERT(SkAlign4(size) == size);
rgn.flatten(writer->reserve(size));
}
///////////////////////////////////////////////////////////////////////////////
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;
fFactoryTDArray = NULL;
fFactoryArray = NULL;
fFactoryCount = 0;
}
SkFlattenableReadBuffer::SkFlattenableReadBuffer(const void* data) :
INHERITED(data, 1024 * 1024) {
fRCArray = NULL;
fRCCount = 0;
fTFArray = NULL;
fTFCount = 0;
fFactoryTDArray = NULL;
fFactoryArray = NULL;
fFactoryCount = 0;
}
SkFlattenableReadBuffer::SkFlattenableReadBuffer(const void* data, size_t size)
: INHERITED(data, size) {
fRCArray = NULL;
fRCCount = 0;
fTFArray = NULL;
fTFCount = 0;
fFactoryTDArray = NULL;
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) {
int32_t index = this->readU32();
if (0 == index) {
return NULL; // writer failed to give us the flattenable
}
index = -index; // we stored the negative of the index
index -= 1; // we stored the index-base-1
SkASSERT(index < fFactoryCount);
factory = fFactoryArray[index];
} else if (fFactoryTDArray) {
const int32_t* peek = (const int32_t*)this->peek();
if (*peek <= 0) {
int32_t index = this->readU32();
if (0 == index) {
return NULL; // writer failed to give us the flattenable
}
index = -index; // we stored the negative of the index
index -= 1; // we stored the index-base-1
factory = (*fFactoryTDArray)[index];
} else {
const char* name = this->readString();
factory = SkFlattenable::NameToFactory(name);
if (factory) {
SkASSERT(fFactoryTDArray->find(factory) < 0);
*fFactoryTDArray->append() = factory;
} else {
// SkDebugf("can't find factory for [%s]\n", name);
}
// if we didn't find a factory, that's our failure, not the writer's,
// so we fall through, so we can skip the sizeRecorded data.
}
} else {
factory = (SkFlattenable::Factory)readFunctionPtr();
if (NULL == factory) {
return NULL; // writer failed to give us the flattenable
}
}
// if we get here, factory may still be null, but if that is the case, the
// failure was ours, not the writer.
SkFlattenable* obj = NULL;
uint32_t sizeRecorded = this->readU32();
if (factory) {
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();
}
} else {
// we must skip the remaining data
this->skip(sizeRecorded);
}
return obj;
}
void* SkFlattenableReadBuffer::readFunctionPtr() {
void* proc;
this->read(&proc, sizeof(proc));
return proc;
}
///////////////////////////////////////////////////////////////////////////////
SkFlattenableWriteBuffer::SkFlattenableWriteBuffer(size_t minSize) :
INHERITED(minSize) {
fFlags = (Flags)0;
fRCSet = NULL;
fTFSet = NULL;
fFactorySet = NULL;
}
SkFlattenableWriteBuffer::~SkFlattenableWriteBuffer() {
SkSafeUnref(fRCSet);
SkSafeUnref(fTFSet);
SkSafeUnref(fFactorySet);
}
SkRefCntSet* SkFlattenableWriteBuffer::setRefCntRecorder(SkRefCntSet* rec) {
SkRefCnt_SafeAssign(fRCSet, rec);
return rec;
}
SkRefCntSet* SkFlattenableWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) {
SkRefCnt_SafeAssign(fTFSet, rec);
return rec;
}
SkFactorySet* SkFlattenableWriteBuffer::setFactoryRecorder(SkFactorySet* rec) {
SkRefCnt_SafeAssign(fFactorySet, rec);
return rec;
}
void SkFlattenableWriteBuffer::writeTypeface(SkTypeface* obj) {
if (NULL == obj || NULL == fTFSet) {
this->write32(0);
} else {
this->write32(fTFSet->add(obj));
}
}
void SkFlattenableWriteBuffer::writeRefCnt(SkRefCnt* obj) {
if (NULL == obj || NULL == fRCSet) {
this->write32(0);
} else {
this->write32(fRCSet->add(obj));
}
}
void SkFlattenableWriteBuffer::writeFlattenable(SkFlattenable* flattenable) {
/*
* If we have a factoryset, then the first 32bits tell us...
* 0: failure to write the flattenable
* <0: we store the negative of the (1-based) index
* >0: the length of the name
* If we don't have a factoryset, then the first "ptr" is either the
* factory, or null for failure.
*
* The distinction is important, since 0-index is 32bits (always), but a
* 0-functionptr might be 32 or 64 bits.
*/
SkFlattenable::Factory factory = NULL;
if (flattenable) {
factory = flattenable->getFactory();
}
if (NULL == factory) {
if (fFactorySet) {
this->write32(0);
} else {
this->writeFunctionPtr(NULL);
}
return;
}
/*
* We can write 1 of 3 versions of the flattenable:
* 1. function-ptr : this is the fastest for the reader, but assumes that
* the writer and reader are in the same process.
* 2. index into fFactorySet : This is assumes the writer will later
* resolve the function-ptrs into strings for its reader. SkPicture
* does exactly this, by writing a table of names (matching the indices)
* up front in its serialized form.
* 3. names : Reuse fFactorySet to store indices, but only after we've
* written the name the first time. SkGPipe uses this technique, as it
* doesn't require the reader to be told to know the table of names
* up front.
*/
if (fFactorySet) {
if (this->inlineFactoryNames()) {
int index = fFactorySet->find(factory);
if (index) {
// we write the negative of the index, to distinguish it from
// the length of a string
this->write32(-index);
} else {
const char* name = SkFlattenable::FactoryToName(factory);
if (NULL == name) {
this->write32(0);
return;
}
this->writeString(name);
index = fFactorySet->add(factory);
}
} else {
// we write the negative of the index, to distinguish it from
// the length of a string
this->write32(-(int)fFactorySet->add(factory));
}
} else {
this->writeFunctionPtr((void*)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;
}
///////////////////////////////////////////////////////////////////////////////
SkRefCntSet::~SkRefCntSet() {
// call this now, while our decPtr() is sill in scope
this->reset();
}
void SkRefCntSet::incPtr(void* ptr) {
((SkRefCnt*)ptr)->ref();
}
void SkRefCntSet::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;
}
#if !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS && defined(SK_DEBUG)
static void report_no_entries(const char* functionName) {
if (!gCount) {
SkDebugf("%s has no registered name/factory pairs."
" Call SkGraphics::Init() at process initialization time.",
functionName);
}
}
#endif
SkFlattenable::Factory SkFlattenable::NameToFactory(const char name[]) {
#if !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS && defined(SK_DEBUG)
report_no_entries(__FUNCTION__);
#endif
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) {
#if !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS && defined(SK_DEBUG)
report_no_entries(__FUNCTION__);
#endif
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;
}
|