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 |
Brian Osman | c69c441 | 2018-07-10 15:50:11 -0400 | [diff] [blame] | 34 | fBitmap.allocN32Pixels(80, 80); |
| 35 | SkCanvas canvas(fBitmap); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 36 | canvas.clear(0x00000000); |
| 37 | SkPaint paint; |
| 38 | paint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 39 | sk_tool_utils::set_portable_typeface(&paint); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 40 | paint.setColor(0xFFFFFFFF); |
| 41 | paint.setTextSize(SkIntToScalar(180)); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 42 | SkPoint pts[2] = { SkPoint::Make(0, 0), |
| 43 | SkPoint::Make(0, SkIntToScalar(80)) }; |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 44 | SkScalar pos[2] = { 0, SkIntToScalar(80) }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 45 | paint.setShader(SkGradientShader::MakeLinear( |
Brian Osman | 878df6d | 2016-12-27 11:04:12 -0500 | [diff] [blame] | 46 | pts, fColors, pos, 2, SkShader::kClamp_TileMode)); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 47 | const char* str = "e"; |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 48 | canvas.drawString(str, SkIntToScalar(-10), SkIntToScalar(80), paint); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 49 | } |
| 50 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 51 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 52 | return SkISize::Make(500, 300); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 53 | } |
| 54 | |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 55 | void draw(SkCanvas* canvas, int x, int y, const SkIPoint& kernelOffset, |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 56 | SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 57 | const SkImageFilter::CropRect* cropRect = nullptr) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 58 | SkScalar kernel[9] = { |
| 59 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 60 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 61 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 62 | }; |
| 63 | SkISize kernelSize = SkISize::Make(3, 3); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 64 | SkScalar gain = 0.3f, bias = SkIntToScalar(100); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 65 | SkPaint paint; |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 66 | paint.setImageFilter(SkMatrixConvolutionImageFilter::Make(kernelSize, |
| 67 | kernel, |
| 68 | gain, |
| 69 | bias, |
| 70 | kernelOffset, |
| 71 | tileMode, |
| 72 | convolveAlpha, |
| 73 | nullptr, |
| 74 | cropRect)); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 75 | canvas->save(); |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 76 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 77 | const SkRect layerBounds = SkRect::MakeIWH(fBitmap.width(), fBitmap.height()); |
| 78 | canvas->clipRect(layerBounds); |
| 79 | // This GM is, in part, intended to display the wrapping behavior of the |
| 80 | // matrix image filter. The only (rational) way to achieve that for repeat mode |
| 81 | // is to create a tight layer. |
| 82 | canvas->saveLayer(layerBounds, &paint); |
| 83 | canvas->drawBitmap(fBitmap, 0, 0, nullptr); |
| 84 | canvas->restore(); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 85 | canvas->restore(); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 86 | } |
| 87 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 88 | typedef SkMatrixConvolutionImageFilter MCIF; |
| 89 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 90 | void onOnceBeforeDraw() override { |
| 91 | this->makeBitmap(); |
| 92 | } |
| 93 | |
| 94 | void onDraw(SkCanvas* canvas) override { |
senorblanco | 16b254a | 2015-04-09 11:13:24 -0700 | [diff] [blame] | 95 | canvas->clear(SK_ColorBLACK); |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 96 | SkIPoint kernelOffset = SkIPoint::Make(1, 0); |
senorblanco | a544eda | 2015-12-07 07:48:34 -0800 | [diff] [blame] | 97 | SkImageFilter::CropRect rect(SkRect::Make(fBitmap.bounds())); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 98 | for (int x = 10; x < 310; x += 100) { |
senorblanco | a544eda | 2015-12-07 07:48:34 -0800 | [diff] [blame] | 99 | this->draw(canvas, x, 10, kernelOffset, MCIF::kClamp_TileMode, true, &rect); |
| 100 | this->draw(canvas, x, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &rect); |
| 101 | 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] | 102 | kernelOffset.fY++; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 103 | } |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 104 | kernelOffset.fY = 1; |
senorblanco | a544eda | 2015-12-07 07:48:34 -0800 | [diff] [blame] | 105 | SkImageFilter::CropRect smallRect(SkRect::MakeXYWH(10, 5, 60, 60)); |
| 106 | this->draw(canvas, 310, 10, kernelOffset, MCIF::kClamp_TileMode, true, &smallRect); |
| 107 | this->draw(canvas, 310, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &smallRect); |
| 108 | this->draw(canvas, 310, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &smallRect); |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 109 | |
senorblanco | a544eda | 2015-12-07 07:48:34 -0800 | [diff] [blame] | 110 | this->draw(canvas, 410, 10, kernelOffset, MCIF::kClamp_TileMode, false, &rect); |
| 111 | this->draw(canvas, 410, 110, kernelOffset, MCIF::kClampToBlack_TileMode, false, &rect); |
| 112 | this->draw(canvas, 410, 210, kernelOffset, MCIF::kRepeat_TileMode, false, &rect); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | private: |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 116 | SkBitmap fBitmap; |
Brian Osman | 878df6d | 2016-12-27 11:04:12 -0500 | [diff] [blame] | 117 | SkColor fColors[2]; |
| 118 | const char* fNameSuffix; |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 119 | |
| 120 | typedef GM INHERITED; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | ////////////////////////////////////////////////////////////////////////////// |
| 124 | |
Brian Osman | 878df6d | 2016-12-27 11:04:12 -0500 | [diff] [blame] | 125 | DEF_GM(return new MatrixConvolutionGM(0xFFFFFFFF, 0x40404040, "");) |
| 126 | DEF_GM(return new MatrixConvolutionGM(0xFFFF0000, 0xFF00FF00, "_color");) |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 127 | |
| 128 | } |