scroggo@google.com | 7b05659 | 2013-11-05 15:57: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" |
| 9 | #include "SkBlurMask.h" |
| 10 | #include "SkBlurMaskFilter.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkColorFilter.h" |
| 13 | #include "SkLayerDrawLooper.h" |
| 14 | #include "SkPaint.h" |
| 15 | #include "SkPath.h" |
| 16 | #include "SkPoint.h" |
| 17 | #include "SkRect.h" |
| 18 | #include "SkRRect.h" |
| 19 | #include "SkString.h" |
| 20 | #include "SkXfermode.h" |
| 21 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 22 | // This GM mimics a blurred RR seen in the wild. |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 23 | class BlurRoundRectGM : public skiagm::GM { |
| 24 | public: |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 25 | BlurRoundRectGM(int width, int height) |
herb | b10fe49 | 2016-01-08 13:48:43 -0800 | [diff] [blame] | 26 | : fName("blurroundrect"), fWidth(width), fHeight(height) { |
scroggo | ef470f1 | 2014-06-03 06:50:26 -0700 | [diff] [blame] | 27 | fName.appendf("-WH-%ix%i-unevenCorners", width, height); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 28 | } |
| 29 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | SkString onShortName() override { |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 31 | return fName; |
| 32 | } |
| 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkISize onISize() override { |
herb | b10fe49 | 2016-01-08 13:48:43 -0800 | [diff] [blame] | 35 | return SkISize::Make(fWidth, fHeight); |
| 36 | } |
| 37 | |
| 38 | void onOnceBeforeDraw() override { |
| 39 | SkVector radii[4]; |
| 40 | radii[0].set(SkIntToScalar(30), SkIntToScalar(30)); |
| 41 | radii[1].set(SkIntToScalar(10), SkIntToScalar(10)); |
| 42 | radii[2].set(SkIntToScalar(30), SkIntToScalar(30)); |
| 43 | radii[3].set(SkIntToScalar(10), SkIntToScalar(10)); |
| 44 | SkRect r = SkRect::MakeWH(SkIntToScalar(fWidth), SkIntToScalar(fHeight)); |
| 45 | fRRect.setRectRadii(r, radii); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 46 | } |
| 47 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 48 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 49 | SkLayerDrawLooper::Builder looperBuilder; |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 50 | { |
| 51 | SkLayerDrawLooper::LayerInfo info; |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 52 | info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit |
| 53 | | SkLayerDrawLooper::kColorFilter_Bit; |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 54 | info.fColorMode = SkXfermode::kSrc_Mode; |
| 55 | info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0)); |
| 56 | info.fPostTranslate = false; |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 57 | SkPaint* paint = looperBuilder.addLayerOnTop(info); |
robertphillips@google.com | 6c1e49a | 2013-11-10 15:08:45 +0000 | [diff] [blame] | 58 | SkMaskFilter* maskFilter = SkBlurMaskFilter::Create( |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 59 | kNormal_SkBlurStyle, |
robertphillips@google.com | 6c1e49a | 2013-11-10 15:08:45 +0000 | [diff] [blame] | 60 | SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf), |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 61 | SkBlurMaskFilter::kHighQuality_BlurFlag); |
| 62 | paint->setMaskFilter(maskFilter)->unref(); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 63 | SkColorFilter* colorFilter = SkColorFilter::CreateModeFilter( |
| 64 | sk_tool_utils::color_to_565(SK_ColorLTGRAY), |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 65 | SkXfermode::kSrcIn_Mode); |
| 66 | paint->setColorFilter(colorFilter)->unref(); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 67 | paint->setColor(sk_tool_utils::color_to_565(SK_ColorGRAY)); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 68 | } |
| 69 | { |
| 70 | SkLayerDrawLooper::LayerInfo info; |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 71 | looperBuilder.addLayerOnTop(info); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 72 | } |
| 73 | SkPaint paint; |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 74 | canvas->drawRect(fRRect.rect(), paint); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 75 | |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 76 | paint.setLooper(looperBuilder.detachLooper())->unref(); |
scroggo@google.com | 5fdef98 | 2013-11-05 16:28:38 +0000 | [diff] [blame] | 77 | paint.setColor(SK_ColorCYAN); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 78 | paint.setAntiAlias(true); |
| 79 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 80 | canvas->drawRRect(fRRect, paint); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | private: |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 84 | SkString fName; |
| 85 | SkRRect fRRect; |
herb | b10fe49 | 2016-01-08 13:48:43 -0800 | [diff] [blame] | 86 | int fWidth, fHeight; |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 87 | |
| 88 | typedef skiagm::GM INHERITED; |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 91 | #include "SkGradientShader.h" |
| 92 | /* |
| 93 | * Spits out a dummy gradient to test blur with shader on paint |
| 94 | */ |
| 95 | static SkShader* MakeRadial() { |
| 96 | SkPoint pts[2] = { |
| 97 | { 0, 0 }, |
| 98 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 99 | }; |
| 100 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
| 101 | const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, }; |
| 102 | const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 103 | SkMatrix scale; |
| 104 | scale.setScale(0.5f, 0.5f); |
| 105 | scale.postTranslate(5.f, 5.f); |
| 106 | SkPoint center0, center1; |
| 107 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 108 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 109 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 110 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
reed | 71a6cbf | 2015-05-04 08:32:51 -0700 | [diff] [blame] | 111 | return SkGradientShader::CreateTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7, |
| 112 | center0, (pts[1].fX - pts[0].fX) / 2, |
| 113 | colors, pos, SK_ARRAY_COUNT(colors), tm, |
| 114 | 0, &scale); |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 115 | } |
| 116 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 117 | // Simpler blurred RR test cases where all the radii are the same. |
| 118 | class SimpleBlurRoundRectGM : public skiagm::GM { |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 119 | public: |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 120 | SimpleBlurRoundRectGM() |
| 121 | : fName("simpleblurroundrect") { |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | protected: |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 125 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 126 | SkString onShortName() override { |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 127 | return fName; |
| 128 | } |
| 129 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 130 | SkISize onISize() override { |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 131 | return SkISize::Make(1000, 500); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 132 | } |
| 133 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 134 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 135 | canvas->scale(1.5f, 1.5f); |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 136 | canvas->translate(50,50); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 137 | |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 138 | const float blurRadii[] = { 1,5,10,20 }; |
| 139 | const int cornerRadii[] = { 1,5,10,20 }; |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 140 | const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25)); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 141 | for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) { |
| 142 | SkAutoCanvasRestore autoRestore(canvas, true); |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 143 | canvas->translate(0, (r.height() + SkIntToScalar(50)) * i); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 144 | for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) { |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 145 | for (int k = 0; k <= 1; k++) { |
| 146 | SkMaskFilter* filter = SkBlurMaskFilter::Create( |
| 147 | kNormal_SkBlurStyle, |
| 148 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i])), |
| 149 | SkBlurMaskFilter::kHighQuality_BlurFlag); |
| 150 | SkPaint paint; |
| 151 | paint.setColor(SK_ColorBLACK); |
| 152 | paint.setMaskFilter(filter)->unref(); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 153 | |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 154 | bool useRadial = SkToBool(k); |
| 155 | if (useRadial) { |
| 156 | paint.setShader(MakeRadial())->unref(); |
| 157 | } |
| 158 | |
| 159 | SkRRect rrect; |
| 160 | rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]), |
| 161 | SkIntToScalar(cornerRadii[j])); |
| 162 | canvas->drawRRect(rrect, paint); |
| 163 | canvas->translate(r.width() + SkIntToScalar(50), 0); |
| 164 | } |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 167 | } |
| 168 | private: |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 169 | const SkString fName; |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 170 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 171 | typedef skiagm::GM INHERITED; |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | // Create one with dimensions/rounded corners based on the skp |
epoger@google.com | 7e6e80a | 2013-11-05 22:04:53 +0000 | [diff] [blame] | 175 | // |
| 176 | // TODO(scroggo): Disabled in an attempt to rememdy |
| 177 | // https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs: |
| 178 | // ran wrong number of tests') |
| 179 | //DEF_GM(return new BlurRoundRectGM(600, 5514, 6);) |
| 180 | |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 181 | // Rounded rect with two opposite corners with large radii, the other two |
| 182 | // small. |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 183 | DEF_GM(return new BlurRoundRectGM(100, 100);) |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 184 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 185 | DEF_GM(return new SimpleBlurRoundRectGM();) |