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 | 1aa90cf | 2012-11-06 13:18:25 +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); |
| 37 | path.addRect(rect); |
| 38 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 39 | |
| 40 | canvas->drawPath(path, p); |
| 41 | } |
| 42 | |
| 43 | class BlurRectGM : public skiagm::GM { |
| 44 | SkAutoTUnref<SkMaskFilter> fMaskFilter; |
| 45 | public: |
| 46 | BlurRectGM() : |
| 47 | fMaskFilter(SkBlurMaskFilter::Create(STROKE_WIDTH/2, |
| 48 | SkBlurMaskFilter::kNormal_BlurStyle, |
| 49 | SkBlurMaskFilter::kHighQuality_BlurFlag)) |
| 50 | {} |
| 51 | |
| 52 | protected: |
| 53 | virtual SkString onShortName() { |
| 54 | return SkString("blurrect"); |
| 55 | } |
| 56 | |
| 57 | virtual SkISize onISize() { |
| 58 | return SkISize::Make(640, 480); |
| 59 | } |
| 60 | |
| 61 | virtual void onDraw(SkCanvas* canvas) { |
| 62 | canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2); |
| 63 | |
| 64 | SkPaint paint; |
| 65 | paint.setMaskFilter(fMaskFilter); |
| 66 | |
| 67 | static const Proc procs[] = { |
| 68 | fill_rect, stroke_rect, draw_donut |
| 69 | }; |
| 70 | |
| 71 | SkRect r = { 0, 0, 250, 120 }; |
| 72 | |
| 73 | this->drawProcs(canvas, r, paint, false, procs, SK_ARRAY_COUNT(procs)); |
| 74 | canvas->translate(r.width() * 4/3, 0); |
| 75 | this->drawProcs(canvas, r, paint, true, procs, SK_ARRAY_COUNT(procs)); |
| 76 | } |
| 77 | |
| 78 | virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; } |
| 79 | |
| 80 | private: |
| 81 | void drawProcs(SkCanvas* canvas, const SkRect& r, const SkPaint& paint, |
| 82 | bool doClip, const Proc procs[], size_t procsCount) { |
| 83 | SkAutoCanvasRestore acr(canvas, true); |
| 84 | for (size_t i = 0; i < procsCount; ++i) { |
| 85 | if (doClip) { |
| 86 | SkRect clipRect(r); |
| 87 | clipRect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
| 88 | canvas->save(); |
| 89 | canvas->clipRect(r); |
| 90 | } |
| 91 | procs[i](canvas, r, paint); |
| 92 | if (doClip) { |
| 93 | canvas->restore(); |
| 94 | } |
| 95 | canvas->translate(0, r.height() * 4/3); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | typedef GM INHERITED; |
| 100 | }; |
| 101 | |
| 102 | ////////////////////////////////////////////////////////////////////////////// |
| 103 | |
| 104 | DEF_GM(return new BlurRectGM;) |
| 105 | |