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 "SkCanvas.h"
9 #include "SkData.h"
10 #include "SkDrawFilter.h"
11 #include "SkImageFilter.h"
12 #include "SkLiteDL.h"
13 #include "SkMath.h"
14 #include "SkPicture.h"
15 #include "SkRegion.h"
16 #include "SkRSXform.h"
17 #include "SkTextBlob.h"
18 #include "SkVertices.h"
19
20 #ifndef SKLITEDL_PAGE
21 #define SKLITEDL_PAGE 4096
22 #endif
23
24 // A stand-in for an optional SkRect which was not set, e.g. bounds for a saveLayer().
25 static const SkRect kUnset = { SK_ScalarInfinity, 0,0,0};
maybe_unset(const SkRect & r)26 static const SkRect* maybe_unset(const SkRect& r) {
27 return r.left() == SK_ScalarInfinity ? nullptr : &r;
28 }
29
30 // copy_v(dst, src,n, src,n, ...) copies an arbitrary number of typed srcs into dst.
copy_v(void * dst)31 static void copy_v(void* dst) {}
32
33 template <typename S, typename... Rest>
copy_v(void * dst,const S * src,int n,Rest &&...rest)34 static void copy_v(void* dst, const S* src, int n, Rest&&... rest) {
35 SkASSERTF(((uintptr_t)dst & (alignof(S)-1)) == 0,
36 "Expected %p to be aligned for at least %zu bytes.", dst, alignof(S));
37 sk_careful_memcpy(dst, src, n*sizeof(S));
38 copy_v(SkTAddOffset<void>(dst, n*sizeof(S)), std::forward<Rest>(rest)...);
39 }
40
41 // Helper for getting back at arrays which have been copy_v'd together after an Op.
42 template <typename D, typename T>
pod(T * op,size_t offset=0)43 static D* pod(T* op, size_t offset = 0) {
44 return SkTAddOffset<D>(op+1, offset);
45 }
46
47 namespace {
48 #define TYPES(M) \
49 M(SetDrawFilter) M(Save) M(Restore) M(SaveLayer) \
50 M(Concat) M(SetMatrix) M(Translate) M(TranslateZ) \
51 M(ClipPath) M(ClipRect) M(ClipRRect) M(ClipRegion) \
52 M(DrawPaint) M(DrawPath) M(DrawRect) M(DrawRegion) M(DrawOval) M(DrawArc) \
53 M(DrawRRect) M(DrawDRRect) M(DrawAnnotation) M(DrawDrawable) M(DrawPicture) \
54 M(DrawShadowedPicture) \
55 M(DrawImage) M(DrawImageNine) M(DrawImageRect) M(DrawImageLattice) \
56 M(DrawText) M(DrawPosText) M(DrawPosTextH) \
57 M(DrawTextOnPath) M(DrawTextRSXform) M(DrawTextBlob) \
58 M(DrawPatch) M(DrawPoints) M(DrawVertices) M(DrawAtlas)
59
60 #define M(T) T,
61 enum class Type : uint8_t { TYPES(M) };
62 #undef M
63
64 struct Op {
65 uint32_t type : 8;
66 uint32_t skip : 24;
67 };
68 static_assert(sizeof(Op) == 4, "");
69
70 struct SetDrawFilter final : Op {
71 #ifdef SK_SUPPORT_LEGACY_DRAWFILTER
72 static const auto kType = Type::SetDrawFilter;
SetDrawFilter__anon84fa9b240111::SetDrawFilter73 SetDrawFilter(SkDrawFilter* df) : drawFilter(sk_ref_sp(df)) {}
74 sk_sp<SkDrawFilter> drawFilter;
75 #endif
draw__anon84fa9b240111::SetDrawFilter76 void draw(SkCanvas* c, const SkMatrix&) {
77 #ifdef SK_SUPPORT_LEGACY_DRAWFILTER
78 c->setDrawFilter(drawFilter.get());
79 #endif
80 }
81 };
82
83 struct Save final : Op {
84 static const auto kType = Type::Save;
draw__anon84fa9b240111::Save85 void draw(SkCanvas* c, const SkMatrix&) { c->save(); }
86 };
87 struct Restore final : Op {
88 static const auto kType = Type::Restore;
draw__anon84fa9b240111::Restore89 void draw(SkCanvas* c, const SkMatrix&) { c->restore(); }
90 };
91 struct SaveLayer final : Op {
92 static const auto kType = Type::SaveLayer;
SaveLayer__anon84fa9b240111::SaveLayer93 SaveLayer(const SkRect* bounds, const SkPaint* paint,
94 const SkImageFilter* backdrop, SkCanvas::SaveLayerFlags flags) {
95 if (bounds) { this->bounds = *bounds; }
96 if (paint) { this->paint = *paint; }
97 this->backdrop = sk_ref_sp(backdrop);
98 this->flags = flags;
99 }
100 SkRect bounds = kUnset;
101 SkPaint paint;
102 sk_sp<const SkImageFilter> backdrop;
103 SkCanvas::SaveLayerFlags flags;
draw__anon84fa9b240111::SaveLayer104 void draw(SkCanvas* c, const SkMatrix&) {
105 c->saveLayer({ maybe_unset(bounds), &paint, backdrop.get(), flags });
106 }
107 };
108
109 struct Concat final : Op {
110 static const auto kType = Type::Concat;
Concat__anon84fa9b240111::Concat111 Concat(const SkMatrix& matrix) : matrix(matrix) {}
112 SkMatrix matrix;
draw__anon84fa9b240111::Concat113 void draw(SkCanvas* c, const SkMatrix&) { c->concat(matrix); }
114 };
115 struct SetMatrix final : Op {
116 static const auto kType = Type::SetMatrix;
SetMatrix__anon84fa9b240111::SetMatrix117 SetMatrix(const SkMatrix& matrix) : matrix(matrix) {}
118 SkMatrix matrix;
draw__anon84fa9b240111::SetMatrix119 void draw(SkCanvas* c, const SkMatrix& original) {
120 c->setMatrix(SkMatrix::Concat(original, matrix));
121 }
122 };
123 struct Translate final : Op {
124 static const auto kType = Type::Translate;
Translate__anon84fa9b240111::Translate125 Translate(SkScalar dx, SkScalar dy) : dx(dx), dy(dy) {}
126 SkScalar dx,dy;
draw__anon84fa9b240111::Translate127 void draw(SkCanvas* c, const SkMatrix&) {
128 c->translate(dx, dy);
129 }
130 };
131 struct TranslateZ final : Op {
132 static const auto kType = Type::TranslateZ;
TranslateZ__anon84fa9b240111::TranslateZ133 TranslateZ(SkScalar dz) : dz(dz) {}
134 SkScalar dz;
draw__anon84fa9b240111::TranslateZ135 void draw(SkCanvas* c, const SkMatrix&) {
136 #ifdef SK_EXPERIMENTAL_SHADOWING
137 c->translateZ(dz);
138 #endif
139 }
140 };
141
142 struct ClipPath final : Op {
143 static const auto kType = Type::ClipPath;
ClipPath__anon84fa9b240111::ClipPath144 ClipPath(const SkPath& path, SkClipOp op, bool aa) : path(path), op(op), aa(aa) {}
145 SkPath path;
146 SkClipOp op;
147 bool aa;
draw__anon84fa9b240111::ClipPath148 void draw(SkCanvas* c, const SkMatrix&) { c->clipPath(path, op, aa); }
149 };
150 struct ClipRect final : Op {
151 static const auto kType = Type::ClipRect;
ClipRect__anon84fa9b240111::ClipRect152 ClipRect(const SkRect& rect, SkClipOp op, bool aa) : rect(rect), op(op), aa(aa) {}
153 SkRect rect;
154 SkClipOp op;
155 bool aa;
draw__anon84fa9b240111::ClipRect156 void draw(SkCanvas* c, const SkMatrix&) { c->clipRect(rect, op, aa); }
157 };
158 struct ClipRRect final : Op {
159 static const auto kType = Type::ClipRRect;
ClipRRect__anon84fa9b240111::ClipRRect160 ClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) : rrect(rrect), op(op), aa(aa) {}
161 SkRRect rrect;
162 SkClipOp op;
163 bool aa;
draw__anon84fa9b240111::ClipRRect164 void draw(SkCanvas* c, const SkMatrix&) { c->clipRRect(rrect, op, aa); }
165 };
166 struct ClipRegion final : Op {
167 static const auto kType = Type::ClipRegion;
ClipRegion__anon84fa9b240111::ClipRegion168 ClipRegion(const SkRegion& region, SkClipOp op) : region(region), op(op) {}
169 SkRegion region;
170 SkClipOp op;
draw__anon84fa9b240111::ClipRegion171 void draw(SkCanvas* c, const SkMatrix&) { c->clipRegion(region, op); }
172 };
173
174 struct DrawPaint final : Op {
175 static const auto kType = Type::DrawPaint;
DrawPaint__anon84fa9b240111::DrawPaint176 DrawPaint(const SkPaint& paint) : paint(paint) {}
177 SkPaint paint;
draw__anon84fa9b240111::DrawPaint178 void draw(SkCanvas* c, const SkMatrix&) { c->drawPaint(paint); }
179 };
180 struct DrawPath final : Op {
181 static const auto kType = Type::DrawPath;
DrawPath__anon84fa9b240111::DrawPath182 DrawPath(const SkPath& path, const SkPaint& paint) : path(path), paint(paint) {}
183 SkPath path;
184 SkPaint paint;
draw__anon84fa9b240111::DrawPath185 void draw(SkCanvas* c, const SkMatrix&) { c->drawPath(path, paint); }
186 };
187 struct DrawRect final : Op {
188 static const auto kType = Type::DrawRect;
DrawRect__anon84fa9b240111::DrawRect189 DrawRect(const SkRect& rect, const SkPaint& paint) : rect(rect), paint(paint) {}
190 SkRect rect;
191 SkPaint paint;
draw__anon84fa9b240111::DrawRect192 void draw(SkCanvas* c, const SkMatrix&) { c->drawRect(rect, paint); }
193 };
194 struct DrawRegion final : Op {
195 static const auto kType = Type::DrawRegion;
DrawRegion__anon84fa9b240111::DrawRegion196 DrawRegion(const SkRegion& region, const SkPaint& paint) : region(region), paint(paint) {}
197 SkRegion region;
198 SkPaint paint;
draw__anon84fa9b240111::DrawRegion199 void draw(SkCanvas* c, const SkMatrix&) { c->drawRegion(region, paint); }
200 };
201 struct DrawOval final : Op {
202 static const auto kType = Type::DrawOval;
DrawOval__anon84fa9b240111::DrawOval203 DrawOval(const SkRect& oval, const SkPaint& paint) : oval(oval), paint(paint) {}
204 SkRect oval;
205 SkPaint paint;
draw__anon84fa9b240111::DrawOval206 void draw(SkCanvas* c, const SkMatrix&) { c->drawOval(oval, paint); }
207 };
208 struct DrawArc final : Op {
209 static const auto kType = Type::DrawArc;
DrawArc__anon84fa9b240111::DrawArc210 DrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
211 const SkPaint& paint)
212 : oval(oval), startAngle(startAngle), sweepAngle(sweepAngle), useCenter(useCenter)
213 , paint(paint) {}
214 SkRect oval;
215 SkScalar startAngle;
216 SkScalar sweepAngle;
217 bool useCenter;
218 SkPaint paint;
draw__anon84fa9b240111::DrawArc219 void draw(SkCanvas* c, const SkMatrix&) { c->drawArc(oval, startAngle, sweepAngle,
220 useCenter, paint); }
221 };
222 struct DrawRRect final : Op {
223 static const auto kType = Type::DrawRRect;
DrawRRect__anon84fa9b240111::DrawRRect224 DrawRRect(const SkRRect& rrect, const SkPaint& paint) : rrect(rrect), paint(paint) {}
225 SkRRect rrect;
226 SkPaint paint;
draw__anon84fa9b240111::DrawRRect227 void draw(SkCanvas* c, const SkMatrix&) { c->drawRRect(rrect, paint); }
228 };
229 struct DrawDRRect final : Op {
230 static const auto kType = Type::DrawDRRect;
DrawDRRect__anon84fa9b240111::DrawDRRect231 DrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint)
232 : outer(outer), inner(inner), paint(paint) {}
233 SkRRect outer, inner;
234 SkPaint paint;
draw__anon84fa9b240111::DrawDRRect235 void draw(SkCanvas* c, const SkMatrix&) { c->drawDRRect(outer, inner, paint); }
236 };
237
238 struct DrawAnnotation final : Op {
239 static const auto kType = Type::DrawAnnotation;
DrawAnnotation__anon84fa9b240111::DrawAnnotation240 DrawAnnotation(const SkRect& rect, SkData* value) : rect(rect), value(sk_ref_sp(value)) {}
241 SkRect rect;
242 sk_sp<SkData> value;
draw__anon84fa9b240111::DrawAnnotation243 void draw(SkCanvas* c, const SkMatrix&) {
244 c->drawAnnotation(rect, pod<char>(this), value.get());
245 }
246 };
247 struct DrawDrawable final : Op {
248 static const auto kType = Type::DrawDrawable;
DrawDrawable__anon84fa9b240111::DrawDrawable249 DrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) : drawable(sk_ref_sp(drawable)) {
250 if (matrix) { this->matrix = *matrix; }
251 }
252 sk_sp<SkDrawable> drawable;
253 SkMatrix matrix = SkMatrix::I();
draw__anon84fa9b240111::DrawDrawable254 void draw(SkCanvas* c, const SkMatrix&) {
255 c->drawDrawable(drawable.get(), &matrix);
256 }
257 };
258 struct DrawPicture final : Op {
259 static const auto kType = Type::DrawPicture;
DrawPicture__anon84fa9b240111::DrawPicture260 DrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint)
261 : picture(sk_ref_sp(picture)) {
262 if (matrix) { this->matrix = *matrix; }
263 if (paint) { this->paint = *paint; has_paint = true; }
264 }
265 sk_sp<const SkPicture> picture;
266 SkMatrix matrix = SkMatrix::I();
267 SkPaint paint;
268 bool has_paint = false; // TODO: why is a default paint not the same?
draw__anon84fa9b240111::DrawPicture269 void draw(SkCanvas* c, const SkMatrix&) {
270 c->drawPicture(picture.get(), &matrix, has_paint ? &paint : nullptr);
271 }
272 };
273 struct DrawShadowedPicture final : Op {
274 static const auto kType = Type::DrawShadowedPicture;
DrawShadowedPicture__anon84fa9b240111::DrawShadowedPicture275 DrawShadowedPicture(const SkPicture* picture, const SkMatrix* matrix,
276 const SkPaint* paint, const SkShadowParams& params)
277 : picture(sk_ref_sp(picture)) {
278 if (matrix) { this->matrix = *matrix; }
279 if (paint) { this->paint = *paint; }
280 this->params = params;
281 }
282 sk_sp<const SkPicture> picture;
283 SkMatrix matrix = SkMatrix::I();
284 SkPaint paint;
285 SkShadowParams params;
draw__anon84fa9b240111::DrawShadowedPicture286 void draw(SkCanvas* c, const SkMatrix&) {
287 #ifdef SK_EXPERIMENTAL_SHADOWING
288 c->drawShadowedPicture(picture.get(), &matrix, &paint, params);
289 #endif
290 }
291 };
292
293 struct DrawImage final : Op {
294 static const auto kType = Type::DrawImage;
DrawImage__anon84fa9b240111::DrawImage295 DrawImage(sk_sp<const SkImage>&& image, SkScalar x, SkScalar y, const SkPaint* paint)
296 : image(std::move(image)), x(x), y(y) {
297 if (paint) { this->paint = *paint; }
298 }
299 sk_sp<const SkImage> image;
300 SkScalar x,y;
301 SkPaint paint;
draw__anon84fa9b240111::DrawImage302 void draw(SkCanvas* c, const SkMatrix&) { c->drawImage(image.get(), x,y, &paint); }
303 };
304 struct DrawImageNine final : Op {
305 static const auto kType = Type::DrawImageNine;
DrawImageNine__anon84fa9b240111::DrawImageNine306 DrawImageNine(sk_sp<const SkImage>&& image,
307 const SkIRect& center, const SkRect& dst, const SkPaint* paint)
308 : image(std::move(image)), center(center), dst(dst) {
309 if (paint) { this->paint = *paint; }
310 }
311 sk_sp<const SkImage> image;
312 SkIRect center;
313 SkRect dst;
314 SkPaint paint;
draw__anon84fa9b240111::DrawImageNine315 void draw(SkCanvas* c, const SkMatrix&) {
316 c->drawImageNine(image.get(), center, dst, &paint);
317 }
318 };
319 struct DrawImageRect final : Op {
320 static const auto kType = Type::DrawImageRect;
DrawImageRect__anon84fa9b240111::DrawImageRect321 DrawImageRect(sk_sp<const SkImage>&& image, const SkRect* src, const SkRect& dst,
322 const SkPaint* paint, SkCanvas::SrcRectConstraint constraint)
323 : image(std::move(image)), dst(dst), constraint(constraint) {
324 this->src = src ? *src : SkRect::MakeIWH(image->width(), image->height());
325 if (paint) { this->paint = *paint; }
326 }
327 sk_sp<const SkImage> image;
328 SkRect src, dst;
329 SkPaint paint;
330 SkCanvas::SrcRectConstraint constraint;
draw__anon84fa9b240111::DrawImageRect331 void draw(SkCanvas* c, const SkMatrix&) {
332 c->drawImageRect(image.get(), src, dst, &paint, constraint);
333 }
334 };
335 struct DrawImageLattice final : Op {
336 static const auto kType = Type::DrawImageLattice;
DrawImageLattice__anon84fa9b240111::DrawImageLattice337 DrawImageLattice(sk_sp<const SkImage>&& image, int xs, int ys, int fs,
338 const SkIRect& src, const SkRect& dst, const SkPaint* paint)
339 : image(std::move(image)), xs(xs), ys(ys), fs(fs), src(src), dst(dst) {
340 if (paint) { this->paint = *paint; }
341 }
342 sk_sp<const SkImage> image;
343 int xs, ys, fs;
344 SkIRect src;
345 SkRect dst;
346 SkPaint paint;
draw__anon84fa9b240111::DrawImageLattice347 void draw(SkCanvas* c, const SkMatrix&) {
348 auto xdivs = pod<int>(this, 0),
349 ydivs = pod<int>(this, xs*sizeof(int));
350 auto flags = (0 == fs) ? nullptr :
351 pod<SkCanvas::Lattice::Flags>(this, (xs+ys)*sizeof(int));
352 c->drawImageLattice(image.get(), {xdivs, ydivs, flags, xs, ys, &src}, dst, &paint);
353 }
354 };
355
356 struct DrawText final : Op {
357 static const auto kType = Type::DrawText;
DrawText__anon84fa9b240111::DrawText358 DrawText(size_t bytes, SkScalar x, SkScalar y, const SkPaint& paint)
359 : bytes(bytes), x(x), y(y), paint(paint) {}
360 size_t bytes;
361 SkScalar x,y;
362 SkPaint paint;
draw__anon84fa9b240111::DrawText363 void draw(SkCanvas* c, const SkMatrix&) {
364 c->drawText(pod<void>(this), bytes, x,y, paint);
365 }
366 };
367 struct DrawPosText final : Op {
368 static const auto kType = Type::DrawPosText;
DrawPosText__anon84fa9b240111::DrawPosText369 DrawPosText(size_t bytes, const SkPaint& paint, int n)
370 : bytes(bytes), paint(paint), n(n) {}
371 size_t bytes;
372 SkPaint paint;
373 int n;
draw__anon84fa9b240111::DrawPosText374 void draw(SkCanvas* c, const SkMatrix&) {
375 auto points = pod<SkPoint>(this);
376 auto text = pod<void>(this, n*sizeof(SkPoint));
377 c->drawPosText(text, bytes, points, paint);
378 }
379 };
380 struct DrawPosTextH final : Op {
381 static const auto kType = Type::DrawPosTextH;
DrawPosTextH__anon84fa9b240111::DrawPosTextH382 DrawPosTextH(size_t bytes, SkScalar y, const SkPaint& paint, int n)
383 : bytes(bytes), y(y), paint(paint), n(n) {}
384 size_t bytes;
385 SkScalar y;
386 SkPaint paint;
387 int n;
draw__anon84fa9b240111::DrawPosTextH388 void draw(SkCanvas* c, const SkMatrix&) {
389 auto xs = pod<SkScalar>(this);
390 auto text = pod<void>(this, n*sizeof(SkScalar));
391 c->drawPosTextH(text, bytes, xs, y, paint);
392 }
393 };
394 struct DrawTextOnPath final : Op {
395 static const auto kType = Type::DrawTextOnPath;
DrawTextOnPath__anon84fa9b240111::DrawTextOnPath396 DrawTextOnPath(size_t bytes, const SkPath& path,
397 const SkMatrix* matrix, const SkPaint& paint)
398 : bytes(bytes), path(path), paint(paint) {
399 if (matrix) { this->matrix = *matrix; }
400 }
401 size_t bytes;
402 SkPath path;
403 SkMatrix matrix = SkMatrix::I();
404 SkPaint paint;
draw__anon84fa9b240111::DrawTextOnPath405 void draw(SkCanvas* c, const SkMatrix&) {
406 c->drawTextOnPath(pod<void>(this), bytes, path, &matrix, paint);
407 }
408 };
409 struct DrawTextRSXform final : Op {
410 static const auto kType = Type::DrawTextRSXform;
DrawTextRSXform__anon84fa9b240111::DrawTextRSXform411 DrawTextRSXform(size_t bytes, const SkRect* cull, const SkPaint& paint)
412 : bytes(bytes), paint(paint) {
413 if (cull) { this->cull = *cull; }
414 }
415 size_t bytes;
416 SkRect cull = kUnset;
417 SkPaint paint;
draw__anon84fa9b240111::DrawTextRSXform418 void draw(SkCanvas* c, const SkMatrix&) {
419 c->drawTextRSXform(pod<void>(this), bytes, pod<SkRSXform>(this, bytes),
420 maybe_unset(cull), paint);
421 }
422 };
423 struct DrawTextBlob final : Op {
424 static const auto kType = Type::DrawTextBlob;
DrawTextBlob__anon84fa9b240111::DrawTextBlob425 DrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint)
426 : blob(sk_ref_sp(blob)), x(x), y(y), paint(paint) {}
427 sk_sp<const SkTextBlob> blob;
428 SkScalar x,y;
429 SkPaint paint;
draw__anon84fa9b240111::DrawTextBlob430 void draw(SkCanvas* c, const SkMatrix&) {
431 c->drawTextBlob(blob.get(), x,y, paint);
432 }
433 };
434
435 struct DrawPatch final : Op {
436 static const auto kType = Type::DrawPatch;
DrawPatch__anon84fa9b240111::DrawPatch437 DrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texs[4],
438 SkBlendMode bmode, const SkPaint& paint)
439 : xfermode(bmode), paint(paint)
440 {
441 copy_v(this->cubics, cubics, 12);
442 if (colors) { copy_v(this->colors, colors, 4); has_colors = true; }
443 if (texs ) { copy_v(this->texs , texs , 4); has_texs = true; }
444 }
445 SkPoint cubics[12];
446 SkColor colors[4];
447 SkPoint texs[4];
448 SkBlendMode xfermode;
449 SkPaint paint;
450 bool has_colors = false;
451 bool has_texs = false;
draw__anon84fa9b240111::DrawPatch452 void draw(SkCanvas* c, const SkMatrix&) {
453 c->drawPatch(cubics, has_colors ? colors : nullptr, has_texs ? texs : nullptr,
454 xfermode, paint);
455 }
456 };
457 struct DrawPoints final : Op {
458 static const auto kType = Type::DrawPoints;
DrawPoints__anon84fa9b240111::DrawPoints459 DrawPoints(SkCanvas::PointMode mode, size_t count, const SkPaint& paint)
460 : mode(mode), count(count), paint(paint) {}
461 SkCanvas::PointMode mode;
462 size_t count;
463 SkPaint paint;
draw__anon84fa9b240111::DrawPoints464 void draw(SkCanvas* c, const SkMatrix&) {
465 c->drawPoints(mode, count, pod<SkPoint>(this), paint);
466 }
467 };
468 struct DrawVertices final : Op {
469 static const auto kType = Type::DrawVertices;
DrawVertices__anon84fa9b240111::DrawVertices470 DrawVertices(const SkVertices* v, SkBlendMode m, const SkPaint& p)
471 : vertices(sk_ref_sp(const_cast<SkVertices*>(v))), mode(m), paint(p) {}
472 sk_sp<SkVertices> vertices;
473 SkBlendMode mode;
474 SkPaint paint;
draw__anon84fa9b240111::DrawVertices475 void draw(SkCanvas* c, const SkMatrix&) {
476 c->drawVertices(vertices, mode, paint);
477 }
478 };
479 struct DrawAtlas final : Op {
480 static const auto kType = Type::DrawAtlas;
DrawAtlas__anon84fa9b240111::DrawAtlas481 DrawAtlas(const SkImage* atlas, int count, SkBlendMode xfermode,
482 const SkRect* cull, const SkPaint* paint, bool has_colors)
483 : atlas(sk_ref_sp(atlas)), count(count), xfermode(xfermode), has_colors(has_colors) {
484 if (cull) { this->cull = *cull; }
485 if (paint) { this->paint = *paint; }
486 }
487 sk_sp<const SkImage> atlas;
488 int count;
489 SkBlendMode xfermode;
490 SkRect cull = kUnset;
491 SkPaint paint;
492 bool has_colors;
draw__anon84fa9b240111::DrawAtlas493 void draw(SkCanvas* c, const SkMatrix&) {
494 auto xforms = pod<SkRSXform>(this, 0);
495 auto texs = pod<SkRect>(this, count*sizeof(SkRSXform));
496 auto colors = has_colors
497 ? pod<SkColor>(this, count*(sizeof(SkRSXform) + sizeof(SkRect)))
498 : nullptr;
499 c->drawAtlas(atlas.get(), xforms, texs, colors, count, xfermode,
500 maybe_unset(cull), &paint);
501 }
502 };
503 }
504
505 template <typename T, typename... Args>
push(size_t pod,Args &&...args)506 void* SkLiteDL::push(size_t pod, Args&&... args) {
507 size_t skip = SkAlignPtr(sizeof(T) + pod);
508 SkASSERT(skip < (1<<24));
509 if (fUsed + skip > fReserved) {
510 static_assert(SkIsPow2(SKLITEDL_PAGE), "This math needs updating for non-pow2.");
511 // Next greater multiple of SKLITEDL_PAGE.
512 fReserved = (fUsed + skip + SKLITEDL_PAGE) & ~(SKLITEDL_PAGE-1);
513 fBytes.realloc(fReserved);
514 }
515 SkASSERT(fUsed + skip <= fReserved);
516 auto op = (T*)(fBytes.get() + fUsed);
517 fUsed += skip;
518 new (op) T{ std::forward<Args>(args)... };
519 op->type = (uint32_t)T::kType;
520 op->skip = skip;
521 return op+1;
522 }
523
524 template <typename Fn, typename... Args>
map(const Fn fns[],Args...args)525 inline void SkLiteDL::map(const Fn fns[], Args... args) {
526 auto end = fBytes.get() + fUsed;
527 for (uint8_t* ptr = fBytes.get(); ptr < end; ) {
528 auto op = (Op*)ptr;
529 auto type = op->type;
530 auto skip = op->skip;
531 if (auto fn = fns[type]) { // We replace no-op functions with nullptrs
532 fn(op, args...); // to avoid the overhead of a pointless call.
533 }
534 ptr += skip;
535 }
536 }
537
538 #ifdef SK_SUPPORT_LEGACY_DRAWFILTER
setDrawFilter(SkDrawFilter * df)539 void SkLiteDL::setDrawFilter(SkDrawFilter* df) {
540 this->push<SetDrawFilter>(0, df);
541 }
542 #endif
543
save()544 void SkLiteDL:: save() { this->push <Save>(0); }
restore()545 void SkLiteDL::restore() { this->push<Restore>(0); }
saveLayer(const SkRect * bounds,const SkPaint * paint,const SkImageFilter * backdrop,SkCanvas::SaveLayerFlags flags)546 void SkLiteDL::saveLayer(const SkRect* bounds, const SkPaint* paint,
547 const SkImageFilter* backdrop, SkCanvas::SaveLayerFlags flags) {
548 this->push<SaveLayer>(0, bounds, paint, backdrop, flags);
549 }
550
concat(const SkMatrix & matrix)551 void SkLiteDL:: concat(const SkMatrix& matrix) { this->push <Concat>(0, matrix); }
setMatrix(const SkMatrix & matrix)552 void SkLiteDL::setMatrix(const SkMatrix& matrix) { this->push<SetMatrix>(0, matrix); }
translate(SkScalar dx,SkScalar dy)553 void SkLiteDL::translate(SkScalar dx, SkScalar dy) { this->push<Translate>(0, dx, dy); }
translateZ(SkScalar dz)554 void SkLiteDL::translateZ(SkScalar dz) { this->push<TranslateZ>(0, dz); }
555
clipPath(const SkPath & path,SkClipOp op,bool aa)556 void SkLiteDL::clipPath(const SkPath& path, SkClipOp op, bool aa) {
557 this->push<ClipPath>(0, path, op, aa);
558 }
clipRect(const SkRect & rect,SkClipOp op,bool aa)559 void SkLiteDL::clipRect(const SkRect& rect, SkClipOp op, bool aa) {
560 this->push<ClipRect>(0, rect, op, aa);
561 }
clipRRect(const SkRRect & rrect,SkClipOp op,bool aa)562 void SkLiteDL::clipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
563 this->push<ClipRRect>(0, rrect, op, aa);
564 }
clipRegion(const SkRegion & region,SkClipOp op)565 void SkLiteDL::clipRegion(const SkRegion& region, SkClipOp op) {
566 this->push<ClipRegion>(0, region, op);
567 }
568
drawPaint(const SkPaint & paint)569 void SkLiteDL::drawPaint(const SkPaint& paint) {
570 this->push<DrawPaint>(0, paint);
571 }
drawPath(const SkPath & path,const SkPaint & paint)572 void SkLiteDL::drawPath(const SkPath& path, const SkPaint& paint) {
573 this->push<DrawPath>(0, path, paint);
574 }
drawRect(const SkRect & rect,const SkPaint & paint)575 void SkLiteDL::drawRect(const SkRect& rect, const SkPaint& paint) {
576 this->push<DrawRect>(0, rect, paint);
577 }
drawRegion(const SkRegion & region,const SkPaint & paint)578 void SkLiteDL::drawRegion(const SkRegion& region, const SkPaint& paint) {
579 this->push<DrawRegion>(0, region, paint);
580 }
drawOval(const SkRect & oval,const SkPaint & paint)581 void SkLiteDL::drawOval(const SkRect& oval, const SkPaint& paint) {
582 this->push<DrawOval>(0, oval, paint);
583 }
drawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)584 void SkLiteDL::drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
585 const SkPaint& paint) {
586 this->push<DrawArc>(0, oval, startAngle, sweepAngle, useCenter, paint);
587 }
drawRRect(const SkRRect & rrect,const SkPaint & paint)588 void SkLiteDL::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
589 this->push<DrawRRect>(0, rrect, paint);
590 }
drawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)591 void SkLiteDL::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
592 this->push<DrawDRRect>(0, outer, inner, paint);
593 }
594
drawAnnotation(const SkRect & rect,const char * key,SkData * value)595 void SkLiteDL::drawAnnotation(const SkRect& rect, const char* key, SkData* value) {
596 size_t bytes = strlen(key)+1;
597 void* pod = this->push<DrawAnnotation>(bytes, rect, value);
598 copy_v(pod, key,bytes);
599 }
drawDrawable(SkDrawable * drawable,const SkMatrix * matrix)600 void SkLiteDL::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
601 this->push<DrawDrawable>(0, drawable, matrix);
602 }
drawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)603 void SkLiteDL::drawPicture(const SkPicture* picture,
604 const SkMatrix* matrix, const SkPaint* paint) {
605 this->push<DrawPicture>(0, picture, matrix, paint);
606 }
drawShadowedPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint,const SkShadowParams & params)607 void SkLiteDL::drawShadowedPicture(const SkPicture* picture, const SkMatrix* matrix,
608 const SkPaint* paint, const SkShadowParams& params) {
609 push<DrawShadowedPicture>(0, picture, matrix, paint, params);
610 }
611
drawImage(sk_sp<const SkImage> image,SkScalar x,SkScalar y,const SkPaint * paint)612 void SkLiteDL::drawImage(sk_sp<const SkImage> image, SkScalar x, SkScalar y, const SkPaint* paint) {
613 this->push<DrawImage>(0, std::move(image), x,y, paint);
614 }
drawImageNine(sk_sp<const SkImage> image,const SkIRect & center,const SkRect & dst,const SkPaint * paint)615 void SkLiteDL::drawImageNine(sk_sp<const SkImage> image, const SkIRect& center,
616 const SkRect& dst, const SkPaint* paint) {
617 this->push<DrawImageNine>(0, std::move(image), center, dst, paint);
618 }
drawImageRect(sk_sp<const SkImage> image,const SkRect * src,const SkRect & dst,const SkPaint * paint,SkCanvas::SrcRectConstraint constraint)619 void SkLiteDL::drawImageRect(sk_sp<const SkImage> image, const SkRect* src, const SkRect& dst,
620 const SkPaint* paint, SkCanvas::SrcRectConstraint constraint) {
621 this->push<DrawImageRect>(0, std::move(image), src, dst, paint, constraint);
622 }
drawImageLattice(sk_sp<const SkImage> image,const SkCanvas::Lattice & lattice,const SkRect & dst,const SkPaint * paint)623 void SkLiteDL::drawImageLattice(sk_sp<const SkImage> image, const SkCanvas::Lattice& lattice,
624 const SkRect& dst, const SkPaint* paint) {
625 int xs = lattice.fXCount, ys = lattice.fYCount;
626 int fs = lattice.fFlags ? (xs + 1) * (ys + 1) : 0;
627 size_t bytes = (xs + ys) * sizeof(int) + fs * sizeof(SkCanvas::Lattice::Flags);
628 SkASSERT(lattice.fBounds);
629 void* pod = this->push<DrawImageLattice>(bytes, std::move(image), xs, ys, fs, *lattice.fBounds,
630 dst, paint);
631 copy_v(pod, lattice.fXDivs, xs,
632 lattice.fYDivs, ys,
633 lattice.fFlags, fs);
634 }
635
drawText(const void * text,size_t bytes,SkScalar x,SkScalar y,const SkPaint & paint)636 void SkLiteDL::drawText(const void* text, size_t bytes,
637 SkScalar x, SkScalar y, const SkPaint& paint) {
638 void* pod = this->push<DrawText>(bytes, bytes, x, y, paint);
639 copy_v(pod, (const char*)text,bytes);
640 }
drawPosText(const void * text,size_t bytes,const SkPoint pos[],const SkPaint & paint)641 void SkLiteDL::drawPosText(const void* text, size_t bytes,
642 const SkPoint pos[], const SkPaint& paint) {
643 int n = paint.countText(text, bytes);
644 void* pod = this->push<DrawPosText>(n*sizeof(SkPoint)+bytes, bytes, paint, n);
645 copy_v(pod, pos,n, (const char*)text,bytes);
646 }
drawPosTextH(const void * text,size_t bytes,const SkScalar xs[],SkScalar y,const SkPaint & paint)647 void SkLiteDL::drawPosTextH(const void* text, size_t bytes,
648 const SkScalar xs[], SkScalar y, const SkPaint& paint) {
649 int n = paint.countText(text, bytes);
650 void* pod = this->push<DrawPosTextH>(n*sizeof(SkScalar)+bytes, bytes, y, paint, n);
651 copy_v(pod, xs,n, (const char*)text,bytes);
652 }
drawTextOnPath(const void * text,size_t bytes,const SkPath & path,const SkMatrix * matrix,const SkPaint & paint)653 void SkLiteDL::drawTextOnPath(const void* text, size_t bytes,
654 const SkPath& path, const SkMatrix* matrix, const SkPaint& paint) {
655 void* pod = this->push<DrawTextOnPath>(bytes, bytes, path, matrix, paint);
656 copy_v(pod, (const char*)text,bytes);
657 }
drawTextRSXform(const void * text,size_t bytes,const SkRSXform xforms[],const SkRect * cull,const SkPaint & paint)658 void SkLiteDL::drawTextRSXform(const void* text, size_t bytes,
659 const SkRSXform xforms[], const SkRect* cull, const SkPaint& paint) {
660 int n = paint.countText(text, bytes);
661 void* pod = this->push<DrawTextRSXform>(bytes+n*sizeof(SkRSXform), bytes, cull, paint);
662 copy_v(pod, (const char*)text,bytes, xforms,n);
663 }
drawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)664 void SkLiteDL::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) {
665 this->push<DrawTextBlob>(0, blob, x,y, paint);
666 }
667
drawPatch(const SkPoint points[12],const SkColor colors[4],const SkPoint texs[4],SkBlendMode bmode,const SkPaint & paint)668 void SkLiteDL::drawPatch(const SkPoint points[12], const SkColor colors[4], const SkPoint texs[4],
669 SkBlendMode bmode, const SkPaint& paint) {
670 this->push<DrawPatch>(0, points, colors, texs, bmode, paint);
671 }
drawPoints(SkCanvas::PointMode mode,size_t count,const SkPoint points[],const SkPaint & paint)672 void SkLiteDL::drawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint points[],
673 const SkPaint& paint) {
674 void* pod = this->push<DrawPoints>(count*sizeof(SkPoint), mode, count, paint);
675 copy_v(pod, points,count);
676 }
drawVertices(const SkVertices * vertices,SkBlendMode mode,const SkPaint & paint)677 void SkLiteDL::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) {
678 this->push<DrawVertices>(0, vertices, mode, paint);
679 }
drawAtlas(const SkImage * atlas,const SkRSXform xforms[],const SkRect texs[],const SkColor colors[],int count,SkBlendMode xfermode,const SkRect * cull,const SkPaint * paint)680 void SkLiteDL::drawAtlas(const SkImage* atlas, const SkRSXform xforms[], const SkRect texs[],
681 const SkColor colors[], int count, SkBlendMode xfermode,
682 const SkRect* cull, const SkPaint* paint) {
683 size_t bytes = count*(sizeof(SkRSXform) + sizeof(SkRect));
684 if (colors) {
685 bytes += count*sizeof(SkColor);
686 }
687 void* pod = this->push<DrawAtlas>(bytes,
688 atlas, count, xfermode, cull, paint, colors != nullptr);
689 copy_v(pod, xforms, count,
690 texs, count,
691 colors, colors ? count : 0);
692 }
693
694 typedef void(*draw_fn)(void*, SkCanvas*, const SkMatrix&);
695 typedef void(*void_fn)(void*);
696
697 // All ops implement draw().
698 #define M(T) [](void* op, SkCanvas* c, const SkMatrix& original) { ((T*)op)->draw(c, original); },
699 static const draw_fn draw_fns[] = { TYPES(M) };
700 #undef M
701
702 // Older libstdc++ has pre-standard std::has_trivial_destructor.
703 #if defined(__GLIBCXX__) && (__GLIBCXX__ < 20130000)
704 template <typename T> using can_skip_destructor = std::has_trivial_destructor<T>;
705 #else
706 template <typename T> using can_skip_destructor = std::is_trivially_destructible<T>;
707 #endif
708
709 // Most state ops (matrix, clip, save, restore) have a trivial destructor.
710 #define M(T) !can_skip_destructor<T>::value ? [](void* op) { ((T*)op)->~T(); } : (void_fn)nullptr,
711 static const void_fn dtor_fns[] = { TYPES(M) };
712 #undef M
713
draw(SkCanvas * canvas)714 void SkLiteDL::draw(SkCanvas* canvas) {
715 SkAutoCanvasRestore acr(canvas, false);
716 this->map(draw_fns, canvas, canvas->getTotalMatrix());
717 }
718
~SkLiteDL()719 SkLiteDL::~SkLiteDL() {
720 this->reset();
721 }
722
reset()723 void SkLiteDL::reset() {
724 this->map(dtor_fns);
725
726 // Leave fBytes and fReserved alone.
727 fUsed = 0;
728 }
729