senorblanco@chromium.org | fd0ec2c | 2014-03-25 17:35:10 +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 "SkColor.h" |
| 10 | #include "SkMatrixImageFilter.h" |
| 11 | |
| 12 | namespace skiagm { |
| 13 | |
| 14 | class MatrixImageFilterGM : public GM { |
| 15 | public: |
| 16 | MatrixImageFilterGM() { |
| 17 | this->setBGColor(0x00000000); |
| 18 | } |
| 19 | |
| 20 | protected: |
| 21 | virtual SkString onShortName() { |
| 22 | return SkString("matriximagefilter"); |
| 23 | } |
| 24 | |
| 25 | void draw(SkCanvas* canvas, const SkRect& rect, const SkBitmap& bitmap, |
| 26 | const SkMatrix& matrix, SkPaint::FilterLevel filterLevel) { |
| 27 | SkAutoTUnref<SkImageFilter> imageFilter( |
| 28 | SkMatrixImageFilter::Create(matrix, filterLevel)); |
| 29 | SkPaint paint; |
| 30 | paint.setImageFilter(imageFilter.get()); |
| 31 | canvas->saveLayer(&rect, &paint); |
| 32 | canvas->drawBitmap(bitmap, 0, 0); |
| 33 | canvas->restore(); |
| 34 | } |
| 35 | |
| 36 | virtual SkISize onISize() { |
| 37 | return make_isize(420, 100); |
| 38 | } |
| 39 | |
| 40 | void make_checkerboard(SkBitmap* bitmap) { |
| 41 | bitmap->allocN32Pixels(64, 64); |
| 42 | SkCanvas canvas(*bitmap); |
| 43 | SkPaint darkPaint; |
| 44 | darkPaint.setColor(0xFF404040); |
| 45 | SkPaint lightPaint; |
| 46 | lightPaint.setColor(0xFFA0A0A0); |
| 47 | for (int y = 0; y < 64; y += 32) { |
| 48 | for (int x = 0; x < 64; x += 32) { |
| 49 | canvas.save(); |
| 50 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 51 | canvas.drawRect(SkRect::MakeXYWH(0, 0, 16, 16), darkPaint); |
| 52 | canvas.drawRect(SkRect::MakeXYWH(16, 0, 16, 16), lightPaint); |
| 53 | canvas.drawRect(SkRect::MakeXYWH(0, 16, 16, 16), lightPaint); |
| 54 | canvas.drawRect(SkRect::MakeXYWH(16, 16, 16, 16), darkPaint); |
| 55 | canvas.restore(); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | virtual void onDraw(SkCanvas* canvas) { |
| 61 | canvas->clear(0x00000000); |
| 62 | SkPaint paint; |
| 63 | SkMatrix matrix; |
| 64 | SkScalar margin = SkIntToScalar(10); |
| 65 | matrix.setSkew(SkDoubleToScalar(0.5), SkDoubleToScalar(0.2)); |
| 66 | SkRect srcBounds; |
| 67 | SkBitmap checkerboard; |
| 68 | make_checkerboard(&checkerboard); |
| 69 | checkerboard.getBounds(&srcBounds); |
| 70 | // Outer paint does no filtering; leave it all for the filter |
| 71 | paint.setFilterLevel(SkPaint::kNone_FilterLevel); |
| 72 | |
| 73 | SkRect srcRect = SkRect::MakeWH(96, 96); |
| 74 | |
| 75 | canvas->translate(margin, margin); |
| 76 | draw(canvas, srcRect, checkerboard, matrix, SkPaint::kNone_FilterLevel); |
| 77 | |
| 78 | canvas->translate(srcRect.width() + margin, 0); |
| 79 | draw(canvas, srcRect, checkerboard, matrix, SkPaint::kLow_FilterLevel); |
| 80 | |
| 81 | canvas->translate(srcRect.width() + margin, 0); |
| 82 | draw(canvas, srcRect, checkerboard, matrix, SkPaint::kMedium_FilterLevel); |
| 83 | |
| 84 | canvas->translate(srcRect.width() + margin, 0); |
| 85 | draw(canvas, srcRect, checkerboard, matrix, SkPaint::kHigh_FilterLevel); |
| 86 | } |
| 87 | |
| 88 | private: |
| 89 | typedef GM INHERITED; |
| 90 | }; |
| 91 | |
| 92 | ////////////////////////////////////////////////////////////////////////////// |
| 93 | |
| 94 | static GM* MyFactory(void*) { return new MatrixImageFilterGM; } |
| 95 | static GMRegistry reg(MyFactory); |
| 96 | |
| 97 | } |