senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkColorFilter.h" |
| 12 | #include "SkColorPriv.h" |
| 13 | #include "SkShader.h" |
Mike Reed | 71f5a0b | 2018-10-25 16:12:39 -0400 | [diff] [blame] | 14 | #include "SkTextUtils.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 15 | |
| 16 | #include "SkBlurImageFilter.h" |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 17 | #include "SkMorphologyImageFilter.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 18 | #include "SkColorFilterImageFilter.h" |
senorblanco@chromium.org | 962c886 | 2013-09-19 17:26:47 +0000 | [diff] [blame] | 19 | #include "SkMergeImageFilter.h" |
| 20 | #include "SkOffsetImageFilter.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 21 | |
| 22 | /////////////////////////////////////////////////////////////////////////////// |
| 23 | |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 24 | static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 25 | SkPaint paint; |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 26 | paint.setImageFilter(std::move(imf)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 27 | paint.setColor(SK_ColorBLACK); |
| 28 | canvas->save(); |
| 29 | canvas->clipRect(r); |
| 30 | canvas->drawPaint(paint); |
| 31 | canvas->restore(); |
| 32 | } |
| 33 | |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 34 | static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 35 | SkPaint paint; |
| 36 | paint.setColor(SK_ColorMAGENTA); |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 37 | paint.setImageFilter(std::move(imf)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 38 | paint.setAntiAlias(true); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 39 | canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 40 | } |
| 41 | |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 42 | static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 43 | SkPaint paint; |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 44 | paint.setImageFilter(std::move(imf)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 45 | paint.setColor(SK_ColorGREEN); |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame] | 46 | |
| 47 | SkFont font(sk_tool_utils::create_portable_typeface(), r.height()/2); |
| 48 | SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), font, paint, SkTextUtils::kCenter_Align); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 49 | } |
| 50 | |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 51 | static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 52 | SkPaint paint; |
| 53 | |
| 54 | SkIRect bounds; |
| 55 | r.roundOut(&bounds); |
| 56 | |
| 57 | SkBitmap bm; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 58 | bm.allocN32Pixels(bounds.width(), bounds.height()); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 59 | bm.eraseColor(SK_ColorTRANSPARENT); |
| 60 | SkCanvas c(bm); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 61 | draw_path(&c, r, nullptr); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 62 | |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 63 | paint.setImageFilter(std::move(imf)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 64 | canvas->drawBitmap(bm, 0, 0, &paint); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 65 | } |
| 66 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 67 | /////////////////////////////////////////////////////////////////////////////// |
| 68 | |
| 69 | class ImageFiltersCroppedGM : public skiagm::GM { |
| 70 | public: |
| 71 | ImageFiltersCroppedGM () {} |
| 72 | |
| 73 | protected: |
senorblanco | 6404981 | 2016-02-01 10:32:42 -0800 | [diff] [blame] | 74 | SkString onShortName() override { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 75 | return SkString("imagefilterscropped"); |
| 76 | } |
| 77 | |
senorblanco | 6404981 | 2016-02-01 10:32:42 -0800 | [diff] [blame] | 78 | SkISize onISize() override { return SkISize::Make(400, 960); } |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 79 | |
| 80 | void make_checkerboard() { |
| 81 | fCheckerboard.allocN32Pixels(80, 80); |
| 82 | SkCanvas canvas(fCheckerboard); |
| 83 | canvas.clear(SK_ColorTRANSPARENT); |
| 84 | SkPaint darkPaint; |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 85 | darkPaint.setColor(0xFF404040); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 86 | SkPaint lightPaint; |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 87 | lightPaint.setColor(0xFFA0A0A0); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 88 | for (int y = 0; y < 80; y += 16) { |
| 89 | for (int x = 0; x < 80; x += 16) { |
| 90 | canvas.save(); |
| 91 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 92 | canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 93 | canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 94 | canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 95 | canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 96 | canvas.restore(); |
| 97 | } |
| 98 | } |
| 99 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 100 | |
| 101 | void draw_frame(SkCanvas* canvas, const SkRect& r) { |
| 102 | SkPaint paint; |
| 103 | paint.setStyle(SkPaint::kStroke_Style); |
| 104 | paint.setColor(SK_ColorRED); |
| 105 | canvas->drawRect(r, paint); |
| 106 | } |
| 107 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 108 | void onOnceBeforeDraw() override{ |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 109 | make_checkerboard(); |
| 110 | } |
| 111 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 112 | void onDraw(SkCanvas* canvas) override { |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 113 | void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = { |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 114 | draw_bitmap, draw_path, draw_paint, draw_text |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 117 | sk_sp<SkColorFilter> cf(SkColorFilter::MakeModeFilter(SK_ColorBLUE, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 118 | SkBlendMode::kSrcIn)); |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 119 | SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)), |
| 120 | SkImageFilter::CropRect::kHasAll_CropEdge); |
| 121 | SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -100, 10, 10)), |
| 122 | SkImageFilter::CropRect::kHasAll_CropEdge); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 123 | |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 124 | sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(SkIntToScalar(-10), |
| 125 | SkIntToScalar(-10), |
| 126 | nullptr)); |
senorblanco@chromium.org | 962c886 | 2013-09-19 17:26:47 +0000 | [diff] [blame] | 127 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 128 | sk_sp<SkImageFilter> cfOffset(SkColorFilterImageFilter::Make(cf, std::move(offset))); |
mtklein | 1eea9fc | 2015-01-23 16:09:32 -0800 | [diff] [blame] | 129 | |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 130 | sk_sp<SkImageFilter> erodeX(SkErodeImageFilter::Make(8, 0, nullptr, &cropRect)); |
| 131 | sk_sp<SkImageFilter> erodeY(SkErodeImageFilter::Make(0, 8, nullptr, &cropRect)); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 132 | |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 133 | sk_sp<SkImageFilter> filters[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 134 | nullptr, |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 135 | SkColorFilterImageFilter::Make(cf, nullptr, &cropRect), |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 136 | SkBlurImageFilter::Make(0.0f, 0.0f, nullptr, &cropRect), |
| 137 | SkBlurImageFilter::Make(1.0f, 1.0f, nullptr, &cropRect), |
| 138 | SkBlurImageFilter::Make(8.0f, 0.0f, nullptr, &cropRect), |
| 139 | SkBlurImageFilter::Make(0.0f, 8.0f, nullptr, &cropRect), |
| 140 | SkBlurImageFilter::Make(8.0f, 8.0f, nullptr, &cropRect), |
| 141 | SkErodeImageFilter::Make(1, 1, nullptr, &cropRect), |
| 142 | SkErodeImageFilter::Make(8, 0, std::move(erodeY), &cropRect), |
| 143 | SkErodeImageFilter::Make(0, 8, std::move(erodeX), &cropRect), |
| 144 | SkErodeImageFilter::Make(8, 8, nullptr, &cropRect), |
| 145 | SkMergeImageFilter::Make(nullptr, |
| 146 | std::move(cfOffset), |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 147 | &cropRect), |
| 148 | SkBlurImageFilter::Make(8.0f, 8.0f, nullptr, &bogusRect), |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 149 | SkColorFilterImageFilter::Make(cf, nullptr, &bogusRect), |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 150 | }; |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 151 | |
| 152 | SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); |
| 153 | SkScalar MARGIN = SkIntToScalar(16); |
| 154 | SkScalar DX = r.width() + MARGIN; |
| 155 | SkScalar DY = r.height() + MARGIN; |
| 156 | |
| 157 | canvas->translate(MARGIN, MARGIN); |
senorblanco@chromium.org | 7168ecc | 2013-07-25 18:08:13 +0000 | [diff] [blame] | 158 | for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) { |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 159 | canvas->save(); |
senorblanco@chromium.org | 7168ecc | 2013-07-25 18:08:13 +0000 | [diff] [blame] | 160 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 161 | SkPaint paint; |
| 162 | canvas->drawBitmap(fCheckerboard, 0, 0); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 163 | drawProc[j](canvas, r, filters[i]); |
| 164 | canvas->translate(0, DY); |
| 165 | } |
| 166 | canvas->restore(); |
| 167 | canvas->translate(DX, 0); |
| 168 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | private: |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 172 | SkBitmap fCheckerboard; |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 173 | typedef GM INHERITED; |
| 174 | }; |
| 175 | |
| 176 | /////////////////////////////////////////////////////////////////////////////// |
| 177 | |
scroggo | 96f16e8 | 2015-12-10 13:31:59 -0800 | [diff] [blame] | 178 | DEF_GM( return new ImageFiltersCroppedGM; ) |