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 | |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame^] | 10 | #include "SkRecordTraits.h" |
| 11 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 12 | namespace { |
| 13 | |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame^] | 14 | // All clip commands, Restore, and SaveLayer may change the clip. |
| 15 | template <typename T> struct ChangesClip { static const bool value = SkRecords::IsClip<T>::value; }; |
| 16 | template <> struct ChangesClip<SkRecords::Restore> { static const bool value = true; }; |
| 17 | template <> struct ChangesClip<SkRecords::SaveLayer> { static const bool value = true; }; |
| 18 | |
| 19 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 20 | // 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] | 21 | class Draw : SkNoncopyable { |
| 22 | public: |
| 23 | explicit Draw(SkCanvas* canvas) : fCanvas(canvas), fIndex(0), fClipEmpty(false) {} |
| 24 | |
| 25 | unsigned index() const { return fIndex; } |
| 26 | void next() { ++fIndex; } |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 27 | |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 28 | template <typename T> void operator()(const T& r) { |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 29 | if (!this->skip(r)) { |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 30 | this->draw(r); |
| 31 | this->updateClip<T>(); |
| 32 | } |
| 33 | } |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 34 | |
| 35 | private: |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 36 | // No base case, so we'll be compile-time checked that we implemented all possibilities below. |
| 37 | template <typename T> void draw(const T&); |
| 38 | |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame^] | 39 | // skip() returns true if we can skip this command, false if not. |
| 40 | // Update fIndex directly to skip more than just this one command. |
| 41 | |
| 42 | // If we're drawing into an empty clip, we can skip it. Otherwise, run the command. |
| 43 | template <typename T> |
| 44 | SK_WHEN(SkRecords::IsDraw<T>, bool) skip(const T&) { return fClipEmpty; } |
| 45 | |
| 46 | template <typename T> |
| 47 | SK_WHEN(!SkRecords::IsDraw<T>, bool) skip(const T&) { return false; } |
| 48 | |
| 49 | // Special versions for commands added by optimizations. |
| 50 | bool skip(const SkRecords::PairedPushCull& r) { |
| 51 | if (fCanvas->quickReject(r.base->rect)) { |
| 52 | fIndex += r.skip; |
| 53 | return true; |
| 54 | } |
| 55 | return this->skip(*r.base); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 56 | } |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 57 | |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame^] | 58 | bool skip(const SkRecords::BoundedDrawPosTextH& r) { |
| 59 | return this->skip(*r.base) || fCanvas->quickRejectY(r.minY, r.maxY); |
| 60 | } |
| 61 | |
| 62 | // If we might have changed the clip, update it, else do nothing. |
| 63 | template <typename T> |
| 64 | SK_WHEN(ChangesClip<T>, void) updateClip() { fClipEmpty = fCanvas->isClipEmpty(); } |
| 65 | template <typename T> |
| 66 | SK_WHEN(!ChangesClip<T>, void) updateClip() {} |
| 67 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 68 | SkCanvas* fCanvas; |
| 69 | unsigned fIndex; |
| 70 | bool fClipEmpty; |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame^] | 73 | // NoOps draw nothing. |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 74 | template <> void Draw::draw(const SkRecords::NoOp&) {} |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 75 | |
| 76 | #define DRAW(T, call) template <> void Draw::draw(const SkRecords::T& r) { fCanvas->call; } |
| 77 | DRAW(Restore, restore()); |
| 78 | DRAW(Save, save(r.flags)); |
| 79 | DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags)); |
| 80 | DRAW(PopCull, popCull()); |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 81 | DRAW(PushCull, pushCull(r.rect)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 82 | DRAW(Clear, clear(r.color)); |
| 83 | DRAW(Concat, concat(r.matrix)); |
| 84 | DRAW(SetMatrix, setMatrix(r.matrix)); |
| 85 | |
| 86 | DRAW(ClipPath, clipPath(r.path, r.op, r.doAA)); |
| 87 | DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA)); |
| 88 | DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA)); |
| 89 | DRAW(ClipRegion, clipRegion(r.region, r.op)); |
| 90 | |
| 91 | DRAW(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint)); |
| 92 | DRAW(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint)); |
| 93 | DRAW(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint)); |
| 94 | DRAW(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint, r.flags)); |
| 95 | DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint)); |
| 96 | DRAW(DrawOval, drawOval(r.oval, r.paint)); |
| 97 | DRAW(DrawPaint, drawPaint(r.paint)); |
| 98 | DRAW(DrawPath, drawPath(r.path, r.paint)); |
| 99 | DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint)); |
| 100 | DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint)); |
| 101 | DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint)); |
| 102 | DRAW(DrawRRect, drawRRect(r.rrect, r.paint)); |
| 103 | DRAW(DrawRect, drawRect(r.rect, r.paint)); |
| 104 | DRAW(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint)); |
| 105 | DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint)); |
| 106 | DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint)); |
| 107 | DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors, |
| 108 | r.xmode.get(), r.indices, r.indexCount, r.paint)); |
| 109 | #undef DRAW |
| 110 | |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame^] | 111 | template <> void Draw::draw(const SkRecords::PairedPushCull& r) { this->draw(*r.base); } |
| 112 | template <> void Draw::draw(const SkRecords::BoundedDrawPosTextH& r) { this->draw(*r.base); } |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 113 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 114 | } // namespace |
| 115 | |
| 116 | void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) { |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 117 | for (Draw draw(canvas); draw.index() < record.count(); draw.next()) { |
| 118 | record.visit(draw.index(), draw); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 119 | } |
| 120 | } |