• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "gm.h"
2 #include "SkGradientShader.h"
3 
4 namespace skiagm {
5 
6 struct GradData {
7     int             fCount;
8     const SkColor*  fColors;
9     const SkScalar* fPos;
10 };
11 
12 static const SkColor gColors[] = {
13     SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
14 };
15 static const SkScalar gPos0[] = { 0, SK_Scalar1 };
16 static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
17 static const SkScalar gPos2[] = {
18     0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
19 };
20 
21 static const GradData gGradData[] = {
22     { 2, gColors, NULL },
23     { 2, gColors, gPos0 },
24     { 2, gColors, gPos1 },
25     { 5, gColors, NULL },
26     { 5, gColors, gPos2 }
27 };
28 
MakeLinear(const SkPoint pts[2],const GradData & data,SkShader::TileMode tm,SkUnitMapper * mapper)29 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data,
30                             SkShader::TileMode tm, SkUnitMapper* mapper) {
31     return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos,
32                                           data.fCount, tm, mapper);
33 }
34 
MakeRadial(const SkPoint pts[2],const GradData & data,SkShader::TileMode tm,SkUnitMapper * mapper)35 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data,
36                             SkShader::TileMode tm, SkUnitMapper* mapper) {
37     SkPoint center;
38     center.set(SkScalarAve(pts[0].fX, pts[1].fX),
39                SkScalarAve(pts[0].fY, pts[1].fY));
40     return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
41                                           data.fPos, data.fCount, tm, mapper);
42 }
43 
MakeSweep(const SkPoint pts[2],const GradData & data,SkShader::TileMode tm,SkUnitMapper * mapper)44 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data,
45                            SkShader::TileMode tm, SkUnitMapper* mapper) {
46     SkPoint center;
47     center.set(SkScalarAve(pts[0].fX, pts[1].fX),
48                SkScalarAve(pts[0].fY, pts[1].fY));
49     return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors,
50                                          data.fPos, data.fCount, mapper);
51 }
52 
Make2Radial(const SkPoint pts[2],const GradData & data,SkShader::TileMode tm,SkUnitMapper * mapper)53 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data,
54                              SkShader::TileMode tm, SkUnitMapper* mapper) {
55     SkPoint center0, center1;
56     center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
57                 SkScalarAve(pts[0].fY, pts[1].fY));
58     center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
59                 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
60     return SkGradientShader::CreateTwoPointRadial(
61                                                   center1, (pts[1].fX - pts[0].fX) / 7,
62                                                   center0, (pts[1].fX - pts[0].fX) / 2,
63                                                   data.fColors, data.fPos, data.fCount, tm, mapper);
64 }
65 
66 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data,
67                                SkShader::TileMode tm, SkUnitMapper* mapper);
68 static const GradMaker gGradMakers[] = {
69     MakeLinear, MakeRadial, MakeSweep, Make2Radial
70 };
71 
72 ///////////////////////////////////////////////////////////////////////////////
73 
74 class GradientsGM : public GM {
75 public:
GradientsGM()76 	GradientsGM() {}
77 
78 protected:
onShortName()79     SkString onShortName() {
80         return SkString("gradients");
81     }
82 
onISize()83 	SkISize onISize() { return make_isize(640, 510); }
84 
drawBG(SkCanvas * canvas)85     void drawBG(SkCanvas* canvas) {
86         canvas->drawColor(0xFFDDDDDD);
87     }
88 
onDraw(SkCanvas * canvas)89     virtual void onDraw(SkCanvas* canvas) {
90         this->drawBG(canvas);
91 
92         SkPoint pts[2] = {
93             { 0, 0 },
94             { SkIntToScalar(100), SkIntToScalar(100) }
95         };
96         SkShader::TileMode tm = SkShader::kClamp_TileMode;
97         SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
98         SkPaint paint;
99         paint.setAntiAlias(true);
100 
101         canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
102         for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
103             canvas->save();
104             for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
105                 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL);
106                 paint.setShader(shader);
107                 canvas->drawRect(r, paint);
108                 shader->unref();
109                 canvas->translate(0, SkIntToScalar(120));
110             }
111             canvas->restore();
112             canvas->translate(SkIntToScalar(120), 0);
113         }
114     }
115 
116 private:
117     typedef GM INHERITED;
118 };
119 
120 ///////////////////////////////////////////////////////////////////////////////
121 
MyFactory(void *)122 static GM* MyFactory(void*) { return new GradientsGM; }
123 static GMRegistry reg(MyFactory);
124 
125 }
126 
127