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 "SkBlurImageFilter.h"
10#include "SkRandom.h"
11
12#define WIDTH 500
13#define HEIGHT 500
14
15namespace skiagm {
16
17class ImageBlurGM : public GM {
18public:
19    ImageBlurGM(SkScalar sigmaX, SkScalar sigmaY, const char* suffix)
20        : fSigmaX(sigmaX), fSigmaY(sigmaY) {
21        this->setBGColor(0xFF000000);
22        fName.printf("imageblur%s", suffix);
23    }
24
25protected:
26    virtual uint32_t onGetFlags() const SK_OVERRIDE {
27        return kSkipTiled_Flag;
28    }
29
30    virtual SkString onShortName() {
31        return fName;
32    }
33
34    virtual SkISize onISize() {
35        return SkISize::Make(WIDTH, HEIGHT);
36    }
37
38    virtual void onDraw(SkCanvas* canvas) {
39        SkPaint paint;
40        paint.setImageFilter(SkBlurImageFilter::Create(fSigmaX, fSigmaY))->unref();
41        canvas->saveLayer(NULL, &paint);
42        const char* str = "The quick brown fox jumped over the lazy dog.";
43
44        SkRandom rand;
45        SkPaint textPaint;
46        textPaint.setAntiAlias(true);
47        sk_tool_utils::set_portable_typeface(&textPaint);
48        for (int i = 0; i < 25; ++i) {
49            int x = rand.nextULessThan(WIDTH);
50            int y = rand.nextULessThan(HEIGHT);
51            textPaint.setColor(rand.nextBits(24) | 0xFF000000);
52            textPaint.setTextSize(rand.nextRangeScalar(0, 300));
53            canvas->drawText(str, strlen(str), SkIntToScalar(x),
54                             SkIntToScalar(y), textPaint);
55        }
56        canvas->restore();
57    }
58
59private:
60    SkScalar fSigmaX;
61    SkScalar fSigmaY;
62    SkString fName;
63
64    typedef GM INHERITED;
65};
66
67//////////////////////////////////////////////////////////////////////////////
68
69static GM* MyFactory1(void*) { return new ImageBlurGM(24.0f, 0.0f, ""); }
70static GMRegistry reg1(MyFactory1);
71
72static GM* MyFactory2(void*) { return new ImageBlurGM(80.0f, 80.0f, "_large"); }
73static GMRegistry reg2(MyFactory2);
74
75}
76