1
2 /*
3 * Copyright 2013 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 using namespace skiagm;
12
13 struct GradData {
14 int fCount;
15 const SkColor* fColors;
16 const SkScalar* fPos;
17 };
18
19 static const SkColor gColors[] = {
20 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
21 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
22 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
23 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
24 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
25 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
26 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
27 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
28 };
29
30 //static const SkScalar gPos[] = { SK_Scalar1*999/2000, SK_Scalar1*1001/2000 };
31
32 static const GradData gGradData[] = {
33 { 40, gColors, NULL },
34 // { 2, gColors, gPos },
35 // { 2, gCol2, NULL },
36 };
37
MakeLinear(const SkPoint pts[2],const GradData & data,SkShader::TileMode tm)38 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
39 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCount, tm);
40 }
41
MakeRadial(const SkPoint pts[2],const GradData & data,SkShader::TileMode tm)42 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
43 SkPoint center;
44 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
45 SkScalarAve(pts[0].fY, pts[1].fY));
46 return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
47 data.fPos, data.fCount, tm);
48 }
49
MakeSweep(const SkPoint pts[2],const GradData & data,SkShader::TileMode)50 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode) {
51 SkPoint center;
52 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
53 SkScalarAve(pts[0].fY, pts[1].fY));
54 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
55 }
56
57
58 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm);
59
60 static const GradMaker gGradMakers[] = {
61 MakeLinear, MakeRadial, MakeSweep,
62 };
63
64 ///////////////////////////////////////////////////////////////////////////////
65
66 class GradientsGM : public GM {
67 public:
GradientsGM()68 GradientsGM() {
69 this->setBGColor(0xFFDDDDDD);
70 }
71
72 protected:
onGetFlags() const73 virtual uint32_t onGetFlags() const SK_OVERRIDE {
74 return kSkipTiled_Flag;
75 }
76
onShortName()77 SkString onShortName() SK_OVERRIDE { return SkString("gradient_dirty_laundry"); }
onISize()78 virtual SkISize onISize() SK_OVERRIDE { return SkISize::Make(640, 615); }
79
onDraw(SkCanvas * canvas)80 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
81 SkPoint pts[2] = { { 0, 0 },
82 { SkIntToScalar(100), SkIntToScalar(100) }
83 };
84 SkShader::TileMode tm = SkShader::kClamp_TileMode;
85 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
86 SkPaint paint;
87 paint.setAntiAlias(true);
88
89 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
90 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
91 canvas->save();
92 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
93 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm);
94 paint.setShader(shader)->unref();
95 canvas->drawRect(r, paint);
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