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 "SkDrawShadowInfo.h"
12 #include "SkImage.h"
13 #include "SkImageFilter.h"
14 #include "SkLiteDL.h"
15 #include "SkMath.h"
16 #include "SkPicture.h"
17 #include "SkRegion.h"
18 #include "SkRSXform.h"
19 #include "SkTextBlob.h"
20 #include "SkVertices.h"
21
22 #ifndef SKLITEDL_PAGE
23 #define SKLITEDL_PAGE 4096
24 #endif
25
26 // A stand-in for an optional SkRect which was not set, e.g. bounds for a saveLayer().
27 static const SkRect kUnset = { SK_ScalarInfinity, 0,0,0};
maybe_unset(const SkRect & r)28 static const SkRect* maybe_unset(const SkRect& r) {
29 return r.left() == SK_ScalarInfinity ? nullptr : &r;
30 }
31
32 // copy_v(dst, src,n, src,n, ...) copies an arbitrary number of typed srcs into dst.
copy_v(void * dst)33 static void copy_v(void* dst) {}
34
35 template <typename S, typename... Rest>
copy_v(void * dst,const S * src,int n,Rest &&...rest)36 static void copy_v(void* dst, const S* src, int n, Rest&&... rest) {
37 SkASSERTF(((uintptr_t)dst & (alignof(S)-1)) == 0,
38 "Expected %p to be aligned for at least %zu bytes.", dst, alignof(S));
39 sk_careful_memcpy(dst, src, n*sizeof(S));
40 copy_v(SkTAddOffset<void>(dst, n*sizeof(S)), std::forward<Rest>(rest)...);
41 }
42
43 // Helper for getting back at arrays which have been copy_v'd together after an Op.
44 template <typename D, typename T>
pod(const T * op,size_t offset=0)45 static const D* pod(const T* op, size_t offset = 0) {
46 return SkTAddOffset<const D>(op+1, offset);
47 }
48
49 namespace {
50 #define TYPES(M) \
51 M(SetDrawFilter) M(Flush) M(Save) M(Restore) M(SaveLayer) \
52 M(Concat) M(SetMatrix) M(Translate) \
53 M(ClipPath) M(ClipRect) M(ClipRRect) M(ClipRegion) \
54 M(DrawPaint) M(DrawPath) M(DrawRect) M(DrawRegion) M(DrawOval) M(DrawArc) \
55 M(DrawRRect) M(DrawDRRect) M(DrawAnnotation) M(DrawDrawable) M(DrawPicture) \
56 M(DrawImage) M(DrawImageNine) M(DrawImageRect) M(DrawImageLattice) \
57 M(DrawText) M(DrawPosText) M(DrawPosTextH) \
58 M(DrawTextOnPath) M(DrawTextRSXform) M(DrawTextBlob) \
59 M(DrawPatch) M(DrawPoints) M(DrawVertices) M(DrawAtlas) M(DrawShadowRec)
60
61 #define M(T) T,
62 enum class Type : uint8_t { TYPES(M) };
63 #undef M
64
65 struct Op {
66 uint32_t type : 8;
67 uint32_t skip : 24;
68 };
69 static_assert(sizeof(Op) == 4, "");
70
71 struct SetDrawFilter final : Op {
72 #ifdef SK_SUPPORT_LEGACY_DRAWFILTER
73 static const auto kType = Type::SetDrawFilter;
SetDrawFilter__anon2adcf5c10111::SetDrawFilter74 SetDrawFilter(SkDrawFilter* df) : drawFilter(sk_ref_sp(df)) {}
75 sk_sp<SkDrawFilter> drawFilter;
76 #endif
draw__anon2adcf5c10111::SetDrawFilter77 void draw(SkCanvas* c, const SkMatrix&) const {
78 #ifdef SK_SUPPORT_LEGACY_DRAWFILTER
79 c->setDrawFilter(drawFilter.get());
80 #endif
81 }
82 };
83
84 struct Flush final : Op {
85 static const auto kType = Type::Flush;
draw__anon2adcf5c10111::Flush86 void draw(SkCanvas* c, const SkMatrix&) const { c->flush(); }
87 };
88
89 struct Save final : Op {
90 static const auto kType = Type::Save;
draw__anon2adcf5c10111::Save91 void draw(SkCanvas* c, const SkMatrix&) const { c->save(); }
92 };
93 struct Restore final : Op {
94 static const auto kType = Type::Restore;
draw__anon2adcf5c10111::Restore95 void draw(SkCanvas* c, const SkMatrix&) const { c->restore(); }
96 };
97 struct SaveLayer final : Op {
98 static const auto kType = Type::SaveLayer;
SaveLayer__anon2adcf5c10111::SaveLayer99 SaveLayer(const SkRect* bounds, const SkPaint* paint,
100 const SkImageFilter* backdrop, const SkImage* clipMask,
101 const SkMatrix* clipMatrix, SkCanvas::SaveLayerFlags flags) {
102 if (bounds) { this->bounds = *bounds; }
103 if (paint) { this->paint = *paint; }
104 this->backdrop = sk_ref_sp(backdrop);
105 this->clipMask = sk_ref_sp(clipMask);
106 this->clipMatrix = clipMatrix ? *clipMatrix : SkMatrix::I();
107 this->flags = flags;
108 }
109 SkRect bounds = kUnset;
110 SkPaint paint;
111 sk_sp<const SkImageFilter> backdrop;
112 sk_sp<const SkImage> clipMask;
113 SkMatrix clipMatrix;
114 SkCanvas::SaveLayerFlags flags;
draw__anon2adcf5c10111::SaveLayer115 void draw(SkCanvas* c, const SkMatrix&) const {
116 c->saveLayer({ maybe_unset(bounds), &paint, backdrop.get(), clipMask.get(),
117 clipMatrix.isIdentity() ? nullptr : &clipMatrix, flags });
118 }
119 };
120
121 struct Concat final : Op {
122 static const auto kType = Type::Concat;
Concat__anon2adcf5c10111::Concat123 Concat(const SkMatrix& matrix) : matrix(matrix) {}
124 SkMatrix matrix;
draw__anon2adcf5c10111::Concat125 void draw(SkCanvas* c, const SkMatrix&) const { c->concat(matrix); }
126 };
127 struct SetMatrix final : Op {
128 static const auto kType = Type::SetMatrix;
SetMatrix__anon2adcf5c10111::SetMatrix129 SetMatrix(const SkMatrix& matrix) : matrix(matrix) {}
130 SkMatrix matrix;
draw__anon2adcf5c10111::SetMatrix131 void draw(SkCanvas* c, const SkMatrix& original) const {
132 c->setMatrix(SkMatrix::Concat(original, matrix));
133 }
134 };
135 struct Translate final : Op {
136 static const auto kType = Type::Translate;
Translate__anon2adcf5c10111::Translate137 Translate(SkScalar dx, SkScalar dy) : dx(dx), dy(dy) {}
138 SkScalar dx,dy;
draw__anon2adcf5c10111::Translate139 void draw(SkCanvas* c, const SkMatrix&) const {
140 c->translate(dx, dy);
141 }
142 };
143
144 struct ClipPath final : Op {
145 static const auto kType = Type::ClipPath;
ClipPath__anon2adcf5c10111::ClipPath146 ClipPath(const SkPath& path, SkClipOp op, bool aa) : path(path), op(op), aa(aa) {}
147 SkPath path;
148 SkClipOp op;
149 bool aa;
draw__anon2adcf5c10111::ClipPath150 void draw(SkCanvas* c, const SkMatrix&) const { c->clipPath(path, op, aa); }
151 };
152 struct ClipRect final : Op {
153 static const auto kType = Type::ClipRect;
ClipRect__anon2adcf5c10111::ClipRect154 ClipRect(const SkRect& rect, SkClipOp op, bool aa) : rect(rect), op(op), aa(aa) {}
155 SkRect rect;
156 SkClipOp op;
157 bool aa;
draw__anon2adcf5c10111::ClipRect158 void draw(SkCanvas* c, const SkMatrix&) const { c->clipRect(rect, op, aa); }
159 };
160 struct ClipRRect final : Op {
161 static const auto kType = Type::ClipRRect;
ClipRRect__anon2adcf5c10111::ClipRRect162 ClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) : rrect(rrect), op(op), aa(aa) {}
163 SkRRect rrect;
164 SkClipOp op;
165 bool aa;
draw__anon2adcf5c10111::ClipRRect166 void draw(SkCanvas* c, const SkMatrix&) const { c->clipRRect(rrect, op, aa); }
167 };
168 struct ClipRegion final : Op {
169 static const auto kType = Type::ClipRegion;
ClipRegion__anon2adcf5c10111::ClipRegion170 ClipRegion(const SkRegion& region, SkClipOp op) : region(region), op(op) {}
171 SkRegion region;
172 SkClipOp op;
draw__anon2adcf5c10111::ClipRegion173 void draw(SkCanvas* c, const SkMatrix&) const { c->clipRegion(region, op); }
174 };
175
176 struct DrawPaint final : Op {
177 static const auto kType = Type::DrawPaint;
DrawPaint__anon2adcf5c10111::DrawPaint178 DrawPaint(const SkPaint& paint) : paint(paint) {}
179 SkPaint paint;
draw__anon2adcf5c10111::DrawPaint180 void draw(SkCanvas* c, const SkMatrix&) const { c->drawPaint(paint); }
181 };
182 struct DrawPath final : Op {
183 static const auto kType = Type::DrawPath;
DrawPath__anon2adcf5c10111::DrawPath184 DrawPath(const SkPath& path, const SkPaint& paint) : path(path), paint(paint) {}
185 SkPath path;
186 SkPaint paint;
draw__anon2adcf5c10111::DrawPath187 void draw(SkCanvas* c, const SkMatrix&) const { c->drawPath(path, paint); }
188 };
189 struct DrawRect final : Op {
190 static const auto kType = Type::DrawRect;
DrawRect__anon2adcf5c10111::DrawRect191 DrawRect(const SkRect& rect, const SkPaint& paint) : rect(rect), paint(paint) {}
192 SkRect rect;
193 SkPaint paint;
draw__anon2adcf5c10111::DrawRect194 void draw(SkCanvas* c, const SkMatrix&) const { c->drawRect(rect, paint); }
195 };
196 struct DrawRegion final : Op {
197 static const auto kType = Type::DrawRegion;
DrawRegion__anon2adcf5c10111::DrawRegion198 DrawRegion(const SkRegion& region, const SkPaint& paint) : region(region), paint(paint) {}
199 SkRegion region;
200 SkPaint paint;
draw__anon2adcf5c10111::DrawRegion201 void draw(SkCanvas* c, const SkMatrix&) const { c->drawRegion(region, paint); }
202 };
203 struct DrawOval final : Op {
204 static const auto kType = Type::DrawOval;
DrawOval__anon2adcf5c10111::DrawOval205 DrawOval(const SkRect& oval, const SkPaint& paint) : oval(oval), paint(paint) {}
206 SkRect oval;
207 SkPaint paint;
draw__anon2adcf5c10111::DrawOval208 void draw(SkCanvas* c, const SkMatrix&) const { c->drawOval(oval, paint); }
209 };
210 struct DrawArc final : Op {
211 static const auto kType = Type::DrawArc;
DrawArc__anon2adcf5c10111::DrawArc212 DrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
213 const SkPaint& paint)
214 : oval(oval), startAngle(startAngle), sweepAngle(sweepAngle), useCenter(useCenter)
215 , paint(paint) {}
216 SkRect oval;
217 SkScalar startAngle;
218 SkScalar sweepAngle;
219 bool useCenter;
220 SkPaint paint;
draw__anon2adcf5c10111::DrawArc221 void draw(SkCanvas* c, const SkMatrix&) const { c->drawArc(oval, startAngle, sweepAngle,
222 useCenter, paint); }
223 };
224 struct DrawRRect final : Op {
225 static const auto kType = Type::DrawRRect;
DrawRRect__anon2adcf5c10111::DrawRRect226 DrawRRect(const SkRRect& rrect, const SkPaint& paint) : rrect(rrect), paint(paint) {}
227 SkRRect rrect;
228 SkPaint paint;
draw__anon2adcf5c10111::DrawRRect229 void draw(SkCanvas* c, const SkMatrix&) const { c->drawRRect(rrect, paint); }
230 };
231 struct DrawDRRect final : Op {
232 static const auto kType = Type::DrawDRRect;
DrawDRRect__anon2adcf5c10111::DrawDRRect233 DrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint)
234 : outer(outer), inner(inner), paint(paint) {}
235 SkRRect outer, inner;
236 SkPaint paint;
draw__anon2adcf5c10111::DrawDRRect237 void draw(SkCanvas* c, const SkMatrix&) const { c->drawDRRect(outer, inner, paint); }
238 };
239
240 struct DrawAnnotation final : Op {
241 static const auto kType = Type::DrawAnnotation;
DrawAnnotation__anon2adcf5c10111::DrawAnnotation242 DrawAnnotation(const SkRect& rect, SkData* value) : rect(rect), value(sk_ref_sp(value)) {}
243 SkRect rect;
244 sk_sp<SkData> value;
draw__anon2adcf5c10111::DrawAnnotation245 void draw(SkCanvas* c, const SkMatrix&) const {
246 c->drawAnnotation(rect, pod<char>(this), value.get());
247 }
248 };
249 struct DrawDrawable final : Op {
250 static const auto kType = Type::DrawDrawable;
DrawDrawable__anon2adcf5c10111::DrawDrawable251 DrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) : drawable(sk_ref_sp(drawable)) {
252 if (matrix) { this->matrix = *matrix; }
253 }
254 sk_sp<SkDrawable> drawable;
255 SkMatrix matrix = SkMatrix::I();
draw__anon2adcf5c10111::DrawDrawable256 void draw(SkCanvas* c, const SkMatrix&) const {
257 c->drawDrawable(drawable.get(), &matrix);
258 }
259 };
260 struct DrawPicture final : Op {
261 static const auto kType = Type::DrawPicture;
DrawPicture__anon2adcf5c10111::DrawPicture262 DrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint)
263 : picture(sk_ref_sp(picture)) {
264 if (matrix) { this->matrix = *matrix; }
265 if (paint) { this->paint = *paint; has_paint = true; }
266 }
267 sk_sp<const SkPicture> picture;
268 SkMatrix matrix = SkMatrix::I();
269 SkPaint paint;
270 bool has_paint = false; // TODO: why is a default paint not the same?
draw__anon2adcf5c10111::DrawPicture271 void draw(SkCanvas* c, const SkMatrix&) const {
272 c->drawPicture(picture.get(), &matrix, has_paint ? &paint : nullptr);
273 }
274 };
275
276 struct DrawImage final : Op {
277 static const auto kType = Type::DrawImage;
DrawImage__anon2adcf5c10111::DrawImage278 DrawImage(sk_sp<const SkImage>&& image, SkScalar x, SkScalar y, const SkPaint* paint)
279 : image(std::move(image)), x(x), y(y) {
280 if (paint) { this->paint = *paint; }
281 }
282 sk_sp<const SkImage> image;
283 SkScalar x,y;
284 SkPaint paint;
draw__anon2adcf5c10111::DrawImage285 void draw(SkCanvas* c, const SkMatrix&) const { c->drawImage(image.get(), x,y, &paint); }
286 };
287 struct DrawImageNine final : Op {
288 static const auto kType = Type::DrawImageNine;
DrawImageNine__anon2adcf5c10111::DrawImageNine289 DrawImageNine(sk_sp<const SkImage>&& image,
290 const SkIRect& center, const SkRect& dst, const SkPaint* paint)
291 : image(std::move(image)), center(center), dst(dst) {
292 if (paint) { this->paint = *paint; }
293 }
294 sk_sp<const SkImage> image;
295 SkIRect center;
296 SkRect dst;
297 SkPaint paint;
draw__anon2adcf5c10111::DrawImageNine298 void draw(SkCanvas* c, const SkMatrix&) const {
299 c->drawImageNine(image.get(), center, dst, &paint);
300 }
301 };
302 struct DrawImageRect final : Op {
303 static const auto kType = Type::DrawImageRect;
DrawImageRect__anon2adcf5c10111::DrawImageRect304 DrawImageRect(sk_sp<const SkImage>&& image, const SkRect* src, const SkRect& dst,
305 const SkPaint* paint, SkCanvas::SrcRectConstraint constraint)
306 : image(std::move(image)), dst(dst), constraint(constraint) {
307 this->src = src ? *src : SkRect::MakeIWH(image->width(), image->height());
308 if (paint) { this->paint = *paint; }
309 }
310 sk_sp<const SkImage> image;
311 SkRect src, dst;
312 SkPaint paint;
313 SkCanvas::SrcRectConstraint constraint;
draw__anon2adcf5c10111::DrawImageRect314 void draw(SkCanvas* c, const SkMatrix&) const {
315 c->drawImageRect(image.get(), src, dst, &paint, constraint);
316 }
317 };
318 struct DrawImageLattice final : Op {
319 static const auto kType = Type::DrawImageLattice;
DrawImageLattice__anon2adcf5c10111::DrawImageLattice320 DrawImageLattice(sk_sp<const SkImage>&& image, int xs, int ys, int fs,
321 const SkIRect& src, const SkRect& dst, const SkPaint* paint)
322 : image(std::move(image)), xs(xs), ys(ys), fs(fs), src(src), dst(dst) {
323 if (paint) { this->paint = *paint; }
324 }
325 sk_sp<const SkImage> image;
326 int xs, ys, fs;
327 SkIRect src;
328 SkRect dst;
329 SkPaint paint;
draw__anon2adcf5c10111::DrawImageLattice330 void draw(SkCanvas* c, const SkMatrix&) const {
331 auto xdivs = pod<int>(this, 0),
332 ydivs = pod<int>(this, xs*sizeof(int));
333 auto colors = (0 == fs) ? nullptr :
334 pod<SkColor>(this, (xs+ys)*sizeof(int));
335 auto flags = (0 == fs) ? nullptr :
336 pod<SkCanvas::Lattice::RectType>(this, (xs+ys)*sizeof(int)+
337 fs*sizeof(SkColor));
338 c->drawImageLattice(image.get(), {xdivs, ydivs, flags, xs, ys, &src, colors}, dst,
339 &paint);
340 }
341 };
342
343 struct DrawText final : Op {
344 static const auto kType = Type::DrawText;
DrawText__anon2adcf5c10111::DrawText345 DrawText(size_t bytes, SkScalar x, SkScalar y, const SkPaint& paint)
346 : bytes(bytes), x(x), y(y), paint(paint) {}
347 size_t bytes;
348 SkScalar x,y;
349 SkPaint paint;
draw__anon2adcf5c10111::DrawText350 void draw(SkCanvas* c, const SkMatrix&) const {
351 c->drawText(pod<void>(this), bytes, x,y, paint);
352 }
353 };
354 struct DrawPosText final : Op {
355 static const auto kType = Type::DrawPosText;
DrawPosText__anon2adcf5c10111::DrawPosText356 DrawPosText(size_t bytes, const SkPaint& paint, int n)
357 : bytes(bytes), paint(paint), n(n) {}
358 size_t bytes;
359 SkPaint paint;
360 int n;
draw__anon2adcf5c10111::DrawPosText361 void draw(SkCanvas* c, const SkMatrix&) const {
362 auto points = pod<SkPoint>(this);
363 auto text = pod<void>(this, n*sizeof(SkPoint));
364 c->drawPosText(text, bytes, points, paint);
365 }
366 };
367 struct DrawPosTextH final : Op {
368 static const auto kType = Type::DrawPosTextH;
DrawPosTextH__anon2adcf5c10111::DrawPosTextH369 DrawPosTextH(size_t bytes, SkScalar y, const SkPaint& paint, int n)
370 : bytes(bytes), y(y), paint(paint), n(n) {}
371 size_t bytes;
372 SkScalar y;
373 SkPaint paint;
374 int n;
draw__anon2adcf5c10111::DrawPosTextH375 void draw(SkCanvas* c, const SkMatrix&) const {
376 auto xs = pod<SkScalar>(this);
377 auto text = pod<void>(this, n*sizeof(SkScalar));
378 c->drawPosTextH(text, bytes, xs, y, paint);
379 }
380 };
381 struct DrawTextOnPath final : Op {
382 static const auto kType = Type::DrawTextOnPath;
DrawTextOnPath__anon2adcf5c10111::DrawTextOnPath383 DrawTextOnPath(size_t bytes, const SkPath& path,
384 const SkMatrix* matrix, const SkPaint& paint)
385 : bytes(bytes), path(path), paint(paint) {
386 if (matrix) { this->matrix = *matrix; }
387 }
388 size_t bytes;
389 SkPath path;
390 SkMatrix matrix = SkMatrix::I();
391 SkPaint paint;
draw__anon2adcf5c10111::DrawTextOnPath392 void draw(SkCanvas* c, const SkMatrix&) const {
393 c->drawTextOnPath(pod<void>(this), bytes, path, &matrix, paint);
394 }
395 };
396 struct DrawTextRSXform final : Op {
397 static const auto kType = Type::DrawTextRSXform;
DrawTextRSXform__anon2adcf5c10111::DrawTextRSXform398 DrawTextRSXform(size_t bytes, int xforms, const SkRect* cull, const SkPaint& paint)
399 : bytes(bytes), xforms(xforms), paint(paint) {
400 if (cull) { this->cull = *cull; }
401 }
402 size_t bytes;
403 int xforms;
404 SkRect cull = kUnset;
405 SkPaint paint;
draw__anon2adcf5c10111::DrawTextRSXform406 void draw(SkCanvas* c, const SkMatrix&) const {
407 // For alignment, the SkRSXforms are first in the pod section, followed by the text.
408 c->drawTextRSXform(pod<void>(this,xforms*sizeof(SkRSXform)),
409 bytes,
410 pod<SkRSXform>(this),
411 maybe_unset(cull),
412 paint);
413 }
414 };
415 struct DrawTextBlob final : Op {
416 static const auto kType = Type::DrawTextBlob;
DrawTextBlob__anon2adcf5c10111::DrawTextBlob417 DrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint)
418 : blob(sk_ref_sp(blob)), x(x), y(y), paint(paint) {}
419 sk_sp<const SkTextBlob> blob;
420 SkScalar x,y;
421 SkPaint paint;
draw__anon2adcf5c10111::DrawTextBlob422 void draw(SkCanvas* c, const SkMatrix&) const {
423 c->drawTextBlob(blob.get(), x,y, paint);
424 }
425 };
426
427 struct DrawPatch final : Op {
428 static const auto kType = Type::DrawPatch;
DrawPatch__anon2adcf5c10111::DrawPatch429 DrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texs[4],
430 SkBlendMode bmode, const SkPaint& paint)
431 : xfermode(bmode), paint(paint)
432 {
433 copy_v(this->cubics, cubics, 12);
434 if (colors) { copy_v(this->colors, colors, 4); has_colors = true; }
435 if (texs ) { copy_v(this->texs , texs , 4); has_texs = true; }
436 }
437 SkPoint cubics[12];
438 SkColor colors[4];
439 SkPoint texs[4];
440 SkBlendMode xfermode;
441 SkPaint paint;
442 bool has_colors = false;
443 bool has_texs = false;
draw__anon2adcf5c10111::DrawPatch444 void draw(SkCanvas* c, const SkMatrix&) const {
445 c->drawPatch(cubics, has_colors ? colors : nullptr, has_texs ? texs : nullptr,
446 xfermode, paint);
447 }
448 };
449 struct DrawPoints final : Op {
450 static const auto kType = Type::DrawPoints;
DrawPoints__anon2adcf5c10111::DrawPoints451 DrawPoints(SkCanvas::PointMode mode, size_t count, const SkPaint& paint)
452 : mode(mode), count(count), paint(paint) {}
453 SkCanvas::PointMode mode;
454 size_t count;
455 SkPaint paint;
draw__anon2adcf5c10111::DrawPoints456 void draw(SkCanvas* c, const SkMatrix&) const {
457 c->drawPoints(mode, count, pod<SkPoint>(this), paint);
458 }
459 };
460 struct DrawVertices final : Op {
461 static const auto kType = Type::DrawVertices;
DrawVertices__anon2adcf5c10111::DrawVertices462 DrawVertices(const SkVertices* v, SkBlendMode m, const SkPaint& p)
463 : vertices(sk_ref_sp(const_cast<SkVertices*>(v))), mode(m), paint(p) {}
464 sk_sp<SkVertices> vertices;
465 SkBlendMode mode;
466 SkPaint paint;
draw__anon2adcf5c10111::DrawVertices467 void draw(SkCanvas* c, const SkMatrix&) const {
468 c->drawVertices(vertices, mode, paint);
469 }
470 };
471 struct DrawAtlas final : Op {
472 static const auto kType = Type::DrawAtlas;
DrawAtlas__anon2adcf5c10111::DrawAtlas473 DrawAtlas(const SkImage* atlas, int count, SkBlendMode xfermode,
474 const SkRect* cull, const SkPaint* paint, bool has_colors)
475 : atlas(sk_ref_sp(atlas)), count(count), xfermode(xfermode), has_colors(has_colors) {
476 if (cull) { this->cull = *cull; }
477 if (paint) { this->paint = *paint; }
478 }
479 sk_sp<const SkImage> atlas;
480 int count;
481 SkBlendMode xfermode;
482 SkRect cull = kUnset;
483 SkPaint paint;
484 bool has_colors;
draw__anon2adcf5c10111::DrawAtlas485 void draw(SkCanvas* c, const SkMatrix&) const {
486 auto xforms = pod<SkRSXform>(this, 0);
487 auto texs = pod<SkRect>(this, count*sizeof(SkRSXform));
488 auto colors = has_colors
489 ? pod<SkColor>(this, count*(sizeof(SkRSXform) + sizeof(SkRect)))
490 : nullptr;
491 c->drawAtlas(atlas.get(), xforms, texs, colors, count, xfermode,
492 maybe_unset(cull), &paint);
493 }
494 };
495 struct DrawShadowRec final : Op {
496 static const auto kType = Type::DrawShadowRec;
DrawShadowRec__anon2adcf5c10111::DrawShadowRec497 DrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec)
498 : fPath(path), fRec(rec)
499 {}
500 SkPath fPath;
501 SkDrawShadowRec fRec;
draw__anon2adcf5c10111::DrawShadowRec502 void draw(SkCanvas* c, const SkMatrix&) const {
503 c->private_draw_shadow_rec(fPath, fRec);
504 }
505 };
506 }
507
508 template <typename T, typename... Args>
push(size_t pod,Args &&...args)509 void* SkLiteDL::push(size_t pod, Args&&... args) {
510 size_t skip = SkAlignPtr(sizeof(T) + pod);
511 SkASSERT(skip < (1<<24));
512 if (fUsed + skip > fReserved) {
513 static_assert(SkIsPow2(SKLITEDL_PAGE), "This math needs updating for non-pow2.");
514 // Next greater multiple of SKLITEDL_PAGE.
515 fReserved = (fUsed + skip + SKLITEDL_PAGE) & ~(SKLITEDL_PAGE-1);
516 fBytes.realloc(fReserved);
517 }
518 SkASSERT(fUsed + skip <= fReserved);
519 auto op = (T*)(fBytes.get() + fUsed);
520 fUsed += skip;
521 new (op) T{ std::forward<Args>(args)... };
522 op->type = (uint32_t)T::kType;
523 op->skip = skip;
524 return op+1;
525 }
526
527 template <typename Fn, typename... Args>
map(const Fn fns[],Args...args) const528 inline void SkLiteDL::map(const Fn fns[], Args... args) const {
529 auto end = fBytes.get() + fUsed;
530 for (const uint8_t* ptr = fBytes.get(); ptr < end; ) {
531 auto op = (const Op*)ptr;
532 auto type = op->type;
533 auto skip = op->skip;
534 if (auto fn = fns[type]) { // We replace no-op functions with nullptrs
535 fn(op, args...); // to avoid the overhead of a pointless call.
536 }
537 ptr += skip;
538 }
539 }
540
541 #ifdef SK_SUPPORT_LEGACY_DRAWFILTER
setDrawFilter(SkDrawFilter * df)542 void SkLiteDL::setDrawFilter(SkDrawFilter* df) {
543 this->push<SetDrawFilter>(0, df);
544 }
545 #endif
546
flush()547 void SkLiteDL::flush() { this->push<Flush>(0); }
548
save()549 void SkLiteDL:: save() { this->push <Save>(0); }
restore()550 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)551 void SkLiteDL::saveLayer(const SkRect* bounds, const SkPaint* paint,
552 const SkImageFilter* backdrop, const SkImage* clipMask,
553 const SkMatrix* clipMatrix, SkCanvas::SaveLayerFlags flags) {
554 this->push<SaveLayer>(0, bounds, paint, backdrop, clipMask, clipMatrix, flags);
555 }
556
concat(const SkMatrix & matrix)557 void SkLiteDL:: concat(const SkMatrix& matrix) { this->push <Concat>(0, matrix); }
setMatrix(const SkMatrix & matrix)558 void SkLiteDL::setMatrix(const SkMatrix& matrix) { this->push<SetMatrix>(0, matrix); }
translate(SkScalar dx,SkScalar dy)559 void SkLiteDL::translate(SkScalar dx, SkScalar dy) { this->push<Translate>(0, dx, dy); }
560
clipPath(const SkPath & path,SkClipOp op,bool aa)561 void SkLiteDL::clipPath(const SkPath& path, SkClipOp op, bool aa) {
562 this->push<ClipPath>(0, path, op, aa);
563 }
clipRect(const SkRect & rect,SkClipOp op,bool aa)564 void SkLiteDL::clipRect(const SkRect& rect, SkClipOp op, bool aa) {
565 this->push<ClipRect>(0, rect, op, aa);
566 }
clipRRect(const SkRRect & rrect,SkClipOp op,bool aa)567 void SkLiteDL::clipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
568 this->push<ClipRRect>(0, rrect, op, aa);
569 }
clipRegion(const SkRegion & region,SkClipOp op)570 void SkLiteDL::clipRegion(const SkRegion& region, SkClipOp op) {
571 this->push<ClipRegion>(0, region, op);
572 }
573
drawPaint(const SkPaint & paint)574 void SkLiteDL::drawPaint(const SkPaint& paint) {
575 this->push<DrawPaint>(0, paint);
576 }
drawPath(const SkPath & path,const SkPaint & paint)577 void SkLiteDL::drawPath(const SkPath& path, const SkPaint& paint) {
578 this->push<DrawPath>(0, path, paint);
579 }
drawRect(const SkRect & rect,const SkPaint & paint)580 void SkLiteDL::drawRect(const SkRect& rect, const SkPaint& paint) {
581 this->push<DrawRect>(0, rect, paint);
582 }
drawRegion(const SkRegion & region,const SkPaint & paint)583 void SkLiteDL::drawRegion(const SkRegion& region, const SkPaint& paint) {
584 this->push<DrawRegion>(0, region, paint);
585 }
drawOval(const SkRect & oval,const SkPaint & paint)586 void SkLiteDL::drawOval(const SkRect& oval, const SkPaint& paint) {
587 this->push<DrawOval>(0, oval, paint);
588 }
drawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)589 void SkLiteDL::drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
590 const SkPaint& paint) {
591 this->push<DrawArc>(0, oval, startAngle, sweepAngle, useCenter, paint);
592 }
drawRRect(const SkRRect & rrect,const SkPaint & paint)593 void SkLiteDL::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
594 this->push<DrawRRect>(0, rrect, paint);
595 }
drawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)596 void SkLiteDL::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
597 this->push<DrawDRRect>(0, outer, inner, paint);
598 }
599
drawAnnotation(const SkRect & rect,const char * key,SkData * value)600 void SkLiteDL::drawAnnotation(const SkRect& rect, const char* key, SkData* value) {
601 size_t bytes = strlen(key)+1;
602 void* pod = this->push<DrawAnnotation>(bytes, rect, value);
603 copy_v(pod, key,bytes);
604 }
drawDrawable(SkDrawable * drawable,const SkMatrix * matrix)605 void SkLiteDL::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
606 this->push<DrawDrawable>(0, drawable, matrix);
607 }
drawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)608 void SkLiteDL::drawPicture(const SkPicture* picture,
609 const SkMatrix* matrix, const SkPaint* paint) {
610 this->push<DrawPicture>(0, picture, matrix, paint);
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.fRectTypes ? (xs + 1) * (ys + 1) : 0;
627 size_t bytes = (xs + ys) * sizeof(int) + fs * sizeof(SkCanvas::Lattice::RectType)
628 + fs * sizeof(SkColor);
629 SkASSERT(lattice.fBounds);
630 void* pod = this->push<DrawImageLattice>(bytes, std::move(image), xs, ys, fs, *lattice.fBounds,
631 dst, paint);
632 copy_v(pod, lattice.fXDivs, xs,
633 lattice.fYDivs, ys,
634 lattice.fColors, fs,
635 lattice.fRectTypes, fs);
636 }
637
drawText(const void * text,size_t bytes,SkScalar x,SkScalar y,const SkPaint & paint)638 void SkLiteDL::drawText(const void* text, size_t bytes,
639 SkScalar x, SkScalar y, const SkPaint& paint) {
640 void* pod = this->push<DrawText>(bytes, bytes, x, y, paint);
641 copy_v(pod, (const char*)text,bytes);
642 }
drawPosText(const void * text,size_t bytes,const SkPoint pos[],const SkPaint & paint)643 void SkLiteDL::drawPosText(const void* text, size_t bytes,
644 const SkPoint pos[], const SkPaint& paint) {
645 int n = paint.countText(text, bytes);
646 void* pod = this->push<DrawPosText>(n*sizeof(SkPoint)+bytes, bytes, paint, n);
647 copy_v(pod, pos,n, (const char*)text,bytes);
648 }
drawPosTextH(const void * text,size_t bytes,const SkScalar xs[],SkScalar y,const SkPaint & paint)649 void SkLiteDL::drawPosTextH(const void* text, size_t bytes,
650 const SkScalar xs[], SkScalar y, const SkPaint& paint) {
651 int n = paint.countText(text, bytes);
652 void* pod = this->push<DrawPosTextH>(n*sizeof(SkScalar)+bytes, bytes, y, paint, n);
653 copy_v(pod, xs,n, (const char*)text,bytes);
654 }
drawTextOnPath(const void * text,size_t bytes,const SkPath & path,const SkMatrix * matrix,const SkPaint & paint)655 void SkLiteDL::drawTextOnPath(const void* text, size_t bytes,
656 const SkPath& path, const SkMatrix* matrix, const SkPaint& paint) {
657 void* pod = this->push<DrawTextOnPath>(bytes, bytes, path, matrix, paint);
658 copy_v(pod, (const char*)text,bytes);
659 }
drawTextRSXform(const void * text,size_t bytes,const SkRSXform xforms[],const SkRect * cull,const SkPaint & paint)660 void SkLiteDL::drawTextRSXform(const void* text, size_t bytes,
661 const SkRSXform xforms[], const SkRect* cull, const SkPaint& paint) {
662 int n = paint.countText(text, bytes);
663 void* pod = this->push<DrawTextRSXform>(bytes+n*sizeof(SkRSXform), bytes, n, cull, paint);
664 copy_v(pod, xforms,n, (const char*)text,bytes);
665 }
drawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)666 void SkLiteDL::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) {
667 this->push<DrawTextBlob>(0, blob, x,y, paint);
668 }
669
drawPatch(const SkPoint points[12],const SkColor colors[4],const SkPoint texs[4],SkBlendMode bmode,const SkPaint & paint)670 void SkLiteDL::drawPatch(const SkPoint points[12], const SkColor colors[4], const SkPoint texs[4],
671 SkBlendMode bmode, const SkPaint& paint) {
672 this->push<DrawPatch>(0, points, colors, texs, bmode, paint);
673 }
drawPoints(SkCanvas::PointMode mode,size_t count,const SkPoint points[],const SkPaint & paint)674 void SkLiteDL::drawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint points[],
675 const SkPaint& paint) {
676 void* pod = this->push<DrawPoints>(count*sizeof(SkPoint), mode, count, paint);
677 copy_v(pod, points,count);
678 }
drawVertices(const SkVertices * vertices,SkBlendMode mode,const SkPaint & paint)679 void SkLiteDL::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) {
680 this->push<DrawVertices>(0, vertices, mode, paint);
681 }
drawAtlas(const SkImage * atlas,const SkRSXform xforms[],const SkRect texs[],const SkColor colors[],int count,SkBlendMode xfermode,const SkRect * cull,const SkPaint * paint)682 void SkLiteDL::drawAtlas(const SkImage* atlas, const SkRSXform xforms[], const SkRect texs[],
683 const SkColor colors[], int count, SkBlendMode xfermode,
684 const SkRect* cull, const SkPaint* paint) {
685 size_t bytes = count*(sizeof(SkRSXform) + sizeof(SkRect));
686 if (colors) {
687 bytes += count*sizeof(SkColor);
688 }
689 void* pod = this->push<DrawAtlas>(bytes,
690 atlas, count, xfermode, cull, paint, colors != nullptr);
691 copy_v(pod, xforms, count,
692 texs, count,
693 colors, colors ? count : 0);
694 }
drawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)695 void SkLiteDL::drawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
696 this->push<DrawShadowRec>(0, path, rec);
697 }
698
699 typedef void(*draw_fn)(const void*, SkCanvas*, const SkMatrix&);
700 typedef void(*void_fn)(const void*);
701
702 // All ops implement draw().
703 #define M(T) [](const void* op, SkCanvas* c, const SkMatrix& original) { \
704 ((const T*)op)->draw(c, original); \
705 },
706 static const draw_fn draw_fns[] = { TYPES(M) };
707 #undef M
708
709 // Older libstdc++ has pre-standard std::has_trivial_destructor.
710 #if defined(__GLIBCXX__) && (__GLIBCXX__ < 20130000)
711 template <typename T> using can_skip_destructor = std::has_trivial_destructor<T>;
712 #else
713 template <typename T> using can_skip_destructor = std::is_trivially_destructible<T>;
714 #endif
715
716 // Most state ops (matrix, clip, save, restore) have a trivial destructor.
717 #define M(T) !can_skip_destructor<T>::value ? [](const void* op) { ((const T*)op)->~T(); } \
718 : (void_fn)nullptr,
719 static const void_fn dtor_fns[] = { TYPES(M) };
720 #undef M
721
draw(SkCanvas * canvas) const722 void SkLiteDL::draw(SkCanvas* canvas) const {
723 SkAutoCanvasRestore acr(canvas, false);
724 this->map(draw_fns, canvas, canvas->getTotalMatrix());
725 }
726
~SkLiteDL()727 SkLiteDL::~SkLiteDL() {
728 this->reset();
729 }
730
reset()731 void SkLiteDL::reset() {
732 this->map(dtor_fns);
733
734 // Leave fBytes and fReserved alone.
735 fUsed = 0;
736 }
737