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