blob: 21b2c7a82b33a630e4769554dfa444cc05c13890 [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
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000020 template <typename T> void operator()(const T& r) {
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000021 if (!this->skip(r)) {
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000022 this->draw(r);
23 this->updateClip<T>();
24 }
25 }
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000026
27private:
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000028 // Return true if we can skip this command, false if not.
29 // Update fIndex here directly to skip more than just this one command.
30 template <typename T> bool skip(const T&) {
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000031 // We can skip most commands if the clip is empty. Exceptions are specialized below.
32 return fClipEmpty;
33 }
34
35 // No base case, so we'll be compile-time checked that we implemented all possibilities below.
36 template <typename T> void draw(const T&);
37
38 // Update fClipEmpty if necessary.
39 template <typename T> void updateClip() {
40 // Most commands don't change the clip. Exceptions are specialized below.
41 }
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000042
43 SkCanvas* fCanvas;
44 unsigned fIndex;
45 bool fClipEmpty;
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000046};
47
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000048// TODO(mtklein): do this specialization with template traits instead of macros
49
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000050// These commands may change the clip.
51#define UPDATE_CLIP(T) template <> void Draw::updateClip<SkRecords::T>() \
52 { fClipEmpty = fCanvas->isClipEmpty(); }
53UPDATE_CLIP(Restore);
54UPDATE_CLIP(SaveLayer);
55UPDATE_CLIP(ClipPath);
56UPDATE_CLIP(ClipRRect);
57UPDATE_CLIP(ClipRect);
58UPDATE_CLIP(ClipRegion);
59#undef UPDATE_CLIP
60
61// These commands must always run.
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000062#define SKIP(T) template <> bool Draw::skip(const SkRecords::T&) { return false; }
63SKIP(Restore);
64SKIP(Save);
65SKIP(SaveLayer);
66SKIP(Clear);
67SKIP(PushCull);
68SKIP(PopCull);
69#undef SKIP
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000070
71// We can skip these commands if they're intersecting with a clip that's already empty.
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000072#define SKIP(T) template <> bool Draw::skip(const SkRecords::T& r) \
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000073 { return fClipEmpty && SkRegion::kIntersect_Op == r.op; }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000074SKIP(ClipPath);
75SKIP(ClipRRect);
76SKIP(ClipRect);
77SKIP(ClipRegion);
78#undef SKIP
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000079
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000080// NoOps can always be skipped and draw nothing.
81template <> bool Draw::skip(const SkRecords::NoOp&) { return true; }
82template <> void Draw::draw(const SkRecords::NoOp&) {}
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000083
84#define DRAW(T, call) template <> void Draw::draw(const SkRecords::T& r) { fCanvas->call; }
85DRAW(Restore, restore());
86DRAW(Save, save(r.flags));
87DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags));
88DRAW(PopCull, popCull());
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000089DRAW(PushCull, pushCull(r.rect));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000090DRAW(Clear, clear(r.color));
91DRAW(Concat, concat(r.matrix));
92DRAW(SetMatrix, setMatrix(r.matrix));
93
94DRAW(ClipPath, clipPath(r.path, r.op, r.doAA));
95DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA));
96DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA));
97DRAW(ClipRegion, clipRegion(r.region, r.op));
98
99DRAW(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint));
100DRAW(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint));
101DRAW(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint));
102DRAW(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint, r.flags));
103DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
104DRAW(DrawOval, drawOval(r.oval, r.paint));
105DRAW(DrawPaint, drawPaint(r.paint));
106DRAW(DrawPath, drawPath(r.path, r.paint));
107DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
108DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
109DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
110DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
111DRAW(DrawRect, drawRect(r.rect, r.paint));
112DRAW(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint));
113DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
114DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint));
115DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
116 r.xmode.get(), r.indices, r.indexCount, r.paint));
117#undef DRAW
118
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000119// Added by SkRecordAnnotateCullingPairs.
120template <> bool Draw::skip(const SkRecords::PairedPushCull& r) {
121 if (fCanvas->quickReject(r.base->rect)) {
122 fIndex += r.skip;
123 return true;
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +0000124 }
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000125 return false;
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +0000126}
127
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +0000128// Added by SkRecordBoundDrawPosTextH
129template <> bool Draw::skip(const SkRecords::BoundedDrawPosTextH& r) {
130 return fClipEmpty || fCanvas->quickRejectY(r.minY, r.maxY);
131}
132
133// These draw by proxying to the commands they wrap. (All the optimization is for skip().)
134#define DRAW(T) template <> void Draw::draw(const SkRecords::T& r) { this->draw(*r.base); }
135DRAW(PairedPushCull);
136DRAW(BoundedDrawPosTextH);
137#undef DRAW
138
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +0000139} // namespace
140
141void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000142 for (Draw draw(canvas); draw.index() < record.count(); draw.next()) {
143 record.visit(draw.index(), draw);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +0000144 }
145}