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