blob: 06ce3131229affc68039d2c2fa062f763cf5331d [file] [log] [blame]
wutao0dc1f4f2017-06-26 15:03:55 -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"
wutao0dc1f4f2017-06-26 15:03:55 -070023
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include <initializer_list>
25#include <utility>
26
wutao0dc1f4f2017-06-26 15:03:55 -070027static sk_sp<SkImage> make_image(SkCanvas* canvas) {
28 SkImageInfo info = SkImageInfo::MakeN32Premul(250, 200);
Mike Kleinea3f0142019-03-20 11:12:10 -050029 auto surface = ToolUtils::makeSurface(canvas, info);
wutao0dc1f4f2017-06-26 15:03:55 -070030 SkCanvas* c = surface->getCanvas();
31 SkPaint paint;
32 paint.setAntiAlias(true);
33
34 paint.setColor(SK_ColorBLUE);
35 c->drawRect(SkRect::MakeIWH(info.width(), info.height()), paint);
36 paint.setColor(SK_ColorGREEN);
37 c->drawCircle(125, 100, 100, paint);
38 paint.setColor(SK_ColorRED);
39 c->drawRect(SkRect::MakeIWH(80, 80), paint);
40
41 return surface->makeImageSnapshot();
42}
43
44static void draw_image(SkCanvas* canvas, const sk_sp<SkImage> image, sk_sp<SkImageFilter> filter) {
45 SkAutoCanvasRestore acr(canvas, true);
46 SkPaint paint;
47 paint.setImageFilter(std::move(filter));
48
49 canvas->translate(SkIntToScalar(30), 0);
50 canvas->clipRect(SkRect::MakeIWH(image->width(),image->height()));
51 canvas->drawImage(image, 0, 0, &paint);
52}
53
54namespace skiagm {
55
56// This GM draws one rectangle, one green inscribed circle, and one red square
57// with different blur settings.
58class ImageBlurClampModeGM : public GM {
59public:
60 ImageBlurClampModeGM() {
Mike Kleind46dce32018-08-16 10:17:03 -040061 this->setBGColor(0xFFCCCCCC);
wutao0dc1f4f2017-06-26 15:03:55 -070062 }
63
64protected:
65
66 SkString onShortName() override {
67 return SkString("imageblurclampmode");
68 }
69
70 SkISize onISize() override {
71 return SkISize::Make(850, 920);
72 }
73
wutao039a7c72017-06-30 10:44:45 -070074 bool runAsBench() const override { return true; }
75
wutao0dc1f4f2017-06-26 15:03:55 -070076 void onDraw(SkCanvas* canvas) override {
77 sk_sp<SkImage> image(make_image(canvas));
Robert Phillips213ce182018-04-25 09:13:28 -040078 sk_sp<SkImageFilter> filter;
wutao0dc1f4f2017-06-26 15:03:55 -070079
80 canvas->translate(0, 30);
81 // Test different kernel size, including the one to launch 2d Gaussian
82 // blur.
83 for (auto sigma: { 0.6f, 3.0f, 8.0f, 20.0f }) {
84 canvas->save();
Robert Phillips213ce182018-04-25 09:13:28 -040085
86 // x-only blur
Michael Ludwig898bbfa2019-08-02 15:21:23 -040087 filter = SkImageFilters::Blur(sigma, 0.0f, SkTileMode::kClamp, nullptr);
wutao0dc1f4f2017-06-26 15:03:55 -070088 draw_image(canvas, image, std::move(filter));
89 canvas->translate(image->width() + 20, 0);
90
Robert Phillips213ce182018-04-25 09:13:28 -040091 // y-only blur
Michael Ludwig898bbfa2019-08-02 15:21:23 -040092 filter = SkImageFilters::Blur(0.0f, sigma, SkTileMode::kClamp, nullptr);
wutao0dc1f4f2017-06-26 15:03:55 -070093 draw_image(canvas, image, std::move(filter));
94 canvas->translate(image->width() + 20, 0);
95
Robert Phillips213ce182018-04-25 09:13:28 -040096 // both directions
Michael Ludwig898bbfa2019-08-02 15:21:23 -040097 filter = SkImageFilters::Blur(sigma, sigma, SkTileMode::kClamp, nullptr);
wutao0dc1f4f2017-06-26 15:03:55 -070098 draw_image(canvas, image, std::move(filter));
99 canvas->translate(image->width() + 20, 0);
100
101 canvas->restore();
Robert Phillips213ce182018-04-25 09:13:28 -0400102
wutao0dc1f4f2017-06-26 15:03:55 -0700103 canvas->translate(0, image->height() + 20);
104 }
105 }
106
107private:
108 typedef GM INHERITED;
109};
110
111//////////////////////////////////////////////////////////////////////////////
112
113DEF_GM(return new ImageBlurClampModeGM;)
114}