blob: 2a3e72be08d58bca610659c20518d2f791773dc3 [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
reed@google.comac10a2d2010-12-22 21:39:39 +000036 int width() const { return fRight - fLeft; }
37 int height() const { return fBottom - fTop; }
38 int area() const { return this->width() * this->height(); }
39 bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000040
commit-bot@chromium.org2f569982014-04-10 18:36:19 +000041 void setEmpty() { memset(this, 0, sizeof(*this)); }
42
robertphillips952841b2014-06-30 08:26:50 -070043 void set(int16_t left, int16_t top, int16_t right, int16_t bottom) {
44 fLeft = left;
45 fTop = top;
46 fRight = right;
47 fBottom = bottom;
48 }
49
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000050 void set(const SkIRect& r) {
reed@google.com20efde72011-05-09 17:00:02 +000051 fLeft = SkToS16(r.fLeft);
52 fTop = SkToS16(r.fTop);
53 fRight = SkToS16(r.fRight);
54 fBottom = SkToS16(r.fBottom);
bsalomon@google.comd302f142011-03-03 13:54:13 +000055 }
reed@google.comac10a2d2010-12-22 21:39:39 +000056};
57
Brian Salomona4677b52017-05-04 12:39:56 -040058/** Returns true if the rectangles have a nonzero area of overlap. It assumed that rects can be
59 infinitely small but not "inverted". */
60static inline bool GrRectsOverlap(const SkRect& a, const SkRect& b) {
Brian Salomon1ffda042017-05-09 18:09:28 -040061 // See skbug.com/6607 about the isFinite() checks.
62 SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom));
63 SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom));
Brian Salomona4677b52017-05-04 12:39:56 -040064 return a.fRight > b.fLeft && a.fBottom > b.fTop && b.fRight > a.fLeft && b.fBottom > a.fTop;
65}
66
67/** Returns true if the rectangles overlap or share an edge or corner. It assumed that rects can be
68 infinitely small but not "inverted". */
69static inline bool GrRectsTouchOrOverlap(const SkRect& a, const SkRect& b) {
Brian Salomon1ffda042017-05-09 18:09:28 -040070 // See skbug.com/6607 about the isFinite() checks.
71 SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom));
72 SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom));
Brian Salomona4677b52017-05-04 12:39:56 -040073 return a.fRight >= b.fLeft && a.fBottom >= b.fTop && b.fRight >= a.fLeft && b.fBottom >= a.fTop;
74}
Michael Ludwigce62dec2019-02-19 11:48:46 -050075
76/**
77 * Apply the transform from 'inRect' to 'outRect' to each point in 'inPts', storing the mapped point
78 * into the parallel index of 'outPts'.
79 */
80static inline void GrMapRectPoints(const SkRect& inRect, const SkRect& outRect,
81 const SkPoint inPts[], SkPoint outPts[], int ptCount) {
82 SkMatrix rectTransform = SkMatrix::MakeRectToRect(inRect, outRect, SkMatrix::kFill_ScaleToFit);
83 rectTransform.mapPoints(outPts, inPts, ptCount);
84}
Greg Daniel46cfbc62019-06-07 11:43:30 -040085
86/**
87 * Clips the srcRect and the dstPoint to the bounds of the srcSize and dstSize respectively. Returns
88 * true if the srcRect and dstRect intersect the srcRect and dst rect (dstPoint with srcRect
89 * width/height). Returns false otherwise. The clipped values are returned in clippedSrcRect and
90 * clippedDstPoint.
91 */
92static inline bool GrClipSrcRectAndDstPoint(const SkISize& dstSize,
93 const SkISize& srcSize,
94 const SkIRect& srcRect,
95 const SkIPoint& dstPoint,
96 SkIRect* clippedSrcRect,
97 SkIPoint* clippedDstPoint) {
98 *clippedSrcRect = srcRect;
99 *clippedDstPoint = dstPoint;
100
101 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
102 if (clippedSrcRect->fLeft < 0) {
103 clippedDstPoint->fX -= clippedSrcRect->fLeft;
104 clippedSrcRect->fLeft = 0;
105 }
106 if (clippedDstPoint->fX < 0) {
107 clippedSrcRect->fLeft -= clippedDstPoint->fX;
108 clippedDstPoint->fX = 0;
109 }
110
111 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
112 if (clippedSrcRect->fTop < 0) {
113 clippedDstPoint->fY -= clippedSrcRect->fTop;
114 clippedSrcRect->fTop = 0;
115 }
116 if (clippedDstPoint->fY < 0) {
117 clippedSrcRect->fTop -= clippedDstPoint->fY;
118 clippedDstPoint->fY = 0;
119 }
120
121 // clip the right edge to the src and dst bounds.
122 if (clippedSrcRect->fRight > srcSize.width()) {
123 clippedSrcRect->fRight = srcSize.width();
124 }
125 if (clippedDstPoint->fX + clippedSrcRect->width() > dstSize.width()) {
126 clippedSrcRect->fRight = clippedSrcRect->fLeft + dstSize.width() - clippedDstPoint->fX;
127 }
128
129 // clip the bottom edge to the src and dst bounds.
130 if (clippedSrcRect->fBottom > srcSize.height()) {
131 clippedSrcRect->fBottom = srcSize.height();
132 }
133 if (clippedDstPoint->fY + clippedSrcRect->height() > dstSize.height()) {
134 clippedSrcRect->fBottom = clippedSrcRect->fTop + dstSize.height() - clippedDstPoint->fY;
135 }
136
137 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
138 // dst bounds.
139 return !clippedSrcRect->isEmpty();
140}
reed@google.comac10a2d2010-12-22 21:39:39 +0000141#endif