1#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "SkGraphics.h"
5#include "SkRandom.h"
6#include "SkLayerDrawLooper.h"
7#include "SkBlurMaskFilter.h"
8
9#define WIDTH   200
10#define HEIGHT  200
11
12class LooperView : public SampleView {
13public:
14
15    SkLayerDrawLooper*   fLooper;
16
17	LooperView() {
18        static const struct {
19            SkColor         fColor;
20            SkPaint::Style  fStyle;
21            SkScalar        fWidth;
22            SkScalar        fOffset;
23            int             fBlur;
24        } gParams[] = {
25            { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 },
26            { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 },
27            { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 },
28            { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), 3 }
29        };
30
31        fLooper = new SkLayerDrawLooper;
32
33        SkLayerDrawLooper::LayerInfo info;
34        info.fFlagsMask = SkPaint::kAntiAlias_Flag;
35        info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit;
36        info.fColorMode = SkXfermode::kSrc_Mode;
37
38        for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
39            info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
40            SkPaint* paint = fLooper->addLayer(info);
41            paint->setAntiAlias(true);
42            paint->setColor(gParams[i].fColor);
43            paint->setStyle(gParams[i].fStyle);
44            paint->setStrokeWidth(gParams[i].fWidth);
45            if (gParams[i].fBlur > 0) {
46                SkMaskFilter* mf = SkBlurMaskFilter::Create(SkIntToScalar(gParams[i].fBlur),
47                                                            SkBlurMaskFilter::kNormal_BlurStyle);
48                paint->setMaskFilter(mf)->unref();
49            }
50        }
51
52        this->setBGColor(0xFFDDDDDD);
53    }
54
55    virtual ~LooperView() {
56        SkSafeUnref(fLooper);
57    }
58
59protected:
60    // overrides from SkEventSink
61    virtual bool onQuery(SkEvent* evt) {
62        if (SampleCode::TitleQ(*evt)) {
63            SampleCode::TitleR(evt, "DrawLooper");
64            return true;
65        }
66        return this->INHERITED::onQuery(evt);
67    }
68
69    virtual void onDrawContent(SkCanvas* canvas) {
70        SkPaint  paint;
71        paint.setTextSize(SkIntToScalar(72));
72        paint.setLooper(fLooper);
73
74        canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
75                           SkIntToScalar(30), paint);
76
77        canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50),
78                               SkIntToScalar(200), SkIntToScalar(100), paint);
79
80        canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100),
81                         paint);
82    }
83
84private:
85    typedef SampleView INHERITED;
86};
87
88//////////////////////////////////////////////////////////////////////////////
89
90static SkView* MyFactory() { return new LooperView; }
91static SkViewRegister reg(MyFactory);
92
93