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, int radius) |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 26 | : fName("blurroundrect") |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 27 | { |
| 28 | SkRect r = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
| 29 | fRRect.setRectXY(r, SkIntToScalar(radius), SkIntToScalar(radius)); |
scroggo | ef470f1 | 2014-06-03 06:50:26 -0700 | [diff] [blame] | 30 | fName.appendf("-WH-%ix%i-corner-%i", width, height, radius); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | BlurRoundRectGM(int width, int height) |
| 34 | : fName("blurroundrect") { |
scroggo | ef470f1 | 2014-06-03 06:50:26 -0700 | [diff] [blame] | 35 | fName.appendf("-WH-%ix%i-unevenCorners", width, height); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 36 | SkVector radii[4]; |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 37 | radii[0].set(SkIntToScalar(30), SkIntToScalar(30)); |
| 38 | radii[1].set(SkIntToScalar(10), SkIntToScalar(10)); |
| 39 | radii[2].set(SkIntToScalar(30), SkIntToScalar(30)); |
| 40 | radii[3].set(SkIntToScalar(10), SkIntToScalar(10)); |
| 41 | SkRect r = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 42 | fRRect.setRectRadii(r, radii); |
| 43 | } |
| 44 | |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 45 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 46 | return kSkipTiled_Flag; |
| 47 | } |
| 48 | |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 49 | virtual SkString onShortName() SK_OVERRIDE { |
| 50 | return fName; |
| 51 | } |
| 52 | |
| 53 | virtual SkISize onISize() SK_OVERRIDE { |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 54 | return SkISize::Make(SkScalarCeilToInt(fRRect.rect().width()), |
| 55 | SkScalarCeilToInt(fRRect.rect().height())); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 59 | SkLayerDrawLooper::Builder looperBuilder; |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 60 | { |
| 61 | SkLayerDrawLooper::LayerInfo info; |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 62 | info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit |
| 63 | | SkLayerDrawLooper::kColorFilter_Bit; |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 64 | info.fColorMode = SkXfermode::kSrc_Mode; |
| 65 | info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0)); |
| 66 | info.fPostTranslate = false; |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 67 | SkPaint* paint = looperBuilder.addLayerOnTop(info); |
robertphillips@google.com | 6c1e49a | 2013-11-10 15:08:45 +0000 | [diff] [blame] | 68 | SkMaskFilter* maskFilter = SkBlurMaskFilter::Create( |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 69 | kNormal_SkBlurStyle, |
robertphillips@google.com | 6c1e49a | 2013-11-10 15:08:45 +0000 | [diff] [blame] | 70 | SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf), |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 71 | SkBlurMaskFilter::kHighQuality_BlurFlag); |
| 72 | paint->setMaskFilter(maskFilter)->unref(); |
scroggo@google.com | 5fdef98 | 2013-11-05 16:28:38 +0000 | [diff] [blame] | 73 | SkColorFilter* colorFilter = SkColorFilter::CreateModeFilter(SK_ColorLTGRAY, |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 74 | SkXfermode::kSrcIn_Mode); |
| 75 | paint->setColorFilter(colorFilter)->unref(); |
scroggo@google.com | 5fdef98 | 2013-11-05 16:28:38 +0000 | [diff] [blame] | 76 | paint->setColor(SK_ColorGRAY); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 77 | } |
| 78 | { |
| 79 | SkLayerDrawLooper::LayerInfo info; |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 80 | looperBuilder.addLayerOnTop(info); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 81 | } |
| 82 | SkPaint paint; |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 83 | canvas->drawRect(fRRect.rect(), paint); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 84 | |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 85 | paint.setLooper(looperBuilder.detachLooper())->unref(); |
scroggo@google.com | 5fdef98 | 2013-11-05 16:28:38 +0000 | [diff] [blame] | 86 | paint.setColor(SK_ColorCYAN); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 87 | paint.setAntiAlias(true); |
| 88 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 89 | canvas->drawRRect(fRRect, paint); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | private: |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 93 | SkString fName; |
| 94 | SkRRect fRRect; |
| 95 | |
| 96 | typedef skiagm::GM INHERITED; |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 99 | // Simpler blurred RR test cases where all the radii are the same. |
| 100 | class SimpleBlurRoundRectGM : public skiagm::GM { |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 101 | public: |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 102 | SimpleBlurRoundRectGM() |
| 103 | : fName("simpleblurroundrect") { |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 107 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 108 | return kSkipTiled_Flag; |
| 109 | } |
| 110 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 111 | virtual SkString onShortName() SK_OVERRIDE { |
| 112 | return fName; |
| 113 | } |
| 114 | |
| 115 | virtual SkISize onISize() SK_OVERRIDE { |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 116 | return SkISize::Make(950, 950); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 120 | canvas->scale(1.5f, 1.5f); |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 121 | canvas->translate(50,50); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 122 | |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 123 | const float blurRadii[] = { 1,5,10,20 }; |
| 124 | const int cornerRadii[] = { 1,5,10,20 }; |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 125 | const SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); |
| 126 | for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) { |
| 127 | SkAutoCanvasRestore autoRestore(canvas, true); |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 128 | canvas->translate(0, (r.height() + SkIntToScalar(50)) * i); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 129 | for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) { |
robertphillips@google.com | 6c1e49a | 2013-11-10 15:08:45 +0000 | [diff] [blame] | 130 | SkMaskFilter* filter = SkBlurMaskFilter::Create( |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 131 | kNormal_SkBlurStyle, |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 132 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i])), |
| 133 | SkBlurMaskFilter::kHighQuality_BlurFlag); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 134 | SkPaint paint; |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 135 | paint.setColor(SK_ColorBLACK); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 136 | paint.setMaskFilter(filter)->unref(); |
| 137 | |
| 138 | SkRRect rrect; |
| 139 | rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]), SkIntToScalar(cornerRadii[j])); |
| 140 | canvas->drawRRect(rrect, paint); |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 141 | canvas->translate(r.width() + SkIntToScalar(50), 0); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 144 | } |
| 145 | private: |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 146 | const SkString fName; |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 147 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 148 | typedef skiagm::GM INHERITED; |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | // Create one with dimensions/rounded corners based on the skp |
epoger@google.com | 7e6e80a | 2013-11-05 22:04:53 +0000 | [diff] [blame] | 152 | // |
| 153 | // TODO(scroggo): Disabled in an attempt to rememdy |
| 154 | // https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs: |
| 155 | // ran wrong number of tests') |
| 156 | //DEF_GM(return new BlurRoundRectGM(600, 5514, 6);) |
| 157 | |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 158 | // Rounded rect with two opposite corners with large radii, the other two |
| 159 | // small. |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 160 | DEF_GM(return new BlurRoundRectGM(100, 100);) |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 161 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 162 | DEF_GM(return new SimpleBlurRoundRectGM();) |