1 /*
2 * Copyright 2006 The Android Open Source Project
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 "SkRect.h"
9
10 #include "SkMalloc.h"
11
join(int32_t left,int32_t top,int32_t right,int32_t bottom)12 void SkIRect::join(int32_t left, int32_t top, int32_t right, int32_t bottom) {
13 // do nothing if the params are empty
14 if (left >= right || top >= bottom) {
15 return;
16 }
17
18 // if we are empty, just assign
19 if (fLeft >= fRight || fTop >= fBottom) {
20 this->set(left, top, right, bottom);
21 } else {
22 if (left < fLeft) fLeft = left;
23 if (top < fTop) fTop = top;
24 if (right > fRight) fRight = right;
25 if (bottom > fBottom) fBottom = bottom;
26 }
27 }
28
29 /////////////////////////////////////////////////////////////////////////////
30
toQuad(SkPoint quad[4]) const31 void SkRect::toQuad(SkPoint quad[4]) const {
32 SkASSERT(quad);
33
34 quad[0].set(fLeft, fTop);
35 quad[1].set(fRight, fTop);
36 quad[2].set(fRight, fBottom);
37 quad[3].set(fLeft, fBottom);
38 }
39
40 #include "SkNx.h"
41
is_finite(const Sk4s & value)42 static inline bool is_finite(const Sk4s& value) {
43 auto finite = value * Sk4s(0) == Sk4s(0);
44 return finite.allTrue();
45 }
46
setBoundsCheck(const SkPoint pts[],int count)47 bool SkRect::setBoundsCheck(const SkPoint pts[], int count) {
48 SkASSERT((pts && count > 0) || count == 0);
49
50 bool isFinite = true;
51
52 if (count <= 0) {
53 sk_bzero(this, sizeof(SkRect));
54 } else {
55 Sk4s min, max, accum;
56
57 if (count & 1) {
58 min = Sk4s(pts[0].fX, pts[0].fY, pts[0].fX, pts[0].fY);
59 pts += 1;
60 count -= 1;
61 } else {
62 min = Sk4s::Load(pts);
63 pts += 2;
64 count -= 2;
65 }
66 accum = max = min;
67 accum = accum * Sk4s(0);
68
69 count >>= 1;
70 for (int i = 0; i < count; ++i) {
71 Sk4s xy = Sk4s::Load(pts);
72 accum = accum * xy;
73 min = Sk4s::Min(min, xy);
74 max = Sk4s::Max(max, xy);
75 pts += 2;
76 }
77
78 /**
79 * With some trickery, we may be able to use Min/Max to also propogate non-finites,
80 * in which case we could eliminate accum entirely, and just check min and max for
81 * "is_finite".
82 */
83 if (is_finite(accum)) {
84 float minArray[4], maxArray[4];
85 min.store(minArray);
86 max.store(maxArray);
87 this->set(SkTMin(minArray[0], minArray[2]), SkTMin(minArray[1], minArray[3]),
88 SkTMax(maxArray[0], maxArray[2]), SkTMax(maxArray[1], maxArray[3]));
89 } else {
90 // we hit a non-finite value, so zero everything and return false
91 this->setEmpty();
92 isFinite = false;
93 }
94 }
95 return isFinite;
96 }
97
98 #define CHECK_INTERSECT(al, at, ar, ab, bl, bt, br, bb) \
99 SkScalar L = SkMaxScalar(al, bl); \
100 SkScalar R = SkMinScalar(ar, br); \
101 SkScalar T = SkMaxScalar(at, bt); \
102 SkScalar B = SkMinScalar(ab, bb); \
103 do { if (L >= R || T >= B) return false; } while (0)
104
intersect(SkScalar left,SkScalar top,SkScalar right,SkScalar bottom)105 bool SkRect::intersect(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
106 CHECK_INTERSECT(left, top, right, bottom, fLeft, fTop, fRight, fBottom);
107 this->setLTRB(L, T, R, B);
108 return true;
109 }
110
intersect(const SkRect & r)111 bool SkRect::intersect(const SkRect& r) {
112 return this->intersect(r.fLeft, r.fTop, r.fRight, r.fBottom);
113 }
114
intersect(const SkRect & a,const SkRect & b)115 bool SkRect::intersect(const SkRect& a, const SkRect& b) {
116 CHECK_INTERSECT(a.fLeft, a.fTop, a.fRight, a.fBottom, b.fLeft, b.fTop, b.fRight, b.fBottom);
117 this->setLTRB(L, T, R, B);
118 return true;
119 }
120
join(SkScalar left,SkScalar top,SkScalar right,SkScalar bottom)121 void SkRect::join(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
122 // do nothing if the params are empty
123 if (left >= right || top >= bottom) {
124 return;
125 }
126
127 // if we are empty, just assign
128 if (fLeft >= fRight || fTop >= fBottom) {
129 this->set(left, top, right, bottom);
130 } else {
131 fLeft = SkMinScalar(fLeft, left);
132 fTop = SkMinScalar(fTop, top);
133 fRight = SkMaxScalar(fRight, right);
134 fBottom = SkMaxScalar(fBottom, bottom);
135 }
136 }
137
138 ////////////////////////////////////////////////////////////////////////////////////////////////
139
140 #include "SkString.h"
141 #include "SkStringUtils.h"
142
set_scalar(SkString * storage,SkScalar value,SkScalarAsStringType asType)143 static const char* set_scalar(SkString* storage, SkScalar value, SkScalarAsStringType asType) {
144 storage->reset();
145 SkAppendScalar(storage, value, asType);
146 return storage->c_str();
147 }
148
dump(bool asHex) const149 void SkRect::dump(bool asHex) const {
150 SkScalarAsStringType asType = asHex ? kHex_SkScalarAsStringType : kDec_SkScalarAsStringType;
151
152 SkString line;
153 if (asHex) {
154 SkString tmp;
155 line.printf( "SkRect::MakeLTRB(%s, /* %f */\n", set_scalar(&tmp, fLeft, asType), fLeft);
156 line.appendf(" %s, /* %f */\n", set_scalar(&tmp, fTop, asType), fTop);
157 line.appendf(" %s, /* %f */\n", set_scalar(&tmp, fRight, asType), fRight);
158 line.appendf(" %s /* %f */);", set_scalar(&tmp, fBottom, asType), fBottom);
159 } else {
160 SkString strL, strT, strR, strB;
161 SkAppendScalarDec(&strL, fLeft);
162 SkAppendScalarDec(&strT, fTop);
163 SkAppendScalarDec(&strR, fRight);
164 SkAppendScalarDec(&strB, fBottom);
165 line.printf("SkRect::MakeLTRB(%s, %s, %s, %s);",
166 strL.c_str(), strT.c_str(), strR.c_str(), strB.c_str());
167 }
168 SkDebugf("%s\n", line.c_str());
169 }
170