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 };
64
65 /** Returns true if the rectangles have a nonzero area of overlap. It assumed that rects can be
66 infinitely small but not "inverted". */
GrRectsOverlap(const SkRect & a,const SkRect & b)67 static inline bool GrRectsOverlap(const SkRect& a, const SkRect& b) {
68 // See skbug.com/6607 about the isFinite() checks.
69 SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom));
70 SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom));
71 return a.fRight > b.fLeft && a.fBottom > b.fTop && b.fRight > a.fLeft && b.fBottom > a.fTop;
72 }
73
74 /** Returns true if the rectangles overlap or share an edge or corner. It assumed that rects can be
75 infinitely small but not "inverted". */
GrRectsTouchOrOverlap(const SkRect & a,const SkRect & b)76 static inline bool GrRectsTouchOrOverlap(const SkRect& a, const SkRect& b) {
77 // See skbug.com/6607 about the isFinite() checks.
78 SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom));
79 SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom));
80 return a.fRight >= b.fLeft && a.fBottom >= b.fTop && b.fRight >= a.fLeft && b.fBottom >= a.fTop;
81 }
82
83 /**
84 * Apply the transform from 'inRect' to 'outRect' to each point in 'inPts', storing the mapped point
85 * into the parallel index of 'outPts'.
86 */
GrMapRectPoints(const SkRect & inRect,const SkRect & outRect,const SkPoint inPts[],SkPoint outPts[],int ptCount)87 static inline void GrMapRectPoints(const SkRect& inRect, const SkRect& outRect,
88 const SkPoint inPts[], SkPoint outPts[], int ptCount) {
89 SkMatrix rectTransform = SkMatrix::MakeRectToRect(inRect, outRect, SkMatrix::kFill_ScaleToFit);
90 rectTransform.mapPoints(outPts, inPts, ptCount);
91 }
92
93 /**
94 * Clips the srcRect and the dstPoint to the bounds of the srcSize and dstSize respectively. Returns
95 * true if the srcRect and dstRect intersect the srcRect and dst rect (dstPoint with srcRect
96 * width/height). Returns false otherwise. The clipped values are returned in clippedSrcRect and
97 * clippedDstPoint.
98 */
GrClipSrcRectAndDstPoint(const SkISize & dstSize,const SkISize & srcSize,const SkIRect & srcRect,const SkIPoint & dstPoint,SkIRect * clippedSrcRect,SkIPoint * clippedDstPoint)99 static inline bool GrClipSrcRectAndDstPoint(const SkISize& dstSize,
100 const SkISize& srcSize,
101 const SkIRect& srcRect,
102 const SkIPoint& dstPoint,
103 SkIRect* clippedSrcRect,
104 SkIPoint* clippedDstPoint) {
105 *clippedSrcRect = srcRect;
106 *clippedDstPoint = dstPoint;
107
108 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
109 if (clippedSrcRect->fLeft < 0) {
110 clippedDstPoint->fX -= clippedSrcRect->fLeft;
111 clippedSrcRect->fLeft = 0;
112 }
113 if (clippedDstPoint->fX < 0) {
114 clippedSrcRect->fLeft -= clippedDstPoint->fX;
115 clippedDstPoint->fX = 0;
116 }
117
118 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
119 if (clippedSrcRect->fTop < 0) {
120 clippedDstPoint->fY -= clippedSrcRect->fTop;
121 clippedSrcRect->fTop = 0;
122 }
123 if (clippedDstPoint->fY < 0) {
124 clippedSrcRect->fTop -= clippedDstPoint->fY;
125 clippedDstPoint->fY = 0;
126 }
127
128 // clip the right edge to the src and dst bounds.
129 if (clippedSrcRect->fRight > srcSize.width()) {
130 clippedSrcRect->fRight = srcSize.width();
131 }
132 if (clippedDstPoint->fX + clippedSrcRect->width() > dstSize.width()) {
133 clippedSrcRect->fRight = clippedSrcRect->fLeft + dstSize.width() - clippedDstPoint->fX;
134 }
135
136 // clip the bottom edge to the src and dst bounds.
137 if (clippedSrcRect->fBottom > srcSize.height()) {
138 clippedSrcRect->fBottom = srcSize.height();
139 }
140 if (clippedDstPoint->fY + clippedSrcRect->height() > dstSize.height()) {
141 clippedSrcRect->fBottom = clippedSrcRect->fTop + dstSize.height() - clippedDstPoint->fY;
142 }
143
144 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
145 // dst bounds.
146 return !clippedSrcRect->isEmpty();
147 }
148 #endif
149