• 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 
53 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data,
54                      SkShader::TileMode tm, SkUnitMapper* mapper);
55 static const GradMaker gGradMakers[] = {
56     MakeLinear, MakeRadial, MakeSweep
57 };
58 
59 ///////////////////////////////////////////////////////////////////////////////
60 
61 class GradientsGM : public GM {
62 public:
GradientsGM()63 	GradientsGM() {}
64 
65 protected:
onShortName()66     SkString onShortName() {
67         return SkString("gradients");
68     }
69 
onISize()70 	SkISize onISize() { return make_isize(640, 380); }
71 
drawBG(SkCanvas * canvas)72     void drawBG(SkCanvas* canvas) {
73         canvas->drawColor(0xFFDDDDDD);
74     }
75 
onDraw(SkCanvas * canvas)76     virtual void onDraw(SkCanvas* canvas) {
77         this->drawBG(canvas);
78 
79         SkPoint pts[2] = {
80             { 0, 0 },
81             { SkIntToScalar(100), SkIntToScalar(100) }
82         };
83         SkShader::TileMode tm = SkShader::kClamp_TileMode;
84         SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
85         SkPaint paint;
86         paint.setAntiAlias(true);
87 
88         canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
89         for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
90             canvas->save();
91             for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
92                 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL);
93                 paint.setShader(shader);
94                 canvas->drawRect(r, paint);
95                 shader->unref();
96                 canvas->translate(0, SkIntToScalar(120));
97             }
98             canvas->restore();
99             canvas->translate(SkIntToScalar(120), 0);
100         }
101     }
102 
103 private:
104     typedef GM INHERITED;
105 };
106 
107 ///////////////////////////////////////////////////////////////////////////////
108 
MyFactory(void *)109 static GM* MyFactory(void*) { return new GradientsGM; }
110 static GMRegistry reg(MyFactory);
111 
112 }
113 
114