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)); |
| 40 | CASE(ClipPath, clipPath(r.path, r.op, r.doAA)); |
| 41 | CASE(ClipRRect, clipRRect(r.rrect, r.op, r.doAA)); |
| 42 | CASE(ClipRect, clipRect(r.rect, r.op, r.doAA)); |
| 43 | CASE(ClipRegion, clipRegion(r.region, r.op)); |
| 44 | #undef CASE |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 45 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 46 | // Commands which must run regardless of the clip. |
| 47 | #define CASE(T, call) \ |
| 48 | template <> void Draw::operator()(const SkRecords::T& r) { fCanvas->call; } |
| 49 | CASE(Save, save(r.flags)); |
| 50 | CASE(Clear, clear(r.color)); |
| 51 | CASE(PopCull, popCull()); |
| 52 | #undef CASE |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 53 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 54 | // Nothing fancy below here. These commands respect and don't change the clip. |
| 55 | #define CASE(T, call) \ |
| 56 | template <> void Draw::operator()(const SkRecords::T& r) { if (!fClipEmpty) fCanvas->call; } |
| 57 | CASE(Concat, concat(r.matrix)); |
| 58 | CASE(SetMatrix, setMatrix(r.matrix)); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 59 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 60 | CASE(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint)); |
| 61 | CASE(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint)); |
| 62 | CASE(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint)); |
| 63 | CASE(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint, r.flags)); |
| 64 | CASE(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint)); |
| 65 | CASE(DrawOval, drawOval(r.oval, r.paint)); |
| 66 | CASE(DrawPaint, drawPaint(r.paint)); |
| 67 | CASE(DrawPath, drawPath(r.path, r.paint)); |
| 68 | CASE(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint)); |
| 69 | CASE(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint)); |
| 70 | CASE(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint)); |
| 71 | CASE(DrawRRect, drawRRect(r.rrect, r.paint)); |
| 72 | CASE(DrawRect, drawRect(r.rect, r.paint)); |
| 73 | CASE(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint)); |
| 74 | CASE(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint)); |
| 75 | CASE(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint)); |
| 76 | CASE(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors, |
| 77 | r.xmode.get(), r.indices, r.indexCount, r.paint)); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 78 | #undef CASE |
| 79 | |
| 80 | } // namespace |
| 81 | |
| 82 | void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) { |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 83 | for (Draw draw(canvas); draw.index() < record.count(); draw.next()) { |
| 84 | record.visit(draw.index(), draw); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 85 | } |
| 86 | } |