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) { |
| 21 | if (!this->canSkip(r)) { |
| 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 | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame^] | 28 | // Can we skip this command right now? |
| 29 | template <typename T> bool canSkip(const T&) const { |
| 30 | // We can skip most commands if the clip is empty. Exceptions are specialized below. |
| 31 | return fClipEmpty; |
| 32 | } |
| 33 | |
| 34 | // No base case, so we'll be compile-time checked that we implemented all possibilities below. |
| 35 | template <typename T> void draw(const T&); |
| 36 | |
| 37 | // Update fClipEmpty if necessary. |
| 38 | template <typename T> void updateClip() { |
| 39 | // Most commands don't change the clip. Exceptions are specialized below. |
| 40 | } |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 41 | |
| 42 | SkCanvas* fCanvas; |
| 43 | unsigned fIndex; |
| 44 | bool fClipEmpty; |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
commit-bot@chromium.org | 73b55eb | 2014-04-14 20:35:12 +0000 | [diff] [blame^] | 47 | // These commands may change the clip. |
| 48 | #define UPDATE_CLIP(T) template <> void Draw::updateClip<SkRecords::T>() \ |
| 49 | { fClipEmpty = fCanvas->isClipEmpty(); } |
| 50 | UPDATE_CLIP(Restore); |
| 51 | UPDATE_CLIP(SaveLayer); |
| 52 | UPDATE_CLIP(ClipPath); |
| 53 | UPDATE_CLIP(ClipRRect); |
| 54 | UPDATE_CLIP(ClipRect); |
| 55 | UPDATE_CLIP(ClipRegion); |
| 56 | #undef UPDATE_CLIP |
| 57 | |
| 58 | // These commands must always run. |
| 59 | #define CAN_SKIP(T) template <> bool Draw::canSkip(const SkRecords::T&) const { return false; } |
| 60 | CAN_SKIP(Restore); |
| 61 | CAN_SKIP(Save); |
| 62 | CAN_SKIP(SaveLayer); |
| 63 | CAN_SKIP(Clear); |
| 64 | CAN_SKIP(PushCull); |
| 65 | CAN_SKIP(PopCull); |
| 66 | #undef CAN_SKIP |
| 67 | |
| 68 | // We can skip these commands if they're intersecting with a clip that's already empty. |
| 69 | #define CAN_SKIP(T) template <> bool Draw::canSkip(const SkRecords::T& r) const \ |
| 70 | { return fClipEmpty && SkRegion::kIntersect_Op == r.op; } |
| 71 | CAN_SKIP(ClipPath); |
| 72 | CAN_SKIP(ClipRRect); |
| 73 | CAN_SKIP(ClipRect); |
| 74 | CAN_SKIP(ClipRegion); |
| 75 | #undef CAN_SKIP |
| 76 | |
| 77 | static bool can_skip_text(const SkCanvas& c, const SkPaint& p, SkScalar minY, SkScalar maxY) { |
| 78 | // If we're drawing vertical text, none of the checks we're about to do make any sense. |
| 79 | // We use canComputeFastBounds as a proxy for "is this text going to be rectangular?". |
| 80 | if (p.isVerticalText() || !p.canComputeFastBounds()) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | // Rather than checking the top and bottom font metrics, we guess. Actually looking up the top |
| 85 | // and bottom metrics is slow, and this overapproximation should be good enough. |
| 86 | const SkScalar buffer = p.getTextSize() * 1.5f; |
| 87 | SkDEBUGCODE(SkPaint::FontMetrics metrics;) |
| 88 | SkDEBUGCODE(p.getFontMetrics(&metrics);) |
| 89 | SkASSERT(-buffer <= metrics.fTop); |
| 90 | SkASSERT(+buffer >= metrics.fBottom); |
| 91 | return c.quickRejectY(minY - buffer, maxY + buffer); |
| 92 | } |
| 93 | |
| 94 | template <> bool Draw::canSkip(const SkRecords::DrawPosTextH& r) const { |
| 95 | return fClipEmpty || can_skip_text(*fCanvas, r.paint, r.y, r.y); |
| 96 | } |
| 97 | |
| 98 | template <> bool Draw::canSkip(const SkRecords::DrawPosText& r) const { |
| 99 | if (fClipEmpty) { |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | // TODO(mtklein): may want to move this minY/maxY calculation into a one-time pass |
| 104 | const unsigned points = r.paint.countText(r.text, r.byteLength); |
| 105 | if (points == 0) { |
| 106 | return true; |
| 107 | } |
| 108 | SkScalar minY = SK_ScalarInfinity, maxY = SK_ScalarNegativeInfinity; |
| 109 | for (unsigned i = 0; i < points; i++) { |
| 110 | minY = SkTMin(minY, r.pos[i].fY); |
| 111 | maxY = SkTMax(maxY, r.pos[i].fY); |
| 112 | } |
| 113 | |
| 114 | return can_skip_text(*fCanvas, r.paint, minY, maxY); |
| 115 | } |
| 116 | |
| 117 | #define DRAW(T, call) template <> void Draw::draw(const SkRecords::T& r) { fCanvas->call; } |
| 118 | DRAW(Restore, restore()); |
| 119 | DRAW(Save, save(r.flags)); |
| 120 | DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags)); |
| 121 | DRAW(PopCull, popCull()); |
| 122 | DRAW(Clear, clear(r.color)); |
| 123 | DRAW(Concat, concat(r.matrix)); |
| 124 | DRAW(SetMatrix, setMatrix(r.matrix)); |
| 125 | |
| 126 | DRAW(ClipPath, clipPath(r.path, r.op, r.doAA)); |
| 127 | DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA)); |
| 128 | DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA)); |
| 129 | DRAW(ClipRegion, clipRegion(r.region, r.op)); |
| 130 | |
| 131 | DRAW(DrawBitmap, drawBitmap(r.bitmap, r.left, r.top, r.paint)); |
| 132 | DRAW(DrawBitmapMatrix, drawBitmapMatrix(r.bitmap, r.matrix, r.paint)); |
| 133 | DRAW(DrawBitmapNine, drawBitmapNine(r.bitmap, r.center, r.dst, r.paint)); |
| 134 | DRAW(DrawBitmapRectToRect, drawBitmapRectToRect(r.bitmap, r.src, r.dst, r.paint, r.flags)); |
| 135 | DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint)); |
| 136 | DRAW(DrawOval, drawOval(r.oval, r.paint)); |
| 137 | DRAW(DrawPaint, drawPaint(r.paint)); |
| 138 | DRAW(DrawPath, drawPath(r.path, r.paint)); |
| 139 | DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint)); |
| 140 | DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint)); |
| 141 | DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint)); |
| 142 | DRAW(DrawRRect, drawRRect(r.rrect, r.paint)); |
| 143 | DRAW(DrawRect, drawRect(r.rect, r.paint)); |
| 144 | DRAW(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint)); |
| 145 | DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint)); |
| 146 | DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint)); |
| 147 | DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors, |
| 148 | r.xmode.get(), r.indices, r.indexCount, r.paint)); |
| 149 | #undef DRAW |
| 150 | |
| 151 | // PushCull is a bit of a oddball. We might be able to just skip until just past its popCull. |
| 152 | template <> void Draw::draw(const SkRecords::PushCull& r) { |
| 153 | if (r.popOffset != SkRecords::kUnsetPopOffset && fCanvas->quickReject(r.rect)) { |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 154 | fIndex += r.popOffset; |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 155 | } else { |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 156 | fCanvas->pushCull(r.rect); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 160 | } // namespace |
| 161 | |
| 162 | void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) { |
commit-bot@chromium.org | d9ce2be | 2014-04-09 23:30:28 +0000 | [diff] [blame] | 163 | for (Draw draw(canvas); draw.index() < record.count(); draw.next()) { |
| 164 | record.visit(draw.index(), draw); |
commit-bot@chromium.org | 506db0b | 2014-04-08 23:31:35 +0000 | [diff] [blame] | 165 | } |
| 166 | } |