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(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__anona4d55b020111::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__anona4d55b020111::Save79 void draw(SkCanvas* c, const SkMatrix&) const { c->save(); }
80 };
81 struct Restore final : Op {
82 static const auto kType = Type::Restore;
draw__anona4d55b020111::Restore83 void draw(SkCanvas* c, const SkMatrix&) const { c->restore(); }
84 };
85 struct SaveLayer final : Op {
86 static const auto kType = Type::SaveLayer;
SaveLayer__anona4d55b020111::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__anona4d55b020111::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__anona4d55b020111::SaveBehind110 SaveBehind(const SkRect* subset) {
111 if (subset) { this->subset = *subset; }
112 }
113 SkRect subset = kUnset;
draw__anona4d55b020111::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__anona4d55b020111::Concat120 Concat(const SkMatrix& matrix) : matrix(matrix) {}
121 SkMatrix matrix;
draw__anona4d55b020111::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__anona4d55b020111::SetMatrix126 SetMatrix(const SkMatrix& matrix) : matrix(matrix) {}
127 SkMatrix matrix;
draw__anona4d55b020111::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__anona4d55b020111::Translate134 Translate(SkScalar dx, SkScalar dy) : dx(dx), dy(dy) {}
135 SkScalar dx,dy;
draw__anona4d55b020111::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__anona4d55b020111::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__anona4d55b020111::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__anona4d55b020111::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__anona4d55b020111::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__anona4d55b020111::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__anona4d55b020111::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__anona4d55b020111::ClipRegion167 ClipRegion(const SkRegion& region, SkClipOp op) : region(region), op(op) {}
168 SkRegion region;
169 SkClipOp op;
draw__anona4d55b020111::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__anona4d55b020111::DrawPaint175 DrawPaint(const SkPaint& paint) : paint(paint) {}
176 SkPaint paint;
draw__anona4d55b020111::DrawPaint177 void draw(SkCanvas* c, const SkMatrix&) const { c->drawPaint(paint); }
178 };
179 struct DrawPath final : Op {
180 static const auto kType = Type::DrawPath;
DrawPath__anona4d55b020111::DrawPath181 DrawPath(const SkPath& path, const SkPaint& paint) : path(path), paint(paint) {}
182 SkPath path;
183 SkPaint paint;
draw__anona4d55b020111::DrawPath184 void draw(SkCanvas* c, const SkMatrix&) const { c->drawPath(path, paint); }
185 };
186 struct DrawRect final : Op {
187 static const auto kType = Type::DrawRect;
DrawRect__anona4d55b020111::DrawRect188 DrawRect(const SkRect& rect, const SkPaint& paint) : rect(rect), paint(paint) {}
189 SkRect rect;
190 SkPaint paint;
draw__anona4d55b020111::DrawRect191 void draw(SkCanvas* c, const SkMatrix&) const { c->drawRect(rect, paint); }
192 };
193 struct DrawEdgeAARect final : Op {
194 static const auto kType = Type::DrawEdgeAARect;
DrawEdgeAARect__anona4d55b020111::DrawEdgeAARect195 DrawEdgeAARect(const SkRect& rect, SkCanvas::QuadAAFlags aa, SkColor color,
196 SkBlendMode mode)
197 : rect(rect), aa(aa), color(color), mode(mode) {}
198 SkRect rect;
199 SkCanvas::QuadAAFlags aa;
200 SkColor color;
201 SkBlendMode mode;
draw__anona4d55b020111::DrawEdgeAARect202 void draw(SkCanvas* c, const SkMatrix&) const {
203 c->experimental_DrawEdgeAARectV1(rect, aa, color, mode);
204 }
205 };
206 struct DrawRegion final : Op {
207 static const auto kType = Type::DrawRegion;
DrawRegion__anona4d55b020111::DrawRegion208 DrawRegion(const SkRegion& region, const SkPaint& paint) : region(region), paint(paint) {}
209 SkRegion region;
210 SkPaint paint;
draw__anona4d55b020111::DrawRegion211 void draw(SkCanvas* c, const SkMatrix&) const { c->drawRegion(region, paint); }
212 };
213 struct DrawOval final : Op {
214 static const auto kType = Type::DrawOval;
DrawOval__anona4d55b020111::DrawOval215 DrawOval(const SkRect& oval, const SkPaint& paint) : oval(oval), paint(paint) {}
216 SkRect oval;
217 SkPaint paint;
draw__anona4d55b020111::DrawOval218 void draw(SkCanvas* c, const SkMatrix&) const { c->drawOval(oval, paint); }
219 };
220 struct DrawArc final : Op {
221 static const auto kType = Type::DrawArc;
DrawArc__anona4d55b020111::DrawArc222 DrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
223 const SkPaint& paint)
224 : oval(oval), startAngle(startAngle), sweepAngle(sweepAngle), useCenter(useCenter)
225 , paint(paint) {}
226 SkRect oval;
227 SkScalar startAngle;
228 SkScalar sweepAngle;
229 bool useCenter;
230 SkPaint paint;
draw__anona4d55b020111::DrawArc231 void draw(SkCanvas* c, const SkMatrix&) const { c->drawArc(oval, startAngle, sweepAngle,
232 useCenter, paint); }
233 };
234 struct DrawRRect final : Op {
235 static const auto kType = Type::DrawRRect;
DrawRRect__anona4d55b020111::DrawRRect236 DrawRRect(const SkRRect& rrect, const SkPaint& paint) : rrect(rrect), paint(paint) {}
237 SkRRect rrect;
238 SkPaint paint;
draw__anona4d55b020111::DrawRRect239 void draw(SkCanvas* c, const SkMatrix&) const { c->drawRRect(rrect, paint); }
240 };
241 struct DrawDRRect final : Op {
242 static const auto kType = Type::DrawDRRect;
DrawDRRect__anona4d55b020111::DrawDRRect243 DrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint)
244 : outer(outer), inner(inner), paint(paint) {}
245 SkRRect outer, inner;
246 SkPaint paint;
draw__anona4d55b020111::DrawDRRect247 void draw(SkCanvas* c, const SkMatrix&) const { c->drawDRRect(outer, inner, paint); }
248 };
249
250 struct DrawAnnotation final : Op {
251 static const auto kType = Type::DrawAnnotation;
DrawAnnotation__anona4d55b020111::DrawAnnotation252 DrawAnnotation(const SkRect& rect, SkData* value) : rect(rect), value(sk_ref_sp(value)) {}
253 SkRect rect;
254 sk_sp<SkData> value;
draw__anona4d55b020111::DrawAnnotation255 void draw(SkCanvas* c, const SkMatrix&) const {
256 c->drawAnnotation(rect, pod<char>(this), value.get());
257 }
258 };
259 struct DrawDrawable final : Op {
260 static const auto kType = Type::DrawDrawable;
DrawDrawable__anona4d55b020111::DrawDrawable261 DrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) : drawable(sk_ref_sp(drawable)) {
262 if (matrix) { this->matrix = *matrix; }
263 }
264 sk_sp<SkDrawable> drawable;
265 SkMatrix matrix = SkMatrix::I();
draw__anona4d55b020111::DrawDrawable266 void draw(SkCanvas* c, const SkMatrix&) const {
267 c->drawDrawable(drawable.get(), &matrix);
268 }
269 };
270 struct DrawPicture final : Op {
271 static const auto kType = Type::DrawPicture;
DrawPicture__anona4d55b020111::DrawPicture272 DrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint)
273 : picture(sk_ref_sp(picture)) {
274 if (matrix) { this->matrix = *matrix; }
275 if (paint) { this->paint = *paint; has_paint = true; }
276 }
277 sk_sp<const SkPicture> picture;
278 SkMatrix matrix = SkMatrix::I();
279 SkPaint paint;
280 bool has_paint = false; // TODO: why is a default paint not the same?
draw__anona4d55b020111::DrawPicture281 void draw(SkCanvas* c, const SkMatrix&) const {
282 c->drawPicture(picture.get(), &matrix, has_paint ? &paint : nullptr);
283 }
284 };
285
286 struct DrawImage final : Op {
287 static const auto kType = Type::DrawImage;
DrawImage__anona4d55b020111::DrawImage288 DrawImage(sk_sp<const SkImage>&& image, SkScalar x, SkScalar y, const SkPaint* paint)
289 : image(std::move(image)), x(x), y(y) {
290 if (paint) { this->paint = *paint; }
291 }
292 sk_sp<const SkImage> image;
293 SkScalar x,y;
294 SkPaint paint;
draw__anona4d55b020111::DrawImage295 void draw(SkCanvas* c, const SkMatrix&) const { c->drawImage(image.get(), x,y, &paint); }
296 };
297 struct DrawImageNine final : Op {
298 static const auto kType = Type::DrawImageNine;
DrawImageNine__anona4d55b020111::DrawImageNine299 DrawImageNine(sk_sp<const SkImage>&& image,
300 const SkIRect& center, const SkRect& dst, const SkPaint* paint)
301 : image(std::move(image)), center(center), dst(dst) {
302 if (paint) { this->paint = *paint; }
303 }
304 sk_sp<const SkImage> image;
305 SkIRect center;
306 SkRect dst;
307 SkPaint paint;
draw__anona4d55b020111::DrawImageNine308 void draw(SkCanvas* c, const SkMatrix&) const {
309 c->drawImageNine(image.get(), center, dst, &paint);
310 }
311 };
312 struct DrawImageRect final : Op {
313 static const auto kType = Type::DrawImageRect;
DrawImageRect__anona4d55b020111::DrawImageRect314 DrawImageRect(sk_sp<const SkImage>&& image, const SkRect* src, const SkRect& dst,
315 const SkPaint* paint, SkCanvas::SrcRectConstraint constraint)
316 : image(std::move(image)), dst(dst), constraint(constraint) {
317 this->src = src ? *src : SkRect::MakeIWH(image->width(), image->height());
318 if (paint) { this->paint = *paint; }
319 }
320 sk_sp<const SkImage> image;
321 SkRect src, dst;
322 SkPaint paint;
323 SkCanvas::SrcRectConstraint constraint;
draw__anona4d55b020111::DrawImageRect324 void draw(SkCanvas* c, const SkMatrix&) const {
325 c->drawImageRect(image.get(), src, dst, &paint, constraint);
326 }
327 };
328 struct DrawImageLattice final : Op {
329 static const auto kType = Type::DrawImageLattice;
DrawImageLattice__anona4d55b020111::DrawImageLattice330 DrawImageLattice(sk_sp<const SkImage>&& image, int xs, int ys, int fs,
331 const SkIRect& src, const SkRect& dst, const SkPaint* paint)
332 : image(std::move(image)), xs(xs), ys(ys), fs(fs), src(src), dst(dst) {
333 if (paint) { this->paint = *paint; }
334 }
335 sk_sp<const SkImage> image;
336 int xs, ys, fs;
337 SkIRect src;
338 SkRect dst;
339 SkPaint paint;
draw__anona4d55b020111::DrawImageLattice340 void draw(SkCanvas* c, const SkMatrix&) const {
341 auto xdivs = pod<int>(this, 0),
342 ydivs = pod<int>(this, xs*sizeof(int));
343 auto colors = (0 == fs) ? nullptr :
344 pod<SkColor>(this, (xs+ys)*sizeof(int));
345 auto flags = (0 == fs) ? nullptr :
346 pod<SkCanvas::Lattice::RectType>(this, (xs+ys)*sizeof(int)+
347 fs*sizeof(SkColor));
348 c->drawImageLattice(image.get(), {xdivs, ydivs, flags, xs, ys, &src, colors}, dst,
349 &paint);
350 }
351 };
352 struct DrawImageSet final : Op {
353 static const auto kType = Type::DrawImageSet;
DrawImageSet__anona4d55b020111::DrawImageSet354 DrawImageSet(const SkCanvas::ImageSetEntry set[], int count, SkFilterQuality quality,
355 SkBlendMode xfermode)
356 : count(count), quality(quality), xfermode(xfermode), set(count) {
357 std::copy_n(set, count, this->set.get());
358 }
359 int count;
360 SkFilterQuality quality;
361 SkBlendMode xfermode;
362 SkAutoTArray<SkCanvas::ImageSetEntry> set;
draw__anona4d55b020111::DrawImageSet363 void draw(SkCanvas* c, const SkMatrix&) const {
364 c->experimental_DrawImageSetV1(set.get(), count, quality, xfermode);
365 }
366 };
367 struct DrawTextBlob final : Op {
368 static const auto kType = Type::DrawTextBlob;
DrawTextBlob__anona4d55b020111::DrawTextBlob369 DrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint)
370 : blob(sk_ref_sp(blob)), x(x), y(y), paint(paint) {}
371 sk_sp<const SkTextBlob> blob;
372 SkScalar x,y;
373 SkPaint paint;
draw__anona4d55b020111::DrawTextBlob374 void draw(SkCanvas* c, const SkMatrix&) const {
375 c->drawTextBlob(blob.get(), x,y, paint);
376 }
377 };
378
379 struct DrawPatch final : Op {
380 static const auto kType = Type::DrawPatch;
DrawPatch__anona4d55b020111::DrawPatch381 DrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texs[4],
382 SkBlendMode bmode, const SkPaint& paint)
383 : xfermode(bmode), paint(paint)
384 {
385 copy_v(this->cubics, cubics, 12);
386 if (colors) { copy_v(this->colors, colors, 4); has_colors = true; }
387 if (texs ) { copy_v(this->texs , texs , 4); has_texs = true; }
388 }
389 SkPoint cubics[12];
390 SkColor colors[4];
391 SkPoint texs[4];
392 SkBlendMode xfermode;
393 SkPaint paint;
394 bool has_colors = false;
395 bool has_texs = false;
draw__anona4d55b020111::DrawPatch396 void draw(SkCanvas* c, const SkMatrix&) const {
397 c->drawPatch(cubics, has_colors ? colors : nullptr, has_texs ? texs : nullptr,
398 xfermode, paint);
399 }
400 };
401 struct DrawPoints final : Op {
402 static const auto kType = Type::DrawPoints;
DrawPoints__anona4d55b020111::DrawPoints403 DrawPoints(SkCanvas::PointMode mode, size_t count, const SkPaint& paint)
404 : mode(mode), count(count), paint(paint) {}
405 SkCanvas::PointMode mode;
406 size_t count;
407 SkPaint paint;
draw__anona4d55b020111::DrawPoints408 void draw(SkCanvas* c, const SkMatrix&) const {
409 c->drawPoints(mode, count, pod<SkPoint>(this), paint);
410 }
411 };
412 struct DrawVertices final : Op {
413 static const auto kType = Type::DrawVertices;
DrawVertices__anona4d55b020111::DrawVertices414 DrawVertices(const SkVertices* v, int bc, SkBlendMode m, const SkPaint& p)
415 : vertices(sk_ref_sp(const_cast<SkVertices*>(v)))
416 , boneCount(bc)
417 , mode(m)
418 , paint(p) {}
419 sk_sp<SkVertices> vertices;
420 int boneCount;
421 SkBlendMode mode;
422 SkPaint paint;
draw__anona4d55b020111::DrawVertices423 void draw(SkCanvas* c, const SkMatrix&) const {
424 c->drawVertices(vertices, pod<SkVertices::Bone>(this), boneCount, mode, paint);
425 }
426 };
427 struct DrawAtlas final : Op {
428 static const auto kType = Type::DrawAtlas;
DrawAtlas__anona4d55b020111::DrawAtlas429 DrawAtlas(const SkImage* atlas, int count, SkBlendMode xfermode,
430 const SkRect* cull, const SkPaint* paint, bool has_colors)
431 : atlas(sk_ref_sp(atlas)), count(count), xfermode(xfermode), has_colors(has_colors) {
432 if (cull) { this->cull = *cull; }
433 if (paint) { this->paint = *paint; }
434 }
435 sk_sp<const SkImage> atlas;
436 int count;
437 SkBlendMode xfermode;
438 SkRect cull = kUnset;
439 SkPaint paint;
440 bool has_colors;
draw__anona4d55b020111::DrawAtlas441 void draw(SkCanvas* c, const SkMatrix&) const {
442 auto xforms = pod<SkRSXform>(this, 0);
443 auto texs = pod<SkRect>(this, count*sizeof(SkRSXform));
444 auto colors = has_colors
445 ? pod<SkColor>(this, count*(sizeof(SkRSXform) + sizeof(SkRect)))
446 : nullptr;
447 c->drawAtlas(atlas.get(), xforms, texs, colors, count, xfermode,
448 maybe_unset(cull), &paint);
449 }
450 };
451 struct DrawShadowRec final : Op {
452 static const auto kType = Type::DrawShadowRec;
DrawShadowRec__anona4d55b020111::DrawShadowRec453 DrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec)
454 : fPath(path), fRec(rec)
455 {}
456 SkPath fPath;
457 SkDrawShadowRec fRec;
draw__anona4d55b020111::DrawShadowRec458 void draw(SkCanvas* c, const SkMatrix&) const {
459 c->private_draw_shadow_rec(fPath, fRec);
460 }
461 };
462 }
463
464 template <typename T, typename... Args>
push(size_t pod,Args &&...args)465 void* SkLiteDL::push(size_t pod, Args&&... args) {
466 size_t skip = SkAlignPtr(sizeof(T) + pod);
467 SkASSERT(skip < (1<<24));
468 if (fUsed + skip > fReserved) {
469 static_assert(SkIsPow2(SKLITEDL_PAGE), "This math needs updating for non-pow2.");
470 // Next greater multiple of SKLITEDL_PAGE.
471 fReserved = (fUsed + skip + SKLITEDL_PAGE) & ~(SKLITEDL_PAGE-1);
472 fBytes.realloc(fReserved);
473 }
474 SkASSERT(fUsed + skip <= fReserved);
475 auto op = (T*)(fBytes.get() + fUsed);
476 fUsed += skip;
477 new (op) T{ std::forward<Args>(args)... };
478 op->type = (uint32_t)T::kType;
479 op->skip = skip;
480 return op+1;
481 }
482
483 template <typename Fn, typename... Args>
map(const Fn fns[],Args...args) const484 inline void SkLiteDL::map(const Fn fns[], Args... args) const {
485 auto end = fBytes.get() + fUsed;
486 for (const uint8_t* ptr = fBytes.get(); ptr < end; ) {
487 auto op = (const Op*)ptr;
488 auto type = op->type;
489 auto skip = op->skip;
490 if (auto fn = fns[type]) { // We replace no-op functions with nullptrs
491 fn(op, args...); // to avoid the overhead of a pointless call.
492 }
493 ptr += skip;
494 }
495 }
496
flush()497 void SkLiteDL::flush() { this->push<Flush>(0); }
498
save()499 void SkLiteDL:: save() { this->push <Save>(0); }
restore()500 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)501 void SkLiteDL::saveLayer(const SkRect* bounds, const SkPaint* paint,
502 const SkImageFilter* backdrop, const SkImage* clipMask,
503 const SkMatrix* clipMatrix, SkCanvas::SaveLayerFlags flags) {
504 this->push<SaveLayer>(0, bounds, paint, backdrop, clipMask, clipMatrix, flags);
505 }
saveBehind(const SkRect * subset)506 void SkLiteDL::saveBehind(const SkRect* subset) {
507 this->push<SaveBehind>(0, subset);
508 }
509
concat(const SkMatrix & matrix)510 void SkLiteDL:: concat(const SkMatrix& matrix) { this->push <Concat>(0, matrix); }
setMatrix(const SkMatrix & matrix)511 void SkLiteDL::setMatrix(const SkMatrix& matrix) { this->push<SetMatrix>(0, matrix); }
translate(SkScalar dx,SkScalar dy)512 void SkLiteDL::translate(SkScalar dx, SkScalar dy) { this->push<Translate>(0, dx, dy); }
513
clipPath(const SkPath & path,SkClipOp op,bool aa)514 void SkLiteDL::clipPath(const SkPath& path, SkClipOp op, bool aa) {
515 this->push<ClipPath>(0, path, op, aa);
516 }
clipRect(const SkRect & rect,SkClipOp op,bool aa)517 void SkLiteDL::clipRect(const SkRect& rect, SkClipOp op, bool aa) {
518 this->push<ClipRect>(0, rect, op, aa);
519 }
clipRRect(const SkRRect & rrect,SkClipOp op,bool aa)520 void SkLiteDL::clipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
521 this->push<ClipRRect>(0, rrect, op, aa);
522 }
clipRegion(const SkRegion & region,SkClipOp op)523 void SkLiteDL::clipRegion(const SkRegion& region, SkClipOp op) {
524 this->push<ClipRegion>(0, region, op);
525 }
526
drawPaint(const SkPaint & paint)527 void SkLiteDL::drawPaint(const SkPaint& paint) {
528 this->push<DrawPaint>(0, paint);
529 }
drawPath(const SkPath & path,const SkPaint & paint)530 void SkLiteDL::drawPath(const SkPath& path, const SkPaint& paint) {
531 this->push<DrawPath>(0, path, paint);
532 }
drawRect(const SkRect & rect,const SkPaint & paint)533 void SkLiteDL::drawRect(const SkRect& rect, const SkPaint& paint) {
534 this->push<DrawRect>(0, rect, paint);
535 }
drawEdgeAARect(const SkRect & rect,SkCanvas::QuadAAFlags aa,SkColor color,SkBlendMode mode)536 void SkLiteDL::drawEdgeAARect(const SkRect& rect, SkCanvas::QuadAAFlags aa, SkColor color,
537 SkBlendMode mode) {
538 this->push<DrawEdgeAARect>(0, rect, aa, color, mode);
539 }
drawRegion(const SkRegion & region,const SkPaint & paint)540 void SkLiteDL::drawRegion(const SkRegion& region, const SkPaint& paint) {
541 this->push<DrawRegion>(0, region, paint);
542 }
drawOval(const SkRect & oval,const SkPaint & paint)543 void SkLiteDL::drawOval(const SkRect& oval, const SkPaint& paint) {
544 this->push<DrawOval>(0, oval, paint);
545 }
drawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)546 void SkLiteDL::drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
547 const SkPaint& paint) {
548 this->push<DrawArc>(0, oval, startAngle, sweepAngle, useCenter, paint);
549 }
drawRRect(const SkRRect & rrect,const SkPaint & paint)550 void SkLiteDL::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
551 this->push<DrawRRect>(0, rrect, paint);
552 }
drawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)553 void SkLiteDL::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
554 this->push<DrawDRRect>(0, outer, inner, paint);
555 }
556
drawAnnotation(const SkRect & rect,const char * key,SkData * value)557 void SkLiteDL::drawAnnotation(const SkRect& rect, const char* key, SkData* value) {
558 size_t bytes = strlen(key)+1;
559 void* pod = this->push<DrawAnnotation>(bytes, rect, value);
560 copy_v(pod, key,bytes);
561 }
drawDrawable(SkDrawable * drawable,const SkMatrix * matrix)562 void SkLiteDL::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
563 this->push<DrawDrawable>(0, drawable, matrix);
564 }
drawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)565 void SkLiteDL::drawPicture(const SkPicture* picture,
566 const SkMatrix* matrix, const SkPaint* paint) {
567 this->push<DrawPicture>(0, picture, matrix, paint);
568 }
drawImage(sk_sp<const SkImage> image,SkScalar x,SkScalar y,const SkPaint * paint)569 void SkLiteDL::drawImage(sk_sp<const SkImage> image, SkScalar x, SkScalar y, const SkPaint* paint) {
570 this->push<DrawImage>(0, std::move(image), x,y, paint);
571 }
drawImageNine(sk_sp<const SkImage> image,const SkIRect & center,const SkRect & dst,const SkPaint * paint)572 void SkLiteDL::drawImageNine(sk_sp<const SkImage> image, const SkIRect& center,
573 const SkRect& dst, const SkPaint* paint) {
574 this->push<DrawImageNine>(0, std::move(image), center, dst, paint);
575 }
drawImageRect(sk_sp<const SkImage> image,const SkRect * src,const SkRect & dst,const SkPaint * paint,SkCanvas::SrcRectConstraint constraint)576 void SkLiteDL::drawImageRect(sk_sp<const SkImage> image, const SkRect* src, const SkRect& dst,
577 const SkPaint* paint, SkCanvas::SrcRectConstraint constraint) {
578 this->push<DrawImageRect>(0, std::move(image), src, dst, paint, constraint);
579 }
drawImageLattice(sk_sp<const SkImage> image,const SkCanvas::Lattice & lattice,const SkRect & dst,const SkPaint * paint)580 void SkLiteDL::drawImageLattice(sk_sp<const SkImage> image, const SkCanvas::Lattice& lattice,
581 const SkRect& dst, const SkPaint* paint) {
582 int xs = lattice.fXCount, ys = lattice.fYCount;
583 int fs = lattice.fRectTypes ? (xs + 1) * (ys + 1) : 0;
584 size_t bytes = (xs + ys) * sizeof(int) + fs * sizeof(SkCanvas::Lattice::RectType)
585 + fs * sizeof(SkColor);
586 SkASSERT(lattice.fBounds);
587 void* pod = this->push<DrawImageLattice>(bytes, std::move(image), xs, ys, fs, *lattice.fBounds,
588 dst, paint);
589 copy_v(pod, lattice.fXDivs, xs,
590 lattice.fYDivs, ys,
591 lattice.fColors, fs,
592 lattice.fRectTypes, fs);
593 }
594
drawImageSet(const SkCanvas::ImageSetEntry set[],int count,SkFilterQuality filterQuality,SkBlendMode mode)595 void SkLiteDL::drawImageSet(const SkCanvas::ImageSetEntry set[], int count,
596 SkFilterQuality filterQuality, SkBlendMode mode) {
597 this->push<DrawImageSet>(0, set, count, filterQuality, mode);
598 }
599
drawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)600 void SkLiteDL::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) {
601 this->push<DrawTextBlob>(0, blob, x,y, paint);
602 }
603
drawPatch(const SkPoint points[12],const SkColor colors[4],const SkPoint texs[4],SkBlendMode bmode,const SkPaint & paint)604 void SkLiteDL::drawPatch(const SkPoint points[12], const SkColor colors[4], const SkPoint texs[4],
605 SkBlendMode bmode, const SkPaint& paint) {
606 this->push<DrawPatch>(0, points, colors, texs, bmode, paint);
607 }
drawPoints(SkCanvas::PointMode mode,size_t count,const SkPoint points[],const SkPaint & paint)608 void SkLiteDL::drawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint points[],
609 const SkPaint& paint) {
610 void* pod = this->push<DrawPoints>(count*sizeof(SkPoint), mode, count, paint);
611 copy_v(pod, points,count);
612 }
drawVertices(const SkVertices * vertices,const SkVertices::Bone bones[],int boneCount,SkBlendMode mode,const SkPaint & paint)613 void SkLiteDL::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[],
614 int boneCount, SkBlendMode mode, const SkPaint& paint) {
615 void* pod = this->push<DrawVertices>(boneCount * sizeof(SkVertices::Bone),
616 vertices,
617 boneCount,
618 mode,
619 paint);
620 copy_v(pod, bones, boneCount);
621 }
drawAtlas(const SkImage * atlas,const SkRSXform xforms[],const SkRect texs[],const SkColor colors[],int count,SkBlendMode xfermode,const SkRect * cull,const SkPaint * paint)622 void SkLiteDL::drawAtlas(const SkImage* atlas, const SkRSXform xforms[], const SkRect texs[],
623 const SkColor colors[], int count, SkBlendMode xfermode,
624 const SkRect* cull, const SkPaint* paint) {
625 size_t bytes = count*(sizeof(SkRSXform) + sizeof(SkRect));
626 if (colors) {
627 bytes += count*sizeof(SkColor);
628 }
629 void* pod = this->push<DrawAtlas>(bytes,
630 atlas, count, xfermode, cull, paint, colors != nullptr);
631 copy_v(pod, xforms, count,
632 texs, count,
633 colors, colors ? count : 0);
634 }
drawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)635 void SkLiteDL::drawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
636 this->push<DrawShadowRec>(0, path, rec);
637 }
638
639 typedef void(*draw_fn)(const void*, SkCanvas*, const SkMatrix&);
640 typedef void(*void_fn)(const void*);
641
642 // All ops implement draw().
643 #define M(T) [](const void* op, SkCanvas* c, const SkMatrix& original) { \
644 ((const T*)op)->draw(c, original); \
645 },
646 static const draw_fn draw_fns[] = { TYPES(M) };
647 #undef M
648
649 // Most state ops (matrix, clip, save, restore) have a trivial destructor.
650 #define M(T) !std::is_trivially_destructible<T>::value \
651 ? [](const void* op) { ((const T*)op)->~T(); } \
652 : (void_fn)nullptr,
653
654 static const void_fn dtor_fns[] = { TYPES(M) };
655 #undef M
656
draw(SkCanvas * canvas) const657 void SkLiteDL::draw(SkCanvas* canvas) const {
658 SkAutoCanvasRestore acr(canvas, false);
659 this->map(draw_fns, canvas, canvas->getTotalMatrix());
660 }
661
~SkLiteDL()662 SkLiteDL::~SkLiteDL() {
663 this->reset();
664 }
665
reset()666 void SkLiteDL::reset() {
667 this->map(dtor_fns);
668
669 // Leave fBytes and fReserved alone.
670 fUsed = 0;
671 }
672