senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +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 "SkBlurImageFilter.h" |
| 10 | #include "SkRandom.h" |
| 11 | |
| 12 | #define WIDTH 640 |
| 13 | #define HEIGHT 480 |
| 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 17 | class ImageBlurTiledGM : public GM { |
| 18 | public: |
| 19 | ImageBlurTiledGM(SkScalar sigmaX, SkScalar sigmaY) |
| 20 | : fSigmaX(sigmaX), fSigmaY(sigmaY) { |
| 21 | } |
| 22 | |
| 23 | protected: |
| 24 | virtual SkString onShortName() { |
| 25 | return SkString("imageblurtiled"); |
| 26 | } |
| 27 | |
| 28 | virtual SkISize onISize() { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 29 | return SkISize::Make(WIDTH, HEIGHT); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | virtual void onDraw(SkCanvas* canvas) { |
| 33 | SkPaint paint; |
xidachen | 467ddc0 | 2015-12-10 12:08:44 -0800 | [diff] [blame] | 34 | SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(fSigmaX, fSigmaY)); |
| 35 | paint.setImageFilter(blur); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 36 | const SkScalar tile_size = SkIntToScalar(128); |
| 37 | SkRect bounds; |
reed@google.com | 76b12b7 | 2014-04-10 21:15:30 +0000 | [diff] [blame] | 38 | if (!canvas->getClipBounds(&bounds)) { |
| 39 | bounds.setEmpty(); |
| 40 | } |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 41 | for (SkScalar y = bounds.top(); y < bounds.bottom(); y += tile_size) { |
| 42 | for (SkScalar x = bounds.left(); x < bounds.right(); x += tile_size) { |
| 43 | canvas->save(); |
| 44 | canvas->clipRect(SkRect::MakeXYWH(x, y, tile_size, tile_size)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 45 | canvas->saveLayer(nullptr, &paint); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 46 | const char* str[] = { |
| 47 | "The quick", |
| 48 | "brown fox", |
| 49 | "jumped over", |
| 50 | "the lazy dog.", |
| 51 | }; |
| 52 | SkPaint textPaint; |
| 53 | textPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 54 | sk_tool_utils::set_portable_typeface(&textPaint); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 55 | textPaint.setTextSize(SkIntToScalar(100)); |
| 56 | int posY = 0; |
| 57 | for (unsigned i = 0; i < SK_ARRAY_COUNT(str); i++) { |
| 58 | posY += 100; |
| 59 | canvas->drawText(str[i], strlen(str[i]), SkIntToScalar(0), |
| 60 | SkIntToScalar(posY), textPaint); |
| 61 | } |
| 62 | canvas->restore(); |
| 63 | canvas->restore(); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | SkScalar fSigmaX; |
| 70 | SkScalar fSigmaY; |
| 71 | |
| 72 | typedef GM INHERITED; |
| 73 | }; |
| 74 | |
| 75 | ////////////////////////////////////////////////////////////////////////////// |
| 76 | |
| 77 | static GM* MyFactory1(void*) { return new ImageBlurTiledGM(3.0f, 3.0f); } |
| 78 | static GMRegistry reg1(MyFactory1); |
| 79 | |
| 80 | } |