commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 1 | #include "SkRecordDraw.h" |
| 2 | |
| 3 | namespace { |
| 4 | |
| 5 | // This is an SkRecord visitor that will draw that SkRecord to an SkCanvas. |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 6 | class Draw : SkNoncopyable { |
| 7 | public: |
| 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.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 12 | |
| 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.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 15 | |
| 16 | private: |
| 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.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 23 | }; |
| 24 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 25 | template <> void Draw::operator()(const SkRecords::PushCull& r) { |
| 26 | if (r.popOffset != SkRecords::kUnsetPopOffset && |
| 27 | fCanvas->quickReject(r.rect)) { |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 28 | // We skip to the popCull, then the loop moves us just beyond it. |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 29 | fIndex += r.popOffset; |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 30 | } else { |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 31 | fCanvas->pushCull(r.rect); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 35 | // These commands might change the clip. |
| 36 | #define CASE(T, call) \ |
| 37 | template <> void Draw::operator()(const SkRecords::T& r) { fCanvas->call; this->updateClip(); } |
| 38 | CASE(Restore, restore()); |
| 39 | CASE(SaveLayer, saveLayer(r.bounds, r.paint, r.flags)); |
commit-bot@chromium.org | ff2de7c | 2014-04-10 02:26:33 +0000 | [diff] [blame^] | 40 | #undef CASE |
| 41 | |
| 42 | // These certainly do change the clip, |
| 43 | // but we can skip them if they're intersecting with a clip that's already empty. |
| 44 | #define CASE(T, call) template <> void Draw::operator()(const SkRecords::T& r) { \ |
| 45 | if (!(fClipEmpty && SkRegion::kIntersect_Op == r.op)) { fCanvas->call; this->updateClip(); } \ |
| 46 | } |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 47 | CASE(ClipPath, clipPath(r.path, r.op, r.doAA)); |
| 48 | CASE(ClipRRect, clipRRect(r.rrect, r.op, r.doAA)); |
| 49 | CASE(ClipRect, clipRect(r.rect, r.op, r.doAA)); |
| 50 | CASE(ClipRegion, clipRegion(r.region, r.op)); |
| 51 | #undef CASE |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 52 | |
commit-bot@chromium.org | ff2de7c | 2014-04-10 02:26:33 +0000 | [diff] [blame^] | 53 | // Commands which must run regardless of the clip, but don't change it themselves. |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 54 | #define CASE(T, call) \ |
| 55 | template <> void Draw::operator()(const SkRecords::T& r) { fCanvas->call; } |
| 56 | CASE(Save, save(r.flags)); |
| 57 | CASE(Clear, clear(r.color)); |
| 58 | CASE(PopCull, popCull()); |
| 59 | #undef CASE |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 60 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 61 | // Nothing fancy below here. These commands respect and don't change the clip. |
| 62 | #define CASE(T, call) \ |
| 63 | template <> void Draw::operator()(const SkRecords::T& r) { if (!fClipEmpty) fCanvas->call; } |
| 64 | CASE(Concat, concat(r.matrix)); |
| 65 | CASE(SetMatrix, setMatrix(r.matrix)); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 66 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 67 | CASE(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint)); |
| 68 | CASE(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint)); |
| 69 | CASE(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint)); |
| 70 | CASE(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint, r.flags)); |
| 71 | CASE(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint)); |
| 72 | CASE(DrawOval, drawOval(r.oval, r.paint)); |
| 73 | CASE(DrawPaint, drawPaint(r.paint)); |
| 74 | CASE(DrawPath, drawPath(r.path, r.paint)); |
| 75 | CASE(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint)); |
| 76 | CASE(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint)); |
| 77 | CASE(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint)); |
| 78 | CASE(DrawRRect, drawRRect(r.rrect, r.paint)); |
| 79 | CASE(DrawRect, drawRect(r.rect, r.paint)); |
| 80 | CASE(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint)); |
| 81 | CASE(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint)); |
| 82 | CASE(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint)); |
| 83 | CASE(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors, |
| 84 | r.xmode.get(), r.indices, r.indexCount, r.paint)); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 85 | #undef CASE |
| 86 | |
| 87 | } // namespace |
| 88 | |
| 89 | void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) { |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 90 | for (Draw draw(canvas); draw.index() < record.count(); draw.next()) { |
| 91 | record.visit(draw.index(), draw); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 92 | } |
| 93 | } |