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