blob: e0ddc55e028e2a1b1ce24be5bd831dc9ea4d0620 [file] [log] [blame]
bsalomon47cc7692016-04-26 12:56:00 -07001/*
2 * Copyright 2016 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 "GrShape.h"
9
10GrShape& GrShape::operator=(const GrShape& that) {
11 bool wasPath = Type::kPath == fType;
12 fStyle = that.fStyle;
13 fType = that.fType;
14 switch (fType) {
15 case Type::kEmpty:
16 if (wasPath) {
17 fPath.reset();
18 }
19 break;
20 case Type::kRRect:
21 if (wasPath) {
22 fPath.reset();
23 }
24 fRRect = that.fRRect;
25 break;
26 case Type::kPath:
27 if (wasPath) {
28 *fPath.get() = *that.fPath.get();
29 } else {
30 fPath.set(*that.fPath.get());
31 }
32 break;
33 }
34 fInheritedKey.reset(that.fInheritedKey.count());
benjaminwagnerd9cca4a2016-05-04 11:06:19 -070035 sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
36 sizeof(uint32_t) * fInheritedKey.count());
bsalomon47cc7692016-04-26 12:56:00 -070037 return *this;
38}
39
bsalomon9fb42032016-05-13 09:23:38 -070040const SkRect& GrShape::bounds() const {
41 static constexpr SkRect kEmpty = SkRect::MakeEmpty();
42 switch (fType) {
43 case Type::kEmpty:
44 return kEmpty;
45 case Type::kRRect:
46 return fRRect.getBounds();
47 case Type::kPath:
48 return fPath.get()->getBounds();
49 }
50 SkFAIL("Unknown shape type");
51 return kEmpty;
52}
53
54void GrShape::styledBounds(SkRect* bounds) const {
55 if (Type::kEmpty == fType && !fStyle.hasNonDashPathEffect()) {
56 *bounds = SkRect::MakeEmpty();
57 } else {
58 fStyle.adjustBounds(bounds, this->bounds());
59 }
60}
61
bsalomon47cc7692016-04-26 12:56:00 -070062int GrShape::unstyledKeySize() const {
63 if (fInheritedKey.count()) {
64 return fInheritedKey.count();
65 }
66 switch (fType) {
67 case Type::kEmpty:
68 return 1;
69 case Type::kRRect:
70 SkASSERT(!fInheritedKey.count());
71 SkASSERT(0 == SkRRect::kSizeInMemory % sizeof(uint32_t));
72 return SkRRect::kSizeInMemory / sizeof(uint32_t);
73 case Type::kPath:
74 if (fPath.get()->isVolatile()) {
75 return -1;
76 } else {
77 return 1;
78 }
79 }
80 SkFAIL("Should never get here.");
81 return 0;
82}
83
84void GrShape::writeUnstyledKey(uint32_t* key) const {
85 SkASSERT(this->unstyledKeySize());
86 SkDEBUGCODE(uint32_t* origKey = key;)
87 if (fInheritedKey.count()) {
88 memcpy(key, fInheritedKey.get(), sizeof(uint32_t) * fInheritedKey.count());
89 SkDEBUGCODE(key += fInheritedKey.count();)
90 } else {
91 switch (fType) {
92 case Type::kEmpty:
93 *key++ = 1;
94 break;
95 case Type::kRRect:
96 fRRect.writeToMemory(key);
97 key += SkRRect::kSizeInMemory / sizeof(uint32_t);
98 break;
99 case Type::kPath:
100 SkASSERT(!fPath.get()->isVolatile());
101 *key++ = fPath.get()->getGenerationID();
102 break;
103 }
104 }
105 SkASSERT(key - origKey == this->unstyledKeySize());
106}
107
bsalomon97fd2d42016-05-09 13:02:01 -0700108void GrShape::setInheritedKey(const GrShape &parent, GrStyle::Apply apply, SkScalar scale) {
bsalomon47cc7692016-04-26 12:56:00 -0700109 SkASSERT(!fInheritedKey.count());
110 // If the output shape turns out to be simple, then we will just use its geometric key
111 if (Type::kPath == fType) {
112 // We want ApplyFullStyle(ApplyPathEffect(shape)) to have the same key as
113 // ApplyFullStyle(shape).
114 // The full key is structured as (geo,path_effect,stroke).
115 // If we do ApplyPathEffect we get get,path_effect as the inherited key. If we then
116 // do ApplyFullStyle we'll memcpy geo,path_effect into the new inherited key
117 // and then append the style key (which should now be stroke only) at the end.
118 int parentCnt = parent.fInheritedKey.count();
119 bool useParentGeoKey = !parentCnt;
120 if (useParentGeoKey) {
121 parentCnt = parent.unstyledKeySize();
bsalomon72dc51c2016-04-27 06:46:23 -0700122 if (parentCnt < 0) {
123 // The parent's geometry has no key so we will have no key.
124 fPath.get()->setIsVolatile(true);
125 return;
126 }
bsalomon47cc7692016-04-26 12:56:00 -0700127 }
bsalomon06077562016-05-04 13:50:29 -0700128 uint32_t styleKeyFlags = 0;
129 if (parent.knownToBeClosed()) {
130 styleKeyFlags |= GrStyle::kClosed_KeyFlag;
131 }
132 int styleCnt = GrStyle::KeySize(parent.fStyle, apply, styleKeyFlags);
bsalomon47cc7692016-04-26 12:56:00 -0700133 if (styleCnt < 0) {
134 // The style doesn't allow a key, set the path to volatile so that we fail when
135 // we try to get a key for the shape.
136 fPath.get()->setIsVolatile(true);
bsalomon72dc51c2016-04-27 06:46:23 -0700137 return;
bsalomon47cc7692016-04-26 12:56:00 -0700138 }
bsalomon72dc51c2016-04-27 06:46:23 -0700139 fInheritedKey.reset(parentCnt + styleCnt);
140 if (useParentGeoKey) {
141 // This will be the geo key.
142 parent.writeUnstyledKey(fInheritedKey.get());
143 } else {
144 // This should be (geo,path_effect).
145 memcpy(fInheritedKey.get(), parent.fInheritedKey.get(),
146 parentCnt * sizeof(uint32_t));
147 }
148 // Now turn (geo,path_effect) or (geo) into (geo,path_effect,stroke)
bsalomon97fd2d42016-05-09 13:02:01 -0700149 GrStyle::WriteKey(fInheritedKey.get() + parentCnt, parent.fStyle, apply, scale,
150 styleKeyFlags);
bsalomon47cc7692016-04-26 12:56:00 -0700151 }
152}
153
154GrShape::GrShape(const GrShape& that) : fType(that.fType), fStyle(that.fStyle) {
155 switch (fType) {
156 case Type::kEmpty:
157 return;
158 case Type::kRRect:
159 fRRect = that.fRRect;
160 return;
161 case Type::kPath:
162 fPath.set(*that.fPath.get());
163 return;
164 }
165 fInheritedKey.reset(that.fInheritedKey.count());
166 memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
167 sizeof(uint32_t) * fInheritedKey.count());
168}
169
bsalomon97fd2d42016-05-09 13:02:01 -0700170GrShape::GrShape(const GrShape& parent, GrStyle::Apply apply, SkScalar scale) {
171 // TODO: Add some quantization of scale for better cache performance here or leave that up
172 // to caller?
173 // TODO: For certain shapes and stroke params we could ignore the scale. (e.g. miter or bevel
174 // stroke of a rect).
bsalomonfb083272016-05-04 08:27:41 -0700175 if (!parent.style().applies() ||
176 (GrStyle::Apply::kPathEffectOnly == apply && !parent.style().pathEffect())) {
177 fType = Type::kEmpty;
178 *this = parent;
179 return;
180 }
181
bsalomon47cc7692016-04-26 12:56:00 -0700182 SkPathEffect* pe = parent.fStyle.pathEffect();
bsalomonfb083272016-05-04 08:27:41 -0700183 SkTLazy<SkPath> tmpPath;
184 const GrShape* parentForKey = &parent;
185 SkTLazy<GrShape> tmpParent;
186 fType = Type::kPath;
187 fPath.init();
bsalomon47cc7692016-04-26 12:56:00 -0700188 if (pe) {
bsalomonfb083272016-05-04 08:27:41 -0700189 SkPath* srcForPathEffect;
bsalomon47cc7692016-04-26 12:56:00 -0700190 if (parent.fType == Type::kPath) {
bsalomonfb083272016-05-04 08:27:41 -0700191 srcForPathEffect = parent.fPath.get();
bsalomon47cc7692016-04-26 12:56:00 -0700192 } else {
bsalomonfb083272016-05-04 08:27:41 -0700193 srcForPathEffect = tmpPath.init();
194 parent.asPath(tmpPath.get());
bsalomon47cc7692016-04-26 12:56:00 -0700195 }
196 // Should we consider bounds? Would have to include in key, but it'd be nice to know
197 // if the bounds actually modified anything before including in key.
bsalomonfb083272016-05-04 08:27:41 -0700198 SkStrokeRec strokeRec = parent.fStyle.strokeRec();
bsalomon97fd2d42016-05-09 13:02:01 -0700199 strokeRec.setResScale(scale);
bsalomonfb083272016-05-04 08:27:41 -0700200 if (!pe->filterPath(fPath.get(), *srcForPathEffect, &strokeRec, nullptr)) {
bsalomon47cc7692016-04-26 12:56:00 -0700201 // Make an empty unstyled shape if filtering fails.
202 fType = Type::kEmpty;
203 fStyle = GrStyle();
204 fPath.reset();
205 return;
206 }
bsalomon97fd2d42016-05-09 13:02:01 -0700207 // A path effect has access to change the res scale but we aren't expecting it to and it
208 // would mess up our key computation.
209 SkASSERT(scale == strokeRec.getResScale());
bsalomonfb083272016-05-04 08:27:41 -0700210 if (GrStyle::Apply::kPathEffectAndStrokeRec == apply) {
211 if (strokeRec.needToApply()) {
212 // The intermediate shape may not be a general path. If we we're just applying
213 // the path effect then attemptToReduceFromPath would catch it. This means that
214 // when we subsequently applied the remaining strokeRec we would have a non-path
215 // parent shape that would be used to determine the the stroked path's key.
216 // We detect that case here and change parentForKey to a temporary that represents
217 // the simpler shape so that applying both path effect and the strokerec all at
218 // once produces the same key.
219 SkRRect rrect;
220 Type parentType = AttemptToReduceFromPathImpl(*fPath.get(), &rrect, nullptr,
221 strokeRec);
222 switch (parentType) {
223 case Type::kEmpty:
224 tmpParent.init();
225 parentForKey = tmpParent.get();
226 break;
227 case Type::kRRect:
228 tmpParent.init(rrect, GrStyle(strokeRec, nullptr));
229 parentForKey = tmpParent.get();
230 case Type::kPath:
231 break;
232 }
233 SkAssertResult(strokeRec.applyToPath(fPath.get(), *fPath.get()));
234 } else {
235 fStyle = GrStyle(strokeRec, nullptr);
bsalomon72dc51c2016-04-27 06:46:23 -0700236 }
bsalomonfb083272016-05-04 08:27:41 -0700237 } else {
238 fStyle = GrStyle(strokeRec, nullptr);
bsalomon72dc51c2016-04-27 06:46:23 -0700239 }
bsalomon47cc7692016-04-26 12:56:00 -0700240 } else {
bsalomon97fd2d42016-05-09 13:02:01 -0700241 const SkPath* srcForParentStyle;
bsalomonfb083272016-05-04 08:27:41 -0700242 if (parent.fType == Type::kPath) {
bsalomon97fd2d42016-05-09 13:02:01 -0700243 srcForParentStyle = parent.fPath.get();
bsalomonfb083272016-05-04 08:27:41 -0700244 } else {
bsalomon97fd2d42016-05-09 13:02:01 -0700245 srcForParentStyle = tmpPath.init();
bsalomonfb083272016-05-04 08:27:41 -0700246 parent.asPath(tmpPath.get());
247 }
bsalomon97fd2d42016-05-09 13:02:01 -0700248 SkStrokeRec::InitStyle fillOrHairline;
249 SkASSERT(parent.fStyle.applies());
250 SkASSERT(!parent.fStyle.pathEffect());
251 SkAssertResult(parent.fStyle.applyToPath(fPath.get(), &fillOrHairline, *srcForParentStyle,
252 scale));
253 fStyle.resetToInitStyle(fillOrHairline);
bsalomon47cc7692016-04-26 12:56:00 -0700254 }
bsalomon72dc51c2016-04-27 06:46:23 -0700255 this->attemptToReduceFromPath();
bsalomon97fd2d42016-05-09 13:02:01 -0700256 this->setInheritedKey(*parentForKey, apply, scale);
bsalomon47cc7692016-04-26 12:56:00 -0700257}