OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkRecordDraw.h" | 8 #include "SkRecordDraw.h" |
9 | 9 |
10 namespace { | 10 namespace { |
11 | 11 |
12 // This is an SkRecord visitor that will draw that SkRecord to an SkCanvas. | 12 // This is an SkRecord visitor that will draw that SkRecord to an SkCanvas. |
13 class Draw : SkNoncopyable { | 13 class Draw : SkNoncopyable { |
14 public: | 14 public: |
15 explicit Draw(SkCanvas* canvas) : fCanvas(canvas), fIndex(0), fClipEmpty(fal
se) {} | 15 explicit Draw(SkCanvas* canvas) : fCanvas(canvas), fIndex(0), fClipEmpty(fal
se) {} |
16 | 16 |
17 unsigned index() const { return fIndex; } | 17 unsigned index() const { return fIndex; } |
18 void next() { ++fIndex; } | 18 void next() { ++fIndex; } |
19 | 19 |
20 // No base case, so we'll be compile-time checked that we implemented all po
ssibilities below. | 20 template <typename T> void operator()(const T& r) { |
21 template <typename T> void operator()(const T&); | 21 if (!this->canSkip(r)) { |
| 22 this->draw(r); |
| 23 this->updateClip<T>(); |
| 24 } |
| 25 } |
22 | 26 |
23 private: | 27 private: |
24 // Must be called after any potential clip change. | 28 // Can we skip this command right now? |
25 void updateClip() { fClipEmpty = fCanvas->isClipEmpty(); } | 29 template <typename T> bool canSkip(const T&) const { |
| 30 // We can skip most commands if the clip is empty. Exceptions are speci
alized below. |
| 31 return fClipEmpty; |
| 32 } |
| 33 |
| 34 // No base case, so we'll be compile-time checked that we implemented all po
ssibilities 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 belo
w. |
| 40 } |
26 | 41 |
27 SkCanvas* fCanvas; | 42 SkCanvas* fCanvas; |
28 unsigned fIndex; | 43 unsigned fIndex; |
29 bool fClipEmpty; | 44 bool fClipEmpty; |
30 }; | 45 }; |
31 | 46 |
32 template <> void Draw::operator()(const SkRecords::PushCull& r) { | 47 // These commands may change the clip. |
33 if (r.popOffset != SkRecords::kUnsetPopOffset && | 48 #define UPDATE_CLIP(T) template <> void Draw::updateClip<SkRecords::T>() \ |
34 fCanvas->quickReject(r.rect)) { | 49 { fClipEmpty = fCanvas->isClipEmpty(); } |
35 // We skip to the popCull, then the loop moves us just beyond it. | 50 UPDATE_CLIP(Restore); |
| 51 UPDATE_CLIP(SaveLayer); |
| 52 UPDATE_CLIP(ClipPath); |
| 53 UPDATE_CLIP(ClipRRect); |
| 54 UPDATE_CLIP(ClipRect); |
| 55 UPDATE_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; } |
| 60 CAN_SKIP(Restore); |
| 61 CAN_SKIP(Save); |
| 62 CAN_SKIP(SaveLayer); |
| 63 CAN_SKIP(Clear); |
| 64 CAN_SKIP(PushCull); |
| 65 CAN_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; } |
| 71 CAN_SKIP(ClipPath); |
| 72 CAN_SKIP(ClipRRect); |
| 73 CAN_SKIP(ClipRect); |
| 74 CAN_SKIP(ClipRegion); |
| 75 #undef CAN_SKIP |
| 76 |
| 77 static bool can_skip_text(const SkCanvas& c, const SkPaint& p, SkScalar minY, Sk
Scalar 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 rect
angular?". |
| 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 eno
ugh. |
| 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 |
| 94 template <> bool Draw::canSkip(const SkRecords::DrawPosTextH& r) const { |
| 95 return fClipEmpty || can_skip_text(*fCanvas, r.paint, r.y, r.y); |
| 96 } |
| 97 |
| 98 template <> 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-tim
e 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) { fCanv
as->call; } |
| 118 DRAW(Restore, restore()); |
| 119 DRAW(Save, save(r.flags)); |
| 120 DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags)); |
| 121 DRAW(PopCull, popCull()); |
| 122 DRAW(Clear, clear(r.color)); |
| 123 DRAW(Concat, concat(r.matrix)); |
| 124 DRAW(SetMatrix, setMatrix(r.matrix)); |
| 125 |
| 126 DRAW(ClipPath, clipPath(r.path, r.op, r.doAA)); |
| 127 DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA)); |
| 128 DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA)); |
| 129 DRAW(ClipRegion, clipRegion(r.region, r.op)); |
| 130 |
| 131 DRAW(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint)); |
| 132 DRAW(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint)); |
| 133 DRAW(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint)); |
| 134 DRAW(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint,
r.flags)); |
| 135 DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint)); |
| 136 DRAW(DrawOval, drawOval(r.oval, r.paint)); |
| 137 DRAW(DrawPaint, drawPaint(r.paint)); |
| 138 DRAW(DrawPath, drawPath(r.path, r.paint)); |
| 139 DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint)); |
| 140 DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint)); |
| 141 DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint)); |
| 142 DRAW(DrawRRect, drawRRect(r.rrect, r.paint)); |
| 143 DRAW(DrawRect, drawRect(r.rect, r.paint)); |
| 144 DRAW(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint)); |
| 145 DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint)); |
| 146 DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.pa
int)); |
| 147 DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.co
lors, |
| 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 pas
t its popCull. |
| 152 template <> void Draw::draw(const SkRecords::PushCull& r) { |
| 153 if (r.popOffset != SkRecords::kUnsetPopOffset && fCanvas->quickReject(r.rect
)) { |
36 fIndex += r.popOffset; | 154 fIndex += r.popOffset; |
37 } else { | 155 } else { |
38 fCanvas->pushCull(r.rect); | 156 fCanvas->pushCull(r.rect); |
39 } | 157 } |
40 } | 158 } |
41 | 159 |
42 // These commands might change the clip. | |
43 #define CASE(T, call) \ | |
44 template <> void Draw::operator()(const SkRecords::T& r) { fCanvas->call; th
is->updateClip(); } | |
45 CASE(Restore, restore()); | |
46 CASE(SaveLayer, saveLayer(r.bounds, r.paint, r.flags)); | |
47 #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 } | |
54 CASE(ClipPath, clipPath(r.path, r.op, r.doAA)); | |
55 CASE(ClipRRect, clipRRect(r.rrect, r.op, r.doAA)); | |
56 CASE(ClipRect, clipRect(r.rect, r.op, r.doAA)); | |
57 CASE(ClipRegion, clipRegion(r.region, r.op)); | |
58 #undef CASE | |
59 | |
60 // Commands which must run regardless of the clip, but don't change it themselve
s. | |
61 #define CASE(T, call) \ | |
62 template <> void Draw::operator()(const SkRecords::T& r) { fCanvas->call; } | |
63 CASE(Save, save(r.flags)); | |
64 CASE(Clear, clear(r.color)); | |
65 CASE(PopCull, popCull()); | |
66 #undef CASE | |
67 | |
68 // 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; } | |
71 CASE(Concat, concat(r.matrix)); | |
72 CASE(SetMatrix, setMatrix(r.matrix)); | |
73 | |
74 CASE(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint)); | |
75 CASE(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint)); | |
76 CASE(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint)); | |
77 CASE(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint,
r.flags)); | |
78 CASE(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint)); | |
79 CASE(DrawOval, drawOval(r.oval, r.paint)); | |
80 CASE(DrawPaint, drawPaint(r.paint)); | |
81 CASE(DrawPath, drawPath(r.path, r.paint)); | |
82 CASE(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint)); | |
83 CASE(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint)); | |
84 CASE(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint)); | |
85 CASE(DrawRRect, drawRRect(r.rrect, r.paint)); | |
86 CASE(DrawRect, drawRect(r.rect, r.paint)); | |
87 CASE(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint)); | |
88 CASE(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint)); | |
89 CASE(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.pa
int)); | |
90 CASE(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.co
lors, | |
91 r.xmode.get(), r.indices, r.indexCount, r.paint)
); | |
92 #undef CASE | |
93 | |
94 } // namespace | 160 } // namespace |
95 | 161 |
96 void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) { | 162 void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) { |
97 for (Draw draw(canvas); draw.index() < record.count(); draw.next()) { | 163 for (Draw draw(canvas); draw.index() < record.count(); draw.next()) { |
98 record.visit(draw.index(), draw); | 164 record.visit(draw.index(), draw); |
99 } | 165 } |
100 } | 166 } |
OLD | NEW |