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