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 | 9 |
10 void GrPath::ComputeKey(const SkPath& path, const GrStrokeInfo& stroke, GrUnique
Key* key) { | 10 namespace { |
11 static const GrUniqueKey::Domain kPathDomain = GrUniqueKey::GenerateDomain()
; | 11 // 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 static const int kSimpleVolatilePathVerbLimit = 10; |
| 14 |
| 15 inline static bool compute_key_for_line_path(const SkPath& path, const GrStrokeI
nfo& stroke, |
| 16 GrUniqueKey* key) { |
| 17 SkPoint pts[2]; |
| 18 if (!path.isLine(pts)) { |
| 19 return false; |
| 20 } |
| 21 SK_COMPILE_ASSERT((sizeof(pts) % sizeof(uint32_t)) == 0 && sizeof(pts) > siz
eof(uint32_t), |
| 22 pts_needs_padding); |
| 23 |
| 24 const int kBaseData32Cnt = 1 + sizeof(pts) / sizeof(uint32_t); |
12 int strokeDataCnt = stroke.computeUniqueKeyFragmentData32Cnt(); | 25 int strokeDataCnt = stroke.computeUniqueKeyFragmentData32Cnt(); |
13 GrUniqueKey::Builder builder(key, kPathDomain, 2 + strokeDataCnt); | 26 static const GrUniqueKey::Domain kOvalPathDomain = GrUniqueKey::GenerateDoma
in(); |
| 27 GrUniqueKey::Builder builder(key, kOvalPathDomain, kBaseData32Cnt + strokeDa
taCnt); |
| 28 builder[0] = path.getFillType(); |
| 29 memcpy(&builder[1], &pts, sizeof(pts)); |
| 30 if (strokeDataCnt > 0) { |
| 31 stroke.asUniqueKeyFragment(&builder[kBaseData32Cnt]); |
| 32 } |
| 33 return true; |
| 34 } |
| 35 |
| 36 inline static bool compute_key_for_oval_path(const SkPath& path, const GrStrokeI
nfo& stroke, |
| 37 GrUniqueKey* key) { |
| 38 SkRect rect; |
| 39 if (!path.isOval(&rect)) { |
| 40 return false; |
| 41 } |
| 42 SK_COMPILE_ASSERT((sizeof(rect) % sizeof(uint32_t)) == 0 && sizeof(rect) > s
izeof(uint32_t), |
| 43 rect_needs_padding); |
| 44 |
| 45 const int kBaseData32Cnt = 1 + sizeof(rect) / sizeof(uint32_t); |
| 46 int strokeDataCnt = stroke.computeUniqueKeyFragmentData32Cnt(); |
| 47 static const GrUniqueKey::Domain kOvalPathDomain = GrUniqueKey::GenerateDoma
in(); |
| 48 GrUniqueKey::Builder builder(key, kOvalPathDomain, kBaseData32Cnt + strokeDa
taCnt); |
| 49 builder[0] = path.getFillType(); |
| 50 memcpy(&builder[1], &rect, sizeof(rect)); |
| 51 if (strokeDataCnt > 0) { |
| 52 stroke.asUniqueKeyFragment(&builder[kBaseData32Cnt]); |
| 53 } |
| 54 return true; |
| 55 } |
| 56 |
| 57 // Encodes the full path data to the unique key for very small, volatile paths.
This is typically |
| 58 // hit when clipping stencils the clip stack. Intention is that this handles rec
ts too, since |
| 59 // SkPath::isRect seems to do non-trivial amount of work. |
| 60 inline static bool compute_key_for_simple_path(const SkPath& path, const GrStrok
eInfo& stroke, |
| 61 GrUniqueKey* key) { |
| 62 if (!path.isVolatile()) { |
| 63 return false; |
| 64 } |
| 65 |
| 66 // The check below should take care of negative values casted positive. |
| 67 const int verbCnt = path.countVerbs(); |
| 68 if (verbCnt > kSimpleVolatilePathVerbLimit) { |
| 69 return false; |
| 70 } |
| 71 |
| 72 // If somebody goes wild with the constant, it might cause an overflow. |
| 73 SK_COMPILE_ASSERT(kSimpleVolatilePathVerbLimit <= 100, |
| 74 big_simple_volatile_path_verb_limit_may_cause_overflow); |
| 75 |
| 76 const int pointCnt = path.countPoints(); |
| 77 if (pointCnt < 0) { |
| 78 SkASSERT(false); |
| 79 return false; |
| 80 } |
| 81 |
| 82 // Construct counts that align as uint32_t counts. |
| 83 #define ARRAY_DATA32_COUNT(array_type, count) \ |
| 84 static_cast<int>((((count) * sizeof(array_type) + sizeof(uint32_t) - 1) / si
zeof(uint32_t))) |
| 85 |
| 86 const int verbData32Cnt = ARRAY_DATA32_COUNT(uint8_t, verbCnt); |
| 87 const int pointData32Cnt = ARRAY_DATA32_COUNT(SkPoint, pointCnt); |
| 88 |
| 89 #undef ARRAY_DATA32_COUNT |
| 90 |
| 91 // The unique key data is a "message" with following fragments: |
| 92 // 0) domain, key length, uint32_t for fill type and uint32_t for verbCnt |
| 93 // (fragment 0, fixed size) |
| 94 // 1) verb and point data (varying size) |
| 95 // 2) stroke data (varying size) |
| 96 |
| 97 const int baseData32Cnt = 2 + verbData32Cnt + pointData32Cnt; |
| 98 const int strokeDataCnt = stroke.computeUniqueKeyFragmentData32Cnt(); |
| 99 static const GrUniqueKey::Domain kSimpleVolatilePathDomain = GrUniqueKey::Ge
nerateDomain(); |
| 100 GrUniqueKey::Builder builder(key, kSimpleVolatilePathDomain, baseData32Cnt +
strokeDataCnt); |
| 101 int i = 0; |
| 102 builder[i++] = path.getFillType(); |
| 103 |
| 104 // Serialize the verbCnt to make the whole message unambiguous. |
| 105 // We serialize two variable length fragments to the message: |
| 106 // * verb and point data (fragment 1) |
| 107 // * stroke data (fragment 2) |
| 108 // "Proof:" |
| 109 // Verb count establishes unambiguous verb data. |
| 110 // Unambiguous verb data establishes unambiguous point data, making fragment
1 unambiguous. |
| 111 // Unambiguous fragment 1 establishes unambiguous fragment 2, since the leng
th of the message |
| 112 // has been established. |
| 113 |
| 114 builder[i++] = SkToU32(verbCnt); // The path limit is compile-asserted above
, so the cast is ok. |
| 115 |
| 116 // Fill the last uint32_t with 0 first, since the last uint8_ts of the uint3
2_t may be |
| 117 // uninitialized. This does not produce ambiguous verb data, since we have s
erialized the exact |
| 118 // verb count. |
| 119 if (verbData32Cnt != static_cast<int>((verbCnt * sizeof(uint8_t) / sizeof(ui
nt32_t)))) { |
| 120 builder[i + verbData32Cnt - 1] = 0; |
| 121 } |
| 122 path.getVerbs(reinterpret_cast<uint8_t*>(&builder[i]), verbCnt); |
| 123 i += verbData32Cnt; |
| 124 |
| 125 SK_COMPILE_ASSERT(((sizeof(SkPoint) % sizeof(uint32_t)) == 0) && |
| 126 sizeof(SkPoint) > sizeof(uint32_t), skpoint_array_needs_pa
dding); |
| 127 |
| 128 // Here we assume getPoints does a memcpy, so that we do not need to worry a
bout the alignment. |
| 129 path.getPoints(reinterpret_cast<SkPoint*>(&builder[i]), pointCnt); |
| 130 SkDEBUGCODE(i += pointData32Cnt); |
| 131 |
| 132 SkASSERT(i == baseData32Cnt); |
| 133 if (strokeDataCnt > 0) { |
| 134 stroke.asUniqueKeyFragment(&builder[baseData32Cnt]); |
| 135 } |
| 136 return true; |
| 137 } |
| 138 |
| 139 inline static void compute_key_for_general_path(const SkPath& path, const GrStro
keInfo& stroke, |
| 140 GrUniqueKey* key) { |
| 141 const int kBaseData32Cnt = 2; |
| 142 int strokeDataCnt = stroke.computeUniqueKeyFragmentData32Cnt(); |
| 143 static const GrUniqueKey::Domain kGeneralPathDomain = GrUniqueKey::GenerateD
omain(); |
| 144 GrUniqueKey::Builder builder(key, kGeneralPathDomain, kBaseData32Cnt + strok
eDataCnt); |
14 builder[0] = path.getGenerationID(); | 145 builder[0] = path.getGenerationID(); |
15 builder[1] = path.getFillType(); | 146 builder[1] = path.getFillType(); |
16 if (strokeDataCnt > 0) { | 147 if (strokeDataCnt > 0) { |
17 stroke.asUniqueKeyFragment(&builder[2]); | 148 stroke.asUniqueKeyFragment(&builder[kBaseData32Cnt]); |
18 } | 149 } |
19 } | 150 } |
20 | 151 |
| 152 } |
| 153 |
| 154 void GrPath::ComputeKey(const SkPath& path, const GrStrokeInfo& stroke, GrUnique
Key* key, |
| 155 bool* outIsVolatile) { |
| 156 if (compute_key_for_line_path(path, stroke, key)) { |
| 157 *outIsVolatile = false; |
| 158 return; |
| 159 } |
| 160 |
| 161 if (compute_key_for_oval_path(path, stroke, key)) { |
| 162 *outIsVolatile = false; |
| 163 return; |
| 164 } |
| 165 |
| 166 if (compute_key_for_simple_path(path, stroke, key)) { |
| 167 *outIsVolatile = false; |
| 168 return; |
| 169 } |
| 170 |
| 171 compute_key_for_general_path(path, stroke, key); |
| 172 *outIsVolatile = path.isVolatile(); |
| 173 } |
| 174 |
OLD | NEW |