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