blob: 4782344bed241b66337c534f96b85e97eb72f961 [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));
40CASE(ClipPath, clipPath(r.path, r.op, r.doAA));
41CASE(ClipRRect, clipRRect(r.rrect, r.op, r.doAA));
42CASE(ClipRect, clipRect(r.rect, r.op, r.doAA));
43CASE(ClipRegion, clipRegion(r.region, r.op));
44#undef CASE
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000045
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000046// Commands which must run regardless of the clip.
47#define CASE(T, call) \
48 template <> void Draw::operator()(const SkRecords::T& r) { fCanvas->call; }
49CASE(Save, save(r.flags));
50CASE(Clear, clear(r.color));
51CASE(PopCull, popCull());
52#undef CASE
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000053
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000054// Nothing fancy below here. These commands respect and don't change the clip.
55#define CASE(T, call) \
56 template <> void Draw::operator()(const SkRecords::T& r) { if (!fClipEmpty) fCanvas->call; }
57CASE(Concat, concat(r.matrix));
58CASE(SetMatrix, setMatrix(r.matrix));
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000059
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000060CASE(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint));
61CASE(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint));
62CASE(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint));
63CASE(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint, r.flags));
64CASE(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
65CASE(DrawOval, drawOval(r.oval, r.paint));
66CASE(DrawPaint, drawPaint(r.paint));
67CASE(DrawPath, drawPath(r.path, r.paint));
68CASE(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
69CASE(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
70CASE(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
71CASE(DrawRRect, drawRRect(r.rrect, r.paint));
72CASE(DrawRect, drawRect(r.rect, r.paint));
73CASE(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint));
74CASE(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
75CASE(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint));
76CASE(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
77 r.xmode.get(), r.indices, r.indexCount, r.paint));
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000078#undef CASE
79
80} // namespace
81
82void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000083 for (Draw draw(canvas); draw.index() < record.count(); draw.next()) {
84 record.visit(draw.index(), draw);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000085 }
86}