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