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: |
commit-bot@chromium.org | 0974a61 | 2014-04-28 19:49:00 +0000 | [diff] [blame] | 15 | explicit Draw(SkCanvas* canvas) : fCanvas(canvas), fIndex(0) {} |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 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); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 23 | } |
| 24 | } |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 25 | |
| 26 | private: |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 27 | // No base case, so we'll be compile-time checked that we implemented all possibilities below. |
| 28 | template <typename T> void draw(const T&); |
| 29 | |
commit-bot@chromium.org | 705b1ff | 2014-04-29 15:34:03 +0000 | [diff] [blame] | 30 | // skip() should return true if we can skip this command, false if not. |
| 31 | // It may update fIndex directly to skip more than just this one command. |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame] | 32 | |
commit-bot@chromium.org | 705b1ff | 2014-04-29 15:34:03 +0000 | [diff] [blame] | 33 | // Mostly we just blindly call fCanvas and let it handle quick rejects itself. |
| 34 | template <typename T> bool skip(const T&) { return false; } |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame] | 35 | |
commit-bot@chromium.org | 705b1ff | 2014-04-29 15:34:03 +0000 | [diff] [blame] | 36 | // We add our own quick rejects for commands added by optimizations. |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame] | 37 | bool skip(const SkRecords::PairedPushCull& r) { |
| 38 | if (fCanvas->quickReject(r.base->rect)) { |
| 39 | fIndex += r.skip; |
| 40 | return true; |
| 41 | } |
commit-bot@chromium.org | 705b1ff | 2014-04-29 15:34:03 +0000 | [diff] [blame] | 42 | return false; |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 43 | } |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame] | 44 | bool skip(const SkRecords::BoundedDrawPosTextH& r) { |
commit-bot@chromium.org | 705b1ff | 2014-04-29 15:34:03 +0000 | [diff] [blame] | 45 | return fCanvas->quickRejectY(r.minY, r.maxY); |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame] | 46 | } |
| 47 | |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 48 | SkCanvas* fCanvas; |
| 49 | unsigned fIndex; |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame] | 52 | // NoOps draw nothing. |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 53 | template <> void Draw::draw(const SkRecords::NoOp&) {} |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 54 | |
| 55 | #define DRAW(T, call) template <> void Draw::draw(const SkRecords::T& r) { fCanvas->call; } |
| 56 | DRAW(Restore, restore()); |
| 57 | DRAW(Save, save(r.flags)); |
| 58 | DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags)); |
| 59 | DRAW(PopCull, popCull()); |
commit-bot@chromium.org | 88c3e27 | 2014-04-22 16:57:20 +0000 | [diff] [blame] | 60 | DRAW(PushCull, pushCull(r.rect)); |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame] | 61 | DRAW(Clear, clear(r.color)); |
| 62 | DRAW(Concat, concat(r.matrix)); |
| 63 | DRAW(SetMatrix, setMatrix(r.matrix)); |
| 64 | |
| 65 | DRAW(ClipPath, clipPath(r.path, r.op, r.doAA)); |
| 66 | DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA)); |
| 67 | DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA)); |
| 68 | DRAW(ClipRegion, clipRegion(r.region, r.op)); |
| 69 | |
| 70 | DRAW(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint)); |
| 71 | DRAW(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint)); |
| 72 | DRAW(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint)); |
| 73 | DRAW(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint, r.flags)); |
| 74 | DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint)); |
| 75 | DRAW(DrawOval, drawOval(r.oval, r.paint)); |
| 76 | DRAW(DrawPaint, drawPaint(r.paint)); |
| 77 | DRAW(DrawPath, drawPath(r.path, r.paint)); |
| 78 | DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint)); |
| 79 | DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint)); |
| 80 | DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint)); |
| 81 | DRAW(DrawRRect, drawRRect(r.rrect, r.paint)); |
| 82 | DRAW(DrawRect, drawRect(r.rect, r.paint)); |
| 83 | DRAW(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint)); |
| 84 | DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint)); |
| 85 | DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint)); |
| 86 | DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors, |
| 87 | r.xmode.get(), r.indices, r.indexCount, r.paint)); |
| 88 | #undef DRAW |
| 89 | |
commit-bot@chromium.org | 2e0c32a | 2014-04-28 16:19:45 +0000 | [diff] [blame] | 90 | template <> void Draw::draw(const SkRecords::PairedPushCull& r) { this->draw(*r.base); } |
| 91 | 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] | 92 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 93 | } // namespace |
| 94 | |
| 95 | void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) { |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 96 | for (Draw draw(canvas); draw.index() < record.count(); draw.next()) { |
commit-bot@chromium.org | c71da1f | 2014-05-07 21:16:09 +0000 | [diff] [blame] | 97 | record.visit<void>(draw.index(), draw); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 98 | } |
| 99 | } |