1 /*
2 * Copyright 2015 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 #include "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkSurface.h"
19 #include "include/core/SkTypes.h"
20 #include "src/core/SkRectPriv.h"
21 #include "tests/Test.h"
22
23 #include <climits>
24 #include <initializer_list>
25
has_green_pixels(const SkBitmap & bm)26 static bool has_green_pixels(const SkBitmap& bm) {
27 for (int j = 0; j < bm.height(); ++j) {
28 for (int i = 0; i < bm.width(); ++i) {
29 if (SkColorGetG(bm.getColor(i, j))) {
30 return true;
31 }
32 }
33 }
34
35 return false;
36 }
37
test_stroke_width_clipping(skiatest::Reporter * reporter)38 static void test_stroke_width_clipping(skiatest::Reporter* reporter) {
39 SkBitmap bm;
40 bm.allocN32Pixels(100, 10);
41 bm.eraseColor(SK_ColorTRANSPARENT);
42
43 SkCanvas canvas(bm);
44 SkPaint paint;
45 paint.setStyle(SkPaint::kStroke_Style);
46 paint.setStrokeWidth(10);
47 paint.setColor(0xff00ff00);
48
49 // clip out the left half of our canvas
50 canvas.clipRect(SkRect::MakeXYWH(51, 0, 49, 100));
51
52 // no stroke bleed should be visible
53 canvas.drawRect(SkRect::MakeWH(44, 100), paint);
54 REPORTER_ASSERT(reporter, !has_green_pixels(bm));
55
56 // right stroke edge should bleed into the visible area
57 canvas.scale(2, 2);
58 canvas.drawRect(SkRect::MakeWH(22, 50), paint);
59 REPORTER_ASSERT(reporter, has_green_pixels(bm));
60 }
61
test_skbug4406(skiatest::Reporter * reporter)62 static void test_skbug4406(skiatest::Reporter* reporter) {
63 SkBitmap bm;
64 bm.allocN32Pixels(10, 10);
65 bm.eraseColor(SK_ColorTRANSPARENT);
66
67 SkCanvas canvas(bm);
68 const SkRect r = { 1.5f, 1, 3.5f, 3 };
69 // draw filled green rect first
70 SkPaint paint;
71 paint.setStyle(SkPaint::kFill_Style);
72 paint.setColor(0xff00ff00);
73 paint.setStrokeWidth(1);
74 paint.setAntiAlias(true);
75 canvas.drawRect(r, paint);
76
77 // paint black with stroke rect (that asserts in bug 4406)
78 // over the filled rect, it should cover it
79 paint.setStyle(SkPaint::kStroke_Style);
80 paint.setColor(0xff000000);
81 paint.setStrokeWidth(1);
82 canvas.drawRect(r, paint);
83 REPORTER_ASSERT(reporter, !has_green_pixels(bm));
84
85 // do it again with thinner stroke
86 paint.setStyle(SkPaint::kFill_Style);
87 paint.setColor(0xff00ff00);
88 paint.setStrokeWidth(1);
89 paint.setAntiAlias(true);
90 canvas.drawRect(r, paint);
91 // paint black with stroke rect (that asserts in bug 4406)
92 // over the filled rect, it doesnt cover it completelly with thinner stroke
93 paint.setStyle(SkPaint::kStroke_Style);
94 paint.setColor(0xff000000);
95 paint.setStrokeWidth(0.99f);
96 canvas.drawRect(r, paint);
97 REPORTER_ASSERT(reporter, has_green_pixels(bm));
98 }
99
DEF_TEST(Rect,reporter)100 DEF_TEST(Rect, reporter) {
101 test_stroke_width_clipping(reporter);
102 test_skbug4406(reporter);
103 }
104
DEF_TEST(Rect_grow,reporter)105 DEF_TEST(Rect_grow, reporter) {
106 test_stroke_width_clipping(reporter);
107 test_skbug4406(reporter);
108 }
109
DEF_TEST(Rect_path_nan,reporter)110 DEF_TEST(Rect_path_nan, reporter) {
111 SkRect r = { 0, 0, SK_ScalarNaN, 100 };
112 SkPath p;
113 p.addRect(r);
114 // path normally just jams its bounds to be r, but it must notice that r is non-finite
115 REPORTER_ASSERT(reporter, !p.isFinite());
116 }
117
DEF_TEST(Rect_largest,reporter)118 DEF_TEST(Rect_largest, reporter) {
119 REPORTER_ASSERT(reporter, !SkRectPriv::MakeILarge().isEmpty());
120 REPORTER_ASSERT(reporter, SkRectPriv::MakeILargestInverted().isEmpty());
121
122 REPORTER_ASSERT(reporter, !SkRectPriv::MakeLargest().isEmpty());
123 REPORTER_ASSERT(reporter, !SkRectPriv::MakeLargeS32().isEmpty());
124 REPORTER_ASSERT(reporter, SkRectPriv::MakeLargestInverted().isEmpty());
125 }
126
127 /*
128 * Test the setBounds always handles non-finite values correctly:
129 * - setBoundsCheck should return false, and set the rect to all zeros
130 * - setBoundsNoCheck should ensure that rect.isFinite() is false (definitely NOT all zeros)
131 */
DEF_TEST(Rect_setbounds,reporter)132 DEF_TEST(Rect_setbounds, reporter) {
133 const SkPoint p0[] = { { SK_ScalarInfinity, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 } };
134 const SkPoint p1[] = { { 0, SK_ScalarInfinity }, { 1, 1 }, { 2, 2 }, { 3, 3 } };
135 const SkPoint p2[] = { { SK_ScalarNaN, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 } };
136 const SkPoint p3[] = { { 0, SK_ScalarNaN }, { 1, 1 }, { 2, 2 }, { 3, 3 } };
137
138 SkRect r;
139 const SkRect zeror = { 0, 0, 0, 0 };
140 for (const SkPoint* pts : { p0, p1, p2, p3 }) {
141 for (int n = 1; n <= 4; ++n) {
142 bool isfinite = r.setBoundsCheck(pts, n);
143 REPORTER_ASSERT(reporter, !isfinite);
144 REPORTER_ASSERT(reporter, r == zeror);
145
146 r.setBoundsNoCheck(pts, n);
147 if (r.isFinite())
148 r.setBoundsNoCheck(pts, n);
149 REPORTER_ASSERT(reporter, !r.isFinite());
150 }
151 }
152 }
153
make_big_value(skiatest::Reporter * reporter)154 static float make_big_value(skiatest::Reporter* reporter) {
155 // need to make a big value, one that will cause rect.width() to overflow to inf.
156 // however, the windows compiler wants about this if it can see the big value inlined.
157 // hence, this stupid trick to try to fool their compiler.
158 SkASSERT(reporter);
159 return reporter ? SK_ScalarMax * 0.75f : 0;
160 }
161
DEF_TEST(Rect_whOverflow,reporter)162 DEF_TEST(Rect_whOverflow, reporter) {
163 const SkScalar big = make_big_value(reporter);
164 const SkRect r = { -big, -big, big, big };
165
166 REPORTER_ASSERT(reporter, r.isFinite());
167 REPORTER_ASSERT(reporter, !SkScalarIsFinite(r.width()));
168 REPORTER_ASSERT(reporter, !SkScalarIsFinite(r.height()));
169
170 // ensure we can compute center even when the width/height might overflow
171 REPORTER_ASSERT(reporter, SkScalarIsFinite(r.centerX()));
172 REPORTER_ASSERT(reporter, SkScalarIsFinite(r.centerY()));
173
174
175 // ensure we can compute halfWidth and halfHeight even when width/height might overflow,
176 // i.e. for use computing the radii filling a rectangle.
177 REPORTER_ASSERT(reporter, SkScalarIsFinite(SkRectPriv::HalfWidth(r)));
178 REPORTER_ASSERT(reporter, SkScalarIsFinite(SkRectPriv::HalfHeight(r)));
179 }
180
DEF_TEST(Rect_subtract,reporter)181 DEF_TEST(Rect_subtract, reporter) {
182 struct Expectation {
183 SkIRect fA;
184 SkIRect fB;
185 SkIRect fExpected;
186 bool fExact;
187 };
188
189 SkIRect a = SkIRect::MakeLTRB(2, 3, 12, 15);
190 Expectation tests[] = {
191 // B contains A == empty rect
192 {a, a.makeOutset(2, 2), SkIRect::MakeEmpty(), true},
193 // A contains B, producing 4x12 (left), 2x12 (right), 4x10(top), and 5x10(bottom)
194 {a, {6, 6, 10, 10}, {2, 10, 12, 15}, false},
195 // A is empty, B is not == empty rect
196 {SkIRect::MakeEmpty(), a, SkIRect::MakeEmpty(), true},
197 // A is not empty, B is empty == a
198 {a, SkIRect::MakeEmpty(), a, true},
199 // A and B are empty == empty
200 {SkIRect::MakeEmpty(), SkIRect::MakeEmpty(), SkIRect::MakeEmpty(), true},
201 // A and B do not intersect == a
202 {a, {15, 17, 20, 40}, a, true},
203 // B cuts off left side of A, producing 6x12 (right)
204 {a, {0, 0, 6, 20}, {6, 3, 12, 15}, true},
205 // B cuts off right side of A, producing 4x12 (left)
206 {a, {6, 0, 20, 20}, {2, 3, 6, 15}, true},
207 // B cuts off top side of A, producing 10x9 (bottom)
208 {a, {0, 0, 20, 6}, {2, 6, 12, 15}, true},
209 // B cuts off bottom side of A, producing 10x7 (top)
210 {a, {0, 10, 20, 20}, {2, 3, 12, 10}, true},
211 // B splits A horizontally, producing 10x3 (top) or 10x5 (bottom)
212 {a, {0, 6, 20, 10}, {2, 10, 12, 15}, false},
213 // B splits A vertically, producing 4x12 (left) or 2x12 (right)
214 {a, {6, 0, 10, 20}, {2, 3, 6, 15}, false},
215 // B cuts top-left of A, producing 8x12 (right) or 10x11 (bottom)
216 {a, {0, 0, 4, 4}, {2, 4, 12, 15}, false},
217 // B cuts top-right of A, producing 8x12 (left) or 10x8 (bottom)
218 {a, {10, 0, 14, 7}, {2, 3, 10, 15}, false},
219 // B cuts bottom-left of A, producing 7x12 (right) or 10x9 (top)
220 {a, {0, 12, 5, 20}, {2, 3, 12, 12}, false},
221 // B cuts bottom-right of A, producing 8x12 (left) or 10x9 (top)
222 {a, {10, 12, 20, 20}, {2, 3, 10, 15}, false},
223 // B crosses the left of A, producing 4x12 (right) or 10x3 (top) or 10x5 (bottom)
224 {a, {0, 6, 8, 10}, {2, 10, 12, 15}, false},
225 // B crosses the right side of A, producing 6x12 (left) or 10x3 (top) or 10x5 (bottom)
226 {a, {8, 6, 20, 10}, {2, 3, 8, 15}, false},
227 // B crosses the top side of A, producing 4x12 (left) or 2x12 (right) or 10x8 (bottom)
228 {a, {6, 0, 10, 7}, {2, 7, 12, 15}, false},
229 // B crosses the bottom side of A, producing 1x12 (left) or 4x12 (right) or 10x3 (top)
230 {a, {4, 6, 8, 20}, {8, 3, 12, 15}, false}
231 };
232
233 for (const Expectation& e : tests) {
234 SkIRect difference;
235 bool exact = SkRectPriv::Subtract(e.fA, e.fB, &difference);
236 REPORTER_ASSERT(reporter, exact == e.fExact);
237 REPORTER_ASSERT(reporter, difference == e.fExpected);
238
239 // Generate equivalent tests for the SkRect case by moving the input rects by 0.5px
240 SkRect af = SkRect::Make(e.fA);
241 SkRect bf = SkRect::Make(e.fB);
242 SkRect ef = SkRect::Make(e.fExpected);
243 af.offset(0.5f, 0.5f);
244 bf.offset(0.5f, 0.5f);
245 ef.offset(0.5f, 0.5f);
246
247 SkRect df;
248 exact = SkRectPriv::Subtract(af, bf, &df);
249 REPORTER_ASSERT(reporter, exact == e.fExact);
250 REPORTER_ASSERT(reporter, (df.isEmpty() && ef.isEmpty()) || (df == ef));
251 }
252 }
253
DEF_TEST(Rect_subtract_overflow,reporter)254 DEF_TEST(Rect_subtract_overflow, reporter) {
255 // This rectangle is sorted but whose int32 width overflows and appears negative (so
256 // isEmpty() returns true).
257 SkIRect reallyBig = SkIRect::MakeLTRB(-INT_MAX + 1000, 0, INT_MAX - 1000, 100);
258 // However, because it's sorted, an intersection with a reasonably sized rectangle is still
259 // valid so the assumption that SkIRect::Intersects() returns false when either input is
260 // empty is invalid, leading to incorrect use of negative width (see crbug.com/1243206)
261 SkIRect reasonable = SkIRect::MakeLTRB(-50, -5, 50, 125);
262
263 // Ignoring overflow, "reallyBig - reasonable" should report exact = false and select either the
264 // left or right portion of 'reallyBig' that excludes 'reasonable', e.g.
265 // {-INT_MAX+1000, 0, -50, 100} or {150, 0, INT_MAX-1000, 100}.
266 // This used to assert, but now it should be detected that 'reallyBig' overflows and is
267 // technically empty, so the result should be itself and exact.
268 SkIRect difference;
269 bool exact = SkRectPriv::Subtract(reallyBig, reasonable, &difference);
270 REPORTER_ASSERT(reporter, exact);
271 REPORTER_ASSERT(reporter, difference == reallyBig);
272
273 // Similarly, if we subtract 'reallyBig', since it's technically empty then we expect the
274 // answer to remain 'reasonable'.
275 exact = SkRectPriv::Subtract(reasonable, reallyBig, &difference);
276 REPORTER_ASSERT(reporter, exact);
277 REPORTER_ASSERT(reporter, difference == reasonable);
278 }
279
280 // Before the fix, this sequence would trigger a release_assert in the Tiler
281 // in SkBitmapDevice.cpp
DEF_TEST(big_tiled_rect_crbug_927075,reporter)282 DEF_TEST(big_tiled_rect_crbug_927075, reporter) {
283 // since part of the regression test allocates a huge buffer, don't bother trying on
284 // 32-bit devices (e.g. chromecast) so we avoid them failing to allocated.
285
286 if (sizeof(void*) == 8) {
287 const int w = 67108863;
288 const int h = 1;
289 const auto info = SkImageInfo::MakeN32Premul(w, h);
290
291 auto surf = SkSurface::MakeRaster(info);
292 auto canvas = surf->getCanvas();
293
294 const SkRect r = { 257, 213, 67109120, 214 };
295 SkPaint paint;
296 paint.setAntiAlias(true);
297
298 canvas->translate(-r.fLeft, -r.fTop);
299 canvas->drawRect(r, paint);
300 }
301 }
302