1 /*
2  * Copyright 2016 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 "SkCanvas.h"
9 #include "SkGradientShader.h"
10 #include "SkPaintImageFilter.h"
11 #include "SkShader.h"
12 #include "Test.h"
13 
test_unscaled(skiatest::Reporter * reporter)14 static void test_unscaled(skiatest::Reporter* reporter) {
15     int w = 10, h = 10;
16     SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h));
17 
18     SkBitmap filterResult, paintResult;
19 
20     filterResult.allocN32Pixels(w, h);
21     SkCanvas canvasFilter(filterResult);
22     canvasFilter.clear(0x00000000);
23 
24     paintResult.allocN32Pixels(w, h);
25     SkCanvas canvasPaint(paintResult);
26     canvasPaint.clear(0x00000000);
27 
28     SkPoint center = SkPoint::Make(SkIntToScalar(5), SkIntToScalar(5));
29     SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN};
30     SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1};
31     SkScalar radius = SkIntToScalar(5);
32 
33     SkAutoTUnref<SkShader> s(SkGradientShader::CreateRadial(
34         center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode));
35     SkPaint gradientPaint;
36     gradientPaint.setShader(s);
37 
38     // Test using the image filter
39     {
40         SkPaint paint;
41         SkImageFilter::CropRect cr(r);
42         paint.setImageFilter(SkPaintImageFilter::Create(gradientPaint, &cr))->unref();
43         canvasFilter.drawRect(r, paint);
44     }
45 
46     // Test using the paint directly
47     {
48         canvasPaint.drawRect(r, gradientPaint);
49     }
50 
51     // Assert that both paths yielded the same result
52     for (int y = 0; y < r.height(); ++y) {
53         const SkPMColor* filterPtr = filterResult.getAddr32(0, y);
54         const SkPMColor* paintPtr = paintResult.getAddr32(0, y);
55         for (int x = 0; x < r.width(); ++x, ++filterPtr, ++paintPtr) {
56             REPORTER_ASSERT(reporter, *filterPtr == *paintPtr);
57         }
58     }
59 }
60 
test_scaled(skiatest::Reporter * reporter)61 static void test_scaled(skiatest::Reporter* reporter) {
62     int w = 10, h = 10;
63     SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h));
64 
65     SkBitmap filterResult, paintResult;
66 
67     filterResult.allocN32Pixels(w, h);
68     SkCanvas canvasFilter(filterResult);
69     canvasFilter.clear(0x00000000);
70 
71     paintResult.allocN32Pixels(w, h);
72     SkCanvas canvasPaint(paintResult);
73     canvasPaint.clear(0x00000000);
74 
75     SkPoint center = SkPoint::Make(SkIntToScalar(5), SkIntToScalar(5));
76     SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN};
77     SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1};
78     SkScalar radius = SkIntToScalar(5);
79 
80     SkAutoTUnref<SkShader> s(SkGradientShader::CreateRadial(
81         center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode));
82     SkPaint gradientPaint;
83     gradientPaint.setShader(s);
84 
85     // Test using the image filter
86     {
87         SkPaint paint;
88         SkImageFilter::CropRect cr(r);
89         paint.setImageFilter(SkPaintImageFilter::Create(gradientPaint, &cr))->unref();
90         canvasFilter.scale(SkIntToScalar(2), SkIntToScalar(2));
91         canvasFilter.drawRect(r, paint);
92     }
93 
94     // Test using the paint directly
95     {
96         canvasPaint.scale(SkIntToScalar(2), SkIntToScalar(2));
97         canvasPaint.drawRect(r, gradientPaint);
98     }
99 
100     // Assert that both paths yielded the same result
101     for (int y = 0; y < r.height(); ++y) {
102         const SkPMColor* filterPtr = filterResult.getAddr32(0, y);
103         const SkPMColor* paintPtr = paintResult.getAddr32(0, y);
104         for (int x = 0; x < r.width(); ++x, ++filterPtr, ++paintPtr) {
105             REPORTER_ASSERT(reporter, *filterPtr == *paintPtr);
106         }
107     }
108 }
109 
DEF_TEST(PaintImageFilter,reporter)110 DEF_TEST(PaintImageFilter, reporter) {
111     test_unscaled(reporter);
112     test_scaled(reporter);
113 }
114