Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: src/pipe/SkPipeCanvas.cpp

Issue 2201323003: add pipecanvas (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: add test for writeImage Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "SkPathEffect.h"
9 #include "SkColorFilter.h"
10 #include "SkDrawLooper.h"
11 #include "SkImageFilter.h"
12 #include "SkMaskFilter.h"
13 #include "SkPipeCanvas.h"
14 #include "SkPipeFormat.h"
15 #include "SkRasterizer.h"
16 #include "SkRSXform.h"
17 #include "SkShader.h"
18 #include "SkStream.h"
19 #include "SkTextBlob.h"
20 #include "SkTypeface.h"
21
22 template <typename T> void write_rrect(T* writer, const SkRRect& rrect) {
23 char tmp[SkRRect::kSizeInMemory];
24 rrect.writeToMemory(tmp);
25 writer->write(tmp, SkRRect::kSizeInMemory);
26 }
27
28 template <typename T> void write_pad(T* writer, const void* buffer, size_t len) {
29 writer->write(buffer, len & ~3);
30 if (len & 3) {
31 const char* src = (const char*)buffer + (len & ~3);
32 len &= 3;
33 uint32_t tmp = 0;
34 memcpy(&tmp, src, len);
35 writer->write(&tmp, 4);
36 }
37 }
38
39 //////////////////////////////////////////////////////////////////////////////// ///////////////////
40
41 static uint16_t compute_nondef(const SkPaint& paint, PaintUsage usage) {
42 // kRespectsStroke_PaintUsage is only valid if other bits are also set
43 SkASSERT(0 != (usage & ~kRespectsStroke_PaintUsage));
44
45 const SkScalar kTextSize_Default = 12;
46 const SkScalar kTextScaleX_Default = 1;
47 const SkScalar kTextSkewX_Default = 0;
48 const SkScalar kStrokeWidth_Default = 0;
49 const SkScalar kStrokeMiter_Default = 4;
50 const SkColor kColor_Default = SK_ColorBLACK;
51
52 unsigned bits = (paint.getColor() != kColor_Default) ? kColor_NonDef : 0;
53
54 if (usage & kText_PaintUsage) {
55 bits |= (paint.getTextSize() != kTextSize_Default ? kTextSize_NonD ef : 0);
56 bits |= (paint.getTextScaleX() != kTextScaleX_Default ? kTextScaleX_No nDef : 0);
57 bits |= (paint.getTextSkewX() != kTextSkewX_Default ? kTextSkewX_Non Def : 0);
58 bits |= (paint.getTypeface() ? kTypeface_NonD ef : 0);
59 }
60
61 // TODO: kImage_PaintUsage only needs the shader/maskfilter IF its colortype is kAlpha_8
62
63 if (usage & (kVertices_PaintUsage | kDrawPaint_PaintUsage | kImage_PaintUsag e |
64 kText_PaintUsage | kGeometry_PaintUsage | kTextBlob_PaintUsage) ) {
65 bits |= (paint.getShader() ? kShader_NonDef : 0);
66 }
67
68 if (usage & (kText_PaintUsage | kGeometry_PaintUsage | kTextBlob_PaintUsage) ) {
69 bits |= (paint.getPathEffect() ? kPathEffect_NonDef : 0);
70 bits |= (paint.getRasterizer() ? kRasterizer_NonDef : 0);
71
72 if (paint.getStyle() != SkPaint::kFill_Style || (usage & kRespectsStroke _PaintUsage)) {
73 bits |= (paint.getStrokeWidth() != kStrokeWidth_Default ? kStrokeWid th_NonDef : 0);
74 bits |= (paint.getStrokeMiter() != kStrokeMiter_Default ? kStrokeMit er_NonDef : 0);
75 }
76 }
77
78 if (usage &
79 (kText_PaintUsage | kGeometry_PaintUsage | kImage_PaintUsage | kTextBlob _PaintUsage))
80 {
81 bits |= (paint.getMaskFilter() ? kMaskFilter_NonDef : 0);
82 }
83
84 bits |= (paint.getXfermode() ? kXfermode_NonDef : 0);
85 bits |= (paint.getColorFilter() ? kColorFilter_NonDef : 0);
86 bits |= (paint.getImageFilter() ? kImageFilter_NonDef : 0);
87 bits |= (paint.getDrawLooper() ? kDrawLooper_NonDef : 0);
88
89 return SkToU16(bits);
90 }
91
92 static uint32_t pack_paint_flags(unsigned flags, unsigned hint, unsigned align,
93 unsigned filter, unsigned style, unsigned caps, unsigned joins,
94 unsigned encoding) {
95 SkASSERT(kFlags_BPF + kHint_BPF + kAlign_BPF + kFilter_BPF <= 32);
96
97 ASSERT_FITS_IN(flags, kFlags_BPF);
98 ASSERT_FITS_IN(filter, kFilter_BPF);
99 ASSERT_FITS_IN(style, kStyle_BPF);
100 ASSERT_FITS_IN(caps, kCaps_BPF);
101 ASSERT_FITS_IN(joins, kJoins_BPF);
102 ASSERT_FITS_IN(hint, kHint_BPF);
103 ASSERT_FITS_IN(align, kAlign_BPF);
104 ASSERT_FITS_IN(encoding, kEncoding_BPF);
105
106 // left-align the fields of "known" size, and right-align the last (flatFlag s) so it can easly
107 // add more bits in the future.
108
109 uint32_t packed = 0;
110 int shift = 32;
111
112 shift -= kFlags_BPF; packed |= (flags << shift);
113 shift -= kFilter_BPF; packed |= (filter << shift);
114 shift -= kStyle_BPF; packed |= (style << shift);
115 // these are only needed for stroking (geometry or text)
116 shift -= kCaps_BPF; packed |= (caps << shift);
117 shift -= kJoins_BPF; packed |= (joins << shift);
118 // these are only needed for text
119 shift -= kHint_BPF; packed |= (hint << shift);
120 shift -= kAlign_BPF; packed |= (align << shift);
121 shift -= kEncoding_BPF; packed |= (encoding << shift);
122
123 return packed;
124 }
125
126 #define CHECK_WRITE_SCALAR(writer, nondef, paint, Field) \
127 do { if (nondef & (k##Field##_NonDef)) { \
128 writer.writeScalar(paint.get##Field()); \
129 }} while (0)
130
131 #define CHECK_WRITE_FLATTENABLE(writer, nondef, paint, Field) \
132 do { if (nondef & (k##Field##_NonDef)) { \
133 SkFlattenable* f = paint.get##Field(); \
134 SkASSERT(f != nullptr); \
135 writer.writeFlattenable(f); \
136 } } while (0)
137
138 /*
139 * Header:
140 * paint flags : 32
141 * non_def bits : 16
142 * xfermode enum : 8
143 * pad zeros : 8
144 */
145 static void write_paint(SkWriteBuffer& writer, const SkPaint& paint, unsigned us age) {
146 uint32_t packedFlags = pack_paint_flags(paint.getFlags(), paint.getHinting() ,
147 paint.getTextAlign(), paint.getFilte rQuality(),
148 paint.getStyle(), paint.getStrokeCap (),
149 paint.getStrokeJoin(), paint.getText Encoding());
150 writer.write32(packedFlags);
151
152 unsigned nondef = compute_nondef(paint, (PaintUsage)usage);
153 SkXfermode::Mode mode;
154 if (SkXfermode::AsMode(paint.getXfermode(), &mode)) {
155 nondef &= ~kXfermode_NonDef; // don't need to store a pointer since w e have an enum
156 } else {
157 SkASSERT(nondef & kXfermode_NonDef);
158 mode = (SkXfermode::Mode)0;
159 }
160 const uint8_t pad = 0;
161 writer.write32((nondef << 16) | ((unsigned)mode << 8) | pad);
162
163 CHECK_WRITE_SCALAR(writer, nondef, paint, TextSize);
164 CHECK_WRITE_SCALAR(writer, nondef, paint, TextScaleX);
165 CHECK_WRITE_SCALAR(writer, nondef, paint, TextSkewX);
166 CHECK_WRITE_SCALAR(writer, nondef, paint, StrokeWidth);
167 CHECK_WRITE_SCALAR(writer, nondef, paint, StrokeMiter);
168
169 if (nondef & kColor_NonDef) {
170 writer.write32(paint.getColor());
171 }
172 if (nondef & kTypeface_NonDef) {
173 // TODO: explore idea of writing bits indicating "use the prev (or prev N) face"
174 // e.g. 1-N bits is an index into a ring buffer of typefaces
175 SkTypeface* tf = paint.getTypeface();
176 SkASSERT(tf);
177 writer.writeTypeface(tf);
178 }
179
180 CHECK_WRITE_FLATTENABLE(writer, nondef, paint, PathEffect);
181 CHECK_WRITE_FLATTENABLE(writer, nondef, paint, Shader);
182 CHECK_WRITE_FLATTENABLE(writer, nondef, paint, Xfermode);
183 CHECK_WRITE_FLATTENABLE(writer, nondef, paint, MaskFilter);
184 CHECK_WRITE_FLATTENABLE(writer, nondef, paint, ColorFilter);
185 CHECK_WRITE_FLATTENABLE(writer, nondef, paint, Rasterizer);
186 CHECK_WRITE_FLATTENABLE(writer, nondef, paint, ImageFilter);
187 CHECK_WRITE_FLATTENABLE(writer, nondef, paint, DrawLooper);
188 }
189
190 class SkPipeWriter : public SkBinaryWriteBuffer {
191 enum {
192 N = 1024/4,
193 };
194 uint32_t fStorage[N];
195 SkWStream* fStream;
196
197 public:
198 SkPipeWriter(SkWStream* stream, SkDeduper* deduper)
199 : SkBinaryWriteBuffer(fStorage, sizeof(fStorage))
200 , fStream(stream)
201 {
202 this->setDeduper(deduper);
203 }
204
205 SkPipeWriter(SkPipeCanvas* pc) : SkPipeWriter(pc->fStream, pc->fDeduper) {}
206
207 ~SkPipeWriter() override {
208 SkASSERT(SkIsAlign4(fStream->bytesWritten()));
209 this->writeToStream(fStream);
210 }
211
212 void writePaint(const SkPaint& paint) override {
213 write_paint(*this, paint, kUnknown_PaintUsage);
214 }
215 };
216
217 //////////////////////////////////////////////////////////////////////////////// ///////////////////
218
219 SkPipeCanvas::SkPipeCanvas(const SkRect& cull, SkPipeDeduper* deduper, SkWStream * stream)
220 : INHERITED(SkScalarCeilToInt(cull.width()), SkScalarCeilToInt(cull.height() ))
221 , fDeduper(deduper)
222 , fStream(stream)
223 {}
224
225 SkPipeCanvas::~SkPipeCanvas() {}
226
227 void SkPipeCanvas::willSave() {
228 fStream->write32(pack_verb(SkPipeVerb::kSave));
229 this->INHERITED::willSave();
230 }
231
232 SkCanvas::SaveLayerStrategy SkPipeCanvas::getSaveLayerStrategy(const SaveLayerRe c& rec) {
233 SkPipeWriter writer(this);
234 uint32_t extra = rec.fSaveLayerFlags;
235
236 // remap this wacky flag
237 if (extra & (1 << 31)/*SkCanvas::kDontClipToLayer_PrivateSaveLayerFlag*/) {
238 extra &= ~(1 << 31);
239 extra |= kDontClipToLayer_SaveLayerMask;
240 }
241
242 if (rec.fBounds) {
243 extra |= kHasBounds_SaveLayerMask;
244 }
245 if (rec.fPaint) {
246 extra |= kHasPaint_SaveLayerMask;
247 }
248 if (rec.fBackdrop) {
249 extra |= kHasBackdrop_SaveLayerMask;
250 }
251
252 writer.write32(pack_verb(SkPipeVerb::kSaveLayer, extra));
253 if (rec.fBounds) {
254 writer.writeRect(*rec.fBounds);
255 }
256 if (rec.fPaint) {
257 write_paint(writer, *rec.fPaint, kSaveLayer_PaintUsage);
258 }
259 if (rec.fBackdrop) {
260 writer.writeFlattenable(rec.fBackdrop);
261 }
262 return kNoLayer_SaveLayerStrategy;
263 }
264
265 void SkPipeCanvas::willRestore() {
266 fStream->write32(pack_verb(SkPipeVerb::kRestore));
267 this->INHERITED::willRestore();
268 }
269
270 template <typename T> void write_sparse_matrix(T* writer, const SkMatrix& matrix ) {
271 SkMatrix::TypeMask tm = matrix.getType();
272 SkScalar tmp[9];
273 if (tm & SkMatrix::kPerspective_Mask) {
274 matrix.get9(tmp);
275 writer->write(tmp, 9 * sizeof(SkScalar));
276 } else if (tm & SkMatrix::kAffine_Mask) {
277 tmp[0] = matrix[SkMatrix::kMScaleX];
278 tmp[1] = matrix[SkMatrix::kMSkewX];
279 tmp[2] = matrix[SkMatrix::kMTransX];
280 tmp[3] = matrix[SkMatrix::kMScaleY];
281 tmp[4] = matrix[SkMatrix::kMSkewY];
282 tmp[5] = matrix[SkMatrix::kMTransY];
283 writer->write(tmp, 6 * sizeof(SkScalar));
284 } else if (tm & SkMatrix::kScale_Mask) {
285 tmp[0] = matrix[SkMatrix::kMScaleX];
286 tmp[1] = matrix[SkMatrix::kMTransX];
287 tmp[2] = matrix[SkMatrix::kMScaleY];
288 tmp[3] = matrix[SkMatrix::kMTransY];
289 writer->write(tmp, 4 * sizeof(SkScalar));
290 } else if (tm & SkMatrix::kTranslate_Mask) {
291 tmp[0] = matrix[SkMatrix::kMTransX];
292 tmp[1] = matrix[SkMatrix::kMTransY];
293 writer->write(tmp, 2 * sizeof(SkScalar));
294 }
295 // else write nothing for Identity
296 }
297
298 static void do_concat(SkWStream* stream, const SkMatrix& matrix, bool isSetMatri x) {
299 unsigned mtype = matrix.getType();
300 SkASSERT(0 == (mtype & ~kTypeMask_ConcatMask));
301 unsigned extra = mtype;
302 if (isSetMatrix) {
303 extra |= kSetMatrix_ConcatMask;
304 }
305 if (mtype || isSetMatrix) {
306 stream->write32(pack_verb(SkPipeVerb::kConcat, extra));
307 write_sparse_matrix(stream, matrix);
308 }
309 }
310
311 void SkPipeCanvas::didConcat(const SkMatrix& matrix) {
312 do_concat(fStream, matrix, false);
313 this->INHERITED::didConcat(matrix);
314 }
315
316 void SkPipeCanvas::didSetMatrix(const SkMatrix& matrix) {
317 do_concat(fStream, matrix, true);
318 this->INHERITED::didSetMatrix(matrix);
319 }
320
321 void SkPipeCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
322 fStream->write32(pack_verb(SkPipeVerb::kClipRect, ((unsigned)op << 1) | edge Style));
323 fStream->write(&rect, 4 * sizeof(SkScalar));
324
325 this->INHERITED::onClipRect(rect, op, edgeStyle);
326 }
327
328 void SkPipeCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeSt yle edgeStyle) {
329 fStream->write32(pack_verb(SkPipeVerb::kClipRRect, ((unsigned)op << 1) | edg eStyle));
330 write_rrect(fStream, rrect);
331
332 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
333 }
334
335 void SkPipeCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
336 SkPipeWriter writer(this);
337 writer.write32(pack_verb(SkPipeVerb::kClipPath, ((unsigned)op << 1) | edgeSt yle));
338 writer.writePath(path);
339
340 this->INHERITED::onClipPath(path, op, edgeStyle);
341 }
342
343 void SkPipeCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
344 SkPipeWriter writer(this);
345 writer.write32(pack_verb(SkPipeVerb::kClipRegion, (unsigned)op << 1));
346 writer.writeRegion(deviceRgn);
347
348 this->INHERITED::onClipRegion(deviceRgn, op);
349 }
350
351 //////////////////////////////////////////////////////////////////////////////// ///////////////////
352
353 void SkPipeCanvas::onDrawArc(const SkRect& bounds, SkScalar startAngle, SkScalar sweepAngle,
354 bool useCenter, const SkPaint& paint) {
355 SkPipeWriter writer(this);
356 writer.write32(pack_verb(SkPipeVerb::kDrawArc, (int)useCenter));
357 writer.writeRect(bounds);
358 writer.writeScalar(startAngle);
359 writer.writeScalar(sweepAngle);
360 write_paint(writer, paint, kGeometry_PaintUsage);
361 }
362
363 void SkPipeCanvas::onDrawAtlas(const SkImage* image, const SkRSXform xform[], co nst SkRect rect[],
364 const SkColor colors[], int count, SkXfermode::Mo de mode,
365 const SkRect* cull, const SkPaint* paint) {
366 unsigned extra = (unsigned)mode;
367 SkASSERT(0 == (extra & ~kMode_DrawAtlasMask));
368 if (colors) {
369 extra |= kHasColors_DrawAtlasMask;
370 }
371 if (cull) {
372 extra |= kHasCull_DrawAtlasMask;
373 }
374 if (paint) {
375 extra |= kHasPaint_DrawAtlasMask;
376 }
377
378 SkPipeWriter writer(this);
379 writer.write32(pack_verb(SkPipeVerb::kDrawAtlas, extra));
380 writer.writeImage(image);
381 writer.write32(count);
382 writer.write(xform, count * sizeof(SkRSXform));
383 writer.write(rect, count * sizeof(SkRect));
384 if (colors) {
385 writer.write(colors, count * sizeof(SkColor));
386 }
387 if (cull) {
388 writer.writeRect(*cull);
389 }
390 if (paint) {
391 write_paint(writer, *paint, kImage_PaintUsage);
392 }
393 }
394
395 void SkPipeCanvas::onDrawPaint(const SkPaint& paint) {
396 SkPipeWriter writer(this);
397 writer.write32(pack_verb(SkPipeVerb::kDrawPaint));
398 write_paint(writer, paint, kDrawPaint_PaintUsage);
399 }
400
401 void SkPipeCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[ ],
402 const SkPaint& paint) {
403 SkPipeWriter writer(this);
404 writer.write32(pack_verb(SkPipeVerb::kDrawPoints, mode));
405 writer.write32(SkToU32(count));
406 writer.write(pts, count * sizeof(SkPoint));
407 write_paint(writer, paint, kGeometry_PaintUsage | kRespectsStroke_PaintUsage );
408 }
409
410 void SkPipeCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
411 SkPipeWriter writer(this);
412 writer.write32(pack_verb(SkPipeVerb::kDrawRect));
413 writer.write(&rect, sizeof(SkRect));
414 write_paint(writer, paint, kGeometry_PaintUsage);
415 }
416
417 void SkPipeCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
418 SkPipeWriter writer(this);
419 writer.write32(pack_verb(SkPipeVerb::kDrawOval));
420 writer.write(&rect, sizeof(SkRect));
421 write_paint(writer, paint, kGeometry_PaintUsage);
422 }
423
424 void SkPipeCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
425 SkPipeWriter writer(this);
426 writer.write32(pack_verb(SkPipeVerb::kDrawRRect));
427 write_rrect(&writer, rrect);
428 write_paint(writer, paint, kGeometry_PaintUsage);
429 }
430
431 void SkPipeCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, cons t SkPaint& paint) {
432 SkPipeWriter writer(this);
433 writer.write32(pack_verb(SkPipeVerb::kDrawDRRect));
434 write_rrect(&writer, outer);
435 write_rrect(&writer, inner);
436 write_paint(writer, paint, kGeometry_PaintUsage);
437 }
438
439 void SkPipeCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
440 SkPipeWriter writer(this);
441 writer.write32(pack_verb(SkPipeVerb::kDrawPath));
442 writer.writePath(path);
443 write_paint(writer, paint, kGeometry_PaintUsage);
444 }
445
446 //////////////////////////////////////////////////////////////////////////////// ///////////////////
447
448 static sk_sp<SkImage> make_from_bitmap(const SkBitmap& bitmap) {
449 // If we just "make" an image, it will force a CPU copy (if its mutable), on ly to have
450 // us then either find it in our cache, or compress and send it.
451 //
452 // Better could be to look it up in our cache first, and only create/compres s it if we have to.
453 //
454 // But for now, just do the dumb thing...
455 return SkImage::MakeFromBitmap(bitmap);
456 }
457
458 void SkPipeCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
459 const SkPaint* paint) {
460 sk_sp<SkImage> image = make_from_bitmap(bitmap);
461 if (image) {
462 this->onDrawImage(image.get(), x, y, paint);
463 }
464 }
465
466 void SkPipeCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, c onst SkRect& dst,
467 const SkPaint* paint, SrcRectConstraint cons traint) {
468 sk_sp<SkImage> image = make_from_bitmap(bitmap);
469 if (image) {
470 this->onDrawImageRect(image.get(), src, dst, paint, constraint);
471 }
472 }
473
474 void SkPipeCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& cente r,
475 const SkRect& dst, const SkPaint* paint) {
476 sk_sp<SkImage> image = make_from_bitmap(bitmap);
477 if (image) {
478 this->onDrawImageNine(image.get(), center, dst, paint);
479 }
480 }
481
482 void SkPipeCanvas::onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& la ttice,
483 const SkRect& dst, const SkPaint* paint) {
484 sk_sp<SkImage> image = make_from_bitmap(bitmap);
485 if (image) {
486 this->onDrawImageLattice(image.get(), lattice, dst, paint);
487 }
488 }
489
490 //////////////////////////////////////////////////////////////////////////////// ///////////////////
491
492 void SkPipeCanvas::onDrawImage(const SkImage* image, SkScalar left, SkScalar top ,
493 const SkPaint* paint) {
494 unsigned extra = 0;
495 if (paint) {
496 extra |= kHasPaint_DrawImageMask;
497 }
498 SkPipeWriter writer(this);
499 writer.write32(pack_verb(SkPipeVerb::kDrawImage, extra));
500 writer.writeImage(image);
501 writer.writeScalar(left);
502 writer.writeScalar(top);
503 if (paint) {
504 write_paint(writer, *paint, kImage_PaintUsage);
505 }
506 }
507
508 void SkPipeCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, cons t SkRect& dst,
509 const SkPaint* paint, SrcRectConstraint const raint) {
510 SkASSERT(0 == ((unsigned)constraint & ~1));
511 unsigned extra = (unsigned)constraint;
512 if (paint) {
513 extra |= kHasPaint_DrawImageRectMask;
514 }
515 if (src) {
516 extra |= kHasSrcRect_DrawImageRectMask;
517 }
518
519 SkPipeWriter writer(this);
520 writer.write32(pack_verb(SkPipeVerb::kDrawImageRect, extra));
521 writer.writeImage(image);
522 if (src) {
523 writer.write(src, sizeof(*src));
524 }
525 writer.write(&dst, sizeof(dst));
526 if (paint) {
527 write_paint(writer, *paint, kImage_PaintUsage);
528 }
529 }
530
531 void SkPipeCanvas::onDrawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst,
532 const SkPaint* paint) {
533 unsigned extra = 0;
534 if (paint) {
535 extra |= kHasPaint_DrawImageNineMask;
536 }
537 SkPipeWriter writer(this);
538 writer.write32(pack_verb(SkPipeVerb::kDrawImageNine, extra));
539 writer.writeImage(image);
540 writer.write(&center, sizeof(center));
541 writer.write(&dst, sizeof(dst));
542 if (paint) {
543 write_paint(writer, *paint, kImage_PaintUsage);
544 }
545 }
546
547 void SkPipeCanvas::onDrawImageLattice(const SkImage* image, const Lattice& latti ce,
548 const SkRect& dst, const SkPaint* paint) {
549 unsigned extra = 0;
550 if (paint) {
551 extra |= kHasPaint_DrawImageLatticeMask;
552 }
553 if (lattice.fFlags) {
554 extra |= kHasFlags_DrawImageLatticeMask;
555 }
556 if (lattice.fXCount >= kCount_DrawImageLatticeMask) {
557 extra |= kCount_DrawImageLatticeMask << kXCount_DrawImageLatticeShift;
558 } else {
559 extra |= lattice.fXCount << kXCount_DrawImageLatticeShift;
560 }
561 if (lattice.fYCount >= kCount_DrawImageLatticeMask) {
562 extra |= kCount_DrawImageLatticeMask << kYCount_DrawImageLatticeShift;
563 } else {
564 extra |= lattice.fYCount << kYCount_DrawImageLatticeShift;
565 }
566
567 SkPipeWriter writer(this);
568 writer.write32(pack_verb(SkPipeVerb::kDrawImageLattice, extra));
569 writer.writeImage(image);
570 if (lattice.fXCount >= kCount_DrawImageLatticeMask) {
571 writer.write32(lattice.fXCount);
572 }
573 if (lattice.fYCount >= kCount_DrawImageLatticeMask) {
574 writer.write32(lattice.fYCount);
575 }
576 // Often these divs will be small (8 or 16 bits). Consider sniffing that and writing a flag
577 // so we can store them smaller.
578 writer.write(lattice.fXDivs, lattice.fXCount * sizeof(int32_t));
579 writer.write(lattice.fYDivs, lattice.fYCount * sizeof(int32_t));
580 if (lattice.fFlags) {
581 int32_t count = (lattice.fXCount + 1) * (lattice.fYCount + 1);
582 SkASSERT(count > 0);
583 write_pad(&writer, lattice.fFlags, count);
584 }
585 writer.write(&dst, sizeof(dst));
586 if (paint) {
587 write_paint(writer, *paint, kImage_PaintUsage);
588 }
589 }
590
591 //////////////////////////////////////////////////////////////////////////////// ///////////////////
592
593 void SkPipeCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, S kScalar y,
594 const SkPaint& paint) {
595 SkASSERT(byteLength);
596
597 bool compact = fits_in(byteLength, 24);
598
599 SkPipeWriter writer(this);
600 writer.write32(pack_verb(SkPipeVerb::kDrawText, compact ? (unsigned)byteLeng th : 0));
601 if (!compact) {
602 writer.write32(SkToU32(byteLength));
603 }
604 write_pad(&writer, text, byteLength);
605 writer.writeScalar(x);
606 writer.writeScalar(y);
607 write_paint(writer, paint, kText_PaintUsage);
608 }
609
610 void SkPipeCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPo int pos[],
611 const SkPaint& paint) {
612 SkASSERT(byteLength);
613
614 bool compact = fits_in(byteLength, 24);
615
616 SkPipeWriter writer(this);
617 writer.write32(pack_verb(SkPipeVerb::kDrawPosText, compact ? (unsigned)byteL ength : 0));
618 if (!compact) {
619 writer.write32(SkToU32(byteLength));
620 }
621 write_pad(&writer, text, byteLength);
622 writer.writePointArray(pos, paint.countText(text, byteLength));
623 write_paint(writer, paint, kText_PaintUsage);
624 }
625
626 void SkPipeCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkS calar xpos[],
627 SkScalar constY, const SkPaint& paint) {
628 SkASSERT(byteLength);
629
630 bool compact = fits_in(byteLength, 24);
631
632 SkPipeWriter writer(this);
633 writer.write32(pack_verb(SkPipeVerb::kDrawPosTextH, compact ? (unsigned)byte Length : 0));
634 if (!compact) {
635 writer.write32(SkToU32(byteLength));
636 }
637 write_pad(&writer, text, byteLength);
638 writer.writeScalarArray(xpos, paint.countText(text, byteLength));
639 writer.writeScalar(constY);
640 write_paint(writer, paint, kText_PaintUsage);
641 }
642
643 void SkPipeCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const S kPath& path,
644 const SkMatrix* matrix, const SkPaint& paint ) {
645 SkASSERT(byteLength > 0);
646
647 unsigned extra = 0;
648 if (byteLength <= kTextLength_DrawTextOnPathMask) {
649 extra |= byteLength;
650 } // else we will write the length after the packedverb
651 SkMatrix::TypeMask tm = matrix ? matrix->getType() : SkMatrix::kIdentity_Mas k;
652 extra |= (unsigned)tm << kMatrixType_DrawTextOnPathShift;
653
654 SkPipeWriter writer(this);
655 writer.write32(pack_verb(SkPipeVerb::kDrawTextOnPath, extra));
656 if (byteLength > kTextLength_DrawTextOnPathMask) {
657 writer.write32(byteLength);
658 }
659 write_pad(&writer, text, byteLength);
660 writer.writePath(path);
661 if (matrix) {
662 write_sparse_matrix(&writer, *matrix);
663 }
664 write_paint(writer, paint, kText_PaintUsage);
665 }
666
667 void SkPipeCanvas::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
668 const SkRect* cull, const SkPaint& paint) {
669 SkASSERT(byteLength);
670
671 bool compact = fits_in(byteLength, 23);
672 unsigned extra = compact ? (byteLength << 1) : 0;
673 if (cull) {
674 extra |= 1;
675 }
676
677 SkPipeWriter writer(this);
678 writer.write32(pack_verb(SkPipeVerb::kDrawTextRSXform, extra));
679 if (!compact) {
680 writer.write32(SkToU32(byteLength));
681 }
682 write_pad(&writer, text, byteLength);
683
684 int count = paint.countText(text, byteLength);
685 writer.write32(count); // maybe we can/should store this in extra as well?
686 writer.write(xform, count * sizeof(SkRSXform));
687 if (cull) {
688 writer.writeRect(*cull);
689 }
690 write_paint(writer, paint, kText_PaintUsage);
691 }
692
693 void SkPipeCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y ,
694 const SkPaint &paint) {
695 SkPipeWriter writer(this);
696 writer.write32(pack_verb(SkPipeVerb::kDrawTextBlob, 0));
697 blob->flatten(writer);
698 writer.writeScalar(x);
699 writer.writeScalar(y);
700 write_paint(writer, paint, kTextBlob_PaintUsage);
701 }
702
703 void SkPipeCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matri x,
704 const SkPaint* paint) {
705 unsigned extra = fDeduper->findOrDefinePicture(const_cast<SkPicture*>(pictur e));
706 if (matrix) {
707 extra |= kHasMatrix_DrawPictureExtra;
708 }
709 if (paint) {
710 extra |= kHasPaint_DrawPictureExtra;
711 }
712 SkPipeWriter writer(this);
713 writer.write32(pack_verb(SkPipeVerb::kDrawPicture, extra));
714 if (matrix) {
715 writer.writeMatrix(*matrix);
716 }
717 if (paint) {
718 write_paint(writer, *paint, kSaveLayer_PaintUsage);
719 }
720 }
721
722 void SkPipeCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
723 size_t size = region.writeToMemory(nullptr);
724 unsigned extra = 0;
725 if (fits_in(size, 24)) {
726 extra = SkToUInt(size);
727 }
728
729 SkPipeWriter writer(this);
730 writer.write32(pack_verb(SkPipeVerb::kDrawRegion, extra));
731 if (0 == extra) {
732 writer.write32(size);
733 }
734 SkAutoSMalloc<2048> storage(size);
735 region.writeToMemory(storage.get());
736 write_pad(&writer, storage.get(), size);
737 write_paint(writer, paint, kGeometry_PaintUsage);
738 }
739
740 void SkPipeCanvas::onDrawVertices(VertexMode vmode, int vertexCount,
741 const SkPoint vertices[], const SkPoint texs[] ,
742 const SkColor colors[], SkXfermode* xmode,
743 const uint16_t indices[], int indexCount,
744 const SkPaint& paint) {
745 SkASSERT(vertexCount > 0);
746
747 unsigned extra = 0;
748 if (vertexCount <= kVCount_DrawVerticesMask) {
749 extra |= vertexCount;
750 }
751 extra |= (unsigned)vmode << kVMode_DrawVerticesShift;
752
753 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
754 if (xmode && !SkXfermode::AsMode(xmode, &mode)) {
755 mode = (SkXfermode::Mode)0xFF; // sentinel for read the xfer later
756 }
757 extra |= (unsigned)mode << kXMode_DrawVerticesShift;
758
759 if (texs) {
760 extra |= kHasTex_DrawVerticesMask;
761 }
762 if (colors) {
763 extra |= kHasColors_DrawVerticesMask;
764 }
765 if (indexCount > 0) {
766 extra |= kHasIndices_DrawVerticesMask;
767 }
768
769 SkPipeWriter writer(this);
770 writer.write32(pack_verb(SkPipeVerb::kDrawVertices, extra));
771 if (vertexCount > kVCount_DrawVerticesMask) {
772 writer.write32(vertexCount);
773 }
774 if (mode == (SkXfermode::Mode)0xFF) {
775 writer.writeFlattenable(xmode);
776 }
777 writer.write(vertices, vertexCount * sizeof(SkPoint));
778 if (texs) {
779 writer.write(texs, vertexCount * sizeof(SkPoint));
780 }
781 if (colors) {
782 writer.write(colors, vertexCount * sizeof(SkColor));
783 }
784 if (indexCount > 0) {
785 writer.write32(indexCount);
786 SkASSERT(SkIsAlign2(indexCount));
787 writer.write(indices, indexCount * sizeof(uint16_t));
788 }
789 write_paint(writer, paint, kVertices_PaintUsage);
790 }
791
792 void SkPipeCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4] ,
793 const SkPoint texCoords[4], SkXfermode* xfer,
794 const SkPaint& paint) {
795 SkPipeWriter writer(this);
796 unsigned extra = 0;
797 SkXfermode::Mode mode = SkXfermode::kModulate_Mode;
798 if (xfer && !xfer->asMode(&mode)) {
799 mode = (SkXfermode::Mode)kExplicitXfer_DrawPatchExtraValue;
800 } else {
801 xfer = nullptr; // signal that we're using the mode enum
802 }
803 SkASSERT(0 == (mode & ~kModeEnum_DrawPatchExtraMask));
804 extra = (unsigned)mode;
805 if (colors) {
806 extra |= kHasColors_DrawPatchExtraMask;
807 }
808 if (texCoords) {
809 extra |= kHasTexture_DrawPatchExtraMask;
810 }
811 writer.write32(pack_verb(SkPipeVerb::kDrawPatch, extra));
812 writer.write(cubics, sizeof(SkPoint) * 12);
813 if (colors) {
814 writer.write(colors, sizeof(SkColor) * 4);
815 }
816 if (texCoords) {
817 writer.write(texCoords, sizeof(SkPoint) * 4);
818 }
819 if (xfer) {
820 xfer->flatten(writer);
821 }
822 write_paint(writer, paint, kGeometry_PaintUsage);
823 }
824
825 void SkPipeCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData * data) {
826 const size_t len = strlen(key) + 1; // must write the trailing 0
827 bool compact = fits_in(len, 23);
828 uint32_t extra = compact ? (unsigned)len : 0;
829 extra <<= 1; // make room for has_data_sentinel
830 if (data) {
831 extra |= 1;
832 }
833
834 fStream->write32(pack_verb(SkPipeVerb::kDrawAnnotation, extra));
835 fStream->write(&rect, sizeof(SkRect));
836 if (!compact) {
837 fStream->write32(SkToU32(len));
838 }
839 write_pad(fStream, key, len);
840 if (data) {
841 fStream->write32(SkToU32(data->size()));
842 write_pad(fStream, data->data(), data->size());
843 }
844 }
845
846 //////////////////////////////////////////////////////////////////////////////// ///////////////////
847
848 class A8Serializer : public SkPixelSerializer {
849 protected:
850 bool onUseEncodedData(const void* data, size_t len) {
851 return true;
852 }
853
854 SkData* onEncode(const SkPixmap& pmap) {
855 if (kAlpha_8_SkColorType == pmap.colorType()) {
856 SkDynamicMemoryWStream stream;
857 stream.write("skiaimgf", 8);
858 stream.write32(pmap.width());
859 stream.write32(pmap.height());
860 stream.write16(pmap.colorType());
861 stream.write16(pmap.alphaType());
862 stream.write32(0); // no colorspace for now
863 for (int y = 0; y < pmap.height(); ++y) {
864 stream.write(pmap.addr8(0, y), pmap.width());
865 }
866 return stream.detachAsData().release();
867 }
868 return nullptr;
869 }
870 };
871
872 static bool show_deduper_traffic = false;
873
874 int SkPipeDeduper::findOrDefineImage(SkImage* image) {
875 int index = fImages.find(image->uniqueID());
876 SkASSERT(index >= 0);
877 if (index) {
878 if (show_deduper_traffic) {
879 SkDebugf(" reuseImage(%d)\n", index - 1);
880 }
881 return index;
882 }
883
884 A8Serializer serial;
885 sk_sp<SkData> data(image->encode(&serial));
886 if (!data) {
887 data.reset(image->encode());
888 }
889 if (data) {
890 index = fImages.add(image->uniqueID());
891 SkASSERT(index > 0);
892 SkASSERT(fits_in(index, 24));
893 fStream->write32(pack_verb(SkPipeVerb::kDefineImage, index));
894
895 uint32_t len = SkToU32(data->size());
896 fStream->write32(SkAlign4(len));
897 write_pad(fStream, data->data(), len);
898
899 if (show_deduper_traffic) {
900 int size = image->width() * image->height() << 2;
901 SkDebugf(" defineImage(%d) %d -> %d\n", index - 1, size, len);
902 }
903 return index;
904 }
905 SkDebugf("+++ failed to encode image [%d %d]\n", image->width(), image->heig ht());
906 return 0; // failed to encode
907 }
908
909 int SkPipeDeduper::findOrDefinePicture(SkPicture* picture) {
910 int index = fPictures.find(picture->uniqueID());
911 SkASSERT(index >= 0);
912 if (index) {
913 if (show_deduper_traffic) {
914 SkDebugf(" reusePicture(%d)\n", index - 1);
915 }
916 return index;
917 }
918
919 size_t prevWritten = fStream->bytesWritten();
920 unsigned extra = 0; // 0 means we're defining a new picture, non-zero means undef_index + 1
921 fStream->write32(pack_verb(SkPipeVerb::kDefinePicture, extra));
922 const SkRect cull = picture->cullRect();
923 fStream->write(&cull, sizeof(cull));
924 picture->playback(fPipeCanvas);
925 // call fPictures.add *after* we're written the picture, so that any nested pictures will have
926 // already been defined, and we get the "last" index value.
927 index = fPictures.add(picture->uniqueID());
928 ASSERT_FITS_IN(index, kObjectDefinitionBits);
929 fStream->write32(pack_verb(SkPipeVerb::kEndPicture, index));
930
931 SkDebugf(" definePicture(%d) %d\n",
932 index - 1, SkToU32(fStream->bytesWritten() - prevWritten));
933 return index;
934 }
935
936 static sk_sp<SkData> encode(SkTypeface* tf) {
937 SkDynamicMemoryWStream stream;
938 tf->serialize(&stream);
939 return sk_sp<SkData>(stream.detachAsData());
940 }
941
942 int SkPipeDeduper::findOrDefineTypeface(SkTypeface* typeface) {
943 if (!typeface) {
944 return 0; // default
945 }
946
947 int index = fTypefaces.find(typeface->uniqueID());
948 SkASSERT(index >= 0);
949 if (index) {
950 if (show_deduper_traffic) {
951 SkDebugf(" reuseTypeface(%d)\n", index - 1);
952 }
953 return index;
954 }
955
956 sk_sp<SkData> data = fTFSerializer ? fTFSerializer->serialize(typeface) : en code(typeface);
957 if (data) {
958 index = fTypefaces.add(typeface->uniqueID());
959 SkASSERT(index > 0);
960 SkASSERT(fits_in(index, 24));
961 fStream->write32(pack_verb(SkPipeVerb::kDefineTypeface, index));
962
963 uint32_t len = SkToU32(data->size());
964 fStream->write32(SkAlign4(len));
965 write_pad(fStream, data->data(), len);
966
967 if (show_deduper_traffic) {
968 SkDebugf(" defineTypeface(%d) %d\n", index - 1, len);
969 }
970 return index;
971 }
972 SkDebugf("+++ failed to encode typeface %d\n", typeface->uniqueID());
973 return 0; // failed to encode
974 }
975
976 int SkPipeDeduper::findOrDefineFactory(SkFlattenable* flattenable) {
977 if (!flattenable) {
978 return 0;
979 }
980
981 int index = fFactories.find(flattenable->getFactory());
982 SkASSERT(index >= 0);
983 if (index) {
984 if (show_deduper_traffic) {
985 SkDebugf(" reuseFactory(%d)\n", index - 1);
986 }
987 return index;
988 }
989
990 index = fFactories.add(flattenable->getFactory());
991 ASSERT_FITS_IN(index, kIndex_DefineFactoryExtraBits);
992 const char* name = flattenable->getTypeName();
993 size_t len = strlen(name);
994 ASSERT_FITS_IN(len, kNameLength_DefineFactoryExtraBits);
995 unsigned extra = (index << kNameLength_DefineFactoryExtraBits) | len;
996 size_t prevWritten = fStream->bytesWritten();
997 fStream->write32(pack_verb(SkPipeVerb::kDefineFactory, extra));
998 write_pad(fStream, name, len + 1);
999 if (false) {
1000 SkDebugf(" defineFactory(%d) %d %s\n",
1001 index - 1, SkToU32(fStream->bytesWritten() - prevWritten), name);
1002 }
1003 return index;
1004 }
1005
1006 //////////////////////////////////////////////////////////////////////////////// ///////////////////
1007 #include "SkPipe.h"
1008
1009 class SkPipeSerializer::Impl {
1010 public:
1011 SkPipeDeduper fDeduper;
1012 std::unique_ptr<SkPipeCanvas> fCanvas;
1013 };
1014
1015 SkPipeSerializer::SkPipeSerializer() : fImpl(new Impl) {}
1016
1017 SkPipeSerializer::~SkPipeSerializer() {
1018 if (fImpl->fCanvas) {
1019 this->endWrite();
1020 }
1021 }
1022
1023 void SkPipeSerializer::setTypefaceSerializer(SkTypefaceSerializer* tfs) {
1024 fImpl->fDeduper.setTypefaceSerializer(tfs);
1025 }
1026
1027 void SkPipeSerializer::resetCache() {
1028 fImpl->fDeduper.resetCaches();
1029 }
1030
1031 void SkPipeSerializer::write(SkPicture* picture, SkWStream* stream) {
1032 stream->write32(kDefinePicture_ExtPipeVerb);
1033 SkRect cull = picture->cullRect();
1034 stream->write(&cull.fLeft, sizeof(SkRect));
1035 picture->playback(this->beginWrite(cull, stream));
1036 this->endWrite();
1037 }
1038
1039 void SkPipeSerializer::write(SkImage* image, SkWStream* stream) {
1040 stream->write32(kDefineImage_ExtPipeVerb);
1041 SkPipeWriter writer(stream, &fImpl->fDeduper);
1042 writer.writeImage(image);
1043 }
1044
1045 SkCanvas* SkPipeSerializer::beginWrite(const SkRect& cull, SkWStream* stream) {
1046 SkASSERT(nullptr == fImpl->fCanvas);
1047 fImpl->fCanvas.reset(new SkPipeCanvas(cull, &fImpl->fDeduper, stream));
1048 fImpl->fDeduper.setStream(stream);
1049 fImpl->fDeduper.setCanvas(fImpl->fCanvas.get());
1050 return fImpl->fCanvas.get();
1051 }
1052
1053 void SkPipeSerializer::endWrite() {
1054 fImpl->fCanvas->restoreToCount(1);
1055 fImpl->fCanvas.reset(nullptr);
1056 }
OLDNEW
« src/core/SkPipe.h ('K') | « src/pipe/SkPipeCanvas.h ('k') | src/pipe/SkPipeFormat.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698