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 "SkRect.h"
11 #include "Test.h"
12 
has_green_pixels(const SkBitmap & bm)13 static bool has_green_pixels(const SkBitmap& bm) {
14     for (int j = 0; j < bm.height(); ++j) {
15         for (int i = 0; i < bm.width(); ++i) {
16             if (SkColorGetG(bm.getColor(i, j))) {
17                 return true;
18             }
19         }
20     }
21 
22     return false;
23 }
24 
test_stroke_width_clipping(skiatest::Reporter * reporter)25 static void test_stroke_width_clipping(skiatest::Reporter* reporter) {
26     SkBitmap bm;
27     bm.allocN32Pixels(100, 10);
28     bm.eraseColor(SK_ColorTRANSPARENT);
29 
30     SkCanvas canvas(bm);
31     SkPaint paint;
32     paint.setStyle(SkPaint::kStroke_Style);
33     paint.setStrokeWidth(10);
34     paint.setColor(0xff00ff00);
35 
36     // clip out the left half of our canvas
37     canvas.clipRect(SkRect::MakeXYWH(51, 0, 49, 100));
38 
39     // no stroke bleed should be visible
40     canvas.drawRect(SkRect::MakeWH(44, 100), paint);
41     REPORTER_ASSERT(reporter, !has_green_pixels(bm));
42 
43     // right stroke edge should bleed into the visible area
44     canvas.scale(2, 2);
45     canvas.drawRect(SkRect::MakeWH(22, 50), paint);
46     REPORTER_ASSERT(reporter, has_green_pixels(bm));
47 }
48 
test_skbug4406(skiatest::Reporter * reporter)49 static void test_skbug4406(skiatest::Reporter* reporter) {
50     SkBitmap bm;
51     bm.allocN32Pixels(10, 10);
52     bm.eraseColor(SK_ColorTRANSPARENT);
53 
54     SkCanvas canvas(bm);
55     const SkRect r = { 1.5f, 1, 3.5f, 3 };
56     // draw filled green rect first
57     SkPaint paint;
58     paint.setStyle(SkPaint::kFill_Style);
59     paint.setColor(0xff00ff00);
60     paint.setStrokeWidth(1);
61     paint.setAntiAlias(true);
62     canvas.drawRect(r, paint);
63 
64     // paint black with stroke rect (that asserts in bug 4406)
65     // over the filled rect, it should cover it
66     paint.setStyle(SkPaint::kStroke_Style);
67     paint.setColor(0xff000000);
68     paint.setStrokeWidth(1);
69     canvas.drawRect(r, paint);
70     REPORTER_ASSERT(reporter, !has_green_pixels(bm));
71 
72     // do it again with thinner stroke
73     paint.setStyle(SkPaint::kFill_Style);
74     paint.setColor(0xff00ff00);
75     paint.setStrokeWidth(1);
76     paint.setAntiAlias(true);
77     canvas.drawRect(r, paint);
78     // paint black with stroke rect (that asserts in bug 4406)
79     // over the filled rect, it doesnt cover it completelly with thinner stroke
80     paint.setStyle(SkPaint::kStroke_Style);
81     paint.setColor(0xff000000);
82     paint.setStrokeWidth(0.99f);
83     canvas.drawRect(r, paint);
84     REPORTER_ASSERT(reporter, has_green_pixels(bm));
85 }
86 
DEF_TEST(Rect,reporter)87 DEF_TEST(Rect, reporter) {
88     test_stroke_width_clipping(reporter);
89     test_skbug4406(reporter);
90 }
91