blob: 36e59ead88142540d3dda7943946eb72bbbfd4e6 [file] [log] [blame]
reed@google.comdb87c962012-11-02 21:11:12 +00001/*
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
15typedef void (*Proc)(SkCanvas*, const SkRect&, const SkPaint&);
16
17static void fill_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
18 canvas->drawRect(r, p);
19}
20
21static 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
28static void draw_donut(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
29 SkRect rect;
30 SkPath path;
skia.committer@gmail.com34587162012-11-20 02:01:23 +000031
reed@google.comdb87c962012-11-02 21:11:12 +000032 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.com34587162012-11-20 02:01:23 +000037
reed@google.comdb87c962012-11-02 21:11:12 +000038 path.addRect(rect);
39 path.setFillType(SkPath::kEvenOdd_FillType);
skia.committer@gmail.com34587162012-11-20 02:01:23 +000040
reed@google.com808b70f2012-11-19 16:14:02 +000041 canvas->drawPath(path, p);
42}
reed@google.comdb87c962012-11-02 21:11:12 +000043
reed@google.com808b70f2012-11-19 16:14:02 +000044static void draw_donut_skewed(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
45 SkRect rect;
46 SkPath path;
skia.committer@gmail.com34587162012-11-20 02:01:23 +000047
reed@google.com808b70f2012-11-19 16:14:02 +000048 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.com34587162012-11-20 02:01:23 +000053
reed@google.com808b70f2012-11-19 16:14:02 +000054 rect.offset(7, -7);
skia.committer@gmail.com34587162012-11-20 02:01:23 +000055
reed@google.com808b70f2012-11-19 16:14:02 +000056 path.addRect(rect);
57 path.setFillType(SkPath::kEvenOdd_FillType);
skia.committer@gmail.com34587162012-11-20 02:01:23 +000058
reed@google.comdb87c962012-11-02 21:11:12 +000059 canvas->drawPath(path, p);
60}
61
62class BlurRectGM : public skiagm::GM {
63 SkAutoTUnref<SkMaskFilter> fMaskFilter;
64public:
65 BlurRectGM() :
66 fMaskFilter(SkBlurMaskFilter::Create(STROKE_WIDTH/2,
67 SkBlurMaskFilter::kNormal_BlurStyle,
68 SkBlurMaskFilter::kHighQuality_BlurFlag))
69 {}
70
71protected:
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.com808b70f2012-11-19 16:14:02 +000087 fill_rect, draw_donut, draw_donut_skewed
reed@google.comdb87c962012-11-02 21:11:12 +000088 };
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
99private:
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
123DEF_GM(return new BlurRectGM;)
124