blob: 2594c53cac48f7a3d77b00ba7009a4624f29e978 [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
8#include "SkBitmap.h"
9#include "SkCanvas.h"
Mike Reed926e1932018-01-29 15:56:11 -050010#include "SkPath.h"
fmalita1a178ca2015-01-15 06:01:23 -080011#include "SkRect.h"
Mike Reedf5817772018-01-09 15:36:51 -050012#include "SkRectPriv.h"
fmalita1a178ca2015-01-15 06:01:23 -080013#include "Test.h"
14
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