OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 "GrPath.h" | 8 #include "GrPath.h" |
9 #include "GrShape.h" | 9 #include "GrShape.h" |
10 | 10 |
11 // Verb count limit for generating path key from content of a volatile path. | 11 static inline void write_style_key(uint32_t* key, const GrStyle& style) { |
12 // The value should accomodate at least simple rects and rrects. | 12 // Pass 1 for the scale since the GPU will apply the style not GrStyle::appl
yToPath(). |
13 static const int kSimpleVolatilePathVerbLimit = 10; | 13 GrStyle::WriteKey(key, style, GrStyle::Apply::kPathEffectAndStrokeRec, SK_Sc
alar1); |
| 14 } |
14 | 15 |
15 static inline int style_data_cnt(const GrStyle& style) { | 16 |
16 int cnt = GrStyle::KeySize(style, GrStyle::Apply::kPathEffectAndStrokeRec); | 17 void GrPath::ComputeKey(const GrShape& shape, GrUniqueKey* key, bool* outIsVolat
ile) { |
| 18 int geoCnt = shape.unstyledKeySize(); |
| 19 int styleCnt = GrStyle::KeySize(shape.style(), GrStyle::Apply::kPathEffectAn
dStrokeRec); |
17 // This should only fail for an arbitrary path effect, and we should not hav
e gotten | 20 // This should only fail for an arbitrary path effect, and we should not hav
e gotten |
18 // here with anything other than a dash path effect. | 21 // here with anything other than a dash path effect. |
19 SkASSERT(cnt >= 0); | 22 SkASSERT(styleCnt >= 0); |
20 return cnt; | 23 if (geoCnt < 0) { |
21 } | 24 *outIsVolatile = true; |
22 | 25 return; |
23 static inline void write_style_key(uint32_t* dst, const GrStyle& style) { | |
24 // Pass 1 for the scale since the GPU will apply the style not GrStyle::appl
yToPath(). | |
25 GrStyle::WriteKey(dst, style, GrStyle::Apply::kPathEffectAndStrokeRec, SK_Sc
alar1); | |
26 } | |
27 | |
28 // Encodes the full path data to the unique key for very small paths that wouldn
't otherwise have a | |
29 // key. This is typically hit when clipping stencils the clip stack. | |
30 inline static bool compute_key_for_simple_path(const GrShape& shape, GrUniqueKey
* key) { | |
31 if (shape.hasUnstyledKey()) { | |
32 return false; | |
33 } | |
34 SkPath path; | |
35 shape.asPath(&path); | |
36 // The check below should take care of negative values casted positive. | |
37 const int verbCnt = path.countVerbs(); | |
38 if (verbCnt > kSimpleVolatilePathVerbLimit) { | |
39 return false; | |
40 } | |
41 | |
42 // If somebody goes wild with the constant, it might cause an overflow. | |
43 static_assert(kSimpleVolatilePathVerbLimit <= 100, | |
44 "big_simple_volatile_path_verb_limit_may_cause_overflow"); | |
45 | |
46 const int pointCnt = path.countPoints(); | |
47 if (pointCnt < 0) { | |
48 SkASSERT(false); | |
49 return false; | |
50 } | |
51 SkSTArray<16, SkScalar, true> conicWeights(16); | |
52 if ((path.getSegmentMasks() & SkPath::kConic_SegmentMask) != 0) { | |
53 SkPath::RawIter iter(path); | |
54 SkPath::Verb verb; | |
55 SkPoint points[4]; | |
56 while ((verb = iter.next(points)) != SkPath::kDone_Verb) { | |
57 if (verb == SkPath::kConic_Verb) { | |
58 conicWeights.push_back(iter.conicWeight()); | |
59 } | |
60 } | |
61 } | |
62 | |
63 const int conicWeightCnt = conicWeights.count(); | |
64 | |
65 // Construct counts that align as uint32_t counts. | |
66 #define ARRAY_DATA32_COUNT(array_type, count) \ | |
67 static_cast<int>((((count) * sizeof(array_type) + sizeof(uint32_t) - 1) / si
zeof(uint32_t))) | |
68 | |
69 const int verbData32Cnt = ARRAY_DATA32_COUNT(uint8_t, verbCnt); | |
70 const int pointData32Cnt = ARRAY_DATA32_COUNT(SkPoint, pointCnt); | |
71 const int conicWeightData32Cnt = ARRAY_DATA32_COUNT(SkScalar, conicWeightCnt
); | |
72 | |
73 #undef ARRAY_DATA32_COUNT | |
74 | |
75 // The unique key data is a "message" with following fragments: | |
76 // 0) domain, key length, uint32_t for fill type and uint32_t for verbCnt | |
77 // (fragment 0, fixed size) | |
78 // 1) verb, point data and conic weights (varying size) | |
79 // 2) stroke data (varying size) | |
80 | |
81 const int baseData32Cnt = 2 + verbData32Cnt + pointData32Cnt + conicWeightDa
ta32Cnt; | |
82 const int styleDataCnt = style_data_cnt(shape.style()); | |
83 static const GrUniqueKey::Domain kSimpleVolatilePathDomain = GrUniqueKey::Ge
nerateDomain(); | |
84 GrUniqueKey::Builder builder(key, kSimpleVolatilePathDomain, baseData32Cnt +
styleDataCnt); | |
85 int i = 0; | |
86 builder[i++] = path.getFillType(); | |
87 | |
88 // Serialize the verbCnt to make the whole message unambiguous. | |
89 // We serialize two variable length fragments to the message: | |
90 // * verbs, point data and conic weights (fragment 1) | |
91 // * stroke data (fragment 2) | |
92 // "Proof:" | |
93 // Verb count establishes unambiguous verb data. | |
94 // Verbs encode also point data size and conic weight size. | |
95 // Thus the fragment 1 is unambiguous. | |
96 // Unambiguous fragment 1 establishes unambiguous fragment 2, since the leng
th of the message | |
97 // has been established. | |
98 | |
99 builder[i++] = SkToU32(verbCnt); // The path limit is compile-asserted above
, so the cast is ok. | |
100 | |
101 // Fill the last uint32_t with 0 first, since the last uint8_ts of the uint3
2_t may be | |
102 // uninitialized. This does not produce ambiguous verb data, since we have s
erialized the exact | |
103 // verb count. | |
104 if (verbData32Cnt != static_cast<int>((verbCnt * sizeof(uint8_t) / sizeof(ui
nt32_t)))) { | |
105 builder[i + verbData32Cnt - 1] = 0; | |
106 } | |
107 path.getVerbs(reinterpret_cast<uint8_t*>(&builder[i]), verbCnt); | |
108 i += verbData32Cnt; | |
109 | |
110 static_assert(((sizeof(SkPoint) % sizeof(uint32_t)) == 0) && sizeof(SkPoint)
> sizeof(uint32_t), | |
111 "skpoint_array_needs_padding"); | |
112 | |
113 // Here we assume getPoints does a memcpy, so that we do not need to worry a
bout the alignment. | |
114 path.getPoints(reinterpret_cast<SkPoint*>(&builder[i]), pointCnt); | |
115 i += pointData32Cnt; | |
116 | |
117 if (conicWeightCnt > 0) { | |
118 if (conicWeightData32Cnt != static_cast<int>( | |
119 (conicWeightCnt * sizeof(SkScalar) / sizeof(uint32_t)))) { | |
120 builder[i + conicWeightData32Cnt - 1] = 0; | |
121 } | |
122 memcpy(&builder[i], conicWeights.begin(), conicWeightCnt * sizeof(SkScal
ar)); | |
123 SkDEBUGCODE(i += conicWeightData32Cnt); | |
124 } | |
125 SkASSERT(i == baseData32Cnt); | |
126 if (styleDataCnt > 0) { | |
127 write_style_key(&builder[baseData32Cnt], shape.style()); | |
128 } | |
129 return true; | |
130 } | |
131 | |
132 inline static bool compute_key_for_general_shape(const GrShape& shape, GrUniqueK
ey* key) { | |
133 int geoCnt = shape.unstyledKeySize(); | |
134 int styleCnt = style_data_cnt(shape.style()); | |
135 if (styleCnt < 0 || geoCnt < 0) { | |
136 return false; | |
137 } | 26 } |
138 static const GrUniqueKey::Domain kGeneralPathDomain = GrUniqueKey::GenerateD
omain(); | 27 static const GrUniqueKey::Domain kGeneralPathDomain = GrUniqueKey::GenerateD
omain(); |
139 GrUniqueKey::Builder builder(key, kGeneralPathDomain, geoCnt + styleCnt); | 28 GrUniqueKey::Builder builder(key, kGeneralPathDomain, geoCnt + styleCnt); |
140 shape.writeUnstyledKey(&builder[0]); | 29 shape.writeUnstyledKey(&builder[0]); |
141 if (styleCnt) { | 30 if (styleCnt) { |
142 write_style_key(&builder[geoCnt], shape.style()); | 31 write_style_key(&builder[geoCnt], shape.style()); |
143 } | 32 } |
144 return true; | 33 *outIsVolatile = false; |
145 } | |
146 | |
147 void GrPath::ComputeKey(const GrShape& shape, GrUniqueKey* key, bool* outIsVolat
ile) { | |
148 | |
149 if (compute_key_for_simple_path(shape, key)) { | |
150 *outIsVolatile = false; | |
151 return; | |
152 } | |
153 *outIsVolatile = !compute_key_for_general_shape(shape, key); | |
154 } | 34 } |
155 | 35 |
156 #ifdef SK_DEBUG | 36 #ifdef SK_DEBUG |
157 bool GrPath::isEqualTo(const SkPath& path, const GrStyle& style) const { | 37 bool GrPath::isEqualTo(const SkPath& path, const GrStyle& style) const { |
158 // Since this is only called in debug we don't care about performance. | 38 // Since this is only called in debug we don't care about performance. |
159 int cnt0 = GrStyle::KeySize(fStyle, GrStyle::Apply::kPathEffectAndStrokeRec)
; | 39 int cnt0 = GrStyle::KeySize(fStyle, GrStyle::Apply::kPathEffectAndStrokeRec)
; |
160 int cnt1 = GrStyle::KeySize(style, GrStyle::Apply::kPathEffectAndStrokeRec); | 40 int cnt1 = GrStyle::KeySize(style, GrStyle::Apply::kPathEffectAndStrokeRec); |
161 if (cnt0 < 0 || cnt1 < 0 || cnt0 != cnt1) { | 41 if (cnt0 < 0 || cnt1 < 0 || cnt0 != cnt1) { |
162 return false; | 42 return false; |
163 } | 43 } |
164 if (cnt0) { | 44 if (cnt0) { |
165 SkAutoTArray<uint32_t> key0(cnt0); | 45 SkAutoTArray<uint32_t> key0(cnt0); |
166 SkAutoTArray<uint32_t> key1(cnt0); | 46 SkAutoTArray<uint32_t> key1(cnt0); |
167 write_style_key(key0.get(), fStyle); | 47 write_style_key(key0.get(), fStyle); |
168 write_style_key(key1.get(), style); | 48 write_style_key(key1.get(), style); |
169 if (0 != memcmp(key0.get(), key1.get(), cnt0)) { | 49 if (0 != memcmp(key0.get(), key1.get(), cnt0)) { |
170 return false; | 50 return false; |
171 } | 51 } |
172 } | 52 } |
173 // We treat same-rect ovals as identical - but only when not dashing. | |
174 SkRect ovalBounds; | |
175 if (!fStyle.isDashed() && fSkPath.isOval(&ovalBounds)) { | |
176 SkRect otherOvalBounds; | |
177 return path.isOval(&otherOvalBounds) && ovalBounds == otherOvalBounds; | |
178 } | |
179 | |
180 return fSkPath == path; | 53 return fSkPath == path; |
181 } | 54 } |
182 #endif | 55 #endif |
OLD | NEW |