drawlooper.cpp revision d6176b0dcacb124539e0cfd051e6d93a9782f020
1/*
2 * Copyright 2011 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 "SkCanvas.h"
10#include "SkGraphics.h"
11#include "SkRandom.h"
12#include "SkLayerDrawLooper.h"
13#include "SkBlurMaskFilter.h"
14
15#define WIDTH   200
16#define HEIGHT  200
17
18class DrawLooperGM : public skiagm::GM {
19public:
20    DrawLooperGM() : fLooper(NULL) {
21        this->setBGColor(0xFFDDDDDD);
22    }
23
24    virtual ~DrawLooperGM() {
25        SkSafeUnref(fLooper);
26    }
27
28protected:
29    virtual SkISize onISize() {
30        return SkISize::Make(520, 160);
31    }
32
33    virtual SkString onShortName() SK_OVERRIDE {
34        return SkString("drawlooper");
35    }
36
37    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
38        this->init();
39
40        SkPaint  paint;
41        paint.setTextSize(SkIntToScalar(72));
42        paint.setLooper(fLooper);
43
44        canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
45                           SkIntToScalar(30), paint);
46
47        canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50),
48                               SkIntToScalar(200), SkIntToScalar(100), paint);
49
50        canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100),
51                         paint);
52    }
53
54private:
55    SkLayerDrawLooper*   fLooper;
56
57    void init() {
58        if (fLooper) return;
59
60        static const struct {
61            SkColor         fColor;
62            SkPaint::Style  fStyle;
63            SkScalar        fWidth;
64            SkScalar        fOffset;
65            int             fBlur;
66        } gParams[] = {
67            { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 },
68            { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 },
69            { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 },
70            { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), 3 }
71        };
72
73        fLooper = new SkLayerDrawLooper;
74
75        SkLayerDrawLooper::LayerInfo info;
76        info.fFlagsMask = SkPaint::kAntiAlias_Flag;
77        info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit;
78        info.fColorMode = SkXfermode::kSrc_Mode;
79
80        for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
81            info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
82            SkPaint* paint = fLooper->addLayer(info);
83            paint->setAntiAlias(true);
84            paint->setColor(gParams[i].fColor);
85            paint->setStyle(gParams[i].fStyle);
86            paint->setStrokeWidth(gParams[i].fWidth);
87            if (gParams[i].fBlur > 0) {
88                SkMaskFilter* mf = SkBlurMaskFilter::Create(SkIntToScalar(gParams[i].fBlur),
89                                                            SkBlurMaskFilter::kNormal_BlurStyle);
90                paint->setMaskFilter(mf)->unref();
91            }
92        }
93    }
94
95    typedef GM INHERITED;
96};
97
98//////////////////////////////////////////////////////////////////////////////
99
100static skiagm::GM* MyFactory(void*) { return new DrawLooperGM; }
101static skiagm::GMRegistry reg(MyFactory);
102
103