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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
|
/*
* 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 <new>
#include "SkPictureData.h"
#include "SkPictureRecord.h"
#include "SkReadBuffer.h"
#include "SkTextBlob.h"
#include "SkTypeface.h"
#include "SkWriteBuffer.h"
#if SK_SUPPORT_GPU
#include "GrContext.h"
#endif
template <typename T> int SafeCount(const T* obj) {
return obj ? obj->count() : 0;
}
SkPictureData::SkPictureData(const SkPictInfo& info)
: fInfo(info) {
this->init();
}
void SkPictureData::initForPlayback() const {
// ensure that the paths bounds are pre-computed
for (int i = 0; i < fPaths.count(); i++) {
fPaths[i].updateBoundsCache();
}
}
SkPictureData::SkPictureData(const SkPictureRecord& record,
const SkPictInfo& info,
bool deepCopyOps)
: fInfo(info) {
this->init();
fOpData = record.opData(deepCopyOps);
fContentInfo.set(record.fContentInfo);
fBitmaps = record.fBitmaps;
fPaints = record.fPaints;
fPaths = record.fPaths;
this->initForPlayback();
const SkTDArray<const SkPicture* >& pictures = record.getPictureRefs();
fPictureCount = pictures.count();
if (fPictureCount > 0) {
fPictureRefs = SkNEW_ARRAY(const SkPicture*, fPictureCount);
for (int i = 0; i < fPictureCount; i++) {
fPictureRefs[i] = pictures[i];
fPictureRefs[i]->ref();
}
}
// templatize to consolidate with similar picture logic?
const SkTDArray<const SkTextBlob*>& blobs = record.getTextBlobRefs();
fTextBlobCount = blobs.count();
if (fTextBlobCount > 0) {
fTextBlobRefs = SkNEW_ARRAY(const SkTextBlob*, fTextBlobCount);
for (int i = 0; i < fTextBlobCount; ++i) {
fTextBlobRefs[i] = SkRef(blobs[i]);
}
}
}
void SkPictureData::init() {
fPictureRefs = NULL;
fPictureCount = 0;
fTextBlobRefs = NULL;
fTextBlobCount = 0;
fOpData = NULL;
fFactoryPlayback = NULL;
}
SkPictureData::~SkPictureData() {
SkSafeUnref(fOpData);
for (int i = 0; i < fPictureCount; i++) {
fPictureRefs[i]->unref();
}
SkDELETE_ARRAY(fPictureRefs);
for (int i = 0; i < fTextBlobCount; i++) {
fTextBlobRefs[i]->unref();
}
SkDELETE_ARRAY(fTextBlobRefs);
SkDELETE(fFactoryPlayback);
}
bool SkPictureData::containsBitmaps() const {
if (fBitmaps.count() > 0) {
return true;
}
for (int i = 0; i < fPictureCount; ++i) {
if (fPictureRefs[i]->willPlayBackBitmaps()) {
return true;
}
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include "SkStream.h"
static size_t compute_chunk_size(SkFlattenable::Factory* array, int count) {
size_t size = 4; // for 'count'
for (int i = 0; i < count; i++) {
const char* name = SkFlattenable::FactoryToName(array[i]);
if (NULL == name || 0 == *name) {
size += SkWStream::SizeOfPackedUInt(0);
} else {
size_t len = strlen(name);
size += SkWStream::SizeOfPackedUInt(len);
size += len;
}
}
return size;
}
static void write_tag_size(SkWriteBuffer& buffer, uint32_t tag, size_t size) {
buffer.writeUInt(tag);
buffer.writeUInt(SkToU32(size));
}
static void write_tag_size(SkWStream* stream, uint32_t tag, size_t size) {
stream->write32(tag);
stream->write32(SkToU32(size));
}
void SkPictureData::WriteFactories(SkWStream* stream, const SkFactorySet& rec) {
int count = rec.count();
SkAutoSTMalloc<16, SkFlattenable::Factory> storage(count);
SkFlattenable::Factory* array = (SkFlattenable::Factory*)storage.get();
rec.copyToArray(array);
size_t size = compute_chunk_size(array, count);
// TODO: write_tag_size should really take a size_t
write_tag_size(stream, SK_PICT_FACTORY_TAG, (uint32_t) size);
SkDEBUGCODE(size_t start = stream->bytesWritten());
stream->write32(count);
for (int i = 0; i < count; i++) {
const char* name = SkFlattenable::FactoryToName(array[i]);
if (NULL == name || 0 == *name) {
stream->writePackedUInt(0);
} else {
size_t len = strlen(name);
stream->writePackedUInt(len);
stream->write(name, len);
}
}
SkASSERT(size == (stream->bytesWritten() - start));
}
void SkPictureData::WriteTypefaces(SkWStream* stream, const SkRefCntSet& rec) {
int count = rec.count();
write_tag_size(stream, SK_PICT_TYPEFACE_TAG, count);
SkAutoSTMalloc<16, SkTypeface*> storage(count);
SkTypeface** array = (SkTypeface**)storage.get();
rec.copyToArray((SkRefCnt**)array);
for (int i = 0; i < count; i++) {
#ifdef SK_PICTURE_FORCE_FONT_EMBEDDING
array[i]->serializeForcingEmbedding(stream);
#else
// TODO: if (embedFonts) { array[i]->serializeForcingEmbedding(stream) } else
array[i]->serialize(stream);
#endif
}
}
void SkPictureData::flattenToBuffer(SkWriteBuffer& buffer) const {
int i, n;
if ((n = fBitmaps.count()) > 0) {
write_tag_size(buffer, SK_PICT_BITMAP_BUFFER_TAG, n);
for (i = 0; i < n; i++) {
buffer.writeBitmap(fBitmaps[i]);
}
}
if ((n = fPaints.count()) > 0) {
write_tag_size(buffer, SK_PICT_PAINT_BUFFER_TAG, n);
for (i = 0; i < n; i++) {
buffer.writePaint(fPaints[i]);
}
}
if ((n = fPaths.count()) > 0) {
write_tag_size(buffer, SK_PICT_PATH_BUFFER_TAG, n);
buffer.writeInt(n);
for (int i = 0; i < n; i++) {
buffer.writePath(fPaths[i]);
}
}
if (fTextBlobCount > 0) {
write_tag_size(buffer, SK_PICT_TEXTBLOB_BUFFER_TAG, fTextBlobCount);
for (i = 0; i < fTextBlobCount; ++i) {
fTextBlobRefs[i]->flatten(buffer);
}
}
}
void SkPictureData::serialize(SkWStream* stream,
SkPixelSerializer* pixelSerializer) const {
write_tag_size(stream, SK_PICT_READER_TAG, fOpData->size());
stream->write(fOpData->bytes(), fOpData->size());
if (fPictureCount > 0) {
write_tag_size(stream, SK_PICT_PICTURE_TAG, fPictureCount);
for (int i = 0; i < fPictureCount; i++) {
fPictureRefs[i]->serialize(stream, pixelSerializer);
}
}
// Write some of our data into a writebuffer, and then serialize that
// into our stream
{
SkRefCntSet typefaceSet;
SkFactorySet factSet;
SkWriteBuffer buffer(SkWriteBuffer::kCrossProcess_Flag);
buffer.setTypefaceRecorder(&typefaceSet);
buffer.setFactoryRecorder(&factSet);
buffer.setPixelSerializer(pixelSerializer);
this->flattenToBuffer(buffer);
// We have to write these two sets into the stream *before* we write
// the buffer, since parsing that buffer will require that we already
// have these sets available to use.
WriteFactories(stream, factSet);
WriteTypefaces(stream, typefaceSet);
write_tag_size(stream, SK_PICT_BUFFER_SIZE_TAG, buffer.bytesWritten());
buffer.writeToStream(stream);
}
stream->write32(SK_PICT_EOF_TAG);
}
void SkPictureData::flatten(SkWriteBuffer& buffer) const {
write_tag_size(buffer, SK_PICT_READER_TAG, fOpData->size());
buffer.writeByteArray(fOpData->bytes(), fOpData->size());
if (fPictureCount > 0) {
write_tag_size(buffer, SK_PICT_PICTURE_TAG, fPictureCount);
for (int i = 0; i < fPictureCount; i++) {
fPictureRefs[i]->flatten(buffer);
}
}
// Write this picture playback's data into a writebuffer
this->flattenToBuffer(buffer);
buffer.write32(SK_PICT_EOF_TAG);
}
///////////////////////////////////////////////////////////////////////////////
/**
* Return the corresponding SkReadBuffer flags, given a set of
* SkPictInfo flags.
*/
static uint32_t pictInfoFlagsToReadBufferFlags(uint32_t pictInfoFlags) {
static const struct {
uint32_t fSrc;
uint32_t fDst;
} gSD[] = {
{ SkPictInfo::kCrossProcess_Flag, SkReadBuffer::kCrossProcess_Flag },
{ SkPictInfo::kScalarIsFloat_Flag, SkReadBuffer::kScalarIsFloat_Flag },
{ SkPictInfo::kPtrIs64Bit_Flag, SkReadBuffer::kPtrIs64Bit_Flag },
};
uint32_t rbMask = 0;
for (size_t i = 0; i < SK_ARRAY_COUNT(gSD); ++i) {
if (pictInfoFlags & gSD[i].fSrc) {
rbMask |= gSD[i].fDst;
}
}
return rbMask;
}
bool SkPictureData::parseStreamTag(SkStream* stream,
uint32_t tag,
uint32_t size,
SkPicture::InstallPixelRefProc proc) {
/*
* By the time we encounter BUFFER_SIZE_TAG, we need to have already seen
* its dependents: FACTORY_TAG and TYPEFACE_TAG. These two are not required
* but if they are present, they need to have been seen before the buffer.
*
* We assert that if/when we see either of these, that we have not yet seen
* the buffer tag, because if we have, then its too-late to deal with the
* factories or typefaces.
*/
SkDEBUGCODE(bool haveBuffer = false;)
switch (tag) {
case SK_PICT_READER_TAG:
SkASSERT(NULL == fOpData);
fOpData = SkData::NewFromStream(stream, size);
if (!fOpData) {
return false;
}
break;
case SK_PICT_FACTORY_TAG: {
SkASSERT(!haveBuffer);
size = stream->readU32();
fFactoryPlayback = SkNEW_ARGS(SkFactoryPlayback, (size));
for (size_t i = 0; i < size; i++) {
SkString str;
const size_t len = stream->readPackedUInt();
str.resize(len);
if (stream->read(str.writable_str(), len) != len) {
return false;
}
fFactoryPlayback->base()[i] = SkFlattenable::NameToFactory(str.c_str());
}
} break;
case SK_PICT_TYPEFACE_TAG: {
SkASSERT(!haveBuffer);
const int count = SkToInt(size);
fTFPlayback.setCount(count);
for (int i = 0; i < count; i++) {
SkAutoTUnref<SkTypeface> tf(SkTypeface::Deserialize(stream));
if (!tf.get()) { // failed to deserialize
// fTFPlayback asserts it never has a null, so we plop in
// the default here.
tf.reset(SkTypeface::RefDefault());
}
fTFPlayback.set(i, tf);
}
} break;
case SK_PICT_PICTURE_TAG: {
fPictureCount = size;
fPictureRefs = SkNEW_ARRAY(const SkPicture*, fPictureCount);
bool success = true;
int i = 0;
for ( ; i < fPictureCount; i++) {
fPictureRefs[i] = SkPicture::CreateFromStream(stream, proc);
if (NULL == fPictureRefs[i]) {
success = false;
break;
}
}
if (!success) {
// Delete all of the pictures that were already created (up to but excluding i):
for (int j = 0; j < i; j++) {
fPictureRefs[j]->unref();
}
// Delete the array
SkDELETE_ARRAY(fPictureRefs);
fPictureCount = 0;
return false;
}
} break;
case SK_PICT_BUFFER_SIZE_TAG: {
SkAutoMalloc storage(size);
if (stream->read(storage.get(), size) != size) {
return false;
}
/* Should we use SkValidatingReadBuffer instead? */
SkReadBuffer buffer(storage.get(), size);
buffer.setFlags(pictInfoFlagsToReadBufferFlags(fInfo.fFlags));
buffer.setVersion(fInfo.fVersion);
fFactoryPlayback->setupBuffer(buffer);
fTFPlayback.setupBuffer(buffer);
buffer.setBitmapDecoder(proc);
while (!buffer.eof() && buffer.isValid()) {
tag = buffer.readUInt();
size = buffer.readUInt();
if (!this->parseBufferTag(buffer, tag, size)) {
return false;
}
}
if (!buffer.isValid()) {
return false;
}
SkDEBUGCODE(haveBuffer = true;)
} break;
}
return true; // success
}
bool SkPictureData::parseBufferTag(SkReadBuffer& buffer,
uint32_t tag, uint32_t size) {
switch (tag) {
case SK_PICT_BITMAP_BUFFER_TAG: {
const int count = SkToInt(size);
fBitmaps.reset(count);
for (int i = 0; i < count; ++i) {
SkBitmap* bm = &fBitmaps[i];
if (buffer.readBitmap(bm)) {
bm->setImmutable();
} else {
return false;
}
}
} break;
case SK_PICT_PAINT_BUFFER_TAG: {
const int count = SkToInt(size);
fPaints.reset(count);
for (int i = 0; i < count; ++i) {
buffer.readPaint(&fPaints[i]);
}
} break;
case SK_PICT_PATH_BUFFER_TAG:
if (size > 0) {
const int count = buffer.readInt();
fPaths.reset(count);
for (int i = 0; i < count; i++) {
buffer.readPath(&fPaths[i]);
}
} break;
case SK_PICT_TEXTBLOB_BUFFER_TAG: {
if (!buffer.validate((0 == fTextBlobCount) && (NULL == fTextBlobRefs))) {
return false;
}
fTextBlobCount = size;
fTextBlobRefs = SkNEW_ARRAY(const SkTextBlob*, fTextBlobCount);
bool success = true;
int i = 0;
for ( ; i < fTextBlobCount; i++) {
fTextBlobRefs[i] = SkTextBlob::CreateFromBuffer(buffer);
if (NULL == fTextBlobRefs[i]) {
success = false;
break;
}
}
if (!success) {
// Delete all of the blobs that were already created (up to but excluding i):
for (int j = 0; j < i; j++) {
fTextBlobRefs[j]->unref();
}
// Delete the array
SkDELETE_ARRAY(fTextBlobRefs);
fTextBlobRefs = NULL;
fTextBlobCount = 0;
return false;
}
} break;
case SK_PICT_READER_TAG: {
SkAutoDataUnref data(SkData::NewUninitialized(size));
if (!buffer.readByteArray(data->writable_data(), size) ||
!buffer.validate(NULL == fOpData)) {
return false;
}
SkASSERT(NULL == fOpData);
fOpData = data.detach();
} break;
case SK_PICT_PICTURE_TAG: {
if (!buffer.validate((0 == fPictureCount) && (NULL == fPictureRefs))) {
return false;
}
fPictureCount = size;
fPictureRefs = SkNEW_ARRAY(const SkPicture*, fPictureCount);
bool success = true;
int i = 0;
for ( ; i < fPictureCount; i++) {
fPictureRefs[i] = SkPicture::CreateFromBuffer(buffer);
if (NULL == fPictureRefs[i]) {
success = false;
break;
}
}
if (!success) {
// Delete all of the pictures that were already created (up to but excluding i):
for (int j = 0; j < i; j++) {
fPictureRefs[j]->unref();
}
// Delete the array
SkDELETE_ARRAY(fPictureRefs);
fPictureCount = 0;
return false;
}
} break;
default:
// The tag was invalid.
return false;
}
return true; // success
}
SkPictureData* SkPictureData::CreateFromStream(SkStream* stream,
const SkPictInfo& info,
SkPicture::InstallPixelRefProc proc) {
SkAutoTDelete<SkPictureData> data(SkNEW_ARGS(SkPictureData, (info)));
if (!data->parseStream(stream, proc)) {
return NULL;
}
return data.detach();
}
SkPictureData* SkPictureData::CreateFromBuffer(SkReadBuffer& buffer,
const SkPictInfo& info) {
SkAutoTDelete<SkPictureData> data(SkNEW_ARGS(SkPictureData, (info)));
buffer.setVersion(info.fVersion);
if (!data->parseBuffer(buffer)) {
return NULL;
}
return data.detach();
}
bool SkPictureData::parseStream(SkStream* stream,
SkPicture::InstallPixelRefProc proc) {
for (;;) {
uint32_t tag = stream->readU32();
if (SK_PICT_EOF_TAG == tag) {
break;
}
uint32_t size = stream->readU32();
if (!this->parseStreamTag(stream, tag, size, proc)) {
return false; // we're invalid
}
}
return true;
}
bool SkPictureData::parseBuffer(SkReadBuffer& buffer) {
for (;;) {
uint32_t tag = buffer.readUInt();
if (SK_PICT_EOF_TAG == tag) {
break;
}
uint32_t size = buffer.readUInt();
if (!this->parseBufferTag(buffer, tag, size)) {
return false; // we're invalid
}
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#if SK_SUPPORT_GPU
bool SkPictureData::suitableForGpuRasterization(GrContext* context, const char **reason,
int sampleCount) const {
return fContentInfo.suitableForGpuRasterization(context, reason, sampleCount);
}
bool SkPictureData::suitableForGpuRasterization(GrContext* context, const char **reason,
GrPixelConfig config, SkScalar dpi) const {
if (context != NULL) {
return this->suitableForGpuRasterization(context, reason,
context->getRecommendedSampleCount(config, dpi));
} else {
return this->suitableForGpuRasterization(NULL, reason);
}
}
bool SkPictureData::suitableForLayerOptimization() const {
return fContentInfo.numLayers() > 0;
}
#endif
///////////////////////////////////////////////////////////////////////////////
|