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 | |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 20 | template <typename T> void operator()(const T& r) { |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 21 | if (!this->skip(r)) { |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 22 | this->draw(r); |
| 23 | this->updateClip<T>(); |
| 24 | } |
| 25 | } |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 26 | |
| 27 | private: |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 28 | // Return true if we can skip this command, false if not. |
| 29 | // Update fIndex here directly to skip more than just this one command. |
| 30 | template <typename T> bool skip(const T&) { |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 31 | // We can skip most commands if the clip is empty. Exceptions are specialized below. |
| 32 | return fClipEmpty; |
| 33 | } |
| 34 | |
| 35 | // No base case, so we'll be compile-time checked that we implemented all possibilities below. |
| 36 | template <typename T> void draw(const T&); |
| 37 | |
| 38 | // Update fClipEmpty if necessary. |
| 39 | template <typename T> void updateClip() { |
| 40 | // Most commands don't change the clip. Exceptions are specialized below. |
| 41 | } |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 42 | |
| 43 | SkCanvas* fCanvas; |
| 44 | unsigned fIndex; |
| 45 | bool fClipEmpty; |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 48 | // TODO(mtklein): do this specialization with template traits instead of macros |
| 49 | |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 50 | // These commands may change the clip. |
| 51 | #define UPDATE_CLIP(T) template <> void Draw::updateClip<SkRecords::T>() \ |
| 52 | { fClipEmpty = fCanvas->isClipEmpty(); } |
| 53 | UPDATE_CLIP(Restore); |
| 54 | UPDATE_CLIP(SaveLayer); |
| 55 | UPDATE_CLIP(ClipPath); |
| 56 | UPDATE_CLIP(ClipRRect); |
| 57 | UPDATE_CLIP(ClipRect); |
| 58 | UPDATE_CLIP(ClipRegion); |
| 59 | #undef UPDATE_CLIP |
| 60 | |
| 61 | // These commands must always run. |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 62 | #define SKIP(T) template <> bool Draw::skip(const SkRecords::T&) { return false; } |
| 63 | SKIP(Restore); |
| 64 | SKIP(Save); |
| 65 | SKIP(SaveLayer); |
| 66 | SKIP(Clear); |
| 67 | SKIP(PushCull); |
| 68 | SKIP(PopCull); |
| 69 | #undef SKIP |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 70 | |
| 71 | // We can skip these commands if they're intersecting with a clip that's already empty. |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 72 | #define SKIP(T) template <> bool Draw::skip(const SkRecords::T& r) \ |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 73 | { return fClipEmpty && SkRegion::kIntersect_Op == r.op; } |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 74 | SKIP(ClipPath); |
| 75 | SKIP(ClipRRect); |
| 76 | SKIP(ClipRect); |
| 77 | SKIP(ClipRegion); |
| 78 | #undef SKIP |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 79 | |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 80 | // NoOps can always be skipped and draw nothing. |
| 81 | template <> bool Draw::skip(const SkRecords::NoOp&) { return true; } |
| 82 | template <> void Draw::draw(const SkRecords::NoOp&) {} |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 83 | |
| 84 | #define DRAW(T, call) template <> void Draw::draw(const SkRecords::T& r) { fCanvas->call; } |
| 85 | DRAW(Restore, restore()); |
| 86 | DRAW(Save, save(r.flags)); |
| 87 | DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags)); |
| 88 | DRAW(PopCull, popCull()); |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 89 | DRAW(PushCull, pushCull(r.rect)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 90 | DRAW(Clear, clear(r.color)); |
| 91 | DRAW(Concat, concat(r.matrix)); |
| 92 | DRAW(SetMatrix, setMatrix(r.matrix)); |
| 93 | |
| 94 | DRAW(ClipPath, clipPath(r.path, r.op, r.doAA)); |
| 95 | DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA)); |
| 96 | DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA)); |
| 97 | DRAW(ClipRegion, clipRegion(r.region, r.op)); |
| 98 | |
| 99 | DRAW(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint)); |
| 100 | DRAW(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint)); |
| 101 | DRAW(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint)); |
| 102 | DRAW(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint, r.flags)); |
| 103 | DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint)); |
| 104 | DRAW(DrawOval, drawOval(r.oval, r.paint)); |
| 105 | DRAW(DrawPaint, drawPaint(r.paint)); |
| 106 | DRAW(DrawPath, drawPath(r.path, r.paint)); |
| 107 | DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint)); |
| 108 | DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint)); |
| 109 | DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint)); |
| 110 | DRAW(DrawRRect, drawRRect(r.rrect, r.paint)); |
| 111 | DRAW(DrawRect, drawRect(r.rect, r.paint)); |
| 112 | DRAW(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint)); |
| 113 | DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint)); |
| 114 | DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint)); |
| 115 | DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors, |
| 116 | r.xmode.get(), r.indices, r.indexCount, r.paint)); |
| 117 | #undef DRAW |
| 118 | |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 119 | // Added by SkRecordAnnotateCullingPairs. |
| 120 | template <> bool Draw::skip(const SkRecords::PairedPushCull& r) { |
| 121 | if (fCanvas->quickReject(r.base->rect)) { |
| 122 | fIndex += r.skip; |
| 123 | return true; |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 124 | } |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 125 | return false; |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 126 | } |
| 127 | |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 128 | // Added by SkRecordBoundDrawPosTextH |
| 129 | template <> bool Draw::skip(const SkRecords::BoundedDrawPosTextH& r) { |
| 130 | return fClipEmpty || fCanvas->quickRejectY(r.minY, r.maxY); |
| 131 | } |
| 132 | |
| 133 | // These draw by proxying to the commands they wrap. (All the optimization is for skip().) |
| 134 | #define DRAW(T) template <> void Draw::draw(const SkRecords::T& r) { this->draw(*r.base); } |
| 135 | DRAW(PairedPushCull); |
| 136 | DRAW(BoundedDrawPosTextH); |
| 137 | #undef DRAW |
| 138 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 139 | } // namespace |
| 140 | |
| 141 | void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) { |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 142 | for (Draw draw(canvas); draw.index() < record.count(); draw.next()) { |
| 143 | record.visit(draw.index(), draw); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 144 | } |
| 145 | } |