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" |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 9 | #include "SkColor.h" |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 10 | #include "SkMatrixConvolutionImageFilter.h" |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 11 | #include "SkGradientShader.h" |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 12 | |
| 13 | namespace skiagm { |
| 14 | |
| 15 | class MatrixConvolutionGM : public GM { |
| 16 | public: |
| 17 | MatrixConvolutionGM() : fInitialized(false) { |
| 18 | this->setBGColor(0x00000000); |
| 19 | } |
| 20 | |
| 21 | protected: |
| 22 | virtual SkString onShortName() { |
| 23 | return SkString("matrixconvolution"); |
| 24 | } |
| 25 | |
| 26 | void make_bitmap() { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 27 | fBitmap.allocN32Pixels(80, 80); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 28 | SkCanvas canvas(fBitmap); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 29 | canvas.clear(0x00000000); |
| 30 | SkPaint paint; |
| 31 | paint.setAntiAlias(true); |
| 32 | paint.setColor(0xFFFFFFFF); |
| 33 | paint.setTextSize(SkIntToScalar(180)); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 34 | SkPoint pts[2] = { SkPoint::Make(0, 0), |
| 35 | SkPoint::Make(0, SkIntToScalar(80)) }; |
| 36 | SkColor colors[2] = { 0xFFFFFFFF, 0x40404040 }; |
| 37 | SkScalar pos[2] = { 0, SkIntToScalar(80) }; |
| 38 | paint.setShader(SkGradientShader::CreateLinear( |
| 39 | pts, colors, pos, 2, SkShader::kClamp_TileMode))->unref(); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 40 | const char* str = "e"; |
| 41 | canvas.drawText(str, strlen(str), SkIntToScalar(-10), SkIntToScalar(80), paint); |
| 42 | } |
| 43 | |
| 44 | virtual SkISize onISize() { |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 45 | return make_isize(500, 300); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 46 | } |
| 47 | |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 48 | void draw(SkCanvas* canvas, int x, int y, const SkIPoint& kernelOffset, |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 49 | SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha, |
| 50 | const SkImageFilter::CropRect* cropRect = NULL) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 51 | SkScalar kernel[9] = { |
| 52 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 53 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 54 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 55 | }; |
| 56 | SkISize kernelSize = SkISize::Make(3, 3); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 57 | SkScalar gain = 0.3f, bias = SkIntToScalar(100); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 58 | SkPaint paint; |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 59 | SkAutoTUnref<SkImageFilter> filter( |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 60 | SkMatrixConvolutionImageFilter::Create(kernelSize, |
| 61 | kernel, |
| 62 | gain, |
| 63 | bias, |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 64 | kernelOffset, |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 65 | tileMode, |
| 66 | convolveAlpha, |
| 67 | NULL, |
| 68 | cropRect)); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 69 | paint.setImageFilter(filter); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 70 | canvas->save(); |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 71 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 72 | canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), |
| 73 | SkIntToScalar(fBitmap.height()))); |
| 74 | canvas->drawBitmap(fBitmap, 0, 0, &paint); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 75 | canvas->restore(); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 76 | } |
| 77 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 78 | typedef SkMatrixConvolutionImageFilter MCIF; |
| 79 | |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 80 | virtual void onDraw(SkCanvas* canvas) { |
| 81 | if (!fInitialized) { |
| 82 | make_bitmap(); |
| 83 | fInitialized = true; |
| 84 | } |
| 85 | canvas->clear(0x00000000); |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 86 | SkIPoint kernelOffset = SkIPoint::Make(1, 0); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 87 | for (int x = 10; x < 310; x += 100) { |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 88 | this->draw(canvas, x, 10, kernelOffset, MCIF::kClamp_TileMode, true); |
| 89 | this->draw(canvas, x, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true); |
| 90 | this->draw(canvas, x, 210, kernelOffset, MCIF::kRepeat_TileMode, true); |
| 91 | kernelOffset.fY++; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 92 | } |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 93 | kernelOffset.fY = 1; |
senorblanco@chromium.org | 2a4223c | 2013-10-20 20:21:19 +0000 | [diff] [blame] | 94 | SkImageFilter::CropRect rect(SkRect::MakeXYWH(10, 5, 60, 60)); |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 95 | this->draw(canvas, 310, 10, kernelOffset, MCIF::kClamp_TileMode, true, &rect); |
| 96 | this->draw(canvas, 310, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &rect); |
| 97 | this->draw(canvas, 310, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &rect); |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 98 | |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 99 | this->draw(canvas, 410, 10, kernelOffset, MCIF::kClamp_TileMode, false); |
| 100 | this->draw(canvas, 410, 110, kernelOffset, MCIF::kClampToBlack_TileMode, false); |
| 101 | this->draw(canvas, 410, 210, kernelOffset, MCIF::kRepeat_TileMode, false); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | private: |
| 105 | typedef GM INHERITED; |
| 106 | SkBitmap fBitmap; |
| 107 | bool fInitialized; |
| 108 | }; |
| 109 | |
| 110 | ////////////////////////////////////////////////////////////////////////////// |
| 111 | |
| 112 | static GM* MyFactory(void*) { return new MatrixConvolutionGM; } |
| 113 | static GMRegistry reg(MyFactory); |
| 114 | |
| 115 | } |