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