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 "SkGradientShader.h"
10#include "SkPath.h"
11
12namespace skiagm {
13
14class FillTypePerspGM : public GM {
15    SkPath fPath;
16public:
17    FillTypePerspGM() {}
18
19    void makePath() {
20        if (fPath.isEmpty()) {
21            const SkScalar radius = SkIntToScalar(45);
22            fPath.addCircle(SkIntToScalar(50), SkIntToScalar(50), radius);
23            fPath.addCircle(SkIntToScalar(100), SkIntToScalar(100), radius);
24        }
25    }
26
27protected:
28
29    SkString onShortName() override {
30        return SkString("filltypespersp");
31    }
32
33    SkISize onISize() override {
34        return SkISize::Make(835, 840);
35    }
36
37    void showPath(SkCanvas* canvas, int x, int y, SkPath::FillType ft,
38                  SkScalar scale, const SkPaint& paint) {
39        const SkRect r = { 0, 0, SkIntToScalar(150), SkIntToScalar(150) };
40
41        canvas->save();
42        canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
43        canvas->clipRect(r);
44        canvas->drawColor(SK_ColorWHITE);
45        fPath.setFillType(ft);
46        canvas->translate(r.centerX(), r.centerY());
47        canvas->scale(scale, scale);
48        canvas->translate(-r.centerX(), -r.centerY());
49        canvas->drawPath(fPath, paint);
50        canvas->restore();
51    }
52
53    void showFour(SkCanvas* canvas, SkScalar scale, bool aa) {
54        SkPaint paint;
55        SkPoint center = SkPoint::Make(SkIntToScalar(100), SkIntToScalar(100));
56        SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN};
57        SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1};
58        SkShader* s = SkGradientShader::CreateRadial(center,
59                                                     SkIntToScalar(100),
60                                                     colors,
61                                                     pos,
62                                                     SK_ARRAY_COUNT(colors),
63                                                     SkShader::kClamp_TileMode);
64        paint.setShader(s)->unref();
65        paint.setAntiAlias(aa);
66
67        showPath(canvas,   0,   0, SkPath::kWinding_FillType,
68                 scale, paint);
69        showPath(canvas, 200,   0, SkPath::kEvenOdd_FillType,
70                 scale, paint);
71        showPath(canvas,  00, 200, SkPath::kInverseWinding_FillType,
72                 scale, paint);
73        showPath(canvas, 200, 200, SkPath::kInverseEvenOdd_FillType,
74                 scale, paint);
75    }
76
77    void onDraw(SkCanvas* canvas) override {
78        this->makePath();
79
80        // do perspective drawPaint as the background;
81        SkPaint bkgnrd;
82        SkPoint center = SkPoint::Make(SkIntToScalar(100),
83                                       SkIntToScalar(100));
84        SkColor colors[] = {SK_ColorBLACK, SK_ColorCYAN,
85                            SK_ColorYELLOW, SK_ColorWHITE};
86        SkScalar pos[] = {0, SK_ScalarHalf / 2,
87                          3 * SK_ScalarHalf / 2, SK_Scalar1};
88        SkShader* s = SkGradientShader::CreateRadial(center,
89                                                     SkIntToScalar(1000),
90                                                     colors,
91                                                     pos,
92                                                     SK_ARRAY_COUNT(colors),
93                                                     SkShader::kClamp_TileMode);
94        bkgnrd.setShader(s)->unref();
95        canvas->save();
96            canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
97            SkMatrix mat;
98            mat.reset();
99            mat.setPerspY(SK_Scalar1 / 1000);
100            canvas->concat(mat);
101            canvas->drawPaint(bkgnrd);
102        canvas->restore();
103
104        // draw the paths in perspective
105        SkMatrix persp;
106        persp.reset();
107        persp.setPerspX(-SK_Scalar1 / 1800);
108        persp.setPerspY(SK_Scalar1 / 500);
109        canvas->concat(persp);
110
111        canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
112        const SkScalar scale = SkIntToScalar(5)/4;
113
114        showFour(canvas, SK_Scalar1, false);
115        canvas->translate(SkIntToScalar(450), 0);
116        showFour(canvas, scale, false);
117
118        canvas->translate(SkIntToScalar(-450), SkIntToScalar(450));
119        showFour(canvas, SK_Scalar1, true);
120        canvas->translate(SkIntToScalar(450), 0);
121        showFour(canvas, scale, true);
122    }
123
124private:
125    typedef GM INHERITED;
126};
127
128//////////////////////////////////////////////////////////////////////////////
129
130static GM* MyFactory(void*) { return new FillTypePerspGM; }
131static GMRegistry reg(MyFactory);
132
133}
134