blob: ddde2fede80ed1a528fe7143a9c97e87fc9b124f [file] [log] [blame]
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +00001#include "SkRecordDraw.h"
2
3namespace {
4
5// This is an SkRecord visitor that will draw that SkRecord to an SkCanvas.
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +00006class Draw : SkNoncopyable {
7public:
8 explicit Draw(SkCanvas* canvas) : fCanvas(canvas), fIndex(0), fClipEmpty(false) {}
9
10 unsigned index() const { return fIndex; }
11 void next() { ++fIndex; }
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000012
13 // No base case, so we'll be compile-time checked that we implemented all possibilities below.
14 template <typename T> void operator()(const T&);
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000015
16private:
17 // Must be called after any potential clip change.
18 void updateClip() { fClipEmpty = fCanvas->isClipEmpty(); }
19
20 SkCanvas* fCanvas;
21 unsigned fIndex;
22 bool fClipEmpty;
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000023};
24
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000025template <> void Draw::operator()(const SkRecords::PushCull& r) {
26 if (r.popOffset != SkRecords::kUnsetPopOffset &&
27 fCanvas->quickReject(r.rect)) {
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000028 // We skip to the popCull, then the loop moves us just beyond it.
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000029 fIndex += r.popOffset;
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000030 } else {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000031 fCanvas->pushCull(r.rect);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000032 }
33}
34
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000035// These commands might change the clip.
36#define CASE(T, call) \
37 template <> void Draw::operator()(const SkRecords::T& r) { fCanvas->call; this->updateClip(); }
38CASE(Restore, restore());
39CASE(SaveLayer, saveLayer(r.bounds, r.paint, r.flags));
commit-bot@chromium.orgff2de7c2014-04-10 02:26:33 +000040#undef CASE
41
42// These certainly do change the clip,
43// but we can skip them if they're intersecting with a clip that's already empty.
44#define CASE(T, call) template <> void Draw::operator()(const SkRecords::T& r) { \
45 if (!(fClipEmpty && SkRegion::kIntersect_Op == r.op)) { fCanvas->call; this->updateClip(); } \
46}
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000047CASE(ClipPath, clipPath(r.path, r.op, r.doAA));
48CASE(ClipRRect, clipRRect(r.rrect, r.op, r.doAA));
49CASE(ClipRect, clipRect(r.rect, r.op, r.doAA));
50CASE(ClipRegion, clipRegion(r.region, r.op));
51#undef CASE
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000052
commit-bot@chromium.orgff2de7c2014-04-10 02:26:33 +000053// Commands which must run regardless of the clip, but don't change it themselves.
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000054#define CASE(T, call) \
55 template <> void Draw::operator()(const SkRecords::T& r) { fCanvas->call; }
56CASE(Save, save(r.flags));
57CASE(Clear, clear(r.color));
58CASE(PopCull, popCull());
59#undef CASE
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000060
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000061// Nothing fancy below here. These commands respect and don't change the clip.
62#define CASE(T, call) \
63 template <> void Draw::operator()(const SkRecords::T& r) { if (!fClipEmpty) fCanvas->call; }
64CASE(Concat, concat(r.matrix));
65CASE(SetMatrix, setMatrix(r.matrix));
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000066
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000067CASE(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint));
68CASE(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint));
69CASE(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint));
70CASE(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint, r.flags));
71CASE(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
72CASE(DrawOval, drawOval(r.oval, r.paint));
73CASE(DrawPaint, drawPaint(r.paint));
74CASE(DrawPath, drawPath(r.path, r.paint));
75CASE(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
76CASE(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
77CASE(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
78CASE(DrawRRect, drawRRect(r.rrect, r.paint));
79CASE(DrawRect, drawRect(r.rect, r.paint));
80CASE(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint));
81CASE(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
82CASE(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint));
83CASE(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
84 r.xmode.get(), r.indices, r.indexCount, r.paint));
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000085#undef CASE
86
87} // namespace
88
89void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000090 for (Draw draw(canvas); draw.index() < record.count(); draw.next()) {
91 record.visit(draw.index(), draw);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000092 }
93}