blob: 1ab93d1c7d8e0c8fa4ad767336765c20054018da [file] [log] [blame]
fmalita1a178ca2015-01-15 06:01:23 -08001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
fmalita1a178ca2015-01-15 06:01:23 -080014
15static 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
27static 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
aleksandar.stojiljkovic76001832015-11-02 13:28:51 -080051static 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
fmalita1a178ca2015-01-15 06:01:23 -080089DEF_TEST(Rect, reporter) {
90 test_stroke_width_clipping(reporter);
aleksandar.stojiljkovic76001832015-11-02 13:28:51 -080091 test_skbug4406(reporter);
fmalita1a178ca2015-01-15 06:01:23 -080092}
Mike Reed185ffe92018-01-08 17:09:54 -050093
94DEF_TEST(Rect_grow, reporter) {
95 test_stroke_width_clipping(reporter);
96 test_skbug4406(reporter);
97}
Mike Reedf5817772018-01-09 15:36:51 -050098
Mike Reed926e1932018-01-29 15:56:11 -050099DEF_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
Mike Reedf5817772018-01-09 15:36:51 -0500107DEF_TEST(Rect_largest, reporter) {
Mike Reed8008df12018-01-17 12:20:04 -0500108 REPORTER_ASSERT(reporter, !SkRectPriv::MakeILarge().isEmpty());
Mike Reedf5817772018-01-09 15:36:51 -0500109 REPORTER_ASSERT(reporter, SkRectPriv::MakeILargestInverted().isEmpty());
110
111 REPORTER_ASSERT(reporter, !SkRectPriv::MakeLargest().isEmpty());
Mike Reed8008df12018-01-17 12:20:04 -0500112 REPORTER_ASSERT(reporter, !SkRectPriv::MakeLargeS32().isEmpty());
Mike Reedf5817772018-01-09 15:36:51 -0500113 REPORTER_ASSERT(reporter, SkRectPriv::MakeLargestInverted().isEmpty());
114}
115
Cary Clarkc06754b2018-05-16 21:28:55 -0400116/*
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 */
121DEF_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}
Mike Reedd2e74b82018-05-24 10:39:57 -0400142
143static 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
151DEF_TEST(Rect_center, reporter) {
152 // ensure we can compute center even when the width/height might overflow
153 const SkScalar big = make_big_value(reporter);
154 const SkRect r = { -big, -big, big, big };
155
156 REPORTER_ASSERT(reporter, r.isFinite());
157 REPORTER_ASSERT(reporter, SkScalarIsFinite(r.centerX()));
158 REPORTER_ASSERT(reporter, SkScalarIsFinite(r.centerY()));
159 REPORTER_ASSERT(reporter, !SkScalarIsFinite(r.width()));
160 REPORTER_ASSERT(reporter, !SkScalarIsFinite(r.height()));
161}
162
Michael Ludwig9e1e9132020-04-15 15:26:05 -0400163DEF_TEST(Rect_subtract, reporter) {
164 struct Expectation {
165 SkIRect fA;
166 SkIRect fB;
167 SkIRect fExpected;
168 bool fExact;
169 };
170
171 SkIRect a = SkIRect::MakeLTRB(2, 3, 12, 15);
172 Expectation tests[] = {
173 // B contains A == empty rect
174 {a, a.makeOutset(2, 2), SkIRect::MakeEmpty(), true},
175 // A contains B, producing 4x12 (left), 2x12 (right), 4x10(top), and 5x10(bottom)
176 {a, {6, 6, 10, 10}, {2, 10, 12, 15}, false},
177 // A is empty, B is not == empty rect
178 {SkIRect::MakeEmpty(), a, SkIRect::MakeEmpty(), true},
179 // A is not empty, B is empty == a
180 {a, SkIRect::MakeEmpty(), a, true},
181 // A and B are empty == empty
182 {SkIRect::MakeEmpty(), SkIRect::MakeEmpty(), SkIRect::MakeEmpty(), true},
183 // A and B do not intersect == a
184 {a, {15, 17, 20, 40}, a, true},
185 // B cuts off left side of A, producing 6x12 (right)
186 {a, {0, 0, 6, 20}, {6, 3, 12, 15}, true},
187 // B cuts off right side of A, producing 4x12 (left)
188 {a, {6, 0, 20, 20}, {2, 3, 6, 15}, true},
189 // B cuts off top side of A, producing 10x9 (bottom)
190 {a, {0, 0, 20, 6}, {2, 6, 12, 15}, true},
191 // B cuts off bottom side of A, producing 10x7 (top)
192 {a, {0, 10, 20, 20}, {2, 3, 12, 10}, true},
193 // B splits A horizontally, producing 10x3 (top) or 10x5 (bottom)
194 {a, {0, 6, 20, 10}, {2, 10, 12, 15}, false},
195 // B splits A vertically, producing 4x12 (left) or 2x12 (right)
196 {a, {6, 0, 10, 20}, {2, 3, 6, 15}, false},
197 // B cuts top-left of A, producing 8x12 (right) or 10x11 (bottom)
198 {a, {0, 0, 4, 4}, {2, 4, 12, 15}, false},
199 // B cuts top-right of A, producing 8x12 (left) or 10x8 (bottom)
200 {a, {10, 0, 14, 7}, {2, 3, 10, 15}, false},
201 // B cuts bottom-left of A, producing 7x12 (right) or 10x9 (top)
202 {a, {0, 12, 5, 20}, {2, 3, 12, 12}, false},
203 // B cuts bottom-right of A, producing 8x12 (left) or 10x9 (top)
204 {a, {10, 12, 20, 20}, {2, 3, 10, 15}, false},
205 // B crosses the left of A, producing 4x12 (right) or 10x3 (top) or 10x5 (bottom)
206 {a, {0, 6, 8, 10}, {2, 10, 12, 15}, false},
207 // B crosses the right side of A, producing 6x12 (left) or 10x3 (top) or 10x5 (bottom)
208 {a, {8, 6, 20, 10}, {2, 3, 8, 15}, false},
209 // B crosses the top side of A, producing 4x12 (left) or 2x12 (right) or 10x8 (bottom)
210 {a, {6, 0, 10, 7}, {2, 7, 12, 15}, false},
211 // B crosses the bottom side of A, producing 1x12 (left) or 4x12 (right) or 10x3 (top)
212 {a, {4, 6, 8, 20}, {8, 3, 12, 15}, false}
213 };
214
215 for (const Expectation& e : tests) {
216 SkIRect difference;
217 bool exact = SkRectPriv::Subtract(e.fA, e.fB, &difference);
218 REPORTER_ASSERT(reporter, exact == e.fExact);
219 REPORTER_ASSERT(reporter, difference == e.fExpected);
220
221 // Generate equivalent tests for the SkRect case by moving the input rects by 0.5px
222 SkRect af = SkRect::Make(e.fA);
223 SkRect bf = SkRect::Make(e.fB);
224 SkRect ef = SkRect::Make(e.fExpected);
225 af.offset(0.5f, 0.5f);
226 bf.offset(0.5f, 0.5f);
227 ef.offset(0.5f, 0.5f);
228
229 SkRect df;
230 exact = SkRectPriv::Subtract(af, bf, &df);
231 REPORTER_ASSERT(reporter, exact == e.fExact);
232 REPORTER_ASSERT(reporter, (df.isEmpty() && ef.isEmpty()) || (df == ef));
233 }
234}
235
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500236#include "include/core/SkSurface.h"
Mike Reed1c205232019-01-31 14:08:25 -0500237
238// Before the fix, this sequence would trigger a release_assert in the Tiler
239// in SkBitmapDevice.cpp
240DEF_TEST(big_tiled_rect_crbug_927075, reporter) {
Mike Reed1a0126f2019-02-01 08:28:36 -0500241 // since part of the regression test allocates a huge buffer, don't bother trying on
242 // 32-bit devices (e.g. chromecast) so we avoid them failing to allocated.
Mike Reed1c205232019-01-31 14:08:25 -0500243
Mike Reed1a0126f2019-02-01 08:28:36 -0500244 if (sizeof(void*) == 8) {
245 const int w = 67108863;
246 const int h = 1;
247 const auto info = SkImageInfo::MakeN32Premul(w, h);
Mike Reed1c205232019-01-31 14:08:25 -0500248
Mike Reed1a0126f2019-02-01 08:28:36 -0500249 auto surf = SkSurface::MakeRaster(info);
250 auto canvas = surf->getCanvas();
Mike Reed1c205232019-01-31 14:08:25 -0500251
Mike Reed1a0126f2019-02-01 08:28:36 -0500252 const SkRect r = { 257, 213, 67109120, 214 };
253 SkPaint paint;
254 paint.setAntiAlias(true);
255
256 canvas->translate(-r.fLeft, -r.fTop);
257 canvas->drawRect(r, paint);
258 }
Mike Reed1c205232019-01-31 14:08:25 -0500259}