blob: ffded093510f3cb9961ecee5a936ee3daa2cb6c6 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef GrRect_DEFINED
9#define GrRect_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkRect.h"
12#include "include/core/SkTypes.h"
13#include "include/private/SkTo.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
reed@google.comac10a2d2010-12-22 21:39:39 +000015struct GrIRect16 {
16 int16_t fLeft, fTop, fRight, fBottom;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000017
commit-bot@chromium.org2f569982014-04-10 18:36:19 +000018 static GrIRect16 SK_WARN_UNUSED_RESULT MakeEmpty() {
19 GrIRect16 r;
20 r.setEmpty();
21 return r;
22 }
23
robertphillips952841b2014-06-30 08:26:50 -070024 static GrIRect16 SK_WARN_UNUSED_RESULT MakeWH(int16_t w, int16_t h) {
25 GrIRect16 r;
26 r.set(0, 0, w, h);
27 return r;
28 }
29
30 static GrIRect16 SK_WARN_UNUSED_RESULT MakeXYWH(int16_t x, int16_t y, int16_t w, int16_t h) {
31 GrIRect16 r;
32 r.set(x, y, x + w, y + h);
33 return r;
34 }
35
Herb Derby3e8e34e2019-06-20 15:16:13 -040036 static GrIRect16 SK_WARN_UNUSED_RESULT Make(const SkIRect& ir) {
37 GrIRect16 r;
38 r.set(ir);
39 return r;
40 }
41
reed@google.comac10a2d2010-12-22 21:39:39 +000042 int width() const { return fRight - fLeft; }
43 int height() const { return fBottom - fTop; }
44 int area() const { return this->width() * this->height(); }
45 bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000046
commit-bot@chromium.org2f569982014-04-10 18:36:19 +000047 void setEmpty() { memset(this, 0, sizeof(*this)); }
48
robertphillips952841b2014-06-30 08:26:50 -070049 void set(int16_t left, int16_t top, int16_t right, int16_t bottom) {
50 fLeft = left;
51 fTop = top;
52 fRight = right;
53 fBottom = bottom;
54 }
55
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000056 void set(const SkIRect& r) {
reed@google.com20efde72011-05-09 17:00:02 +000057 fLeft = SkToS16(r.fLeft);
58 fTop = SkToS16(r.fTop);
59 fRight = SkToS16(r.fRight);
60 fBottom = SkToS16(r.fBottom);
bsalomon@google.comd302f142011-03-03 13:54:13 +000061 }
reed@google.comac10a2d2010-12-22 21:39:39 +000062};
63
Brian Salomona4677b52017-05-04 12:39:56 -040064/** Returns true if the rectangles have a nonzero area of overlap. It assumed that rects can be
65 infinitely small but not "inverted". */
66static inline bool GrRectsOverlap(const SkRect& a, const SkRect& b) {
Brian Salomon1ffda042017-05-09 18:09:28 -040067 // See skbug.com/6607 about the isFinite() checks.
68 SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom));
69 SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom));
Brian Salomona4677b52017-05-04 12:39:56 -040070 return a.fRight > b.fLeft && a.fBottom > b.fTop && b.fRight > a.fLeft && b.fBottom > a.fTop;
71}
72
73/** Returns true if the rectangles overlap or share an edge or corner. It assumed that rects can be
74 infinitely small but not "inverted". */
75static inline bool GrRectsTouchOrOverlap(const SkRect& a, const SkRect& b) {
Brian Salomon1ffda042017-05-09 18:09:28 -040076 // See skbug.com/6607 about the isFinite() checks.
77 SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom));
78 SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom));
Brian Salomona4677b52017-05-04 12:39:56 -040079 return a.fRight >= b.fLeft && a.fBottom >= b.fTop && b.fRight >= a.fLeft && b.fBottom >= a.fTop;
80}
Michael Ludwigce62dec2019-02-19 11:48:46 -050081
82/**
83 * Apply the transform from 'inRect' to 'outRect' to each point in 'inPts', storing the mapped point
84 * into the parallel index of 'outPts'.
85 */
86static inline void GrMapRectPoints(const SkRect& inRect, const SkRect& outRect,
87 const SkPoint inPts[], SkPoint outPts[], int ptCount) {
88 SkMatrix rectTransform = SkMatrix::MakeRectToRect(inRect, outRect, SkMatrix::kFill_ScaleToFit);
89 rectTransform.mapPoints(outPts, inPts, ptCount);
90}
Greg Daniel46cfbc62019-06-07 11:43:30 -040091
92/**
93 * Clips the srcRect and the dstPoint to the bounds of the srcSize and dstSize respectively. Returns
94 * true if the srcRect and dstRect intersect the srcRect and dst rect (dstPoint with srcRect
95 * width/height). Returns false otherwise. The clipped values are returned in clippedSrcRect and
96 * clippedDstPoint.
97 */
98static inline bool GrClipSrcRectAndDstPoint(const SkISize& dstSize,
99 const SkISize& srcSize,
100 const SkIRect& srcRect,
101 const SkIPoint& dstPoint,
102 SkIRect* clippedSrcRect,
103 SkIPoint* clippedDstPoint) {
104 *clippedSrcRect = srcRect;
105 *clippedDstPoint = dstPoint;
106
107 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
108 if (clippedSrcRect->fLeft < 0) {
109 clippedDstPoint->fX -= clippedSrcRect->fLeft;
110 clippedSrcRect->fLeft = 0;
111 }
112 if (clippedDstPoint->fX < 0) {
113 clippedSrcRect->fLeft -= clippedDstPoint->fX;
114 clippedDstPoint->fX = 0;
115 }
116
117 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
118 if (clippedSrcRect->fTop < 0) {
119 clippedDstPoint->fY -= clippedSrcRect->fTop;
120 clippedSrcRect->fTop = 0;
121 }
122 if (clippedDstPoint->fY < 0) {
123 clippedSrcRect->fTop -= clippedDstPoint->fY;
124 clippedDstPoint->fY = 0;
125 }
126
127 // clip the right edge to the src and dst bounds.
128 if (clippedSrcRect->fRight > srcSize.width()) {
129 clippedSrcRect->fRight = srcSize.width();
130 }
131 if (clippedDstPoint->fX + clippedSrcRect->width() > dstSize.width()) {
132 clippedSrcRect->fRight = clippedSrcRect->fLeft + dstSize.width() - clippedDstPoint->fX;
133 }
134
135 // clip the bottom edge to the src and dst bounds.
136 if (clippedSrcRect->fBottom > srcSize.height()) {
137 clippedSrcRect->fBottom = srcSize.height();
138 }
139 if (clippedDstPoint->fY + clippedSrcRect->height() > dstSize.height()) {
140 clippedSrcRect->fBottom = clippedSrcRect->fTop + dstSize.height() - clippedDstPoint->fY;
141 }
142
143 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
144 // dst bounds.
145 return !clippedSrcRect->isEmpty();
146}
reed@google.comac10a2d2010-12-22 21:39:39 +0000147#endif