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" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkPath.h" |
| 12 | |
| 13 | #define STROKE_WIDTH SkIntToScalar(10) |
| 14 | |
| 15 | typedef void (*Proc)(SkCanvas*, const SkRect&, const SkPaint&); |
| 16 | |
| 17 | static void fill_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 18 | canvas->drawRect(r, p); |
| 19 | } |
| 20 | |
| 21 | static void stroke_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 22 | SkPaint paint(p); |
| 23 | paint.setStyle(SkPaint::kStroke_Style); |
| 24 | paint.setStrokeWidth(STROKE_WIDTH); |
| 25 | canvas->drawRect(r, paint); |
| 26 | } |
| 27 | |
| 28 | static void draw_donut(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 29 | SkRect rect; |
| 30 | SkPath path; |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame^] | 31 | |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 32 | rect = r; |
| 33 | rect.outset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
| 34 | path.addRect(rect); |
| 35 | rect = r; |
| 36 | rect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame^] | 37 | |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 38 | path.addRect(rect); |
| 39 | path.setFillType(SkPath::kEvenOdd_FillType); |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame^] | 40 | |
reed@google.com | 808b70f | 2012-11-19 16:14:02 +0000 | [diff] [blame] | 41 | canvas->drawPath(path, p); |
| 42 | } |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 43 | |
reed@google.com | 808b70f | 2012-11-19 16:14:02 +0000 | [diff] [blame] | 44 | static void draw_donut_skewed(SkCanvas* canvas, const SkRect& r, const SkPaint& p) { |
| 45 | SkRect rect; |
| 46 | SkPath path; |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame^] | 47 | |
reed@google.com | 808b70f | 2012-11-19 16:14:02 +0000 | [diff] [blame] | 48 | rect = r; |
| 49 | rect.outset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
| 50 | path.addRect(rect); |
| 51 | rect = r; |
| 52 | rect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame^] | 53 | |
reed@google.com | 808b70f | 2012-11-19 16:14:02 +0000 | [diff] [blame] | 54 | rect.offset(7, -7); |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame^] | 55 | |
reed@google.com | 808b70f | 2012-11-19 16:14:02 +0000 | [diff] [blame] | 56 | path.addRect(rect); |
| 57 | path.setFillType(SkPath::kEvenOdd_FillType); |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame^] | 58 | |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 59 | canvas->drawPath(path, p); |
| 60 | } |
| 61 | |
| 62 | class BlurRectGM : public skiagm::GM { |
| 63 | SkAutoTUnref<SkMaskFilter> fMaskFilter; |
| 64 | public: |
| 65 | BlurRectGM() : |
| 66 | fMaskFilter(SkBlurMaskFilter::Create(STROKE_WIDTH/2, |
| 67 | SkBlurMaskFilter::kNormal_BlurStyle, |
| 68 | SkBlurMaskFilter::kHighQuality_BlurFlag)) |
| 69 | {} |
| 70 | |
| 71 | protected: |
| 72 | virtual SkString onShortName() { |
| 73 | return SkString("blurrect"); |
| 74 | } |
| 75 | |
| 76 | virtual SkISize onISize() { |
| 77 | return SkISize::Make(640, 480); |
| 78 | } |
| 79 | |
| 80 | virtual void onDraw(SkCanvas* canvas) { |
| 81 | canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2); |
| 82 | |
| 83 | SkPaint paint; |
| 84 | paint.setMaskFilter(fMaskFilter); |
| 85 | |
| 86 | static const Proc procs[] = { |
reed@google.com | 808b70f | 2012-11-19 16:14:02 +0000 | [diff] [blame] | 87 | fill_rect, draw_donut, draw_donut_skewed |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | SkRect r = { 0, 0, 250, 120 }; |
| 91 | |
| 92 | this->drawProcs(canvas, r, paint, false, procs, SK_ARRAY_COUNT(procs)); |
| 93 | canvas->translate(r.width() * 4/3, 0); |
| 94 | this->drawProcs(canvas, r, paint, true, procs, SK_ARRAY_COUNT(procs)); |
| 95 | } |
| 96 | |
| 97 | virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; } |
| 98 | |
| 99 | private: |
| 100 | void drawProcs(SkCanvas* canvas, const SkRect& r, const SkPaint& paint, |
| 101 | bool doClip, const Proc procs[], size_t procsCount) { |
| 102 | SkAutoCanvasRestore acr(canvas, true); |
| 103 | for (size_t i = 0; i < procsCount; ++i) { |
| 104 | if (doClip) { |
| 105 | SkRect clipRect(r); |
| 106 | clipRect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
| 107 | canvas->save(); |
| 108 | canvas->clipRect(r); |
| 109 | } |
| 110 | procs[i](canvas, r, paint); |
| 111 | if (doClip) { |
| 112 | canvas->restore(); |
| 113 | } |
| 114 | canvas->translate(0, r.height() * 4/3); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | typedef GM INHERITED; |
| 119 | }; |
| 120 | |
| 121 | ////////////////////////////////////////////////////////////////////////////// |
| 122 | |
| 123 | DEF_GM(return new BlurRectGM;) |
| 124 | |