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