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() { |
| 27 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80); |
| 28 | fBitmap.allocPixels(); |
| 29 | SkDevice device(fBitmap); |
| 30 | SkCanvas canvas(&device); |
| 31 | canvas.clear(0x00000000); |
| 32 | SkPaint paint; |
| 33 | paint.setAntiAlias(true); |
| 34 | paint.setColor(0xFFFFFFFF); |
| 35 | paint.setTextSize(SkIntToScalar(180)); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 36 | SkPoint pts[2] = { SkPoint::Make(0, 0), |
| 37 | SkPoint::Make(0, SkIntToScalar(80)) }; |
| 38 | SkColor colors[2] = { 0xFFFFFFFF, 0x40404040 }; |
| 39 | SkScalar pos[2] = { 0, SkIntToScalar(80) }; |
| 40 | paint.setShader(SkGradientShader::CreateLinear( |
| 41 | pts, colors, pos, 2, SkShader::kClamp_TileMode))->unref(); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 42 | const char* str = "e"; |
| 43 | canvas.drawText(str, strlen(str), SkIntToScalar(-10), SkIntToScalar(80), paint); |
| 44 | } |
| 45 | |
| 46 | virtual SkISize onISize() { |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 47 | return make_isize(400, 300); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 48 | } |
| 49 | |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 50 | void draw(SkCanvas* canvas, int x, int y, const SkIPoint& target, SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha) { |
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); |
| 57 | SkScalar gain = SkFloatToScalar(0.3f), bias = SkIntToScalar(100); |
| 58 | SkPaint paint; |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 59 | SkAutoTUnref<SkImageFilter> filter(SkNEW_ARGS(SkMatrixConvolutionImageFilter, (kernelSize, kernel, gain, bias, target, tileMode, convolveAlpha))); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 60 | paint.setImageFilter(filter); |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 61 | canvas->save(); |
| 62 | canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
| 63 | SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); |
| 64 | canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); |
| 65 | canvas->restore(); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | virtual void onDraw(SkCanvas* canvas) { |
| 69 | if (!fInitialized) { |
| 70 | make_bitmap(); |
| 71 | fInitialized = true; |
| 72 | } |
| 73 | canvas->clear(0x00000000); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 74 | SkIPoint target = SkIPoint::Make(1, 0); |
| 75 | for (int x = 10; x < 310; x += 100) { |
| 76 | draw(canvas, x, 10, target, SkMatrixConvolutionImageFilter::kClamp_TileMode, true); |
| 77 | draw(canvas, x, 110, target, SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, true); |
| 78 | draw(canvas, x, 210, target, SkMatrixConvolutionImageFilter::kRepeat_TileMode, true); |
| 79 | target.fY++; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 80 | } |
senorblanco@chromium.org | 8640d50 | 2012-09-25 14:32:42 +0000 | [diff] [blame] | 81 | target.fY = 1; |
| 82 | draw(canvas, 310, 10, target, SkMatrixConvolutionImageFilter::kClamp_TileMode, false); |
senorblanco@chromium.org | 3bc16c8 | 2012-10-04 17:18:20 +0000 | [diff] [blame] | 83 | draw(canvas, 310, 110, target, SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, false); |
| 84 | draw(canvas, 310, 210, target, SkMatrixConvolutionImageFilter::kRepeat_TileMode, false); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | private: |
| 88 | typedef GM INHERITED; |
| 89 | SkBitmap fBitmap; |
| 90 | bool fInitialized; |
| 91 | }; |
| 92 | |
| 93 | ////////////////////////////////////////////////////////////////////////////// |
| 94 | |
| 95 | static GM* MyFactory(void*) { return new MatrixConvolutionGM; } |
| 96 | static GMRegistry reg(MyFactory); |
| 97 | |
| 98 | } |