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        sk_tool_utils::set_portable_typeface(&paint);
44        paint.setTextSize(SkIntToScalar(72));
45        paint.setLooper(fLooper);
46
47        canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50),
48                           SkIntToScalar(30), paint);
49
50        canvas->drawRectCoords(SkIntToScalar(150), SkIntToScalar(50),
51                               SkIntToScalar(200), SkIntToScalar(100), paint);
52
53        canvas->drawText("Looper", 6, SkIntToScalar(230), SkIntToScalar(100),
54                         paint);
55    }
56
57private:
58    SkLayerDrawLooper*   fLooper;
59
60    void init() {
61        if (fLooper) return;
62
63        static const struct {
64            SkColor         fColor;
65            SkPaint::Style  fStyle;
66            SkScalar        fWidth;
67            SkScalar        fOffset;
68            SkScalar        fBlur;
69        } gParams[] = {
70            { SK_ColorWHITE, SkPaint::kStroke_Style, SkIntToScalar(1)*3/4, 0, 0 },
71            { SK_ColorRED, SkPaint::kStroke_Style, SkIntToScalar(4), 0, 0 },
72            { SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0 },
73            { 0x88000000, SkPaint::kFill_Style, 0, SkIntToScalar(10), SkIntToScalar(3) }
74        };
75
76        SkLayerDrawLooper::Builder looperBuilder;
77
78        SkLayerDrawLooper::LayerInfo info;
79        info.fPaintBits = SkLayerDrawLooper::kStyle_Bit | SkLayerDrawLooper::kMaskFilter_Bit;
80        info.fColorMode = SkXfermode::kSrc_Mode;
81
82        for (size_t i = 0; i < SK_ARRAY_COUNT(gParams); i++) {
83            info.fOffset.set(gParams[i].fOffset, gParams[i].fOffset);
84            SkPaint* paint = looperBuilder.addLayer(info);
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(kNormal_SkBlurStyle,
90                                         SkBlurMask::ConvertRadiusToSigma(gParams[i].fBlur));
91                paint->setMaskFilter(mf)->unref();
92            }
93        }
94        fLooper = looperBuilder.detachLooper();
95    }
96
97    typedef GM INHERITED;
98};
99
100//////////////////////////////////////////////////////////////////////////////
101
102static skiagm::GM* MyFactory(void*) { return new DrawLooperGM; }
103static skiagm::GMRegistry reg(MyFactory);
104