robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 10 | #include "SkBlurMask.h" |
| 11 | #include "SkBlurMaskFilter.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 12 | #include "SkPath.h" |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 13 | |
| 14 | namespace skiagm { |
| 15 | |
| 16 | // This GM exercises the blurred rect nine-patching special cases when the |
| 17 | // 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] | 18 | // 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] | 19 | // middle. |
| 20 | class BigBlursGM : public GM { |
| 21 | public: |
| 22 | BigBlursGM() { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 23 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | SkString onShortName() override { |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 28 | return SkString("bigblurs"); |
| 29 | } |
| 30 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 31 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 32 | return SkISize::Make(kWidth, kHeight); |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 33 | } |
| 34 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 36 | constexpr int kBig = 65536; |
| 37 | const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)); |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 38 | |
| 39 | const SkRect bigRect = SkRect::MakeWH(SkIntToScalar(kBig), SkIntToScalar(kBig)); |
| 40 | SkRect insetRect = bigRect; |
| 41 | insetRect.inset(20, 20); |
| 42 | |
| 43 | SkPath rectori; |
| 44 | |
| 45 | rectori.addRect(bigRect); |
| 46 | rectori.addRect(insetRect, SkPath::kCCW_Direction); |
| 47 | |
| 48 | // The blur extends 3*kSigma out from the big rect. |
| 49 | // Offset the close-up windows so we get the entire blur |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 50 | const SkScalar kLeftTopPad = 3*kSigma; // use on left & up of big rect |
| 51 | const SkScalar kRightBotPad = kCloseUpSize-3*kSigma; // use on right and bot sides |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 52 | |
| 53 | // UL hand corners of the rendered closeups |
| 54 | const SkPoint origins[] = { |
| 55 | { -kLeftTopPad, -kLeftTopPad }, // UL |
| 56 | { kBig-kRightBotPad, -kLeftTopPad }, // UR |
| 57 | { kBig-kRightBotPad, kBig-kRightBotPad }, // LR |
| 58 | { -kLeftTopPad, kBig-kRightBotPad }, // LL |
| 59 | { kBig/2-kCloseUpSize/2, kBig/2-kCloseUpSize/2 }, // center |
| 60 | }; |
| 61 | |
| 62 | SkPaint outlinePaint; |
| 63 | outlinePaint.setColor(SK_ColorRED); |
| 64 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 65 | |
| 66 | SkPaint blurPaint; |
| 67 | blurPaint.setAntiAlias(true); |
| 68 | blurPaint.setColor(SK_ColorBLACK); |
| 69 | |
| 70 | int desiredX = 0, desiredY = 0; |
| 71 | |
| 72 | for (int i = 0; i < 2; ++i) { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 73 | for (int j = 0; j <= kLastEnum_SkBlurStyle; ++j) { |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 74 | blurPaint.setMaskFilter(SkBlurMaskFilter::Make((SkBlurStyle)j, kSigma)); |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 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 | |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 84 | canvas->clipRect(clipRect); |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 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: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 107 | static constexpr int kCloseUpSize = 64; |
| 108 | static constexpr int kWidth = 5 * kCloseUpSize; |
| 109 | static constexpr 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 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 114 | DEF_GM(return new BigBlursGM;) |
robertphillips@google.com | f3db546 | 2013-11-07 22:43:04 +0000 | [diff] [blame] | 115 | } |