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 | |
30 inline static bool compute_key_for_line_path(const SkPath& path, const GrStyle& style, | |
bsalomon
2016/09/15 14:53:50
GrShape already computes geometric keys for simple
| |
31 GrUniqueKey* key) { | |
32 SkPoint pts[2]; | |
33 if (!path.isLine(pts)) { | |
34 return false; | |
35 } | |
36 static_assert((sizeof(pts) % sizeof(uint32_t)) == 0 && sizeof(pts) > sizeof( uint32_t), | |
37 "pts_needs_padding"); | |
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 | 28 // 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 | 29 // 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. | 30 // 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, | 31 inline static bool compute_key_for_simple_path(const GrShape& shape, GrUniqueKey * key) { |
77 GrUniqueKey* key) { | 32 SkPath path; |
78 if (!path.isVolatile()) { | 33 shape.asPath(&path); |
79 return false; | |
80 } | |
81 // The check below should take care of negative values casted positive. | 34 // The check below should take care of negative values casted positive. |
82 const int verbCnt = path.countVerbs(); | 35 const int verbCnt = path.countVerbs(); |
83 if (verbCnt > kSimpleVolatilePathVerbLimit) { | 36 if (verbCnt > kSimpleVolatilePathVerbLimit) { |
84 return false; | 37 return false; |
85 } | 38 } |
86 | 39 |
87 // If somebody goes wild with the constant, it might cause an overflow. | 40 // If somebody goes wild with the constant, it might cause an overflow. |
88 static_assert(kSimpleVolatilePathVerbLimit <= 100, | 41 static_assert(kSimpleVolatilePathVerbLimit <= 100, |
89 "big_simple_volatile_path_verb_limit_may_cause_overflow"); | 42 "big_simple_volatile_path_verb_limit_may_cause_overflow"); |
90 | 43 |
(...skipping 26 matching lines...) Expand all Loading... | |
117 | 70 |
118 #undef ARRAY_DATA32_COUNT | 71 #undef ARRAY_DATA32_COUNT |
119 | 72 |
120 // The unique key data is a "message" with following fragments: | 73 // 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 | 74 // 0) domain, key length, uint32_t for fill type and uint32_t for verbCnt |
122 // (fragment 0, fixed size) | 75 // (fragment 0, fixed size) |
123 // 1) verb, point data and conic weights (varying size) | 76 // 1) verb, point data and conic weights (varying size) |
124 // 2) stroke data (varying size) | 77 // 2) stroke data (varying size) |
125 | 78 |
126 const int baseData32Cnt = 2 + verbData32Cnt + pointData32Cnt + conicWeightDa ta32Cnt; | 79 const int baseData32Cnt = 2 + verbData32Cnt + pointData32Cnt + conicWeightDa ta32Cnt; |
127 const int styleDataCnt = style_data_cnt(style); | 80 const int styleDataCnt = style_data_cnt(shape.style()); |
128 static const GrUniqueKey::Domain kSimpleVolatilePathDomain = GrUniqueKey::Ge nerateDomain(); | 81 static const GrUniqueKey::Domain kSimpleVolatilePathDomain = GrUniqueKey::Ge nerateDomain(); |
129 GrUniqueKey::Builder builder(key, kSimpleVolatilePathDomain, baseData32Cnt + styleDataCnt); | 82 GrUniqueKey::Builder builder(key, kSimpleVolatilePathDomain, baseData32Cnt + styleDataCnt); |
130 int i = 0; | 83 int i = 0; |
131 builder[i++] = path.getFillType(); | 84 builder[i++] = path.getFillType(); |
132 | 85 |
133 // Serialize the verbCnt to make the whole message unambiguous. | 86 // Serialize the verbCnt to make the whole message unambiguous. |
134 // We serialize two variable length fragments to the message: | 87 // We serialize two variable length fragments to the message: |
135 // * verbs, point data and conic weights (fragment 1) | 88 // * verbs, point data and conic weights (fragment 1) |
136 // * stroke data (fragment 2) | 89 // * stroke data (fragment 2) |
137 // "Proof:" | 90 // "Proof:" |
(...skipping 24 matching lines...) Expand all Loading... | |
162 if (conicWeightCnt > 0) { | 115 if (conicWeightCnt > 0) { |
163 if (conicWeightData32Cnt != static_cast<int>( | 116 if (conicWeightData32Cnt != static_cast<int>( |
164 (conicWeightCnt * sizeof(SkScalar) / sizeof(uint32_t)))) { | 117 (conicWeightCnt * sizeof(SkScalar) / sizeof(uint32_t)))) { |
165 builder[i + conicWeightData32Cnt - 1] = 0; | 118 builder[i + conicWeightData32Cnt - 1] = 0; |
166 } | 119 } |
167 memcpy(&builder[i], conicWeights.begin(), conicWeightCnt * sizeof(SkScal ar)); | 120 memcpy(&builder[i], conicWeights.begin(), conicWeightCnt * sizeof(SkScal ar)); |
168 SkDEBUGCODE(i += conicWeightData32Cnt); | 121 SkDEBUGCODE(i += conicWeightData32Cnt); |
169 } | 122 } |
170 SkASSERT(i == baseData32Cnt); | 123 SkASSERT(i == baseData32Cnt); |
171 if (styleDataCnt > 0) { | 124 if (styleDataCnt > 0) { |
172 write_style_key(&builder[baseData32Cnt], style); | 125 write_style_key(&builder[baseData32Cnt], shape.style()); |
173 } | 126 } |
174 return true; | 127 return true; |
175 } | 128 } |
176 | 129 |
177 inline static void compute_key_for_general_path(const SkPath& path, const GrStyl e& style, | 130 inline static bool compute_key_for_general_shape(const GrShape& shape, GrUniqueK ey* key) { |
178 GrUniqueKey* key) { | 131 if (!shape.hasUnstyledKey()) { |
179 const int kBaseData32Cnt = 2; | 132 return false; |
180 int styleDataCnt = style_data_cnt(style); | 133 } |
134 int cnt = shape.unstyledKeySize(); | |
181 static const GrUniqueKey::Domain kGeneralPathDomain = GrUniqueKey::GenerateD omain(); | 135 static const GrUniqueKey::Domain kGeneralPathDomain = GrUniqueKey::GenerateD omain(); |
182 GrUniqueKey::Builder builder(key, kGeneralPathDomain, kBaseData32Cnt + style DataCnt); | 136 GrUniqueKey::Builder builder(key, kGeneralPathDomain, cnt); |
183 builder[0] = path.getGenerationID(); | 137 shape.writeUnstyledKey(&builder[0]); |
184 builder[1] = path.getFillType(); | 138 return true; |
185 if (styleDataCnt > 0) { | |
186 write_style_key(&builder[kBaseData32Cnt], style); | |
187 } | |
188 } | 139 } |
189 | 140 |
190 } | 141 void GrPath::ComputeKey(const GrShape& shape, GrUniqueKey* key, bool* outIsVolat ile) { |
191 | 142 |
192 void GrPath::ComputeKey(const SkPath& path, const GrStyle& style, GrUniqueKey* k ey, | 143 if (compute_key_for_simple_path(shape, key)) { |
193 bool* outIsVolatile) { | |
194 if (compute_key_for_line_path(path, style, key)) { | |
195 *outIsVolatile = false; | 144 *outIsVolatile = false; |
196 return; | 145 return; |
197 } | 146 } |
198 | 147 |
199 if (compute_key_for_oval_path(path, style, key)) { | 148 if (!compute_key_for_general_shape(shape, key)) { |
200 *outIsVolatile = false; | 149 *outIsVolatile = true; |
201 return; | |
202 } | 150 } |
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 } | 151 } |
212 | 152 |
213 #ifdef SK_DEBUG | 153 #ifdef SK_DEBUG |
214 bool GrPath::isEqualTo(const SkPath& path, const GrStyle& style) const { | 154 bool GrPath::isEqualTo(const SkPath& path, const GrStyle& style) const { |
215 // Since this is only called in debug we don't care about performance. | 155 // Since this is only called in debug we don't care about performance. |
216 int cnt0 = GrStyle::KeySize(fStyle, GrStyle::Apply::kPathEffectAndStrokeRec) ; | 156 int cnt0 = GrStyle::KeySize(fStyle, GrStyle::Apply::kPathEffectAndStrokeRec) ; |
217 int cnt1 = GrStyle::KeySize(style, GrStyle::Apply::kPathEffectAndStrokeRec); | 157 int cnt1 = GrStyle::KeySize(style, GrStyle::Apply::kPathEffectAndStrokeRec); |
218 if (cnt0 < 0 || cnt1 < 0 || cnt0 != cnt1) { | 158 if (cnt0 < 0 || cnt1 < 0 || cnt0 != cnt1) { |
219 return false; | 159 return false; |
220 } | 160 } |
221 if (cnt0) { | 161 if (cnt0) { |
222 SkAutoTArray<uint32_t> key0(cnt0); | 162 SkAutoTArray<uint32_t> key0(cnt0); |
223 SkAutoTArray<uint32_t> key1(cnt0); | 163 SkAutoTArray<uint32_t> key1(cnt0); |
224 write_style_key(key0.get(), fStyle); | 164 write_style_key(key0.get(), fStyle); |
225 write_style_key(key1.get(), style); | 165 write_style_key(key1.get(), style); |
226 if (0 != memcmp(key0.get(), key1.get(), cnt0)) { | 166 if (0 != memcmp(key0.get(), key1.get(), cnt0)) { |
227 return false; | 167 return false; |
228 } | 168 } |
229 } | 169 } |
230 // We treat same-rect ovals as identical - but only when not dashing. | 170 // We treat same-rect ovals as identical - but only when not dashing. |
231 SkRect ovalBounds; | 171 SkRect ovalBounds; |
232 if (!fStyle.isDashed() && fSkPath.isOval(&ovalBounds)) { | 172 if (!fStyle.isDashed() && fSkPath.isOval(&ovalBounds)) { |
233 SkRect otherOvalBounds; | 173 SkRect otherOvalBounds; |
234 return path.isOval(&otherOvalBounds) && ovalBounds == otherOvalBounds; | 174 return path.isOval(&otherOvalBounds) && ovalBounds == otherOvalBounds; |
235 } | 175 } |
236 | 176 |
237 return fSkPath == path; | 177 return fSkPath == path; |
238 } | 178 } |
239 #endif | 179 #endif |
OLD | NEW |