blob: 911595c47c3a316b99ec299acb8aa41b85f72ddb [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
Hal Canary95e3c052017-01-11 12:44:43 -05008#include "SkAutoMalloc.h"
Mike Reed7c9c9e42018-01-03 09:23:34 -05009#include "SkCanvasPriv.h"
reed54dc4872016-09-13 08:09:45 -070010#include "SkColorFilter.h"
Brian Osman37886ce2018-03-09 13:40:31 -050011#include "SkDrawable.h"
reed54dc4872016-09-13 08:09:45 -070012#include "SkDrawLooper.h"
Mike Reed7c9c9e42018-01-03 09:23:34 -050013#include "SkDrawShadowInfo.h"
reed54dc4872016-09-13 08:09:45 -070014#include "SkImageFilter.h"
15#include "SkMaskFilter.h"
Hal Canary95e3c052017-01-11 12:44:43 -050016#include "SkPathEffect.h"
reed54dc4872016-09-13 08:09:45 -070017#include "SkPipeCanvas.h"
18#include "SkPipeFormat.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000019#include "SkRSXform.h"
reed54dc4872016-09-13 08:09:45 -070020#include "SkShader.h"
21#include "SkStream.h"
22#include "SkTextBlob.h"
23#include "SkTypeface.h"
24
25template <typename T> void write_rrect(T* writer, const SkRRect& rrect) {
26 char tmp[SkRRect::kSizeInMemory];
27 rrect.writeToMemory(tmp);
28 writer->write(tmp, SkRRect::kSizeInMemory);
29}
30
31template <typename T> void write_pad(T* writer, const void* buffer, size_t len) {
32 writer->write(buffer, len & ~3);
33 if (len & 3) {
34 const char* src = (const char*)buffer + (len & ~3);
35 len &= 3;
36 uint32_t tmp = 0;
37 memcpy(&tmp, src, len);
38 writer->write(&tmp, 4);
39 }
40}
41
42///////////////////////////////////////////////////////////////////////////////////////////////////
43
44static uint16_t compute_nondef(const SkPaint& paint, PaintUsage usage) {
45 // kRespectsStroke_PaintUsage is only valid if other bits are also set
46 SkASSERT(0 != (usage & ~kRespectsStroke_PaintUsage));
47
48 const SkScalar kTextSize_Default = 12;
49 const SkScalar kTextScaleX_Default = 1;
50 const SkScalar kTextSkewX_Default = 0;
51 const SkScalar kStrokeWidth_Default = 0;
52 const SkScalar kStrokeMiter_Default = 4;
53 const SkColor kColor_Default = SK_ColorBLACK;
54
55 unsigned bits = (paint.getColor() != kColor_Default) ? kColor_NonDef : 0;
56
57 if (usage & kText_PaintUsage) {
58 bits |= (paint.getTextSize() != kTextSize_Default ? kTextSize_NonDef : 0);
59 bits |= (paint.getTextScaleX() != kTextScaleX_Default ? kTextScaleX_NonDef : 0);
60 bits |= (paint.getTextSkewX() != kTextSkewX_Default ? kTextSkewX_NonDef : 0);
61 bits |= (paint.getTypeface() ? kTypeface_NonDef : 0);
62 }
63
64 // TODO: kImage_PaintUsage only needs the shader/maskfilter IF its colortype is kAlpha_8
65
66 if (usage & (kVertices_PaintUsage | kDrawPaint_PaintUsage | kImage_PaintUsage |
67 kText_PaintUsage | kGeometry_PaintUsage | kTextBlob_PaintUsage)) {
68 bits |= (paint.getShader() ? kShader_NonDef : 0);
69 }
70
71 if (usage & (kText_PaintUsage | kGeometry_PaintUsage | kTextBlob_PaintUsage)) {
72 bits |= (paint.getPathEffect() ? kPathEffect_NonDef : 0);
reed54dc4872016-09-13 08:09:45 -070073
74 if (paint.getStyle() != SkPaint::kFill_Style || (usage & kRespectsStroke_PaintUsage)) {
75 bits |= (paint.getStrokeWidth() != kStrokeWidth_Default ? kStrokeWidth_NonDef : 0);
76 bits |= (paint.getStrokeMiter() != kStrokeMiter_Default ? kStrokeMiter_NonDef : 0);
77 }
78 }
79
80 if (usage &
81 (kText_PaintUsage | kGeometry_PaintUsage | kImage_PaintUsage | kTextBlob_PaintUsage))
82 {
83 bits |= (paint.getMaskFilter() ? kMaskFilter_NonDef : 0);
84 }
85
reed54dc4872016-09-13 08:09:45 -070086 bits |= (paint.getColorFilter() ? kColorFilter_NonDef : 0);
87 bits |= (paint.getImageFilter() ? kImageFilter_NonDef : 0);
88 bits |= (paint.getDrawLooper() ? kDrawLooper_NonDef : 0);
89
90 return SkToU16(bits);
91}
92
93static uint32_t pack_paint_flags(unsigned flags, unsigned hint, unsigned align,
94 unsigned filter, unsigned style, unsigned caps, unsigned joins,
95 unsigned encoding) {
96 SkASSERT(kFlags_BPF + kHint_BPF + kAlign_BPF + kFilter_BPF <= 32);
97
98 ASSERT_FITS_IN(flags, kFlags_BPF);
99 ASSERT_FITS_IN(filter, kFilter_BPF);
100 ASSERT_FITS_IN(style, kStyle_BPF);
101 ASSERT_FITS_IN(caps, kCaps_BPF);
102 ASSERT_FITS_IN(joins, kJoins_BPF);
103 ASSERT_FITS_IN(hint, kHint_BPF);
104 ASSERT_FITS_IN(align, kAlign_BPF);
105 ASSERT_FITS_IN(encoding, kEncoding_BPF);
106
107 // left-align the fields of "known" size, and right-align the last (flatFlags) so it can easly
108 // add more bits in the future.
109
110 uint32_t packed = 0;
111 int shift = 32;
112
113 shift -= kFlags_BPF; packed |= (flags << shift);
114 shift -= kFilter_BPF; packed |= (filter << shift);
115 shift -= kStyle_BPF; packed |= (style << shift);
116 // these are only needed for stroking (geometry or text)
117 shift -= kCaps_BPF; packed |= (caps << shift);
118 shift -= kJoins_BPF; packed |= (joins << shift);
119 // these are only needed for text
120 shift -= kHint_BPF; packed |= (hint << shift);
121 shift -= kAlign_BPF; packed |= (align << shift);
122 shift -= kEncoding_BPF; packed |= (encoding << shift);
123
124 return packed;
125}
126
127#define CHECK_WRITE_SCALAR(writer, nondef, paint, Field) \
128 do { if (nondef & (k##Field##_NonDef)) { \
129 writer.writeScalar(paint.get##Field()); \
130 }} while (0)
131
132#define CHECK_WRITE_FLATTENABLE(writer, nondef, paint, Field) \
133 do { if (nondef & (k##Field##_NonDef)) { \
134 SkFlattenable* f = paint.get##Field(); \
135 SkASSERT(f != nullptr); \
136 writer.writeFlattenable(f); \
137 } } while (0)
138
139/*
140 * Header:
141 * paint flags : 32
142 * non_def bits : 16
143 * xfermode enum : 8
144 * pad zeros : 8
145 */
146static void write_paint(SkWriteBuffer& writer, const SkPaint& paint, unsigned usage) {
147 uint32_t packedFlags = pack_paint_flags(paint.getFlags(), paint.getHinting(),
148 paint.getTextAlign(), paint.getFilterQuality(),
149 paint.getStyle(), paint.getStrokeCap(),
150 paint.getStrokeJoin(), paint.getTextEncoding());
151 writer.write32(packedFlags);
152
153 unsigned nondef = compute_nondef(paint, (PaintUsage)usage);
reed54dc4872016-09-13 08:09:45 -0700154 const uint8_t pad = 0;
reed374772b2016-10-05 17:33:02 -0700155 writer.write32((nondef << 16) | ((unsigned)paint.getBlendMode() << 8) | pad);
reed54dc4872016-09-13 08:09:45 -0700156
157 CHECK_WRITE_SCALAR(writer, nondef, paint, TextSize);
158 CHECK_WRITE_SCALAR(writer, nondef, paint, TextScaleX);
159 CHECK_WRITE_SCALAR(writer, nondef, paint, TextSkewX);
160 CHECK_WRITE_SCALAR(writer, nondef, paint, StrokeWidth);
161 CHECK_WRITE_SCALAR(writer, nondef, paint, StrokeMiter);
162
163 if (nondef & kColor_NonDef) {
164 writer.write32(paint.getColor());
165 }
166 if (nondef & kTypeface_NonDef) {
167 // TODO: explore idea of writing bits indicating "use the prev (or prev N) face"
168 // e.g. 1-N bits is an index into a ring buffer of typefaces
169 SkTypeface* tf = paint.getTypeface();
170 SkASSERT(tf);
171 writer.writeTypeface(tf);
172 }
173
174 CHECK_WRITE_FLATTENABLE(writer, nondef, paint, PathEffect);
175 CHECK_WRITE_FLATTENABLE(writer, nondef, paint, Shader);
reed54dc4872016-09-13 08:09:45 -0700176 CHECK_WRITE_FLATTENABLE(writer, nondef, paint, MaskFilter);
177 CHECK_WRITE_FLATTENABLE(writer, nondef, paint, ColorFilter);
reed54dc4872016-09-13 08:09:45 -0700178 CHECK_WRITE_FLATTENABLE(writer, nondef, paint, ImageFilter);
179 CHECK_WRITE_FLATTENABLE(writer, nondef, paint, DrawLooper);
180}
181
182class SkPipeWriter : public SkBinaryWriteBuffer {
183 enum {
184 N = 1024/4,
185 };
186 uint32_t fStorage[N];
187 SkWStream* fStream;
188
189public:
190 SkPipeWriter(SkWStream* stream, SkDeduper* deduper)
191 : SkBinaryWriteBuffer(fStorage, sizeof(fStorage))
192 , fStream(stream)
193 {
194 this->setDeduper(deduper);
195 }
196
197 SkPipeWriter(SkPipeCanvas* pc) : SkPipeWriter(pc->fStream, pc->fDeduper) {}
198
199 ~SkPipeWriter() override {
200 SkASSERT(SkIsAlign4(fStream->bytesWritten()));
201 this->writeToStream(fStream);
202 }
203
204 void writePaint(const SkPaint& paint) override {
205 write_paint(*this, paint, kUnknown_PaintUsage);
206 }
207};
208
209///////////////////////////////////////////////////////////////////////////////////////////////////
210
211SkPipeCanvas::SkPipeCanvas(const SkRect& cull, SkPipeDeduper* deduper, SkWStream* stream)
Florin Malita439ace92016-12-02 12:05:41 -0500212 : INHERITED(cull.roundOut())
reed54dc4872016-09-13 08:09:45 -0700213 , fDeduper(deduper)
214 , fStream(stream)
215{}
216
217SkPipeCanvas::~SkPipeCanvas() {}
218
219void SkPipeCanvas::willSave() {
220 fStream->write32(pack_verb(SkPipeVerb::kSave));
221 this->INHERITED::willSave();
222}
223
224SkCanvas::SaveLayerStrategy SkPipeCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
225 SkPipeWriter writer(this);
226 uint32_t extra = rec.fSaveLayerFlags;
227
228 // remap this wacky flag
Cary Clark7eddfb82018-03-13 14:41:10 -0400229 if (extra & SkCanvasPriv::kDontClipToLayer_SaveLayerFlag) {
230 extra &= ~SkCanvasPriv::kDontClipToLayer_SaveLayerFlag;
reed54dc4872016-09-13 08:09:45 -0700231 extra |= kDontClipToLayer_SaveLayerMask;
232 }
233
234 if (rec.fBounds) {
235 extra |= kHasBounds_SaveLayerMask;
236 }
237 if (rec.fPaint) {
238 extra |= kHasPaint_SaveLayerMask;
239 }
240 if (rec.fBackdrop) {
241 extra |= kHasBackdrop_SaveLayerMask;
242 }
Florin Malita53f77bd2017-04-28 13:48:37 -0400243 if (rec.fClipMask) {
244 extra |= kHasClipMask_SaveLayerMask;
245 }
246 if (rec.fClipMatrix) {
247 extra |= kHasClipMatrix_SaveLayerMask;
248 }
Mike Kleinb3384842017-03-27 14:28:07 -0400249
reed54dc4872016-09-13 08:09:45 -0700250 writer.write32(pack_verb(SkPipeVerb::kSaveLayer, extra));
251 if (rec.fBounds) {
252 writer.writeRect(*rec.fBounds);
253 }
254 if (rec.fPaint) {
255 write_paint(writer, *rec.fPaint, kSaveLayer_PaintUsage);
256 }
257 if (rec.fBackdrop) {
258 writer.writeFlattenable(rec.fBackdrop);
259 }
Florin Malita53f77bd2017-04-28 13:48:37 -0400260 if (rec.fClipMask) {
Mike Kleinb34ab042017-05-01 21:34:14 +0000261 writer.writeImage(rec.fClipMask);
Florin Malita53f77bd2017-04-28 13:48:37 -0400262 }
263 if (rec.fClipMatrix) {
264 writer.writeMatrix(*rec.fClipMatrix);
265 }
266
reed54dc4872016-09-13 08:09:45 -0700267 return kNoLayer_SaveLayerStrategy;
268}
269
270void SkPipeCanvas::willRestore() {
271 fStream->write32(pack_verb(SkPipeVerb::kRestore));
272 this->INHERITED::willRestore();
273}
274
275template <typename T> void write_sparse_matrix(T* writer, const SkMatrix& matrix) {
276 SkMatrix::TypeMask tm = matrix.getType();
277 SkScalar tmp[9];
278 if (tm & SkMatrix::kPerspective_Mask) {
279 matrix.get9(tmp);
280 writer->write(tmp, 9 * sizeof(SkScalar));
281 } else if (tm & SkMatrix::kAffine_Mask) {
282 tmp[0] = matrix[SkMatrix::kMScaleX];
283 tmp[1] = matrix[SkMatrix::kMSkewX];
284 tmp[2] = matrix[SkMatrix::kMTransX];
285 tmp[3] = matrix[SkMatrix::kMScaleY];
286 tmp[4] = matrix[SkMatrix::kMSkewY];
287 tmp[5] = matrix[SkMatrix::kMTransY];
288 writer->write(tmp, 6 * sizeof(SkScalar));
289 } else if (tm & SkMatrix::kScale_Mask) {
290 tmp[0] = matrix[SkMatrix::kMScaleX];
291 tmp[1] = matrix[SkMatrix::kMTransX];
292 tmp[2] = matrix[SkMatrix::kMScaleY];
293 tmp[3] = matrix[SkMatrix::kMTransY];
294 writer->write(tmp, 4 * sizeof(SkScalar));
295 } else if (tm & SkMatrix::kTranslate_Mask) {
296 tmp[0] = matrix[SkMatrix::kMTransX];
297 tmp[1] = matrix[SkMatrix::kMTransY];
298 writer->write(tmp, 2 * sizeof(SkScalar));
299 }
300 // else write nothing for Identity
301}
302
303static void do_concat(SkWStream* stream, const SkMatrix& matrix, bool isSetMatrix) {
304 unsigned mtype = matrix.getType();
305 SkASSERT(0 == (mtype & ~kTypeMask_ConcatMask));
306 unsigned extra = mtype;
307 if (isSetMatrix) {
308 extra |= kSetMatrix_ConcatMask;
309 }
310 if (mtype || isSetMatrix) {
311 stream->write32(pack_verb(SkPipeVerb::kConcat, extra));
312 write_sparse_matrix(stream, matrix);
313 }
314}
315
316void SkPipeCanvas::didConcat(const SkMatrix& matrix) {
317 do_concat(fStream, matrix, false);
318 this->INHERITED::didConcat(matrix);
319}
320
321void SkPipeCanvas::didSetMatrix(const SkMatrix& matrix) {
322 do_concat(fStream, matrix, true);
323 this->INHERITED::didSetMatrix(matrix);
324}
325
Mike Reedc1f77742016-12-09 09:00:50 -0500326void SkPipeCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
reed54dc4872016-09-13 08:09:45 -0700327 fStream->write32(pack_verb(SkPipeVerb::kClipRect, ((unsigned)op << 1) | edgeStyle));
328 fStream->write(&rect, 4 * sizeof(SkScalar));
329
330 this->INHERITED::onClipRect(rect, op, edgeStyle);
331}
332
Mike Reedc1f77742016-12-09 09:00:50 -0500333void SkPipeCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
reed54dc4872016-09-13 08:09:45 -0700334 fStream->write32(pack_verb(SkPipeVerb::kClipRRect, ((unsigned)op << 1) | edgeStyle));
335 write_rrect(fStream, rrect);
336
337 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
338}
339
Mike Reedc1f77742016-12-09 09:00:50 -0500340void SkPipeCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
reed54dc4872016-09-13 08:09:45 -0700341 SkPipeWriter writer(this);
342 writer.write32(pack_verb(SkPipeVerb::kClipPath, ((unsigned)op << 1) | edgeStyle));
343 writer.writePath(path);
344
345 this->INHERITED::onClipPath(path, op, edgeStyle);
346}
347
Mike Reedc1f77742016-12-09 09:00:50 -0500348void SkPipeCanvas::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
reed54dc4872016-09-13 08:09:45 -0700349 SkPipeWriter writer(this);
350 writer.write32(pack_verb(SkPipeVerb::kClipRegion, (unsigned)op << 1));
351 writer.writeRegion(deviceRgn);
352
353 this->INHERITED::onClipRegion(deviceRgn, op);
354}
355
356///////////////////////////////////////////////////////////////////////////////////////////////////
357
358void SkPipeCanvas::onDrawArc(const SkRect& bounds, SkScalar startAngle, SkScalar sweepAngle,
359 bool useCenter, const SkPaint& paint) {
360 SkPipeWriter writer(this);
361 writer.write32(pack_verb(SkPipeVerb::kDrawArc, (int)useCenter));
362 writer.writeRect(bounds);
363 writer.writeScalar(startAngle);
364 writer.writeScalar(sweepAngle);
365 write_paint(writer, paint, kGeometry_PaintUsage);
366}
367
368void SkPipeCanvas::onDrawAtlas(const SkImage* image, const SkRSXform xform[], const SkRect rect[],
Mike Reedfaba3712016-11-03 14:45:31 -0400369 const SkColor colors[], int count, SkBlendMode mode,
reed54dc4872016-09-13 08:09:45 -0700370 const SkRect* cull, const SkPaint* paint) {
371 unsigned extra = (unsigned)mode;
372 SkASSERT(0 == (extra & ~kMode_DrawAtlasMask));
373 if (colors) {
374 extra |= kHasColors_DrawAtlasMask;
375 }
376 if (cull) {
377 extra |= kHasCull_DrawAtlasMask;
378 }
379 if (paint) {
380 extra |= kHasPaint_DrawAtlasMask;
381 }
382
383 SkPipeWriter writer(this);
384 writer.write32(pack_verb(SkPipeVerb::kDrawAtlas, extra));
385 writer.writeImage(image);
386 writer.write32(count);
387 writer.write(xform, count * sizeof(SkRSXform));
388 writer.write(rect, count * sizeof(SkRect));
389 if (colors) {
390 writer.write(colors, count * sizeof(SkColor));
391 }
392 if (cull) {
393 writer.writeRect(*cull);
394 }
395 if (paint) {
396 write_paint(writer, *paint, kImage_PaintUsage);
397 }
398}
399
400void SkPipeCanvas::onDrawPaint(const SkPaint& paint) {
401 SkPipeWriter writer(this);
402 writer.write32(pack_verb(SkPipeVerb::kDrawPaint));
403 write_paint(writer, paint, kDrawPaint_PaintUsage);
404}
405
406void SkPipeCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
407 const SkPaint& paint) {
408 SkPipeWriter writer(this);
409 writer.write32(pack_verb(SkPipeVerb::kDrawPoints, mode));
410 writer.write32(SkToU32(count));
411 writer.write(pts, count * sizeof(SkPoint));
412 write_paint(writer, paint, kGeometry_PaintUsage | kRespectsStroke_PaintUsage);
413}
414
415void SkPipeCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
416 SkPipeWriter writer(this);
417 writer.write32(pack_verb(SkPipeVerb::kDrawRect));
418 writer.write(&rect, sizeof(SkRect));
419 write_paint(writer, paint, kGeometry_PaintUsage);
420}
421
422void SkPipeCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
423 SkPipeWriter writer(this);
424 writer.write32(pack_verb(SkPipeVerb::kDrawOval));
425 writer.write(&rect, sizeof(SkRect));
426 write_paint(writer, paint, kGeometry_PaintUsage);
427}
428
429void SkPipeCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
430 SkPipeWriter writer(this);
431 writer.write32(pack_verb(SkPipeVerb::kDrawRRect));
432 write_rrect(&writer, rrect);
433 write_paint(writer, paint, kGeometry_PaintUsage);
434}
435
436void SkPipeCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
437 SkPipeWriter writer(this);
438 writer.write32(pack_verb(SkPipeVerb::kDrawDRRect));
439 write_rrect(&writer, outer);
440 write_rrect(&writer, inner);
441 write_paint(writer, paint, kGeometry_PaintUsage);
442}
443
444void SkPipeCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
445 SkPipeWriter writer(this);
446 writer.write32(pack_verb(SkPipeVerb::kDrawPath));
447 writer.writePath(path);
448 write_paint(writer, paint, kGeometry_PaintUsage);
449}
450
Mike Reed7c9c9e42018-01-03 09:23:34 -0500451void SkPipeCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
452 SkPipeWriter writer(this);
453 writer.write32(pack_verb(SkPipeVerb::kDrawShadowRec));
454 writer.writePath(path);
455 writer.write(&rec, sizeof(rec));
456}
457
reed54dc4872016-09-13 08:09:45 -0700458///////////////////////////////////////////////////////////////////////////////////////////////////
459
460static sk_sp<SkImage> make_from_bitmap(const SkBitmap& bitmap) {
461 // If we just "make" an image, it will force a CPU copy (if its mutable), only to have
462 // us then either find it in our cache, or compress and send it.
463 //
464 // Better could be to look it up in our cache first, and only create/compress it if we have to.
465 //
466 // But for now, just do the dumb thing...
467 return SkImage::MakeFromBitmap(bitmap);
468}
469
470void SkPipeCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
471 const SkPaint* paint) {
472 sk_sp<SkImage> image = make_from_bitmap(bitmap);
473 if (image) {
474 this->onDrawImage(image.get(), x, y, paint);
475 }
476}
477
478void SkPipeCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
479 const SkPaint* paint, SrcRectConstraint constraint) {
480 sk_sp<SkImage> image = make_from_bitmap(bitmap);
481 if (image) {
482 this->onDrawImageRect(image.get(), src, dst, paint, constraint);
483 }
484}
485
486void SkPipeCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
487 const SkRect& dst, const SkPaint* paint) {
488 sk_sp<SkImage> image = make_from_bitmap(bitmap);
489 if (image) {
490 this->onDrawImageNine(image.get(), center, dst, paint);
491 }
492}
493
494void SkPipeCanvas::onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice,
495 const SkRect& dst, const SkPaint* paint) {
496 sk_sp<SkImage> image = make_from_bitmap(bitmap);
497 if (image) {
498 this->onDrawImageLattice(image.get(), lattice, dst, paint);
499 }
500}
501
502///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinb3384842017-03-27 14:28:07 -0400503
reed54dc4872016-09-13 08:09:45 -0700504void SkPipeCanvas::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
505 const SkPaint* paint) {
506 unsigned extra = 0;
507 if (paint) {
508 extra |= kHasPaint_DrawImageMask;
509 }
510 SkPipeWriter writer(this);
511 writer.write32(pack_verb(SkPipeVerb::kDrawImage, extra));
512 writer.writeImage(image);
513 writer.writeScalar(left);
514 writer.writeScalar(top);
515 if (paint) {
516 write_paint(writer, *paint, kImage_PaintUsage);
517 }
518}
519
520void SkPipeCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
521 const SkPaint* paint, SrcRectConstraint constraint) {
522 SkASSERT(0 == ((unsigned)constraint & ~1));
523 unsigned extra = (unsigned)constraint;
524 if (paint) {
525 extra |= kHasPaint_DrawImageRectMask;
526 }
527 if (src) {
528 extra |= kHasSrcRect_DrawImageRectMask;
529 }
530
531 SkPipeWriter writer(this);
532 writer.write32(pack_verb(SkPipeVerb::kDrawImageRect, extra));
533 writer.writeImage(image);
534 if (src) {
535 writer.write(src, sizeof(*src));
536 }
537 writer.write(&dst, sizeof(dst));
538 if (paint) {
539 write_paint(writer, *paint, kImage_PaintUsage);
540 }
541}
542
543void SkPipeCanvas::onDrawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst,
544 const SkPaint* paint) {
545 unsigned extra = 0;
546 if (paint) {
547 extra |= kHasPaint_DrawImageNineMask;
548 }
549 SkPipeWriter writer(this);
550 writer.write32(pack_verb(SkPipeVerb::kDrawImageNine, extra));
551 writer.writeImage(image);
552 writer.write(&center, sizeof(center));
553 writer.write(&dst, sizeof(dst));
554 if (paint) {
555 write_paint(writer, *paint, kImage_PaintUsage);
556 }
557}
558
559void SkPipeCanvas::onDrawImageLattice(const SkImage* image, const Lattice& lattice,
560 const SkRect& dst, const SkPaint* paint) {
561 unsigned extra = 0;
562 if (paint) {
563 extra |= kHasPaint_DrawImageLatticeMask;
564 }
reed54dc4872016-09-13 08:09:45 -0700565 SkPipeWriter writer(this);
566 writer.write32(pack_verb(SkPipeVerb::kDrawImageLattice, extra));
567 writer.writeImage(image);
Mike Reed7c9c9e42018-01-03 09:23:34 -0500568 SkCanvasPriv::WriteLattice(writer, lattice);
reed54dc4872016-09-13 08:09:45 -0700569 writer.write(&dst, sizeof(dst));
570 if (paint) {
571 write_paint(writer, *paint, kImage_PaintUsage);
572 }
573}
574
575///////////////////////////////////////////////////////////////////////////////////////////////////
576
577void SkPipeCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
578 const SkPaint& paint) {
579 SkASSERT(byteLength);
580
581 bool compact = fits_in(byteLength, 24);
582
583 SkPipeWriter writer(this);
584 writer.write32(pack_verb(SkPipeVerb::kDrawText, compact ? (unsigned)byteLength : 0));
585 if (!compact) {
586 writer.write32(SkToU32(byteLength));
587 }
588 write_pad(&writer, text, byteLength);
589 writer.writeScalar(x);
590 writer.writeScalar(y);
591 write_paint(writer, paint, kText_PaintUsage);
592}
593
594void SkPipeCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
595 const SkPaint& paint) {
596 SkASSERT(byteLength);
Mike Kleinb3384842017-03-27 14:28:07 -0400597
reed54dc4872016-09-13 08:09:45 -0700598 bool compact = fits_in(byteLength, 24);
Mike Kleinb3384842017-03-27 14:28:07 -0400599
reed54dc4872016-09-13 08:09:45 -0700600 SkPipeWriter writer(this);
601 writer.write32(pack_verb(SkPipeVerb::kDrawPosText, compact ? (unsigned)byteLength : 0));
602 if (!compact) {
603 writer.write32(SkToU32(byteLength));
604 }
605 write_pad(&writer, text, byteLength);
606 writer.writePointArray(pos, paint.countText(text, byteLength));
607 write_paint(writer, paint, kText_PaintUsage);
608}
609
610void SkPipeCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
611 SkScalar constY, const SkPaint& paint) {
612 SkASSERT(byteLength);
Mike Kleinb3384842017-03-27 14:28:07 -0400613
reed54dc4872016-09-13 08:09:45 -0700614 bool compact = fits_in(byteLength, 24);
Mike Kleinb3384842017-03-27 14:28:07 -0400615
reed54dc4872016-09-13 08:09:45 -0700616 SkPipeWriter writer(this);
617 writer.write32(pack_verb(SkPipeVerb::kDrawPosTextH, compact ? (unsigned)byteLength : 0));
618 if (!compact) {
619 writer.write32(SkToU32(byteLength));
620 }
621 write_pad(&writer, text, byteLength);
622 writer.writeScalarArray(xpos, paint.countText(text, byteLength));
623 writer.writeScalar(constY);
624 write_paint(writer, paint, kText_PaintUsage);
625}
626
627void SkPipeCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
628 const SkMatrix* matrix, const SkPaint& paint) {
629 SkASSERT(byteLength > 0);
630
631 unsigned extra = 0;
632 if (byteLength <= kTextLength_DrawTextOnPathMask) {
633 extra |= byteLength;
634 } // else we will write the length after the packedverb
635 SkMatrix::TypeMask tm = matrix ? matrix->getType() : SkMatrix::kIdentity_Mask;
636 extra |= (unsigned)tm << kMatrixType_DrawTextOnPathShift;
637
638 SkPipeWriter writer(this);
639 writer.write32(pack_verb(SkPipeVerb::kDrawTextOnPath, extra));
640 if (byteLength > kTextLength_DrawTextOnPathMask) {
641 writer.write32(byteLength);
642 }
643 write_pad(&writer, text, byteLength);
644 writer.writePath(path);
645 if (matrix) {
646 write_sparse_matrix(&writer, *matrix);
647 }
648 write_paint(writer, paint, kText_PaintUsage);
649}
650
651void SkPipeCanvas::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
652 const SkRect* cull, const SkPaint& paint) {
653 SkASSERT(byteLength);
Mike Kleinb3384842017-03-27 14:28:07 -0400654
reed54dc4872016-09-13 08:09:45 -0700655 bool compact = fits_in(byteLength, 23);
656 unsigned extra = compact ? (byteLength << 1) : 0;
657 if (cull) {
658 extra |= 1;
659 }
Mike Kleinb3384842017-03-27 14:28:07 -0400660
reed54dc4872016-09-13 08:09:45 -0700661 SkPipeWriter writer(this);
662 writer.write32(pack_verb(SkPipeVerb::kDrawTextRSXform, extra));
663 if (!compact) {
664 writer.write32(SkToU32(byteLength));
665 }
666 write_pad(&writer, text, byteLength);
667
668 int count = paint.countText(text, byteLength);
669 writer.write32(count); // maybe we can/should store this in extra as well?
670 writer.write(xform, count * sizeof(SkRSXform));
671 if (cull) {
672 writer.writeRect(*cull);
673 }
674 write_paint(writer, paint, kText_PaintUsage);
675}
676
677void SkPipeCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
678 const SkPaint &paint) {
679 SkPipeWriter writer(this);
680 writer.write32(pack_verb(SkPipeVerb::kDrawTextBlob, 0));
681 blob->flatten(writer);
682 writer.writeScalar(x);
683 writer.writeScalar(y);
684 write_paint(writer, paint, kTextBlob_PaintUsage);
685}
686
687void SkPipeCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
688 const SkPaint* paint) {
689 unsigned extra = fDeduper->findOrDefinePicture(const_cast<SkPicture*>(picture));
690 if (matrix) {
691 extra |= kHasMatrix_DrawPictureExtra;
692 }
693 if (paint) {
694 extra |= kHasPaint_DrawPictureExtra;
695 }
696 SkPipeWriter writer(this);
697 writer.write32(pack_verb(SkPipeVerb::kDrawPicture, extra));
698 if (matrix) {
699 writer.writeMatrix(*matrix);
700 }
701 if (paint) {
702 write_paint(writer, *paint, kSaveLayer_PaintUsage);
703 }
704}
705
Brian Osman37886ce2018-03-09 13:40:31 -0500706void SkPipeCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
707 // TODO: Is there a better solution than just exploding the drawable?
708 drawable->draw(this, matrix);
709}
710
reed54dc4872016-09-13 08:09:45 -0700711void SkPipeCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
712 size_t size = region.writeToMemory(nullptr);
713 unsigned extra = 0;
714 if (fits_in(size, 24)) {
715 extra = SkToUInt(size);
716 }
717
718 SkPipeWriter writer(this);
719 writer.write32(pack_verb(SkPipeVerb::kDrawRegion, extra));
720 if (0 == extra) {
721 writer.write32(size);
722 }
723 SkAutoSMalloc<2048> storage(size);
724 region.writeToMemory(storage.get());
725 write_pad(&writer, storage.get(), size);
726 write_paint(writer, paint, kGeometry_PaintUsage);
727}
728
Mike Reedfed9cfd2017-03-17 12:09:04 -0400729void SkPipeCanvas::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode bmode,
730 const SkPaint& paint) {
731 unsigned extra = static_cast<unsigned>(bmode);
reed54dc4872016-09-13 08:09:45 -0700732
733 SkPipeWriter writer(this);
734 writer.write32(pack_verb(SkPipeVerb::kDrawVertices, extra));
Mike Reedfed9cfd2017-03-17 12:09:04 -0400735 // TODO: dedup vertices?
736 writer.writeDataAsByteArray(vertices->encode().get());
reed54dc4872016-09-13 08:09:45 -0700737 write_paint(writer, paint, kVertices_PaintUsage);
738}
739
740void SkPipeCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reedfaba3712016-11-03 14:45:31 -0400741 const SkPoint texCoords[4], SkBlendMode bmode,
reed54dc4872016-09-13 08:09:45 -0700742 const SkPaint& paint) {
743 SkPipeWriter writer(this);
744 unsigned extra = 0;
Mike Reed7d954ad2016-10-28 15:42:34 -0400745 SkASSERT(0 == ((int)bmode & ~kModeEnum_DrawPatchExtraMask));
746 extra = (unsigned)bmode;
reed54dc4872016-09-13 08:09:45 -0700747 if (colors) {
748 extra |= kHasColors_DrawPatchExtraMask;
749 }
750 if (texCoords) {
751 extra |= kHasTexture_DrawPatchExtraMask;
752 }
753 writer.write32(pack_verb(SkPipeVerb::kDrawPatch, extra));
754 writer.write(cubics, sizeof(SkPoint) * 12);
755 if (colors) {
756 writer.write(colors, sizeof(SkColor) * 4);
757 }
758 if (texCoords) {
759 writer.write(texCoords, sizeof(SkPoint) * 4);
760 }
reed54dc4872016-09-13 08:09:45 -0700761 write_paint(writer, paint, kGeometry_PaintUsage);
762}
763
764void SkPipeCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* data) {
765 const size_t len = strlen(key) + 1; // must write the trailing 0
766 bool compact = fits_in(len, 23);
767 uint32_t extra = compact ? (unsigned)len : 0;
768 extra <<= 1; // make room for has_data_sentinel
769 if (data) {
770 extra |= 1;
771 }
772
773 fStream->write32(pack_verb(SkPipeVerb::kDrawAnnotation, extra));
774 fStream->write(&rect, sizeof(SkRect));
775 if (!compact) {
776 fStream->write32(SkToU32(len));
777 }
778 write_pad(fStream, key, len);
779 if (data) {
780 fStream->write32(SkToU32(data->size()));
781 write_pad(fStream, data->data(), data->size());
782 }
783}
784
785///////////////////////////////////////////////////////////////////////////////////////////////////
786
Mike Reedc0cec872017-12-14 10:45:27 -0500787static sk_sp<SkData> encode(SkImage* img, SkSerialImageProc proc, void* ctx) {
788 if (proc) {
789 if (auto data = proc(img, ctx)) {
790 return data;
reed54dc4872016-09-13 08:09:45 -0700791 }
reed54dc4872016-09-13 08:09:45 -0700792 }
Mike Reedc0cec872017-12-14 10:45:27 -0500793 return img->encodeToData();
Mike Reed3ac64b42016-10-18 19:34:08 -0400794}
795
reed54dc4872016-09-13 08:09:45 -0700796static bool show_deduper_traffic = false;
797
798int SkPipeDeduper::findOrDefineImage(SkImage* image) {
799 int index = fImages.find(image->uniqueID());
800 SkASSERT(index >= 0);
801 if (index) {
802 if (show_deduper_traffic) {
803 SkDebugf(" reuseImage(%d)\n", index - 1);
804 }
805 return index;
806 }
807
Mike Reedc0cec872017-12-14 10:45:27 -0500808 sk_sp<SkData> data = encode(image, fProcs.fImageProc, fProcs.fImageCtx);
reed54dc4872016-09-13 08:09:45 -0700809 if (data) {
810 index = fImages.add(image->uniqueID());
811 SkASSERT(index > 0);
812 SkASSERT(fits_in(index, 24));
813 fStream->write32(pack_verb(SkPipeVerb::kDefineImage, index));
814
815 uint32_t len = SkToU32(data->size());
816 fStream->write32(SkAlign4(len));
817 write_pad(fStream, data->data(), len);
818
819 if (show_deduper_traffic) {
820 int size = image->width() * image->height() << 2;
821 SkDebugf(" defineImage(%d) %d -> %d\n", index - 1, size, len);
822 }
823 return index;
824 }
825 SkDebugf("+++ failed to encode image [%d %d]\n", image->width(), image->height());
826 return 0; // failed to encode
827}
828
829int SkPipeDeduper::findOrDefinePicture(SkPicture* picture) {
830 int index = fPictures.find(picture->uniqueID());
831 SkASSERT(index >= 0);
832 if (index) {
833 if (show_deduper_traffic) {
834 SkDebugf(" reusePicture(%d)\n", index - 1);
835 }
836 return index;
837 }
838
839 size_t prevWritten = fStream->bytesWritten();
840 unsigned extra = 0; // 0 means we're defining a new picture, non-zero means undef_index + 1
841 fStream->write32(pack_verb(SkPipeVerb::kDefinePicture, extra));
842 const SkRect cull = picture->cullRect();
843 fStream->write(&cull, sizeof(cull));
844 picture->playback(fPipeCanvas);
845 // call fPictures.add *after* we're written the picture, so that any nested pictures will have
846 // already been defined, and we get the "last" index value.
847 index = fPictures.add(picture->uniqueID());
848 ASSERT_FITS_IN(index, kObjectDefinitionBits);
849 fStream->write32(pack_verb(SkPipeVerb::kEndPicture, index));
850
Mike Kleinb3384842017-03-27 14:28:07 -0400851 if (show_deduper_traffic) {
852 SkDebugf(" definePicture(%d) %d\n",
853 index - 1, SkToU32(fStream->bytesWritten() - prevWritten));
854 }
reed54dc4872016-09-13 08:09:45 -0700855 return index;
856}
857
Mike Reedc0cec872017-12-14 10:45:27 -0500858static sk_sp<SkData> encode(const SkSerialProcs& procs, SkTypeface* tf) {
859 if (procs.fTypefaceProc) {
860 auto data = procs.fTypefaceProc(tf, procs.fTypefaceCtx);
861 if (data) {
862 return data;
863 }
864 }
reed54dc4872016-09-13 08:09:45 -0700865 SkDynamicMemoryWStream stream;
866 tf->serialize(&stream);
867 return sk_sp<SkData>(stream.detachAsData());
868}
869
870int SkPipeDeduper::findOrDefineTypeface(SkTypeface* typeface) {
871 if (!typeface) {
872 return 0; // default
873 }
874
875 int index = fTypefaces.find(typeface->uniqueID());
876 SkASSERT(index >= 0);
877 if (index) {
878 if (show_deduper_traffic) {
879 SkDebugf(" reuseTypeface(%d)\n", index - 1);
880 }
881 return index;
882 }
883
Mike Reedc0cec872017-12-14 10:45:27 -0500884 sk_sp<SkData> data = encode(fProcs, typeface);
reed54dc4872016-09-13 08:09:45 -0700885 if (data) {
886 index = fTypefaces.add(typeface->uniqueID());
887 SkASSERT(index > 0);
888 SkASSERT(fits_in(index, 24));
889 fStream->write32(pack_verb(SkPipeVerb::kDefineTypeface, index));
890
891 uint32_t len = SkToU32(data->size());
892 fStream->write32(SkAlign4(len));
893 write_pad(fStream, data->data(), len);
894
895 if (show_deduper_traffic) {
896 SkDebugf(" defineTypeface(%d) %d\n", index - 1, len);
897 }
898 return index;
899 }
900 SkDebugf("+++ failed to encode typeface %d\n", typeface->uniqueID());
901 return 0; // failed to encode
902}
903
904int SkPipeDeduper::findOrDefineFactory(SkFlattenable* flattenable) {
905 if (!flattenable) {
906 return 0;
907 }
908
909 int index = fFactories.find(flattenable->getFactory());
910 SkASSERT(index >= 0);
911 if (index) {
912 if (show_deduper_traffic) {
913 SkDebugf(" reuseFactory(%d)\n", index - 1);
914 }
915 return index;
916 }
917
918 index = fFactories.add(flattenable->getFactory());
919 ASSERT_FITS_IN(index, kIndex_DefineFactoryExtraBits);
920 const char* name = flattenable->getTypeName();
921 size_t len = strlen(name);
922 ASSERT_FITS_IN(len, kNameLength_DefineFactoryExtraBits);
923 unsigned extra = (index << kNameLength_DefineFactoryExtraBits) | len;
924 size_t prevWritten = fStream->bytesWritten();
925 fStream->write32(pack_verb(SkPipeVerb::kDefineFactory, extra));
926 write_pad(fStream, name, len + 1);
927 if (false) {
928 SkDebugf(" defineFactory(%d) %d %s\n",
929 index - 1, SkToU32(fStream->bytesWritten() - prevWritten), name);
930 }
931 return index;
932}
933
934///////////////////////////////////////////////////////////////////////////////////////////////////
935#include "SkPipe.h"
936
937class SkPipeSerializer::Impl {
938public:
939 SkPipeDeduper fDeduper;
940 std::unique_ptr<SkPipeCanvas> fCanvas;
941};
942
943SkPipeSerializer::SkPipeSerializer() : fImpl(new Impl) {}
944
945SkPipeSerializer::~SkPipeSerializer() {
946 if (fImpl->fCanvas) {
947 this->endWrite();
948 }
949}
950
reed54dc4872016-09-13 08:09:45 -0700951void SkPipeSerializer::resetCache() {
952 fImpl->fDeduper.resetCaches();
953}
954
reed262052c2016-09-15 14:24:53 -0700955sk_sp<SkData> SkPipeSerializer::writeImage(SkImage* image) {
956 SkDynamicMemoryWStream stream;
957 this->writeImage(image, &stream);
958 return stream.detachAsData();
reed54dc4872016-09-13 08:09:45 -0700959}
960
reed262052c2016-09-15 14:24:53 -0700961sk_sp<SkData> SkPipeSerializer::writePicture(SkPicture* picture) {
962 SkDynamicMemoryWStream stream;
963 this->writePicture(picture, &stream);
964 return stream.detachAsData();
965}
966
967void SkPipeSerializer::writePicture(SkPicture* picture, SkWStream* stream) {
968 int index = fImpl->fDeduper.findPicture(picture);
969 if (0 == index) {
970 // Try to define the picture
971 this->beginWrite(picture->cullRect(), stream);
972 index = fImpl->fDeduper.findOrDefinePicture(picture);
973 this->endWrite();
974 }
975 stream->write32(pack_verb(SkPipeVerb::kWritePicture, index));
976}
977
978void SkPipeSerializer::writeImage(SkImage* image, SkWStream* stream) {
reed7e3ba9f2016-09-13 17:25:19 -0700979 int index = fImpl->fDeduper.findImage(image);
980 if (0 == index) {
981 // Try to define the image
982 fImpl->fDeduper.setStream(stream);
983 index = fImpl->fDeduper.findOrDefineImage(image);
984 }
985 stream->write32(pack_verb(SkPipeVerb::kWriteImage, index));
reed54dc4872016-09-13 08:09:45 -0700986}
987
988SkCanvas* SkPipeSerializer::beginWrite(const SkRect& cull, SkWStream* stream) {
989 SkASSERT(nullptr == fImpl->fCanvas);
990 fImpl->fCanvas.reset(new SkPipeCanvas(cull, &fImpl->fDeduper, stream));
991 fImpl->fDeduper.setStream(stream);
992 fImpl->fDeduper.setCanvas(fImpl->fCanvas.get());
993 return fImpl->fCanvas.get();
994}
995
996void SkPipeSerializer::endWrite() {
997 fImpl->fCanvas->restoreToCount(1);
998 fImpl->fCanvas.reset(nullptr);
reed7e3ba9f2016-09-13 17:25:19 -0700999 fImpl->fDeduper.setCanvas(nullptr);
reed54dc4872016-09-13 08:09:45 -07001000}