robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2013 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "gm.h" |
| 10 | #include "SkBlurMask.h" |
| 11 | #include "SkBlurMaskFilter.h" |
| 12 | |
| 13 | namespace skiagm { |
| 14 | |
| 15 | // This GM exercises the blurred rect nine-patching special cases when the |
| 16 | // blurred rect is very large and/or very far from the origin. |
skia.committer@gmail.com | ab7442c | 2013-11-08 07:01:56 +0000 | [diff] [blame] | 17 | // It creates a large blurred rect/rectori then renders the 4 corners and the |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 18 | // middle. |
| 19 | class BigBlursGM : public GM { |
| 20 | public: |
| 21 | BigBlursGM() { |
| 22 | this->setBGColor(0xFFDDDDDD); |
| 23 | } |
| 24 | |
| 25 | protected: |
| 26 | virtual SkString onShortName() SK_OVERRIDE { |
| 27 | return SkString("bigblurs"); |
| 28 | } |
| 29 | |
| 30 | virtual SkISize onISize() SK_OVERRIDE { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 31 | return SkISize::Make(kWidth, kHeight); |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 35 | static const int kBig = 65536; |
| 36 | static const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)); |
| 37 | |
| 38 | const SkRect bigRect = SkRect::MakeWH(SkIntToScalar(kBig), SkIntToScalar(kBig)); |
| 39 | SkRect insetRect = bigRect; |
| 40 | insetRect.inset(20, 20); |
| 41 | |
| 42 | SkPath rectori; |
| 43 | |
| 44 | rectori.addRect(bigRect); |
| 45 | rectori.addRect(insetRect, SkPath::kCCW_Direction); |
| 46 | |
| 47 | // The blur extends 3*kSigma out from the big rect. |
| 48 | // Offset the close-up windows so we get the entire blur |
| 49 | static const SkScalar kLeftTopPad = 3*kSigma; // use on left & up of big rect |
| 50 | static const SkScalar kRightBotPad = kCloseUpSize-3*kSigma; // use on right and bot sides |
| 51 | |
| 52 | // UL hand corners of the rendered closeups |
| 53 | const SkPoint origins[] = { |
| 54 | { -kLeftTopPad, -kLeftTopPad }, // UL |
| 55 | { kBig-kRightBotPad, -kLeftTopPad }, // UR |
| 56 | { kBig-kRightBotPad, kBig-kRightBotPad }, // LR |
| 57 | { -kLeftTopPad, kBig-kRightBotPad }, // LL |
| 58 | { kBig/2-kCloseUpSize/2, kBig/2-kCloseUpSize/2 }, // center |
| 59 | }; |
| 60 | |
| 61 | SkPaint outlinePaint; |
| 62 | outlinePaint.setColor(SK_ColorRED); |
| 63 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 64 | |
| 65 | SkPaint blurPaint; |
| 66 | blurPaint.setAntiAlias(true); |
| 67 | blurPaint.setColor(SK_ColorBLACK); |
| 68 | |
| 69 | int desiredX = 0, desiredY = 0; |
| 70 | |
| 71 | for (int i = 0; i < 2; ++i) { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 72 | for (int j = 0; j <= kLastEnum_SkBlurStyle; ++j) { |
| 73 | SkMaskFilter* mf = SkBlurMaskFilter::Create((SkBlurStyle)j, kSigma); |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 74 | blurPaint.setMaskFilter(mf)->unref(); |
| 75 | |
| 76 | for (int k = 0; k < (int)SK_ARRAY_COUNT(origins); ++k) { |
| 77 | canvas->save(); |
| 78 | |
skia.committer@gmail.com | ab7442c | 2013-11-08 07:01:56 +0000 | [diff] [blame] | 79 | SkRect clipRect = SkRect::MakeXYWH(SkIntToScalar(desiredX), |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 80 | SkIntToScalar(desiredY), |
skia.committer@gmail.com | ab7442c | 2013-11-08 07:01:56 +0000 | [diff] [blame] | 81 | SkIntToScalar(kCloseUpSize), |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 82 | SkIntToScalar(kCloseUpSize)); |
| 83 | |
| 84 | canvas->clipRect(clipRect, SkRegion::kReplace_Op, false); |
| 85 | |
skia.committer@gmail.com | ab7442c | 2013-11-08 07:01:56 +0000 | [diff] [blame] | 86 | canvas->translate(desiredX-origins[k].fX, |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 87 | desiredY-origins[k].fY); |
| 88 | |
| 89 | if (0 == i) { |
| 90 | canvas->drawRect(bigRect, blurPaint); |
| 91 | } else { |
| 92 | canvas->drawPath(rectori, blurPaint); |
| 93 | } |
| 94 | canvas->restore(); |
| 95 | canvas->drawRect(clipRect, outlinePaint); |
| 96 | |
| 97 | desiredX += kCloseUpSize; |
| 98 | } |
| 99 | |
| 100 | desiredX = 0; |
| 101 | desiredY += kCloseUpSize; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | static const int kCloseUpSize = 64; |
| 108 | static const int kWidth = 5 * kCloseUpSize; |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 109 | static const int kHeight = 2 * (kLastEnum_SkBlurStyle + 1) * kCloseUpSize; |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 110 | |
| 111 | typedef GM INHERITED; |
| 112 | }; |
| 113 | |
| 114 | DEF_GM( return SkNEW(BigBlursGM); ) |
| 115 | |
robertphillips@google.com | f3db546 | 2013-11-07 22:43:04 +0000 | [diff] [blame] | 116 | } |