reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 8 | #ifndef GrRect_DEFINED |
| 9 | #define GrRect_DEFINED |
| 10 | |
Hal Canary | 02eefbe | 2019-06-26 13:54:14 -0400 | [diff] [blame] | 11 | #include "include/core/SkMatrix.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkRect.h" |
| 13 | #include "include/core/SkTypes.h" |
| 14 | #include "include/private/SkTo.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 15 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | struct GrIRect16 { |
| 17 | int16_t fLeft, fTop, fRight, fBottom; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 18 | |
commit-bot@chromium.org | 2f56998 | 2014-04-10 18:36:19 +0000 | [diff] [blame] | 19 | static GrIRect16 SK_WARN_UNUSED_RESULT MakeEmpty() { |
| 20 | GrIRect16 r; |
| 21 | r.setEmpty(); |
| 22 | return r; |
| 23 | } |
| 24 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 25 | 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 | |
| 31 | 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 | |
Herb Derby | 3e8e34e | 2019-06-20 15:16:13 -0400 | [diff] [blame] | 37 | static GrIRect16 SK_WARN_UNUSED_RESULT Make(const SkIRect& ir) { |
| 38 | GrIRect16 r; |
| 39 | r.set(ir); |
| 40 | return r; |
| 41 | } |
| 42 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 43 | int width() const { return fRight - fLeft; } |
| 44 | int height() const { return fBottom - fTop; } |
| 45 | int area() const { return this->width() * this->height(); } |
| 46 | bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 47 | |
commit-bot@chromium.org | 2f56998 | 2014-04-10 18:36:19 +0000 | [diff] [blame] | 48 | void setEmpty() { memset(this, 0, sizeof(*this)); } |
| 49 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 50 | 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 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 57 | void set(const SkIRect& r) { |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 58 | fLeft = SkToS16(r.fLeft); |
| 59 | fTop = SkToS16(r.fTop); |
| 60 | fRight = SkToS16(r.fRight); |
| 61 | fBottom = SkToS16(r.fBottom); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 62 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
Brian Salomon | a4677b5 | 2017-05-04 12:39:56 -0400 | [diff] [blame] | 65 | /** Returns true if the rectangles have a nonzero area of overlap. It assumed that rects can be |
| 66 | infinitely small but not "inverted". */ |
| 67 | static inline bool GrRectsOverlap(const SkRect& a, const SkRect& b) { |
Brian Salomon | 1ffda04 | 2017-05-09 18:09:28 -0400 | [diff] [blame] | 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)); |
Brian Salomon | a4677b5 | 2017-05-04 12:39:56 -0400 | [diff] [blame] | 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". */ |
| 76 | static inline bool GrRectsTouchOrOverlap(const SkRect& a, const SkRect& b) { |
Brian Salomon | 1ffda04 | 2017-05-09 18:09:28 -0400 | [diff] [blame] | 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)); |
Brian Salomon | a4677b5 | 2017-05-04 12:39:56 -0400 | [diff] [blame] | 80 | return a.fRight >= b.fLeft && a.fBottom >= b.fTop && b.fRight >= a.fLeft && b.fBottom >= a.fTop; |
| 81 | } |
Michael Ludwig | ce62dec | 2019-02-19 11:48:46 -0500 | [diff] [blame] | 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 | */ |
| 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 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 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 | */ |
| 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 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 148 | #endif |