blob: f0d3772e9fbbc426d57b100481a09ab39234154b [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) {
21 if (!this->canSkip(r)) {
22 this->draw(r);
23 this->updateClip<T>();
24 }
25 }
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000026
27private:
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000028 // Can we skip this command right now?
29 template <typename T> bool canSkip(const T&) const {
30 // We can skip most commands if the clip is empty. Exceptions are specialized below.
31 return fClipEmpty;
32 }
33
34 // No base case, so we'll be compile-time checked that we implemented all possibilities below.
35 template <typename T> void draw(const T&);
36
37 // Update fClipEmpty if necessary.
38 template <typename T> void updateClip() {
39 // Most commands don't change the clip. Exceptions are specialized below.
40 }
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000041
42 SkCanvas* fCanvas;
43 unsigned fIndex;
44 bool fClipEmpty;
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000045};
46
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000047// These commands may change the clip.
48#define UPDATE_CLIP(T) template <> void Draw::updateClip<SkRecords::T>() \
49 { fClipEmpty = fCanvas->isClipEmpty(); }
50UPDATE_CLIP(Restore);
51UPDATE_CLIP(SaveLayer);
52UPDATE_CLIP(ClipPath);
53UPDATE_CLIP(ClipRRect);
54UPDATE_CLIP(ClipRect);
55UPDATE_CLIP(ClipRegion);
56#undef UPDATE_CLIP
57
58// These commands must always run.
59#define CAN_SKIP(T) template <> bool Draw::canSkip(const SkRecords::T&) const { return false; }
60CAN_SKIP(Restore);
61CAN_SKIP(Save);
62CAN_SKIP(SaveLayer);
63CAN_SKIP(Clear);
64CAN_SKIP(PushCull);
65CAN_SKIP(PopCull);
66#undef CAN_SKIP
67
68// We can skip these commands if they're intersecting with a clip that's already empty.
69#define CAN_SKIP(T) template <> bool Draw::canSkip(const SkRecords::T& r) const \
70 { return fClipEmpty && SkRegion::kIntersect_Op == r.op; }
71CAN_SKIP(ClipPath);
72CAN_SKIP(ClipRRect);
73CAN_SKIP(ClipRect);
74CAN_SKIP(ClipRegion);
75#undef CAN_SKIP
76
77static bool can_skip_text(const SkCanvas& c, const SkPaint& p, SkScalar minY, SkScalar maxY) {
78 // If we're drawing vertical text, none of the checks we're about to do make any sense.
79 // We use canComputeFastBounds as a proxy for "is this text going to be rectangular?".
80 if (p.isVerticalText() || !p.canComputeFastBounds()) {
81 return false;
82 }
83
84 // Rather than checking the top and bottom font metrics, we guess. Actually looking up the top
85 // and bottom metrics is slow, and this overapproximation should be good enough.
86 const SkScalar buffer = p.getTextSize() * 1.5f;
87 SkDEBUGCODE(SkPaint::FontMetrics metrics;)
88 SkDEBUGCODE(p.getFontMetrics(&metrics);)
89 SkASSERT(-buffer <= metrics.fTop);
90 SkASSERT(+buffer >= metrics.fBottom);
91 return c.quickRejectY(minY - buffer, maxY + buffer);
92}
93
94template <> bool Draw::canSkip(const SkRecords::DrawPosTextH& r) const {
95 return fClipEmpty || can_skip_text(*fCanvas, r.paint, r.y, r.y);
96}
97
98template <> bool Draw::canSkip(const SkRecords::DrawPosText& r) const {
99 if (fClipEmpty) {
100 return true;
101 }
102
103 // TODO(mtklein): may want to move this minY/maxY calculation into a one-time pass
104 const unsigned points = r.paint.countText(r.text, r.byteLength);
105 if (points == 0) {
106 return true;
107 }
108 SkScalar minY = SK_ScalarInfinity, maxY = SK_ScalarNegativeInfinity;
109 for (unsigned i = 0; i < points; i++) {
110 minY = SkTMin(minY, r.pos[i].fY);
111 maxY = SkTMax(maxY, r.pos[i].fY);
112 }
113
114 return can_skip_text(*fCanvas, r.paint, minY, maxY);
115}
116
117#define DRAW(T, call) template <> void Draw::draw(const SkRecords::T& r) { fCanvas->call; }
118DRAW(Restore, restore());
119DRAW(Save, save(r.flags));
120DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags));
121DRAW(PopCull, popCull());
122DRAW(Clear, clear(r.color));
123DRAW(Concat, concat(r.matrix));
124DRAW(SetMatrix, setMatrix(r.matrix));
125
126DRAW(ClipPath, clipPath(r.path, r.op, r.doAA));
127DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA));
128DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA));
129DRAW(ClipRegion, clipRegion(r.region, r.op));
130
131DRAW(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint));
132DRAW(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint));
133DRAW(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint));
134DRAW(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint, r.flags));
135DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
136DRAW(DrawOval, drawOval(r.oval, r.paint));
137DRAW(DrawPaint, drawPaint(r.paint));
138DRAW(DrawPath, drawPath(r.path, r.paint));
139DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
140DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
141DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
142DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
143DRAW(DrawRect, drawRect(r.rect, r.paint));
144DRAW(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint));
145DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
146DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint));
147DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
148 r.xmode.get(), r.indices, r.indexCount, r.paint));
149#undef DRAW
150
151// PushCull is a bit of a oddball. We might be able to just skip until just past its popCull.
152template <> void Draw::draw(const SkRecords::PushCull& r) {
153 if (r.popOffset != SkRecords::kUnsetPopOffset && fCanvas->quickReject(r.rect)) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000154 fIndex += r.popOffset;
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +0000155 } else {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000156 fCanvas->pushCull(r.rect);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +0000157 }
158}
159
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +0000160} // namespace
161
162void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) {
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +0000163 for (Draw draw(canvas); draw.index() < record.count(); draw.next()) {
164 record.visit(draw.index(), draw);
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +0000165 }
166}