Debian Codegraph @

src/gpu/graphite/geom/Shape.cpp

raw
1 /*
2 * Copyright 2021 Google LLC
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 "src/gpu/graphite/geom/Shape.h"
9
10 #include "include/core/SkPathBuilder.h"
11 #include "include/core/SkRect.h"
12 #include "include/core/SkScalar.h"
13 #include "include/private/base/SkAlign.h"
14 #include "include/private/base/SkDebug.h"
15 #include "include/private/base/SkMalloc.h"
16 #include "src/core/SkPathPriv.h"
17 #include "src/core/SkRRectPriv.h"
18
19 #include <cstring>
20
21 namespace {
22 // Keys for paths may be extracted from the path data for small paths, to maximize matches
23 // even when the genIDs may differ. The value is based on emperical experience, to trade off
24 // matches vs. key size.
25 constexpr int kMaxKeyFromDataVerbCnt = 10;
26 }
27
28 namespace skgpu::graphite {
29
30 Shape& Shape::operator=(const Shape& shape) {
31 switch (shape.type()) {
32 case Type::kEmpty: this->reset(); break;
33 case Type::kLine: this->setLine(shape.p0(), shape.p1()); break;
34 case Type::kRect: this->setRect(shape.rect()); break;
35 case Type::kRRect: this->setRRect(shape.rrect()); break;
36 case Type::kArc: this->setArc(shape.arc()); break;
37 case Type::kPath: this->setPath(shape.path()); break;
38 }
39
40 fInverted = shape.fInverted;
41 return *this;
42 }
43
44 bool Shape::conservativeContains(const Rect& rect) const {
45 switch (fType) {
46 case Type::kEmpty: return false;
47 case Type::kLine: return false;
48 case Type::kRect: return fRect.contains(rect);
49 case Type::kRRect: return fRRect.contains(rect.asSkRect());
50 case Type::kPath: // We need to ensure the path is non-inverted.
51 if (this->inverted()) {
52 SkPath nonInverted(fPath);
53 nonInverted.toggleInverseFillType();
54 return nonInverted.conservativelyContainsRect(rect.asSkRect());
55 } else {
56 return fPath.conservativelyContainsRect(rect.asSkRect());
57 }
58 case Type::kArc: if (fArc.fType == SkArc::Type::kWedge) {
59 SkPath arc = this->asPath();
60 if (this->inverted()) {
61 arc.toggleInverseFillType();
62 }
63 return arc.conservativelyContainsRect(rect.asSkRect());
64 } else {
65 return false;
66 }
67 }
68 SkUNREACHABLE;
69 }
70
71 bool Shape::conservativeContains(skvx::float2 point) const {
72 switch (fType) {
73 case Type::kEmpty: return false;
74 case Type::kLine: return false;
75 case Type::kRect: return fRect.contains(Rect::Point(point));
76 case Type::kRRect: return SkRRectPriv::ContainsPoint(fRRect, {point.x(), point.y()});
77 case Type::kPath: // We need to ensure the path is non-inverted.
78 if (this->inverted()) {
79 SkPath nonInverted(fPath);
80 nonInverted.toggleInverseFillType();
81 return nonInverted.contains(point.x(), point.y());
82 } else {
83 return fPath.contains(point.x(), point.y());
84 }
85 case Type::kArc: return false;
86 }
87 SkUNREACHABLE;
88 }
89
90 bool Shape::convex(bool simpleFill) const {
91 if (this->isPath()) {
92 // SkPath.isConvex() really means "is this path convex were it to be closed".
93 return (simpleFill || fPath.isLastContourClosed()) && fPath.isConvex();
94 } else if (this->isArc()) {
95 return SkPathPriv::DrawArcIsConvex(fArc.sweepAngle(), fArc.fType, simpleFill);
96 } else {
97 // Every other shape type is convex by construction.
98 return true;
99 }
100 }
101
102 Rect Shape::bounds() const {
103 switch (fType) {
104 case Type::kEmpty: return Rect(0, 0, 0, 0);
105 case Type::kLine: return fRect.makeSorted(); // sorting corners computes bbox of segment
106 case Type::kRect: return fRect; // assuming it's sorted
107 case Type::kRRect: return fRRect.getBounds();
108 case Type::kArc: return fArc.oval();
109 case Type::kPath: return fPath.getBounds();
110 }
111 SkUNREACHABLE;
112 }
113
114 SkPath Shape::asPath() const {
115 if (fType == Type::kPath) {
116 return fPath;
117 }
118
119 if (fType == Type::kArc) {
120 // Filled ovals are already culled out so we assume no simple fills
121 SkPath out = SkPathPriv::CreateDrawArcPath(fArc, /*isFillNoPathEffect=*/false);
122 // CreateDrawArcPath resets the output path and configures its fill
123 // type, so we just have to ensure invertedness is correct.
124 if (fInverted) {
125 out.toggleInverseFillType();
126 }
127 return out;
128 }
129
130 SkPathBuilder builder(this->fillType());
131 switch (fType) {
132 case Type::kEmpty: /* do nothing */ break;
133 case Type::kLine: builder.moveTo(fRect.left(), fRect.top())
134 .lineTo(fRect.right(), fRect.bot()); break;
135 case Type::kRect: builder.addRect(fRect.asSkRect()); break;
136 case Type::kRRect: builder.addRRect(fRRect); break;
137 case Type::kPath:
138 case Type::kArc: SkUNREACHABLE;
139 }
140 return builder.detach();
141 }
142
143 namespace {
144 int path_key_from_data_size(const SkPath& path) {
145 const int verbCnt = path.countVerbs();
146 if (verbCnt > kMaxKeyFromDataVerbCnt) {
147 return -1;
148 }
149 const size_t pointCnt = path.points().size();
150 const size_t conicWeightCnt = path.conicWeights().size();
151
152 static_assert(sizeof(SkPoint) == 2 * sizeof(uint32_t));
153 static_assert(sizeof(SkScalar) == sizeof(uint32_t));
154 // 1 is for the verb count. Each verb is a byte but we'll pad the verb data out to
155 // a uint32_t length.
156 return 1 + (SkAlign4(verbCnt) >> 2) + 2 * pointCnt + conicWeightCnt;
157 }
158
159 // Writes the path data key into the passed pointer.
160 void write_path_key_from_data(const SkPath& path, uint32_t* origKey) {
161 uint32_t* key = origKey;
162 // The check below should take care of negative values casted positive.
163 SkSpan<const SkPathVerb> verbs = path.verbs();
164 SkSpan<const SkPoint> points = path.points();
165 SkSpan<const float> conics = path.conicWeights();
166 SkASSERT(verbs.size() <= kMaxKeyFromDataVerbCnt);
167 SkASSERT(points.size() && verbs.size());
168 *key++ = SkToInt(verbs.size());
169 memcpy(key, verbs.data(), verbs.size_bytes());
170 const size_t verbKeySize = SkAlign4(verbs.size());
171 // pad out to uint32_t alignment using value that will stand out when debugging.
172 uint8_t* pad = reinterpret_cast<uint8_t*>(key)+ verbs.size();
173 memset(pad, 0xDE, verbKeySize - verbs.size());
174 key += verbKeySize >> 2;
175
176 memcpy(key, points.data(), points.size_bytes());
177 static_assert(sizeof(SkPoint) == 2 * sizeof(uint32_t));
178 key += 2 * points.size();
179 sk_careful_memcpy(key, conics.data(), conics.size_bytes());
180 static_assert(sizeof(SkScalar) == sizeof(uint32_t));
181 SkDEBUGCODE(key += conics.size());
182 SkASSERT(key - origKey == path_key_from_data_size(path));
183 }
184 } // anonymous namespace
185
186 uint16_t Shape::keySize() const {
187 uint16_t count = 1; // Every key has the state flags from the Shape
188 switch(this->type()) {
189 case Type::kLine:
190 static_assert(0 == sizeof(skvx::float4) % sizeof(uint32_t));
191 count += sizeof(skvx::float4) / sizeof(uint32_t);
192 break;
193 case Type::kRect:
194 static_assert(0 == sizeof(Rect) % sizeof(uint32_t));
195 count += sizeof(Rect) / sizeof(uint32_t);
196 break;
197 case Type::kRRect:
198 static_assert(0 == SkRRect::kSizeInMemory % sizeof(uint32_t));
199 count += SkRRect::kSizeInMemory / sizeof(uint32_t);
200 break;
201 case Type::kArc:
202 static_assert(0 == sizeof(SkArc) % sizeof(uint32_t));
203 count += sizeof(SkArc) / sizeof(uint32_t);
204 break;
205 case Type::kPath: {
206 // An empty path is the same as an empty shape -- only needs the state flags
207 if (!this->path().isEmpty()) {
208 int dataKeySize = path_key_from_data_size(this->path());
209 if (dataKeySize >= 0) {
210 count += SkTo<uint16_t>(dataKeySize);
211 } else {
212 count++; // Just adds the gen ID.
213 }
214 }
215 break;
216 }
217 default:
218 // else it's empty, which just needs the state flags for its key
219 SkASSERT(this->isEmpty());
220 }
221 return count;
222 }
223
224 void Shape::writeKey(uint32_t* key, bool includeInverted) const {
225 SkASSERT(this->keySize());
226 SkDEBUGCODE(uint32_t* origKey = key;)
227
228 // Every key starts with the state from the Shape (this includes path fill type,
229 // and any tracked inversion, as well as the class of geometry).
230 *key++ = this->stateKey(includeInverted);
231
232 switch(this->type()) {
233 case Type::kPath: {
234 // An empty path is the same as an empty shape -- only needs the state flags
235 if (!this->path().isEmpty()) {
236 // The path's inversion must match our state in order for the path's key to suffice.
237 SkASSERT(this->inverted() == this->path().isInverseFillType());
238
239 int dataKeySize = path_key_from_data_size(this->path());
240 if (dataKeySize >= 0) {
241 // We check the rest of the size in write_path_key_from_data
242 SkASSERT(key - origKey == 1);
243 write_path_key_from_data(this->path(), key);
244 return;
245 } else {
246 *key++ = this->path().getGenerationID();
247 }
248 }
249 break;
250 }
251 case Type::kRect:
252 memcpy(key, &this->rect(), sizeof(Rect));
253 key += sizeof(Rect) / sizeof(uint32_t);
254 break;
255 case Type::kRRect:
256 this->rrect().writeToMemory(key);
257 key += SkRRect::kSizeInMemory / sizeof(uint32_t);
258 break;
259 case Type::kArc: {
260 // Write dense floats first
261 memcpy(key, &fArc, sizeof(SkRect) + 2 * sizeof(float));
262 key += (sizeof(SkArc) / sizeof(uint32_t) - 1);
263 // Then write the final bool as an int, to make sure upper bits are set
264 *key++ = fArc.isWedge() ? 1 : 0;
265 break;
266 }
267 case Type::kLine: {
268 skvx::float4 line = this->line();
269 memcpy(key, &line, sizeof(skvx::float4));
270 key += sizeof(skvx::float4) / sizeof(uint32_t);
271 break;
272 }
273 default:
274 // Nothing other than the flag state is needed in the key for an empty shape
275 SkASSERT(this->isEmpty());
276 }
277 SkASSERT(key - origKey == this->keySize());
278 }
279
280 namespace {
281 SkPathFillType noninverted_fill_type(SkPathFillType fillType) {
282 switch (fillType) {
283 case SkPathFillType::kWinding:
284 case SkPathFillType::kInverseWinding:
285 return SkPathFillType::kWinding;
286 case SkPathFillType::kEvenOdd:
287 case SkPathFillType::kInverseEvenOdd:
288 return SkPathFillType::kEvenOdd;
289 }
290 SkUNREACHABLE;
291 }
292 } // anonymous namespace
293
294 uint32_t Shape::stateKey(bool includeInverted) const {
295 uint32_t key;
296 if (includeInverted) {
297 // Use the path's full fill type instead of just whether or not it's inverted.
298 key = this->isPath() ? static_cast<uint32_t>(fPath.getFillType())
299 : (fInverted ? 1 : 0);
300 } else {
301 // Use the path's noninverted fill type.
302 key = this->isPath() ? static_cast<uint32_t>(noninverted_fill_type(fPath.getFillType()))
303 : 0;
304 }
305 key |= ((uint32_t) fType) << 2; // fill type was 2 bits
306 return key;
307 }
308
309 } // namespace skgpu::graphite