| 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_Rect_DEFINED |
| 9 |
#define skgpu_graphite_geom_Rect_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkRect.h" |
| 12 |
#include "include/core/SkScalar.h" |
| 13 |
#include "include/private/base/SkAttributes.h" |
| 14 |
#include "include/private/base/SkFloatingPoint.h" |
| 15 |
#include "src/base/SkUtils.h" |
| 16 |
#include "src/base/SkVx.h" |
| 17 |
|
| 18 |
namespace skgpu::graphite { |
| 19 |
|
| 20 |
#define AI SK_ALWAYS_INLINE |
| 21 |
|
| 22 |
/** |
| 23 |
* SIMD rect implementation. Values are stored internally in the form: [left, top, -right, -bot]. |
| 24 |
* |
| 25 |
* Some operations (e.g., intersect, inset) may return a negative or empty rect |
| 26 |
* (negative meaning, left >= right or top >= bot). |
| 27 |
* |
| 28 |
* Operations on a rect that is either negative or empty, while well-defined, might not give the |
| 29 |
* intended result. It is the caller's responsibility to check isEmptyOrNegative() if needed. |
| 30 |
*/ |
| 31 |
class Rect { |
| 32 |
using float2 = skvx::float2; |
| 33 |
using float4 = skvx::float4; |
| 34 |
public: |
| 35 |
AI Rect() = default; |
| 36 |
AI Rect(float l, float t, float r, float b) : fVals(NegateBotRight({l,t,r,b})) {} |
| 37 |
AI Rect(float2 topLeft, float2 botRight) : fVals(topLeft, -botRight) {} |
| 38 |
AI Rect(const SkRect& r) : fVals(NegateBotRight(float4::Load(r.asScalars()))) {} |
| 39 |
AI Rect(const SkIRect& r) |
| 40 |
: fVals(NegateBotRight(skvx::cast<float>(skvx::int4::Load(r.asInt32s())))) {} |
| 41 |
|
| 42 |
AI static Rect LTRB(float4 ltrb) { |
| 43 |
return Rect(NegateBotRight(ltrb)); |
| 44 |
} |
| 45 |
|
| 46 |
AI static Rect XYWH(float x, float y, float w, float h) { |
| 47 |
return Rect(x, y, x + w, y + h); |
| 48 |
} |
| 49 |
AI static Rect XYWH(float2 topLeft, float2 size) { |
| 50 |
return Rect(topLeft, topLeft + size); |
| 51 |
} |
| 52 |
AI static Rect WH(float w, float h) { |
| 53 |
return Rect(0, 0, w, h); |
| 54 |
} |
| 55 |
AI static Rect WH(float2 size) { |
| 56 |
return Rect(float2(0), size); |
| 57 |
} |
| 58 |
AI static Rect Point(float2 p) { |
| 59 |
return Rect(p, p); |
| 60 |
} |
| 61 |
AI static Rect FromVals(float4 vals) { // vals.zw must already be negated. |
| 62 |
return Rect(vals); |
| 63 |
} |
| 64 |
|
| 65 |
// Constructs a Rect with ltrb = [-inf, -inf, inf, inf], useful for accumulating intersections |
| 66 |
AI static Rect Infinite() { |
| 67 |
return FromVals(float4(SK_FloatNegativeInfinity)); |
| 68 |
} |
| 69 |
// Constructs a negative Rect with ltrb = [inf, inf, -inf, -inf], useful for accumulating unions |
| 70 |
AI static Rect InfiniteInverted() { |
| 71 |
return FromVals(float4(SK_FloatInfinity)); |
| 72 |
} |
| 73 |
|
| 74 |
AI bool operator==(Rect rect) const { return all(fVals == rect.fVals); } |
| 75 |
AI bool operator!=(Rect rect) const { return any(fVals != rect.fVals); } |
| 76 |
|
| 77 |
AI bool nearlyEquals(const Rect& r, float epsilon = SK_ScalarNearlyZero) const { |
| 78 |
return all(abs(fVals - r.fVals) <= epsilon); |
| 79 |
} |
| 80 |
|
| 81 |
AI const float4& vals() const { return fVals; } // [left, top, -right, -bot]. |
| 82 |
AI float4& vals() { return fVals; } // [left, top, -right, -bot]. |
| 83 |
|
| 84 |
AI float x() const { return fVals.x(); } |
| 85 |
AI float y() const { return fVals.y(); } |
| 86 |
AI float left() const { return fVals.x(); } |
| 87 |
AI float top() const { return fVals.y(); } |
| 88 |
AI float right() const { return -fVals.z(); } |
| 89 |
AI float bot() const { return -fVals.w(); } |
| 90 |
AI float2 topLeft() const { return fVals.xy(); } |
| 91 |
AI float2 botRight() const { return -fVals.zw(); } |
| 92 |
AI float4 ltrb() const { return NegateBotRight(fVals); } |
| 93 |
|
| 94 |
AI void setLeft(float left) { fVals.x() = left; } |
| 95 |
AI void setTop(float top) { fVals.y() = top; } |
| 96 |
AI void setRight(float right) { fVals.z() = -right; } |
| 97 |
AI void setBot(float bot) { fVals.w() = -bot; } |
| 98 |
AI void setTopLeft(float2 topLeft) { fVals.xy() = topLeft; } |
| 99 |
AI void setBotRight(float2 botRight) { fVals.zw() = -botRight; } |
| 100 |
|
| 101 |
AI SkRect asSkRect() const { |
| 102 |
SkRect rect; |
| 103 |
this->ltrb().store(&rect); |
| 104 |
return rect; |
| 105 |
} |
| 106 |
AI SkIRect asSkIRect() const { |
| 107 |
SkIRect rect; |
| 108 |
skvx::cast<int>(this->ltrb()).store(&rect); |
| 109 |
return rect; |
| 110 |
} |
| 111 |
|
| 112 |
AI bool isEmptyNegativeOrNaN() const { |
| 113 |
return !all(fVals.xy() + fVals.zw() < 0); // !([l-r, r-b] < 0) == ([w, h] <= 0) |
| 114 |
// Use "!(-size < 0)" in order to detect NaN. |
| 115 |
} |
| 116 |
|
| 117 |
AI float2 size() const { return -(fVals.xy() + fVals.zw()); } // == [-(l-r), -(t-b)] == [w, h] |
| 118 |
|
| 119 |
AI float2 center() const { |
| 120 |
float4 p = fVals * float4(.5f, .5f, -.5f, -.5f); // == [l, t, r, b] * .5 |
| 121 |
return p.xy() + p.zw(); // == [(l + r)/2, (t + b)/2] |
| 122 |
} |
| 123 |
|
| 124 |
AI float area() const { |
| 125 |
float2 negativeSize = fVals.xy() + fVals.zw(); // == [l-r, t-b] == [-w, -h] |
| 126 |
return negativeSize.x() * negativeSize.y(); |
| 127 |
} |
| 128 |
|
| 129 |
// A rect stored in a complementary form of: [right, bottom, -left, -top]. Store a local |
| 130 |
// ComplementRect object if intersects() will be called many times. |
| 131 |
struct ComplementRect { |
| 132 |
AI ComplementRect(Rect rect) : fVals(-rect.fVals.zwxy()) {} |
| 133 |
float4 fVals; // [right, bottom, -left, -top] |
| 134 |
}; |
| 135 |
|
| 136 |
AI bool intersects(ComplementRect comp) const { return all(fVals < comp.fVals); } |
| 137 |
AI bool contains(Rect rect) const { return all(fVals <= rect.fVals); } |
| 138 |
|
| 139 |
// Some operations may return a negative or empty rect. Operations on a rect that either is |
| 140 |
// negative or empty, while well-defined, might not give the intended result. It is the caller's |
| 141 |
// responsibility to check isEmptyOrNegative() if needed. |
| 142 |
AI Rect makeRoundIn() const { return ceil(fVals); } |
| 143 |
AI Rect makeRoundOut() const { return floor(fVals); } |
| 144 |
AI Rect makeRound() const { |
| 145 |
// To match SkRect::round(), which is implemented as floor(x+.5), we don't use std::round. |
| 146 |
// But this means we have to undo the negative R and B components before flooring. |
| 147 |
return LTRB(floor(this->ltrb() + 0.5f)); |
| 148 |
} |
| 149 |
AI Rect makeInset(float inset) const { return fVals + inset; } |
| 150 |
AI Rect makeInset(float2 inset) const { return fVals + inset.xyxy(); } |
| 151 |
AI Rect makeOutset(float outset) const { return fVals - outset; } |
| 152 |
AI Rect makeOutset(float2 outset) const { return fVals - outset.xyxy(); } |
| 153 |
AI Rect makeOffset(float2 offset) const { return fVals + float4(offset, -offset); } |
| 154 |
AI Rect makeJoin(Rect rect) const { return min(fVals, rect.fVals); } |
| 155 |
AI Rect makeIntersect(Rect rect) const { return max(fVals, rect.fVals); } |
| 156 |
AI Rect makeSorted() const { return min(fVals, -fVals.zwxy()); } |
| 157 |
|
| 158 |
AI Rect& roundIn() { return *this = this->makeRoundIn(); } |
| 159 |
AI Rect& roundOut() { return *this = this->makeRoundOut(); } |
| 160 |
AI Rect& round() { return *this = this->makeRound(); } |
| 161 |
AI Rect& inset(float inset) { return *this = this->makeInset(inset); } |
| 162 |
AI Rect& inset(float2 inset) { return *this = this->makeInset(inset); } |
| 163 |
AI Rect& outset(float outset) { return *this = this->makeOutset(outset); } |
| 164 |
AI Rect& outset(float2 outset) { return *this = this->makeOutset(outset); } |
| 165 |
AI Rect& offset(float2 offset) { return *this = this->makeOffset(offset); } |
| 166 |
AI Rect& join(Rect rect) { return *this = this->makeJoin(rect); } |
| 167 |
AI Rect& intersect(Rect rect) { return *this = this->makeIntersect(rect); } |
| 168 |
AI Rect& sort() { return *this = this->makeSorted(); } |
| 169 |
|
| 170 |
private: |
| 171 |
AI static float4 NegateBotRight(float4 vals) { // Returns [vals.xy, -vals.zw]. |
| 172 |
using uint4 = skvx::uint4; |
| 173 |
return sk_bit_cast<float4>(sk_bit_cast<uint4>(vals) ^ uint4(0, 0, 1u << 31, 1u << 31)); |
| 174 |
} |
| 175 |
|
| 176 |
AI Rect(float4 vals) : fVals(vals) {} // vals.zw must already be negated. |
| 177 |
|
| 178 |
float4 fVals; // [left, top, -right, -bottom] |
| 179 |
}; |
| 180 |
|
| 181 |
#undef AI |
| 182 |
|
| 183 |
} // namespace skgpu::graphite |
| 184 |
|
| 185 |
#endif // skgpu_graphite_geom_Rect_DEFINED |