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 "SkBitmap.h"
9 #include "SkCanvas.h"
10 #include "SkPath.h"
11 #include "SkRect.h"
12 #include "SkRectPriv.h"
13 #include "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