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" | 10 #include "SkRecordTraits.h" |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 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 | |
20 // This is an SkRecord visitor that will draw that SkRecord to an SkCanvas. | 14 // This is an SkRecord visitor that will draw that SkRecord to an SkCanvas. |
21 class Draw : SkNoncopyable { | 15 class Draw : SkNoncopyable { |
22 public: | 16 public: |
23 explicit Draw(SkCanvas* canvas) : fCanvas(canvas), fIndex(0), fClipEmpty(fal
se) {} | 17 explicit Draw(SkCanvas* canvas) : fCanvas(canvas), fIndex(0) {} |
24 | 18 |
25 unsigned index() const { return fIndex; } | 19 unsigned index() const { return fIndex; } |
26 void next() { ++fIndex; } | 20 void next() { ++fIndex; } |
27 | 21 |
28 template <typename T> void operator()(const T& r) { | 22 template <typename T> void operator()(const T& r) { |
29 if (!this->skip(r)) { | 23 if (!this->skip(r)) { |
30 this->draw(r); | 24 this->draw(r); |
31 this->updateClip<T>(); | |
32 } | 25 } |
33 } | 26 } |
34 | 27 |
35 private: | 28 private: |
36 // No base case, so we'll be compile-time checked that we implemented all po
ssibilities below. | 29 // No base case, so we'll be compile-time checked that we implemented all po
ssibilities below. |
37 template <typename T> void draw(const T&); | 30 template <typename T> void draw(const T&); |
38 | 31 |
39 // skip() returns true if we can skip this command, false if not. | 32 // skip() returns true if we can skip this command, false if not. |
40 // Update fIndex directly to skip more than just this one command. | 33 // Update fIndex directly to skip more than just this one command. |
41 | 34 |
42 // If we're drawing into an empty clip, we can skip it. Otherwise, run the
command. | 35 // If we're drawing into an empty clip, we can skip it. Otherwise, run the
command. |
43 template <typename T> | 36 template <typename T> |
44 SK_WHEN(SkRecords::IsDraw<T>, bool) skip(const T&) { return fClipEmpty; } | 37 SK_WHEN(SkRecords::IsDraw<T>, bool) skip(const T&) { return fCanvas->isClipE
mpty(); } |
45 | 38 |
46 template <typename T> | 39 template <typename T> |
47 SK_WHEN(!SkRecords::IsDraw<T>, bool) skip(const T&) { return false; } | 40 SK_WHEN(!SkRecords::IsDraw<T>, bool) skip(const T&) { return false; } |
48 | 41 |
49 // Special versions for commands added by optimizations. | 42 // Special versions for commands added by optimizations. |
50 bool skip(const SkRecords::PairedPushCull& r) { | 43 bool skip(const SkRecords::PairedPushCull& r) { |
51 if (fCanvas->quickReject(r.base->rect)) { | 44 if (fCanvas->quickReject(r.base->rect)) { |
52 fIndex += r.skip; | 45 fIndex += r.skip; |
53 return true; | 46 return true; |
54 } | 47 } |
55 return this->skip(*r.base); | 48 return this->skip(*r.base); |
56 } | 49 } |
57 | 50 |
58 bool skip(const SkRecords::BoundedDrawPosTextH& r) { | 51 bool skip(const SkRecords::BoundedDrawPosTextH& r) { |
59 return this->skip(*r.base) || fCanvas->quickRejectY(r.minY, r.maxY); | 52 return this->skip(*r.base) || fCanvas->quickRejectY(r.minY, r.maxY); |
60 } | 53 } |
61 | 54 |
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 | |
68 SkCanvas* fCanvas; | 55 SkCanvas* fCanvas; |
69 unsigned fIndex; | 56 unsigned fIndex; |
70 bool fClipEmpty; | |
71 }; | 57 }; |
72 | 58 |
73 // NoOps draw nothing. | 59 // NoOps draw nothing. |
74 template <> void Draw::draw(const SkRecords::NoOp&) {} | 60 template <> void Draw::draw(const SkRecords::NoOp&) {} |
75 | 61 |
76 #define DRAW(T, call) template <> void Draw::draw(const SkRecords::T& r) { fCanv
as->call; } | 62 #define DRAW(T, call) template <> void Draw::draw(const SkRecords::T& r) { fCanv
as->call; } |
77 DRAW(Restore, restore()); | 63 DRAW(Restore, restore()); |
78 DRAW(Save, save(r.flags)); | 64 DRAW(Save, save(r.flags)); |
79 DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags)); | 65 DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags)); |
80 DRAW(PopCull, popCull()); | 66 DRAW(PopCull, popCull()); |
(...skipping 30 matching lines...) Expand all Loading... |
111 template <> void Draw::draw(const SkRecords::PairedPushCull& r) { this->draw(*r.
base); } | 97 template <> void Draw::draw(const SkRecords::PairedPushCull& r) { this->draw(*r.
base); } |
112 template <> void Draw::draw(const SkRecords::BoundedDrawPosTextH& r) { this->dra
w(*r.base); } | 98 template <> void Draw::draw(const SkRecords::BoundedDrawPosTextH& r) { this->dra
w(*r.base); } |
113 | 99 |
114 } // namespace | 100 } // namespace |
115 | 101 |
116 void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) { | 102 void SkRecordDraw(const SkRecord& record, SkCanvas* canvas) { |
117 for (Draw draw(canvas); draw.index() < record.count(); draw.next()) { | 103 for (Draw draw(canvas); draw.index() < record.count(); draw.next()) { |
118 record.visit(draw.index(), draw); | 104 record.visit(draw.index(), draw); |
119 } | 105 } |
120 } | 106 } |
OLD | NEW |