OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "sk_tool_utils.h" | 8 #include "sk_tool_utils.h" |
9 #include "SkBlurImageFilter.h" | 9 #include "SkBlurImageFilter.h" |
10 #include "SkColor.h" | 10 #include "SkColor.h" |
11 #include "SkDisplacementMapEffect.h" | 11 #include "SkDisplacementMapEffect.h" |
12 #include "SkDropShadowImageFilter.h" | 12 #include "SkDropShadowImageFilter.h" |
13 #include "SkGradientShader.h" | 13 #include "SkGradientShader.h" |
14 #include "SkImage.h" | 14 #include "SkImage.h" |
15 #include "SkImageSource.h" | 15 #include "SkImageSource.h" |
16 #include "SkLightingImageFilter.h" | 16 #include "SkLightingImageFilter.h" |
17 #include "SkMorphologyImageFilter.h" | 17 #include "SkMorphologyImageFilter.h" |
18 #include "SkOffsetImageFilter.h" | 18 #include "SkOffsetImageFilter.h" |
19 #include "SkPaintImageFilter.h" | 19 #include "SkPaintImageFilter.h" |
20 #include "SkPerlinNoiseShader.h" | 20 #include "SkPerlinNoiseShader.h" |
21 #include "SkPoint3.h" | 21 #include "SkPoint3.h" |
22 #include "SkScalar.h" | 22 #include "SkScalar.h" |
23 #include "SkSurface.h" | 23 #include "SkSurface.h" |
24 #include "gm.h" | 24 #include "gm.h" |
25 | 25 |
26 #define RESIZE_FACTOR_X SkIntToScalar(2) | 26 #define RESIZE_FACTOR_X SkIntToScalar(2) |
27 #define RESIZE_FACTOR_Y SkIntToScalar(5) | 27 #define RESIZE_FACTOR_Y SkIntToScalar(5) |
28 | 28 |
| 29 static sk_sp<SkImage> make_gradient_circle(int width, int height) { |
| 30 SkScalar x = SkIntToScalar(width / 2); |
| 31 SkScalar y = SkIntToScalar(height / 2); |
| 32 SkScalar radius = SkMinScalar(x, y) * 0.8f; |
| 33 auto surface(SkSurface::MakeRasterN32Premul(width, height)); |
| 34 SkCanvas* canvas = surface->getCanvas(); |
| 35 canvas->clear(0x00000000); |
| 36 SkColor colors[2]; |
| 37 colors[0] = SK_ColorWHITE; |
| 38 colors[1] = SK_ColorBLACK; |
| 39 SkPaint paint; |
| 40 paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(x, y), radius, co
lors, nullptr, |
| 41 2, SkShader::kClamp_TileMode)); |
| 42 canvas->drawCircle(x, y, radius, paint); |
| 43 return surface->makeImageSnapshot(); |
| 44 } |
| 45 |
| 46 static void draw_clipped_filter(SkCanvas* canvas, SkImageFilter* filter, size_t
i, |
| 47 const SkRect& primBounds, const SkRect& clipBoun
ds) { |
| 48 SkPaint paint; |
| 49 paint.setColor(SK_ColorWHITE); |
| 50 paint.setImageFilter(filter); |
| 51 paint.setAntiAlias(true); |
| 52 canvas->save(); |
| 53 canvas->clipRect(clipBounds); |
| 54 if (5 == i) { |
| 55 canvas->translate(SkIntToScalar(16), SkIntToScalar(-32)); |
| 56 } else if (6 == i) { |
| 57 canvas->scale(SkScalarInvert(RESIZE_FACTOR_X), SkScalarInvert(RESIZE_FAC
TOR_Y)); |
| 58 } |
| 59 canvas->drawCircle(primBounds.centerX(), primBounds.centerY(), |
| 60 primBounds.width() * 2 / 5, paint); |
| 61 canvas->restore(); |
| 62 } |
| 63 |
29 namespace skiagm { | 64 namespace skiagm { |
30 | 65 |
31 class ImageFiltersClippedGM : public GM { | 66 class ImageFiltersClippedGM : public GM { |
32 public: | 67 public: |
33 ImageFiltersClippedGM() { | 68 ImageFiltersClippedGM() { |
34 this->setBGColor(0x00000000); | 69 this->setBGColor(0x00000000); |
35 } | 70 } |
36 | 71 |
37 protected: | 72 protected: |
38 SkString onShortName() override { | 73 SkString onShortName() override { |
39 return SkString("imagefiltersclipped"); | 74 return SkString("imagefiltersclipped"); |
40 } | 75 } |
41 | 76 |
42 SkISize onISize() override { | 77 SkISize onISize() override { |
43 return SkISize::Make(860, 500); | 78 return SkISize::Make(860, 500); |
44 } | 79 } |
45 | 80 |
46 void makeGradientCircle(int width, int height) { | |
47 SkScalar x = SkIntToScalar(width / 2); | |
48 SkScalar y = SkIntToScalar(height / 2); | |
49 SkScalar radius = SkMinScalar(x, y) * 0.8f; | |
50 auto surface(SkSurface::MakeRasterN32Premul(width, height)); | |
51 SkCanvas* canvas = surface->getCanvas(); | |
52 canvas->clear(0x00000000); | |
53 SkColor colors[2]; | |
54 colors[0] = SK_ColorWHITE; | |
55 colors[1] = SK_ColorBLACK; | |
56 SkPaint paint; | |
57 paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(x, y), radius
, colors, nullptr, | |
58 2, SkShader::kClamp_TileMod
e)); | |
59 canvas->drawCircle(x, y, radius, paint); | |
60 fGradientCircle = surface->makeImageSnapshot(); | |
61 } | |
62 | |
63 static void draw_clipped_filter(SkCanvas* canvas, SkImageFilter* filter, siz
e_t i, | |
64 const SkRect& primBounds, const SkRect& clip
Bounds) { | |
65 SkPaint paint; | |
66 paint.setColor(SK_ColorWHITE); | |
67 paint.setImageFilter(filter); | |
68 paint.setAntiAlias(true); | |
69 canvas->save(); | |
70 canvas->clipRect(clipBounds); | |
71 if (5 == i) { | |
72 canvas->translate(SkIntToScalar(16), SkIntToScalar(-32)); | |
73 } else if (6 == i) { | |
74 canvas->scale(SkScalarInvert(RESIZE_FACTOR_X), | |
75 SkScalarInvert(RESIZE_FACTOR_Y)); | |
76 } | |
77 canvas->drawCircle(primBounds.centerX(), primBounds.centerY(), | |
78 primBounds.width() * 2 / 5, paint); | |
79 canvas->restore(); | |
80 } | |
81 | |
82 void onOnceBeforeDraw() override { | 81 void onOnceBeforeDraw() override { |
83 fCheckerboard = SkImage::MakeFromBitmap | 82 fCheckerboard = SkImage::MakeFromBitmap |
84 (sk_tool_utils::create_checkerboard_bitmap(64, 64, 0xFFA0A0A0, 0xFF4
04040, 8)); | 83 (sk_tool_utils::create_checkerboard_bitmap(64, 64, 0xFFA0A0A0, 0xFF4
04040, 8)); |
85 this->makeGradientCircle(64, 64); | 84 fGradientCircle = make_gradient_circle(64, 64); |
86 } | 85 } |
87 | 86 |
88 void onDraw(SkCanvas* canvas) override { | 87 void onDraw(SkCanvas* canvas) override { |
89 canvas->clear(SK_ColorBLACK); | 88 canvas->clear(SK_ColorBLACK); |
90 | 89 |
91 SkAutoTUnref<SkImageFilter> gradient(SkImageSource::Create(fGradientCirc
le.get())); | 90 sk_sp<SkImageFilter> gradient(SkImageSource::Make(fGradientCircle)); |
92 SkAutoTUnref<SkImageFilter> checkerboard(SkImageSource::Create(fCheckerb
oard.get())); | 91 sk_sp<SkImageFilter> checkerboard(SkImageSource::Make(fCheckerboard)); |
93 sk_sp<SkShader> noise(SkPerlinNoiseShader::MakeFractalNoise( | |
94 SkDoubleToScalar(0.1), SkDoubleToScalar(0.05), 1, 0)); | |
95 SkMatrix resizeMatrix; | 92 SkMatrix resizeMatrix; |
96 resizeMatrix.setScale(RESIZE_FACTOR_X, RESIZE_FACTOR_Y); | 93 resizeMatrix.setScale(RESIZE_FACTOR_X, RESIZE_FACTOR_Y); |
97 SkPoint3 pointLocation = SkPoint3::Make(32, 32, SkIntToScalar(10)); | 94 SkPoint3 pointLocation = SkPoint3::Make(32, 32, SkIntToScalar(10)); |
98 | 95 |
99 SkImageFilter* filters[] = { | 96 SkImageFilter* filters[] = { |
100 SkBlurImageFilter::Create(SkIntToScalar(12), SkIntToScalar(12)), | 97 SkBlurImageFilter::Create(SkIntToScalar(12), SkIntToScalar(12)), |
101 SkDropShadowImageFilter::Create(SkIntToScalar(10), SkIntToScalar(10)
, | 98 SkDropShadowImageFilter::Create(SkIntToScalar(10), SkIntToScalar(10)
, |
102 SkIntToScalar(3), SkIntToScalar(3), SK_ColorGREEN, | 99 SkIntToScalar(3), SkIntToScalar(3), SK_ColorGREEN, |
103 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode), | 100 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode), |
104 SkDisplacementMapEffect::Create(SkDisplacementMapEffect::kR_ChannelS
electorType, | 101 SkDisplacementMapEffect::Create(SkDisplacementMapEffect::kR_ChannelS
electorType, |
105 SkDisplacementMapEffect::kR_ChannelS
electorType, | 102 SkDisplacementMapEffect::kR_ChannelS
electorType, |
106 SkIntToScalar(12), | 103 SkIntToScalar(12), |
107 gradient.get(), | 104 gradient.get(), |
108 checkerboard.get()), | 105 checkerboard.get()), |
109 SkDilateImageFilter::Create(2, 2, checkerboard.get()), | 106 SkDilateImageFilter::Create(2, 2, checkerboard.get()), |
110 SkErodeImageFilter::Create(2, 2, checkerboard.get()), | 107 SkErodeImageFilter::Create(2, 2, checkerboard.get()), |
111 SkOffsetImageFilter::Make(SkIntToScalar(-16), | 108 SkOffsetImageFilter::Make(SkIntToScalar(-16), |
112 SkIntToScalar(32), | 109 SkIntToScalar(32), |
113 nullptr).release(), | 110 nullptr).release(), |
114 SkImageFilter::CreateMatrixFilter(resizeMatrix, kNone_SkFilterQualit
y), | 111 SkImageFilter::CreateMatrixFilter(resizeMatrix, kNone_SkFilterQualit
y), |
115 SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, SK_Color
WHITE, SK_Scalar1, SkIntToScalar(2), checkerboard.get()), | 112 SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, SK_Color
WHITE, SK_Scalar1, |
| 113 SkIntToScalar(2), check
erboard.get()), |
116 | 114 |
117 }; | 115 }; |
118 | 116 |
119 SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); | 117 SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); |
120 SkScalar margin = SkIntToScalar(16); | 118 SkScalar margin = SkIntToScalar(16); |
121 SkRect bounds = r; | 119 SkRect bounds = r; |
122 bounds.outset(margin, margin); | 120 bounds.outset(margin, margin); |
123 | 121 |
124 canvas->save(); | 122 canvas->save(); |
125 for (int xOffset = 0; xOffset < 80; xOffset += 16) { | 123 for (int xOffset = 0; xOffset < 80; xOffset += 16) { |
126 canvas->save(); | 124 canvas->save(); |
127 bounds.fLeft = SkIntToScalar(xOffset); | 125 bounds.fLeft = SkIntToScalar(xOffset); |
128 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { | 126 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
129 draw_clipped_filter(canvas, filters[i], i, r, bounds); | 127 draw_clipped_filter(canvas, filters[i], i, r, bounds); |
130 canvas->translate(r.width() + margin, 0); | 128 canvas->translate(r.width() + margin, 0); |
131 } | 129 } |
132 canvas->restore(); | 130 canvas->restore(); |
133 canvas->translate(0, r.height() + margin); | 131 canvas->translate(0, r.height() + margin); |
134 } | 132 } |
135 canvas->restore(); | 133 canvas->restore(); |
136 | 134 |
137 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { | 135 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
138 SkSafeUnref(filters[i]); | 136 SkSafeUnref(filters[i]); |
139 } | 137 } |
140 | 138 |
141 SkPaint noisePaint; | 139 SkPaint noisePaint; |
142 noisePaint.setShader(noise); | 140 noisePaint.setShader(SkPerlinNoiseShader::MakeFractalNoise(0.1f, 0.05f,
1, 0)); |
| 141 |
143 sk_sp<SkImageFilter> rectFilter(SkPaintImageFilter::Make(noisePaint)); | 142 sk_sp<SkImageFilter> rectFilter(SkPaintImageFilter::Make(noisePaint)); |
144 canvas->translate(SK_ARRAY_COUNT(filters)*(r.width() + margin), 0); | 143 canvas->translate(SK_ARRAY_COUNT(filters)*(r.width() + margin), 0); |
145 for (int xOffset = 0; xOffset < 80; xOffset += 16) { | 144 for (int xOffset = 0; xOffset < 80; xOffset += 16) { |
146 bounds.fLeft = SkIntToScalar(xOffset); | 145 bounds.fLeft = SkIntToScalar(xOffset); |
147 draw_clipped_filter(canvas, rectFilter.get(), 0, r, bounds); | 146 draw_clipped_filter(canvas, rectFilter.get(), 0, r, bounds); |
148 canvas->translate(0, r.height() + margin); | 147 canvas->translate(0, r.height() + margin); |
149 } | 148 } |
150 } | 149 } |
151 | 150 |
152 private: | 151 private: |
153 sk_sp<SkImage> fCheckerboard, fGradientCircle; | 152 sk_sp<SkImage> fCheckerboard, fGradientCircle; |
154 | 153 |
155 typedef GM INHERITED; | 154 typedef GM INHERITED; |
156 }; | 155 }; |
157 | 156 |
158 ////////////////////////////////////////////////////////////////////////////// | 157 ////////////////////////////////////////////////////////////////////////////// |
159 | 158 |
160 DEF_GM(return new ImageFiltersClippedGM;) | 159 DEF_GM(return new ImageFiltersClippedGM;) |
161 } | 160 } |
OLD | NEW |