Flutter Engine
The Flutter Engine
SkRect.cpp
Go to the documentation of this file.
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
9
10#include "include/core/SkM44.h"
13#include "src/core/SkRectPriv.h"
14
15class SkMatrix;
16
17bool SkIRect::intersect(const SkIRect& a, const SkIRect& b) {
18 SkIRect tmp = {
19 std::max(a.fLeft, b.fLeft),
20 std::max(a.fTop, b.fTop),
21 std::min(a.fRight, b.fRight),
22 std::min(a.fBottom, b.fBottom)
23 };
24 if (tmp.isEmpty()) {
25 return false;
26 }
27 *this = tmp;
28 return true;
29}
30
31void SkIRect::join(const SkIRect& r) {
32 // do nothing if the params are empty
33 if (r.fLeft >= r.fRight || r.fTop >= r.fBottom) {
34 return;
35 }
36
37 // if we are empty, just assign
38 if (fLeft >= fRight || fTop >= fBottom) {
39 *this = r;
40 } else {
41 if (r.fLeft < fLeft) fLeft = r.fLeft;
42 if (r.fTop < fTop) fTop = r.fTop;
43 if (r.fRight > fRight) fRight = r.fRight;
44 if (r.fBottom > fBottom) fBottom = r.fBottom;
45 }
46}
47
48/////////////////////////////////////////////////////////////////////////////
49
50void SkRect::toQuad(SkPoint quad[4]) const {
51 SkASSERT(quad);
52
53 quad[0].set(fLeft, fTop);
54 quad[1].set(fRight, fTop);
55 quad[2].set(fRight, fBottom);
56 quad[3].set(fLeft, fBottom);
57}
58
59#include "src/base/SkVx.h"
60
61bool SkRect::setBoundsCheck(const SkPoint pts[], int count) {
62 SkASSERT((pts && count > 0) || count == 0);
63
64 if (count <= 0) {
65 this->setEmpty();
66 return true;
67 }
68
70 if (count & 1) {
71 min = max = skvx::float2::Load(pts).xyxy();
72 pts += 1;
73 count -= 1;
74 } else {
75 min = max = skvx::float4::Load(pts);
76 pts += 2;
77 count -= 2;
78 }
79
80 skvx::float4 accum = min * 0;
81 while (count) {
83 accum = accum * xy;
84 min = skvx::min(min, xy);
85 max = skvx::max(max, xy);
86 pts += 2;
87 count -= 2;
88 }
89
90 const bool all_finite = all(accum * 0 == 0);
91 if (all_finite) {
92 this->setLTRB(std::min(min[0], min[2]), std::min(min[1], min[3]),
93 std::max(max[0], max[2]), std::max(max[1], max[3]));
94 } else {
95 this->setEmpty();
96 }
97 return all_finite;
98}
99
100void SkRect::setBoundsNoCheck(const SkPoint pts[], int count) {
101 if (!this->setBoundsCheck(pts, count)) {
103 }
104}
105
106#define CHECK_INTERSECT(al, at, ar, ab, bl, bt, br, bb) \
107 float L = std::max(al, bl); \
108 float R = std::min(ar, br); \
109 float T = std::max(at, bt); \
110 float B = std::min(ab, bb); \
111 do { if (!(L < R && T < B)) return false; } while (0)
112 // do the !(opposite) check so we return false if either arg is NaN
113
114bool SkRect::intersect(const SkRect& r) {
116 this->setLTRB(L, T, R, B);
117 return true;
118}
119
120bool SkRect::intersect(const SkRect& a, const SkRect& b) {
121 CHECK_INTERSECT(a.fLeft, a.fTop, a.fRight, a.fBottom, b.fLeft, b.fTop, b.fRight, b.fBottom);
122 this->setLTRB(L, T, R, B);
123 return true;
124}
125
126void SkRect::join(const SkRect& r) {
127 if (r.isEmpty()) {
128 return;
129 }
130
131 if (this->isEmpty()) {
132 *this = r;
133 } else {
135 fTop = std::min(fTop, r.fTop);
138 }
139}
140
141////////////////////////////////////////////////////////////////////////////////////////////////
142
145
146static const char* set_scalar(SkString* storage, float value, SkScalarAsStringType asType) {
147 storage->reset();
148 SkAppendScalar(storage, value, asType);
149 return storage->c_str();
150}
151
152void SkRect::dump(bool asHex) const {
154
156 if (asHex) {
157 SkString tmp;
158 line.printf( "SkRect::MakeLTRB(%s, /* %f */\n", set_scalar(&tmp, fLeft, asType), fLeft);
159 line.appendf(" %s, /* %f */\n", set_scalar(&tmp, fTop, asType), fTop);
160 line.appendf(" %s, /* %f */\n", set_scalar(&tmp, fRight, asType), fRight);
161 line.appendf(" %s /* %f */);", set_scalar(&tmp, fBottom, asType), fBottom);
162 } else {
163 SkString strL, strT, strR, strB;
164 SkAppendScalarDec(&strL, fLeft);
165 SkAppendScalarDec(&strT, fTop);
168 line.printf("SkRect::MakeLTRB(%s, %s, %s, %s);",
169 strL.c_str(), strT.c_str(), strR.c_str(), strB.c_str());
170 }
171 SkDebugf("%s\n", line.c_str());
172}
173
174////////////////////////////////////////////////////////////////////////////////////////////////
175
176template<typename R>
177static bool subtract(const R& a, const R& b, R* out) {
178 if (a.isEmpty() || b.isEmpty() || !R::Intersects(a, b)) {
179 // Either already empty, or subtracting the empty rect, or there's no intersection, so
180 // in all cases the answer is A.
181 *out = a;
182 return true;
183 }
184
185 // 4 rectangles to consider. If the edge in A is contained in B, the resulting difference can
186 // be represented exactly as a rectangle. Otherwise the difference is the largest subrectangle
187 // that is disjoint from B:
188 // 1. Left part of A: (A.left, A.top, B.left, A.bottom)
189 // 2. Right part of A: (B.right, A.top, A.right, A.bottom)
190 // 3. Top part of A: (A.left, A.top, A.right, B.top)
191 // 4. Bottom part of A: (A.left, B.bottom, A.right, A.bottom)
192 //
193 // Depending on how B intersects A, there will be 1 to 4 positive areas:
194 // - 4 occur when A contains B
195 // - 3 occur when B intersects a single edge
196 // - 2 occur when B intersects at a corner, or spans two opposing edges
197 // - 1 occurs when B spans two opposing edges and contains a 3rd, resulting in an exact rect
198 // - 0 occurs when B contains A, resulting in the empty rect
199 //
200 // Compute the relative areas of the 4 rects described above. Since each subrectangle shares
201 // either the width or height of A, we only have to divide by the other dimension, which avoids
202 // overflow on int32 types, and even if the float relative areas overflow to infinity, the
203 // comparisons work out correctly and (one of) the infinitely large subrects will be chosen.
204 float aHeight = (float) a.height();
205 float aWidth = (float) a.width();
206 float leftArea = 0.f, rightArea = 0.f, topArea = 0.f, bottomArea = 0.f;
207 int positiveCount = 0;
208 if (b.fLeft > a.fLeft) {
209 leftArea = (b.fLeft - a.fLeft) / aWidth;
210 positiveCount++;
211 }
212 if (a.fRight > b.fRight) {
213 rightArea = (a.fRight - b.fRight) / aWidth;
214 positiveCount++;
215 }
216 if (b.fTop > a.fTop) {
217 topArea = (b.fTop - a.fTop) / aHeight;
218 positiveCount++;
219 }
220 if (a.fBottom > b.fBottom) {
221 bottomArea = (a.fBottom - b.fBottom) / aHeight;
222 positiveCount++;
223 }
224
225 if (positiveCount == 0) {
226 SkASSERT(b.contains(a));
227 *out = R::MakeEmpty();
228 return true;
229 }
230
231 *out = a;
232 if (leftArea > rightArea && leftArea > topArea && leftArea > bottomArea) {
233 // Left chunk of A, so the new right edge is B's left edge
234 out->fRight = b.fLeft;
235 } else if (rightArea > topArea && rightArea > bottomArea) {
236 // Right chunk of A, so the new left edge is B's right edge
237 out->fLeft = b.fRight;
238 } else if (topArea > bottomArea) {
239 // Top chunk of A, so the new bottom edge is B's top edge
240 out->fBottom = b.fTop;
241 } else {
242 // Bottom chunk of A, so the new top edge is B's bottom edge
243 SkASSERT(bottomArea > 0.f);
244 out->fTop = b.fBottom;
245 }
246
247 // If we have 1 valid area, the disjoint shape is representable as a rectangle.
248 SkASSERT(!R::Intersects(*out, b));
249 return positiveCount == 1;
250}
251
253 return subtract<SkRect>(a, b, out);
254}
255
257 return subtract<SkIRect>(a, b, out);
258}
259
260
262 const SkIRect& a,
263 const SkIRect& b,
264 float tol) {
266}
267
268bool SkRectPriv::QuadContainsRect(const SkM44& m, const SkRect& a, const SkRect& b, float tol) {
269 return all(QuadContainsRectMask(m, a, b, tol));
270}
271
273 const SkRect& a,
274 const SkRect& b,
275 float tol) {
276 SkDEBUGCODE(SkM44 inverse;)
277 SkASSERT(m.invert(&inverse));
278 // With empty rectangles, the calculated edges could give surprising results. If 'a' were not
279 // sorted, its normals would point outside the sorted rectangle, so lots of potential rects
280 // would be seen as "contained". If 'a' is all 0s, its edge equations are also (0,0,0) so every
281 // point has a distance of 0, and would be interpreted as inside.
282 if (a.isEmpty()) {
283 return skvx::int4(0); // all "false"
284 }
285 // However, 'b' is only used to define its 4 corners to check against the transformed edges.
286 // This is valid regardless of b's emptiness or sortedness.
287
288 // Calculate the 4 homogenous coordinates of 'a' transformed by 'm' where Z=0 and W=1.
289 auto ax = skvx::float4{a.fLeft, a.fRight, a.fRight, a.fLeft};
290 auto ay = skvx::float4{a.fTop, a.fTop, a.fBottom, a.fBottom};
291
292 auto max = m.rc(0,0)*ax + m.rc(0,1)*ay + m.rc(0,3);
293 auto may = m.rc(1,0)*ax + m.rc(1,1)*ay + m.rc(1,3);
294 auto maw = m.rc(3,0)*ax + m.rc(3,1)*ay + m.rc(3,3);
295
296 if (all(maw < 0.f)) {
297 // If all points of A are mapped to w < 0, then the edge equations end up representing the
298 // convex hull of projected points when A should in fact be considered empty.
299 return skvx::int4(0); // all "false"
300 }
301
302 // Cross product of adjacent vertices provides homogenous lines for the 4 sides of the quad
303 auto lA = may*skvx::shuffle<1,2,3,0>(maw) - maw*skvx::shuffle<1,2,3,0>(may);
304 auto lB = maw*skvx::shuffle<1,2,3,0>(max) - max*skvx::shuffle<1,2,3,0>(maw);
305 auto lC = max*skvx::shuffle<1,2,3,0>(may) - may*skvx::shuffle<1,2,3,0>(max);
306
307 // Before transforming, the corners of 'a' were in CW order, but afterwards they may become CCW,
308 // so the sign corrects the direction of the edge normals to point inwards.
309 float sign = (lA[0]*lB[1] - lB[0]*lA[1]) < 0 ? -1.f : 1.f;
310
311 // Calculate distance from 'b' to each edge. Since 'b' has presumably been transformed by 'm'
312 // *and* projected, this assumes W = 1.
313 SkRect bInset = b.makeInset(tol, tol);
314 auto d0 = sign * (lA*bInset.fLeft + lB*bInset.fTop + lC);
315 auto d1 = sign * (lA*bInset.fRight + lB*bInset.fTop + lC);
316 auto d2 = sign * (lA*bInset.fRight + lB*bInset.fBottom + lC);
317 auto d3 = sign * (lA*bInset.fLeft + lB*bInset.fBottom + lC);
318
319 // 'b' is contained in the mapped rectangle if all distances are >= 0
320 return (d0 >= 0.f) & (d1 >= 0.f) & (d2 >= 0.f) & (d3 >= 0.f);
321}
322
324 if (src.isEmpty() || dst.isEmpty()) {
325 return SkIRect::MakeEmpty();
326 }
327
328 int l = src.fLeft;
329 int r = src.fRight;
330 if (r <= dst.fLeft) {
331 // Select right column of pixels in crop
332 l = r - 1;
333 } else if (l >= dst.fRight) {
334 // Left column of 'crop'
335 r = l + 1;
336 } else {
337 // Regular intersection along X axis.
338 l = SkTPin(l, dst.fLeft, dst.fRight);
339 r = SkTPin(r, dst.fLeft, dst.fRight);
340 }
341
342 int t = src.fTop;
343 int b = src.fBottom;
344 if (b <= dst.fTop) {
345 // Select bottom row of pixels in crop
346 t = b - 1;
347 } else if (t >= dst.fBottom) {
348 // Top row of 'crop'
349 b = t + 1;
350 } else {
351 t = SkTPin(t, dst.fTop, dst.fBottom);
352 b = SkTPin(b, dst.fTop, dst.fBottom);
353 }
354
355 return SkIRect::MakeLTRB(l,t,r,b);
356}
int count
Definition: FontMgrTest.cpp:50
#define SkASSERT(cond)
Definition: SkAssert.h:116
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
constexpr float SK_FloatNaN
static int sign(SkScalar x)
Definition: SkPath.cpp:2205
static bool subtract(const R &a, const R &b, R *out)
Definition: SkRect.cpp:177
static const char * set_scalar(SkString *storage, float value, SkScalarAsStringType asType)
Definition: SkRect.cpp:146
#define CHECK_INTERSECT(al, at, ar, ab, bl, bt, br, bb)
Definition: SkRect.cpp:106
void SkAppendScalar(SkString *str, SkScalar value, SkScalarAsStringType asType)
SkScalarAsStringType
Definition: SkStringUtils.h:18
@ kHex_SkScalarAsStringType
Definition: SkStringUtils.h:20
@ kDec_SkScalarAsStringType
Definition: SkStringUtils.h:19
static void SkAppendScalarDec(SkString *str, SkScalar value)
Definition: SkStringUtils.h:25
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition: SkTPin.h:19
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()
Definition: SkM44.h:150
static skvx::int4 QuadContainsRectMask(const SkM44 &m, const SkRect &a, const SkRect &b, float tol=0.f)
Definition: SkRect.cpp:272
static SkIRect ClosestDisjointEdge(const SkIRect &src, const SkIRect &dst)
Definition: SkRect.cpp:323
static bool Subtract(const SkRect &a, const SkRect &b, SkRect *out)
Definition: SkRect.cpp:252
static bool QuadContainsRect(const SkMatrix &m, const SkIRect &a, const SkIRect &b, float tol=0.f)
Definition: SkRect.cpp:261
void reset()
Definition: SkString.cpp:358
const char * c_str() const
Definition: SkString.h:133
static bool b
struct MyStruct a[10]
uint8_t value
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
#define R(r)
dst
Definition: cp.py:12
Vec< 4, int32_t > int4
Definition: SkVx.h:1159
SIT bool all(const Vec< 1, T > &x)
Definition: SkVx.h:582
SIT T max(const Vec< 1, T > &x)
Definition: SkVx.h:641
SIT T min(const Vec< 1, T > &x)
Definition: SkVx.h:640
#define T
Definition: precompiler.cc:65
Definition: SkRect.h:32
bool intersect(const SkIRect &r)
Definition: SkRect.h:513
int32_t fBottom
larger y-axis bounds
Definition: SkRect.h:36
static constexpr SkIRect MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b)
Definition: SkRect.h:91
int32_t fTop
smaller y-axis bounds
Definition: SkRect.h:34
void join(const SkIRect &r)
Definition: SkRect.cpp:31
static constexpr SkIRect MakeEmpty()
Definition: SkRect.h:45
bool isEmpty() const
Definition: SkRect.h:202
int32_t fLeft
smaller x-axis bounds
Definition: SkRect.h:33
int32_t fRight
larger x-axis bounds
Definition: SkRect.h:35
void set(float x, float y)
Definition: SkPoint_impl.h:200
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
SkScalar fBottom
larger y-axis bounds
Definition: extension.cpp:17
void toQuad(SkPoint quad[4]) const
Definition: SkRect.cpp:50
bool intersect(const SkRect &r)
Definition: SkRect.cpp:114
SkScalar fLeft
smaller x-axis bounds
Definition: extension.cpp:14
SkScalar fRight
larger x-axis bounds
Definition: extension.cpp:16
bool setBoundsCheck(const SkPoint pts[], int count)
Definition: SkRect.cpp:61
void setLTRB(float left, float top, float right, float bottom)
Definition: SkRect.h:865
bool isEmpty() const
Definition: SkRect.h:693
void join(const SkRect &r)
Definition: SkRect.cpp:126
void dump() const
Definition: SkRect.h:1356
void setBoundsNoCheck(const SkPoint pts[], int count)
Definition: SkRect.cpp:100
SkScalar fTop
smaller y-axis bounds
Definition: extension.cpp:15
void setEmpty()
Definition: SkRect.h:842
Definition: SkVx.h:83
static SKVX_ALWAYS_INLINE Vec Load(const void *ptr)
Definition: SkVx.h:109