blob: 37be71092f9e823494eaff5422e8718253310106 [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
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000031///////////////////////////////////////////////////////////////////////////////
32
33struct GradData {
34 int fCount;
35 const SkColor* fColors;
36 const SkScalar* fPos;
37};
38
39static const SkColor gColors[] = {
40 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
41};
42
43static const GradData gGradData[] = {
44 { 2, gColors, NULL },
45 { 5, gColors, NULL },
46};
47
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000048static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
49 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCount, tm);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000050}
51
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000052static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000053 SkPoint center;
54 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
55 SkScalarAve(pts[0].fY, pts[1].fY));
56 return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000057 data.fPos, data.fCount, tm);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000058}
59
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000060static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000061 SkPoint center;
62 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
63 SkScalarAve(pts[0].fY, pts[1].fY));
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000064 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000065}
66
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000067static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000068 SkPoint center0, center1;
69 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
70 SkScalarAve(pts[0].fY, pts[1].fY));
71 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
72 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
73 return SkGradientShader::CreateTwoPointRadial(
74 center1, (pts[1].fX - pts[0].fX) / 7,
75 center0, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000076 data.fColors, data.fPos, data.fCount, tm);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000077}
78
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000079typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm);
skia.committer@gmail.com7693dbf2014-05-23 03:03:34 +000080
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000081static const GradMaker gGradMakers[] = {
82 MakeLinear, MakeRadial, MakeSweep, Make2Radial
83};
84
85///////////////////////////////////////////////////////////////////////////////
86
87class ShaderTextGM : public GM {
88public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000089 ShaderTextGM() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000090 this->setBGColor(0xFFDDDDDD);
91 }
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000092
93protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000094 virtual uint32_t onGetFlags() const SK_OVERRIDE {
95 return kSkipTiled_Flag;
96 }
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000097
98 SkString onShortName() {
99 return SkString("shadertext");
100 }
101
tfarinaf5393182014-06-09 23:59:03 -0700102 SkISize onISize() { return SkISize::Make(1450, 500); }
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000103
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000104 virtual void onDraw(SkCanvas* canvas) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000105 const char text[] = "Shaded Text";
106 const int textLen = SK_ARRAY_COUNT(text) - 1;
bsalomon@google.com39149582011-06-13 21:55:32 +0000107 const int pointSize = 36;
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000108
reedda8cf5a2014-09-04 13:18:36 -0700109 const int w = pointSize * textLen;
110 const int h = pointSize;
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000111
112 SkPoint pts[2] = {
113 { 0, 0 },
114 { SkIntToScalar(w), SkIntToScalar(h) }
115 };
116 SkScalar textBase = SkIntToScalar(h/2);
117
118 SkShader::TileMode tileModes[] = {
119 SkShader::kClamp_TileMode,
120 SkShader::kRepeat_TileMode,
121 SkShader::kMirror_TileMode
122 };
123
124 static const int gradCount = SK_ARRAY_COUNT(gGradData) *
125 SK_ARRAY_COUNT(gGradMakers);
126 static const int bmpCount = SK_ARRAY_COUNT(tileModes) *
127 SK_ARRAY_COUNT(tileModes);
128 SkShader* shaders[gradCount + bmpCount];
129
130 int shdIdx = 0;
131 for (size_t d = 0; d < SK_ARRAY_COUNT(gGradData); ++d) {
132 for (size_t m = 0; m < SK_ARRAY_COUNT(gGradMakers); ++m) {
133 shaders[shdIdx++] = gGradMakers[m](pts,
134 gGradData[d],
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000135 SkShader::kClamp_TileMode);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000136 }
137 }
reedda8cf5a2014-09-04 13:18:36 -0700138
139 SkBitmap bm;
140 makebm(&bm, w/16, h/4);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000141 for (size_t tx = 0; tx < SK_ARRAY_COUNT(tileModes); ++tx) {
142 for (size_t ty = 0; ty < SK_ARRAY_COUNT(tileModes); ++ty) {
reedda8cf5a2014-09-04 13:18:36 -0700143 shaders[shdIdx++] = SkShader::CreateBitmapShader(bm, tileModes[tx], tileModes[ty]);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000144 }
145 }
146
147 SkPaint paint;
148 paint.setDither(true);
149 paint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400150 sk_tool_utils::set_portable_typeface(&paint);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000151 paint.setTextSize(SkIntToScalar(pointSize));
152
153 canvas->save();
154 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
155
bsalomon@google.com39149582011-06-13 21:55:32 +0000156 SkPath path;
bsalomon@google.com137c4282011-06-14 17:13:22 +0000157 path.arcTo(SkRect::MakeXYWH(SkIntToScalar(-40), SkIntToScalar(15),
rmistry@google.comae933ce2012-08-23 18:19:56 +0000158 SkIntToScalar(300), SkIntToScalar(90)),
159 SkIntToScalar(225), SkIntToScalar(90),
bsalomon@google.com137c4282011-06-14 17:13:22 +0000160 false);
bsalomon@google.com39149582011-06-13 21:55:32 +0000161 path.close();
162
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000163 static const int testsPerCol = 8;
164 static const int rowHeight = 60;
165 static const int colWidth = 300;
166 canvas->save();
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000167 for (int s = 0; s < static_cast<int>(SK_ARRAY_COUNT(shaders)); s++) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000168 canvas->save();
bsalomon@google.com39149582011-06-13 21:55:32 +0000169 int i = 2*s;
170 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
171 SkIntToScalar((i % testsPerCol) * rowHeight));
172 paint.setShader(shaders[s])->unref();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000173 canvas->drawText(text, textLen, 0, textBase, paint);
174 canvas->restore();
bsalomon@google.com39149582011-06-13 21:55:32 +0000175 canvas->save();
176 ++i;
177 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
178 SkIntToScalar((i % testsPerCol) * rowHeight));
179 canvas->drawTextOnPath(text, textLen, path, NULL, paint);
180 canvas->restore();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000181 }
182 canvas->restore();
bsalomon@google.com39149582011-06-13 21:55:32 +0000183
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000184 }
185
186private:
187 typedef GM INHERITED;
188};
189
190///////////////////////////////////////////////////////////////////////////////
191
192static GM* MyFactory(void*) { return new ShaderTextGM; }
193static GMRegistry reg(MyFactory);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000194}