1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker * Copyright 2010 Google Inc.
3*c8dee2aaSAndroid Build Coastguard Worker *
4*c8dee2aaSAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker */
7*c8dee2aaSAndroid Build Coastguard Worker
8*c8dee2aaSAndroid Build Coastguard Worker #ifndef GrRect_DEFINED
9*c8dee2aaSAndroid Build Coastguard Worker #define GrRect_DEFINED
10*c8dee2aaSAndroid Build Coastguard Worker
11*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkMatrix.h"
12*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkRect.h"
13*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkTypes.h"
14*c8dee2aaSAndroid Build Coastguard Worker
15*c8dee2aaSAndroid Build Coastguard Worker /** Returns true if the rectangles have a nonzero area of overlap. It assumed that rects can be
16*c8dee2aaSAndroid Build Coastguard Worker infinitely small but not "inverted". */
GrRectsOverlap(const SkRect & a,const SkRect & b)17*c8dee2aaSAndroid Build Coastguard Worker static inline bool GrRectsOverlap(const SkRect& a, const SkRect& b) {
18*c8dee2aaSAndroid Build Coastguard Worker // See skbug.com/6607 about the isFinite() checks.
19*c8dee2aaSAndroid Build Coastguard Worker SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom));
20*c8dee2aaSAndroid Build Coastguard Worker SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom));
21*c8dee2aaSAndroid Build Coastguard Worker return a.fRight > b.fLeft && a.fBottom > b.fTop && b.fRight > a.fLeft && b.fBottom > a.fTop;
22*c8dee2aaSAndroid Build Coastguard Worker }
23*c8dee2aaSAndroid Build Coastguard Worker
24*c8dee2aaSAndroid Build Coastguard Worker /** Returns true if the rectangles overlap or share an edge or corner. It assumed that rects can be
25*c8dee2aaSAndroid Build Coastguard Worker infinitely small but not "inverted". */
GrRectsTouchOrOverlap(const SkRect & a,const SkRect & b)26*c8dee2aaSAndroid Build Coastguard Worker static inline bool GrRectsTouchOrOverlap(const SkRect& a, const SkRect& b) {
27*c8dee2aaSAndroid Build Coastguard Worker // See skbug.com/6607 about the isFinite() checks.
28*c8dee2aaSAndroid Build Coastguard Worker SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom));
29*c8dee2aaSAndroid Build Coastguard Worker SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom));
30*c8dee2aaSAndroid Build Coastguard Worker return a.fRight >= b.fLeft && a.fBottom >= b.fTop && b.fRight >= a.fLeft && b.fBottom >= a.fTop;
31*c8dee2aaSAndroid Build Coastguard Worker }
32*c8dee2aaSAndroid Build Coastguard Worker
33*c8dee2aaSAndroid Build Coastguard Worker /**
34*c8dee2aaSAndroid Build Coastguard Worker * Apply the transform from 'inRect' to 'outRect' to each point in 'inPts', storing the mapped point
35*c8dee2aaSAndroid Build Coastguard Worker * into the parallel index of 'outPts'.
36*c8dee2aaSAndroid Build Coastguard Worker */
GrMapRectPoints(const SkRect & inRect,const SkRect & outRect,const SkPoint inPts[],SkPoint outPts[],int ptCount)37*c8dee2aaSAndroid Build Coastguard Worker static inline void GrMapRectPoints(const SkRect& inRect, const SkRect& outRect,
38*c8dee2aaSAndroid Build Coastguard Worker const SkPoint inPts[], SkPoint outPts[], int ptCount) {
39*c8dee2aaSAndroid Build Coastguard Worker SkMatrix::RectToRect(inRect, outRect).mapPoints(outPts, inPts, ptCount);
40*c8dee2aaSAndroid Build Coastguard Worker }
41*c8dee2aaSAndroid Build Coastguard Worker
42*c8dee2aaSAndroid Build Coastguard Worker /**
43*c8dee2aaSAndroid Build Coastguard Worker * Clips the srcRect and the dstPoint to the bounds of the srcSize and dstSize respectively. Returns
44*c8dee2aaSAndroid Build Coastguard Worker * true if the srcRect and dstRect intersect the srcRect and dst rect (dstPoint with srcRect
45*c8dee2aaSAndroid Build Coastguard Worker * width/height). Returns false otherwise. The clipped values are stored back into 'dstPoint'
46*c8dee2aaSAndroid Build Coastguard Worker * and 'srcRect'
47*c8dee2aaSAndroid Build Coastguard Worker */
GrClipSrcRectAndDstPoint(const SkISize & dstSize,SkIPoint * dstPoint,const SkISize & srcSize,SkIRect * srcRect)48*c8dee2aaSAndroid Build Coastguard Worker static inline bool GrClipSrcRectAndDstPoint(const SkISize& dstSize,
49*c8dee2aaSAndroid Build Coastguard Worker SkIPoint* dstPoint,
50*c8dee2aaSAndroid Build Coastguard Worker const SkISize& srcSize,
51*c8dee2aaSAndroid Build Coastguard Worker SkIRect* srcRect) {
52*c8dee2aaSAndroid Build Coastguard Worker // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
53*c8dee2aaSAndroid Build Coastguard Worker if (srcRect->fLeft < 0) {
54*c8dee2aaSAndroid Build Coastguard Worker dstPoint->fX -= srcRect->fLeft;
55*c8dee2aaSAndroid Build Coastguard Worker srcRect->fLeft = 0;
56*c8dee2aaSAndroid Build Coastguard Worker }
57*c8dee2aaSAndroid Build Coastguard Worker if (dstPoint->fX < 0) {
58*c8dee2aaSAndroid Build Coastguard Worker srcRect->fLeft -= dstPoint->fX;
59*c8dee2aaSAndroid Build Coastguard Worker dstPoint->fX = 0;
60*c8dee2aaSAndroid Build Coastguard Worker }
61*c8dee2aaSAndroid Build Coastguard Worker
62*c8dee2aaSAndroid Build Coastguard Worker // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
63*c8dee2aaSAndroid Build Coastguard Worker if (srcRect->fTop < 0) {
64*c8dee2aaSAndroid Build Coastguard Worker dstPoint->fY -= srcRect->fTop;
65*c8dee2aaSAndroid Build Coastguard Worker srcRect->fTop = 0;
66*c8dee2aaSAndroid Build Coastguard Worker }
67*c8dee2aaSAndroid Build Coastguard Worker if (dstPoint->fY < 0) {
68*c8dee2aaSAndroid Build Coastguard Worker srcRect->fTop -= dstPoint->fY;
69*c8dee2aaSAndroid Build Coastguard Worker dstPoint->fY = 0;
70*c8dee2aaSAndroid Build Coastguard Worker }
71*c8dee2aaSAndroid Build Coastguard Worker
72*c8dee2aaSAndroid Build Coastguard Worker // clip the right edge to the src and dst bounds.
73*c8dee2aaSAndroid Build Coastguard Worker if (srcRect->fRight > srcSize.width()) {
74*c8dee2aaSAndroid Build Coastguard Worker srcRect->fRight = srcSize.width();
75*c8dee2aaSAndroid Build Coastguard Worker }
76*c8dee2aaSAndroid Build Coastguard Worker if (dstPoint->fX + srcRect->width() > dstSize.width()) {
77*c8dee2aaSAndroid Build Coastguard Worker srcRect->fRight = srcRect->fLeft + dstSize.width() - dstPoint->fX;
78*c8dee2aaSAndroid Build Coastguard Worker }
79*c8dee2aaSAndroid Build Coastguard Worker
80*c8dee2aaSAndroid Build Coastguard Worker // clip the bottom edge to the src and dst bounds.
81*c8dee2aaSAndroid Build Coastguard Worker if (srcRect->fBottom > srcSize.height()) {
82*c8dee2aaSAndroid Build Coastguard Worker srcRect->fBottom = srcSize.height();
83*c8dee2aaSAndroid Build Coastguard Worker }
84*c8dee2aaSAndroid Build Coastguard Worker if (dstPoint->fY + srcRect->height() > dstSize.height()) {
85*c8dee2aaSAndroid Build Coastguard Worker srcRect->fBottom = srcRect->fTop + dstSize.height() - dstPoint->fY;
86*c8dee2aaSAndroid Build Coastguard Worker }
87*c8dee2aaSAndroid Build Coastguard Worker
88*c8dee2aaSAndroid Build Coastguard Worker // The above clipping steps may have inverted the rect if it didn't intersect either the src or
89*c8dee2aaSAndroid Build Coastguard Worker // dst bounds.
90*c8dee2aaSAndroid Build Coastguard Worker return !srcRect->isEmpty();
91*c8dee2aaSAndroid Build Coastguard Worker }
92*c8dee2aaSAndroid Build Coastguard Worker
93*c8dee2aaSAndroid Build Coastguard Worker #endif
94