commit-bot@chromium.org | c4b21e6 | 2014-04-11 18:33:31 +0000 | [diff] [blame^] | 1 | /* |
| 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.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 8 | #include "SkRecordDraw.h" |
| 9 | |
| 10 | namespace { |
| 11 | |
| 12 | // 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] | 13 | class Draw : SkNoncopyable { |
| 14 | public: |
| 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.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 19 | |
| 20 | // No base case, so we'll be compile-time checked that we implemented all possibilities below. |
| 21 | template <typename T> void operator()(const T&); |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 22 | |
| 23 | private: |
| 24 | // Must be called after any potential clip change. |
| 25 | void updateClip() { fClipEmpty = fCanvas->isClipEmpty(); } |
| 26 | |
| 27 | SkCanvas* fCanvas; |
| 28 | unsigned fIndex; |
| 29 | bool fClipEmpty; |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 32 | template <> void Draw::operator()(const SkRecords::PushCull& r) { |
| 33 | if (r.popOffset != SkRecords::kUnsetPopOffset && |
| 34 | fCanvas->quickReject(r.rect)) { |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 35 | // 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] | 36 | fIndex += r.popOffset; |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 37 | } else { |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 38 | fCanvas->pushCull(r.rect); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 42 | // These commands might change the clip. |
| 43 | #define CASE(T, call) \ |
| 44 | template <> void Draw::operator()(const SkRecords::T& r) { fCanvas->call; this->updateClip(); } |
| 45 | CASE(Restore, restore()); |
| 46 | CASE(SaveLayer, saveLayer(r.bounds, r.paint, r.flags)); |
commit-bot@chromium.org | ff2de7c | 2014-04-10 02:26:33 +0000 | [diff] [blame] | 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 | } |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 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 |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 59 | |
commit-bot@chromium.org | ff2de7c | 2014-04-10 02:26:33 +0000 | [diff] [blame] | 60 | // 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] | 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 |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 67 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 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)); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 73 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 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.paint)); |
| 90 | CASE(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors, |
| 91 | r.xmode.get(), r.indices, r.indexCount, r.paint)); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 92 | #undef CASE |
| 93 | |
| 94 | } // namespace |
| 95 | |
| 96 | void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) { |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 97 | for (Draw draw(canvas); draw.index() < record.count(); draw.next()) { |
| 98 | record.visit(draw.index(), draw); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 99 | } |
| 100 | } |