blob: d6c3e7ba7f61834d82ea9758dbbcbb7bd18a3f48 [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;
31
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
43class BlurRectGM : public skiagm::GM {
44 SkAutoTUnref<SkMaskFilter> fMaskFilter;
45public:
46 BlurRectGM() :
47 fMaskFilter(SkBlurMaskFilter::Create(STROKE_WIDTH/2,
48 SkBlurMaskFilter::kNormal_BlurStyle,
49 SkBlurMaskFilter::kHighQuality_BlurFlag))
50 {}
51
52protected:
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
80private:
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
104DEF_GM(return new BlurRectGM;)
105