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 | |
reed@google.com | 53007a2 | 2012-11-26 14:39:50 +0000 | [diff] [blame] | 62 | #include "SkGradientShader.h" |
| 63 | |
| 64 | typedef void (*PaintProc)(SkPaint*, SkScalar width); |
| 65 | |
| 66 | static void setgrad(SkPaint* paint, SkScalar width) { |
| 67 | SkPoint pts[] = { { 0, 0 }, { width, 0 } }; |
| 68 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN }; |
| 69 | SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
| 70 | SkShader::kClamp_TileMode); |
| 71 | paint->setShader(s)->unref(); |
| 72 | } |
| 73 | |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 74 | class BlurRectGM : public skiagm::GM { |
| 75 | SkAutoTUnref<SkMaskFilter> fMaskFilter; |
reed@google.com | 53007a2 | 2012-11-26 14:39:50 +0000 | [diff] [blame] | 76 | SkString fName; |
| 77 | PaintProc fPProc; |
| 78 | SkAlpha fAlpha; |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 79 | public: |
reed@google.com | 53007a2 | 2012-11-26 14:39:50 +0000 | [diff] [blame] | 80 | BlurRectGM(const char name[], PaintProc pproc, U8CPU alpha) : |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 81 | fMaskFilter(SkBlurMaskFilter::Create(STROKE_WIDTH/2, |
| 82 | SkBlurMaskFilter::kNormal_BlurStyle, |
| 83 | SkBlurMaskFilter::kHighQuality_BlurFlag)) |
reed@google.com | 53007a2 | 2012-11-26 14:39:50 +0000 | [diff] [blame] | 84 | , fName(name) |
| 85 | , fPProc(pproc) |
| 86 | , fAlpha(SkToU8(alpha)) |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 87 | {} |
| 88 | |
| 89 | protected: |
| 90 | virtual SkString onShortName() { |
reed@google.com | 53007a2 | 2012-11-26 14:39:50 +0000 | [diff] [blame] | 91 | return fName; |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | virtual SkISize onISize() { |
| 95 | return SkISize::Make(640, 480); |
| 96 | } |
| 97 | |
| 98 | virtual void onDraw(SkCanvas* canvas) { |
| 99 | canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2); |
| 100 | |
reed@google.com | 53007a2 | 2012-11-26 14:39:50 +0000 | [diff] [blame] | 101 | SkRect r = { 0, 0, 250, 120 }; |
skia.committer@gmail.com | 8ccf590 | 2012-11-27 02:01:19 +0000 | [diff] [blame^] | 102 | |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 103 | SkPaint paint; |
| 104 | paint.setMaskFilter(fMaskFilter); |
reed@google.com | 53007a2 | 2012-11-26 14:39:50 +0000 | [diff] [blame] | 105 | if (fPProc) { |
| 106 | fPProc(&paint, r.width()); |
| 107 | } |
| 108 | paint.setAlpha(fAlpha); |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 109 | |
| 110 | static const Proc procs[] = { |
reed@google.com | 808b70f | 2012-11-19 16:14:02 +0000 | [diff] [blame] | 111 | fill_rect, draw_donut, draw_donut_skewed |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 114 | this->drawProcs(canvas, r, paint, false, procs, SK_ARRAY_COUNT(procs)); |
| 115 | canvas->translate(r.width() * 4/3, 0); |
| 116 | this->drawProcs(canvas, r, paint, true, procs, SK_ARRAY_COUNT(procs)); |
| 117 | } |
| 118 | |
| 119 | virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; } |
| 120 | |
| 121 | private: |
| 122 | void drawProcs(SkCanvas* canvas, const SkRect& r, const SkPaint& paint, |
| 123 | bool doClip, const Proc procs[], size_t procsCount) { |
| 124 | SkAutoCanvasRestore acr(canvas, true); |
| 125 | for (size_t i = 0; i < procsCount; ++i) { |
| 126 | if (doClip) { |
| 127 | SkRect clipRect(r); |
| 128 | clipRect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2); |
| 129 | canvas->save(); |
| 130 | canvas->clipRect(r); |
| 131 | } |
| 132 | procs[i](canvas, r, paint); |
| 133 | if (doClip) { |
| 134 | canvas->restore(); |
| 135 | } |
| 136 | canvas->translate(0, r.height() * 4/3); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | typedef GM INHERITED; |
| 141 | }; |
| 142 | |
| 143 | ////////////////////////////////////////////////////////////////////////////// |
| 144 | |
reed@google.com | 53007a2 | 2012-11-26 14:39:50 +0000 | [diff] [blame] | 145 | DEF_GM(return new BlurRectGM("blurrect", NULL, 0xFF);) |
| 146 | DEF_GM(return new BlurRectGM("blurrect_grad_80", setgrad, 0x80);) |
reed@google.com | db87c96 | 2012-11-02 21:11:12 +0000 | [diff] [blame] | 147 | |