blob: ce500a2fcb88922ab1ddf15d94acabcabd33245a [file] [log] [blame]
wutao039a7c72017-06-30 10:44:45 -07001/*
2 * Copyright 2017 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkImage.h"
12#include "include/core/SkImageFilter.h"
13#include "include/core/SkImageInfo.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkRefCnt.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/core/SkSurface.h"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040021#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "tools/ToolUtils.h"
wutao039a7c72017-06-30 10:44:45 -070023
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include <initializer_list>
25#include <utility>
26
wutao039a7c72017-06-30 10:44:45 -070027static sk_sp<SkImage> make_image(SkCanvas* canvas, int direction) {
28 SkImageInfo info = SkImageInfo::MakeN32Premul(250, 200);
Mike Kleinea3f0142019-03-20 11:12:10 -050029 auto surface = ToolUtils::makeSurface(canvas, info);
wutao039a7c72017-06-30 10:44:45 -070030 SkCanvas* c = surface->getCanvas();
31 SkPaint paint;
32 paint.setAntiAlias(true);
33
34 const SkColor colors[] = {
35 SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN, SK_ColorYELLOW, SK_ColorBLACK
36 };
37
38 int width = 25;
39 bool xDirection = (direction & 0x1) == 1;
40 bool yDirection = (direction & 0x2) == 2;
41 if (xDirection) {
42 for (int x = 0; x < info.width(); x += width) {
43 paint.setColor(colors[x/width % 5]);
44 if (yDirection) {
Mike Reed9407e242019-02-15 16:13:57 -050045 paint.setAlphaf(0.5f);
wutao039a7c72017-06-30 10:44:45 -070046 }
47 c->drawRect(SkRect::MakeXYWH(x, 0, width, info.height()), paint);
48 }
49 }
50
51 if (yDirection) {
52 for (int y = 0; y < info.height(); y += width) {
53 paint.setColor(colors[y/width % 5]);
54 if (xDirection) {
Mike Reed9407e242019-02-15 16:13:57 -050055 paint.setAlphaf(0.5f);
wutao039a7c72017-06-30 10:44:45 -070056 }
57 c->drawRect(SkRect::MakeXYWH(0, y, info.width(), width), paint);
58 }
59 }
60 return surface->makeImageSnapshot();
61}
62
63static void draw_image(SkCanvas* canvas, const sk_sp<SkImage> image, sk_sp<SkImageFilter> filter) {
64 SkAutoCanvasRestore acr(canvas, true);
65 SkPaint paint;
66 paint.setImageFilter(std::move(filter));
67
68 canvas->translate(SkIntToScalar(30), 0);
69 canvas->clipRect(SkRect::MakeIWH(image->width(),image->height()));
70 canvas->drawImage(image, 0, 0, &paint);
71}
72
73namespace skiagm {
74
75// This GM draws a colorful grids with different blur settings.
76class ImageBlurRepeatModeGM : public GM {
77public:
78 ImageBlurRepeatModeGM() {
Mike Kleind46dce32018-08-16 10:17:03 -040079 this->setBGColor(0xFFCCCCCC);
wutao039a7c72017-06-30 10:44:45 -070080 }
81
82protected:
83
84 SkString onShortName() override {
85 return SkString("imageblurrepeatmode");
86 }
87
88 SkISize onISize() override {
89 return SkISize::Make(850, 920);
90 }
91
92 bool runAsBench() const override { return true; }
93
94 void onDraw(SkCanvas* canvas) override {
95 sk_sp<SkImage> image[] =
96 { make_image(canvas, 1), make_image(canvas, 2), make_image(canvas, 3) };
97
98 canvas->translate(0, 30);
99 // Test different kernel size, including the one to launch 2d Gaussian
100 // blur.
101 for (auto sigma: { 0.6f, 3.0f, 8.0f, 20.0f }) {
102 canvas->save();
103 sk_sp<SkImageFilter> filter(
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400104 SkImageFilters::Blur(sigma, 0.0f, SkTileMode::kRepeat, nullptr));
wutao039a7c72017-06-30 10:44:45 -0700105 draw_image(canvas, image[0], std::move(filter));
106 canvas->translate(image[0]->width() + 20, 0);
107
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400108 filter = SkImageFilters::Blur(0.0f, sigma, SkTileMode::kRepeat, nullptr);
wutao039a7c72017-06-30 10:44:45 -0700109 draw_image(canvas, image[1], std::move(filter));
110 canvas->translate(image[1]->width() + 20, 0);
111
Michael Ludwig898bbfa2019-08-02 15:21:23 -0400112 filter = SkImageFilters::Blur(sigma, sigma, SkTileMode::kRepeat, nullptr);
wutao039a7c72017-06-30 10:44:45 -0700113 draw_image(canvas, image[2], std::move(filter));
114 canvas->translate(image[2]->width() + 20, 0);
115
116 canvas->restore();
117 canvas->translate(0, image[0]->height() + 20);
118 }
119 }
120
121private:
122 typedef GM INHERITED;
123};
124
125//////////////////////////////////////////////////////////////////////////////
126
127DEF_GM(return new ImageBlurRepeatModeGM;)
128}