• 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_X SkIntToScalar(2)
35 #define RESIZE_FACTOR_Y SkIntToScalar(5)
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) * 0.8f;
41     auto 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     return surface->makeImageSnapshot();
52 }
53 
draw_clipped_filter(SkCanvas * canvas,sk_sp<SkImageFilter> filter,size_t i,const SkRect & primBounds,const SkRect & clipBounds)54 static void draw_clipped_filter(SkCanvas* canvas, sk_sp<SkImageFilter> filter, size_t i,
55                                 const SkRect& primBounds, const SkRect& clipBounds) {
56     SkPaint paint;
57     paint.setColor(SK_ColorWHITE);
58     paint.setImageFilter(std::move(filter));
59     paint.setAntiAlias(true);
60     canvas->save();
61     canvas->clipRect(clipBounds);
62     if (5 == i) {
63         canvas->translate(SkIntToScalar(16), SkIntToScalar(-32));
64     } else if (6 == i) {
65         canvas->scale(SkScalarInvert(RESIZE_FACTOR_X), SkScalarInvert(RESIZE_FACTOR_Y));
66     }
67     canvas->drawCircle(primBounds.centerX(), primBounds.centerY(),
68                        primBounds.width() * 2 / 5, paint);
69     canvas->restore();
70 }
71 
72 namespace skiagm {
73 
74 class ImageFiltersClippedGM : public GM {
75 public:
ImageFiltersClippedGM()76     ImageFiltersClippedGM() {
77         this->setBGColor(0x00000000);
78     }
79 
80 protected:
onShortName()81     SkString onShortName() override {
82         return SkString("imagefiltersclipped");
83     }
84 
onISize()85     SkISize onISize() override {
86         return SkISize::Make(860, 500);
87     }
88 
onOnceBeforeDraw()89     void onOnceBeforeDraw() override {
90         fCheckerboard =
91                 ToolUtils::create_checkerboard_image(64, 64, 0xFFA0A0A0, 0xFF404040, 8);
92         fGradientCircle = make_gradient_circle(64, 64);
93     }
94 
onDraw(SkCanvas * canvas)95     void onDraw(SkCanvas* canvas) override {
96         canvas->clear(SK_ColorBLACK);
97 
98         sk_sp<SkImageFilter> gradient(SkImageFilters::Image(fGradientCircle));
99         sk_sp<SkImageFilter> checkerboard(SkImageFilters::Image(fCheckerboard));
100         SkMatrix resizeMatrix;
101         resizeMatrix.setScale(RESIZE_FACTOR_X, RESIZE_FACTOR_Y);
102         SkPoint3 pointLocation = SkPoint3::Make(32, 32, SkIntToScalar(10));
103 
104         sk_sp<SkImageFilter> filters[] = {
105             SkImageFilters::Blur(SkIntToScalar(12), SkIntToScalar(12), nullptr),
106             SkImageFilters::DropShadow(SkIntToScalar(10), SkIntToScalar(10),
107                                        SkIntToScalar(3), SkIntToScalar(3), SK_ColorGREEN, nullptr),
108             SkImageFilters::DisplacementMap(SkColorChannel::kR, SkColorChannel::kR,
109                                             SkIntToScalar(12), std::move(gradient), checkerboard),
110             SkImageFilters::Dilate(2, 2, checkerboard),
111             SkImageFilters::Erode(2, 2, checkerboard),
112             SkImageFilters::Offset(SkIntToScalar(-16), SkIntToScalar(32), nullptr),
113             SkImageFilters::MatrixTransform(resizeMatrix, SkSamplingOptions(), nullptr),
114             SkImageFilters::PointLitDiffuse(pointLocation, SK_ColorWHITE, SK_Scalar1,
115                                             SkIntToScalar(2), checkerboard),
116 
117         };
118 
119         SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
120         SkScalar margin = SkIntToScalar(16);
121         SkRect bounds = r;
122         bounds.outset(margin, margin);
123 
124         canvas->save();
125         for (int xOffset = 0; xOffset < 80; xOffset += 16) {
126             canvas->save();
127             bounds.fLeft = SkIntToScalar(xOffset);
128             for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
129                 draw_clipped_filter(canvas, filters[i], i, r, bounds);
130                 canvas->translate(r.width() + margin, 0);
131             }
132             canvas->restore();
133             canvas->translate(0, r.height() + margin);
134         }
135         canvas->restore();
136 
137         sk_sp<SkImageFilter> rectFilter(SkImageFilters::Shader(
138                 SkPerlinNoiseShader::MakeFractalNoise(0.1f, 0.05f, 1, 0)));
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     using INHERITED = GM;
151 };
152 
153 //////////////////////////////////////////////////////////////////////////////
154 
155 DEF_GM(return new ImageFiltersClippedGM;)
156 }  // namespace skiagm
157