1 /*
2 * Copyright 2014 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 "SkBlurImageFilter.h"
9 #include "SkColor.h"
10 #include "SkDisplacementMapEffect.h"
11 #include "SkDropShadowImageFilter.h"
12 #include "SkGradientShader.h"
13 #include "SkImage.h"
14 #include "SkImageSource.h"
15 #include "SkLightingImageFilter.h"
16 #include "SkMorphologyImageFilter.h"
17 #include "SkOffsetImageFilter.h"
18 #include "SkPaintImageFilter.h"
19 #include "SkPerlinNoiseShader.h"
20 #include "SkPoint3.h"
21 #include "SkScalar.h"
22 #include "SkSurface.h"
23 #include "gm.h"
24 #include "sk_tool_utils.h"
25
26 #define RESIZE_FACTOR_X SkIntToScalar(2)
27 #define RESIZE_FACTOR_Y SkIntToScalar(5)
28
make_gradient_circle(int width,int height)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, colors, nullptr,
41 2, SkShader::kClamp_TileMode));
42 canvas->drawCircle(x, y, radius, paint);
43 return surface->makeImageSnapshot();
44 }
45
draw_clipped_filter(SkCanvas * canvas,sk_sp<SkImageFilter> filter,size_t i,const SkRect & primBounds,const SkRect & clipBounds)46 static void draw_clipped_filter(SkCanvas* canvas, sk_sp<SkImageFilter> filter, size_t i,
47 const SkRect& primBounds, const SkRect& clipBounds) {
48 SkPaint paint;
49 paint.setColor(SK_ColorWHITE);
50 paint.setImageFilter(std::move(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_FACTOR_Y));
58 }
59 canvas->drawCircle(primBounds.centerX(), primBounds.centerY(),
60 primBounds.width() * 2 / 5, paint);
61 canvas->restore();
62 }
63
64 namespace skiagm {
65
66 class ImageFiltersClippedGM : public GM {
67 public:
ImageFiltersClippedGM()68 ImageFiltersClippedGM() {
69 this->setBGColor(0x00000000);
70 }
71
72 protected:
onShortName()73 SkString onShortName() override {
74 return SkString("imagefiltersclipped");
75 }
76
onISize()77 SkISize onISize() override {
78 return SkISize::Make(860, 500);
79 }
80
onOnceBeforeDraw()81 void onOnceBeforeDraw() override {
82 fCheckerboard = SkImage::MakeFromBitmap
83 (sk_tool_utils::create_checkerboard_bitmap(64, 64, 0xFFA0A0A0, 0xFF404040, 8));
84 fGradientCircle = make_gradient_circle(64, 64);
85 }
86
onDraw(SkCanvas * canvas)87 void onDraw(SkCanvas* canvas) override {
88 canvas->clear(SK_ColorBLACK);
89
90 sk_sp<SkImageFilter> gradient(SkImageSource::Make(fGradientCircle));
91 sk_sp<SkImageFilter> checkerboard(SkImageSource::Make(fCheckerboard));
92 SkMatrix resizeMatrix;
93 resizeMatrix.setScale(RESIZE_FACTOR_X, RESIZE_FACTOR_Y);
94 SkPoint3 pointLocation = SkPoint3::Make(32, 32, SkIntToScalar(10));
95
96 sk_sp<SkImageFilter> filters[] = {
97 SkBlurImageFilter::Make(SkIntToScalar(12), SkIntToScalar(12), nullptr),
98 SkDropShadowImageFilter::Make(
99 SkIntToScalar(10), SkIntToScalar(10),
100 SkIntToScalar(3), SkIntToScalar(3), SK_ColorGREEN,
101 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
102 nullptr),
103 SkDisplacementMapEffect::Make(SkDisplacementMapEffect::kR_ChannelSelectorType,
104 SkDisplacementMapEffect::kR_ChannelSelectorType,
105 SkIntToScalar(12),
106 std::move(gradient),
107 checkerboard),
108 SkDilateImageFilter::Make(2, 2, checkerboard),
109 SkErodeImageFilter::Make(2, 2, checkerboard),
110 SkOffsetImageFilter::Make(SkIntToScalar(-16), SkIntToScalar(32), nullptr),
111 SkImageFilter::MakeMatrixFilter(resizeMatrix, kNone_SkFilterQuality, nullptr),
112 SkLightingImageFilter::MakePointLitDiffuse(pointLocation, SK_ColorWHITE, SK_Scalar1,
113 SkIntToScalar(2), checkerboard),
114
115 };
116
117 SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
118 SkScalar margin = SkIntToScalar(16);
119 SkRect bounds = r;
120 bounds.outset(margin, margin);
121
122 canvas->save();
123 for (int xOffset = 0; xOffset < 80; xOffset += 16) {
124 canvas->save();
125 bounds.fLeft = SkIntToScalar(xOffset);
126 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
127 draw_clipped_filter(canvas, filters[i], i, r, bounds);
128 canvas->translate(r.width() + margin, 0);
129 }
130 canvas->restore();
131 canvas->translate(0, r.height() + margin);
132 }
133 canvas->restore();
134
135 SkPaint noisePaint;
136 noisePaint.setShader(SkPerlinNoiseShader::MakeFractalNoise(0.1f, 0.05f, 1, 0));
137
138 sk_sp<SkImageFilter> rectFilter(SkPaintImageFilter::Make(noisePaint));
139 canvas->translate(SK_ARRAY_COUNT(filters)*(r.width() + margin), 0);
140 for (int xOffset = 0; xOffset < 80; xOffset += 16) {
141 bounds.fLeft = SkIntToScalar(xOffset);
142 draw_clipped_filter(canvas, rectFilter, 0, r, bounds);
143 canvas->translate(0, r.height() + margin);
144 }
145 }
146
147 private:
148 sk_sp<SkImage> fCheckerboard, fGradientCircle;
149
150 typedef GM INHERITED;
151 };
152
153 //////////////////////////////////////////////////////////////////////////////
154
155 DEF_GM(return new ImageFiltersClippedGM;)
156 }
157