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