drawlooper.cpp revision b7061176c7f414616fe2e79e832b3e0abe326af6
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.setTextSize(SkIntToScalar(72));
43        paint.setLooper(fLooper);
44
45        canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
46                           SkIntToScalar(30), paint);
47
48        canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50),
49                               SkIntToScalar(200), SkIntToScalar(100), paint);
50
51        canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100),
52                         paint);
53    }
54
55private:
56    SkLayerDrawLooper*   fLooper;
57
58    void init() {
59        if (fLooper) return;
60
61        static const struct {
62            SkColor         fColor;
63            SkPaint::Style  fStyle;
64            SkScalar        fWidth;
65            SkScalar        fOffset;
66            SkScalar        fBlur;
67        } gParams[] = {
68            { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 },
69            { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 },
70            { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 },
71            { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), SkIntToScalar(3) }
72        };
73
74        fLooper = new SkLayerDrawLooper;
75
76        SkLayerDrawLooper::LayerInfo info;
77        info.fFlagsMask = SkPaint::kAntiAlias_Flag;
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->setAntiAlias(true);
85            paint->setColor(gParams[i].fColor);
86            paint->setStyle(gParams[i].fStyle);
87            paint->setStrokeWidth(gParams[i].fWidth);
88            if (gParams[i].fBlur > 0) {
89                SkMaskFilter* mf = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
90                                         SkBlurMask::ConvertRadiusToSigma(gParams[i].fBlur));
91                paint->setMaskFilter(mf)->unref();
92            }
93        }
94    }
95
96    typedef GM INHERITED;
97};
98
99//////////////////////////////////////////////////////////////////////////////
100
101static skiagm::GM* MyFactory(void*) { return new DrawLooperGM; }
102static skiagm::GMRegistry reg(MyFactory);
103