• 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     SkPaint gradientPaint;
37     gradientPaint.setShader(SkGradientShader::MakeRadial(
38         center, radius, colors, pos, SK_ARRAY_COUNT(colors), SkTileMode::kClamp));
39 
40     // Test using the image filter
41     {
42         SkPaint paint;
43         paint.setImageFilter(SkImageFilters::Paint(gradientPaint, &ir));
44         canvasFilter.drawRect(SkRect::Make(ir), paint);
45     }
46 
47     // Test using the paint directly
48     {
49         canvasPaint.drawRect(SkRect::Make(ir), gradientPaint);
50     }
51 
52     // Assert that both paths yielded the same result
53     for (int y = 0; y < kHeight; ++y) {
54         const SkPMColor* filterPtr = filterResult.getAddr32(0, y);
55         const SkPMColor* paintPtr = paintResult.getAddr32(0, y);
56         for (int x = 0; x < kWidth; ++x, ++filterPtr, ++paintPtr) {
57             REPORTER_ASSERT(reporter, *filterPtr == *paintPtr);
58         }
59     }
60 }
61 
test_scaled(skiatest::Reporter * reporter)62 static void test_scaled(skiatest::Reporter* reporter) {
63     static const int kWidth = 10;
64     static const int kHeight = 10;
65 
66     SkIRect ir = SkIRect::MakeWH(kWidth, kHeight);
67 
68     SkBitmap filterResult, paintResult;
69 
70     filterResult.allocN32Pixels(kWidth, kHeight);
71     SkCanvas canvasFilter(filterResult);
72     canvasFilter.clear(0x00000000);
73 
74     paintResult.allocN32Pixels(kWidth, kHeight);
75     SkCanvas canvasPaint(paintResult);
76     canvasPaint.clear(0x00000000);
77 
78     SkPoint center = SkPoint::Make(SkIntToScalar(5), SkIntToScalar(5));
79     SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN};
80     SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1};
81     SkScalar radius = SkIntToScalar(5);
82 
83     SkPaint gradientPaint;
84     gradientPaint.setShader(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::Paint(gradientPaint, &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         canvasPaint.scale(SkIntToScalar(2), SkIntToScalar(2));
98         canvasPaint.drawRect(SkRect::Make(ir), gradientPaint);
99     }
100 
101     // Assert that both paths yielded the same result
102     for (int y = 0; y < kHeight; ++y) {
103         const SkPMColor* filterPtr = filterResult.getAddr32(0, y);
104         const SkPMColor* paintPtr = paintResult.getAddr32(0, y);
105         for (int x = 0; x < kWidth; ++x, ++filterPtr, ++paintPtr) {
106             REPORTER_ASSERT(reporter, *filterPtr == *paintPtr);
107         }
108     }
109 }
110 
DEF_TEST(PaintImageFilter,reporter)111 DEF_TEST(PaintImageFilter, reporter) {
112     test_unscaled(reporter);
113     test_scaled(reporter);
114 }
115