blob: 78db5811b0c191a72454d7b93777309e850ef21b [file] [log] [blame]
reed54dc4872016-09-13 08:09:45 -07001/*
2 * Copyright 2016 Google Inc.
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 "SkCanvas.h"
Mike Reed7c9c9e42018-01-03 09:23:34 -05009#include "SkCanvasPriv.h"
reed54dc4872016-09-13 08:09:45 -070010#include "SkDeduper.h"
Mike Reed7c9c9e42018-01-03 09:23:34 -050011#include "SkDrawShadowInfo.h"
reed54dc4872016-09-13 08:09:45 -070012#include "SkPicture.h"
13#include "SkPictureRecorder.h"
14#include "SkPipe.h"
15#include "SkPipeFormat.h"
16#include "SkReadBuffer.h"
17#include "SkRefSet.h"
18#include "SkRSXform.h"
Cary Clark53c87692018-07-17 08:59:34 -040019#include "SkTextBlobPriv.h"
reed54dc4872016-09-13 08:09:45 -070020#include "SkTypeface.h"
Mike Reed887cdf12017-04-03 11:11:09 -040021#include "SkVertices.h"
reed54dc4872016-09-13 08:09:45 -070022
23class SkPipeReader;
24
25static bool do_playback(SkPipeReader& reader, SkCanvas* canvas, int* endPictureIndex = nullptr);
26
27///////////////////////////////////////////////////////////////////////////////////////////////////
28
29class SkPipeInflator : public SkInflator {
30public:
31 SkPipeInflator(SkRefSet<SkImage>* images, SkRefSet<SkPicture>* pictures,
32 SkRefSet<SkTypeface>* typefaces, SkTDArray<SkFlattenable::Factory>* factories,
Mike Reedc0cec872017-12-14 10:45:27 -050033 const SkDeserialProcs& procs)
reed54dc4872016-09-13 08:09:45 -070034 : fImages(images)
35 , fPictures(pictures)
36 , fTypefaces(typefaces)
37 , fFactories(factories)
Mike Reedc0cec872017-12-14 10:45:27 -050038 , fProcs(procs)
reed54dc4872016-09-13 08:09:45 -070039 {}
Ben Wagner63fd7602017-10-09 15:45:33 -040040
reed54dc4872016-09-13 08:09:45 -070041 SkImage* getImage(int index) override {
42 return index ? fImages->get(index - 1) : nullptr;
43 }
44 SkPicture* getPicture(int index) override {
45 return index ? fPictures->get(index - 1) : nullptr;
46 }
47 SkTypeface* getTypeface(int index) override {
48 return fTypefaces->get(index - 1);
49 }
50 SkFlattenable::Factory getFactory(int index) override {
51 return index ? fFactories->getAt(index - 1) : nullptr;
52 }
53
Ben Wagner36fe60d2018-07-10 17:38:12 -040054 bool setImage(int index, sk_sp<SkImage> img) {
55 return fImages->set(index - 1, std::move(img));
reed54dc4872016-09-13 08:09:45 -070056 }
Ben Wagner36fe60d2018-07-10 17:38:12 -040057 bool setPicture(int index, sk_sp<SkPicture> pic) {
58 return fPictures->set(index - 1, std::move(pic));
reed54dc4872016-09-13 08:09:45 -070059 }
Ben Wagner36fe60d2018-07-10 17:38:12 -040060 bool setTypeface(int index, sk_sp<SkTypeface> face) {
61 return fTypefaces->set(index - 1, std::move(face));
reed54dc4872016-09-13 08:09:45 -070062 }
63 bool setFactory(int index, SkFlattenable::Factory factory) {
64 SkASSERT(index > 0);
65 SkASSERT(factory);
66 index -= 1;
67 if ((unsigned)index < (unsigned)fFactories->count()) {
68 (*fFactories)[index] = factory;
69 return true;
70 }
71 if (fFactories->count() == index) {
72 *fFactories->append() = factory;
73 return true;
74 }
75 SkDebugf("setFactory: index [%d] out of range %d\n", index, fFactories->count());
76 return false;
77 }
78
Mike Reedc0cec872017-12-14 10:45:27 -050079 void setDeserialProcs(const SkDeserialProcs& procs) {
80 fProcs = procs;
Mike Reed3ac64b42016-10-18 19:34:08 -040081 }
Ben Wagner63fd7602017-10-09 15:45:33 -040082
reed54dc4872016-09-13 08:09:45 -070083 sk_sp<SkTypeface> makeTypeface(const void* data, size_t size);
Mike Reed3ac64b42016-10-18 19:34:08 -040084 sk_sp<SkImage> makeImage(const sk_sp<SkData>&);
reed54dc4872016-09-13 08:09:45 -070085
86private:
87 SkRefSet<SkImage>* fImages;
88 SkRefSet<SkPicture>* fPictures;
89 SkRefSet<SkTypeface>* fTypefaces;
90 SkTDArray<SkFlattenable::Factory>* fFactories;
Mike Reedc0cec872017-12-14 10:45:27 -050091 SkDeserialProcs fProcs;
reed54dc4872016-09-13 08:09:45 -070092};
93
94///////////////////////////////////////////////////////////////////////////////////////////////////
95
reed54dc4872016-09-13 08:09:45 -070096static SkRRect read_rrect(SkReadBuffer& reader) {
97 SkRRect rrect;
98 rrect.readFromMemory(reader.skip(SkRRect::kSizeInMemory), SkRRect::kSizeInMemory);
99 return rrect;
100}
101
102static SkMatrix read_sparse_matrix(SkReadBuffer& reader, SkMatrix::TypeMask tm) {
103 SkMatrix matrix;
104 matrix.reset();
105
106 if (tm & SkMatrix::kPerspective_Mask) {
Mike Reed22d77cb2018-01-03 13:22:51 -0500107 matrix.set9(reader.skipT<SkScalar>(9));
reed54dc4872016-09-13 08:09:45 -0700108 } else if (tm & SkMatrix::kAffine_Mask) {
Mike Reed22d77cb2018-01-03 13:22:51 -0500109 const SkScalar* tmp = reader.skipT<SkScalar>(6);
reed54dc4872016-09-13 08:09:45 -0700110 matrix[SkMatrix::kMScaleX] = tmp[0];
111 matrix[SkMatrix::kMSkewX] = tmp[1];
112 matrix[SkMatrix::kMTransX] = tmp[2];
113 matrix[SkMatrix::kMScaleY] = tmp[3];
114 matrix[SkMatrix::kMSkewY] = tmp[4];
115 matrix[SkMatrix::kMTransY] = tmp[5];
116 } else if (tm & SkMatrix::kScale_Mask) {
Mike Reed22d77cb2018-01-03 13:22:51 -0500117 const SkScalar* tmp = reader.skipT<SkScalar>(4);
reed54dc4872016-09-13 08:09:45 -0700118 matrix[SkMatrix::kMScaleX] = tmp[0];
119 matrix[SkMatrix::kMTransX] = tmp[1];
120 matrix[SkMatrix::kMScaleY] = tmp[2];
121 matrix[SkMatrix::kMTransY] = tmp[3];
122 } else if (tm & SkMatrix::kTranslate_Mask) {
Mike Reed22d77cb2018-01-03 13:22:51 -0500123 const SkScalar* tmp = reader.skipT<SkScalar>(2);
reed54dc4872016-09-13 08:09:45 -0700124 matrix[SkMatrix::kMTransX] = tmp[0];
125 matrix[SkMatrix::kMTransY] = tmp[1];
126 }
127 // else read nothing for Identity
128 return matrix;
129}
130
131///////////////////////////////////////////////////////////////////////////////////////////////////
132
133#define CHECK_SET_SCALAR(Field) \
134 do { if (nondef & k##Field##_NonDef) { \
135 paint.set##Field(reader.readScalar()); \
136 }} while (0)
137
138#define CHECK_SET_FLATTENABLE(Field) \
139 do { if (nondef & k##Field##_NonDef) { \
140 paint.set##Field(reader.read##Field()); \
141 }} while (0)
142
143/*
144 * Header:
145 * paint flags : 32
146 * non_def bits : 16
147 * xfermode enum : 8
148 * pad zeros : 8
149 */
150static SkPaint read_paint(SkReadBuffer& reader) {
reed374772b2016-10-05 17:33:02 -0700151 SkPaint paint;
152
reed54dc4872016-09-13 08:09:45 -0700153 uint32_t packedFlags = reader.read32();
154 uint32_t extra = reader.read32();
155 unsigned nondef = extra >> 16;
reed374772b2016-10-05 17:33:02 -0700156 paint.setBlendMode(SkBlendMode((extra >> 8) & 0xFF));
157 SkASSERT((extra & 0xFF) == 0); // zero pad byte
reed54dc4872016-09-13 08:09:45 -0700158
159 packedFlags >>= 2; // currently unused
160 paint.setTextEncoding((SkPaint::TextEncoding)(packedFlags & 3)); packedFlags >>= 2;
161 paint.setTextAlign((SkPaint::Align)(packedFlags & 3)); packedFlags >>= 2;
162 paint.setHinting((SkPaint::Hinting)(packedFlags & 3)); packedFlags >>= 2;
163 paint.setStrokeJoin((SkPaint::Join)(packedFlags & 3)); packedFlags >>= 2;
164 paint.setStrokeCap((SkPaint::Cap)(packedFlags & 3)); packedFlags >>= 2;
165 paint.setStyle((SkPaint::Style)(packedFlags & 3)); packedFlags >>= 2;
166 paint.setFilterQuality((SkFilterQuality)(packedFlags & 3)); packedFlags >>= 2;
167 paint.setFlags(packedFlags);
168
169 CHECK_SET_SCALAR(TextSize);
170 CHECK_SET_SCALAR(TextScaleX);
171 CHECK_SET_SCALAR(TextSkewX);
172 CHECK_SET_SCALAR(StrokeWidth);
173 CHECK_SET_SCALAR(StrokeMiter);
174
175 if (nondef & kColor_NonDef) {
176 paint.setColor(reader.read32());
177 }
178
179 CHECK_SET_FLATTENABLE(Typeface);
180 CHECK_SET_FLATTENABLE(PathEffect);
181 CHECK_SET_FLATTENABLE(Shader);
reed54dc4872016-09-13 08:09:45 -0700182 CHECK_SET_FLATTENABLE(MaskFilter);
183 CHECK_SET_FLATTENABLE(ColorFilter);
reed54dc4872016-09-13 08:09:45 -0700184 CHECK_SET_FLATTENABLE(ImageFilter);
185 CHECK_SET_FLATTENABLE(DrawLooper);
186
reed54dc4872016-09-13 08:09:45 -0700187 return paint;
188}
189
190class SkPipeReader : public SkReadBuffer {
191public:
192 SkPipeReader(SkPipeDeserializer* sink, const void* data, size_t size)
193 : SkReadBuffer(data, size)
194 , fSink(sink)
195 {}
196
197 SkPipeDeserializer* fSink;
198
199 SkFlattenable::Factory findFactory(const char name[]) {
200 SkFlattenable::Factory factory;
201 // Check if a custom Factory has been specified for this flattenable.
202 if (!(factory = this->getCustomFactory(SkString(name)))) {
203 // If there is no custom Factory, check for a default.
204 factory = SkFlattenable::NameToFactory(name);
205 }
206 return factory;
207 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400208
Mike Reede97e7922018-01-18 15:57:38 -0500209 bool readPaint(SkPaint* paint) override {
reed54dc4872016-09-13 08:09:45 -0700210 *paint = read_paint(*this);
Mike Reede97e7922018-01-18 15:57:38 -0500211 return this->isValid();
reed54dc4872016-09-13 08:09:45 -0700212 }
213};
214
215///////////////////////////////////////////////////////////////////////////////////////////////////
216
217typedef void (*SkPipeHandler)(SkPipeReader&, uint32_t packedVerb, SkCanvas*);
218
219static void save_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
220 SkASSERT(SkPipeVerb::kSave == unpack_verb(packedVerb));
221 canvas->save();
222}
223
224static void saveLayer_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
225 SkASSERT(SkPipeVerb::kSaveLayer == unpack_verb(packedVerb));
226 unsigned extra = unpack_verb_extra(packedVerb);
Mike Reed22d77cb2018-01-03 13:22:51 -0500227 const SkRect* bounds = (extra & kHasBounds_SaveLayerMask) ? reader.skipT<SkRect>() : nullptr;
reed54dc4872016-09-13 08:09:45 -0700228 SkPaint paintStorage, *paint = nullptr;
229 if (extra & kHasPaint_SaveLayerMask) {
230 paintStorage = read_paint(reader);
231 paint = &paintStorage;
232 }
233 sk_sp<SkImageFilter> backdrop;
234 if (extra & kHasBackdrop_SaveLayerMask) {
235 backdrop = reader.readImageFilter();
236 }
Florin Malita53f77bd2017-04-28 13:48:37 -0400237 sk_sp<SkImage> clipMask;
238 if (extra & kHasClipMask_SaveLayerMask) {
239 clipMask = reader.readImage();
240 }
241 SkMatrix clipMatrix;
242 if (extra & kHasClipMatrix_SaveLayerMask) {
243 reader.readMatrix(&clipMatrix);
244 }
reed54dc4872016-09-13 08:09:45 -0700245 SkCanvas::SaveLayerFlags flags = (SkCanvas::SaveLayerFlags)(extra & kFlags_SaveLayerMask);
246
247 // unremap this wacky flag
248 if (extra & kDontClipToLayer_SaveLayerMask) {
Cary Clark7eddfb82018-03-13 14:41:10 -0400249 flags |= SkCanvasPriv::kDontClipToLayer_SaveLayerFlag;
reed54dc4872016-09-13 08:09:45 -0700250 }
251
Mike Kleinb34ab042017-05-01 21:34:14 +0000252 canvas->saveLayer(SkCanvas::SaveLayerRec(bounds, paint, backdrop.get(), clipMask.get(),
Florin Malita53f77bd2017-04-28 13:48:37 -0400253 (extra & kHasClipMatrix_SaveLayerMask) ? &clipMatrix : nullptr, flags));
reed54dc4872016-09-13 08:09:45 -0700254}
255
256static void restore_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
257 SkASSERT(SkPipeVerb::kRestore == unpack_verb(packedVerb));
258 canvas->restore();
259}
260
261static void concat_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
262 SkASSERT(SkPipeVerb::kConcat == unpack_verb(packedVerb));
263 SkMatrix::TypeMask tm = (SkMatrix::TypeMask)(packedVerb & kTypeMask_ConcatMask);
264 const SkMatrix matrix = read_sparse_matrix(reader, tm);
265 if (packedVerb & kSetMatrix_ConcatMask) {
266 canvas->setMatrix(matrix);
267 } else {
268 canvas->concat(matrix);
269 }
270}
271
272static void clipRect_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
273 SkASSERT(SkPipeVerb::kClipRect == unpack_verb(packedVerb));
Mike Reedc1f77742016-12-09 09:00:50 -0500274 SkClipOp op = (SkClipOp)(unpack_verb_extra(packedVerb) >> 1);
reed54dc4872016-09-13 08:09:45 -0700275 bool isAA = unpack_verb_extra(packedVerb) & 1;
Mike Reed22d77cb2018-01-03 13:22:51 -0500276 canvas->clipRect(*reader.skipT<SkRect>(), op, isAA);
reed54dc4872016-09-13 08:09:45 -0700277}
278
279static void clipRRect_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
280 SkASSERT(SkPipeVerb::kClipRRect == unpack_verb(packedVerb));
Mike Reedc1f77742016-12-09 09:00:50 -0500281 SkClipOp op = (SkClipOp)(unpack_verb_extra(packedVerb) >> 1);
reed54dc4872016-09-13 08:09:45 -0700282 bool isAA = unpack_verb_extra(packedVerb) & 1;
283 canvas->clipRRect(read_rrect(reader), op, isAA);
284}
285
286static void clipPath_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
287 SkASSERT(SkPipeVerb::kClipPath == unpack_verb(packedVerb));
Mike Reedc1f77742016-12-09 09:00:50 -0500288 SkClipOp op = (SkClipOp)(unpack_verb_extra(packedVerb) >> 1);
reed54dc4872016-09-13 08:09:45 -0700289 bool isAA = unpack_verb_extra(packedVerb) & 1;
290 SkPath path;
291 reader.readPath(&path);
292 canvas->clipPath(path, op, isAA);
293}
294
295static void clipRegion_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
296 SkASSERT(SkPipeVerb::kClipRegion == unpack_verb(packedVerb));
Mike Reedc1f77742016-12-09 09:00:50 -0500297 SkClipOp op = (SkClipOp)(unpack_verb_extra(packedVerb) >> 1);
reed54dc4872016-09-13 08:09:45 -0700298 SkRegion region;
299 reader.readRegion(&region);
300 canvas->clipRegion(region, op);
301}
302
303static void drawArc_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
304 SkASSERT(SkPipeVerb::kDrawArc == unpack_verb(packedVerb));
305 const bool useCenter = (bool)(unpack_verb_extra(packedVerb) & 1);
Mike Reed22d77cb2018-01-03 13:22:51 -0500306 const SkScalar* scalars = reader.skipT<SkScalar>(6); // bounds[0..3], start[4], sweep[5]
reed54dc4872016-09-13 08:09:45 -0700307 const SkRect* bounds = (const SkRect*)scalars;
308 canvas->drawArc(*bounds, scalars[4], scalars[5], useCenter, read_paint(reader));
309}
310
311static void drawAtlas_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
312 SkASSERT(SkPipeVerb::kDrawAtlas == unpack_verb(packedVerb));
Mike Reed7d954ad2016-10-28 15:42:34 -0400313 SkBlendMode mode = (SkBlendMode)(packedVerb & kMode_DrawAtlasMask);
reed54dc4872016-09-13 08:09:45 -0700314 sk_sp<SkImage> image(reader.readImage());
315 int count = reader.read32();
Mike Reed22d77cb2018-01-03 13:22:51 -0500316 const SkRSXform* xform = reader.skipT<SkRSXform>(count);
317 const SkRect* rect = reader.skipT<SkRect>(count);
reed54dc4872016-09-13 08:09:45 -0700318 const SkColor* color = nullptr;
319 if (packedVerb & kHasColors_DrawAtlasMask) {
Mike Reed22d77cb2018-01-03 13:22:51 -0500320 color = reader.skipT<SkColor>(count);
reed54dc4872016-09-13 08:09:45 -0700321 }
322 const SkRect* cull = nullptr;
323 if (packedVerb & kHasCull_DrawAtlasMask) {
Mike Reed22d77cb2018-01-03 13:22:51 -0500324 cull = reader.skipT<SkRect>();
reed54dc4872016-09-13 08:09:45 -0700325 }
326 SkPaint paintStorage, *paint = nullptr;
327 if (packedVerb & kHasPaint_DrawAtlasMask) {
328 paintStorage = read_paint(reader);
329 paint = &paintStorage;
330 }
331 canvas->drawAtlas(image, xform, rect, color, count, mode, cull, paint);
332}
333
334static void drawDRRect_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
335 SkASSERT(SkPipeVerb::kDrawDRRect == unpack_verb(packedVerb));
336 const SkRRect outer = read_rrect(reader);
337 const SkRRect inner = read_rrect(reader);
338 canvas->drawDRRect(outer, inner, read_paint(reader));
339}
340
341static void drawText_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
342 SkASSERT(SkPipeVerb::kDrawText == unpack_verb(packedVerb));
343 uint32_t len = unpack_verb_extra(packedVerb);
344 if (0 == len) {
345 len = reader.read32();
346 }
347 const void* text = reader.skip(SkAlign4(len));
348 SkScalar x = reader.readScalar();
349 SkScalar y = reader.readScalar();
350 canvas->drawText(text, len, x, y, read_paint(reader));
351}
352
353static void drawPosText_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
354 SkASSERT(SkPipeVerb::kDrawPosText == unpack_verb(packedVerb));
355 uint32_t len = unpack_verb_extra(packedVerb);
356 if (0 == len) {
357 len = reader.read32();
358 }
359 const void* text = reader.skip(SkAlign4(len));
360 int count = reader.read32();
Mike Reed22d77cb2018-01-03 13:22:51 -0500361 const SkPoint* pos = reader.skipT<SkPoint>(count);
reed54dc4872016-09-13 08:09:45 -0700362 SkPaint paint = read_paint(reader);
363 SkASSERT(paint.countText(text, len) == count);
364 canvas->drawPosText(text, len, pos, paint);
365}
366
367static void drawPosTextH_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
368 SkASSERT(SkPipeVerb::kDrawPosTextH == unpack_verb(packedVerb));
369 uint32_t len = unpack_verb_extra(packedVerb);
370 if (0 == len) {
371 len = reader.read32();
372 }
373 const void* text = reader.skip(SkAlign4(len));
374 int count = reader.read32();
Mike Reed22d77cb2018-01-03 13:22:51 -0500375 const SkScalar* xpos = reader.skipT<SkScalar>(count);
reed54dc4872016-09-13 08:09:45 -0700376 SkScalar constY = reader.readScalar();
377 SkPaint paint = read_paint(reader);
378 SkASSERT(paint.countText(text, len) == count);
379 canvas->drawPosTextH(text, len, xpos, constY, paint);
380}
381
reed54dc4872016-09-13 08:09:45 -0700382static void drawTextBlob_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
Cary Clark53c87692018-07-17 08:59:34 -0400383 sk_sp<SkTextBlob> tb = SkTextBlobPriv::MakeFromBuffer(reader);
reed54dc4872016-09-13 08:09:45 -0700384 SkScalar x = reader.readScalar();
385 SkScalar y = reader.readScalar();
386 canvas->drawTextBlob(tb, x, y, read_paint(reader));
387}
388
389static void drawTextRSXform_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
390 SkASSERT(SkPipeVerb::kDrawTextRSXform == unpack_verb(packedVerb));
391 uint32_t len = unpack_verb_extra(packedVerb) >> 1;
392 if (0 == len) {
393 len = reader.read32();
394 }
395 const void* text = reader.skip(SkAlign4(len));
396 int count = reader.read32();
Mike Reed22d77cb2018-01-03 13:22:51 -0500397 const SkRSXform* xform = reader.skipT<SkRSXform>(count);
398 const SkRect* cull = (packedVerb & 1) ? reader.skipT<SkRect>() : nullptr;
reed54dc4872016-09-13 08:09:45 -0700399 SkPaint paint = read_paint(reader);
400 SkASSERT(paint.countText(text, len) == count);
401 canvas->drawTextRSXform(text, len, xform, cull, paint);
402}
403
404static void drawPatch_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
405 SkASSERT(SkPipeVerb::kDrawPatch == unpack_verb(packedVerb));
406 const SkColor* colors = nullptr;
407 const SkPoint* tex = nullptr;
Mike Reed22d77cb2018-01-03 13:22:51 -0500408 const SkPoint* cubics = reader.skipT<SkPoint>(12);
reed54dc4872016-09-13 08:09:45 -0700409 if (packedVerb & kHasColors_DrawPatchExtraMask) {
Mike Reed22d77cb2018-01-03 13:22:51 -0500410 colors = reader.skipT<SkColor>(4);
reed54dc4872016-09-13 08:09:45 -0700411 }
412 if (packedVerb & kHasTexture_DrawPatchExtraMask) {
Mike Reed22d77cb2018-01-03 13:22:51 -0500413 tex = reader.skipT<SkPoint>(4);
reed54dc4872016-09-13 08:09:45 -0700414 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400415 SkBlendMode mode = (SkBlendMode)(packedVerb & kModeEnum_DrawPatchExtraMask);
416 canvas->drawPatch(cubics, colors, tex, mode, read_paint(reader));
reed54dc4872016-09-13 08:09:45 -0700417}
418
419static void drawPaint_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
420 SkASSERT(SkPipeVerb::kDrawPaint == unpack_verb(packedVerb));
421 canvas->drawPaint(read_paint(reader));
422}
423
424static void drawRect_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
425 SkASSERT(SkPipeVerb::kDrawRect == unpack_verb(packedVerb));
Mike Reed22d77cb2018-01-03 13:22:51 -0500426 const SkRect* rect = reader.skipT<SkRect>();
reed54dc4872016-09-13 08:09:45 -0700427 canvas->drawRect(*rect, read_paint(reader));
428}
429
430static void drawRegion_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
431 SkASSERT(SkPipeVerb::kDrawRegion == unpack_verb(packedVerb));
432 size_t size = unpack_verb_extra(packedVerb);
433 if (0 == size) {
434 size = reader.read32();
435 }
436 SkRegion region;
Mike Reed22d77cb2018-01-03 13:22:51 -0500437 region.readFromMemory(reader.skipT<char>(size), size);
reed54dc4872016-09-13 08:09:45 -0700438 canvas->drawRegion(region, read_paint(reader));
439}
440
441static void drawOval_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
442 SkASSERT(SkPipeVerb::kDrawOval == unpack_verb(packedVerb));
Mike Reed22d77cb2018-01-03 13:22:51 -0500443 const SkRect* rect = reader.skipT<SkRect>();
reed54dc4872016-09-13 08:09:45 -0700444 canvas->drawOval(*rect, read_paint(reader));
445}
446
447static void drawRRect_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
448 SkASSERT(SkPipeVerb::kDrawRRect == unpack_verb(packedVerb));
449 SkRRect rrect = read_rrect(reader);
450 canvas->drawRRect(rrect, read_paint(reader));
451}
452
453static void drawPath_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
454 SkASSERT(SkPipeVerb::kDrawPath == unpack_verb(packedVerb));
455 SkPath path;
456 reader.readPath(&path);
457 canvas->drawPath(path, read_paint(reader));
458}
459
Mike Reed7c9c9e42018-01-03 09:23:34 -0500460static void drawShadowRec_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
461 SkASSERT(SkPipeVerb::kDrawShadowRec == unpack_verb(packedVerb));
462 SkPath path;
463 reader.readPath(&path);
464 SkDrawShadowRec rec;
465 reader.readPad32(&rec, sizeof(rec));
466 canvas->private_draw_shadow_rec(path, rec);
467}
468
reed54dc4872016-09-13 08:09:45 -0700469static void drawPoints_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
470 SkASSERT(SkPipeVerb::kDrawPoints == unpack_verb(packedVerb));
471 SkCanvas::PointMode mode = (SkCanvas::PointMode)unpack_verb_extra(packedVerb);
472 int count = reader.read32();
Mike Reed22d77cb2018-01-03 13:22:51 -0500473 const SkPoint* points = reader.skipT<SkPoint>(count);
reed54dc4872016-09-13 08:09:45 -0700474 canvas->drawPoints(mode, count, points, read_paint(reader));
475}
476
477static void drawImage_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
478 SkASSERT(SkPipeVerb::kDrawImage == unpack_verb(packedVerb));
479 sk_sp<SkImage> image(reader.readImage());
480 SkScalar x = reader.readScalar();
481 SkScalar y = reader.readScalar();
482 SkPaint paintStorage, *paint = nullptr;
483 if (packedVerb & kHasPaint_DrawImageMask) {
484 paintStorage = read_paint(reader);
485 paint = &paintStorage;
486 }
487 canvas->drawImage(image, x, y, paint);
488}
489
490static void drawImageRect_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
491 SkASSERT(SkPipeVerb::kDrawImageRect == unpack_verb(packedVerb));
492 sk_sp<SkImage> image(reader.readImage());
493 SkCanvas::SrcRectConstraint constraint =
494 (SkCanvas::SrcRectConstraint)(packedVerb & kConstraint_DrawImageRectMask);
495 const SkRect* src = (packedVerb & kHasSrcRect_DrawImageRectMask) ?
Mike Reed22d77cb2018-01-03 13:22:51 -0500496 reader.skipT<SkRect>() : nullptr;
497 const SkRect* dst = reader.skipT<SkRect>();
reed54dc4872016-09-13 08:09:45 -0700498 SkPaint paintStorage, *paint = nullptr;
499 if (packedVerb & kHasPaint_DrawImageRectMask) {
500 paintStorage = read_paint(reader);
501 paint = &paintStorage;
502 }
503 if (src) {
504 canvas->drawImageRect(image, *src, *dst, paint, constraint);
505 } else {
506 canvas->drawImageRect(image, *dst, paint);
507 }
508}
509
510static void drawImageNine_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
511 SkASSERT(SkPipeVerb::kDrawImageNine == unpack_verb(packedVerb));
512 sk_sp<SkImage> image(reader.readImage());
Mike Reed22d77cb2018-01-03 13:22:51 -0500513 const SkIRect* center = reader.skipT<SkIRect>();
514 const SkRect* dst = reader.skipT<SkRect>();
reed54dc4872016-09-13 08:09:45 -0700515 SkPaint paintStorage, *paint = nullptr;
516 if (packedVerb & kHasPaint_DrawImageNineMask) {
517 paintStorage = read_paint(reader);
518 paint = &paintStorage;
519 }
520 canvas->drawImageNine(image, *center, *dst, paint);
521}
522
523static void drawImageLattice_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
524 SkASSERT(SkPipeVerb::kDrawImageLattice == unpack_verb(packedVerb));
525 sk_sp<SkImage> image(reader.readImage());
526
527 SkCanvas::Lattice lattice;
Mike Reed7c9c9e42018-01-03 09:23:34 -0500528 if (!SkCanvasPriv::ReadLattice(reader, &lattice)) {
529 return;
reed54dc4872016-09-13 08:09:45 -0700530 }
Mike Reed22d77cb2018-01-03 13:22:51 -0500531 const SkRect* dst = reader.skipT<SkRect>();
reed54dc4872016-09-13 08:09:45 -0700532
533 SkPaint paintStorage, *paint = nullptr;
534 if (packedVerb & kHasPaint_DrawImageLatticeMask) {
535 paintStorage = read_paint(reader);
536 paint = &paintStorage;
537 }
538 canvas->drawImageLattice(image.get(), lattice, *dst, paint);
539}
540
541static void drawVertices_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
542 SkASSERT(SkPipeVerb::kDrawVertices == unpack_verb(packedVerb));
Mike Reed887cdf12017-04-03 11:11:09 -0400543 SkBlendMode bmode = (SkBlendMode)unpack_verb_extra(packedVerb);
Ruiqi Maof5101492018-06-29 14:32:21 -0400544 sk_sp<SkVertices> vertices = nullptr;
Florin Malitac2ea3272018-05-10 09:41:38 -0400545 if (sk_sp<SkData> data = reader.readByteArrayAsData()) {
Ruiqi Maof5101492018-06-29 14:32:21 -0400546 vertices = SkVertices::Decode(data->data(), data->size());
547 }
548 int boneCount = reader.read32();
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400549 const SkVertices::Bone* bones = boneCount ? reader.skipT<SkVertices::Bone>(boneCount) : nullptr;
Ruiqi Maof5101492018-06-29 14:32:21 -0400550 if (vertices) {
551 canvas->drawVertices(vertices, bones, boneCount, bmode, read_paint(reader));
Florin Malitac2ea3272018-05-10 09:41:38 -0400552 }
reed54dc4872016-09-13 08:09:45 -0700553}
554
555static void drawPicture_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
556 SkASSERT(SkPipeVerb::kDrawPicture == unpack_verb(packedVerb));
557 unsigned extra = unpack_verb_extra(packedVerb);
558 int index = extra & kIndex_ObjectDefinitionMask;
559 SkPicture* pic = reader.getInflator()->getPicture(index);
560 SkMatrix matrixStorage, *matrix = nullptr;
561 SkPaint paintStorage, *paint = nullptr;
562 if (extra & kHasMatrix_DrawPictureExtra) {
563 reader.readMatrix(&matrixStorage);
564 matrix = &matrixStorage;
565 }
566 if (extra & kHasPaint_DrawPictureExtra) {
567 paintStorage = read_paint(reader);
568 paint = &paintStorage;
569 }
570 canvas->drawPicture(pic, matrix, paint);
571}
572
573static void drawAnnotation_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
574 SkASSERT(SkPipeVerb::kDrawAnnotation == unpack_verb(packedVerb));
Mike Reed22d77cb2018-01-03 13:22:51 -0500575 const SkRect* rect = reader.skipT<SkRect>();
reed54dc4872016-09-13 08:09:45 -0700576
577 // len includes the key's trailing 0
578 uint32_t len = unpack_verb_extra(packedVerb) >> 1;
579 if (0 == len) {
580 len = reader.read32();
581 }
Mike Reed22d77cb2018-01-03 13:22:51 -0500582 const char* key = reader.skipT<char>(len);
reed54dc4872016-09-13 08:09:45 -0700583 sk_sp<SkData> data;
584 if (packedVerb & 1) {
585 uint32_t size = reader.read32();
586 data = SkData::MakeWithCopy(reader.skip(SkAlign4(size)), size);
587 }
588 canvas->drawAnnotation(*rect, key, data);
589}
590
591#if 0
592 stream.write("skiacodc", 8);
593 stream.write32(pmap.width());
594 stream.write32(pmap.height());
595 stream.write16(pmap.colorType());
596 stream.write16(pmap.alphaType());
597 stream.write32(0); // no colorspace for now
598 for (int y = 0; y < pmap.height(); ++y) {
599 stream.write(pmap.addr8(0, y), pmap.width());
600 }
601#endif
602
Mike Reed3ac64b42016-10-18 19:34:08 -0400603sk_sp<SkImage> SkPipeInflator::makeImage(const sk_sp<SkData>& data) {
Mike Reedc0cec872017-12-14 10:45:27 -0500604 if (fProcs.fImageProc) {
605 return fProcs.fImageProc(data->data(), data->size(), fProcs.fImageCtx);
Mike Reed3ac64b42016-10-18 19:34:08 -0400606 }
Mike Reedc0cec872017-12-14 10:45:27 -0500607 return SkImage::MakeFromEncoded(data);
reed54dc4872016-09-13 08:09:45 -0700608}
609
Mike Reed3ac64b42016-10-18 19:34:08 -0400610
reed7e3ba9f2016-09-13 17:25:19 -0700611static void defineImage_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas*) {
reed54dc4872016-09-13 08:09:45 -0700612 SkASSERT(SkPipeVerb::kDefineImage == unpack_verb(packedVerb));
613 SkPipeInflator* inflator = (SkPipeInflator*)reader.getInflator();
614 uint32_t extra = unpack_verb_extra(packedVerb);
615 int index = extra & kIndex_ObjectDefinitionMask;
616
617 if (extra & kUndef_ObjectDefinitionMask) {
618 // zero-index means we are "forgetting" that cache entry
619 inflator->setImage(index, nullptr);
620 } else {
621 // we are defining a new image
622 sk_sp<SkData> data = reader.readByteArrayAsData();
Florin Malitac2ea3272018-05-10 09:41:38 -0400623 sk_sp<SkImage> image = data ? inflator->makeImage(data) : nullptr;
reed54dc4872016-09-13 08:09:45 -0700624 if (!image) {
625 SkDebugf("-- failed to decode\n");
626 }
Ben Wagner36fe60d2018-07-10 17:38:12 -0400627 inflator->setImage(index, std::move(image));
reed54dc4872016-09-13 08:09:45 -0700628 }
629}
630
631sk_sp<SkTypeface> SkPipeInflator::makeTypeface(const void* data, size_t size) {
Mike Reedc0cec872017-12-14 10:45:27 -0500632 if (fProcs.fTypefaceProc) {
633 return fProcs.fTypefaceProc(data, size, fProcs.fTypefaceCtx);
reed54dc4872016-09-13 08:09:45 -0700634 }
635 SkMemoryStream stream(data, size, false);
636 return SkTypeface::MakeDeserialize(&stream);
637}
638
639static void defineTypeface_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
640 SkASSERT(SkPipeVerb::kDefineTypeface == unpack_verb(packedVerb));
641 SkPipeInflator* inflator = (SkPipeInflator*)reader.getInflator();
642 uint32_t extra = unpack_verb_extra(packedVerb);
643 int index = extra & kIndex_ObjectDefinitionMask;
644
645 if (extra & kUndef_ObjectDefinitionMask) {
646 // zero-index means we are "forgetting" that cache entry
647 inflator->setTypeface(index, nullptr);
648 } else {
649 // we are defining a new image
650 sk_sp<SkData> data = reader.readByteArrayAsData();
651 // TODO: seems like we could "peek" to see the array, and not need to copy it.
Florin Malitac2ea3272018-05-10 09:41:38 -0400652 sk_sp<SkTypeface> tf = data ? inflator->makeTypeface(data->data(), data->size()) : nullptr;
Ben Wagner36fe60d2018-07-10 17:38:12 -0400653 inflator->setTypeface(index, std::move(tf));
reed54dc4872016-09-13 08:09:45 -0700654 }
655}
656
657static void defineFactory_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
658 SkASSERT(SkPipeVerb::kDefineFactory == unpack_verb(packedVerb));
659 SkPipeInflator* inflator = (SkPipeInflator*)reader.getInflator();
660 uint32_t extra = unpack_verb_extra(packedVerb);
661 int index = extra >> kNameLength_DefineFactoryExtraBits;
662 size_t len = extra & kNameLength_DefineFactoryExtraMask;
663 // +1 for the trailing null char
664 const char* name = (const char*)reader.skip(SkAlign4(len + 1));
665 SkFlattenable::Factory factory = reader.findFactory(name);
666 if (factory) {
667 inflator->setFactory(index, factory);
668 }
669}
670
671static void definePicture_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
672 SkASSERT(SkPipeVerb::kDefinePicture == unpack_verb(packedVerb));
673 int deleteIndex = unpack_verb_extra(packedVerb);
674
675 SkPipeInflator* inflator = (SkPipeInflator*)reader.getInflator();
676
677 if (deleteIndex) {
678 inflator->setPicture(deleteIndex - 1, nullptr);
679 } else {
680 SkPictureRecorder recorder;
681 int pictureIndex = -1; // invalid
Mike Reed22d77cb2018-01-03 13:22:51 -0500682 const SkRect* cull = reader.skipT<SkRect>();
683 if (!cull) {
684 return;
685 }
reed54dc4872016-09-13 08:09:45 -0700686 do_playback(reader, recorder.beginRecording(*cull), &pictureIndex);
687 SkASSERT(pictureIndex > 0);
688 sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
Ben Wagner36fe60d2018-07-10 17:38:12 -0400689 inflator->setPicture(pictureIndex, std::move(picture));
reed54dc4872016-09-13 08:09:45 -0700690 }
691}
692
693static void endPicture_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) {
Ben Wagner7ca9a742017-08-17 14:05:04 -0400694 SK_ABORT("not reached"); // never call me
reed54dc4872016-09-13 08:09:45 -0700695}
696
697///////////////////////////////////////////////////////////////////////////////////////////////////
698
699struct HandlerRec {
700 SkPipeHandler fProc;
701 const char* fName;
702};
703
704#define HANDLER(name) { name##_handler, #name }
705const HandlerRec gPipeHandlers[] = {
706 HANDLER(save),
707 HANDLER(saveLayer),
708 HANDLER(restore),
709 HANDLER(concat),
Ben Wagner63fd7602017-10-09 15:45:33 -0400710
reed54dc4872016-09-13 08:09:45 -0700711 HANDLER(clipRect),
712 HANDLER(clipRRect),
713 HANDLER(clipPath),
714 HANDLER(clipRegion),
Ben Wagner63fd7602017-10-09 15:45:33 -0400715
reed54dc4872016-09-13 08:09:45 -0700716 HANDLER(drawArc),
717 HANDLER(drawAtlas),
718 HANDLER(drawDRRect),
719 HANDLER(drawText),
720 HANDLER(drawPosText),
721 HANDLER(drawPosTextH),
722 HANDLER(drawRegion),
reed54dc4872016-09-13 08:09:45 -0700723 HANDLER(drawTextBlob),
724 HANDLER(drawTextRSXform),
725 HANDLER(drawPatch),
726 HANDLER(drawPaint),
727 HANDLER(drawPoints),
728 HANDLER(drawRect),
729 HANDLER(drawPath),
Mike Reed7c9c9e42018-01-03 09:23:34 -0500730 HANDLER(drawShadowRec),
reed54dc4872016-09-13 08:09:45 -0700731 HANDLER(drawOval),
732 HANDLER(drawRRect),
Ben Wagner63fd7602017-10-09 15:45:33 -0400733
reed54dc4872016-09-13 08:09:45 -0700734 HANDLER(drawImage),
735 HANDLER(drawImageRect),
736 HANDLER(drawImageNine),
737 HANDLER(drawImageLattice),
Ben Wagner63fd7602017-10-09 15:45:33 -0400738
reed54dc4872016-09-13 08:09:45 -0700739 HANDLER(drawVertices),
Ben Wagner63fd7602017-10-09 15:45:33 -0400740
reed54dc4872016-09-13 08:09:45 -0700741 HANDLER(drawPicture),
742 HANDLER(drawAnnotation),
743
744 HANDLER(defineImage),
745 HANDLER(defineTypeface),
746 HANDLER(defineFactory),
747 HANDLER(definePicture),
748 HANDLER(endPicture), // handled special -- should never be called
749};
750#undef HANDLER
751
752///////////////////////////////////////////////////////////////////////////////////////////////////
753
754class SkPipeDeserializer::Impl {
755public:
756 SkRefSet<SkImage> fImages;
757 SkRefSet<SkPicture> fPictures;
758 SkRefSet<SkTypeface> fTypefaces;
759 SkTDArray<SkFlattenable::Factory> fFactories;
Mike Reedc0cec872017-12-14 10:45:27 -0500760 SkDeserialProcs fProcs;
reed54dc4872016-09-13 08:09:45 -0700761};
762
763SkPipeDeserializer::SkPipeDeserializer() : fImpl(new Impl) {}
764SkPipeDeserializer::~SkPipeDeserializer() {}
765
Mike Reedc0cec872017-12-14 10:45:27 -0500766void SkPipeDeserializer::setDeserialProcs(const SkDeserialProcs& procs) {
767 fImpl->fProcs = procs;
Mike Reed3ac64b42016-10-18 19:34:08 -0400768}
769
reed54dc4872016-09-13 08:09:45 -0700770sk_sp<SkImage> SkPipeDeserializer::readImage(const void* data, size_t size) {
771 if (size < sizeof(uint32_t)) {
reed7e3ba9f2016-09-13 17:25:19 -0700772 SkDebugf("-------- data length too short for readImage %d\n", size);
reed54dc4872016-09-13 08:09:45 -0700773 return nullptr;
774 }
775
reed7e3ba9f2016-09-13 17:25:19 -0700776 const uint32_t* ptr = (const uint32_t*)data;
777 uint32_t packedVerb = *ptr++;
778 size -= 4;
779
780 if (SkPipeVerb::kDefineImage == unpack_verb(packedVerb)) {
781 SkPipeInflator inflator(&fImpl->fImages, &fImpl->fPictures,
782 &fImpl->fTypefaces, &fImpl->fFactories,
Mike Reedc0cec872017-12-14 10:45:27 -0500783 fImpl->fProcs);
reed7e3ba9f2016-09-13 17:25:19 -0700784 SkPipeReader reader(this, ptr, size);
785 reader.setInflator(&inflator);
786 defineImage_handler(reader, packedVerb, nullptr);
787 packedVerb = reader.read32(); // read the next verb
788 }
789 if (SkPipeVerb::kWriteImage != unpack_verb(packedVerb)) {
790 SkDebugf("-------- unexpected verb for readImage %d\n", unpack_verb(packedVerb));
reed54dc4872016-09-13 08:09:45 -0700791 return nullptr;
792 }
reed7e3ba9f2016-09-13 17:25:19 -0700793 int index = unpack_verb_extra(packedVerb);
794 if (0 == index) {
795 return nullptr; // writer failed
796 }
797 return sk_ref_sp(fImpl->fImages.get(index - 1));
reed54dc4872016-09-13 08:09:45 -0700798}
799
reed262052c2016-09-15 14:24:53 -0700800sk_sp<SkPicture> SkPipeDeserializer::readPicture(const void* data, size_t size) {
801 if (size < sizeof(uint32_t)) {
802 SkDebugf("-------- data length too short for readPicture %d\n", size);
803 return nullptr;
804 }
805
806 const uint32_t* ptr = (const uint32_t*)data;
807 uint32_t packedVerb = *ptr++;
808 size -= 4;
809
810 if (SkPipeVerb::kDefinePicture == unpack_verb(packedVerb)) {
811 SkPipeInflator inflator(&fImpl->fImages, &fImpl->fPictures,
812 &fImpl->fTypefaces, &fImpl->fFactories,
Mike Reedc0cec872017-12-14 10:45:27 -0500813 fImpl->fProcs);
reed262052c2016-09-15 14:24:53 -0700814 SkPipeReader reader(this, ptr, size);
815 reader.setInflator(&inflator);
816 definePicture_handler(reader, packedVerb, nullptr);
817 packedVerb = reader.read32(); // read the next verb
818 }
819 if (SkPipeVerb::kWritePicture != unpack_verb(packedVerb)) {
820 SkDebugf("-------- unexpected verb for readPicture %d\n", unpack_verb(packedVerb));
821 return nullptr;
822 }
823 int index = unpack_verb_extra(packedVerb);
824 if (0 == index) {
825 return nullptr; // writer failed
826 }
827 return sk_ref_sp(fImpl->fPictures.get(index - 1));
828}
829
reed54dc4872016-09-13 08:09:45 -0700830static bool do_playback(SkPipeReader& reader, SkCanvas* canvas, int* endPictureIndex) {
831 int indent = 0;
832
833 const bool showEachVerb = false;
834 int counter = 0;
835 while (!reader.eof()) {
836 uint32_t prevOffset = reader.offset();
837 uint32_t packedVerb = reader.read32();
838 SkPipeVerb verb = unpack_verb(packedVerb);
839 if ((unsigned)verb >= SK_ARRAY_COUNT(gPipeHandlers)) {
840 SkDebugf("------- bad verb %d\n", verb);
841 return false;
842 }
843 if (SkPipeVerb::kRestore == verb) {
844 indent -= 1;
845 SkASSERT(indent >= 0);
846 }
847
848 if (SkPipeVerb::kEndPicture == verb) {
849 if (endPictureIndex) {
850 *endPictureIndex = unpack_verb_extra(packedVerb);
851 }
852 return true;
853 }
854 HandlerRec rec = gPipeHandlers[(unsigned)verb];
855 rec.fProc(reader, packedVerb, canvas);
856 if (showEachVerb) {
857 for (int i = 0; i < indent; ++i) {
858 SkDebugf(" ");
859 }
860 SkDebugf("%d [%d] %s %d\n", prevOffset, counter++, rec.fName, reader.offset() - prevOffset);
861 }
862 if (!reader.isValid()) {
863 SkDebugf("-------- bad reader\n");
864 return false;
865 }
866
867 switch (verb) {
868 case SkPipeVerb::kSave:
869 case SkPipeVerb::kSaveLayer:
870 indent += 1;
871 break;
872 default:
873 break;
874 }
875 }
876 return true;
877}
878
879bool SkPipeDeserializer::playback(const void* data, size_t size, SkCanvas* canvas) {
880 SkPipeInflator inflator(&fImpl->fImages, &fImpl->fPictures,
881 &fImpl->fTypefaces, &fImpl->fFactories,
Mike Reedc0cec872017-12-14 10:45:27 -0500882 fImpl->fProcs);
reed54dc4872016-09-13 08:09:45 -0700883 SkPipeReader reader(this, data, size);
884 reader.setInflator(&inflator);
885 return do_playback(reader, canvas);
886}
887