| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkRecordDraw.h" | 8 #include "SkRecordDraw.h" |
| 9 | 9 |
| 10 #include "SkRecordTraits.h" |
| 11 |
| 10 namespace { | 12 namespace { |
| 11 | 13 |
| 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 = t
rue; }; |
| 17 template <> struct ChangesClip<SkRecords::SaveLayer> { static const bool value =
true; }; |
| 18 |
| 19 |
| 12 // This is an SkRecord visitor that will draw that SkRecord to an SkCanvas. | 20 // This is an SkRecord visitor that will draw that SkRecord to an SkCanvas. |
| 13 class Draw : SkNoncopyable { | 21 class Draw : SkNoncopyable { |
| 14 public: | 22 public: |
| 15 explicit Draw(SkCanvas* canvas) : fCanvas(canvas), fIndex(0), fClipEmpty(fal
se) {} | 23 explicit Draw(SkCanvas* canvas) : fCanvas(canvas), fIndex(0), fClipEmpty(fal
se) {} |
| 16 | 24 |
| 17 unsigned index() const { return fIndex; } | 25 unsigned index() const { return fIndex; } |
| 18 void next() { ++fIndex; } | 26 void next() { ++fIndex; } |
| 19 | 27 |
| 20 template <typename T> void operator()(const T& r) { | 28 template <typename T> void operator()(const T& r) { |
| 21 if (!this->skip(r)) { | 29 if (!this->skip(r)) { |
| 22 this->draw(r); | 30 this->draw(r); |
| 23 this->updateClip<T>(); | 31 this->updateClip<T>(); |
| 24 } | 32 } |
| 25 } | 33 } |
| 26 | 34 |
| 27 private: | 35 private: |
| 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&) { | |
| 31 // We can skip most commands if the clip is empty. Exceptions are speci
alized below. | |
| 32 return fClipEmpty; | |
| 33 } | |
| 34 | |
| 35 // No base case, so we'll be compile-time checked that we implemented all po
ssibilities below. | 36 // No base case, so we'll be compile-time checked that we implemented all po
ssibilities below. |
| 36 template <typename T> void draw(const T&); | 37 template <typename T> void draw(const T&); |
| 37 | 38 |
| 38 // Update fClipEmpty if necessary. | 39 // skip() returns true if we can skip this command, false if not. |
| 39 template <typename T> void updateClip() { | 40 // Update fIndex directly to skip more than just this one command. |
| 40 // Most commands don't change the clip. Exceptions are specialized belo
w. | 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); |
| 41 } | 56 } |
| 42 | 57 |
| 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->isClipEmp
ty(); } |
| 65 template <typename T> |
| 66 SK_WHEN(!ChangesClip<T>, void) updateClip() {} |
| 67 |
| 43 SkCanvas* fCanvas; | 68 SkCanvas* fCanvas; |
| 44 unsigned fIndex; | 69 unsigned fIndex; |
| 45 bool fClipEmpty; | 70 bool fClipEmpty; |
| 46 }; | 71 }; |
| 47 | 72 |
| 48 // TODO(mtklein): do this specialization with template traits instead of macros | 73 // NoOps draw nothing. |
| 49 | |
| 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. | |
| 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 | |
| 70 | |
| 71 // We can skip these commands if they're intersecting with a clip that's already
empty. | |
| 72 #define SKIP(T) template <> bool Draw::skip(const SkRecords::T& r) \ | |
| 73 { return fClipEmpty && SkRegion::kIntersect_Op == r.op; } | |
| 74 SKIP(ClipPath); | |
| 75 SKIP(ClipRRect); | |
| 76 SKIP(ClipRect); | |
| 77 SKIP(ClipRegion); | |
| 78 #undef SKIP | |
| 79 | |
| 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&) {} | 74 template <> void Draw::draw(const SkRecords::NoOp&) {} |
| 83 | 75 |
| 84 #define DRAW(T, call) template <> void Draw::draw(const SkRecords::T& r) { fCanv
as->call; } | 76 #define DRAW(T, call) template <> void Draw::draw(const SkRecords::T& r) { fCanv
as->call; } |
| 85 DRAW(Restore, restore()); | 77 DRAW(Restore, restore()); |
| 86 DRAW(Save, save(r.flags)); | 78 DRAW(Save, save(r.flags)); |
| 87 DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags)); | 79 DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags)); |
| 88 DRAW(PopCull, popCull()); | 80 DRAW(PopCull, popCull()); |
| 89 DRAW(PushCull, pushCull(r.rect)); | 81 DRAW(PushCull, pushCull(r.rect)); |
| 90 DRAW(Clear, clear(r.color)); | 82 DRAW(Clear, clear(r.color)); |
| 91 DRAW(Concat, concat(r.matrix)); | 83 DRAW(Concat, concat(r.matrix)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 109 DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint)); | 101 DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint)); |
| 110 DRAW(DrawRRect, drawRRect(r.rrect, r.paint)); | 102 DRAW(DrawRRect, drawRRect(r.rrect, r.paint)); |
| 111 DRAW(DrawRect, drawRect(r.rect, r.paint)); | 103 DRAW(DrawRect, drawRect(r.rect, r.paint)); |
| 112 DRAW(DrawSprite, drawSprite(r.bitmap, r.left, r.top, r.paint)); | 104 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)); | 105 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.pa
int)); | 106 DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.pa
int)); |
| 115 DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.co
lors, | 107 DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.co
lors, |
| 116 r.xmode.get(), r.indices, r.indexCount, r.paint)
); | 108 r.xmode.get(), r.indices, r.indexCount, r.paint)
); |
| 117 #undef DRAW | 109 #undef DRAW |
| 118 | 110 |
| 119 // Added by SkRecordAnnotateCullingPairs. | 111 template <> void Draw::draw(const SkRecords::PairedPushCull& r) { this->draw(*r.
base); } |
| 120 template <> bool Draw::skip(const SkRecords::PairedPushCull& r) { | 112 template <> void Draw::draw(const SkRecords::BoundedDrawPosTextH& r) { this->dra
w(*r.base); } |
| 121 if (fCanvas->quickReject(r.base->rect)) { | |
| 122 fIndex += r.skip; | |
| 123 return true; | |
| 124 } | |
| 125 return false; | |
| 126 } | |
| 127 | |
| 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 f
or 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 | 113 |
| 139 } // namespace | 114 } // namespace |
| 140 | 115 |
| 141 void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) { | 116 void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) { |
| 142 for (Draw draw(canvas); draw.index() < record.count(); draw.next()) { | 117 for (Draw draw(canvas); draw.index() < record.count(); draw.next()) { |
| 143 record.visit(draw.index(), draw); | 118 record.visit(draw.index(), draw); |
| 144 } | 119 } |
| 145 } | 120 } |
| OLD | NEW |