blob: 2df03cce9315af227063f4af68f2b08d902f8383 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
bsalomon@google.com0fdaa222011-01-25 18:35:26 +00008#include "gm.h"
9#include "SkCanvas.h"
10#include "SkGradientShader.h"
11#include "SkUnitMappers.h"
12
13namespace skiagm {
14
15static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) {
16 bm->setConfig(config, w, h);
17 bm->allocPixels();
18 bm->eraseColor(0);
19
20 SkCanvas canvas(*bm);
epoger@google.comb28b5e42011-05-24 14:51:57 +000021 SkScalar s = SkIntToScalar(SkMin32(w, h));
reed@google.com38690072011-01-26 01:44:18 +000022 SkPoint pts[] = { { 0, 0 }, { s, s } };
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000023 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
24 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
25 SkPaint paint;
26
27 SkUnitMapper* um = NULL;
28
29 um = new SkCosineMapper;
30
31 SkAutoUnref au(um);
32
33 paint.setDither(true);
34 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
35 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref();
36 canvas.drawPaint(paint);
37}
38
39SkShader* MakeBitmapShader(SkShader::TileMode tx, SkShader::TileMode ty,
40 int w, int h) {
41 static SkBitmap bmp;
42 if (bmp.isNull()) {
43 makebm(&bmp, SkBitmap::kARGB_8888_Config, w/2, h/4);
44 }
45 return SkShader::CreateBitmapShader(bmp, tx, ty);
46}
47
48///////////////////////////////////////////////////////////////////////////////
49
50struct GradData {
51 int fCount;
52 const SkColor* fColors;
53 const SkScalar* fPos;
54};
55
56static const SkColor gColors[] = {
57 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
58};
59
60static const GradData gGradData[] = {
61 { 2, gColors, NULL },
62 { 5, gColors, NULL },
63};
64
65static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data,
66 SkShader::TileMode tm, SkUnitMapper* mapper) {
67 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos,
68 data.fCount, tm, mapper);
69}
70
71static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data,
72 SkShader::TileMode tm, SkUnitMapper* mapper) {
73 SkPoint center;
74 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
75 SkScalarAve(pts[0].fY, pts[1].fY));
76 return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
77 data.fPos, data.fCount, tm, mapper);
78}
79
80static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data,
81 SkShader::TileMode tm, SkUnitMapper* mapper) {
82 SkPoint center;
83 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
84 SkScalarAve(pts[0].fY, pts[1].fY));
85 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors,
86 data.fPos, data.fCount, mapper);
87}
88
89static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data,
90 SkShader::TileMode tm, SkUnitMapper* mapper) {
91 SkPoint center0, center1;
92 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
93 SkScalarAve(pts[0].fY, pts[1].fY));
94 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
95 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
96 return SkGradientShader::CreateTwoPointRadial(
97 center1, (pts[1].fX - pts[0].fX) / 7,
98 center0, (pts[1].fX - pts[0].fX) / 2,
99 data.fColors, data.fPos, data.fCount, tm, mapper);
100}
101
102typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data,
103 SkShader::TileMode tm, SkUnitMapper* mapper);
104static const GradMaker gGradMakers[] = {
105 MakeLinear, MakeRadial, MakeSweep, Make2Radial
106};
107
108///////////////////////////////////////////////////////////////////////////////
109
110class ShaderTextGM : public GM {
111public:
112 ShaderTextGM() {}
113
114protected:
115
116 SkString onShortName() {
117 return SkString("shadertext");
118 }
119
bsalomon@google.com39149582011-06-13 21:55:32 +0000120 SkISize onISize() { return make_isize(1450, 500); }
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000121
122 void drawBG(SkCanvas* canvas) {
123 canvas->drawColor(0xFFDDDDDD);
124 }
125
126 virtual void onDraw(SkCanvas* canvas) {
127 this->drawBG(canvas);
128
129 const char text[] = "Shaded Text";
130 const int textLen = SK_ARRAY_COUNT(text) - 1;
bsalomon@google.com39149582011-06-13 21:55:32 +0000131 const int pointSize = 36;
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000132
133 int w = pointSize * textLen;
134 int h = pointSize;
135
136 SkPoint pts[2] = {
137 { 0, 0 },
138 { SkIntToScalar(w), SkIntToScalar(h) }
139 };
140 SkScalar textBase = SkIntToScalar(h/2);
141
142 SkShader::TileMode tileModes[] = {
143 SkShader::kClamp_TileMode,
144 SkShader::kRepeat_TileMode,
145 SkShader::kMirror_TileMode
146 };
147
148 static const int gradCount = SK_ARRAY_COUNT(gGradData) *
149 SK_ARRAY_COUNT(gGradMakers);
150 static const int bmpCount = SK_ARRAY_COUNT(tileModes) *
151 SK_ARRAY_COUNT(tileModes);
152 SkShader* shaders[gradCount + bmpCount];
153
154 int shdIdx = 0;
155 for (size_t d = 0; d < SK_ARRAY_COUNT(gGradData); ++d) {
156 for (size_t m = 0; m < SK_ARRAY_COUNT(gGradMakers); ++m) {
157 shaders[shdIdx++] = gGradMakers[m](pts,
158 gGradData[d],
159 SkShader::kClamp_TileMode,
160 NULL);
161 }
162 }
163 for (size_t tx = 0; tx < SK_ARRAY_COUNT(tileModes); ++tx) {
164 for (size_t ty = 0; ty < SK_ARRAY_COUNT(tileModes); ++ty) {
165 shaders[shdIdx++] = MakeBitmapShader(tileModes[tx],
166 tileModes[ty],
167 w/8, h);
168 }
169 }
170
171 SkPaint paint;
172 paint.setDither(true);
173 paint.setAntiAlias(true);
174 paint.setTextSize(SkIntToScalar(pointSize));
175
176 canvas->save();
177 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
178
bsalomon@google.com39149582011-06-13 21:55:32 +0000179 SkPath path;
bsalomon@google.com137c4282011-06-14 17:13:22 +0000180 path.arcTo(SkRect::MakeXYWH(SkIntToScalar(-40), SkIntToScalar(15),
181 SkIntToScalar(300), SkIntToScalar(90)),
182 SkIntToScalar(225), SkIntToScalar(90),
183 false);
bsalomon@google.com39149582011-06-13 21:55:32 +0000184 path.close();
185
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000186 static const int testsPerCol = 8;
187 static const int rowHeight = 60;
188 static const int colWidth = 300;
189 canvas->save();
190 for (size_t s = 0; s < SK_ARRAY_COUNT(shaders); s++) {
191 canvas->save();
bsalomon@google.com39149582011-06-13 21:55:32 +0000192 int i = 2*s;
193 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
194 SkIntToScalar((i % testsPerCol) * rowHeight));
195 paint.setShader(shaders[s])->unref();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000196 canvas->drawText(text, textLen, 0, textBase, paint);
197 canvas->restore();
bsalomon@google.com39149582011-06-13 21:55:32 +0000198 canvas->save();
199 ++i;
200 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
201 SkIntToScalar((i % testsPerCol) * rowHeight));
202 canvas->drawTextOnPath(text, textLen, path, NULL, paint);
203 canvas->restore();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000204 }
205 canvas->restore();
bsalomon@google.com39149582011-06-13 21:55:32 +0000206
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000207 }
208
209private:
210 typedef GM INHERITED;
211};
212
213///////////////////////////////////////////////////////////////////////////////
214
215static GM* MyFactory(void*) { return new ShaderTextGM; }
216static GMRegistry reg(MyFactory);
217
218}