robertphillips@google.com | 7fa1876 | 2012-09-11 13:02:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrPath.h" |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 9 | #include "GrStyle.h" |
robertphillips@google.com | 7fa1876 | 2012-09-11 13:02:31 +0000 | [diff] [blame] | 10 | |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 11 | namespace { |
| 12 | // Verb count limit for generating path key from content of a volatile path. |
| 13 | // The value should accomodate at least simple rects and rrects. |
| 14 | static const int kSimpleVolatilePathVerbLimit = 10; |
| 15 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 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 have 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::applyToPath(). |
| 26 | GrStyle::WriteKey(dst, style, GrStyle::Apply::kPathEffectAndStrokeRec, SK_Scalar1); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | inline static bool compute_key_for_line_path(const SkPath& path, const GrStyle& style, |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 31 | GrUniqueKey* key) { |
| 32 | SkPoint pts[2]; |
| 33 | if (!path.isLine(pts)) { |
| 34 | return false; |
| 35 | } |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 36 | static_assert((sizeof(pts) % sizeof(uint32_t)) == 0 && sizeof(pts) > sizeof(uint32_t), |
| 37 | "pts_needs_padding"); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 38 | int styleDataCnt = style_data_cnt(style); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 39 | |
| 40 | const int kBaseData32Cnt = 1 + sizeof(pts) / sizeof(uint32_t); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 41 | static const GrUniqueKey::Domain kOvalPathDomain = GrUniqueKey::GenerateDomain(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 42 | GrUniqueKey::Builder builder(key, kOvalPathDomain, kBaseData32Cnt + styleDataCnt); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 43 | builder[0] = path.getFillType(); |
| 44 | memcpy(&builder[1], &pts, sizeof(pts)); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 45 | if (styleDataCnt > 0) { |
| 46 | write_style_key(&builder[kBaseData32Cnt], style); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 47 | } |
| 48 | return true; |
| 49 | } |
| 50 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 51 | inline static bool compute_key_for_oval_path(const SkPath& path, const GrStyle& style, |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 52 | GrUniqueKey* key) { |
| 53 | SkRect rect; |
fmalita | fbe1c11 | 2015-11-18 20:12:56 -0800 | [diff] [blame] | 54 | // Point order is significant when dashing, so we cannot devolve to a rect key. |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 55 | if (style.pathEffect() || !path.isOval(&rect)) { |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 56 | return false; |
| 57 | } |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 58 | static_assert((sizeof(rect) % sizeof(uint32_t)) == 0 && sizeof(rect) > sizeof(uint32_t), |
| 59 | "rect_needs_padding"); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 60 | |
| 61 | const int kBaseData32Cnt = 1 + sizeof(rect) / sizeof(uint32_t); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 62 | int styleDataCnt = style_data_cnt(style); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 63 | static const GrUniqueKey::Domain kOvalPathDomain = GrUniqueKey::GenerateDomain(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 64 | GrUniqueKey::Builder builder(key, kOvalPathDomain, kBaseData32Cnt + styleDataCnt); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 65 | builder[0] = path.getFillType(); |
| 66 | memcpy(&builder[1], &rect, sizeof(rect)); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 67 | if (styleDataCnt > 0) { |
| 68 | write_style_key(&builder[kBaseData32Cnt], style); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 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 rects too, since |
| 75 | // SkPath::isRect seems to do non-trivial amount of work. |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 76 | inline static bool compute_key_for_simple_path(const SkPath& path, const GrStyle& style, |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 77 | GrUniqueKey* key) { |
| 78 | if (!path.isVolatile()) { |
| 79 | return false; |
| 80 | } |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 81 | // The check below should take care of negative values casted positive. |
| 82 | const int verbCnt = path.countVerbs(); |
| 83 | if (verbCnt > kSimpleVolatilePathVerbLimit) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | // If somebody goes wild with the constant, it might cause an overflow. |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 88 | static_assert(kSimpleVolatilePathVerbLimit <= 100, |
| 89 | "big_simple_volatile_path_verb_limit_may_cause_overflow"); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 90 | |
| 91 | const int pointCnt = path.countPoints(); |
| 92 | if (pointCnt < 0) { |
| 93 | SkASSERT(false); |
| 94 | return false; |
| 95 | } |
kkinnunen | f152463 | 2015-11-26 01:51:44 -0800 | [diff] [blame] | 96 | SkSTArray<16, SkScalar, true> conicWeights(16); |
| 97 | if ((path.getSegmentMasks() & SkPath::kConic_SegmentMask) != 0) { |
| 98 | SkPath::RawIter iter(path); |
| 99 | SkPath::Verb verb; |
| 100 | SkPoint points[4]; |
| 101 | while ((verb = iter.next(points)) != SkPath::kDone_Verb) { |
| 102 | if (verb == SkPath::kConic_Verb) { |
| 103 | conicWeights.push_back(iter.conicWeight()); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | const int conicWeightCnt = conicWeights.count(); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 109 | |
| 110 | // Construct counts that align as uint32_t counts. |
| 111 | #define ARRAY_DATA32_COUNT(array_type, count) \ |
| 112 | static_cast<int>((((count) * sizeof(array_type) + sizeof(uint32_t) - 1) / sizeof(uint32_t))) |
| 113 | |
| 114 | const int verbData32Cnt = ARRAY_DATA32_COUNT(uint8_t, verbCnt); |
| 115 | const int pointData32Cnt = ARRAY_DATA32_COUNT(SkPoint, pointCnt); |
kkinnunen | f152463 | 2015-11-26 01:51:44 -0800 | [diff] [blame] | 116 | const int conicWeightData32Cnt = ARRAY_DATA32_COUNT(SkScalar, conicWeightCnt); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 117 | |
| 118 | #undef ARRAY_DATA32_COUNT |
| 119 | |
| 120 | // 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 |
| 122 | // (fragment 0, fixed size) |
kkinnunen | f152463 | 2015-11-26 01:51:44 -0800 | [diff] [blame] | 123 | // 1) verb, point data and conic weights (varying size) |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 124 | // 2) stroke data (varying size) |
| 125 | |
kkinnunen | f152463 | 2015-11-26 01:51:44 -0800 | [diff] [blame] | 126 | const int baseData32Cnt = 2 + verbData32Cnt + pointData32Cnt + conicWeightData32Cnt; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 127 | const int styleDataCnt = style_data_cnt(style); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 128 | static const GrUniqueKey::Domain kSimpleVolatilePathDomain = GrUniqueKey::GenerateDomain(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 129 | GrUniqueKey::Builder builder(key, kSimpleVolatilePathDomain, baseData32Cnt + styleDataCnt); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 130 | int i = 0; |
| 131 | builder[i++] = path.getFillType(); |
| 132 | |
| 133 | // Serialize the verbCnt to make the whole message unambiguous. |
| 134 | // We serialize two variable length fragments to the message: |
kkinnunen | f152463 | 2015-11-26 01:51:44 -0800 | [diff] [blame] | 135 | // * verbs, point data and conic weights (fragment 1) |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 136 | // * stroke data (fragment 2) |
| 137 | // "Proof:" |
| 138 | // Verb count establishes unambiguous verb data. |
kkinnunen | f152463 | 2015-11-26 01:51:44 -0800 | [diff] [blame] | 139 | // Verbs encode also point data size and conic weight size. |
| 140 | // Thus the fragment 1 is unambiguous. |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 141 | // Unambiguous fragment 1 establishes unambiguous fragment 2, since the length of the message |
| 142 | // has been established. |
| 143 | |
| 144 | builder[i++] = SkToU32(verbCnt); // The path limit is compile-asserted above, so the cast is ok. |
| 145 | |
| 146 | // Fill the last uint32_t with 0 first, since the last uint8_ts of the uint32_t may be |
| 147 | // uninitialized. This does not produce ambiguous verb data, since we have serialized the exact |
| 148 | // verb count. |
| 149 | if (verbData32Cnt != static_cast<int>((verbCnt * sizeof(uint8_t) / sizeof(uint32_t)))) { |
| 150 | builder[i + verbData32Cnt - 1] = 0; |
| 151 | } |
| 152 | path.getVerbs(reinterpret_cast<uint8_t*>(&builder[i]), verbCnt); |
| 153 | i += verbData32Cnt; |
| 154 | |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 155 | static_assert(((sizeof(SkPoint) % sizeof(uint32_t)) == 0) && sizeof(SkPoint) > sizeof(uint32_t), |
| 156 | "skpoint_array_needs_padding"); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 157 | |
| 158 | // Here we assume getPoints does a memcpy, so that we do not need to worry about the alignment. |
| 159 | path.getPoints(reinterpret_cast<SkPoint*>(&builder[i]), pointCnt); |
kkinnunen | f152463 | 2015-11-26 01:51:44 -0800 | [diff] [blame] | 160 | i += pointData32Cnt; |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 161 | |
kkinnunen | f152463 | 2015-11-26 01:51:44 -0800 | [diff] [blame] | 162 | if (conicWeightCnt > 0) { |
| 163 | if (conicWeightData32Cnt != static_cast<int>( |
| 164 | (conicWeightCnt * sizeof(SkScalar) / sizeof(uint32_t)))) { |
| 165 | builder[i + conicWeightData32Cnt - 1] = 0; |
| 166 | } |
| 167 | memcpy(&builder[i], conicWeights.begin(), conicWeightCnt * sizeof(SkScalar)); |
| 168 | SkDEBUGCODE(i += conicWeightData32Cnt); |
| 169 | } |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 170 | SkASSERT(i == baseData32Cnt); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 171 | if (styleDataCnt > 0) { |
| 172 | write_style_key(&builder[baseData32Cnt], style); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 173 | } |
| 174 | return true; |
| 175 | } |
| 176 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 177 | inline static void compute_key_for_general_path(const SkPath& path, const GrStyle& style, |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 178 | GrUniqueKey* key) { |
| 179 | const int kBaseData32Cnt = 2; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 180 | int styleDataCnt = style_data_cnt(style); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 181 | static const GrUniqueKey::Domain kGeneralPathDomain = GrUniqueKey::GenerateDomain(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 182 | GrUniqueKey::Builder builder(key, kGeneralPathDomain, kBaseData32Cnt + styleDataCnt); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 183 | builder[0] = path.getGenerationID(); |
| 184 | builder[1] = path.getFillType(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 185 | if (styleDataCnt > 0) { |
| 186 | write_style_key(&builder[kBaseData32Cnt], style); |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 187 | } |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 188 | } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 189 | |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 190 | } |
| 191 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 192 | void GrPath::ComputeKey(const SkPath& path, const GrStyle& style, GrUniqueKey* key, |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 193 | bool* outIsVolatile) { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 194 | if (compute_key_for_line_path(path, style, key)) { |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 195 | *outIsVolatile = false; |
| 196 | return; |
| 197 | } |
| 198 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 199 | if (compute_key_for_oval_path(path, style, key)) { |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 200 | *outIsVolatile = false; |
| 201 | return; |
| 202 | } |
| 203 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 204 | if (compute_key_for_simple_path(path, style, key)) { |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 205 | *outIsVolatile = false; |
| 206 | return; |
| 207 | } |
| 208 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 209 | compute_key_for_general_path(path, style, key); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 210 | *outIsVolatile = path.isVolatile(); |
| 211 | } |
| 212 | |
fmalita | fbe1c11 | 2015-11-18 20:12:56 -0800 | [diff] [blame] | 213 | #ifdef SK_DEBUG |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 214 | bool GrPath::isEqualTo(const SkPath& path, const GrStyle& style) const { |
| 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) { |
fmalita | fbe1c11 | 2015-11-18 20:12:56 -0800 | [diff] [blame] | 219 | return false; |
| 220 | } |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 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 | } |
fmalita | fbe1c11 | 2015-11-18 20:12:56 -0800 | [diff] [blame] | 230 | // We treat same-rect ovals as identical - but only when not dashing. |
| 231 | SkRect ovalBounds; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 232 | if (!fStyle.isDashed() && fSkPath.isOval(&ovalBounds)) { |
fmalita | fbe1c11 | 2015-11-18 20:12:56 -0800 | [diff] [blame] | 233 | SkRect otherOvalBounds; |
| 234 | return path.isOval(&otherOvalBounds) && ovalBounds == otherOvalBounds; |
| 235 | } |
| 236 | |
| 237 | return fSkPath == path; |
| 238 | } |
| 239 | #endif |