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