reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 "SkBlurMaskFilter.h" |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame^] | 10 | #include "SkBlurMask.h" |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| 12 | #include "SkPath.h" |
| 13 | |
| 14 | #define STROKE_WIDTH SkIntToScalar(10) |
| 15 | |
| 16 | typedef void (*Proc)(SkCanvas*, const SkRect&, const SkPaint&); |
| 17 | |
| 18 | static void fill_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 19 | canvas->drawRect(r, p); |
| 20 | } |
| 21 | |
| 22 | static void stroke_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 23 | SkPaint paint(p); |
| 24 | paint.setStyle(SkPaint::kStroke_Style); |
| 25 | paint.setStrokeWidth(STROKE_WIDTH); |
| 26 | canvas->drawRect(r, paint); |
| 27 | } |
| 28 | |
| 29 | static void draw_donut(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 30 | SkRect rect; |
| 31 | SkPath path; |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame] | 32 | |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 33 | rect = r; |
| 34 | rect.outset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
| 35 | path.addRect(rect); |
| 36 | rect = r; |
| 37 | rect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame] | 38 | |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 39 | path.addRect(rect); |
| 40 | path.setFillType(SkPath::kEvenOdd_FillType); |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame] | 41 | |
reed@google.com | 808b70f | 2012-11-19 16:14:02 +0000 | [diff] [blame] | 42 | canvas->drawPath(path, p); |
| 43 | } |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 44 | |
reed@google.com | 808b70f | 2012-11-19 16:14:02 +0000 | [diff] [blame] | 45 | static void draw_donut_skewed(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 46 | SkRect rect; |
| 47 | SkPath path; |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame] | 48 | |
reed@google.com | 808b70f | 2012-11-19 16:14:02 +0000 | [diff] [blame] | 49 | rect = r; |
| 50 | rect.outset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
| 51 | path.addRect(rect); |
| 52 | rect = r; |
| 53 | rect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame] | 54 | |
reed@google.com | 808b70f | 2012-11-19 16:14:02 +0000 | [diff] [blame] | 55 | rect.offset(7, -7); |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame] | 56 | |
reed@google.com | 808b70f | 2012-11-19 16:14:02 +0000 | [diff] [blame] | 57 | path.addRect(rect); |
| 58 | path.setFillType(SkPath::kEvenOdd_FillType); |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame] | 59 | |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 60 | canvas->drawPath(path, p); |
| 61 | } |
| 62 | |
reed@google.com | 53007a2 | 2012-11-26 14:39:50 +0000 | [diff] [blame] | 63 | #include "SkGradientShader.h" |
| 64 | |
| 65 | typedef void (*PaintProc)(SkPaint*, SkScalar width); |
| 66 | |
| 67 | static void setgrad(SkPaint* paint, SkScalar width) { |
| 68 | SkPoint pts[] = { { 0, 0 }, { width, 0 } }; |
| 69 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN }; |
| 70 | SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
| 71 | SkShader::kClamp_TileMode); |
| 72 | paint->setShader(s)->unref(); |
| 73 | } |
| 74 | |
reed@google.com | 57850b9 | 2012-12-17 21:20:53 +0000 | [diff] [blame] | 75 | static const char* gBlurStyle2Name[] = { |
| 76 | "normal", |
| 77 | "solid", |
| 78 | "outer", |
| 79 | "inner" |
| 80 | }; |
| 81 | |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 82 | class BlurRectGM : public skiagm::GM { |
| 83 | SkAutoTUnref<SkMaskFilter> fMaskFilter; |
reed@google.com | 53007a2 | 2012-11-26 14:39:50 +0000 | [diff] [blame] | 84 | SkString fName; |
| 85 | PaintProc fPProc; |
| 86 | SkAlpha fAlpha; |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 87 | public: |
reed@google.com | 57850b9 | 2012-12-17 21:20:53 +0000 | [diff] [blame] | 88 | BlurRectGM(const char name[], PaintProc pproc, U8CPU alpha, |
| 89 | SkBlurMaskFilter::BlurStyle bs) : |
| 90 | fMaskFilter(SkBlurMaskFilter::Create(STROKE_WIDTH/2, bs, |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 91 | SkBlurMaskFilter::kHighQuality_BlurFlag)) |
reed@google.com | 53007a2 | 2012-11-26 14:39:50 +0000 | [diff] [blame] | 92 | , fName(name) |
| 93 | , fPProc(pproc) |
| 94 | , fAlpha(SkToU8(alpha)) |
reed@google.com | 57850b9 | 2012-12-17 21:20:53 +0000 | [diff] [blame] | 95 | { |
| 96 | fName.appendf("_%s", gBlurStyle2Name[bs]); |
| 97 | } |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 98 | |
| 99 | protected: |
| 100 | virtual SkString onShortName() { |
reed@google.com | 53007a2 | 2012-11-26 14:39:50 +0000 | [diff] [blame] | 101 | return fName; |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | virtual SkISize onISize() { |
| 105 | return SkISize::Make(640, 480); |
| 106 | } |
| 107 | |
| 108 | virtual void onDraw(SkCanvas* canvas) { |
| 109 | canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2); |
| 110 | |
reed@google.com | 53007a2 | 2012-11-26 14:39:50 +0000 | [diff] [blame] | 111 | SkRect r = { 0, 0, 250, 120 }; |
skia.committer@gmail.com | 8ccf590 | 2012-11-27 02:01:19 +0000 | [diff] [blame] | 112 | |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 113 | SkPaint paint; |
| 114 | paint.setMaskFilter(fMaskFilter); |
reed@google.com | 53007a2 | 2012-11-26 14:39:50 +0000 | [diff] [blame] | 115 | if (fPProc) { |
| 116 | fPProc(&paint, r.width()); |
| 117 | } |
| 118 | paint.setAlpha(fAlpha); |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 119 | |
| 120 | static const Proc procs[] = { |
reed@google.com | 808b70f | 2012-11-19 16:14:02 +0000 | [diff] [blame] | 121 | fill_rect, draw_donut, draw_donut_skewed |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 124 | this->drawProcs(canvas, r, paint, false, procs, SK_ARRAY_COUNT(procs)); |
| 125 | canvas->translate(r.width() * 4/3, 0); |
| 126 | this->drawProcs(canvas, r, paint, true, procs, SK_ARRAY_COUNT(procs)); |
| 127 | } |
| 128 | |
| 129 | virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; } |
| 130 | |
| 131 | private: |
| 132 | void drawProcs(SkCanvas* canvas, const SkRect& r, const SkPaint& paint, |
| 133 | bool doClip, const Proc procs[], size_t procsCount) { |
| 134 | SkAutoCanvasRestore acr(canvas, true); |
| 135 | for (size_t i = 0; i < procsCount; ++i) { |
| 136 | if (doClip) { |
| 137 | SkRect clipRect(r); |
| 138 | clipRect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
| 139 | canvas->save(); |
| 140 | canvas->clipRect(r); |
| 141 | } |
| 142 | procs[i](canvas, r, paint); |
| 143 | if (doClip) { |
| 144 | canvas->restore(); |
| 145 | } |
| 146 | canvas->translate(0, r.height() * 4/3); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | typedef GM INHERITED; |
| 151 | }; |
| 152 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame^] | 153 | class BlurRectCompareGM : public skiagm::GM { |
| 154 | SkString fName; |
| 155 | unsigned int fRectWidth, fRectHeight; |
| 156 | float fRadius; |
| 157 | public: |
| 158 | BlurRectCompareGM(const char name[], unsigned int rectWidth, unsigned int rectHeight, float radius) : |
| 159 | fName(name) |
| 160 | , fRectWidth( rectWidth ) |
| 161 | , fRectHeight( rectHeight ) |
| 162 | , fRadius( radius ) |
| 163 | {} |
| 164 | |
| 165 | int width() const { return fRectWidth; } |
| 166 | int height() const { return fRectHeight; } |
| 167 | int radius() const { return fRadius; } |
| 168 | |
| 169 | protected: |
| 170 | virtual SkString onShortName() { |
| 171 | return fName; |
| 172 | } |
| 173 | |
| 174 | virtual SkISize onISize() { |
| 175 | return SkISize::Make(640, 480); |
| 176 | } |
| 177 | |
| 178 | virtual void makeMask( SkMask *m, SkRect r ) = 0; |
| 179 | |
| 180 | virtual void onDraw(SkCanvas* canvas) { |
| 181 | SkRect r; |
| 182 | r.setWH( fRectWidth, fRectHeight ); |
| 183 | |
| 184 | SkMask mask; |
| 185 | |
| 186 | makeMask( &mask, r ); |
| 187 | |
| 188 | SkBitmap bm; |
| 189 | bm.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(), mask.fBounds.height()); |
| 190 | bm.setPixels(mask.fImage); |
| 191 | canvas->drawBitmap(bm, 50, 50, NULL); |
| 192 | } |
| 193 | |
| 194 | virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; } |
| 195 | |
| 196 | private: |
| 197 | typedef GM INHERITED; |
| 198 | }; |
| 199 | |
| 200 | class BlurRectFastGM: public BlurRectCompareGM { |
| 201 | public: |
| 202 | BlurRectFastGM(const char name[], unsigned int rect_width, unsigned int rect_height, float blur_radius) : |
| 203 | BlurRectCompareGM( name, rect_width, rect_height, blur_radius ) {} |
| 204 | protected: |
| 205 | virtual void makeMask( SkMask *m, SkRect r ) { |
| 206 | SkBlurMask::BlurRect( m, r, radius(), SkBlurMask::kNormal_Style, SkBlurMask::kHigh_Quality ); |
| 207 | } |
| 208 | }; |
| 209 | |
| 210 | class BlurRectSlowGM: public BlurRectCompareGM { |
| 211 | public: |
| 212 | BlurRectSlowGM(const char name[], unsigned int rect_width, unsigned int rect_height, float blur_radius) : |
| 213 | BlurRectCompareGM( name, rect_width, rect_height, blur_radius ) {} |
| 214 | protected: |
| 215 | virtual void makeMask( SkMask *m, SkRect r ) { |
| 216 | SkMask src; |
| 217 | src.fFormat = SkMask::kA8_Format; |
| 218 | src.fRowBytes = r.width(); |
| 219 | src.fBounds = SkIRect::MakeWH(r.width(), r.height()); |
| 220 | src.fImage = SkMask::AllocImage( src.computeTotalImageSize() ); |
| 221 | |
| 222 | memset( src.fImage, 0xff, src.computeTotalImageSize() ); |
| 223 | |
| 224 | SkBlurMask::BlurSeparable( m, src, radius()/2, SkBlurMask::kNormal_Style, SkBlurMask::kHigh_Quality ); |
| 225 | } |
| 226 | }; |
| 227 | |
| 228 | |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 229 | ////////////////////////////////////////////////////////////////////////////// |
| 230 | |
reed@google.com | 57850b9 | 2012-12-17 21:20:53 +0000 | [diff] [blame] | 231 | DEF_GM(return new BlurRectGM("blurrect", NULL, 0xFF, SkBlurMaskFilter::kNormal_BlurStyle);) |
| 232 | DEF_GM(return new BlurRectGM("blurrect", NULL, 0xFF, SkBlurMaskFilter::kSolid_BlurStyle);) |
| 233 | DEF_GM(return new BlurRectGM("blurrect", NULL, 0xFF, SkBlurMaskFilter::kOuter_BlurStyle);) |
| 234 | DEF_GM(return new BlurRectGM("blurrect", NULL, 0xFF, SkBlurMaskFilter::kInner_BlurStyle);) |
| 235 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame^] | 236 | DEF_GM(return new BlurRectFastGM("blurrect_fast_100_100_10", 100, 100, 10);) |
| 237 | DEF_GM(return new BlurRectFastGM("blurrect_fast_100_100_2", 100, 100, 2);) |
| 238 | DEF_GM(return new BlurRectFastGM("blurrect_fast_10_10_100", 10, 10, 100);) |
| 239 | DEF_GM(return new BlurRectFastGM("blurrect_fast_10_100_10", 10, 100, 10);) |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 240 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame^] | 241 | DEF_GM(return new BlurRectSlowGM("blurrect_slow_100_100_10", 100, 100, 10);) |
| 242 | DEF_GM(return new BlurRectSlowGM("blurrect_slow_100_100_2", 100, 100, 2);) |
| 243 | DEF_GM(return new BlurRectSlowGM("blurrect_slow_10_10_100", 10, 10, 100);) |
| 244 | DEF_GM(return new BlurRectSlowGM("blurrect_slow_10_100_10", 10, 100, 10);) |