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