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
|
/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkCanvas.h"
#include "SkPatchUtils.h"
#include "SkPictureData.h"
#include "SkPicturePlayback.h"
#include "SkPictureRecord.h"
#include "SkReader32.h"
#include "SkTextBlob.h"
#include "SkTDArray.h"
#include "SkTypes.h"
/*
* Read the next op code and chunk size from 'reader'. The returned size
* is the entire size of the chunk (including the opcode). Thus, the
* offset just prior to calling ReadOpAndSize + 'size' is the offset
* to the next chunk's op code. This also means that the size of a chunk
* with no arguments (just an opcode) will be 4.
*/
DrawType SkPicturePlayback::ReadOpAndSize(SkReader32* reader, uint32_t* size) {
uint32_t temp = reader->readInt();
uint32_t op;
if (((uint8_t)temp) == temp) {
// old skp file - no size information
op = temp;
*size = 0;
} else {
UNPACK_8_24(temp, op, *size);
if (MASK_24 == *size) {
*size = reader->readInt();
}
}
return (DrawType)op;
}
static const SkRect* get_rect_ptr(SkReader32* reader) {
if (reader->readBool()) {
return &reader->skipT<SkRect>();
} else {
return NULL;
}
}
class TextContainer {
public:
size_t length() { return fByteLength; }
const void* text() { return (const void*)fText; }
size_t fByteLength;
const char* fText;
};
void get_text(SkReader32* reader, TextContainer* text) {
size_t length = text->fByteLength = reader->readInt();
text->fText = (const char*)reader->skip(length);
}
// FIXME: SkBitmaps are stateful, so we need to copy them to play back in multiple threads.
static SkBitmap shallow_copy(const SkBitmap& bitmap) {
return bitmap;
}
void SkPicturePlayback::draw(SkCanvas* canvas, SkPicture::AbortCallback* callback) {
AutoResetOpID aroi(this);
SkASSERT(0 == fCurOffset);
SkReader32 reader(fPictureData->opData()->bytes(), fPictureData->opData()->size());
// Record this, so we can concat w/ it if we encounter a setMatrix()
SkMatrix initialMatrix = canvas->getTotalMatrix();
SkAutoCanvasRestore acr(canvas, false);
while (!reader.eof()) {
if (callback && callback->abort()) {
return;
}
fCurOffset = reader.offset();
uint32_t size;
DrawType op = ReadOpAndSize(&reader, &size);
this->handleOp(&reader, op, size, canvas, initialMatrix);
}
}
void SkPicturePlayback::handleOp(SkReader32* reader,
DrawType op,
uint32_t size,
SkCanvas* canvas,
const SkMatrix& initialMatrix) {
switch (op) {
case NOOP: {
SkASSERT(size >= 4);
reader->skip(size - 4);
} break;
case CLIP_PATH: {
const SkPath& path = fPictureData->getPath(reader);
uint32_t packed = reader->readInt();
SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed);
bool doAA = ClipParams_unpackDoAA(packed);
size_t offsetToRestore = reader->readInt();
SkASSERT(!offsetToRestore || offsetToRestore >= reader->offset());
canvas->clipPath(path, regionOp, doAA);
if (canvas->isClipEmpty() && offsetToRestore) {
reader->setOffset(offsetToRestore);
}
} break;
case CLIP_REGION: {
SkRegion region;
reader->readRegion(®ion);
uint32_t packed = reader->readInt();
SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed);
size_t offsetToRestore = reader->readInt();
SkASSERT(!offsetToRestore || offsetToRestore >= reader->offset());
canvas->clipRegion(region, regionOp);
if (canvas->isClipEmpty() && offsetToRestore) {
reader->setOffset(offsetToRestore);
}
} break;
case CLIP_RECT: {
const SkRect& rect = reader->skipT<SkRect>();
uint32_t packed = reader->readInt();
SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed);
bool doAA = ClipParams_unpackDoAA(packed);
size_t offsetToRestore = reader->readInt();
SkASSERT(!offsetToRestore || offsetToRestore >= reader->offset());
canvas->clipRect(rect, regionOp, doAA);
if (canvas->isClipEmpty() && offsetToRestore) {
reader->setOffset(offsetToRestore);
}
} break;
case CLIP_RRECT: {
SkRRect rrect;
reader->readRRect(&rrect);
uint32_t packed = reader->readInt();
SkRegion::Op regionOp = ClipParams_unpackRegionOp(packed);
bool doAA = ClipParams_unpackDoAA(packed);
size_t offsetToRestore = reader->readInt();
SkASSERT(!offsetToRestore || offsetToRestore >= reader->offset());
canvas->clipRRect(rrect, regionOp, doAA);
if (canvas->isClipEmpty() && offsetToRestore) {
reader->setOffset(offsetToRestore);
}
} break;
case PUSH_CULL: break; // Deprecated, safe to ignore both push and pop.
case POP_CULL: break;
case CONCAT: {
SkMatrix matrix;
reader->readMatrix(&matrix);
canvas->concat(matrix);
break;
}
case DRAW_BITMAP: {
const SkPaint* paint = fPictureData->getPaint(reader);
const SkBitmap bitmap = shallow_copy(fPictureData->getBitmap(reader));
const SkPoint& loc = reader->skipT<SkPoint>();
canvas->drawBitmap(bitmap, loc.fX, loc.fY, paint);
} break;
case DRAW_BITMAP_RECT_TO_RECT: {
const SkPaint* paint = fPictureData->getPaint(reader);
const SkBitmap bitmap = shallow_copy(fPictureData->getBitmap(reader));
const SkRect* src = get_rect_ptr(reader); // may be null
const SkRect& dst = reader->skipT<SkRect>(); // required
SkCanvas::DrawBitmapRectFlags flags;
flags = (SkCanvas::DrawBitmapRectFlags) reader->readInt();
canvas->drawBitmapRectToRect(bitmap, src, dst, paint, flags);
} break;
case DRAW_BITMAP_MATRIX: {
const SkPaint* paint = fPictureData->getPaint(reader);
const SkBitmap bitmap = shallow_copy(fPictureData->getBitmap(reader));
SkMatrix matrix;
reader->readMatrix(&matrix);
SkAutoCanvasRestore acr(canvas, true);
canvas->concat(matrix);
canvas->drawBitmap(bitmap, 0, 0, paint);
} break;
case DRAW_BITMAP_NINE: {
const SkPaint* paint = fPictureData->getPaint(reader);
const SkBitmap bitmap = shallow_copy(fPictureData->getBitmap(reader));
const SkIRect& src = reader->skipT<SkIRect>();
const SkRect& dst = reader->skipT<SkRect>();
canvas->drawBitmapNine(bitmap, src, dst, paint);
} break;
case DRAW_CLEAR:
canvas->clear(reader->readInt());
break;
case DRAW_DATA: {
// This opcode is now dead, just need to skip it for backwards compatibility
size_t length = reader->readInt();
(void)reader->skip(length);
// skip handles padding the read out to a multiple of 4
} break;
case DRAW_DRRECT: {
const SkPaint& paint = *fPictureData->getPaint(reader);
SkRRect outer, inner;
reader->readRRect(&outer);
reader->readRRect(&inner);
canvas->drawDRRect(outer, inner, paint);
} break;
case BEGIN_COMMENT_GROUP: {
const char* desc = reader->readString();
canvas->beginCommentGroup(desc);
} break;
case COMMENT: {
const char* kywd = reader->readString();
const char* value = reader->readString();
canvas->addComment(kywd, value);
} break;
case END_COMMENT_GROUP: {
canvas->endCommentGroup();
} break;
case DRAW_OVAL: {
const SkPaint& paint = *fPictureData->getPaint(reader);
canvas->drawOval(reader->skipT<SkRect>(), paint);
} break;
case DRAW_PAINT:
canvas->drawPaint(*fPictureData->getPaint(reader));
break;
case DRAW_PATCH: {
const SkPaint& paint = *fPictureData->getPaint(reader);
const SkPoint* cubics = (const SkPoint*)reader->skip(SkPatchUtils::kNumCtrlPts *
sizeof(SkPoint));
uint32_t flag = reader->readInt();
const SkColor* colors = NULL;
if (flag & DRAW_VERTICES_HAS_COLORS) {
colors = (const SkColor*)reader->skip(SkPatchUtils::kNumCorners * sizeof(SkColor));
}
const SkPoint* texCoords = NULL;
if (flag & DRAW_VERTICES_HAS_TEXS) {
texCoords = (const SkPoint*)reader->skip(SkPatchUtils::kNumCorners *
sizeof(SkPoint));
}
SkAutoTUnref<SkXfermode> xfer;
if (flag & DRAW_VERTICES_HAS_XFER) {
int mode = reader->readInt();
if (mode < 0 || mode > SkXfermode::kLastMode) {
mode = SkXfermode::kModulate_Mode;
}
xfer.reset(SkXfermode::Create((SkXfermode::Mode)mode));
}
canvas->drawPatch(cubics, colors, texCoords, xfer, paint);
} break;
case DRAW_PATH: {
const SkPaint& paint = *fPictureData->getPaint(reader);
canvas->drawPath(fPictureData->getPath(reader), paint);
} break;
case DRAW_PICTURE:
canvas->drawPicture(fPictureData->getPicture(reader));
break;
case DRAW_PICTURE_MATRIX_PAINT: {
const SkPaint* paint = fPictureData->getPaint(reader);
SkMatrix matrix;
reader->readMatrix(&matrix);
const SkPicture* pic = fPictureData->getPicture(reader);
canvas->drawPicture(pic, &matrix, paint);
} break;
case DRAW_POINTS: {
const SkPaint& paint = *fPictureData->getPaint(reader);
SkCanvas::PointMode mode = (SkCanvas::PointMode)reader->readInt();
size_t count = reader->readInt();
const SkPoint* pts = (const SkPoint*)reader->skip(sizeof(SkPoint)* count);
canvas->drawPoints(mode, count, pts, paint);
} break;
case DRAW_POS_TEXT: {
const SkPaint& paint = *fPictureData->getPaint(reader);
TextContainer text;
get_text(reader, &text);
size_t points = reader->readInt();
const SkPoint* pos = (const SkPoint*)reader->skip(points * sizeof(SkPoint));
canvas->drawPosText(text.text(), text.length(), pos, paint);
} break;
case DRAW_POS_TEXT_TOP_BOTTOM: {
const SkPaint& paint = *fPictureData->getPaint(reader);
TextContainer text;
get_text(reader, &text);
size_t points = reader->readInt();
const SkPoint* pos = (const SkPoint*)reader->skip(points * sizeof(SkPoint));
const SkScalar top = reader->readScalar();
const SkScalar bottom = reader->readScalar();
if (!canvas->quickRejectY(top, bottom)) {
canvas->drawPosText(text.text(), text.length(), pos, paint);
}
} break;
case DRAW_POS_TEXT_H: {
const SkPaint& paint = *fPictureData->getPaint(reader);
TextContainer text;
get_text(reader, &text);
size_t xCount = reader->readInt();
const SkScalar constY = reader->readScalar();
const SkScalar* xpos = (const SkScalar*)reader->skip(xCount * sizeof(SkScalar));
canvas->drawPosTextH(text.text(), text.length(), xpos, constY, paint);
} break;
case DRAW_POS_TEXT_H_TOP_BOTTOM: {
const SkPaint& paint = *fPictureData->getPaint(reader);
TextContainer text;
get_text(reader, &text);
size_t xCount = reader->readInt();
const SkScalar* xpos = (const SkScalar*)reader->skip((3 + xCount) * sizeof(SkScalar));
const SkScalar top = *xpos++;
const SkScalar bottom = *xpos++;
const SkScalar constY = *xpos++;
if (!canvas->quickRejectY(top, bottom)) {
canvas->drawPosTextH(text.text(), text.length(), xpos, constY, paint);
}
} break;
case DRAW_RECT: {
const SkPaint& paint = *fPictureData->getPaint(reader);
canvas->drawRect(reader->skipT<SkRect>(), paint);
} break;
case DRAW_RRECT: {
const SkPaint& paint = *fPictureData->getPaint(reader);
SkRRect rrect;
reader->readRRect(&rrect);
canvas->drawRRect(rrect, paint);
} break;
case DRAW_SPRITE: {
const SkPaint* paint = fPictureData->getPaint(reader);
const SkBitmap bitmap = shallow_copy(fPictureData->getBitmap(reader));
int left = reader->readInt();
int top = reader->readInt();
canvas->drawSprite(bitmap, left, top, paint);
} break;
case DRAW_TEXT: {
const SkPaint& paint = *fPictureData->getPaint(reader);
TextContainer text;
get_text(reader, &text);
SkScalar x = reader->readScalar();
SkScalar y = reader->readScalar();
canvas->drawText(text.text(), text.length(), x, y, paint);
} break;
case DRAW_TEXT_BLOB: {
const SkPaint& paint = *fPictureData->getPaint(reader);
const SkTextBlob* blob = fPictureData->getTextBlob(reader);
SkScalar x = reader->readScalar();
SkScalar y = reader->readScalar();
canvas->drawTextBlob(blob, x, y, paint);
} break;
case DRAW_TEXT_TOP_BOTTOM: {
const SkPaint& paint = *fPictureData->getPaint(reader);
TextContainer text;
get_text(reader, &text);
const SkScalar* ptr = (const SkScalar*)reader->skip(4 * sizeof(SkScalar));
// ptr[0] == x
// ptr[1] == y
// ptr[2] == top
// ptr[3] == bottom
if (!canvas->quickRejectY(ptr[2], ptr[3])) {
canvas->drawText(text.text(), text.length(), ptr[0], ptr[1], paint);
}
} break;
case DRAW_TEXT_ON_PATH: {
const SkPaint& paint = *fPictureData->getPaint(reader);
TextContainer text;
get_text(reader, &text);
const SkPath& path = fPictureData->getPath(reader);
SkMatrix matrix;
reader->readMatrix(&matrix);
canvas->drawTextOnPath(text.text(), text.length(), path, &matrix, paint);
} break;
case DRAW_VERTICES: {
SkAutoTUnref<SkXfermode> xfer;
const SkPaint& paint = *fPictureData->getPaint(reader);
DrawVertexFlags flags = (DrawVertexFlags)reader->readInt();
SkCanvas::VertexMode vmode = (SkCanvas::VertexMode)reader->readInt();
int vCount = reader->readInt();
const SkPoint* verts = (const SkPoint*)reader->skip(vCount * sizeof(SkPoint));
const SkPoint* texs = NULL;
const SkColor* colors = NULL;
const uint16_t* indices = NULL;
int iCount = 0;
if (flags & DRAW_VERTICES_HAS_TEXS) {
texs = (const SkPoint*)reader->skip(vCount * sizeof(SkPoint));
}
if (flags & DRAW_VERTICES_HAS_COLORS) {
colors = (const SkColor*)reader->skip(vCount * sizeof(SkColor));
}
if (flags & DRAW_VERTICES_HAS_INDICES) {
iCount = reader->readInt();
indices = (const uint16_t*)reader->skip(iCount * sizeof(uint16_t));
}
if (flags & DRAW_VERTICES_HAS_XFER) {
int mode = reader->readInt();
if (mode < 0 || mode > SkXfermode::kLastMode) {
mode = SkXfermode::kModulate_Mode;
}
xfer.reset(SkXfermode::Create((SkXfermode::Mode)mode));
}
canvas->drawVertices(vmode, vCount, verts, texs, colors, xfer, indices, iCount, paint);
} break;
case RESTORE:
canvas->restore();
break;
case ROTATE:
canvas->rotate(reader->readScalar());
break;
case SAVE:
// SKPs with version < 29 also store a SaveFlags param.
if (size > 4) {
SkASSERT(8 == size);
reader->readInt();
}
canvas->save();
break;
case SAVE_LAYER: {
const SkRect* boundsPtr = get_rect_ptr(reader);
const SkPaint* paint = fPictureData->getPaint(reader);
canvas->saveLayer(boundsPtr, paint, (SkCanvas::SaveFlags) reader->readInt());
} break;
case SCALE: {
SkScalar sx = reader->readScalar();
SkScalar sy = reader->readScalar();
canvas->scale(sx, sy);
} break;
case SET_MATRIX: {
SkMatrix matrix;
reader->readMatrix(&matrix);
matrix.postConcat(initialMatrix);
canvas->setMatrix(matrix);
} break;
case SKEW: {
SkScalar sx = reader->readScalar();
SkScalar sy = reader->readScalar();
canvas->skew(sx, sy);
} break;
case TRANSLATE: {
SkScalar dx = reader->readScalar();
SkScalar dy = reader->readScalar();
canvas->translate(dx, dy);
} break;
default:
SkASSERTF(false, "Unknown draw type: %d", op);
}
}
|