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
12 namespace skiagm {
13
14 class FillTypePerspGM : public GM {
15 SkPath fPath;
16 public:
FillTypePerspGM()17 FillTypePerspGM() {}
18
makePath()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
27 protected:
28
onShortName()29 SkString onShortName() override {
30 return SkString("filltypespersp");
31 }
32
onISize()33 SkISize onISize() override {
34 return SkISize::Make(835, 840);
35 }
36
showPath(SkCanvas * canvas,int x,int y,SkPath::FillType ft,SkScalar scale,const SkPaint & paint)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
showFour(SkCanvas * canvas,SkScalar scale,bool aa)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 paint.setShader(SkGradientShader::MakeRadial(center,
59 SkIntToScalar(100),
60 colors,
61 pos,
62 SK_ARRAY_COUNT(colors),
63 SkShader::kClamp_TileMode));
64 paint.setAntiAlias(aa);
65
66 showPath(canvas, 0, 0, SkPath::kWinding_FillType,
67 scale, paint);
68 showPath(canvas, 200, 0, SkPath::kEvenOdd_FillType,
69 scale, paint);
70 showPath(canvas, 00, 200, SkPath::kInverseWinding_FillType,
71 scale, paint);
72 showPath(canvas, 200, 200, SkPath::kInverseEvenOdd_FillType,
73 scale, paint);
74 }
75
onDraw(SkCanvas * canvas)76 void onDraw(SkCanvas* canvas) override {
77 this->makePath();
78
79 // do perspective drawPaint as the background;
80 SkPaint bkgnrd;
81 SkPoint center = SkPoint::Make(SkIntToScalar(100),
82 SkIntToScalar(100));
83 SkColor colors[] = {SK_ColorBLACK, SK_ColorCYAN,
84 SK_ColorYELLOW, SK_ColorWHITE};
85 SkScalar pos[] = {0, SK_ScalarHalf / 2,
86 3 * SK_ScalarHalf / 2, SK_Scalar1};
87 bkgnrd.setShader(SkGradientShader::MakeRadial(center,
88 SkIntToScalar(1000),
89 colors,
90 pos,
91 SK_ARRAY_COUNT(colors),
92 SkShader::kClamp_TileMode));
93 canvas->save();
94 canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
95 SkMatrix mat;
96 mat.reset();
97 mat.setPerspY(SK_Scalar1 / 1000);
98 canvas->concat(mat);
99 canvas->drawPaint(bkgnrd);
100 canvas->restore();
101
102 // draw the paths in perspective
103 SkMatrix persp;
104 persp.reset();
105 persp.setPerspX(-SK_Scalar1 / 1800);
106 persp.setPerspY(SK_Scalar1 / 500);
107 canvas->concat(persp);
108
109 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
110 const SkScalar scale = SkIntToScalar(5)/4;
111
112 showFour(canvas, SK_Scalar1, false);
113 canvas->translate(SkIntToScalar(450), 0);
114 showFour(canvas, scale, false);
115
116 canvas->translate(SkIntToScalar(-450), SkIntToScalar(450));
117 showFour(canvas, SK_Scalar1, true);
118 canvas->translate(SkIntToScalar(450), 0);
119 showFour(canvas, scale, true);
120 }
121
122 private:
123 typedef GM INHERITED;
124 };
125
126 //////////////////////////////////////////////////////////////////////////////
127
MyFactory(void *)128 static GM* MyFactory(void*) { return new FillTypePerspGM; }
129 static GMRegistry reg(MyFactory);
130
131 }
132