reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 1 | /* |
| 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 Reed | 7c9c9e4 | 2018-01-03 09:23:34 -0500 | [diff] [blame] | 9 | #include "SkCanvasPriv.h" |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 10 | #include "SkDeduper.h" |
Mike Reed | 7c9c9e4 | 2018-01-03 09:23:34 -0500 | [diff] [blame] | 11 | #include "SkDrawShadowInfo.h" |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 12 | #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 Clark | 53c8769 | 2018-07-17 08:59:34 -0400 | [diff] [blame] | 19 | #include "SkTextBlobPriv.h" |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 20 | #include "SkTypeface.h" |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 21 | #include "SkVertices.h" |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 22 | |
| 23 | class SkPipeReader; |
| 24 | |
| 25 | static bool do_playback(SkPipeReader& reader, SkCanvas* canvas, int* endPictureIndex = nullptr); |
| 26 | |
| 27 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 28 | |
| 29 | class SkPipeInflator : public SkInflator { |
| 30 | public: |
| 31 | SkPipeInflator(SkRefSet<SkImage>* images, SkRefSet<SkPicture>* pictures, |
| 32 | SkRefSet<SkTypeface>* typefaces, SkTDArray<SkFlattenable::Factory>* factories, |
Mike Reed | c0cec87 | 2017-12-14 10:45:27 -0500 | [diff] [blame] | 33 | const SkDeserialProcs& procs) |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 34 | : fImages(images) |
| 35 | , fPictures(pictures) |
| 36 | , fTypefaces(typefaces) |
| 37 | , fFactories(factories) |
Mike Reed | c0cec87 | 2017-12-14 10:45:27 -0500 | [diff] [blame] | 38 | , fProcs(procs) |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 39 | {} |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 40 | |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 41 | 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 Wagner | 36fe60d | 2018-07-10 17:38:12 -0400 | [diff] [blame] | 54 | bool setImage(int index, sk_sp<SkImage> img) { |
| 55 | return fImages->set(index - 1, std::move(img)); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 56 | } |
Ben Wagner | 36fe60d | 2018-07-10 17:38:12 -0400 | [diff] [blame] | 57 | bool setPicture(int index, sk_sp<SkPicture> pic) { |
| 58 | return fPictures->set(index - 1, std::move(pic)); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 59 | } |
Ben Wagner | 36fe60d | 2018-07-10 17:38:12 -0400 | [diff] [blame] | 60 | bool setTypeface(int index, sk_sp<SkTypeface> face) { |
| 61 | return fTypefaces->set(index - 1, std::move(face)); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 62 | } |
| 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 Reed | c0cec87 | 2017-12-14 10:45:27 -0500 | [diff] [blame] | 79 | void setDeserialProcs(const SkDeserialProcs& procs) { |
| 80 | fProcs = procs; |
Mike Reed | 3ac64b4 | 2016-10-18 19:34:08 -0400 | [diff] [blame] | 81 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 82 | |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 83 | sk_sp<SkTypeface> makeTypeface(const void* data, size_t size); |
Mike Reed | 3ac64b4 | 2016-10-18 19:34:08 -0400 | [diff] [blame] | 84 | sk_sp<SkImage> makeImage(const sk_sp<SkData>&); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | SkRefSet<SkImage>* fImages; |
| 88 | SkRefSet<SkPicture>* fPictures; |
| 89 | SkRefSet<SkTypeface>* fTypefaces; |
| 90 | SkTDArray<SkFlattenable::Factory>* fFactories; |
Mike Reed | c0cec87 | 2017-12-14 10:45:27 -0500 | [diff] [blame] | 91 | SkDeserialProcs fProcs; |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 95 | |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 96 | static SkRRect read_rrect(SkReadBuffer& reader) { |
| 97 | SkRRect rrect; |
| 98 | rrect.readFromMemory(reader.skip(SkRRect::kSizeInMemory), SkRRect::kSizeInMemory); |
| 99 | return rrect; |
| 100 | } |
| 101 | |
| 102 | static SkMatrix read_sparse_matrix(SkReadBuffer& reader, SkMatrix::TypeMask tm) { |
| 103 | SkMatrix matrix; |
| 104 | matrix.reset(); |
| 105 | |
| 106 | if (tm & SkMatrix::kPerspective_Mask) { |
Mike Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 107 | matrix.set9(reader.skipT<SkScalar>(9)); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 108 | } else if (tm & SkMatrix::kAffine_Mask) { |
Mike Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 109 | const SkScalar* tmp = reader.skipT<SkScalar>(6); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 110 | 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 Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 117 | const SkScalar* tmp = reader.skipT<SkScalar>(4); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 118 | 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 Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 123 | const SkScalar* tmp = reader.skipT<SkScalar>(2); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 124 | 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 | */ |
| 150 | static SkPaint read_paint(SkReadBuffer& reader) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 151 | SkPaint paint; |
| 152 | |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 153 | uint32_t packedFlags = reader.read32(); |
| 154 | uint32_t extra = reader.read32(); |
| 155 | unsigned nondef = extra >> 16; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 156 | paint.setBlendMode(SkBlendMode((extra >> 8) & 0xFF)); |
| 157 | SkASSERT((extra & 0xFF) == 0); // zero pad byte |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 158 | |
| 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); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 182 | CHECK_SET_FLATTENABLE(MaskFilter); |
| 183 | CHECK_SET_FLATTENABLE(ColorFilter); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 184 | CHECK_SET_FLATTENABLE(ImageFilter); |
| 185 | CHECK_SET_FLATTENABLE(DrawLooper); |
| 186 | |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 187 | return paint; |
| 188 | } |
| 189 | |
| 190 | class SkPipeReader : public SkReadBuffer { |
| 191 | public: |
| 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 Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 208 | |
Mike Reed | e97e792 | 2018-01-18 15:57:38 -0500 | [diff] [blame] | 209 | bool readPaint(SkPaint* paint) override { |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 210 | *paint = read_paint(*this); |
Mike Reed | e97e792 | 2018-01-18 15:57:38 -0500 | [diff] [blame] | 211 | return this->isValid(); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 212 | } |
| 213 | }; |
| 214 | |
| 215 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 216 | |
| 217 | typedef void (*SkPipeHandler)(SkPipeReader&, uint32_t packedVerb, SkCanvas*); |
| 218 | |
| 219 | static void save_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) { |
| 220 | SkASSERT(SkPipeVerb::kSave == unpack_verb(packedVerb)); |
| 221 | canvas->save(); |
| 222 | } |
| 223 | |
| 224 | static 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 Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 227 | const SkRect* bounds = (extra & kHasBounds_SaveLayerMask) ? reader.skipT<SkRect>() : nullptr; |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 228 | 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 Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 237 | 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 | } |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 245 | SkCanvas::SaveLayerFlags flags = (SkCanvas::SaveLayerFlags)(extra & kFlags_SaveLayerMask); |
| 246 | |
| 247 | // unremap this wacky flag |
| 248 | if (extra & kDontClipToLayer_SaveLayerMask) { |
Cary Clark | 7eddfb8 | 2018-03-13 14:41:10 -0400 | [diff] [blame] | 249 | flags |= SkCanvasPriv::kDontClipToLayer_SaveLayerFlag; |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Mike Klein | b34ab04 | 2017-05-01 21:34:14 +0000 | [diff] [blame] | 252 | canvas->saveLayer(SkCanvas::SaveLayerRec(bounds, paint, backdrop.get(), clipMask.get(), |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 253 | (extra & kHasClipMatrix_SaveLayerMask) ? &clipMatrix : nullptr, flags)); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static void restore_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) { |
| 257 | SkASSERT(SkPipeVerb::kRestore == unpack_verb(packedVerb)); |
| 258 | canvas->restore(); |
| 259 | } |
| 260 | |
| 261 | static 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 | |
| 272 | static void clipRect_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) { |
| 273 | SkASSERT(SkPipeVerb::kClipRect == unpack_verb(packedVerb)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 274 | SkClipOp op = (SkClipOp)(unpack_verb_extra(packedVerb) >> 1); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 275 | bool isAA = unpack_verb_extra(packedVerb) & 1; |
Mike Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 276 | canvas->clipRect(*reader.skipT<SkRect>(), op, isAA); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static void clipRRect_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) { |
| 280 | SkASSERT(SkPipeVerb::kClipRRect == unpack_verb(packedVerb)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 281 | SkClipOp op = (SkClipOp)(unpack_verb_extra(packedVerb) >> 1); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 282 | bool isAA = unpack_verb_extra(packedVerb) & 1; |
| 283 | canvas->clipRRect(read_rrect(reader), op, isAA); |
| 284 | } |
| 285 | |
| 286 | static void clipPath_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) { |
| 287 | SkASSERT(SkPipeVerb::kClipPath == unpack_verb(packedVerb)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 288 | SkClipOp op = (SkClipOp)(unpack_verb_extra(packedVerb) >> 1); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 289 | bool isAA = unpack_verb_extra(packedVerb) & 1; |
| 290 | SkPath path; |
| 291 | reader.readPath(&path); |
| 292 | canvas->clipPath(path, op, isAA); |
| 293 | } |
| 294 | |
| 295 | static void clipRegion_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) { |
| 296 | SkASSERT(SkPipeVerb::kClipRegion == unpack_verb(packedVerb)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 297 | SkClipOp op = (SkClipOp)(unpack_verb_extra(packedVerb) >> 1); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 298 | SkRegion region; |
| 299 | reader.readRegion(®ion); |
| 300 | canvas->clipRegion(region, op); |
| 301 | } |
| 302 | |
| 303 | static 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 Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 306 | const SkScalar* scalars = reader.skipT<SkScalar>(6); // bounds[0..3], start[4], sweep[5] |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 307 | const SkRect* bounds = (const SkRect*)scalars; |
| 308 | canvas->drawArc(*bounds, scalars[4], scalars[5], useCenter, read_paint(reader)); |
| 309 | } |
| 310 | |
| 311 | static void drawAtlas_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) { |
| 312 | SkASSERT(SkPipeVerb::kDrawAtlas == unpack_verb(packedVerb)); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 313 | SkBlendMode mode = (SkBlendMode)(packedVerb & kMode_DrawAtlasMask); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 314 | sk_sp<SkImage> image(reader.readImage()); |
| 315 | int count = reader.read32(); |
Mike Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 316 | const SkRSXform* xform = reader.skipT<SkRSXform>(count); |
| 317 | const SkRect* rect = reader.skipT<SkRect>(count); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 318 | const SkColor* color = nullptr; |
| 319 | if (packedVerb & kHasColors_DrawAtlasMask) { |
Mike Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 320 | color = reader.skipT<SkColor>(count); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 321 | } |
| 322 | const SkRect* cull = nullptr; |
| 323 | if (packedVerb & kHasCull_DrawAtlasMask) { |
Mike Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 324 | cull = reader.skipT<SkRect>(); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 325 | } |
| 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 | |
| 334 | static 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 | |
| 341 | static 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 | |
| 353 | static 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 Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 361 | const SkPoint* pos = reader.skipT<SkPoint>(count); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 362 | SkPaint paint = read_paint(reader); |
| 363 | SkASSERT(paint.countText(text, len) == count); |
| 364 | canvas->drawPosText(text, len, pos, paint); |
| 365 | } |
| 366 | |
| 367 | static 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 Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 375 | const SkScalar* xpos = reader.skipT<SkScalar>(count); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 376 | 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 | |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 382 | static void drawTextBlob_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) { |
Cary Clark | 53c8769 | 2018-07-17 08:59:34 -0400 | [diff] [blame] | 383 | sk_sp<SkTextBlob> tb = SkTextBlobPriv::MakeFromBuffer(reader); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 384 | SkScalar x = reader.readScalar(); |
| 385 | SkScalar y = reader.readScalar(); |
| 386 | canvas->drawTextBlob(tb, x, y, read_paint(reader)); |
| 387 | } |
| 388 | |
| 389 | static 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 Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 397 | const SkRSXform* xform = reader.skipT<SkRSXform>(count); |
| 398 | const SkRect* cull = (packedVerb & 1) ? reader.skipT<SkRect>() : nullptr; |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 399 | SkPaint paint = read_paint(reader); |
| 400 | SkASSERT(paint.countText(text, len) == count); |
| 401 | canvas->drawTextRSXform(text, len, xform, cull, paint); |
| 402 | } |
| 403 | |
| 404 | static 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 Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 408 | const SkPoint* cubics = reader.skipT<SkPoint>(12); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 409 | if (packedVerb & kHasColors_DrawPatchExtraMask) { |
Mike Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 410 | colors = reader.skipT<SkColor>(4); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 411 | } |
| 412 | if (packedVerb & kHasTexture_DrawPatchExtraMask) { |
Mike Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 413 | tex = reader.skipT<SkPoint>(4); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 414 | } |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 415 | SkBlendMode mode = (SkBlendMode)(packedVerb & kModeEnum_DrawPatchExtraMask); |
| 416 | canvas->drawPatch(cubics, colors, tex, mode, read_paint(reader)); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static 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 | |
| 424 | static void drawRect_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) { |
| 425 | SkASSERT(SkPipeVerb::kDrawRect == unpack_verb(packedVerb)); |
Mike Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 426 | const SkRect* rect = reader.skipT<SkRect>(); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 427 | canvas->drawRect(*rect, read_paint(reader)); |
| 428 | } |
| 429 | |
| 430 | static 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 Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 437 | region.readFromMemory(reader.skipT<char>(size), size); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 438 | canvas->drawRegion(region, read_paint(reader)); |
| 439 | } |
| 440 | |
| 441 | static void drawOval_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) { |
| 442 | SkASSERT(SkPipeVerb::kDrawOval == unpack_verb(packedVerb)); |
Mike Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 443 | const SkRect* rect = reader.skipT<SkRect>(); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 444 | canvas->drawOval(*rect, read_paint(reader)); |
| 445 | } |
| 446 | |
| 447 | static 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 | |
| 453 | static 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 Reed | 7c9c9e4 | 2018-01-03 09:23:34 -0500 | [diff] [blame] | 460 | static 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 | |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 469 | static 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 Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 473 | const SkPoint* points = reader.skipT<SkPoint>(count); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 474 | canvas->drawPoints(mode, count, points, read_paint(reader)); |
| 475 | } |
| 476 | |
| 477 | static 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 | |
| 490 | static 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 Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 496 | reader.skipT<SkRect>() : nullptr; |
| 497 | const SkRect* dst = reader.skipT<SkRect>(); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 498 | 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 | |
| 510 | static 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 Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 513 | const SkIRect* center = reader.skipT<SkIRect>(); |
| 514 | const SkRect* dst = reader.skipT<SkRect>(); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 515 | 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 | |
| 523 | static 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 Reed | 7c9c9e4 | 2018-01-03 09:23:34 -0500 | [diff] [blame] | 528 | if (!SkCanvasPriv::ReadLattice(reader, &lattice)) { |
| 529 | return; |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 530 | } |
Mike Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 531 | const SkRect* dst = reader.skipT<SkRect>(); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 532 | |
| 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 | |
| 541 | static void drawVertices_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) { |
| 542 | SkASSERT(SkPipeVerb::kDrawVertices == unpack_verb(packedVerb)); |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 543 | SkBlendMode bmode = (SkBlendMode)unpack_verb_extra(packedVerb); |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 544 | sk_sp<SkVertices> vertices = nullptr; |
Florin Malita | c2ea327 | 2018-05-10 09:41:38 -0400 | [diff] [blame] | 545 | if (sk_sp<SkData> data = reader.readByteArrayAsData()) { |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 546 | vertices = SkVertices::Decode(data->data(), data->size()); |
| 547 | } |
| 548 | int boneCount = reader.read32(); |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 549 | const SkVertices::Bone* bones = boneCount ? reader.skipT<SkVertices::Bone>(boneCount) : nullptr; |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 550 | if (vertices) { |
| 551 | canvas->drawVertices(vertices, bones, boneCount, bmode, read_paint(reader)); |
Florin Malita | c2ea327 | 2018-05-10 09:41:38 -0400 | [diff] [blame] | 552 | } |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | static 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 | |
| 573 | static void drawAnnotation_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) { |
| 574 | SkASSERT(SkPipeVerb::kDrawAnnotation == unpack_verb(packedVerb)); |
Mike Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 575 | const SkRect* rect = reader.skipT<SkRect>(); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 576 | |
| 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 Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 582 | const char* key = reader.skipT<char>(len); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 583 | 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 Reed | 3ac64b4 | 2016-10-18 19:34:08 -0400 | [diff] [blame] | 603 | sk_sp<SkImage> SkPipeInflator::makeImage(const sk_sp<SkData>& data) { |
Mike Reed | c0cec87 | 2017-12-14 10:45:27 -0500 | [diff] [blame] | 604 | if (fProcs.fImageProc) { |
| 605 | return fProcs.fImageProc(data->data(), data->size(), fProcs.fImageCtx); |
Mike Reed | 3ac64b4 | 2016-10-18 19:34:08 -0400 | [diff] [blame] | 606 | } |
Mike Reed | c0cec87 | 2017-12-14 10:45:27 -0500 | [diff] [blame] | 607 | return SkImage::MakeFromEncoded(data); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Mike Reed | 3ac64b4 | 2016-10-18 19:34:08 -0400 | [diff] [blame] | 610 | |
reed | 7e3ba9f | 2016-09-13 17:25:19 -0700 | [diff] [blame] | 611 | static void defineImage_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas*) { |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 612 | 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 Malita | c2ea327 | 2018-05-10 09:41:38 -0400 | [diff] [blame] | 623 | sk_sp<SkImage> image = data ? inflator->makeImage(data) : nullptr; |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 624 | if (!image) { |
| 625 | SkDebugf("-- failed to decode\n"); |
| 626 | } |
Ben Wagner | 36fe60d | 2018-07-10 17:38:12 -0400 | [diff] [blame] | 627 | inflator->setImage(index, std::move(image)); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
| 631 | sk_sp<SkTypeface> SkPipeInflator::makeTypeface(const void* data, size_t size) { |
Mike Reed | c0cec87 | 2017-12-14 10:45:27 -0500 | [diff] [blame] | 632 | if (fProcs.fTypefaceProc) { |
| 633 | return fProcs.fTypefaceProc(data, size, fProcs.fTypefaceCtx); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 634 | } |
| 635 | SkMemoryStream stream(data, size, false); |
| 636 | return SkTypeface::MakeDeserialize(&stream); |
| 637 | } |
| 638 | |
| 639 | static 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 Malita | c2ea327 | 2018-05-10 09:41:38 -0400 | [diff] [blame] | 652 | sk_sp<SkTypeface> tf = data ? inflator->makeTypeface(data->data(), data->size()) : nullptr; |
Ben Wagner | 36fe60d | 2018-07-10 17:38:12 -0400 | [diff] [blame] | 653 | inflator->setTypeface(index, std::move(tf)); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
| 657 | static 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 | |
| 671 | static 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 Reed | 22d77cb | 2018-01-03 13:22:51 -0500 | [diff] [blame] | 682 | const SkRect* cull = reader.skipT<SkRect>(); |
| 683 | if (!cull) { |
| 684 | return; |
| 685 | } |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 686 | do_playback(reader, recorder.beginRecording(*cull), &pictureIndex); |
| 687 | SkASSERT(pictureIndex > 0); |
| 688 | sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture(); |
Ben Wagner | 36fe60d | 2018-07-10 17:38:12 -0400 | [diff] [blame] | 689 | inflator->setPicture(pictureIndex, std::move(picture)); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | |
| 693 | static void endPicture_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas* canvas) { |
Ben Wagner | 7ca9a74 | 2017-08-17 14:05:04 -0400 | [diff] [blame] | 694 | SK_ABORT("not reached"); // never call me |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 698 | |
| 699 | struct HandlerRec { |
| 700 | SkPipeHandler fProc; |
| 701 | const char* fName; |
| 702 | }; |
| 703 | |
| 704 | #define HANDLER(name) { name##_handler, #name } |
| 705 | const HandlerRec gPipeHandlers[] = { |
| 706 | HANDLER(save), |
| 707 | HANDLER(saveLayer), |
| 708 | HANDLER(restore), |
| 709 | HANDLER(concat), |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 710 | |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 711 | HANDLER(clipRect), |
| 712 | HANDLER(clipRRect), |
| 713 | HANDLER(clipPath), |
| 714 | HANDLER(clipRegion), |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 715 | |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 716 | HANDLER(drawArc), |
| 717 | HANDLER(drawAtlas), |
| 718 | HANDLER(drawDRRect), |
| 719 | HANDLER(drawText), |
| 720 | HANDLER(drawPosText), |
| 721 | HANDLER(drawPosTextH), |
| 722 | HANDLER(drawRegion), |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 723 | HANDLER(drawTextBlob), |
| 724 | HANDLER(drawTextRSXform), |
| 725 | HANDLER(drawPatch), |
| 726 | HANDLER(drawPaint), |
| 727 | HANDLER(drawPoints), |
| 728 | HANDLER(drawRect), |
| 729 | HANDLER(drawPath), |
Mike Reed | 7c9c9e4 | 2018-01-03 09:23:34 -0500 | [diff] [blame] | 730 | HANDLER(drawShadowRec), |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 731 | HANDLER(drawOval), |
| 732 | HANDLER(drawRRect), |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 733 | |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 734 | HANDLER(drawImage), |
| 735 | HANDLER(drawImageRect), |
| 736 | HANDLER(drawImageNine), |
| 737 | HANDLER(drawImageLattice), |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 738 | |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 739 | HANDLER(drawVertices), |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 740 | |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 741 | 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 | |
| 754 | class SkPipeDeserializer::Impl { |
| 755 | public: |
| 756 | SkRefSet<SkImage> fImages; |
| 757 | SkRefSet<SkPicture> fPictures; |
| 758 | SkRefSet<SkTypeface> fTypefaces; |
| 759 | SkTDArray<SkFlattenable::Factory> fFactories; |
Mike Reed | c0cec87 | 2017-12-14 10:45:27 -0500 | [diff] [blame] | 760 | SkDeserialProcs fProcs; |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 761 | }; |
| 762 | |
| 763 | SkPipeDeserializer::SkPipeDeserializer() : fImpl(new Impl) {} |
| 764 | SkPipeDeserializer::~SkPipeDeserializer() {} |
| 765 | |
Mike Reed | c0cec87 | 2017-12-14 10:45:27 -0500 | [diff] [blame] | 766 | void SkPipeDeserializer::setDeserialProcs(const SkDeserialProcs& procs) { |
| 767 | fImpl->fProcs = procs; |
Mike Reed | 3ac64b4 | 2016-10-18 19:34:08 -0400 | [diff] [blame] | 768 | } |
| 769 | |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 770 | sk_sp<SkImage> SkPipeDeserializer::readImage(const void* data, size_t size) { |
| 771 | if (size < sizeof(uint32_t)) { |
reed | 7e3ba9f | 2016-09-13 17:25:19 -0700 | [diff] [blame] | 772 | SkDebugf("-------- data length too short for readImage %d\n", size); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 773 | return nullptr; |
| 774 | } |
| 775 | |
reed | 7e3ba9f | 2016-09-13 17:25:19 -0700 | [diff] [blame] | 776 | 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 Reed | c0cec87 | 2017-12-14 10:45:27 -0500 | [diff] [blame] | 783 | fImpl->fProcs); |
reed | 7e3ba9f | 2016-09-13 17:25:19 -0700 | [diff] [blame] | 784 | 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)); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 791 | return nullptr; |
| 792 | } |
reed | 7e3ba9f | 2016-09-13 17:25:19 -0700 | [diff] [blame] | 793 | 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)); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 798 | } |
| 799 | |
reed | 262052c | 2016-09-15 14:24:53 -0700 | [diff] [blame] | 800 | sk_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 Reed | c0cec87 | 2017-12-14 10:45:27 -0500 | [diff] [blame] | 813 | fImpl->fProcs); |
reed | 262052c | 2016-09-15 14:24:53 -0700 | [diff] [blame] | 814 | 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 | |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 830 | static 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 | |
| 879 | bool SkPipeDeserializer::playback(const void* data, size_t size, SkCanvas* canvas) { |
| 880 | SkPipeInflator inflator(&fImpl->fImages, &fImpl->fPictures, |
| 881 | &fImpl->fTypefaces, &fImpl->fFactories, |
Mike Reed | c0cec87 | 2017-12-14 10:45:27 -0500 | [diff] [blame] | 882 | fImpl->fProcs); |
reed | 54dc487 | 2016-09-13 08:09:45 -0700 | [diff] [blame] | 883 | SkPipeReader reader(this, data, size); |
| 884 | reader.setInflator(&inflator); |
| 885 | return do_playback(reader, canvas); |
| 886 | } |
| 887 | |