| 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 |
#ifndef skgpu_graphite_geom_Shape_DEFINED |
| 9 |
#define skgpu_graphite_geom_Shape_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkArc.h" |
| 12 |
#include "include/core/SkM44.h" |
| 13 |
#include "include/core/SkPath.h" |
| 14 |
#include "include/core/SkPathTypes.h" |
| 15 |
#include "include/core/SkPoint.h" |
| 16 |
#include "include/core/SkRRect.h" |
| 17 |
#include "include/private/base/SkAssert.h" |
| 18 |
#include "src/base/SkVx.h" |
| 19 |
#include "src/gpu/graphite/geom/Rect.h" |
| 20 |
|
| 21 |
#include <cstdint> |
| 22 |
#include <new> |
| 23 |
|
| 24 |
struct SkRect; |
| 25 |
|
| 26 |
namespace skgpu::graphite { |
| 27 |
|
| 28 |
/** |
| 29 |
* Shape is effectively a std::variant over different geometric shapes, with the most complex |
| 30 |
* being an SkPath. It provides a consistent way to query geometric properties, such as convexity, |
| 31 |
* point containment, or iteration. |
| 32 |
*/ |
| 33 |
class Shape { |
| 34 |
public: |
| 35 |
enum class Type : uint8_t { |
| 36 |
kEmpty, kLine, kRect, kRRect, kArc, kPath |
| 37 |
}; |
| 38 |
inline static constexpr int kTypeCount = static_cast<int>(Type::kPath) + 1; |
| 39 |
|
| 40 |
// The default tolerance to use for fuzzy geometric comparisons that are already transformed |
| 41 |
// into device-space. Distances, containment checks, or equality tests closer than |
| 42 |
// kDefaultPixelTolerance (< ~0.004) can be considered perceptibly equivalent. This can be |
| 43 |
// tested in a different coordinate space by scaling this constant with Transform::localAARadius |
| 44 |
static constexpr float kDefaultPixelTolerance = 0.0039f; // (1.f - 0.001f) / 255.f; |
| 45 |
|
| 46 |
Shape() {} |
| 47 |
Shape(const Shape& shape) { *this = shape; } |
| 48 |
Shape(Shape&&) = delete; |
| 49 |
|
| 50 |
Shape(SkPoint p0, SkPoint p1) { this->setLine(p0, p1); } |
| 51 |
Shape(SkV2 p0, SkV2 p1) { this->setLine(p0, p1); } |
| 52 |
Shape(skvx::float2 p0, skvx::float2 p1) { this->setLine(p0, p1); } |
| 53 |
explicit Shape(const Rect& rect) { this->setRect(rect); } |
| 54 |
explicit Shape(const SkRect& rect) { this->setRect(rect); } |
| 55 |
explicit Shape(const SkRRect& rrect) { this->setRRect(rrect); } |
| 56 |
explicit Shape(const SkArc& arc) { this->setArc(arc); } |
| 57 |
explicit Shape(const SkPath& path) { this->setPath(path); } |
| 58 |
|
| 59 |
~Shape() { this->reset(); } |
| 60 |
|
| 61 |
// NOTE: None of the geometry types benefit from move semantics, so we don't bother |
| 62 |
// defining a move assignment operator for Shape. |
| 63 |
Shape& operator=(Shape&&) = delete; |
| 64 |
Shape& operator=(const Shape&); |
| 65 |
|
| 66 |
// Return the type of the data last stored in the Shape, which does not incorporate any possible |
| 67 |
// simplifications that could be applied to it (e.g. a degenerate round rect with 0 radius |
| 68 |
// corners is kRRect and not kRect). |
| 69 |
Type type() const { return fType; } |
| 70 |
|
| 71 |
bool isEmpty() const { return fType == Type::kEmpty; } |
| 72 |
bool isLine() const { return fType == Type::kLine; } |
| 73 |
bool isRect() const { return fType == Type::kRect; } |
| 74 |
bool isRRect() const { return fType == Type::kRRect; } |
| 75 |
bool isArc() const { return fType == Type::kArc; } |
| 76 |
bool isPath() const { return fType == Type::kPath; } |
| 77 |
|
| 78 |
bool isFloodFill() const { return this->isEmpty() && this->inverted(); } |
| 79 |
|
| 80 |
bool isVolatilePath() const { |
| 81 |
return fType == Type::kPath && this->path().isVolatile(); |
| 82 |
} |
| 83 |
|
| 84 |
bool inverted() const { |
| 85 |
SkASSERT(fType != Type::kPath || fInverted == fPath.isInverseFillType()); |
| 86 |
return fInverted; |
| 87 |
} |
| 88 |
|
| 89 |
void setInverted(bool inverted) { |
| 90 |
if (fType == Type::kPath && inverted != fPath.isInverseFillType()) { |
| 91 |
fPath.toggleInverseFillType(); |
| 92 |
} |
| 93 |
fInverted = inverted; |
| 94 |
} |
| 95 |
|
| 96 |
SkPathFillType fillType() const { |
| 97 |
if (fType == Type::kPath) { |
| 98 |
return fPath.getFillType(); // already incorporates invertedness |
| 99 |
} else { |
| 100 |
return fInverted ? SkPathFillType::kInverseEvenOdd : SkPathFillType::kEvenOdd; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// True if the given bounding box is completely inside the shape, if it's conservatively treated |
| 105 |
// as a filled, closed shape. |
| 106 |
bool conservativeContains(const Rect& rect) const; |
| 107 |
bool conservativeContains(skvx::float2 point) const; |
| 108 |
|
| 109 |
// True if the underlying shape is known to be convex, assuming no other styles. If 'simpleFill' |
| 110 |
// is true, it is assumed the contours will be implicitly closed when drawn or used. |
| 111 |
bool convex(bool simpleFill = true) const; |
| 112 |
|
| 113 |
// The bounding box of the shape. |
| 114 |
Rect bounds() const; |
| 115 |
|
| 116 |
// Convert the shape into a path that describes the same geometry. |
| 117 |
SkPath asPath() const; |
| 118 |
|
| 119 |
// Access the actual geometric description of the shape. May only access the appropriate type |
| 120 |
// based on what was last set. |
| 121 |
skvx::float2 p0() const { SkASSERT(this->isLine()); return fRect.topLeft(); } |
| 122 |
skvx::float2 p1() const { SkASSERT(this->isLine()); return fRect.botRight(); } |
| 123 |
skvx::float4 line() const { SkASSERT(this->isLine()); return fRect.ltrb(); } |
| 124 |
const Rect& rect() const { SkASSERT(this->isRect()); return fRect; } |
| 125 |
const SkRRect& rrect() const { SkASSERT(this->isRRect()); return fRRect; } |
| 126 |
const SkArc& arc() const { SkASSERT(this->isArc()); return fArc; } |
| 127 |
const SkPath& path() const { SkASSERT(this->isPath()); return fPath; } |
| 128 |
|
| 129 |
// Non-const access to the more complex types |
| 130 |
Rect& rect() { SkASSERT(this->isRect()); return fRect; } |
| 131 |
SkRRect& rrect() { SkASSERT(this->isRRect()); return fRRect; } |
| 132 |
SkArc& arc() { SkASSERT(this->isArc()); return fArc; } |
| 133 |
SkPath& path() { SkASSERT(this->isPath()); return fPath; } |
| 134 |
|
| 135 |
// Update the geometry stored in the Shape and update its associated type to match. This |
| 136 |
// performs no simplification, so calling setRRect() with a round rect that has isRect() return |
| 137 |
// true will still be considered an rrect by Shape. |
| 138 |
// |
| 139 |
// These reset inversion to the default for the geometric type. |
| 140 |
void setLine(SkPoint p0, SkPoint p1) { |
| 141 |
this->setLine(skvx::float2{p0.fX, p0.fY}, skvx::float2{p1.fX, p1.fY}); |
| 142 |
} |
| 143 |
void setLine(SkV2 p0, SkV2 p1) { |
| 144 |
this->setLine(skvx::float2{p0.x, p0.y}, skvx::float2{p1.x, p1.y}); |
| 145 |
} |
| 146 |
void setLine(skvx::float2 p0, skvx::float2 p1) { |
| 147 |
this->setType(Type::kLine); |
| 148 |
fRect = Rect(p0, p1); |
| 149 |
fInverted = false; |
| 150 |
} |
| 151 |
void setRect(const SkRect& rect) { this->setRect(Rect(rect)); } |
| 152 |
void setRect(const Rect& rect) { |
| 153 |
this->setType(Type::kRect); |
| 154 |
fRect = rect; |
| 155 |
fInverted = false; |
| 156 |
} |
| 157 |
void setRRect(const SkRRect& rrect) { |
| 158 |
this->setType(Type::kRRect); |
| 159 |
fRRect = rrect; |
| 160 |
fInverted = false; |
| 161 |
} |
| 162 |
void setArc(const SkArc& arc) { |
| 163 |
this->setType(Type::kArc); |
| 164 |
fArc = arc; |
| 165 |
fInverted = false; |
| 166 |
} |
| 167 |
void setPath(const SkPath& path) { |
| 168 |
if (fType == Type::kPath) { |
| 169 |
// Assign directly |
| 170 |
fPath = path; |
| 171 |
} else { |
| 172 |
// In-place initialize |
| 173 |
this->setType(Type::kPath); |
| 174 |
new (&fPath) SkPath(path); |
| 175 |
} |
| 176 |
fInverted = path.isInverseFillType(); |
| 177 |
} |
| 178 |
|
| 179 |
void reset() { |
| 180 |
this->setType(Type::kEmpty); |
| 181 |
fInverted = false; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Gets the size of the key for the shape represented by this Shape. |
| 186 |
*/ |
| 187 |
uint16_t keySize() const; |
| 188 |
|
| 189 |
/** |
| 190 |
* Writes keySize() bytes into the provided pointer. Assumes that there is enough |
| 191 |
* space allocated for the key. If includeInverted is false, non-inverted state will |
| 192 |
* be written into the key regardless of the Shape's state. |
| 193 |
*/ |
| 194 |
void writeKey(uint32_t* key, bool includeInverted) const; |
| 195 |
|
| 196 |
private: |
| 197 |
void setType(Type type) { |
| 198 |
if (this->isPath() && type != Type::kPath) { |
| 199 |
fPath.~SkPath(); |
| 200 |
} |
| 201 |
fType = type; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Key for the state data in the shape. This includes path fill type, |
| 206 |
* and any tracked inversion, as well as the class of geometry. |
| 207 |
* If includeInverted is false, non-inverted state will be written into |
| 208 |
* the key regardless of the Shape's state. |
| 209 |
*/ |
| 210 |
uint32_t stateKey(bool includeInverted) const; |
| 211 |
|
| 212 |
union { |
| 213 |
Rect fRect; // p0 = top-left, p1 = bot-right if type is kLine (may be unsorted) |
| 214 |
SkRRect fRRect; |
| 215 |
SkArc fArc; |
| 216 |
SkPath fPath; |
| 217 |
}; |
| 218 |
|
| 219 |
Type fType = Type::kEmpty; |
| 220 |
bool fInverted = false; |
| 221 |
}; |
| 222 |
|
| 223 |
} // namespace skgpu::graphite |
| 224 |
|
| 225 |
#endif // skgpu_graphite_geom_Shape_DEFINED |