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