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 "GrStyle.h" | 9 #include "GrShape.h" |
10 | 10 |
11 namespace { | |
12 // Verb count limit for generating path key from content of a volatile path. | 11 // Verb count limit for generating path key from content of a volatile path. |
13 // The value should accomodate at least simple rects and rrects. | 12 // The value should accomodate at least simple rects and rrects. |
14 static const int kSimpleVolatilePathVerbLimit = 10; | 13 static const int kSimpleVolatilePathVerbLimit = 10; |
15 | 14 |
16 static inline int style_data_cnt(const GrStyle& style) { | 15 static inline int style_data_cnt(const GrStyle& style) { |
17 int cnt = GrStyle::KeySize(style, GrStyle::Apply::kPathEffectAndStrokeRec); | 16 int cnt = GrStyle::KeySize(style, GrStyle::Apply::kPathEffectAndStrokeRec); |
18 // This should only fail for an arbitrary path effect, and we should not hav
e gotten | 17 // This should only fail for an arbitrary path effect, and we should not hav
e gotten |
19 // here with anything other than a dash path effect. | 18 // here with anything other than a dash path effect. |
20 SkASSERT(cnt >= 0); | 19 SkASSERT(cnt >= 0); |
21 return cnt; | 20 return cnt; |
22 } | 21 } |
23 | 22 |
24 static inline void write_style_key(uint32_t* dst, const GrStyle& style) { | 23 static inline void write_style_key(uint32_t* dst, const GrStyle& style) { |
25 // Pass 1 for the scale since the GPU will apply the style not GrStyle::appl
yToPath(). | 24 // Pass 1 for the scale since the GPU will apply the style not GrStyle::appl
yToPath(). |
26 GrStyle::WriteKey(dst, style, GrStyle::Apply::kPathEffectAndStrokeRec, SK_Sc
alar1); | 25 GrStyle::WriteKey(dst, style, GrStyle::Apply::kPathEffectAndStrokeRec, SK_Sc
alar1); |
27 } | 26 } |
28 | 27 |
29 | 28 // Encodes the full path data to the unique key for very small paths that wouldn
't otherwise have a |
30 inline static bool compute_key_for_line_path(const SkPath& path, const GrStyle&
style, | 29 // key. This is typically hit when clipping stencils the clip stack. |
31 GrUniqueKey* key) { | 30 inline static bool compute_key_for_simple_path(const GrShape& shape, GrUniqueKey
* key) { |
32 SkPoint pts[2]; | 31 if (shape.hasUnstyledKey()) { |
33 if (!path.isLine(pts)) { | |
34 return false; | 32 return false; |
35 } | 33 } |
36 static_assert((sizeof(pts) % sizeof(uint32_t)) == 0 && sizeof(pts) > sizeof(
uint32_t), | 34 SkPath path; |
37 "pts_needs_padding"); | 35 shape.asPath(&path); |
38 int styleDataCnt = style_data_cnt(style); | |
39 | |
40 const int kBaseData32Cnt = 1 + sizeof(pts) / sizeof(uint32_t); | |
41 static const GrUniqueKey::Domain kOvalPathDomain = GrUniqueKey::GenerateDoma
in(); | |
42 GrUniqueKey::Builder builder(key, kOvalPathDomain, kBaseData32Cnt + styleDat
aCnt); | |
43 builder[0] = path.getFillType(); | |
44 memcpy(&builder[1], &pts, sizeof(pts)); | |
45 if (styleDataCnt > 0) { | |
46 write_style_key(&builder[kBaseData32Cnt], style); | |
47 } | |
48 return true; | |
49 } | |
50 | |
51 inline static bool compute_key_for_oval_path(const SkPath& path, const GrStyle&
style, | |
52 GrUniqueKey* key) { | |
53 SkRect rect; | |
54 // Point order is significant when dashing, so we cannot devolve to a rect k
ey. | |
55 if (style.pathEffect() || !path.isOval(&rect)) { | |
56 return false; | |
57 } | |
58 static_assert((sizeof(rect) % sizeof(uint32_t)) == 0 && sizeof(rect) > sizeo
f(uint32_t), | |
59 "rect_needs_padding"); | |
60 | |
61 const int kBaseData32Cnt = 1 + sizeof(rect) / sizeof(uint32_t); | |
62 int styleDataCnt = style_data_cnt(style); | |
63 static const GrUniqueKey::Domain kOvalPathDomain = GrUniqueKey::GenerateDoma
in(); | |
64 GrUniqueKey::Builder builder(key, kOvalPathDomain, kBaseData32Cnt + styleDat
aCnt); | |
65 builder[0] = path.getFillType(); | |
66 memcpy(&builder[1], &rect, sizeof(rect)); | |
67 if (styleDataCnt > 0) { | |
68 write_style_key(&builder[kBaseData32Cnt], style); | |
69 } | |
70 return true; | |
71 } | |
72 | |
73 // Encodes the full path data to the unique key for very small, volatile paths.
This is typically | |
74 // hit when clipping stencils the clip stack. Intention is that this handles rec
ts too, since | |
75 // SkPath::isRect seems to do non-trivial amount of work. | |
76 inline static bool compute_key_for_simple_path(const SkPath& path, const GrStyle
& style, | |
77 GrUniqueKey* key) { | |
78 if (!path.isVolatile()) { | |
79 return false; | |
80 } | |
81 // The check below should take care of negative values casted positive. | 36 // The check below should take care of negative values casted positive. |
82 const int verbCnt = path.countVerbs(); | 37 const int verbCnt = path.countVerbs(); |
83 if (verbCnt > kSimpleVolatilePathVerbLimit) { | 38 if (verbCnt > kSimpleVolatilePathVerbLimit) { |
84 return false; | 39 return false; |
85 } | 40 } |
86 | 41 |
87 // If somebody goes wild with the constant, it might cause an overflow. | 42 // If somebody goes wild with the constant, it might cause an overflow. |
88 static_assert(kSimpleVolatilePathVerbLimit <= 100, | 43 static_assert(kSimpleVolatilePathVerbLimit <= 100, |
89 "big_simple_volatile_path_verb_limit_may_cause_overflow"); | 44 "big_simple_volatile_path_verb_limit_may_cause_overflow"); |
90 | 45 |
(...skipping 26 matching lines...) Expand all Loading... |
117 | 72 |
118 #undef ARRAY_DATA32_COUNT | 73 #undef ARRAY_DATA32_COUNT |
119 | 74 |
120 // The unique key data is a "message" with following fragments: | 75 // The unique key data is a "message" with following fragments: |
121 // 0) domain, key length, uint32_t for fill type and uint32_t for verbCnt | 76 // 0) domain, key length, uint32_t for fill type and uint32_t for verbCnt |
122 // (fragment 0, fixed size) | 77 // (fragment 0, fixed size) |
123 // 1) verb, point data and conic weights (varying size) | 78 // 1) verb, point data and conic weights (varying size) |
124 // 2) stroke data (varying size) | 79 // 2) stroke data (varying size) |
125 | 80 |
126 const int baseData32Cnt = 2 + verbData32Cnt + pointData32Cnt + conicWeightDa
ta32Cnt; | 81 const int baseData32Cnt = 2 + verbData32Cnt + pointData32Cnt + conicWeightDa
ta32Cnt; |
127 const int styleDataCnt = style_data_cnt(style); | 82 const int styleDataCnt = style_data_cnt(shape.style()); |
128 static const GrUniqueKey::Domain kSimpleVolatilePathDomain = GrUniqueKey::Ge
nerateDomain(); | 83 static const GrUniqueKey::Domain kSimpleVolatilePathDomain = GrUniqueKey::Ge
nerateDomain(); |
129 GrUniqueKey::Builder builder(key, kSimpleVolatilePathDomain, baseData32Cnt +
styleDataCnt); | 84 GrUniqueKey::Builder builder(key, kSimpleVolatilePathDomain, baseData32Cnt +
styleDataCnt); |
130 int i = 0; | 85 int i = 0; |
131 builder[i++] = path.getFillType(); | 86 builder[i++] = path.getFillType(); |
132 | 87 |
133 // Serialize the verbCnt to make the whole message unambiguous. | 88 // Serialize the verbCnt to make the whole message unambiguous. |
134 // We serialize two variable length fragments to the message: | 89 // We serialize two variable length fragments to the message: |
135 // * verbs, point data and conic weights (fragment 1) | 90 // * verbs, point data and conic weights (fragment 1) |
136 // * stroke data (fragment 2) | 91 // * stroke data (fragment 2) |
137 // "Proof:" | 92 // "Proof:" |
(...skipping 24 matching lines...) Expand all Loading... |
162 if (conicWeightCnt > 0) { | 117 if (conicWeightCnt > 0) { |
163 if (conicWeightData32Cnt != static_cast<int>( | 118 if (conicWeightData32Cnt != static_cast<int>( |
164 (conicWeightCnt * sizeof(SkScalar) / sizeof(uint32_t)))) { | 119 (conicWeightCnt * sizeof(SkScalar) / sizeof(uint32_t)))) { |
165 builder[i + conicWeightData32Cnt - 1] = 0; | 120 builder[i + conicWeightData32Cnt - 1] = 0; |
166 } | 121 } |
167 memcpy(&builder[i], conicWeights.begin(), conicWeightCnt * sizeof(SkScal
ar)); | 122 memcpy(&builder[i], conicWeights.begin(), conicWeightCnt * sizeof(SkScal
ar)); |
168 SkDEBUGCODE(i += conicWeightData32Cnt); | 123 SkDEBUGCODE(i += conicWeightData32Cnt); |
169 } | 124 } |
170 SkASSERT(i == baseData32Cnt); | 125 SkASSERT(i == baseData32Cnt); |
171 if (styleDataCnt > 0) { | 126 if (styleDataCnt > 0) { |
172 write_style_key(&builder[baseData32Cnt], style); | 127 write_style_key(&builder[baseData32Cnt], shape.style()); |
173 } | 128 } |
174 return true; | 129 return true; |
175 } | 130 } |
176 | 131 |
177 inline static void compute_key_for_general_path(const SkPath& path, const GrStyl
e& style, | 132 inline static bool compute_key_for_general_shape(const GrShape& shape, GrUniqueK
ey* key) { |
178 GrUniqueKey* key) { | 133 int geoCnt = shape.unstyledKeySize(); |
179 const int kBaseData32Cnt = 2; | 134 int styleCnt = style_data_cnt(shape.style()); |
180 int styleDataCnt = style_data_cnt(style); | 135 if (styleCnt < 0 || geoCnt < 0) { |
| 136 return false; |
| 137 } |
181 static const GrUniqueKey::Domain kGeneralPathDomain = GrUniqueKey::GenerateD
omain(); | 138 static const GrUniqueKey::Domain kGeneralPathDomain = GrUniqueKey::GenerateD
omain(); |
182 GrUniqueKey::Builder builder(key, kGeneralPathDomain, kBaseData32Cnt + style
DataCnt); | 139 GrUniqueKey::Builder builder(key, kGeneralPathDomain, geoCnt + styleCnt); |
183 builder[0] = path.getGenerationID(); | 140 shape.writeUnstyledKey(&builder[0]); |
184 builder[1] = path.getFillType(); | 141 if (styleCnt) { |
185 if (styleDataCnt > 0) { | 142 write_style_key(&builder[geoCnt], shape.style()); |
186 write_style_key(&builder[kBaseData32Cnt], style); | |
187 } | 143 } |
| 144 return true; |
188 } | 145 } |
189 | 146 |
190 } | 147 void GrPath::ComputeKey(const GrShape& shape, GrUniqueKey* key, bool* outIsVolat
ile) { |
191 | 148 |
192 void GrPath::ComputeKey(const SkPath& path, const GrStyle& style, GrUniqueKey* k
ey, | 149 if (compute_key_for_simple_path(shape, key)) { |
193 bool* outIsVolatile) { | |
194 if (compute_key_for_line_path(path, style, key)) { | |
195 *outIsVolatile = false; | 150 *outIsVolatile = false; |
196 return; | 151 return; |
197 } | 152 } |
198 | 153 *outIsVolatile = !compute_key_for_general_shape(shape, key); |
199 if (compute_key_for_oval_path(path, style, key)) { | |
200 *outIsVolatile = false; | |
201 return; | |
202 } | |
203 | |
204 if (compute_key_for_simple_path(path, style, key)) { | |
205 *outIsVolatile = false; | |
206 return; | |
207 } | |
208 | |
209 compute_key_for_general_path(path, style, key); | |
210 *outIsVolatile = path.isVolatile(); | |
211 } | 154 } |
212 | 155 |
213 #ifdef SK_DEBUG | 156 #ifdef SK_DEBUG |
214 bool GrPath::isEqualTo(const SkPath& path, const GrStyle& style) const { | 157 bool GrPath::isEqualTo(const SkPath& path, const GrStyle& style) const { |
215 // Since this is only called in debug we don't care about performance. | 158 // Since this is only called in debug we don't care about performance. |
216 int cnt0 = GrStyle::KeySize(fStyle, GrStyle::Apply::kPathEffectAndStrokeRec)
; | 159 int cnt0 = GrStyle::KeySize(fStyle, GrStyle::Apply::kPathEffectAndStrokeRec)
; |
217 int cnt1 = GrStyle::KeySize(style, GrStyle::Apply::kPathEffectAndStrokeRec); | 160 int cnt1 = GrStyle::KeySize(style, GrStyle::Apply::kPathEffectAndStrokeRec); |
218 if (cnt0 < 0 || cnt1 < 0 || cnt0 != cnt1) { | 161 if (cnt0 < 0 || cnt1 < 0 || cnt0 != cnt1) { |
219 return false; | 162 return false; |
220 } | 163 } |
221 if (cnt0) { | 164 if (cnt0) { |
222 SkAutoTArray<uint32_t> key0(cnt0); | 165 SkAutoTArray<uint32_t> key0(cnt0); |
223 SkAutoTArray<uint32_t> key1(cnt0); | 166 SkAutoTArray<uint32_t> key1(cnt0); |
224 write_style_key(key0.get(), fStyle); | 167 write_style_key(key0.get(), fStyle); |
225 write_style_key(key1.get(), style); | 168 write_style_key(key1.get(), style); |
226 if (0 != memcmp(key0.get(), key1.get(), cnt0)) { | 169 if (0 != memcmp(key0.get(), key1.get(), cnt0)) { |
227 return false; | 170 return false; |
228 } | 171 } |
229 } | 172 } |
230 // We treat same-rect ovals as identical - but only when not dashing. | 173 // We treat same-rect ovals as identical - but only when not dashing. |
231 SkRect ovalBounds; | 174 SkRect ovalBounds; |
232 if (!fStyle.isDashed() && fSkPath.isOval(&ovalBounds)) { | 175 if (!fStyle.isDashed() && fSkPath.isOval(&ovalBounds)) { |
233 SkRect otherOvalBounds; | 176 SkRect otherOvalBounds; |
234 return path.isOval(&otherOvalBounds) && ovalBounds == otherOvalBounds; | 177 return path.isOval(&otherOvalBounds) && ovalBounds == otherOvalBounds; |
235 } | 178 } |
236 | 179 |
237 return fSkPath == path; | 180 return fSkPath == path; |
238 } | 181 } |
239 #endif | 182 #endif |
OLD | NEW |