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