Debian Codegraph @

src/core/SkPictureRecord.cpp

raw
1 /*
2 * Copyright 2011 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 "src/core/SkPictureRecord.h"
9
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkImageFilter.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPoint3.h"
14 #include "include/core/SkRRect.h"
15 #include "include/core/SkRSXform.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRegion.h"
18 #include "include/core/SkShader.h"
19 #include "include/core/SkSurface.h"
20 #include "include/core/SkTextBlob.h"
21 #include "include/core/SkTileMode.h"
22 #include "include/private/base/SkPoint_impl.h"
23 #include "include/private/base/SkTo.h"
24 #include "include/private/chromium/Slug.h"
25 #include "src/core/SkCanvasPriv.h"
26 #include "src/core/SkDrawShadowInfo.h"
27 #include "src/core/SkMatrixPriv.h"
28 #include "src/core/SkSamplingPriv.h"
29 #include "src/utils/SkPatchUtils.h"
30
31 #include <utility>
32
33 class SkSurfaceProps;
34 enum class SkClipOp;
35 struct SkISize;
36 struct SkImageInfo;
37
38 using namespace skia_private;
39
40 enum {
41 // just need a value that save or getSaveCount would never return
42 kNoInitialSave = -1,
43 };
44
45 // A lot of basic types get stored as a uint32_t: bools, ints, paint indices, etc.
46 static int const kUInt32Size = 4;
47
48 SkPictureRecord::SkPictureRecord(const SkIRect& dimensions, uint32_t flags)
49 : SkCanvasVirtualEnforcer<SkCanvas>(dimensions)
50 , fRecordFlags(flags)
51 , fInitialSaveCount(kNoInitialSave) {
52 }
53
54 SkPictureRecord::SkPictureRecord(const SkISize& dimensions, uint32_t flags)
55 : SkPictureRecord(SkIRect::MakeSize(dimensions), flags) {}
56
57 void SkPictureRecord::willSave() {
58 // record the offset to us, making it non-positive to distinguish a save
59 // from a clip entry.
60 fRestoreOffsetStack.push_back(-(int32_t)fWriter.bytesWritten());
61 this->recordSave();
62
63 this->SkCanvasVirtualEnforcer<SkCanvas>::willSave();
64 }
65
66 void SkPictureRecord::recordSave() {
67 // op only
68 size_t size = sizeof(kUInt32Size);
69 size_t initialOffset = this->addDraw(SAVE, &size);
70
71 this->validate(initialOffset, size);
72 }
73
74 SkCanvas::SaveLayerStrategy SkPictureRecord::getSaveLayerStrategy(const SaveLayerRec& rec) {
75 // record the offset to us, making it non-positive to distinguish a save
76 // from a clip entry.
77 fRestoreOffsetStack.push_back(-(int32_t)fWriter.bytesWritten());
78 this->recordSaveLayer(rec);
79
80 (void)this->SkCanvasVirtualEnforcer<SkCanvas>::getSaveLayerStrategy(rec);
81 /* No need for a (potentially very big) layer which we don't actually need
82 at this time (and may not be able to afford since during record our
83 clip starts out the size of the picture, which is often much larger
84 than the size of the actual device we'll use during playback).
85 */
86 return kNoLayer_SaveLayerStrategy;
87 }
88
89 bool SkPictureRecord::onDoSaveBehind(const SkRect* subset) {
90 fRestoreOffsetStack.push_back(-(int32_t)fWriter.bytesWritten());
91
92 size_t size = sizeof(kUInt32Size) + sizeof(uint32_t); // op + flags
93 uint32_t flags = 0;
94 if (subset) {
95 flags |= SAVEBEHIND_HAS_SUBSET;
96 size += sizeof(*subset);
97 }
98
99 size_t initialOffset = this->addDraw(SAVE_BEHIND, &size);
100 this->addInt(flags);
101 if (subset) {
102 this->addRect(*subset);
103 }
104
105 this->validate(initialOffset, size);
106 return false;
107 }
108
109 void SkPictureRecord::recordSaveLayer(const SaveLayerRec& rec) {
110 // op + flatflags
111 size_t size = 2 * kUInt32Size;
112 uint32_t flatFlags = 0;
113 uint32_t filterCount = SkToU32(rec.fFilters.size());
114
115 if (rec.fBounds) {
116 flatFlags |= SAVELAYERREC_HAS_BOUNDS;
117 size += sizeof(*rec.fBounds);
118 }
119 if (rec.fPaint) {
120 flatFlags |= SAVELAYERREC_HAS_PAINT;
121 size += sizeof(uint32_t); // index
122 }
123 if (rec.fBackdrop) {
124 flatFlags |= SAVELAYERREC_HAS_BACKDROP;
125 size += sizeof(uint32_t); // (paint) index
126 }
127 if (rec.fSaveLayerFlags) {
128 flatFlags |= SAVELAYERREC_HAS_FLAGS;
129 size += sizeof(uint32_t);
130 }
131 if (SkCanvasPriv::GetBackdropScaleFactor(rec) != 1.f) {
132 flatFlags |= SAVELAYERREC_HAS_BACKDROP_SCALE;
133 size += sizeof(SkScalar);
134 }
135 if (filterCount) {
136 flatFlags |= SAVELAYERREC_HAS_MULTIPLE_FILTERS;
137 size += sizeof(uint32_t); // count
138 size += sizeof(uint32_t) * filterCount; // N (paint) indices
139 }
140 if (rec.fBackdropTileMode != SkTileMode::kClamp) {
141 flatFlags |= SAVELAYERREC_HAS_BACKDROP_TILEMODE;
142 size += sizeof(uint32_t); // SkTileMode
143 }
144
145 const size_t initialOffset = this->addDraw(SAVE_LAYER_SAVELAYERREC, &size);
146 this->addInt(flatFlags);
147 if (flatFlags & SAVELAYERREC_HAS_BOUNDS) {
148 this->addRect(*rec.fBounds);
149 }
150 if (flatFlags & SAVELAYERREC_HAS_PAINT) {
151 this->addPaintPtr(rec.fPaint);
152 }
153 if (flatFlags & SAVELAYERREC_HAS_BACKDROP) {
154 // overkill, but we didn't already track single flattenables, so using a paint for that
155 SkPaint paint;
156 paint.setImageFilter(sk_ref_sp(const_cast<SkImageFilter*>(rec.fBackdrop)));
157 this->addPaint(paint);
158 }
159 if (flatFlags & SAVELAYERREC_HAS_FLAGS) {
160 this->addInt(rec.fSaveLayerFlags);
161 }
162 if (flatFlags & SAVELAYERREC_HAS_BACKDROP_SCALE) {
163 this->addScalar(SkCanvasPriv::GetBackdropScaleFactor(rec));
164 }
165 if (flatFlags & SAVELAYERREC_HAS_MULTIPLE_FILTERS) {
166 this->addInt(filterCount);
167 for (uint32_t i = 0; i < filterCount; ++i) {
168 // overkill to store a paint, oh well.
169 SkPaint paint;
170 paint.setImageFilter(rec.fFilters[i]);
171 this->addPaint(paint);
172 }
173 }
174 if (rec.fBackdropTileMode != SkTileMode::kClamp) {
175 this->addInt((int) rec.fBackdropTileMode);
176 }
177 this->validate(initialOffset, size);
178 }
179
180 #ifdef SK_DEBUG
181 /*
182 * Read the op code from 'offset' in 'writer' and extract the size too.
183 */
184 static DrawType peek_op_and_size(SkWriter32* writer, size_t offset, uint32_t* size) {
185 uint32_t peek = writer->readTAt<uint32_t>(offset);
186
187 uint32_t op;
188 UNPACK_8_24(peek, op, *size);
189 if (MASK_24 == *size) {
190 // size required its own slot right after the op code
191 *size = writer->readTAt<uint32_t>(offset + kUInt32Size);
192 }
193 return (DrawType) op;
194 }
195 #endif//SK_DEBUG
196
197 void SkPictureRecord::willRestore() {
198 #if 0
199 SkASSERT(fRestoreOffsetStack.count() > 1);
200 #endif
201
202 // check for underflow
203 if (fRestoreOffsetStack.empty()) {
204 return;
205 }
206
207 this->recordRestore();
208
209 fRestoreOffsetStack.pop_back();
210
211 this->SkCanvasVirtualEnforcer<SkCanvas>::willRestore();
212 }
213
214 void SkPictureRecord::recordRestore(bool fillInSkips) {
215 if (fillInSkips) {
216 this->fillRestoreOffsetPlaceholdersForCurrentStackLevel((uint32_t)fWriter.bytesWritten());
217 }
218 size_t size = 1 * kUInt32Size; // RESTORE consists solely of 1 op code
219 size_t initialOffset = this->addDraw(RESTORE, &size);
220 this->validate(initialOffset, size);
221 }
222
223 void SkPictureRecord::recordTranslate(const SkMatrix& m) {
224 SkASSERT(SkMatrix::kTranslate_Mask == m.getType());
225
226 // op + dx + dy
227 size_t size = 1 * kUInt32Size + 2 * sizeof(SkScalar);
228 size_t initialOffset = this->addDraw(TRANSLATE, &size);
229 this->addScalar(m.getTranslateX());
230 this->addScalar(m.getTranslateY());
231 this->validate(initialOffset, size);
232 }
233
234 void SkPictureRecord::recordScale(const SkMatrix& m) {
235 SkASSERT(SkMatrix::kScale_Mask == m.getType());
236
237 // op + sx + sy
238 size_t size = 1 * kUInt32Size + 2 * sizeof(SkScalar);
239 size_t initialOffset = this->addDraw(SCALE, &size);
240 this->addScalar(m.getScaleX());
241 this->addScalar(m.getScaleY());
242 this->validate(initialOffset, size);
243 }
244
245 void SkPictureRecord::didConcat44(const SkM44& m) {
246 this->validate(fWriter.bytesWritten(), 0);
247 // op + matrix
248 size_t size = kUInt32Size + 16 * sizeof(SkScalar);
249 size_t initialOffset = this->addDraw(CONCAT44, &size);
250 fWriter.write(SkMatrixPriv::M44ColMajor(m), 16 * sizeof(SkScalar));
251 this->validate(initialOffset, size);
252
253 this->SkCanvasVirtualEnforcer<SkCanvas>::didConcat44(m);
254 }
255
256 void SkPictureRecord::didSetM44(const SkM44& m) {
257 this->validate(fWriter.bytesWritten(), 0);
258 // op + matrix
259 size_t size = kUInt32Size + 16 * sizeof(SkScalar);
260 size_t initialOffset = this->addDraw(SET_M44, &size);
261 fWriter.write(SkMatrixPriv::M44ColMajor(m), 16 * sizeof(SkScalar));
262 this->validate(initialOffset, size);
263 this->SkCanvasVirtualEnforcer<SkCanvas>::didSetM44(m);
264 }
265
266 void SkPictureRecord::didScale(SkScalar x, SkScalar y) {
267 this->didConcat44(SkM44::Scale(x, y));
268 }
269
270 void SkPictureRecord::didTranslate(SkScalar x, SkScalar y) {
271 this->didConcat44(SkM44::Translate(x, y));
272 }
273
274 void SkPictureRecord::recordConcat(const SkMatrix& matrix) {
275 this->validate(fWriter.bytesWritten(), 0);
276 // op + matrix
277 size_t size = kUInt32Size + SkMatrixPriv::WriteToMemory(matrix, nullptr);
278 size_t initialOffset = this->addDraw(CONCAT, &size);
279 this->addMatrix(matrix);
280 this->validate(initialOffset, size);
281 }
282
283 void SkPictureRecord::fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset) {
284 int32_t offset = fRestoreOffsetStack.back();
285 while (offset > 0) {
286 uint32_t peek = fWriter.readTAt<uint32_t>(offset);
287 fWriter.overwriteTAt(offset, restoreOffset);
288 offset = peek;
289 }
290
291 #ifdef SK_DEBUG
292 // offset of 0 has been disabled, so we skip it
293 if (offset > 0) {
294 // assert that the final offset value points to a save verb
295 uint32_t opSize;
296 DrawType drawOp = peek_op_and_size(&fWriter, -offset, &opSize);
297 SkASSERT(SAVE == drawOp || SAVE_LAYER_SAVELAYERREC == drawOp);
298 }
299 #endif
300 }
301
302 void SkPictureRecord::beginRecording() {
303 // we have to call this *after* our constructor, to ensure that it gets
304 // recorded. This is balanced by restoreToCount() call from endRecording,
305 // which in-turn calls our overridden restore(), so those get recorded too.
306 fInitialSaveCount = this->save();
307 }
308
309 void SkPictureRecord::endRecording() {
310 SkASSERT(kNoInitialSave != fInitialSaveCount);
311 this->restoreToCount(fInitialSaveCount);
312 }
313
314 size_t SkPictureRecord::recordRestoreOffsetPlaceholder() {
315 if (fRestoreOffsetStack.empty()) {
316 return -1;
317 }
318
319 // The RestoreOffset field is initially filled with a placeholder
320 // value that points to the offset of the previous RestoreOffset
321 // in the current stack level, thus forming a linked list so that
322 // the restore offsets can be filled in when the corresponding
323 // restore command is recorded.
324 int32_t prevOffset = fRestoreOffsetStack.back();
325
326 size_t offset = fWriter.bytesWritten();
327 this->addInt(prevOffset);
328 fRestoreOffsetStack.back() = SkToU32(offset);
329 return offset;
330 }
331
332 void SkPictureRecord::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
333 this->recordClipRect(rect, op, kSoft_ClipEdgeStyle == edgeStyle);
334 this->SkCanvasVirtualEnforcer<SkCanvas>::onClipRect(rect, op, edgeStyle);
335 }
336
337 size_t SkPictureRecord::recordClipRect(const SkRect& rect, SkClipOp op, bool doAA) {
338 // id + rect + clip params
339 size_t size = 1 * kUInt32Size + sizeof(rect) + 1 * kUInt32Size;
340 // recordRestoreOffsetPlaceholder doesn't always write an offset
341 if (!fRestoreOffsetStack.empty()) {
342 // + restore offset
343 size += kUInt32Size;
344 }
345 size_t initialOffset = this->addDraw(CLIP_RECT, &size);
346 this->addRect(rect);
347 this->addInt(ClipParams_pack(op, doAA));
348 size_t offset = this->recordRestoreOffsetPlaceholder();
349
350 this->validate(initialOffset, size);
351 return offset;
352 }
353
354 void SkPictureRecord::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
355 this->recordClipRRect(rrect, op, kSoft_ClipEdgeStyle == edgeStyle);
356 this->SkCanvasVirtualEnforcer<SkCanvas>::onClipRRect(rrect, op, edgeStyle);
357 }
358
359 size_t SkPictureRecord::recordClipRRect(const SkRRect& rrect, SkClipOp op, bool doAA) {
360 // op + rrect + clip params
361 size_t size = 1 * kUInt32Size + SkRRect::kSizeInMemory + 1 * kUInt32Size;
362 // recordRestoreOffsetPlaceholder doesn't always write an offset
363 if (!fRestoreOffsetStack.empty()) {
364 // + restore offset
365 size += kUInt32Size;
366 }
367 size_t initialOffset = this->addDraw(CLIP_RRECT, &size);
368 this->addRRect(rrect);
369 this->addInt(ClipParams_pack(op, doAA));
370 size_t offset = recordRestoreOffsetPlaceholder();
371 this->validate(initialOffset, size);
372 return offset;
373 }
374
375 void SkPictureRecord::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
376 int pathID = this->addPathToHeap(path);
377 this->recordClipPath(pathID, op, kSoft_ClipEdgeStyle == edgeStyle);
378 this->SkCanvasVirtualEnforcer<SkCanvas>::onClipPath(path, op, edgeStyle);
379 }
380
381 size_t SkPictureRecord::recordClipPath(int pathID, SkClipOp op, bool doAA) {
382 // op + path index + clip params
383 size_t size = 3 * kUInt32Size;
384 // recordRestoreOffsetPlaceholder doesn't always write an offset
385 if (!fRestoreOffsetStack.empty()) {
386 // + restore offset
387 size += kUInt32Size;
388 }
389 size_t initialOffset = this->addDraw(CLIP_PATH, &size);
390 this->addInt(pathID);
391 this->addInt(ClipParams_pack(op, doAA));
392 size_t offset = recordRestoreOffsetPlaceholder();
393 this->validate(initialOffset, size);
394 return offset;
395 }
396
397 void SkPictureRecord::onClipShader(sk_sp<SkShader> cs, SkClipOp op) {
398 // Overkill to store a whole paint, but we don't have an existing structure to just store
399 // shaders. If size becomes an issue in the future, we can optimize this.
400 SkPaint paint;
401 paint.setShader(cs);
402
403 // op + paint index + clipop
404 size_t size = 3 * kUInt32Size;
405 size_t initialOffset = this->addDraw(CLIP_SHADER_IN_PAINT, &size);
406 this->addPaint(paint);
407 this->addInt((int)op);
408 this->validate(initialOffset, size);
409
410 this->SkCanvasVirtualEnforcer<SkCanvas>::onClipShader(std::move(cs), op);
411 }
412
413 void SkPictureRecord::onClipRegion(const SkRegion& region, SkClipOp op) {
414 this->recordClipRegion(region, op);
415 this->SkCanvasVirtualEnforcer<SkCanvas>::onClipRegion(region, op);
416 }
417
418 size_t SkPictureRecord::recordClipRegion(const SkRegion& region, SkClipOp op) {
419 // op + clip params + region
420 size_t size = 2 * kUInt32Size + region.writeToMemory(nullptr);
421 // recordRestoreOffsetPlaceholder doesn't always write an offset
422 if (!fRestoreOffsetStack.empty()) {
423 // + restore offset
424 size += kUInt32Size;
425 }
426 size_t initialOffset = this->addDraw(CLIP_REGION, &size);
427 this->addRegion(region);
428 this->addInt(ClipParams_pack(op, false));
429 size_t offset = this->recordRestoreOffsetPlaceholder();
430
431 this->validate(initialOffset, size);
432 return offset;
433 }
434
435 void SkPictureRecord::onResetClip() {
436 if (!fRestoreOffsetStack.empty()) {
437 // Run back through any previous clip ops, and mark their offset to
438 // be 0, disabling their ability to trigger a jump-to-restore, otherwise
439 // they could hide this expansion of the clip.
440 this->fillRestoreOffsetPlaceholdersForCurrentStackLevel(0);
441 }
442 size_t size = sizeof(kUInt32Size);
443 size_t initialOffset = this->addDraw(RESET_CLIP, &size);
444 this->validate(initialOffset, size);
445 this->SkCanvasVirtualEnforcer<SkCanvas>::onResetClip();
446 }
447
448 void SkPictureRecord::onDrawPaint(const SkPaint& paint) {
449 // op + paint index
450 size_t size = 2 * kUInt32Size;
451 size_t initialOffset = this->addDraw(DRAW_PAINT, &size);
452 this->addPaint(paint);
453 this->validate(initialOffset, size);
454 }
455
456 void SkPictureRecord::onDrawBehind(const SkPaint& paint) {
457 // logically the same as drawPaint, but with a diff enum
458 // op + paint index
459 size_t size = 2 * kUInt32Size;
460 size_t initialOffset = this->addDraw(DRAW_BEHIND_PAINT, &size);
461 this->addPaint(paint);
462 this->validate(initialOffset, size);
463 }
464
465 void SkPictureRecord::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
466 const SkPaint& paint) {
467 // op + paint index + mode + count + point data
468 size_t size = 4 * kUInt32Size + count * sizeof(SkPoint);
469 size_t initialOffset = this->addDraw(DRAW_POINTS, &size);
470 this->addPaint(paint);
471
472 this->addInt(mode);
473 this->addInt(SkToInt(count));
474 fWriter.writeMul4(pts, count * sizeof(SkPoint));
475 this->validate(initialOffset, size);
476 }
477
478 void SkPictureRecord::onDrawOval(const SkRect& oval, const SkPaint& paint) {
479 // op + paint index + rect
480 size_t size = 2 * kUInt32Size + sizeof(oval);
481 size_t initialOffset = this->addDraw(DRAW_OVAL, &size);
482 this->addPaint(paint);
483 this->addRect(oval);
484 this->validate(initialOffset, size);
485 }
486
487 void SkPictureRecord::onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
488 bool useCenter, const SkPaint& paint) {
489 // op + paint index + rect + start + sweep + bool (as int)
490 size_t size = 2 * kUInt32Size + sizeof(oval) + sizeof(startAngle) + sizeof(sweepAngle) +
491 sizeof(int);
492 size_t initialOffset = this->addDraw(DRAW_ARC, &size);
493 this->addPaint(paint);
494 this->addRect(oval);
495 this->addScalar(startAngle);
496 this->addScalar(sweepAngle);
497 this->addInt(useCenter);
498 this->validate(initialOffset, size);
499 }
500
501 void SkPictureRecord::onDrawRect(const SkRect& rect, const SkPaint& paint) {
502 // op + paint index + rect
503 size_t size = 2 * kUInt32Size + sizeof(rect);
504 size_t initialOffset = this->addDraw(DRAW_RECT, &size);
505 this->addPaint(paint);
506 this->addRect(rect);
507 this->validate(initialOffset, size);
508 }
509
510 void SkPictureRecord::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
511 // op + paint index + region
512 size_t regionBytes = region.writeToMemory(nullptr);
513 size_t size = 2 * kUInt32Size + regionBytes;
514 size_t initialOffset = this->addDraw(DRAW_REGION, &size);
515 this->addPaint(paint);
516 fWriter.writeRegion(region);
517 this->validate(initialOffset, size);
518 }
519
520 void SkPictureRecord::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
521 // op + paint index + rrect
522 size_t size = 2 * kUInt32Size + SkRRect::kSizeInMemory;
523 size_t initialOffset = this->addDraw(DRAW_RRECT, &size);
524 this->addPaint(paint);
525 this->addRRect(rrect);
526 this->validate(initialOffset, size);
527 }
528
529 void SkPictureRecord::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
530 const SkPaint& paint) {
531 // op + paint index + rrects
532 size_t size = 2 * kUInt32Size + SkRRect::kSizeInMemory * 2;
533 size_t initialOffset = this->addDraw(DRAW_DRRECT, &size);
534 this->addPaint(paint);
535 this->addRRect(outer);
536 this->addRRect(inner);
537 this->validate(initialOffset, size);
538 }
539
540 void SkPictureRecord::onDrawPath(const SkPath& path, const SkPaint& paint) {
541 // op + paint index + path index
542 size_t size = 3 * kUInt32Size;
543 size_t initialOffset = this->addDraw(DRAW_PATH, &size);
544 this->addPaint(paint);
545 this->addPath(path);
546 this->validate(initialOffset, size);
547 }
548
549 void SkPictureRecord::onDrawImage2(const SkImage* image, SkScalar x, SkScalar y,
550 const SkSamplingOptions& sampling, const SkPaint* paint) {
551 // op + paint_index + image_index + x + y
552 size_t size = 3 * kUInt32Size + 2 * sizeof(SkScalar) + SkSamplingPriv::FlatSize(sampling);
553 size_t initialOffset = this->addDraw(DRAW_IMAGE2, &size);
554 this->addPaintPtr(paint);
555 this->addImage(image);
556 this->addScalar(x);
557 this->addScalar(y);
558 this->addSampling(sampling);
559 this->validate(initialOffset, size);
560 }
561
562 void SkPictureRecord::onDrawImageRect2(const SkImage* image, const SkRect& src, const SkRect& dst,
563 const SkSamplingOptions& sampling, const SkPaint* paint,
564 SrcRectConstraint constraint) {
565 // id + paint_index + image_index + constraint
566 size_t size = 3 * kUInt32Size + 2 * sizeof(dst) + SkSamplingPriv::FlatSize(sampling) +
567 kUInt32Size;
568
569 size_t initialOffset = this->addDraw(DRAW_IMAGE_RECT2, &size);
570 this->addPaintPtr(paint);
571 this->addImage(image);
572 this->addRect(src);
573 this->addRect(dst);
574 this->addSampling(sampling);
575 this->addInt(constraint);
576 this->validate(initialOffset, size);
577 }
578
579 void SkPictureRecord::onDrawImageLattice2(const SkImage* image, const Lattice& lattice,
580 const SkRect& dst, SkFilterMode filter,
581 const SkPaint* paint) {
582 size_t latticeSize = SkCanvasPriv::WriteLattice(nullptr, lattice);
583 // op + paint index + image index + lattice + dst rect
584 size_t size = 3 * kUInt32Size + latticeSize + sizeof(dst) + sizeof(uint32_t); // filter
585 size_t initialOffset = this->addDraw(DRAW_IMAGE_LATTICE2, &size);
586 this->addPaintPtr(paint);
587 this->addImage(image);
588 (void)SkCanvasPriv::WriteLattice(fWriter.reservePad(latticeSize), lattice);
589 this->addRect(dst);
590 this->addInt(static_cast<uint32_t>(filter));
591 this->validate(initialOffset, size);
592 }
593
594 void SkPictureRecord::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
595 const SkPaint& paint) {
596
597 // op + paint index + blob index + x/y
598 size_t size = 3 * kUInt32Size + 2 * sizeof(SkScalar);
599 size_t initialOffset = this->addDraw(DRAW_TEXT_BLOB, &size);
600
601 this->addPaint(paint);
602 this->addTextBlob(blob);
603 this->addScalar(x);
604 this->addScalar(y);
605
606 this->validate(initialOffset, size);
607 }
608
609 void SkPictureRecord::onDrawSlug(const sktext::gpu::Slug* slug, const SkPaint& paint) {
610 // op + paint index + slug id
611 size_t size = 3 * kUInt32Size;
612 size_t initialOffset = this->addDraw(DRAW_SLUG, &size);
613
614 this->addPaint(paint);
615 this->addSlug(slug);
616 this->validate(initialOffset, size);
617 }
618
619 void SkPictureRecord::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
620 const SkPaint* paint) {
621 // op + picture index
622 size_t size = 2 * kUInt32Size;
623 size_t initialOffset;
624
625 if (nullptr == matrix && nullptr == paint) {
626 initialOffset = this->addDraw(DRAW_PICTURE, &size);
627 this->addPicture(picture);
628 } else {
629 const SkMatrix& m = matrix ? *matrix : SkMatrix::I();
630 size += SkMatrixPriv::WriteToMemory(m, nullptr) + kUInt32Size; // matrix + paint
631 initialOffset = this->addDraw(DRAW_PICTURE_MATRIX_PAINT, &size);
632 this->addPaintPtr(paint);
633 this->addMatrix(m);
634 this->addPicture(picture);
635 }
636 this->validate(initialOffset, size);
637 }
638
639 void SkPictureRecord::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
640 // op + drawable index
641 size_t size = 2 * kUInt32Size;
642 size_t initialOffset;
643
644 if (nullptr == matrix) {
645 initialOffset = this->addDraw(DRAW_DRAWABLE, &size);
646 this->addDrawable(drawable);
647 } else {
648 size += SkMatrixPriv::WriteToMemory(*matrix, nullptr); // matrix
649 initialOffset = this->addDraw(DRAW_DRAWABLE_MATRIX, &size);
650 this->addMatrix(*matrix);
651 this->addDrawable(drawable);
652 }
653 this->validate(initialOffset, size);
654 }
655
656 void SkPictureRecord::onDrawVerticesObject(const SkVertices* vertices,
657 SkBlendMode mode, const SkPaint& paint) {
658 // op + paint index + vertices index + zero_bones + mode
659 size_t size = 5 * kUInt32Size;
660 size_t initialOffset = this->addDraw(DRAW_VERTICES_OBJECT, &size);
661
662 this->addPaint(paint);
663 this->addVertices(vertices);
664 this->addInt(0); // legacy bone count
665 this->addInt(static_cast<uint32_t>(mode));
666
667 this->validate(initialOffset, size);
668 }
669
670 void SkPictureRecord::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
671 const SkPoint texCoords[4], SkBlendMode bmode,
672 const SkPaint& paint) {
673 // op + paint index + patch 12 control points + flag + patch 4 colors + 4 texture coordinates
674 size_t size = 2 * kUInt32Size + SkPatchUtils::kNumCtrlPts * sizeof(SkPoint) + kUInt32Size;
675 uint32_t flag = 0;
676 if (colors) {
677 flag |= DRAW_VERTICES_HAS_COLORS;
678 size += SkPatchUtils::kNumCorners * sizeof(SkColor);
679 }
680 if (texCoords) {
681 flag |= DRAW_VERTICES_HAS_TEXS;
682 size += SkPatchUtils::kNumCorners * sizeof(SkPoint);
683 }
684 if (SkBlendMode::kModulate != bmode) {
685 flag |= DRAW_VERTICES_HAS_XFER;
686 size += kUInt32Size;
687 }
688
689 size_t initialOffset = this->addDraw(DRAW_PATCH, &size);
690 this->addPaint(paint);
691 this->addPatch(cubics);
692 this->addInt(flag);
693
694 // write optional parameters
695 if (colors) {
696 fWriter.write(colors, SkPatchUtils::kNumCorners * sizeof(SkColor));
697 }
698 if (texCoords) {
699 fWriter.write(texCoords, SkPatchUtils::kNumCorners * sizeof(SkPoint));
700 }
701 if (flag & DRAW_VERTICES_HAS_XFER) {
702 this->addInt((int)bmode);
703 }
704 this->validate(initialOffset, size);
705 }
706
707 void SkPictureRecord::onDrawAtlas2(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
708 const SkColor colors[], int count, SkBlendMode mode,
709 const SkSamplingOptions& sampling, const SkRect* cull,
710 const SkPaint* paint) {
711 // [op + paint-index + atlas-index + flags + count] + [xform] + [tex] + [*colors + mode] + cull
712 size_t size = 5 * kUInt32Size + count * sizeof(SkRSXform) + count * sizeof(SkRect);
713 size += SkSamplingPriv::FlatSize(sampling);
714 uint32_t flags = 0;
715 if (colors) {
716 flags |= DRAW_ATLAS_HAS_COLORS;
717 size += count * sizeof(SkColor);
718 size += sizeof(uint32_t); // xfermode::mode
719 }
720 if (cull) {
721 flags |= DRAW_ATLAS_HAS_CULL;
722 size += sizeof(SkRect);
723 }
724 flags |= DRAW_ATLAS_HAS_SAMPLING;
725
726 size_t initialOffset = this->addDraw(DRAW_ATLAS, &size);
727 this->addPaintPtr(paint);
728 this->addImage(atlas);
729 this->addInt(flags);
730 this->addInt(count);
731 fWriter.write(xform, count * sizeof(SkRSXform));
732 fWriter.write(tex, count * sizeof(SkRect));
733
734 // write optional parameters
735 if (colors) {
736 fWriter.write(colors, count * sizeof(SkColor));
737 this->addInt((int)mode);
738 }
739 if (cull) {
740 fWriter.write(cull, sizeof(SkRect));
741 }
742 this->addSampling(sampling);
743 this->validate(initialOffset, size);
744 }
745
746 void SkPictureRecord::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
747 // op + path index + zParams + lightPos + lightRadius + spot/ambient alphas + color + flags
748 size_t size = 2 * kUInt32Size + 2 * sizeof(SkPoint3) + 1 * sizeof(SkScalar) + 3 * kUInt32Size;
749 size_t initialOffset = this->addDraw(DRAW_SHADOW_REC, &size);
750
751 this->addPath(path);
752
753 fWriter.writePoint3(rec.fZPlaneParams);
754 fWriter.writePoint3(rec.fLightPos);
755 fWriter.writeScalar(rec.fLightRadius);
756 fWriter.write32(rec.fAmbientColor);
757 fWriter.write32(rec.fSpotColor);
758 fWriter.write32(rec.fFlags);
759
760 this->validate(initialOffset, size);
761 }
762
763 void SkPictureRecord::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
764 size_t keyLen = SkWriter32::WriteStringSize(key);
765 size_t valueLen = SkWriter32::WriteDataSize(value);
766 size_t size = 4 + sizeof(SkRect) + keyLen + valueLen;
767
768 size_t initialOffset = this->addDraw(DRAW_ANNOTATION, &size);
769 this->addRect(rect);
770 fWriter.writeString(key);
771 fWriter.writeData(value);
772 this->validate(initialOffset, size);
773 }
774
775 void SkPictureRecord::onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
776 SkCanvas::QuadAAFlags aa, const SkColor4f& color,
777 SkBlendMode mode) {
778
779 // op + rect + aa flags + color + mode + hasClip(as int) + clipCount*points
780 size_t size = 4 * kUInt32Size + sizeof(SkColor4f) + sizeof(rect) +
781 (clip ? 4 : 0) * sizeof(SkPoint);
782 size_t initialOffset = this->addDraw(DRAW_EDGEAA_QUAD, &size);
783 this->addRect(rect);
784 this->addInt((int) aa);
785 fWriter.write(&color, sizeof(SkColor4f));
786 this->addInt((int) mode);
787 this->addInt(clip != nullptr);
788 if (clip) {
789 this->addPoints(clip, 4);
790 }
791 this->validate(initialOffset, size);
792 }
793
794 void SkPictureRecord::onDrawEdgeAAImageSet2(const SkCanvas::ImageSetEntry set[], int count,
795 const SkPoint dstClips[],
796 const SkMatrix preViewMatrices[],
797 const SkSamplingOptions& sampling,
798 const SkPaint* paint,
799 SkCanvas::SrcRectConstraint constraint) {
800 static constexpr size_t kMatrixSize = 9 * sizeof(SkScalar); // *not* sizeof(SkMatrix)
801 // op + count + paint + constraint + (image index, src rect, dst rect, alpha, aa flags,
802 // hasClip(int), matrixIndex) * cnt + totalClipCount + dstClips + totalMatrixCount + matrices
803 int totalDstClipCount, totalMatrixCount;
804 SkCanvasPriv::GetDstClipAndMatrixCounts(set, count, &totalDstClipCount, &totalMatrixCount);
805
806 size_t size = 6 * kUInt32Size + sizeof(SkPoint) * totalDstClipCount +
807 kMatrixSize * totalMatrixCount +
808 (4 * kUInt32Size + 2 * sizeof(SkRect) + sizeof(SkScalar)) * count +
809 SkSamplingPriv::FlatSize(sampling);
810 size_t initialOffset = this->addDraw(DRAW_EDGEAA_IMAGE_SET2, &size);
811 this->addInt(count);
812 this->addPaintPtr(paint);
813 this->addSampling(sampling);
814 this->addInt((int) constraint);
815 for (int i = 0; i < count; ++i) {
816 this->addImage(set[i].fImage.get());
817 this->addRect(set[i].fSrcRect);
818 this->addRect(set[i].fDstRect);
819 this->addInt(set[i].fMatrixIndex);
820 this->addScalar(set[i].fAlpha);
821 this->addInt((int)set[i].fAAFlags);
822 this->addInt(set[i].fHasClip);
823 }
824 this->addInt(totalDstClipCount);
825 this->addPoints(dstClips, totalDstClipCount);
826 this->addInt(totalMatrixCount);
827 for (int i = 0; i < totalMatrixCount; ++i) {
828 this->addMatrix(preViewMatrices[i]);
829 }
830 this->validate(initialOffset, size);
831 }
832
833 ///////////////////////////////////////////////////////////////////////////////
834
835 // De-duping helper.
836
837 template <typename T>
838 static bool equals(T* a, T* b) { return a->uniqueID() == b->uniqueID(); }
839
840 template <>
841 bool equals(SkDrawable* a, SkDrawable* b) {
842 // SkDrawable's generationID is not a stable unique identifier.
843 return a == b;
844 }
845
846 template <typename T>
847 static int find_or_append(TArray<sk_sp<T>>& array, T* obj) {
848 for (int i = 0; i < array.size(); i++) {
849 if (equals(array[i].get(), obj)) {
850 return i;
851 }
852 }
853
854 array.push_back(sk_ref_sp(obj));
855
856 return array.size() - 1;
857 }
858
859 sk_sp<SkSurface> SkPictureRecord::onNewSurface(const SkImageInfo& info, const SkSurfaceProps&) {
860 return nullptr;
861 }
862
863 void SkPictureRecord::addImage(const SkImage* image) {
864 // convention for images is 0-based index
865 this->addInt(find_or_append(fImages, image));
866 }
867
868 void SkPictureRecord::addMatrix(const SkMatrix& matrix) {
869 fWriter.writeMatrix(matrix);
870 }
871
872 void SkPictureRecord::addPaintPtr(const SkPaint* paint) {
873 if (paint) {
874 fPaints.push_back(*paint);
875 this->addInt(fPaints.size());
876 } else {
877 this->addInt(0);
878 }
879 }
880
881 int SkPictureRecord::addPathToHeap(const SkPath& path) {
882 if (int* n = fPaths.find(path)) {
883 return *n;
884 }
885 int n = fPaths.count() + 1; // 0 is reserved for null / error.
886 fPaths.set(path, n);
887 return n;
888 }
889
890 void SkPictureRecord::addPath(const SkPath& path) {
891 this->addInt(this->addPathToHeap(path));
892 }
893
894 void SkPictureRecord::addPatch(const SkPoint cubics[12]) {
895 fWriter.write(cubics, SkPatchUtils::kNumCtrlPts * sizeof(SkPoint));
896 }
897
898 void SkPictureRecord::addPicture(const SkPicture* picture) {
899 // follow the convention of recording a 1-based index
900 this->addInt(find_or_append(fPictures, picture) + 1);
901 }
902
903 void SkPictureRecord::addDrawable(SkDrawable* drawable) {
904 // follow the convention of recording a 1-based index
905 this->addInt(find_or_append(fDrawables, drawable) + 1);
906 }
907
908 void SkPictureRecord::addPoint(const SkPoint& point) {
909 fWriter.writePoint(point);
910 }
911
912 void SkPictureRecord::addPoints(const SkPoint pts[], int count) {
913 fWriter.writeMul4(pts, count * sizeof(SkPoint));
914 }
915
916 void SkPictureRecord::addNoOp() {
917 size_t size = kUInt32Size; // op
918 this->addDraw(NOOP, &size);
919 }
920
921 void SkPictureRecord::addRect(const SkRect& rect) {
922 fWriter.writeRect(rect);
923 }
924
925 void SkPictureRecord::addRectPtr(const SkRect* rect) {
926 if (fWriter.writeBool(rect != nullptr)) {
927 fWriter.writeRect(*rect);
928 }
929 }
930
931 void SkPictureRecord::addIRect(const SkIRect& rect) {
932 fWriter.write(&rect, sizeof(rect));
933 }
934
935 void SkPictureRecord::addIRectPtr(const SkIRect* rect) {
936 if (fWriter.writeBool(rect != nullptr)) {
937 *(SkIRect*)fWriter.reserve(sizeof(SkIRect)) = *rect;
938 }
939 }
940
941 void SkPictureRecord::addRRect(const SkRRect& rrect) {
942 fWriter.writeRRect(rrect);
943 }
944
945 void SkPictureRecord::addRegion(const SkRegion& region) {
946 fWriter.writeRegion(region);
947 }
948
949 void SkPictureRecord::addSampling(const SkSamplingOptions& sampling) {
950 fWriter.writeSampling(sampling);
951 }
952
953 void SkPictureRecord::addText(const void* text, size_t byteLength) {
954 addInt(SkToInt(byteLength));
955 fWriter.writePad(text, byteLength);
956 }
957
958 void SkPictureRecord::addTextBlob(const SkTextBlob* blob) {
959 // follow the convention of recording a 1-based index
960 this->addInt(find_or_append(fTextBlobs, blob) + 1);
961 }
962
963 void SkPictureRecord::addSlug(const sktext::gpu::Slug* slug) {
964 // follow the convention of recording a 1-based index
965 this->addInt(find_or_append(fSlugs, slug) + 1);
966 }
967
968 void SkPictureRecord::addVertices(const SkVertices* vertices) {
969 // follow the convention of recording a 1-based index
970 this->addInt(find_or_append(fVertices, vertices) + 1);
971 }