blob: ac858468f656f58c6353d16cd2c5cd9399bd2d3d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +00007
bsalomon@google.com0fdaa222011-01-25 18:35:26 +00008#include "gm.h"
9#include "SkCanvas.h"
10#include "SkGradientShader.h"
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000011
12namespace skiagm {
13
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000014static void makebm(SkBitmap* bm, int w, int h) {
15 bm->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000016 bm->eraseColor(SK_ColorTRANSPARENT);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000017
18 SkCanvas canvas(*bm);
epoger@google.comb28b5e42011-05-24 14:51:57 +000019 SkScalar s = SkIntToScalar(SkMin32(w, h));
reed@google.com38690072011-01-26 01:44:18 +000020 SkPoint pts[] = { { 0, 0 }, { s, s } };
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000021 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
22 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
23 SkPaint paint;
24
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000025 paint.setDither(true);
26 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000027 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000028 canvas.drawPaint(paint);
29}
30
caryclark@google.com13130862012-06-06 12:10:45 +000031static SkShader* MakeBitmapShader(SkShader::TileMode tx, SkShader::TileMode ty,
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000032 int w, int h) {
33 static SkBitmap bmp;
34 if (bmp.isNull()) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000035 makebm(&bmp, w/2, h/4);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000036 }
37 return SkShader::CreateBitmapShader(bmp, tx, ty);
38}
39
40///////////////////////////////////////////////////////////////////////////////
41
42struct GradData {
43 int fCount;
44 const SkColor* fColors;
45 const SkScalar* fPos;
46};
47
48static const SkColor gColors[] = {
49 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
50};
51
52static const GradData gGradData[] = {
53 { 2, gColors, NULL },
54 { 5, gColors, NULL },
55};
56
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000057static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
58 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCount, tm);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000059}
60
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000061static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000062 SkPoint center;
63 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
64 SkScalarAve(pts[0].fY, pts[1].fY));
65 return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000066 data.fPos, data.fCount, tm);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000067}
68
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000069static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000070 SkPoint center;
71 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
72 SkScalarAve(pts[0].fY, pts[1].fY));
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000073 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000074}
75
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000076static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000077 SkPoint center0, center1;
78 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
79 SkScalarAve(pts[0].fY, pts[1].fY));
80 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
81 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
82 return SkGradientShader::CreateTwoPointRadial(
83 center1, (pts[1].fX - pts[0].fX) / 7,
84 center0, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000085 data.fColors, data.fPos, data.fCount, tm);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000086}
87
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000088typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm);
skia.committer@gmail.com7693dbf2014-05-23 03:03:34 +000089
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000090static const GradMaker gGradMakers[] = {
91 MakeLinear, MakeRadial, MakeSweep, Make2Radial
92};
93
94///////////////////////////////////////////////////////////////////////////////
95
96class ShaderTextGM : public GM {
97public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000098 ShaderTextGM() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000099 this->setBGColor(0xFFDDDDDD);
100 }
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000101
102protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000103 virtual uint32_t onGetFlags() const SK_OVERRIDE {
104 return kSkipTiled_Flag;
105 }
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000106
107 SkString onShortName() {
108 return SkString("shadertext");
109 }
110
tfarinaf5393182014-06-09 23:59:03 -0700111 SkISize onISize() { return SkISize::Make(1450, 500); }
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000112
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000113 virtual void onDraw(SkCanvas* canvas) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000114 const char text[] = "Shaded Text";
115 const int textLen = SK_ARRAY_COUNT(text) - 1;
bsalomon@google.com39149582011-06-13 21:55:32 +0000116 const int pointSize = 36;
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000117
118 int w = pointSize * textLen;
119 int h = pointSize;
120
121 SkPoint pts[2] = {
122 { 0, 0 },
123 { SkIntToScalar(w), SkIntToScalar(h) }
124 };
125 SkScalar textBase = SkIntToScalar(h/2);
126
127 SkShader::TileMode tileModes[] = {
128 SkShader::kClamp_TileMode,
129 SkShader::kRepeat_TileMode,
130 SkShader::kMirror_TileMode
131 };
132
133 static const int gradCount = SK_ARRAY_COUNT(gGradData) *
134 SK_ARRAY_COUNT(gGradMakers);
135 static const int bmpCount = SK_ARRAY_COUNT(tileModes) *
136 SK_ARRAY_COUNT(tileModes);
137 SkShader* shaders[gradCount + bmpCount];
138
139 int shdIdx = 0;
140 for (size_t d = 0; d < SK_ARRAY_COUNT(gGradData); ++d) {
141 for (size_t m = 0; m < SK_ARRAY_COUNT(gGradMakers); ++m) {
142 shaders[shdIdx++] = gGradMakers[m](pts,
143 gGradData[d],
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000144 SkShader::kClamp_TileMode);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000145 }
146 }
147 for (size_t tx = 0; tx < SK_ARRAY_COUNT(tileModes); ++tx) {
148 for (size_t ty = 0; ty < SK_ARRAY_COUNT(tileModes); ++ty) {
149 shaders[shdIdx++] = MakeBitmapShader(tileModes[tx],
150 tileModes[ty],
151 w/8, h);
152 }
153 }
154
155 SkPaint paint;
156 paint.setDither(true);
157 paint.setAntiAlias(true);
158 paint.setTextSize(SkIntToScalar(pointSize));
159
160 canvas->save();
161 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
162
bsalomon@google.com39149582011-06-13 21:55:32 +0000163 SkPath path;
bsalomon@google.com137c4282011-06-14 17:13:22 +0000164 path.arcTo(SkRect::MakeXYWH(SkIntToScalar(-40), SkIntToScalar(15),
rmistry@google.comae933ce2012-08-23 18:19:56 +0000165 SkIntToScalar(300), SkIntToScalar(90)),
166 SkIntToScalar(225), SkIntToScalar(90),
bsalomon@google.com137c4282011-06-14 17:13:22 +0000167 false);
bsalomon@google.com39149582011-06-13 21:55:32 +0000168 path.close();
169
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000170 static const int testsPerCol = 8;
171 static const int rowHeight = 60;
172 static const int colWidth = 300;
173 canvas->save();
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000174 for (int s = 0; s < static_cast<int>(SK_ARRAY_COUNT(shaders)); s++) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000175 canvas->save();
bsalomon@google.com39149582011-06-13 21:55:32 +0000176 int i = 2*s;
177 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
178 SkIntToScalar((i % testsPerCol) * rowHeight));
179 paint.setShader(shaders[s])->unref();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000180 canvas->drawText(text, textLen, 0, textBase, paint);
181 canvas->restore();
bsalomon@google.com39149582011-06-13 21:55:32 +0000182 canvas->save();
183 ++i;
184 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
185 SkIntToScalar((i % testsPerCol) * rowHeight));
186 canvas->drawTextOnPath(text, textLen, path, NULL, paint);
187 canvas->restore();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000188 }
189 canvas->restore();
bsalomon@google.com39149582011-06-13 21:55:32 +0000190
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000191 }
192
193private:
194 typedef GM INHERITED;
195};
196
197///////////////////////////////////////////////////////////////////////////////
198
199static GM* MyFactory(void*) { return new ShaderTextGM; }
200static GMRegistry reg(MyFactory);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000201}