OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
9 #include "SkView.h" | 9 #include "SkView.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkGradientShader.h" | 11 #include "SkGradientShader.h" |
12 | 12 |
13 static SkShader* setgrad(const SkRect& r, SkColor c0, SkColor c1) { | 13 static SkShader* setgrad(const SkRect& r, SkColor c0, SkColor c1) { |
14 SkColor colors[] = { c0, c1 }; | 14 SkColor colors[] = { c0, c1 }; |
15 SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } }; | 15 SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } }; |
16 return SkGradientShader::CreateLinear(pts, colors, NULL, 2, | 16 return SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp
_TileMode); |
17 SkShader::kClamp_TileMode, NULL); | |
18 } | 17 } |
19 | 18 |
20 static void test_alphagradients(SkCanvas* canvas) { | 19 static void test_alphagradients(SkCanvas* canvas) { |
21 SkRect r; | 20 SkRect r; |
22 r.set(SkIntToScalar(10), SkIntToScalar(10), | 21 r.set(SkIntToScalar(10), SkIntToScalar(10), |
23 SkIntToScalar(410), SkIntToScalar(30)); | 22 SkIntToScalar(410), SkIntToScalar(30)); |
24 SkPaint p, p2; | 23 SkPaint p, p2; |
25 p2.setStyle(SkPaint::kStroke_Style); | 24 p2.setStyle(SkPaint::kStroke_Style); |
26 | 25 |
27 p.setShader(setgrad(r, 0xFF00FF00, 0x0000FF00))->unref(); | 26 p.setShader(setgrad(r, 0xFF00FF00, 0x0000FF00))->unref(); |
(...skipping 29 matching lines...) Expand all Loading... |
57 }; | 56 }; |
58 | 57 |
59 static const GradData gGradData[] = { | 58 static const GradData gGradData[] = { |
60 { 2, gColors, NULL }, | 59 { 2, gColors, NULL }, |
61 { 2, gColors, gPos0 }, | 60 { 2, gColors, gPos0 }, |
62 { 2, gColors, gPos1 }, | 61 { 2, gColors, gPos1 }, |
63 { 5, gColors, NULL }, | 62 { 5, gColors, NULL }, |
64 { 5, gColors, gPos2 } | 63 { 5, gColors, gPos2 } |
65 }; | 64 }; |
66 | 65 |
67 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, | 66 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader
::TileMode tm) { |
68 SkShader::TileMode tm, SkUnitMapper* mapper) { | 67 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCo
unt, tm); |
69 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, | |
70 data.fCount, tm, mapper); | |
71 } | 68 } |
72 | 69 |
73 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, | 70 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader
::TileMode tm) { |
74 SkShader::TileMode tm, SkUnitMapper* mapper) { | |
75 SkPoint center; | 71 SkPoint center; |
76 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 72 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
77 SkScalarAve(pts[0].fY, pts[1].fY)); | 73 SkScalarAve(pts[0].fY, pts[1].fY)); |
78 return SkGradientShader::CreateRadial(center, center.fX, data.fColors, | 74 return SkGradientShader::CreateRadial(center, center.fX, data.fColors, |
79 data.fPos, data.fCount, tm, mapper); | 75 data.fPos, data.fCount, tm); |
80 } | 76 } |
81 | 77 |
82 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, | 78 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader:
:TileMode tm) { |
83 SkShader::TileMode tm, SkUnitMapper* mapper) { | |
84 SkPoint center; | 79 SkPoint center; |
85 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 80 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
86 SkScalarAve(pts[0].fY, pts[1].fY)); | 81 SkScalarAve(pts[0].fY, pts[1].fY)); |
87 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, | 82 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, dat
a.fPos, data.fCount); |
88 data.fPos, data.fCount, mapper); | |
89 } | 83 } |
90 | 84 |
91 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, | 85 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, SkShade
r::TileMode tm) { |
92 SkShader::TileMode tm, SkUnitMapper* mapper) { | |
93 SkPoint center0, center1; | 86 SkPoint center0, center1; |
94 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), | 87 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
95 SkScalarAve(pts[0].fY, pts[1].fY)); | 88 SkScalarAve(pts[0].fY, pts[1].fY)); |
96 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), | 89 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
97 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); | 90 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
98 return SkGradientShader::CreateTwoPointRadial( | 91 return SkGradientShader::CreateTwoPointRadial( |
99 center1, (pts[1].fX - pts[0].fX) / 7, | 92 center1, (pts[1].fX - pts[0].fX) / 7, |
100 center0, (pts[1].fX - pts[0].fX) / 2, | 93 center0, (pts[1].fX - pts[0].fX) / 2, |
101 data.fColors, data.fPos, data.fCount, tm, mapper); | 94 data.fColors, data.fPos, data.fCount, tm); |
102 } | 95 } |
103 | 96 |
104 static SkShader* Make2RadialConcentric(const SkPoint pts[2], const GradData& dat
a, | 97 static SkShader* Make2RadialConcentric(const SkPoint pts[2], const GradData& dat
a, |
105 SkShader::TileMode tm, SkUnitMapper* mapp
er) { | 98 SkShader::TileMode tm) { |
106 SkPoint center; | 99 SkPoint center; |
107 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 100 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
108 SkScalarAve(pts[0].fY, pts[1].fY)); | 101 SkScalarAve(pts[0].fY, pts[1].fY)); |
109 return SkGradientShader::CreateTwoPointRadial( | 102 return SkGradientShader::CreateTwoPointRadial( |
110 center, (pts[1].fX - pts[0].fX) / 7, | 103 center, (pts[1].fX - pts[0].fX) / 7, |
111 center, (pts[1].fX - pts[0].fX) / 2, | 104 center, (pts[1].fX - pts[0].fX) / 2, |
112 data.fColors, data.fPos, data.fCount, tm, mapper); | 105 data.fColors, data.fPos, data.fCount, tm); |
113 } | 106 } |
114 | 107 |
115 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, | 108 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkSha
der::TileMode tm); |
116 SkShader::TileMode tm, SkUnitMapper* mapper); | 109 |
117 static const GradMaker gGradMakers[] = { | 110 static const GradMaker gGradMakers[] = { |
118 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2RadialConcentric | 111 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2RadialConcentric |
119 }; | 112 }; |
120 | 113 |
121 /////////////////////////////////////////////////////////////////////////////// | 114 /////////////////////////////////////////////////////////////////////////////// |
122 | 115 |
123 class GradientsView : public SampleView { | 116 class GradientsView : public SampleView { |
124 public: | 117 public: |
125 GradientsView() { | 118 GradientsView() { |
126 this->setBGColor(0xFFDDDDDD); | 119 this->setBGColor(0xFFDDDDDD); |
(...skipping 20 matching lines...) Expand all Loading... |
147 | 140 |
148 canvas->save(); | 141 canvas->save(); |
149 canvas->translate(SkIntToScalar(20), SkIntToScalar(10)); | 142 canvas->translate(SkIntToScalar(20), SkIntToScalar(10)); |
150 | 143 |
151 for (int tm = 0; tm < SkShader::kTileModeCount; ++tm) { | 144 for (int tm = 0; tm < SkShader::kTileModeCount; ++tm) { |
152 canvas->save(); | 145 canvas->save(); |
153 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { | 146 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
154 canvas->save(); | 147 canvas->save(); |
155 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { | 148 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
156 SkShader* shader; | 149 SkShader* shader; |
157 shader = gGradMakers[j](pts, gGradData[i], (SkShader::TileMo
de)tm, NULL); | 150 shader = gGradMakers[j](pts, gGradData[i], (SkShader::TileMo
de)tm); |
158 paint.setShader(shader)->unref(); | 151 paint.setShader(shader)->unref(); |
159 canvas->drawRect(r, paint); | 152 canvas->drawRect(r, paint); |
160 canvas->translate(0, SkIntToScalar(120)); | 153 canvas->translate(0, SkIntToScalar(120)); |
161 } | 154 } |
162 canvas->restore(); | 155 canvas->restore(); |
163 canvas->translate(SkIntToScalar(120), 0); | 156 canvas->translate(SkIntToScalar(120), 0); |
164 } | 157 } |
165 canvas->restore(); | 158 canvas->restore(); |
166 canvas->translate(SK_ARRAY_COUNT(gGradData)*SkIntToScalar(120), 0); | 159 canvas->translate(SK_ARRAY_COUNT(gGradData)*SkIntToScalar(120), 0); |
167 } | 160 } |
168 canvas->restore(); | 161 canvas->restore(); |
169 | 162 |
170 canvas->translate(0, SkIntToScalar(370)); | 163 canvas->translate(0, SkIntToScalar(370)); |
171 if (false) { // avoid bit rot, suppress warning | 164 if (false) { // avoid bit rot, suppress warning |
172 test_alphagradients(canvas); | 165 test_alphagradients(canvas); |
173 } | 166 } |
174 this->inval(NULL); | 167 this->inval(NULL); |
175 } | 168 } |
176 | 169 |
177 private: | 170 private: |
178 typedef SampleView INHERITED; | 171 typedef SampleView INHERITED; |
179 }; | 172 }; |
180 | 173 |
181 /////////////////////////////////////////////////////////////////////////////// | 174 /////////////////////////////////////////////////////////////////////////////// |
182 | 175 |
183 static SkView* MyFactory() { return new GradientsView; } | 176 static SkView* MyFactory() { return new GradientsView; } |
184 static SkViewRegister reg(MyFactory); | 177 static SkViewRegister reg(MyFactory); |
OLD | NEW |