• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkLiteDL.h"
9 #include <algorithm>
10 #include "SkCanvas.h"
11 #include "SkCanvasPriv.h"
12 #include "SkData.h"
13 #include "SkDrawShadowInfo.h"
14 #include "SkImage.h"
15 #include "SkImageFilter.h"
16 #include "SkMath.h"
17 #include "SkPicture.h"
18 #include "SkRSXform.h"
19 #include "SkRegion.h"
20 #include "SkTextBlob.h"
21 #include "SkVertices.h"
22 
23 #ifndef SKLITEDL_PAGE
24     #define SKLITEDL_PAGE 4096
25 #endif
26 
27 // A stand-in for an optional SkRect which was not set, e.g. bounds for a saveLayer().
28 static const SkRect kUnset = { SK_ScalarInfinity, 0,0,0};
maybe_unset(const SkRect & r)29 static const SkRect* maybe_unset(const SkRect& r) {
30     return r.left() == SK_ScalarInfinity ? nullptr : &r;
31 }
32 
33 // copy_v(dst, src,n, src,n, ...) copies an arbitrary number of typed srcs into dst.
copy_v(void * dst)34 static void copy_v(void* dst) {}
35 
36 template <typename S, typename... Rest>
copy_v(void * dst,const S * src,int n,Rest &&...rest)37 static void copy_v(void* dst, const S* src, int n, Rest&&... rest) {
38     SkASSERTF(((uintptr_t)dst & (alignof(S)-1)) == 0,
39               "Expected %p to be aligned for at least %zu bytes.", dst, alignof(S));
40     sk_careful_memcpy(dst, src, n*sizeof(S));
41     copy_v(SkTAddOffset<void>(dst, n*sizeof(S)), std::forward<Rest>(rest)...);
42 }
43 
44 // Helper for getting back at arrays which have been copy_v'd together after an Op.
45 template <typename D, typename T>
pod(const T * op,size_t offset=0)46 static const D* pod(const T* op, size_t offset = 0) {
47     return SkTAddOffset<const D>(op+1, offset);
48 }
49 
50 namespace {
51 #define TYPES(M)                                                                       \
52     M(Flush) M(Save) M(Restore) M(SaveLayer) M(SaveBehind)                             \
53     M(Concat) M(SetMatrix) M(Translate)                                                \
54     M(ClipPath) M(ClipRect) M(ClipRRect) M(ClipRegion)                                 \
55     M(DrawPaint) M(DrawBehind) M(DrawPath) M(DrawRect) M(DrawEdgeAARect)               \
56     M(DrawRegion) M(DrawOval) M(DrawArc)                                               \
57     M(DrawRRect) M(DrawDRRect) M(DrawAnnotation) M(DrawDrawable) M(DrawPicture)        \
58     M(DrawImage) M(DrawImageNine) M(DrawImageRect) M(DrawImageLattice) M(DrawImageSet) \
59     M(DrawTextBlob)                                                                    \
60     M(DrawPatch) M(DrawPoints) M(DrawVertices) M(DrawAtlas) M(DrawShadowRec)
61 
62 #define M(T) T,
63     enum class Type : uint8_t { TYPES(M) };
64 #undef M
65 
66     struct Op {
67         uint32_t type :  8;
68         uint32_t skip : 24;
69     };
70     static_assert(sizeof(Op) == 4, "");
71 
72     struct Flush final : Op {
73         static const auto kType = Type::Flush;
draw__anonc556bdcd0111::Flush74         void draw(SkCanvas* c, const SkMatrix&) const { c->flush(); }
75     };
76 
77     struct Save final : Op {
78         static const auto kType = Type::Save;
draw__anonc556bdcd0111::Save79         void draw(SkCanvas* c, const SkMatrix&) const { c->save(); }
80     };
81     struct Restore final : Op {
82         static const auto kType = Type::Restore;
draw__anonc556bdcd0111::Restore83         void draw(SkCanvas* c, const SkMatrix&) const { c->restore(); }
84     };
85     struct SaveLayer final : Op {
86         static const auto kType = Type::SaveLayer;
SaveLayer__anonc556bdcd0111::SaveLayer87         SaveLayer(const SkRect* bounds, const SkPaint* paint,
88                   const SkImageFilter* backdrop, const SkImage* clipMask,
89                   const SkMatrix* clipMatrix, SkCanvas::SaveLayerFlags flags) {
90             if (bounds) { this->bounds = *bounds; }
91             if (paint)  { this->paint  = *paint;  }
92             this->backdrop = sk_ref_sp(backdrop);
93             this->clipMask = sk_ref_sp(clipMask);
94             this->clipMatrix = clipMatrix ? *clipMatrix : SkMatrix::I();
95             this->flags = flags;
96         }
97         SkRect                     bounds = kUnset;
98         SkPaint                    paint;
99         sk_sp<const SkImageFilter> backdrop;
100         sk_sp<const SkImage>       clipMask;
101         SkMatrix                   clipMatrix;
102         SkCanvas::SaveLayerFlags   flags;
draw__anonc556bdcd0111::SaveLayer103         void draw(SkCanvas* c, const SkMatrix&) const {
104             c->saveLayer({ maybe_unset(bounds), &paint, backdrop.get(), clipMask.get(),
105                            clipMatrix.isIdentity() ? nullptr : &clipMatrix, flags });
106         }
107     };
108     struct SaveBehind final : Op {
109         static const auto kType = Type::SaveBehind;
SaveBehind__anonc556bdcd0111::SaveBehind110         SaveBehind(const SkRect* subset) {
111             if (subset) { this->subset = *subset; }
112         }
113         SkRect  subset = kUnset;
draw__anonc556bdcd0111::SaveBehind114         void draw(SkCanvas* c, const SkMatrix&) const {
115             SkCanvasPriv::SaveBehind(c, maybe_unset(subset));
116         }
117     };
118     struct Concat final : Op {
119         static const auto kType = Type::Concat;
Concat__anonc556bdcd0111::Concat120         Concat(const SkMatrix& matrix) : matrix(matrix) {}
121         SkMatrix matrix;
draw__anonc556bdcd0111::Concat122         void draw(SkCanvas* c, const SkMatrix&) const { c->concat(matrix); }
123     };
124     struct SetMatrix final : Op {
125         static const auto kType = Type::SetMatrix;
SetMatrix__anonc556bdcd0111::SetMatrix126         SetMatrix(const SkMatrix& matrix) : matrix(matrix) {}
127         SkMatrix matrix;
draw__anonc556bdcd0111::SetMatrix128         void draw(SkCanvas* c, const SkMatrix& original) const {
129             c->setMatrix(SkMatrix::Concat(original, matrix));
130         }
131     };
132     struct Translate final : Op {
133         static const auto kType = Type::Translate;
Translate__anonc556bdcd0111::Translate134         Translate(SkScalar dx, SkScalar dy) : dx(dx), dy(dy) {}
135         SkScalar dx,dy;
draw__anonc556bdcd0111::Translate136         void draw(SkCanvas* c, const SkMatrix&) const {
137             c->translate(dx, dy);
138         }
139     };
140 
141     struct ClipPath final : Op {
142         static const auto kType = Type::ClipPath;
ClipPath__anonc556bdcd0111::ClipPath143         ClipPath(const SkPath& path, SkClipOp op, bool aa) : path(path), op(op), aa(aa) {}
144         SkPath   path;
145         SkClipOp op;
146         bool     aa;
draw__anonc556bdcd0111::ClipPath147         void draw(SkCanvas* c, const SkMatrix&) const { c->clipPath(path, op, aa); }
148     };
149     struct ClipRect final : Op {
150         static const auto kType = Type::ClipRect;
ClipRect__anonc556bdcd0111::ClipRect151         ClipRect(const SkRect& rect, SkClipOp op, bool aa) : rect(rect), op(op), aa(aa) {}
152         SkRect   rect;
153         SkClipOp op;
154         bool     aa;
draw__anonc556bdcd0111::ClipRect155         void draw(SkCanvas* c, const SkMatrix&) const { c->clipRect(rect, op, aa); }
156     };
157     struct ClipRRect final : Op {
158         static const auto kType = Type::ClipRRect;
ClipRRect__anonc556bdcd0111::ClipRRect159         ClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) : rrect(rrect), op(op), aa(aa) {}
160         SkRRect  rrect;
161         SkClipOp op;
162         bool     aa;
draw__anonc556bdcd0111::ClipRRect163         void draw(SkCanvas* c, const SkMatrix&) const { c->clipRRect(rrect, op, aa); }
164     };
165     struct ClipRegion final : Op {
166         static const auto kType = Type::ClipRegion;
ClipRegion__anonc556bdcd0111::ClipRegion167         ClipRegion(const SkRegion& region, SkClipOp op) : region(region), op(op) {}
168         SkRegion region;
169         SkClipOp op;
draw__anonc556bdcd0111::ClipRegion170         void draw(SkCanvas* c, const SkMatrix&) const { c->clipRegion(region, op); }
171     };
172 
173     struct DrawPaint final : Op {
174         static const auto kType = Type::DrawPaint;
DrawPaint__anonc556bdcd0111::DrawPaint175         DrawPaint(const SkPaint& paint) : paint(paint) {}
176         SkPaint paint;
draw__anonc556bdcd0111::DrawPaint177         void draw(SkCanvas* c, const SkMatrix&) const { c->drawPaint(paint); }
178     };
179     struct DrawBehind final : Op {
180         static const auto kType = Type::DrawBehind;
DrawBehind__anonc556bdcd0111::DrawBehind181         DrawBehind(const SkPaint& paint) : paint(paint) {}
182         SkPaint paint;
draw__anonc556bdcd0111::DrawBehind183         void draw(SkCanvas* c, const SkMatrix&) const { SkCanvasPriv::DrawBehind(c, paint); }
184     };
185     struct DrawPath final : Op {
186         static const auto kType = Type::DrawPath;
DrawPath__anonc556bdcd0111::DrawPath187         DrawPath(const SkPath& path, const SkPaint& paint) : path(path), paint(paint) {}
188         SkPath  path;
189         SkPaint paint;
draw__anonc556bdcd0111::DrawPath190         void draw(SkCanvas* c, const SkMatrix&) const { c->drawPath(path, paint); }
191     };
192     struct DrawRect final : Op {
193         static const auto kType = Type::DrawRect;
DrawRect__anonc556bdcd0111::DrawRect194         DrawRect(const SkRect& rect, const SkPaint& paint) : rect(rect), paint(paint) {}
195         SkRect  rect;
196         SkPaint paint;
draw__anonc556bdcd0111::DrawRect197         void draw(SkCanvas* c, const SkMatrix&) const { c->drawRect(rect, paint); }
198     };
199     struct DrawEdgeAARect final : Op {
200         static const auto kType = Type::DrawEdgeAARect;
DrawEdgeAARect__anonc556bdcd0111::DrawEdgeAARect201         DrawEdgeAARect(const SkRect& rect, SkCanvas::QuadAAFlags aa, SkColor color,
202                        SkBlendMode mode)
203             : rect(rect), aa(aa), color(color), mode(mode) {}
204         SkRect rect;
205         SkCanvas::QuadAAFlags aa;
206         SkColor color;
207         SkBlendMode mode;
draw__anonc556bdcd0111::DrawEdgeAARect208         void draw(SkCanvas* c, const SkMatrix&) const {
209             c->experimental_DrawEdgeAARectV1(rect, aa, color, mode);
210         }
211     };
212     struct DrawRegion final : Op {
213         static const auto kType = Type::DrawRegion;
DrawRegion__anonc556bdcd0111::DrawRegion214         DrawRegion(const SkRegion& region, const SkPaint& paint) : region(region), paint(paint) {}
215         SkRegion region;
216         SkPaint  paint;
draw__anonc556bdcd0111::DrawRegion217         void draw(SkCanvas* c, const SkMatrix&) const { c->drawRegion(region, paint); }
218     };
219     struct DrawOval final : Op {
220         static const auto kType = Type::DrawOval;
DrawOval__anonc556bdcd0111::DrawOval221         DrawOval(const SkRect& oval, const SkPaint& paint) : oval(oval), paint(paint) {}
222         SkRect  oval;
223         SkPaint paint;
draw__anonc556bdcd0111::DrawOval224         void draw(SkCanvas* c, const SkMatrix&) const { c->drawOval(oval, paint); }
225     };
226     struct DrawArc final : Op {
227         static const auto kType = Type::DrawArc;
DrawArc__anonc556bdcd0111::DrawArc228         DrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
229                 const SkPaint& paint)
230             : oval(oval), startAngle(startAngle), sweepAngle(sweepAngle), useCenter(useCenter)
231             , paint(paint) {}
232         SkRect  oval;
233         SkScalar startAngle;
234         SkScalar sweepAngle;
235         bool useCenter;
236         SkPaint paint;
draw__anonc556bdcd0111::DrawArc237         void draw(SkCanvas* c, const SkMatrix&) const { c->drawArc(oval, startAngle, sweepAngle,
238                                                                    useCenter, paint); }
239     };
240     struct DrawRRect final : Op {
241         static const auto kType = Type::DrawRRect;
DrawRRect__anonc556bdcd0111::DrawRRect242         DrawRRect(const SkRRect& rrect, const SkPaint& paint) : rrect(rrect), paint(paint) {}
243         SkRRect rrect;
244         SkPaint paint;
draw__anonc556bdcd0111::DrawRRect245         void draw(SkCanvas* c, const SkMatrix&) const { c->drawRRect(rrect, paint); }
246     };
247     struct DrawDRRect final : Op {
248         static const auto kType = Type::DrawDRRect;
DrawDRRect__anonc556bdcd0111::DrawDRRect249         DrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint)
250             : outer(outer), inner(inner), paint(paint) {}
251         SkRRect outer, inner;
252         SkPaint paint;
draw__anonc556bdcd0111::DrawDRRect253         void draw(SkCanvas* c, const SkMatrix&) const { c->drawDRRect(outer, inner, paint); }
254     };
255 
256     struct DrawAnnotation final : Op {
257         static const auto kType = Type::DrawAnnotation;
DrawAnnotation__anonc556bdcd0111::DrawAnnotation258         DrawAnnotation(const SkRect& rect, SkData* value) : rect(rect), value(sk_ref_sp(value)) {}
259         SkRect        rect;
260         sk_sp<SkData> value;
draw__anonc556bdcd0111::DrawAnnotation261         void draw(SkCanvas* c, const SkMatrix&) const {
262             c->drawAnnotation(rect, pod<char>(this), value.get());
263         }
264     };
265     struct DrawDrawable final : Op {
266         static const auto kType = Type::DrawDrawable;
DrawDrawable__anonc556bdcd0111::DrawDrawable267         DrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) : drawable(sk_ref_sp(drawable)) {
268             if (matrix) { this->matrix = *matrix; }
269         }
270         sk_sp<SkDrawable> drawable;
271         SkMatrix          matrix = SkMatrix::I();
draw__anonc556bdcd0111::DrawDrawable272         void draw(SkCanvas* c, const SkMatrix&) const {
273             c->drawDrawable(drawable.get(), &matrix);
274         }
275     };
276     struct DrawPicture final : Op {
277         static const auto kType = Type::DrawPicture;
DrawPicture__anonc556bdcd0111::DrawPicture278         DrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint)
279             : picture(sk_ref_sp(picture)) {
280             if (matrix) { this->matrix = *matrix; }
281             if (paint)  { this->paint  = *paint; has_paint = true; }
282         }
283         sk_sp<const SkPicture> picture;
284         SkMatrix               matrix = SkMatrix::I();
285         SkPaint                paint;
286         bool                   has_paint = false;  // TODO: why is a default paint not the same?
draw__anonc556bdcd0111::DrawPicture287         void draw(SkCanvas* c, const SkMatrix&) const {
288             c->drawPicture(picture.get(), &matrix, has_paint ? &paint : nullptr);
289         }
290     };
291 
292     struct DrawImage final : Op {
293         static const auto kType = Type::DrawImage;
DrawImage__anonc556bdcd0111::DrawImage294         DrawImage(sk_sp<const SkImage>&& image, SkScalar x, SkScalar y, const SkPaint* paint)
295             : image(std::move(image)), x(x), y(y) {
296             if (paint) { this->paint = *paint; }
297         }
298         sk_sp<const SkImage> image;
299         SkScalar x,y;
300         SkPaint paint;
draw__anonc556bdcd0111::DrawImage301         void draw(SkCanvas* c, const SkMatrix&) const { c->drawImage(image.get(), x,y, &paint); }
302     };
303     struct DrawImageNine final : Op {
304         static const auto kType = Type::DrawImageNine;
DrawImageNine__anonc556bdcd0111::DrawImageNine305         DrawImageNine(sk_sp<const SkImage>&& image,
306                       const SkIRect& center, const SkRect& dst, const SkPaint* paint)
307             : image(std::move(image)), center(center), dst(dst) {
308             if (paint) { this->paint = *paint; }
309         }
310         sk_sp<const SkImage> image;
311         SkIRect center;
312         SkRect  dst;
313         SkPaint paint;
draw__anonc556bdcd0111::DrawImageNine314         void draw(SkCanvas* c, const SkMatrix&) const {
315             c->drawImageNine(image.get(), center, dst, &paint);
316         }
317     };
318     struct DrawImageRect final : Op {
319         static const auto kType = Type::DrawImageRect;
DrawImageRect__anonc556bdcd0111::DrawImageRect320         DrawImageRect(sk_sp<const SkImage>&& image, const SkRect* src, const SkRect& dst,
321                       const SkPaint* paint, SkCanvas::SrcRectConstraint constraint)
322             : image(std::move(image)), dst(dst), constraint(constraint) {
323             this->src = src ? *src : SkRect::MakeIWH(this->image->width(), this->image->height());
324             if (paint) { this->paint = *paint; }
325         }
326         sk_sp<const SkImage> image;
327         SkRect src, dst;
328         SkPaint paint;
329         SkCanvas::SrcRectConstraint constraint;
draw__anonc556bdcd0111::DrawImageRect330         void draw(SkCanvas* c, const SkMatrix&) const {
331             c->drawImageRect(image.get(), src, dst, &paint, constraint);
332         }
333     };
334     struct DrawImageLattice final : Op {
335         static const auto kType = Type::DrawImageLattice;
DrawImageLattice__anonc556bdcd0111::DrawImageLattice336         DrawImageLattice(sk_sp<const SkImage>&& image, int xs, int ys, int fs,
337                          const SkIRect& src, const SkRect& dst, const SkPaint* paint)
338             : image(std::move(image)), xs(xs), ys(ys), fs(fs), src(src), dst(dst) {
339             if (paint) { this->paint = *paint; }
340         }
341         sk_sp<const SkImage> image;
342         int                  xs, ys, fs;
343         SkIRect              src;
344         SkRect               dst;
345         SkPaint              paint;
draw__anonc556bdcd0111::DrawImageLattice346         void draw(SkCanvas* c, const SkMatrix&) const {
347             auto xdivs = pod<int>(this, 0),
348                  ydivs = pod<int>(this, xs*sizeof(int));
349             auto colors = (0 == fs) ? nullptr :
350                           pod<SkColor>(this, (xs+ys)*sizeof(int));
351             auto flags = (0 == fs) ? nullptr :
352                          pod<SkCanvas::Lattice::RectType>(this, (xs+ys)*sizeof(int)+
353                                                           fs*sizeof(SkColor));
354             c->drawImageLattice(image.get(), {xdivs, ydivs, flags, xs, ys, &src, colors}, dst,
355                                 &paint);
356         }
357     };
358     struct DrawImageSet final : Op {
359         static const auto kType = Type::DrawImageSet;
DrawImageSet__anonc556bdcd0111::DrawImageSet360         DrawImageSet(const SkCanvas::ImageSetEntry set[], int count, SkFilterQuality quality,
361                      SkBlendMode xfermode)
362                 : count(count), quality(quality), xfermode(xfermode), set(count) {
363             std::copy_n(set, count, this->set.get());
364         }
365         int                                   count;
366         SkFilterQuality                       quality;
367         SkBlendMode                           xfermode;
368         SkAutoTArray<SkCanvas::ImageSetEntry> set;
draw__anonc556bdcd0111::DrawImageSet369         void draw(SkCanvas* c, const SkMatrix&) const {
370             c->experimental_DrawImageSetV1(set.get(), count, quality, xfermode);
371         }
372     };
373     struct DrawTextBlob final : Op {
374         static const auto kType = Type::DrawTextBlob;
DrawTextBlob__anonc556bdcd0111::DrawTextBlob375         DrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint)
376             : blob(sk_ref_sp(blob)), x(x), y(y), paint(paint) {}
377         sk_sp<const SkTextBlob> blob;
378         SkScalar x,y;
379         SkPaint paint;
draw__anonc556bdcd0111::DrawTextBlob380         void draw(SkCanvas* c, const SkMatrix&) const {
381             c->drawTextBlob(blob.get(), x,y, paint);
382         }
383     };
384 
385     struct DrawPatch final : Op {
386         static const auto kType = Type::DrawPatch;
DrawPatch__anonc556bdcd0111::DrawPatch387         DrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texs[4],
388                   SkBlendMode bmode, const SkPaint& paint)
389             : xfermode(bmode), paint(paint)
390         {
391             copy_v(this->cubics, cubics, 12);
392             if (colors) { copy_v(this->colors, colors, 4); has_colors = true; }
393             if (texs  ) { copy_v(this->texs  , texs  , 4); has_texs   = true; }
394         }
395         SkPoint           cubics[12];
396         SkColor           colors[4];
397         SkPoint           texs[4];
398         SkBlendMode       xfermode;
399         SkPaint           paint;
400         bool              has_colors = false;
401         bool              has_texs   = false;
draw__anonc556bdcd0111::DrawPatch402         void draw(SkCanvas* c, const SkMatrix&) const {
403             c->drawPatch(cubics, has_colors ? colors : nullptr, has_texs ? texs : nullptr,
404                          xfermode, paint);
405         }
406     };
407     struct DrawPoints final : Op {
408         static const auto kType = Type::DrawPoints;
DrawPoints__anonc556bdcd0111::DrawPoints409         DrawPoints(SkCanvas::PointMode mode, size_t count, const SkPaint& paint)
410             : mode(mode), count(count), paint(paint) {}
411         SkCanvas::PointMode mode;
412         size_t              count;
413         SkPaint             paint;
draw__anonc556bdcd0111::DrawPoints414         void draw(SkCanvas* c, const SkMatrix&) const {
415             c->drawPoints(mode, count, pod<SkPoint>(this), paint);
416         }
417     };
418     struct DrawVertices final : Op {
419         static const auto kType = Type::DrawVertices;
DrawVertices__anonc556bdcd0111::DrawVertices420         DrawVertices(const SkVertices* v, int bc, SkBlendMode m, const SkPaint& p)
421             : vertices(sk_ref_sp(const_cast<SkVertices*>(v)))
422             , boneCount(bc)
423             , mode(m)
424             , paint(p) {}
425         sk_sp<SkVertices> vertices;
426         int boneCount;
427         SkBlendMode mode;
428         SkPaint paint;
draw__anonc556bdcd0111::DrawVertices429         void draw(SkCanvas* c, const SkMatrix&) const {
430             c->drawVertices(vertices, pod<SkVertices::Bone>(this), boneCount, mode, paint);
431         }
432     };
433     struct DrawAtlas final : Op {
434         static const auto kType = Type::DrawAtlas;
DrawAtlas__anonc556bdcd0111::DrawAtlas435         DrawAtlas(const SkImage* atlas, int count, SkBlendMode xfermode,
436                   const SkRect* cull, const SkPaint* paint, bool has_colors)
437             : atlas(sk_ref_sp(atlas)), count(count), xfermode(xfermode), has_colors(has_colors) {
438             if (cull)  { this->cull  = *cull; }
439             if (paint) { this->paint = *paint; }
440         }
441         sk_sp<const SkImage> atlas;
442         int                  count;
443         SkBlendMode          xfermode;
444         SkRect               cull = kUnset;
445         SkPaint              paint;
446         bool                 has_colors;
draw__anonc556bdcd0111::DrawAtlas447         void draw(SkCanvas* c, const SkMatrix&) const {
448             auto xforms = pod<SkRSXform>(this, 0);
449             auto   texs = pod<SkRect>(this, count*sizeof(SkRSXform));
450             auto colors = has_colors
451                 ? pod<SkColor>(this, count*(sizeof(SkRSXform) + sizeof(SkRect)))
452                 : nullptr;
453             c->drawAtlas(atlas.get(), xforms, texs, colors, count, xfermode,
454                          maybe_unset(cull), &paint);
455         }
456     };
457     struct DrawShadowRec final : Op {
458         static const auto kType = Type::DrawShadowRec;
DrawShadowRec__anonc556bdcd0111::DrawShadowRec459         DrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec)
460             : fPath(path), fRec(rec)
461         {}
462         SkPath          fPath;
463         SkDrawShadowRec fRec;
draw__anonc556bdcd0111::DrawShadowRec464         void draw(SkCanvas* c, const SkMatrix&) const {
465             c->private_draw_shadow_rec(fPath, fRec);
466         }
467     };
468 }
469 
470 template <typename T, typename... Args>
push(size_t pod,Args &&...args)471 void* SkLiteDL::push(size_t pod, Args&&... args) {
472     size_t skip = SkAlignPtr(sizeof(T) + pod);
473     SkASSERT(skip < (1<<24));
474     if (fUsed + skip > fReserved) {
475         static_assert(SkIsPow2(SKLITEDL_PAGE), "This math needs updating for non-pow2.");
476         // Next greater multiple of SKLITEDL_PAGE.
477         fReserved = (fUsed + skip + SKLITEDL_PAGE) & ~(SKLITEDL_PAGE-1);
478         fBytes.realloc(fReserved);
479     }
480     SkASSERT(fUsed + skip <= fReserved);
481     auto op = (T*)(fBytes.get() + fUsed);
482     fUsed += skip;
483     new (op) T{ std::forward<Args>(args)... };
484     op->type = (uint32_t)T::kType;
485     op->skip = skip;
486     return op+1;
487 }
488 
489 template <typename Fn, typename... Args>
map(const Fn fns[],Args...args) const490 inline void SkLiteDL::map(const Fn fns[], Args... args) const {
491     auto end = fBytes.get() + fUsed;
492     for (const uint8_t* ptr = fBytes.get(); ptr < end; ) {
493         auto op = (const Op*)ptr;
494         auto type = op->type;
495         auto skip = op->skip;
496         if (auto fn = fns[type]) {  // We replace no-op functions with nullptrs
497             fn(op, args...);        // to avoid the overhead of a pointless call.
498         }
499         ptr += skip;
500     }
501 }
502 
flush()503 void SkLiteDL::flush() { this->push<Flush>(0); }
504 
save()505 void SkLiteDL::   save() { this->push   <Save>(0); }
restore()506 void SkLiteDL::restore() { this->push<Restore>(0); }
saveLayer(const SkRect * bounds,const SkPaint * paint,const SkImageFilter * backdrop,const SkImage * clipMask,const SkMatrix * clipMatrix,SkCanvas::SaveLayerFlags flags)507 void SkLiteDL::saveLayer(const SkRect* bounds, const SkPaint* paint,
508                          const SkImageFilter* backdrop, const SkImage* clipMask,
509                          const SkMatrix* clipMatrix, SkCanvas::SaveLayerFlags flags) {
510     this->push<SaveLayer>(0, bounds, paint, backdrop, clipMask, clipMatrix, flags);
511 }
saveBehind(const SkRect * subset)512 void SkLiteDL::saveBehind(const SkRect* subset) {
513     this->push<SaveBehind>(0, subset);
514 }
515 
concat(const SkMatrix & matrix)516 void SkLiteDL::   concat(const SkMatrix& matrix)   { this->push   <Concat>(0, matrix); }
setMatrix(const SkMatrix & matrix)517 void SkLiteDL::setMatrix(const SkMatrix& matrix)   { this->push<SetMatrix>(0, matrix); }
translate(SkScalar dx,SkScalar dy)518 void SkLiteDL::translate(SkScalar dx, SkScalar dy) { this->push<Translate>(0, dx, dy); }
519 
clipPath(const SkPath & path,SkClipOp op,bool aa)520 void SkLiteDL::clipPath(const SkPath& path, SkClipOp op, bool aa) {
521     this->push<ClipPath>(0, path, op, aa);
522 }
clipRect(const SkRect & rect,SkClipOp op,bool aa)523 void SkLiteDL::clipRect(const SkRect& rect, SkClipOp op, bool aa) {
524     this->push<ClipRect>(0, rect, op, aa);
525 }
clipRRect(const SkRRect & rrect,SkClipOp op,bool aa)526 void SkLiteDL::clipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
527     this->push<ClipRRect>(0, rrect, op, aa);
528 }
clipRegion(const SkRegion & region,SkClipOp op)529 void SkLiteDL::clipRegion(const SkRegion& region, SkClipOp op) {
530     this->push<ClipRegion>(0, region, op);
531 }
532 
drawPaint(const SkPaint & paint)533 void SkLiteDL::drawPaint(const SkPaint& paint) {
534     this->push<DrawPaint>(0, paint);
535 }
drawBehind(const SkPaint & paint)536 void SkLiteDL::drawBehind(const SkPaint& paint) {
537     this->push<DrawBehind>(0, paint);
538 }
drawPath(const SkPath & path,const SkPaint & paint)539 void SkLiteDL::drawPath(const SkPath& path, const SkPaint& paint) {
540     this->push<DrawPath>(0, path, paint);
541 }
drawRect(const SkRect & rect,const SkPaint & paint)542 void SkLiteDL::drawRect(const SkRect& rect, const SkPaint& paint) {
543     this->push<DrawRect>(0, rect, paint);
544 }
drawEdgeAARect(const SkRect & rect,SkCanvas::QuadAAFlags aa,SkColor color,SkBlendMode mode)545 void SkLiteDL::drawEdgeAARect(const SkRect& rect, SkCanvas::QuadAAFlags aa, SkColor color,
546                               SkBlendMode mode) {
547     this->push<DrawEdgeAARect>(0, rect, aa, color, mode);
548 }
drawRegion(const SkRegion & region,const SkPaint & paint)549 void SkLiteDL::drawRegion(const SkRegion& region, const SkPaint& paint) {
550     this->push<DrawRegion>(0, region, paint);
551 }
drawOval(const SkRect & oval,const SkPaint & paint)552 void SkLiteDL::drawOval(const SkRect& oval, const SkPaint& paint) {
553     this->push<DrawOval>(0, oval, paint);
554 }
drawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)555 void SkLiteDL::drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
556                        const SkPaint& paint) {
557     this->push<DrawArc>(0, oval, startAngle, sweepAngle, useCenter, paint);
558 }
drawRRect(const SkRRect & rrect,const SkPaint & paint)559 void SkLiteDL::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
560     this->push<DrawRRect>(0, rrect, paint);
561 }
drawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)562 void SkLiteDL::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
563     this->push<DrawDRRect>(0, outer, inner, paint);
564 }
565 
drawAnnotation(const SkRect & rect,const char * key,SkData * value)566 void SkLiteDL::drawAnnotation(const SkRect& rect, const char* key, SkData* value) {
567     size_t bytes = strlen(key)+1;
568     void* pod = this->push<DrawAnnotation>(bytes, rect, value);
569     copy_v(pod, key,bytes);
570 }
drawDrawable(SkDrawable * drawable,const SkMatrix * matrix)571 void SkLiteDL::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
572     this->push<DrawDrawable>(0, drawable, matrix);
573 }
drawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)574 void SkLiteDL::drawPicture(const SkPicture* picture,
575                            const SkMatrix* matrix, const SkPaint* paint) {
576     this->push<DrawPicture>(0, picture, matrix, paint);
577 }
drawImage(sk_sp<const SkImage> image,SkScalar x,SkScalar y,const SkPaint * paint)578 void SkLiteDL::drawImage(sk_sp<const SkImage> image, SkScalar x, SkScalar y, const SkPaint* paint) {
579     this->push<DrawImage>(0, std::move(image), x,y, paint);
580 }
drawImageNine(sk_sp<const SkImage> image,const SkIRect & center,const SkRect & dst,const SkPaint * paint)581 void SkLiteDL::drawImageNine(sk_sp<const SkImage> image, const SkIRect& center,
582                              const SkRect& dst, const SkPaint* paint) {
583     this->push<DrawImageNine>(0, std::move(image), center, dst, paint);
584 }
drawImageRect(sk_sp<const SkImage> image,const SkRect * src,const SkRect & dst,const SkPaint * paint,SkCanvas::SrcRectConstraint constraint)585 void SkLiteDL::drawImageRect(sk_sp<const SkImage> image, const SkRect* src, const SkRect& dst,
586                              const SkPaint* paint, SkCanvas::SrcRectConstraint constraint) {
587     this->push<DrawImageRect>(0, std::move(image), src, dst, paint, constraint);
588 }
drawImageLattice(sk_sp<const SkImage> image,const SkCanvas::Lattice & lattice,const SkRect & dst,const SkPaint * paint)589 void SkLiteDL::drawImageLattice(sk_sp<const SkImage> image, const SkCanvas::Lattice& lattice,
590                                 const SkRect& dst, const SkPaint* paint) {
591     int xs = lattice.fXCount, ys = lattice.fYCount;
592     int fs = lattice.fRectTypes ? (xs + 1) * (ys + 1) : 0;
593     size_t bytes = (xs + ys) * sizeof(int) + fs * sizeof(SkCanvas::Lattice::RectType)
594                    + fs * sizeof(SkColor);
595     SkASSERT(lattice.fBounds);
596     void* pod = this->push<DrawImageLattice>(bytes, std::move(image), xs, ys, fs, *lattice.fBounds,
597                                              dst, paint);
598     copy_v(pod, lattice.fXDivs, xs,
599                 lattice.fYDivs, ys,
600                 lattice.fColors, fs,
601                 lattice.fRectTypes, fs);
602 }
603 
drawImageSet(const SkCanvas::ImageSetEntry set[],int count,SkFilterQuality filterQuality,SkBlendMode mode)604 void SkLiteDL::drawImageSet(const SkCanvas::ImageSetEntry set[], int count,
605                             SkFilterQuality filterQuality, SkBlendMode mode) {
606     this->push<DrawImageSet>(0, set, count, filterQuality, mode);
607 }
608 
drawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)609 void SkLiteDL::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) {
610     this->push<DrawTextBlob>(0, blob, x,y, paint);
611 }
612 
drawPatch(const SkPoint points[12],const SkColor colors[4],const SkPoint texs[4],SkBlendMode bmode,const SkPaint & paint)613 void SkLiteDL::drawPatch(const SkPoint points[12], const SkColor colors[4], const SkPoint texs[4],
614                          SkBlendMode bmode, const SkPaint& paint) {
615     this->push<DrawPatch>(0, points, colors, texs, bmode, paint);
616 }
drawPoints(SkCanvas::PointMode mode,size_t count,const SkPoint points[],const SkPaint & paint)617 void SkLiteDL::drawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint points[],
618                           const SkPaint& paint) {
619     void* pod = this->push<DrawPoints>(count*sizeof(SkPoint), mode, count, paint);
620     copy_v(pod, points,count);
621 }
drawVertices(const SkVertices * vertices,const SkVertices::Bone bones[],int boneCount,SkBlendMode mode,const SkPaint & paint)622 void SkLiteDL::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[],
623                             int boneCount, SkBlendMode mode, const SkPaint& paint) {
624     void* pod = this->push<DrawVertices>(boneCount * sizeof(SkVertices::Bone),
625                                          vertices,
626                                          boneCount,
627                                          mode,
628                                          paint);
629     copy_v(pod, bones, boneCount);
630 }
drawAtlas(const SkImage * atlas,const SkRSXform xforms[],const SkRect texs[],const SkColor colors[],int count,SkBlendMode xfermode,const SkRect * cull,const SkPaint * paint)631 void SkLiteDL::drawAtlas(const SkImage* atlas, const SkRSXform xforms[], const SkRect texs[],
632                          const SkColor colors[], int count, SkBlendMode xfermode,
633                          const SkRect* cull, const SkPaint* paint) {
634     size_t bytes = count*(sizeof(SkRSXform) + sizeof(SkRect));
635     if (colors) {
636         bytes += count*sizeof(SkColor);
637     }
638     void* pod = this->push<DrawAtlas>(bytes,
639                                       atlas, count, xfermode, cull, paint, colors != nullptr);
640     copy_v(pod, xforms, count,
641                   texs, count,
642                 colors, colors ? count : 0);
643 }
drawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)644 void SkLiteDL::drawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
645     this->push<DrawShadowRec>(0, path, rec);
646 }
647 
648 typedef void(*draw_fn)(const void*,  SkCanvas*, const SkMatrix&);
649 typedef void(*void_fn)(const void*);
650 
651 // All ops implement draw().
652 #define M(T) [](const void* op, SkCanvas* c, const SkMatrix& original) { \
653     ((const T*)op)->draw(c, original);                                         \
654 },
655 static const draw_fn draw_fns[] = { TYPES(M) };
656 #undef M
657 
658 // Most state ops (matrix, clip, save, restore) have a trivial destructor.
659 #define M(T) !std::is_trivially_destructible<T>::value \
660     ? [](const void* op) { ((const T*)op)->~T(); }     \
661     : (void_fn)nullptr,
662 
663 static const void_fn dtor_fns[] = { TYPES(M) };
664 #undef M
665 
draw(SkCanvas * canvas) const666 void SkLiteDL::draw(SkCanvas* canvas) const {
667     SkAutoCanvasRestore acr(canvas, false);
668     this->map(draw_fns, canvas, canvas->getTotalMatrix());
669 }
670 
~SkLiteDL()671 SkLiteDL::~SkLiteDL() {
672     this->reset();
673 }
674 
reset()675 void SkLiteDL::reset() {
676     this->map(dtor_fns);
677 
678     // Leave fBytes and fReserved alone.
679     fUsed   = 0;
680 }
681