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 "SkCanvas.h"
10#include "SkColorFilter.h"
11#include "SkColorPriv.h"
12#include "SkShader.h"
13
14#include "SkBlurImageFilter.h"
15#include "SkColorFilterImageFilter.h"
16#include "SkComposeImageFilter.h"
17#include "SkMergeImageFilter.h"
18#include "SkOffsetImageFilter.h"
19#include "SkTestImageFilters.h"
20
21#define FILTER_WIDTH    SkIntToScalar(150)
22#define FILTER_HEIGHT   SkIntToScalar(200)
23
24static SkImageFilter* make0() { return SkDownSampleImageFilter::Create(SK_Scalar1 / 5); }
25static SkImageFilter* make1() { return SkOffsetImageFilter::Create(SkIntToScalar(16), SkIntToScalar(16)); }
26static SkImageFilter* make2() {
27    SkColorFilter* cf = SkColorFilter::CreateModeFilter(SK_ColorBLUE,
28                                                        SkXfermode::kSrcIn_Mode);
29    SkAutoUnref aur(cf);
30    return SkColorFilterImageFilter::Create(cf);
31}
32static SkImageFilter* make3() {
33    return SkBlurImageFilter::Create(8, 0);
34}
35
36static SkImageFilter* make4() {
37    SkImageFilter* outer = SkOffsetImageFilter::Create(SkIntToScalar(16), SkIntToScalar(16));
38    SkImageFilter* inner = SkDownSampleImageFilter::Create(SK_Scalar1 / 5);
39    SkAutoUnref aur0(outer);
40    SkAutoUnref aur1(inner);
41    return SkComposeImageFilter::Create(outer, inner);
42}
43static SkImageFilter* make5() {
44    SkImageFilter* first = SkOffsetImageFilter::Create(SkIntToScalar(16), SkIntToScalar(16));
45    SkImageFilter* second = SkDownSampleImageFilter::Create(SK_Scalar1 / 5);
46    SkAutoUnref aur0(first);
47    SkAutoUnref aur1(second);
48    return SkMergeImageFilter::Create(first, second);
49}
50
51static SkImageFilter* make6() {
52    SkImageFilter* outer = SkOffsetImageFilter::Create(SkIntToScalar(16), SkIntToScalar(16));
53    SkImageFilter* inner = SkDownSampleImageFilter::Create(SK_Scalar1 / 5);
54    SkAutoUnref aur0(outer);
55    SkAutoUnref aur1(inner);
56    SkImageFilter* compose = SkComposeImageFilter::Create(outer, inner);
57    SkAutoUnref aur2(compose);
58
59    SkColorFilter* cf = SkColorFilter::CreateModeFilter(0x880000FF,
60                                                        SkXfermode::kSrcIn_Mode);
61    SkAutoUnref aur3(cf);
62    SkImageFilter* blue = SkColorFilterImageFilter::Create(cf);
63    SkAutoUnref aur4(blue);
64
65    return SkMergeImageFilter::Create(compose, blue);
66}
67
68static SkImageFilter* make7() {
69    SkImageFilter* outer = SkOffsetImageFilter::Create(SkIntToScalar(16), SkIntToScalar(16));
70    SkImageFilter* inner = make3();
71    SkAutoUnref aur0(outer);
72    SkAutoUnref aur1(inner);
73    SkImageFilter* compose = SkComposeImageFilter::Create(outer, inner);
74    SkAutoUnref aur2(compose);
75
76    SkColorFilter* cf = SkColorFilter::CreateModeFilter(0x880000FF,
77                                                        SkXfermode::kSrcIn_Mode);
78    SkAutoUnref aur3(cf);
79    SkImageFilter* blue = SkColorFilterImageFilter::Create(cf);
80    SkAutoUnref aur4(blue);
81
82    return SkMergeImageFilter::Create(compose, blue);
83}
84
85static void draw0(SkCanvas* canvas) {
86    SkPaint p;
87    p.setAntiAlias(true);
88    SkRect r = SkRect::MakeWH(FILTER_WIDTH, FILTER_HEIGHT);
89    r.inset(SK_Scalar1 * 12, SK_Scalar1 * 12);
90    p.setColor(SK_ColorRED);
91    canvas->drawOval(r, p);
92}
93
94class TestImageFiltersGM : public skiagm::GM {
95public:
96    TestImageFiltersGM () {}
97
98protected:
99
100    SkString onShortName() override {
101        return SkString("testimagefilters");
102    }
103
104    SkISize onISize() override { return SkISize::Make(700, 460); }
105
106    void onDraw(SkCanvas* canvas) override {
107//        this->drawSizeBounds(canvas, 0xFFCCCCCC);
108
109        static SkImageFilter* (*gFilterProc[])() = {
110            make0, make1, make2, make3, make4, make5, make6, make7
111        };
112
113        const SkRect bounds = SkRect::MakeWH(FILTER_WIDTH, FILTER_HEIGHT);
114
115        const SkScalar dx = bounds.width() * 8 / 7;
116        const SkScalar dy = bounds.height() * 8 / 7;
117
118        canvas->translate(SkIntToScalar(8), SkIntToScalar(8));
119
120        for (int i = 0; i < (int)SK_ARRAY_COUNT(gFilterProc); ++i) {
121            int ix = i % 4;
122            int iy = i / 4;
123
124            SkAutoCanvasRestore acr(canvas, true);
125            canvas->translate(ix * dx, iy * dy);
126
127            SkPaint p;
128            p.setStyle(SkPaint::kStroke_Style);
129            canvas->drawRect(bounds, p);
130
131            SkPaint paint;
132            paint.setImageFilter(gFilterProc[i]())->unref();
133            canvas->saveLayer(&bounds, &paint);
134            draw0(canvas);
135        }
136    }
137
138private:
139    typedef GM INHERITED;
140};
141
142//////////////////////////////////////////////////////////////////////////////
143
144DEF_GM( return new TestImageFiltersGM; )
145