blob: 7a833906401711cbacc69c9ffa5ac64a5d500c5a [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);
Mike Reed568f0ae2021-01-24 08:57:23 -050050 canvas->clipIRect(image->bounds());
Mike Reed07c5f522021-01-23 12:23:23 -050051 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
wutao0dc1f4f2017-06-26 15:03:55 -070052}
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:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000065 SkString getName() const override { return SkString("imageblurclampmode"); }
wutao0dc1f4f2017-06-26 15:03:55 -070066
Leandro Lovisolo8f023882023-08-15 21:13:52 +000067 SkISize getISize() override { return SkISize::Make(850, 920); }
wutao0dc1f4f2017-06-26 15:03:55 -070068
wutao039a7c72017-06-30 10:44:45 -070069 bool runAsBench() const override { return true; }
70
wutao0dc1f4f2017-06-26 15:03:55 -070071 void onDraw(SkCanvas* canvas) override {
72 sk_sp<SkImage> image(make_image(canvas));
Robert Phillips213ce182018-04-25 09:13:28 -040073 sk_sp<SkImageFilter> filter;
wutao0dc1f4f2017-06-26 15:03:55 -070074
75 canvas->translate(0, 30);
76 // Test different kernel size, including the one to launch 2d Gaussian
77 // blur.
78 for (auto sigma: { 0.6f, 3.0f, 8.0f, 20.0f }) {
79 canvas->save();
Robert Phillips213ce182018-04-25 09:13:28 -040080
81 // x-only blur
Michael Ludwig02eecda2023-08-23 12:05:53 -040082 filter = SkImageFilters::Blur(
83 sigma, 0.0f, SkTileMode::kClamp, nullptr, image->bounds());
wutao0dc1f4f2017-06-26 15:03:55 -070084 draw_image(canvas, image, std::move(filter));
85 canvas->translate(image->width() + 20, 0);
86
Robert Phillips213ce182018-04-25 09:13:28 -040087 // y-only blur
Michael Ludwig02eecda2023-08-23 12:05:53 -040088 filter = SkImageFilters::Blur(
89 0.0f, sigma, SkTileMode::kClamp, nullptr, image->bounds());
wutao0dc1f4f2017-06-26 15:03:55 -070090 draw_image(canvas, image, std::move(filter));
91 canvas->translate(image->width() + 20, 0);
92
Robert Phillips213ce182018-04-25 09:13:28 -040093 // both directions
Michael Ludwig02eecda2023-08-23 12:05:53 -040094 filter = SkImageFilters::Blur(
95 sigma, sigma, SkTileMode::kClamp, nullptr, image->bounds());
wutao0dc1f4f2017-06-26 15:03:55 -070096 draw_image(canvas, image, std::move(filter));
97 canvas->translate(image->width() + 20, 0);
98
99 canvas->restore();
Robert Phillips213ce182018-04-25 09:13:28 -0400100
wutao0dc1f4f2017-06-26 15:03:55 -0700101 canvas->translate(0, image->height() + 20);
102 }
103 }
104
105private:
John Stiles7571f9e2020-09-02 22:42:33 -0400106 using INHERITED = GM;
wutao0dc1f4f2017-06-26 15:03:55 -0700107};
108
109//////////////////////////////////////////////////////////////////////////////
110
111DEF_GM(return new ImageBlurClampModeGM;)
John Stilesa6841be2020-08-06 14:11:56 -0400112} // namespace skiagm