1 /*
2 * Copyright 2010 Google Inc.
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 GrRect_DEFINED
9 #define GrRect_DEFINED
10
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkRect.h"
13 #include "include/core/SkTypes.h"
14 #include "include/private/SkTo.h"
15
16 struct GrIRect16 {
17 int16_t fLeft, fTop, fRight, fBottom;
18
MakeEmptyGrIRect1619 static GrIRect16 SK_WARN_UNUSED_RESULT MakeEmpty() {
20 GrIRect16 r;
21 r.setEmpty();
22 return r;
23 }
24
MakeWHGrIRect1625 static GrIRect16 SK_WARN_UNUSED_RESULT MakeWH(int16_t w, int16_t h) {
26 GrIRect16 r;
27 r.set(0, 0, w, h);
28 return r;
29 }
30
MakeXYWHGrIRect1631 static GrIRect16 SK_WARN_UNUSED_RESULT MakeXYWH(int16_t x, int16_t y, int16_t w, int16_t h) {
32 GrIRect16 r;
33 r.set(x, y, x + w, y + h);
34 return r;
35 }
36
MakeGrIRect1637 static GrIRect16 SK_WARN_UNUSED_RESULT Make(const SkIRect& ir) {
38 GrIRect16 r;
39 r.set(ir);
40 return r;
41 }
42
widthGrIRect1643 int width() const { return fRight - fLeft; }
heightGrIRect1644 int height() const { return fBottom - fTop; }
areaGrIRect1645 int area() const { return this->width() * this->height(); }
isEmptyGrIRect1646 bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
47
setEmptyGrIRect1648 void setEmpty() { memset(this, 0, sizeof(*this)); }
49
setGrIRect1650 void set(int16_t left, int16_t top, int16_t right, int16_t bottom) {
51 fLeft = left;
52 fTop = top;
53 fRight = right;
54 fBottom = bottom;
55 }
56
setGrIRect1657 void set(const SkIRect& r) {
58 fLeft = SkToS16(r.fLeft);
59 fTop = SkToS16(r.fTop);
60 fRight = SkToS16(r.fRight);
61 fBottom = SkToS16(r.fBottom);
62 }
63
offsetGrIRect1664 void offset(int16_t dx, int16_t dy) {
65 fLeft += dx;
66 fTop += dy;
67 fRight += dx;
68 fBottom += dy;
69 }
70 };
71
72 /** Returns true if the rectangles have a nonzero area of overlap. It assumed that rects can be
73 infinitely small but not "inverted". */
GrRectsOverlap(const SkRect & a,const SkRect & b)74 static inline bool GrRectsOverlap(const SkRect& a, const SkRect& b) {
75 // See skbug.com/6607 about the isFinite() checks.
76 SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom));
77 SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom));
78 return a.fRight > b.fLeft && a.fBottom > b.fTop && b.fRight > a.fLeft && b.fBottom > a.fTop;
79 }
80
81 /** Returns true if the rectangles overlap or share an edge or corner. It assumed that rects can be
82 infinitely small but not "inverted". */
GrRectsTouchOrOverlap(const SkRect & a,const SkRect & b)83 static inline bool GrRectsTouchOrOverlap(const SkRect& a, const SkRect& b) {
84 // See skbug.com/6607 about the isFinite() checks.
85 SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom));
86 SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom));
87 return a.fRight >= b.fLeft && a.fBottom >= b.fTop && b.fRight >= a.fLeft && b.fBottom >= a.fTop;
88 }
89
90 /**
91 * Apply the transform from 'inRect' to 'outRect' to each point in 'inPts', storing the mapped point
92 * into the parallel index of 'outPts'.
93 */
GrMapRectPoints(const SkRect & inRect,const SkRect & outRect,const SkPoint inPts[],SkPoint outPts[],int ptCount)94 static inline void GrMapRectPoints(const SkRect& inRect, const SkRect& outRect,
95 const SkPoint inPts[], SkPoint outPts[], int ptCount) {
96 SkMatrix::RectToRect(inRect, outRect).mapPoints(outPts, inPts, ptCount);
97 }
98
99 /**
100 * Clips the srcRect and the dstPoint to the bounds of the srcSize and dstSize respectively. Returns
101 * true if the srcRect and dstRect intersect the srcRect and dst rect (dstPoint with srcRect
102 * width/height). Returns false otherwise. The clipped values are returned in clippedSrcRect and
103 * clippedDstPoint.
104 */
GrClipSrcRectAndDstPoint(const SkISize & dstSize,const SkISize & srcSize,const SkIRect & srcRect,const SkIPoint & dstPoint,SkIRect * clippedSrcRect,SkIPoint * clippedDstPoint)105 static inline bool GrClipSrcRectAndDstPoint(const SkISize& dstSize,
106 const SkISize& srcSize,
107 const SkIRect& srcRect,
108 const SkIPoint& dstPoint,
109 SkIRect* clippedSrcRect,
110 SkIPoint* clippedDstPoint) {
111 *clippedSrcRect = srcRect;
112 *clippedDstPoint = dstPoint;
113
114 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
115 if (clippedSrcRect->fLeft < 0) {
116 clippedDstPoint->fX -= clippedSrcRect->fLeft;
117 clippedSrcRect->fLeft = 0;
118 }
119 if (clippedDstPoint->fX < 0) {
120 clippedSrcRect->fLeft -= clippedDstPoint->fX;
121 clippedDstPoint->fX = 0;
122 }
123
124 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
125 if (clippedSrcRect->fTop < 0) {
126 clippedDstPoint->fY -= clippedSrcRect->fTop;
127 clippedSrcRect->fTop = 0;
128 }
129 if (clippedDstPoint->fY < 0) {
130 clippedSrcRect->fTop -= clippedDstPoint->fY;
131 clippedDstPoint->fY = 0;
132 }
133
134 // clip the right edge to the src and dst bounds.
135 if (clippedSrcRect->fRight > srcSize.width()) {
136 clippedSrcRect->fRight = srcSize.width();
137 }
138 if (clippedDstPoint->fX + clippedSrcRect->width() > dstSize.width()) {
139 clippedSrcRect->fRight = clippedSrcRect->fLeft + dstSize.width() - clippedDstPoint->fX;
140 }
141
142 // clip the bottom edge to the src and dst bounds.
143 if (clippedSrcRect->fBottom > srcSize.height()) {
144 clippedSrcRect->fBottom = srcSize.height();
145 }
146 if (clippedDstPoint->fY + clippedSrcRect->height() > dstSize.height()) {
147 clippedSrcRect->fBottom = clippedSrcRect->fTop + dstSize.height() - clippedDstPoint->fY;
148 }
149
150 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
151 // dst bounds.
152 return !clippedSrcRect->isEmpty();
153 }
154 #endif
155