blob: e68ee98df04c1a419fd8c2569d3b6c145174b3da [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"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000012
13namespace skiagm {
14
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000015static void makebm(SkBitmap* bm, int w, int h) {
16 bm->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000017 bm->eraseColor(SK_ColorTRANSPARENT);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000018
19 SkCanvas canvas(*bm);
epoger@google.comb28b5e42011-05-24 14:51:57 +000020 SkScalar s = SkIntToScalar(SkMin32(w, h));
reed@google.com38690072011-01-26 01:44:18 +000021 SkPoint pts[] = { { 0, 0 }, { s, s } };
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000022 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
23 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
24 SkPaint paint;
25
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000026 paint.setDither(true);
27 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000028 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000029 canvas.drawPaint(paint);
30}
31
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000032///////////////////////////////////////////////////////////////////////////////
33
34struct GradData {
35 int fCount;
36 const SkColor* fColors;
37 const SkScalar* fPos;
38};
39
40static const SkColor gColors[] = {
41 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
42};
43
44static const GradData gGradData[] = {
halcanary96fcdcc2015-08-27 07:41:13 -070045 { 2, gColors, nullptr },
46 { 5, gColors, nullptr },
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000047};
48
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000049static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
50 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCount, tm);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000051}
52
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000053static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000054 SkPoint center;
55 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
56 SkScalarAve(pts[0].fY, pts[1].fY));
57 return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000058 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* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode) {
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));
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000065 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000066}
67
reed71a6cbf2015-05-04 08:32:51 -070068static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000069 SkPoint center0, center1;
70 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
71 SkScalarAve(pts[0].fY, pts[1].fY));
72 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
73 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed71a6cbf2015-05-04 08:32:51 -070074 return SkGradientShader::CreateTwoPointConical(
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000075 center1, (pts[1].fX - pts[0].fX) / 7,
76 center0, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000077 data.fColors, data.fPos, data.fCount, tm);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000078}
79
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000080typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm);
skia.committer@gmail.com7693dbf2014-05-23 03:03:34 +000081
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000082static const GradMaker gGradMakers[] = {
reed71a6cbf2015-05-04 08:32:51 -070083 MakeLinear, MakeRadial, MakeSweep, Make2Conical
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000084};
85
86///////////////////////////////////////////////////////////////////////////////
87
88class ShaderTextGM : public GM {
89public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000090 ShaderTextGM() {
caryclark65cdba62015-06-15 06:51:08 -070091 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000092 }
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000093
94protected:
95
mtklein36352bf2015-03-25 18:17:31 -070096 SkString onShortName() override {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000097 return SkString("shadertext");
98 }
99
mtklein36352bf2015-03-25 18:17:31 -0700100 SkISize onISize() override { return SkISize::Make(1450, 500); }
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000101
mtklein36352bf2015-03-25 18:17:31 -0700102 void onDraw(SkCanvas* canvas) override {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000103 const char text[] = "Shaded Text";
104 const int textLen = SK_ARRAY_COUNT(text) - 1;
bsalomon@google.com39149582011-06-13 21:55:32 +0000105 const int pointSize = 36;
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000106
reedda8cf5a2014-09-04 13:18:36 -0700107 const int w = pointSize * textLen;
108 const int h = pointSize;
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000109
110 SkPoint pts[2] = {
111 { 0, 0 },
112 { SkIntToScalar(w), SkIntToScalar(h) }
113 };
114 SkScalar textBase = SkIntToScalar(h/2);
115
116 SkShader::TileMode tileModes[] = {
117 SkShader::kClamp_TileMode,
118 SkShader::kRepeat_TileMode,
119 SkShader::kMirror_TileMode
120 };
121
122 static const int gradCount = SK_ARRAY_COUNT(gGradData) *
123 SK_ARRAY_COUNT(gGradMakers);
124 static const int bmpCount = SK_ARRAY_COUNT(tileModes) *
125 SK_ARRAY_COUNT(tileModes);
126 SkShader* shaders[gradCount + bmpCount];
127
128 int shdIdx = 0;
129 for (size_t d = 0; d < SK_ARRAY_COUNT(gGradData); ++d) {
130 for (size_t m = 0; m < SK_ARRAY_COUNT(gGradMakers); ++m) {
131 shaders[shdIdx++] = gGradMakers[m](pts,
132 gGradData[d],
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000133 SkShader::kClamp_TileMode);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000134 }
135 }
reedda8cf5a2014-09-04 13:18:36 -0700136
137 SkBitmap bm;
138 makebm(&bm, w/16, h/4);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000139 for (size_t tx = 0; tx < SK_ARRAY_COUNT(tileModes); ++tx) {
140 for (size_t ty = 0; ty < SK_ARRAY_COUNT(tileModes); ++ty) {
reedda8cf5a2014-09-04 13:18:36 -0700141 shaders[shdIdx++] = SkShader::CreateBitmapShader(bm, tileModes[tx], tileModes[ty]);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000142 }
143 }
144
145 SkPaint paint;
146 paint.setDither(true);
147 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700148 sk_tool_utils::set_portable_typeface(&paint);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000149 paint.setTextSize(SkIntToScalar(pointSize));
150
151 canvas->save();
152 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
153
bsalomon@google.com39149582011-06-13 21:55:32 +0000154 SkPath path;
bsalomon@google.com137c4282011-06-14 17:13:22 +0000155 path.arcTo(SkRect::MakeXYWH(SkIntToScalar(-40), SkIntToScalar(15),
rmistry@google.comae933ce2012-08-23 18:19:56 +0000156 SkIntToScalar(300), SkIntToScalar(90)),
157 SkIntToScalar(225), SkIntToScalar(90),
bsalomon@google.com137c4282011-06-14 17:13:22 +0000158 false);
bsalomon@google.com39149582011-06-13 21:55:32 +0000159 path.close();
160
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000161 static const int testsPerCol = 8;
162 static const int rowHeight = 60;
163 static const int colWidth = 300;
164 canvas->save();
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000165 for (int s = 0; s < static_cast<int>(SK_ARRAY_COUNT(shaders)); s++) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000166 canvas->save();
bsalomon@google.com39149582011-06-13 21:55:32 +0000167 int i = 2*s;
168 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
169 SkIntToScalar((i % testsPerCol) * rowHeight));
170 paint.setShader(shaders[s])->unref();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000171 canvas->drawText(text, textLen, 0, textBase, paint);
172 canvas->restore();
bsalomon@google.com39149582011-06-13 21:55:32 +0000173 canvas->save();
174 ++i;
175 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
176 SkIntToScalar((i % testsPerCol) * rowHeight));
halcanary96fcdcc2015-08-27 07:41:13 -0700177 canvas->drawTextOnPath(text, textLen, path, nullptr, paint);
bsalomon@google.com39149582011-06-13 21:55:32 +0000178 canvas->restore();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000179 }
180 canvas->restore();
bsalomon@google.com39149582011-06-13 21:55:32 +0000181
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000182 }
183
184private:
185 typedef GM INHERITED;
186};
187
188///////////////////////////////////////////////////////////////////////////////
189
190static GM* MyFactory(void*) { return new ShaderTextGM; }
191static GMRegistry reg(MyFactory);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000192}