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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 9 | #include "include/core/SkBlurTypes.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkMaskFilter.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 13 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkPath.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 15 | #include "include/core/SkPoint.h" |
| 16 | #include "include/core/SkRect.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkSize.h" |
| 19 | #include "include/core/SkString.h" |
| 20 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/core/SkBlurMask.h" |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 22 | |
| 23 | namespace skiagm { |
| 24 | |
| 25 | // This GM exercises the blurred rect nine-patching special cases when the |
| 26 | // 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] | 27 | // 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] | 28 | // middle. |
| 29 | class BigBlursGM : public GM { |
| 30 | public: |
| 31 | BigBlursGM() { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 32 | this->setBGColor(0xFFDDDDDD); |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 36 | SkString onShortName() override { |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 37 | return SkString("bigblurs"); |
| 38 | } |
| 39 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 40 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 41 | return SkISize::Make(kWidth, kHeight); |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 42 | } |
| 43 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 45 | constexpr int kBig = 65536; |
| 46 | const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)); |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 47 | |
| 48 | const SkRect bigRect = SkRect::MakeWH(SkIntToScalar(kBig), SkIntToScalar(kBig)); |
| 49 | SkRect insetRect = bigRect; |
| 50 | insetRect.inset(20, 20); |
| 51 | |
| 52 | SkPath rectori; |
| 53 | |
| 54 | rectori.addRect(bigRect); |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 55 | rectori.addRect(insetRect, SkPathDirection::kCCW); |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 56 | |
| 57 | // The blur extends 3*kSigma out from the big rect. |
| 58 | // Offset the close-up windows so we get the entire blur |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 59 | const SkScalar kLeftTopPad = 3*kSigma; // use on left & up of big rect |
| 60 | 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] | 61 | |
| 62 | // UL hand corners of the rendered closeups |
| 63 | const SkPoint origins[] = { |
| 64 | { -kLeftTopPad, -kLeftTopPad }, // UL |
| 65 | { kBig-kRightBotPad, -kLeftTopPad }, // UR |
| 66 | { kBig-kRightBotPad, kBig-kRightBotPad }, // LR |
| 67 | { -kLeftTopPad, kBig-kRightBotPad }, // LL |
| 68 | { kBig/2-kCloseUpSize/2, kBig/2-kCloseUpSize/2 }, // center |
| 69 | }; |
| 70 | |
| 71 | SkPaint outlinePaint; |
| 72 | outlinePaint.setColor(SK_ColorRED); |
| 73 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 74 | |
| 75 | SkPaint blurPaint; |
| 76 | blurPaint.setAntiAlias(true); |
| 77 | blurPaint.setColor(SK_ColorBLACK); |
| 78 | |
| 79 | int desiredX = 0, desiredY = 0; |
| 80 | |
| 81 | for (int i = 0; i < 2; ++i) { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 82 | for (int j = 0; j <= kLastEnum_SkBlurStyle; ++j) { |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 83 | blurPaint.setMaskFilter(SkMaskFilter::MakeBlur((SkBlurStyle)j, kSigma)); |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 84 | |
| 85 | for (int k = 0; k < (int)SK_ARRAY_COUNT(origins); ++k) { |
| 86 | canvas->save(); |
| 87 | |
skia.committer@gmail.com | ab7442c | 2013-11-08 07:01:56 +0000 | [diff] [blame] | 88 | SkRect clipRect = SkRect::MakeXYWH(SkIntToScalar(desiredX), |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 89 | SkIntToScalar(desiredY), |
skia.committer@gmail.com | ab7442c | 2013-11-08 07:01:56 +0000 | [diff] [blame] | 90 | SkIntToScalar(kCloseUpSize), |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 91 | SkIntToScalar(kCloseUpSize)); |
| 92 | |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 93 | canvas->clipRect(clipRect); |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 94 | |
skia.committer@gmail.com | ab7442c | 2013-11-08 07:01:56 +0000 | [diff] [blame] | 95 | canvas->translate(desiredX-origins[k].fX, |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 96 | desiredY-origins[k].fY); |
| 97 | |
| 98 | if (0 == i) { |
| 99 | canvas->drawRect(bigRect, blurPaint); |
| 100 | } else { |
| 101 | canvas->drawPath(rectori, blurPaint); |
| 102 | } |
| 103 | canvas->restore(); |
| 104 | canvas->drawRect(clipRect, outlinePaint); |
| 105 | |
| 106 | desiredX += kCloseUpSize; |
| 107 | } |
| 108 | |
| 109 | desiredX = 0; |
| 110 | desiredY += kCloseUpSize; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 116 | static constexpr int kCloseUpSize = 64; |
| 117 | static constexpr int kWidth = 5 * kCloseUpSize; |
| 118 | static constexpr int kHeight = 2 * (kLastEnum_SkBlurStyle + 1) * kCloseUpSize; |
robertphillips@google.com | 96ac2f6 | 2013-11-07 22:25:21 +0000 | [diff] [blame] | 119 | |
| 120 | typedef GM INHERITED; |
| 121 | }; |
| 122 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 123 | DEF_GM(return new BigBlursGM;) |
robertphillips@google.com | f3db546 | 2013-11-07 22:43:04 +0000 | [diff] [blame] | 124 | } |