senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 10 | #include "SkColor.h" |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 11 | #include "SkGradientShader.h" |
Brian Osman | 878df6d | 2016-12-27 11:04:12 -0500 | [diff] [blame] | 12 | #include "SkMatrixConvolutionImageFilter.h" |
| 13 | #include "SkPixelRef.h" |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 17 | class MatrixConvolutionGM : public GM { |
| 18 | public: |
Brian Osman | 878df6d | 2016-12-27 11:04:12 -0500 | [diff] [blame] | 19 | MatrixConvolutionGM(SkColor colorOne, SkColor colorTwo, const char* nameSuffix) |
| 20 | : fNameSuffix(nameSuffix) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 21 | this->setBGColor(0x00000000); |
Brian Osman | 878df6d | 2016-12-27 11:04:12 -0500 | [diff] [blame] | 22 | fColors[0] = colorOne; |
| 23 | fColors[1] = colorTwo; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 27 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 28 | SkString onShortName() override { |
Brian Osman | 878df6d | 2016-12-27 11:04:12 -0500 | [diff] [blame] | 29 | return SkStringPrintf("matrixconvolution%s", fNameSuffix); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 30 | } |
| 31 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 32 | void makeBitmap() { |
Brian Osman | 878df6d | 2016-12-27 11:04:12 -0500 | [diff] [blame] | 33 | // Draw our bitmap in N32, so legacy devices get "premul" values they understand |
| 34 | SkBitmap n32Bitmap; |
| 35 | n32Bitmap.allocN32Pixels(80, 80); |
| 36 | SkCanvas canvas(n32Bitmap); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 37 | canvas.clear(0x00000000); |
| 38 | SkPaint paint; |
| 39 | paint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 40 | sk_tool_utils::set_portable_typeface(&paint); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 41 | paint.setColor(0xFFFFFFFF); |
| 42 | paint.setTextSize(SkIntToScalar(180)); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 43 | SkPoint pts[2] = { SkPoint::Make(0, 0), |
| 44 | SkPoint::Make(0, SkIntToScalar(80)) }; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 45 | SkScalar pos[2] = { 0, SkIntToScalar(80) }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 46 | paint.setShader(SkGradientShader::MakeLinear( |
Brian Osman | 878df6d | 2016-12-27 11:04:12 -0500 | [diff] [blame] | 47 | pts, fColors, pos, 2, SkShader::kClamp_TileMode)); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 48 | const char* str = "e"; |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 49 | canvas.drawString(str, SkIntToScalar(-10), SkIntToScalar(80), paint); |
Brian Osman | 878df6d | 2016-12-27 11:04:12 -0500 | [diff] [blame] | 50 | |
| 51 | // ... tag the data as sRGB, so color-aware devices do gamut adjustment, etc... |
| 52 | fBitmap.setInfo(SkImageInfo::MakeS32(80, 80, kPremul_SkAlphaType)); |
| 53 | fBitmap.setPixelRef(sk_ref_sp(n32Bitmap.pixelRef()), 0, 0); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 54 | } |
| 55 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 56 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 57 | return SkISize::Make(500, 300); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 58 | } |
| 59 | |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 60 | void draw(SkCanvas* canvas, int x, int y, const SkIPoint& kernelOffset, |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 61 | SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 62 | const SkImageFilter::CropRect* cropRect = nullptr) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 63 | SkScalar kernel[9] = { |
| 64 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 65 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 66 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 67 | }; |
| 68 | SkISize kernelSize = SkISize::Make(3, 3); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 69 | SkScalar gain = 0.3f, bias = SkIntToScalar(100); |
Brian Osman | 878df6d | 2016-12-27 11:04:12 -0500 | [diff] [blame] | 70 | if (canvas->imageInfo().colorSpace()) { |
| 71 | // TODO: Gain and bias are poorly specified (in the feConvolveMatrix SVG documentation, |
| 72 | // there is obviously no mention of gamma or color spaces). Eventually, we need to |
| 73 | // decide what to do with these (they generally have an extreme brightening effect). |
| 74 | // For now, I'm modifying this GM to use values tuned to preserve luminance across the |
| 75 | // range of input values (compared to the legacy math and values). |
| 76 | // |
| 77 | // It's impossible to match the results exactly, because legacy math produces a flat |
| 78 | // response (when looking at sRGB encoded results), while gamma-correct math produces |
| 79 | // a curve. |
| 80 | gain = 0.25f; |
| 81 | bias = 16.5f; |
| 82 | } |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 83 | SkPaint paint; |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 84 | paint.setImageFilter(SkMatrixConvolutionImageFilter::Make(kernelSize, |
| 85 | kernel, |
| 86 | gain, |
| 87 | bias, |
| 88 | kernelOffset, |
| 89 | tileMode, |
| 90 | convolveAlpha, |
| 91 | nullptr, |
| 92 | cropRect)); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 93 | canvas->save(); |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 94 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 95 | canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), |
| 96 | SkIntToScalar(fBitmap.height()))); |
| 97 | canvas->drawBitmap(fBitmap, 0, 0, &paint); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 98 | canvas->restore(); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 99 | } |
| 100 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 101 | typedef SkMatrixConvolutionImageFilter MCIF; |
| 102 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 103 | void onOnceBeforeDraw() override { |
| 104 | this->makeBitmap(); |
| 105 | } |
| 106 | |
| 107 | void onDraw(SkCanvas* canvas) override { |
senorblanco | 16b254a | 2015-04-09 11:13:24 -0700 | [diff] [blame] | 108 | canvas->clear(SK_ColorBLACK); |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 109 | SkIPoint kernelOffset = SkIPoint::Make(1, 0); |
senorblanco | a544eda | 2015-12-07 07:48:34 -0800 | [diff] [blame] | 110 | SkImageFilter::CropRect rect(SkRect::Make(fBitmap.bounds())); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 111 | for (int x = 10; x < 310; x += 100) { |
senorblanco | a544eda | 2015-12-07 07:48:34 -0800 | [diff] [blame] | 112 | this->draw(canvas, x, 10, kernelOffset, MCIF::kClamp_TileMode, true, &rect); |
| 113 | this->draw(canvas, x, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &rect); |
| 114 | this->draw(canvas, x, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &rect); |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 115 | kernelOffset.fY++; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 116 | } |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 117 | kernelOffset.fY = 1; |
senorblanco | a544eda | 2015-12-07 07:48:34 -0800 | [diff] [blame] | 118 | SkImageFilter::CropRect smallRect(SkRect::MakeXYWH(10, 5, 60, 60)); |
| 119 | this->draw(canvas, 310, 10, kernelOffset, MCIF::kClamp_TileMode, true, &smallRect); |
| 120 | this->draw(canvas, 310, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &smallRect); |
| 121 | this->draw(canvas, 310, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &smallRect); |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 122 | |
senorblanco | a544eda | 2015-12-07 07:48:34 -0800 | [diff] [blame] | 123 | this->draw(canvas, 410, 10, kernelOffset, MCIF::kClamp_TileMode, false, &rect); |
| 124 | this->draw(canvas, 410, 110, kernelOffset, MCIF::kClampToBlack_TileMode, false, &rect); |
| 125 | this->draw(canvas, 410, 210, kernelOffset, MCIF::kRepeat_TileMode, false, &rect); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | private: |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 129 | SkBitmap fBitmap; |
Brian Osman | 878df6d | 2016-12-27 11:04:12 -0500 | [diff] [blame] | 130 | SkColor fColors[2]; |
| 131 | const char* fNameSuffix; |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 132 | |
| 133 | typedef GM INHERITED; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | ////////////////////////////////////////////////////////////////////////////// |
| 137 | |
Brian Osman | 878df6d | 2016-12-27 11:04:12 -0500 | [diff] [blame] | 138 | DEF_GM(return new MatrixConvolutionGM(0xFFFFFFFF, 0x40404040, "");) |
| 139 | DEF_GM(return new MatrixConvolutionGM(0xFFFF0000, 0xFF00FF00, "_color");) |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 140 | |
| 141 | } |