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 "gm/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkFilterQuality.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageFilter.h"
15 #include "include/core/SkMatrix.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPoint.h"
18 #include "include/core/SkPoint3.h"
19 #include "include/core/SkRect.h"
20 #include "include/core/SkRefCnt.h"
21 #include "include/core/SkScalar.h"
22 #include "include/core/SkShader.h"
23 #include "include/core/SkSize.h"
24 #include "include/core/SkString.h"
25 #include "include/core/SkSurface.h"
26 #include "include/core/SkTileMode.h"
27 #include "include/core/SkTypes.h"
28 #include "include/effects/SkGradientShader.h"
29 #include "include/effects/SkImageFilters.h"
30 #include "include/effects/SkPerlinNoiseShader.h"
31 #include "tools/ToolUtils.h"
32
33 #include <utility>
34
35 #define RESIZE_FACTOR SkIntToScalar(4)
36
make_gradient_circle(int width,int height)37 static sk_sp<SkImage> make_gradient_circle(int width, int height) {
38 SkScalar x = SkIntToScalar(width / 2);
39 SkScalar y = SkIntToScalar(height / 2);
40 SkScalar radius = std::min(x, y) * 4 / 5;
41 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(width, height));
42 SkCanvas* canvas = surface->getCanvas();
43 canvas->clear(0x00000000);
44 SkColor colors[2];
45 colors[0] = SK_ColorWHITE;
46 colors[1] = SK_ColorBLACK;
47 SkPaint paint;
48 paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(x, y), radius, colors, nullptr,
49 2, SkTileMode::kClamp));
50 canvas->drawCircle(x, y, radius, paint);
51
52 return surface->makeImageSnapshot();
53 }
54
55
56 namespace skiagm {
57
58 class ImageFiltersScaledGM : public GM {
59 public:
ImageFiltersScaledGM()60 ImageFiltersScaledGM() {
61 this->setBGColor(0x00000000);
62 }
63
64 protected:
65
onShortName()66 SkString onShortName() override {
67 return SkString("imagefiltersscaled");
68 }
69
onISize()70 SkISize onISize() override {
71 return SkISize::Make(1428, 500);
72 }
73
onOnceBeforeDraw()74 void onOnceBeforeDraw() override {
75 fCheckerboard = SkImage::MakeFromBitmap(
76 ToolUtils::create_checkerboard_bitmap(64, 64, 0xFFA0A0A0, 0xFF404040, 8));
77 fGradientCircle = make_gradient_circle(64, 64);
78 }
79
onDraw(SkCanvas * canvas)80 void onDraw(SkCanvas* canvas) override {
81 canvas->clear(SK_ColorBLACK);
82
83 sk_sp<SkImageFilter> gradient(SkImageFilters::Image(fGradientCircle));
84 sk_sp<SkImageFilter> checkerboard(SkImageFilters::Image(fCheckerboard));
85
86 SkPaint noisePaint;
87 noisePaint.setShader(SkPerlinNoiseShader::MakeFractalNoise(SkDoubleToScalar(0.1),
88 SkDoubleToScalar(0.05), 1, 0));
89
90 SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10));
91 SkPoint3 spotLocation = SkPoint3::Make(SkIntToScalar(-10),
92 SkIntToScalar(-10),
93 SkIntToScalar(20));
94 SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0);
95 SkScalar spotExponent = SK_Scalar1;
96 SkScalar cutoffAngle = SkIntToScalar(15);
97 SkScalar kd = SkIntToScalar(2);
98 SkScalar surfaceScale = SkIntToScalar(1);
99 SkColor white(0xFFFFFFFF);
100 SkMatrix resizeMatrix;
101 resizeMatrix.setScale(RESIZE_FACTOR, RESIZE_FACTOR);
102
103 sk_sp<SkImageFilter> filters[] = {
104 SkImageFilters::Blur(SkIntToScalar(4), SkIntToScalar(4), nullptr),
105 SkImageFilters::DropShadow(5, 10, 3, 3, SK_ColorYELLOW, nullptr),
106 SkImageFilters::DisplacementMap(SkColorChannel::kR, SkColorChannel::kR, 12,
107 std::move(gradient), checkerboard),
108 SkImageFilters::Dilate(1, 1, checkerboard),
109 SkImageFilters::Erode(1, 1, checkerboard),
110 SkImageFilters::Offset(SkIntToScalar(32), 0, nullptr),
111 SkImageFilters::MatrixTransform(resizeMatrix, kNone_SkFilterQuality, nullptr),
112 SkImageFilters::Paint(noisePaint),
113 SkImageFilters::PointLitDiffuse(pointLocation, white, surfaceScale, kd, nullptr),
114 SkImageFilters::SpotLitDiffuse(spotLocation, spotTarget, spotExponent,
115 cutoffAngle, white, surfaceScale, kd, nullptr),
116 };
117
118 SkVector scales[] = {
119 SkVector::Make(SkScalarInvert(2), SkScalarInvert(2)),
120 SkVector::Make(SkIntToScalar(1), SkIntToScalar(1)),
121 SkVector::Make(SkIntToScalar(1), SkIntToScalar(2)),
122 SkVector::Make(SkIntToScalar(2), SkIntToScalar(1)),
123 SkVector::Make(SkIntToScalar(2), SkIntToScalar(2)),
124 };
125
126 SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
127 SkScalar margin = SkIntToScalar(16);
128 SkRect bounds = r;
129 bounds.outset(margin, margin);
130
131 for (size_t j = 0; j < SK_ARRAY_COUNT(scales); ++j) {
132 canvas->save();
133 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
134 SkPaint paint;
135 paint.setColor(SK_ColorBLUE);
136 paint.setImageFilter(filters[i]);
137 paint.setAntiAlias(true);
138 canvas->save();
139 canvas->scale(scales[j].fX, scales[j].fY);
140 canvas->clipRect(r);
141 if (5 == i) {
142 canvas->translate(SkIntToScalar(-32), 0);
143 } else if (6 == i) {
144 canvas->scale(SkScalarInvert(RESIZE_FACTOR),
145 SkScalarInvert(RESIZE_FACTOR));
146 }
147 canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint);
148 canvas->restore();
149 canvas->translate(r.width() * scales[j].fX + margin, 0);
150 }
151 canvas->restore();
152 canvas->translate(0, r.height() * scales[j].fY + margin);
153 }
154 }
155
156 private:
157 sk_sp<SkImage> fCheckerboard, fGradientCircle;
158
159 typedef GM INHERITED;
160 };
161
162 //////////////////////////////////////////////////////////////////////////////
163
164 DEF_GM(return new ImageFiltersScaledGM;)
165 }
166