senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 14 | #include "SkBlurImageFilter.h" |
| 15 | #include "SkColorMatrixFilter.h" |
| 16 | #include "SkDisplacementMapEffect.h" |
| 17 | #include "SkDropShadowImageFilter.h" |
| 18 | #include "SkGradientShader.h" |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 19 | #include "SkImageSource.h" |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 20 | #include "SkMorphologyImageFilter.h" |
| 21 | #include "SkColorFilterImageFilter.h" |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 22 | #include "SkLightingImageFilter.h" |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 23 | #include "SkMergeImageFilter.h" |
| 24 | #include "SkOffsetImageFilter.h" |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 25 | #include "SkPoint3.h" |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 26 | #include "SkSurface.h" |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 27 | |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
| 30 | class ImageFiltersCropExpandGM : public skiagm::GM { |
| 31 | public: |
| 32 | ImageFiltersCropExpandGM () {} |
| 33 | |
| 34 | protected: |
| 35 | |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 36 | SkString onShortName() override { |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 37 | return SkString("imagefilterscropexpand"); |
| 38 | } |
| 39 | |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 40 | SkISize onISize() override { return SkISize::Make(730, 650); } |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 41 | |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 42 | void onDraw(SkCanvas* canvas) override { |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 43 | SkImageFilter::CropRect cropRect( |
| 44 | SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)), |
| 45 | SkImageFilter::CropRect::kHasAll_CropEdge); |
| 46 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 47 | sk_sp<SkImage> gradientCircle(MakeGradientCircle(64, 64)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 48 | SkBitmap checkerboard; |
| 49 | MakeCheckerboard(&checkerboard); |
| 50 | |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 51 | sk_sp<SkImageFilter> gradientCircleSource(SkImageSource::Make(std::move(gradientCircle))); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 52 | sk_sp<SkImageFilter> noopCropped(SkOffsetImageFilter::Make(0, 0, nullptr, &cropRect)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 53 | SkScalar sk255 = SkIntToScalar(255); |
| 54 | SkScalar matrix[20] = { 1, 0, 0, 0, 0, |
| 55 | 0, 1, 0, 0, sk255, |
| 56 | 0, 0, 1, 0, 0, |
| 57 | 0, 0, 0, 0, sk255 }; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 58 | auto cfAlphaTrans(SkColorFilter::MakeMatrixFilterRowMajor255(matrix)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 59 | |
| 60 | SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); |
| 61 | SkScalar MARGIN = SkIntToScalar(12); |
| 62 | |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 63 | SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10)); |
| 64 | SkScalar kd = SkIntToScalar(2); |
| 65 | SkScalar surfaceScale = SkIntToScalar(1); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 66 | SkIRect bounds; |
| 67 | r.roundOut(&bounds); |
| 68 | |
| 69 | SkPaint paint; |
| 70 | canvas->translate(MARGIN, MARGIN); |
| 71 | for (int outset = -15; outset <= 20; outset += 5) { |
| 72 | canvas->save(); |
| 73 | SkRect rect = cropRect.rect(); |
| 74 | rect.outset(SkIntToScalar(outset), |
| 75 | SkIntToScalar(outset)); |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 76 | SkImageFilter::CropRect bigRect(rect, SkImageFilter::CropRect::kHasAll_CropEdge); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 77 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 78 | Draw(canvas, checkerboard, rect, SkColorFilterImageFilter::Make(cfAlphaTrans, |
| 79 | noopCropped, |
| 80 | &bigRect)); |
senorblanco | 84f0e74 | 2016-02-16 13:26:56 -0800 | [diff] [blame] | 81 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 82 | Draw(canvas, checkerboard, rect, SkBlurImageFilter::Make(0.3f, 0.3f, |
| 83 | noopCropped, |
| 84 | &bigRect)); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 85 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 86 | Draw(canvas, checkerboard, rect, SkBlurImageFilter::Make(8.0f, 8.0f, |
| 87 | noopCropped, |
| 88 | &bigRect)); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 89 | |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 90 | Draw(canvas, checkerboard, rect, SkDilateImageFilter::Make(2, 2, |
| 91 | noopCropped, |
| 92 | &bigRect)); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 93 | |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 94 | Draw(canvas, checkerboard, rect, SkErodeImageFilter::Make(2, 2, |
| 95 | noopCropped, |
| 96 | &bigRect)); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 97 | |
| 98 | Draw(canvas, checkerboard, rect, |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame^] | 99 | SkDropShadowImageFilter::Make( |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 100 | SkIntToScalar(10), |
| 101 | SkIntToScalar(10), |
| 102 | SkIntToScalar(3), |
| 103 | SkIntToScalar(3), |
| 104 | SK_ColorBLUE, |
| 105 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame^] | 106 | noopCropped, |
| 107 | &bigRect)); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 108 | |
| 109 | Draw(canvas, checkerboard, rect, |
| 110 | sk_sp<SkImageFilter>(SkDisplacementMapEffect::Create( |
| 111 | SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 112 | SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 113 | SkIntToScalar(12), |
| 114 | gradientCircleSource.get(), |
| 115 | noopCropped.get(), |
| 116 | &bigRect))); |
| 117 | |
| 118 | Draw(canvas, checkerboard, rect, |
| 119 | SkOffsetImageFilter::Make(SkIntToScalar(-8), SkIntToScalar(16), |
| 120 | noopCropped, |
| 121 | &bigRect)); |
| 122 | |
| 123 | Draw(canvas, checkerboard, rect, |
| 124 | sk_sp<SkImageFilter>(SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, |
| 125 | SK_ColorWHITE, |
| 126 | surfaceScale, |
| 127 | kd, |
| 128 | noopCropped.get(), |
| 129 | &bigRect))); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 130 | |
| 131 | canvas->restore(); |
| 132 | canvas->translate(0, SkIntToScalar(80)); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private: |
| 137 | static void MakeCheckerboard(SkBitmap* bitmap) { |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 138 | bitmap->allocN32Pixels(64, 64); |
| 139 | SkCanvas canvas(*bitmap); |
| 140 | canvas.clear(0xFFFF0000); |
| 141 | SkPaint darkPaint; |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 142 | darkPaint.setColor(sk_tool_utils::color_to_565(0xFF404040)); |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 143 | SkPaint lightPaint; |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 144 | lightPaint.setColor(sk_tool_utils::color_to_565(0xFFA0A0A0)); |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 145 | for (int y = 8; y < 48; y += 16) { |
| 146 | for (int x = 8; x < 48; x += 16) { |
| 147 | canvas.save(); |
| 148 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 149 | canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 150 | canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 151 | canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 152 | canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 153 | canvas.restore(); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 158 | static sk_sp<SkImage> MakeGradientCircle(int width, int height) { |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 159 | SkScalar x = SkIntToScalar(width / 2); |
| 160 | SkScalar y = SkIntToScalar(height / 2); |
| 161 | SkScalar radius = SkMinScalar(x, y) * 0.8f; |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 162 | auto surface(SkSurface::MakeRasterN32Premul(width, height)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 163 | SkCanvas* canvas = surface->getCanvas(); |
| 164 | canvas->clear(0x00000000); |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 165 | SkColor colors[2]; |
| 166 | colors[0] = SK_ColorWHITE; |
| 167 | colors[1] = SK_ColorBLACK; |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 168 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 169 | paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(x, y), radius, colors, nullptr, |
| 170 | 2, SkShader::kClamp_TileMode)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 171 | canvas->drawCircle(x, y, radius, paint); |
| 172 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 173 | return surface->makeImageSnapshot(); |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 174 | } |
| 175 | |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 176 | static void Draw(SkCanvas* canvas, const SkBitmap& bitmap, const SkRect& rect, |
| 177 | sk_sp<SkImageFilter> filter) { |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 178 | SkPaint paint; |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 179 | paint.setImageFilter(std::move(filter)); |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 180 | canvas->saveLayer(&rect, &paint); |
| 181 | canvas->drawBitmap(bitmap, 0, 0); |
| 182 | canvas->restore(); |
skia.committer@gmail.com | f0f9f5f | 2014-03-15 03:02:32 +0000 | [diff] [blame] | 183 | |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 184 | SkPaint strokePaint; |
| 185 | strokePaint.setColor(0xFFFF0000); |
| 186 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 187 | canvas->drawRect(rect, strokePaint); |
| 188 | |
| 189 | canvas->translate(SkIntToScalar(80), 0); |
| 190 | } |
| 191 | |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 192 | typedef GM INHERITED; |
| 193 | }; |
| 194 | |
| 195 | /////////////////////////////////////////////////////////////////////////////// |
| 196 | |
scroggo | 96f16e8 | 2015-12-10 13:31:59 -0800 | [diff] [blame] | 197 | DEF_GM( return new ImageFiltersCropExpandGM; ) |