blob: 91a245d854383579168af01eeb764769ec76e913 [file] [log] [blame]
robertphillips@google.com7fa18762012-09-11 13:02:31 +00001/*
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"
bsalomon6663acf2016-05-10 09:14:17 -07009#include "GrStyle.h"
robertphillips@google.com7fa18762012-09-11 13:02:31 +000010
kkinnunen070e0102015-05-21 00:37:30 -070011namespace {
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.
14static const int kSimpleVolatilePathVerbLimit = 10;
15
bsalomon6663acf2016-05-10 09:14:17 -070016static 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
24static 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
30inline static bool compute_key_for_line_path(const SkPath& path, const GrStyle& style,
kkinnunen070e0102015-05-21 00:37:30 -070031 GrUniqueKey* key) {
32 SkPoint pts[2];
33 if (!path.isLine(pts)) {
34 return false;
35 }
bungeman99fe8222015-08-20 07:57:51 -070036 static_assert((sizeof(pts) % sizeof(uint32_t)) == 0 && sizeof(pts) > sizeof(uint32_t),
37 "pts_needs_padding");
bsalomon6663acf2016-05-10 09:14:17 -070038 int styleDataCnt = style_data_cnt(style);
kkinnunen070e0102015-05-21 00:37:30 -070039
40 const int kBaseData32Cnt = 1 + sizeof(pts) / sizeof(uint32_t);
kkinnunen070e0102015-05-21 00:37:30 -070041 static const GrUniqueKey::Domain kOvalPathDomain = GrUniqueKey::GenerateDomain();
bsalomon6663acf2016-05-10 09:14:17 -070042 GrUniqueKey::Builder builder(key, kOvalPathDomain, kBaseData32Cnt + styleDataCnt);
kkinnunen070e0102015-05-21 00:37:30 -070043 builder[0] = path.getFillType();
44 memcpy(&builder[1], &pts, sizeof(pts));
bsalomon6663acf2016-05-10 09:14:17 -070045 if (styleDataCnt > 0) {
46 write_style_key(&builder[kBaseData32Cnt], style);
kkinnunen070e0102015-05-21 00:37:30 -070047 }
48 return true;
49}
50
bsalomon6663acf2016-05-10 09:14:17 -070051inline static bool compute_key_for_oval_path(const SkPath& path, const GrStyle& style,
kkinnunen070e0102015-05-21 00:37:30 -070052 GrUniqueKey* key) {
53 SkRect rect;
fmalitafbe1c112015-11-18 20:12:56 -080054 // Point order is significant when dashing, so we cannot devolve to a rect key.
bsalomon6663acf2016-05-10 09:14:17 -070055 if (style.pathEffect() || !path.isOval(&rect)) {
kkinnunen070e0102015-05-21 00:37:30 -070056 return false;
57 }
bungeman99fe8222015-08-20 07:57:51 -070058 static_assert((sizeof(rect) % sizeof(uint32_t)) == 0 && sizeof(rect) > sizeof(uint32_t),
59 "rect_needs_padding");
kkinnunen070e0102015-05-21 00:37:30 -070060
61 const int kBaseData32Cnt = 1 + sizeof(rect) / sizeof(uint32_t);
bsalomon6663acf2016-05-10 09:14:17 -070062 int styleDataCnt = style_data_cnt(style);
kkinnunen070e0102015-05-21 00:37:30 -070063 static const GrUniqueKey::Domain kOvalPathDomain = GrUniqueKey::GenerateDomain();
bsalomon6663acf2016-05-10 09:14:17 -070064 GrUniqueKey::Builder builder(key, kOvalPathDomain, kBaseData32Cnt + styleDataCnt);
kkinnunen070e0102015-05-21 00:37:30 -070065 builder[0] = path.getFillType();
66 memcpy(&builder[1], &rect, sizeof(rect));
bsalomon6663acf2016-05-10 09:14:17 -070067 if (styleDataCnt > 0) {
68 write_style_key(&builder[kBaseData32Cnt], style);
kkinnunen070e0102015-05-21 00:37:30 -070069 }
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.
bsalomon6663acf2016-05-10 09:14:17 -070076inline static bool compute_key_for_simple_path(const SkPath& path, const GrStyle& style,
kkinnunen070e0102015-05-21 00:37:30 -070077 GrUniqueKey* key) {
78 if (!path.isVolatile()) {
79 return false;
80 }
kkinnunen070e0102015-05-21 00:37:30 -070081 // 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.
bungeman99fe8222015-08-20 07:57:51 -070088 static_assert(kSimpleVolatilePathVerbLimit <= 100,
89 "big_simple_volatile_path_verb_limit_may_cause_overflow");
kkinnunen070e0102015-05-21 00:37:30 -070090
91 const int pointCnt = path.countPoints();
92 if (pointCnt < 0) {
93 SkASSERT(false);
94 return false;
95 }
kkinnunenf1524632015-11-26 01:51:44 -080096 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();
kkinnunen070e0102015-05-21 00:37:30 -0700109
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);
kkinnunenf1524632015-11-26 01:51:44 -0800116 const int conicWeightData32Cnt = ARRAY_DATA32_COUNT(SkScalar, conicWeightCnt);
kkinnunen070e0102015-05-21 00:37:30 -0700117
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)
kkinnunenf1524632015-11-26 01:51:44 -0800123 // 1) verb, point data and conic weights (varying size)
kkinnunen070e0102015-05-21 00:37:30 -0700124 // 2) stroke data (varying size)
125
kkinnunenf1524632015-11-26 01:51:44 -0800126 const int baseData32Cnt = 2 + verbData32Cnt + pointData32Cnt + conicWeightData32Cnt;
bsalomon6663acf2016-05-10 09:14:17 -0700127 const int styleDataCnt = style_data_cnt(style);
kkinnunen070e0102015-05-21 00:37:30 -0700128 static const GrUniqueKey::Domain kSimpleVolatilePathDomain = GrUniqueKey::GenerateDomain();
bsalomon6663acf2016-05-10 09:14:17 -0700129 GrUniqueKey::Builder builder(key, kSimpleVolatilePathDomain, baseData32Cnt + styleDataCnt);
kkinnunen070e0102015-05-21 00:37:30 -0700130 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:
kkinnunenf1524632015-11-26 01:51:44 -0800135 // * verbs, point data and conic weights (fragment 1)
kkinnunen070e0102015-05-21 00:37:30 -0700136 // * stroke data (fragment 2)
137 // "Proof:"
138 // Verb count establishes unambiguous verb data.
kkinnunenf1524632015-11-26 01:51:44 -0800139 // Verbs encode also point data size and conic weight size.
140 // Thus the fragment 1 is unambiguous.
kkinnunen070e0102015-05-21 00:37:30 -0700141 // 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
bungeman99fe8222015-08-20 07:57:51 -0700155 static_assert(((sizeof(SkPoint) % sizeof(uint32_t)) == 0) && sizeof(SkPoint) > sizeof(uint32_t),
156 "skpoint_array_needs_padding");
kkinnunen070e0102015-05-21 00:37:30 -0700157
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);
kkinnunenf1524632015-11-26 01:51:44 -0800160 i += pointData32Cnt;
kkinnunen070e0102015-05-21 00:37:30 -0700161
kkinnunenf1524632015-11-26 01:51:44 -0800162 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 }
kkinnunen070e0102015-05-21 00:37:30 -0700170 SkASSERT(i == baseData32Cnt);
bsalomon6663acf2016-05-10 09:14:17 -0700171 if (styleDataCnt > 0) {
172 write_style_key(&builder[baseData32Cnt], style);
kkinnunen070e0102015-05-21 00:37:30 -0700173 }
174 return true;
175}
176
bsalomon6663acf2016-05-10 09:14:17 -0700177inline static void compute_key_for_general_path(const SkPath& path, const GrStyle& style,
kkinnunen070e0102015-05-21 00:37:30 -0700178 GrUniqueKey* key) {
179 const int kBaseData32Cnt = 2;
bsalomon6663acf2016-05-10 09:14:17 -0700180 int styleDataCnt = style_data_cnt(style);
kkinnunen070e0102015-05-21 00:37:30 -0700181 static const GrUniqueKey::Domain kGeneralPathDomain = GrUniqueKey::GenerateDomain();
bsalomon6663acf2016-05-10 09:14:17 -0700182 GrUniqueKey::Builder builder(key, kGeneralPathDomain, kBaseData32Cnt + styleDataCnt);
kkinnunen50b58e62015-05-18 23:02:07 -0700183 builder[0] = path.getGenerationID();
184 builder[1] = path.getFillType();
bsalomon6663acf2016-05-10 09:14:17 -0700185 if (styleDataCnt > 0) {
186 write_style_key(&builder[kBaseData32Cnt], style);
cdaltonb85a0aa2014-07-21 15:32:44 -0700187 }
cdaltonb85a0aa2014-07-21 15:32:44 -0700188}
kkinnunen50b58e62015-05-18 23:02:07 -0700189
kkinnunen070e0102015-05-21 00:37:30 -0700190}
191
bsalomon6663acf2016-05-10 09:14:17 -0700192void GrPath::ComputeKey(const SkPath& path, const GrStyle& style, GrUniqueKey* key,
kkinnunen070e0102015-05-21 00:37:30 -0700193 bool* outIsVolatile) {
bsalomon6663acf2016-05-10 09:14:17 -0700194 if (compute_key_for_line_path(path, style, key)) {
kkinnunen070e0102015-05-21 00:37:30 -0700195 *outIsVolatile = false;
196 return;
197 }
198
bsalomon6663acf2016-05-10 09:14:17 -0700199 if (compute_key_for_oval_path(path, style, key)) {
kkinnunen070e0102015-05-21 00:37:30 -0700200 *outIsVolatile = false;
201 return;
202 }
203
bsalomon6663acf2016-05-10 09:14:17 -0700204 if (compute_key_for_simple_path(path, style, key)) {
kkinnunen070e0102015-05-21 00:37:30 -0700205 *outIsVolatile = false;
206 return;
207 }
208
bsalomon6663acf2016-05-10 09:14:17 -0700209 compute_key_for_general_path(path, style, key);
kkinnunen070e0102015-05-21 00:37:30 -0700210 *outIsVolatile = path.isVolatile();
211}
212
fmalitafbe1c112015-11-18 20:12:56 -0800213#ifdef SK_DEBUG
bsalomon6663acf2016-05-10 09:14:17 -0700214bool 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) {
fmalitafbe1c112015-11-18 20:12:56 -0800219 return false;
220 }
bsalomon6663acf2016-05-10 09:14:17 -0700221 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 }
fmalitafbe1c112015-11-18 20:12:56 -0800230 // We treat same-rect ovals as identical - but only when not dashing.
231 SkRect ovalBounds;
bsalomon6663acf2016-05-10 09:14:17 -0700232 if (!fStyle.isDashed() && fSkPath.isOval(&ovalBounds)) {
fmalitafbe1c112015-11-18 20:12:56 -0800233 SkRect otherOvalBounds;
234 return path.isOval(&otherOvalBounds) && ovalBounds == otherOvalBounds;
235 }
236
237 return fSkPath == path;
238}
239#endif