blob: f13fd4edc7927fb430c92ed6043e5033be6a473b [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 "SampleCode.h"
bsalomon@google.com0fdaa222011-01-25 18:35:26 +00009#include "SkCanvas.h"
10#include "SkGradientShader.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
12#include "SkView.h"
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000013
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +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);
reed@google.com261b8e22011-04-14 17:53:24 +000019 SkScalar s = SkIntToScalar(w < h ? w : h);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +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.com02939ce2012-06-06 12:09:51 +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.orga8c18312014-02-17 02:55:57 +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[] = {
halcanary96fcdcc2015-08-27 07:41:13 -070053 { 2, gColors, nullptr },
54 { 5, gColors, nullptr },
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000055};
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 tm) {
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
reed71a6cbf2015-05-04 08:32:51 -070076static SkShader* Make2Conical(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));
reed71a6cbf2015-05-04 08:32:51 -070082 return SkGradientShader::CreateTwoPointConical(
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000083 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);
89
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000090static const GradMaker gGradMakers[] = {
reed71a6cbf2015-05-04 08:32:51 -070091 MakeLinear, MakeRadial, MakeSweep, Make2Conical
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000092};
93
94///////////////////////////////////////////////////////////////////////////////
95
reed@google.comf2183392011-04-22 14:10:48 +000096class ShaderTextView : public SampleView {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000097public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000098 ShaderTextView() {
reed@google.comf2183392011-04-22 14:10:48 +000099 this->setBGColor(0xFFDDDDDD);
100 }
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000101
102protected:
103 // overrides from SkEventSink
104 virtual bool onQuery(SkEvent* evt) {
105 if (SampleCode::TitleQ(*evt)) {
106 SampleCode::TitleR(evt, "Shader Text");
107 return true;
108 }
109 return this->INHERITED::onQuery(evt);
110 }
111
reed@google.comf2183392011-04-22 14:10:48 +0000112 virtual void onDrawContent(SkCanvas* canvas) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000113 const char text[] = "Shaded Text";
114 const int textLen = SK_ARRAY_COUNT(text) - 1;
bsalomon@google.com39149582011-06-13 21:55:32 +0000115 static int pointSize = 36;
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000116
117 int w = pointSize * textLen;
118 int h = pointSize;
119
120 SkPoint pts[2] = {
121 { 0, 0 },
122 { SkIntToScalar(w), SkIntToScalar(h) }
123 };
124 SkScalar textBase = SkIntToScalar(h/2);
125
126 SkShader::TileMode tileModes[] = {
127 SkShader::kClamp_TileMode,
128 SkShader::kRepeat_TileMode,
129 SkShader::kMirror_TileMode
130 };
131
132 static const int gradCount = SK_ARRAY_COUNT(gGradData) *
133 SK_ARRAY_COUNT(gGradMakers);
134 static const int bmpCount = SK_ARRAY_COUNT(tileModes) *
135 SK_ARRAY_COUNT(tileModes);
136 SkShader* shaders[gradCount + bmpCount];
137
138 int shdIdx = 0;
139 for (size_t d = 0; d < SK_ARRAY_COUNT(gGradData); ++d) {
140 for (size_t m = 0; m < SK_ARRAY_COUNT(gGradMakers); ++m) {
141 shaders[shdIdx++] = gGradMakers[m](pts,
142 gGradData[d],
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000143 SkShader::kClamp_TileMode);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000144 }
145 }
146 for (size_t tx = 0; tx < SK_ARRAY_COUNT(tileModes); ++tx) {
147 for (size_t ty = 0; ty < SK_ARRAY_COUNT(tileModes); ++ty) {
148 shaders[shdIdx++] = MakeBitmapShader(tileModes[tx],
149 tileModes[ty],
150 w/8, h);
151 }
152 }
153
154 SkPaint paint;
155 paint.setDither(true);
156 paint.setAntiAlias(true);
157 paint.setTextSize(SkIntToScalar(pointSize));
158
159 canvas->save();
160 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
161
bsalomon@google.com39149582011-06-13 21:55:32 +0000162 SkPath path;
bsalomon@google.com137c4282011-06-14 17:13:22 +0000163 path.arcTo(SkRect::MakeXYWH(SkIntToScalar(-40), SkIntToScalar(15),
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164 SkIntToScalar(300), SkIntToScalar(90)),
165 SkIntToScalar(225), SkIntToScalar(90),
bsalomon@google.com137c4282011-06-14 17:13:22 +0000166 false);
bsalomon@google.com39149582011-06-13 21:55:32 +0000167 path.close();
168
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000169 static const int testsPerCol = 8;
170 static const int rowHeight = 60;
171 static const int colWidth = 300;
172 canvas->save();
173 for (size_t s = 0; s < SK_ARRAY_COUNT(shaders); s++) {
174 canvas->save();
reed@google.com7fa2a652014-01-27 13:42:58 +0000175 size_t i = 2*s;
bsalomon@google.com39149582011-06-13 21:55:32 +0000176 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
177 SkIntToScalar((i % testsPerCol) * rowHeight));
senorblanco@chromium.orge580bc12011-05-19 18:33:33 +0000178 paint.setShader(shaders[s])->unref();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000179 canvas->drawText(text, textLen, 0, textBase, paint);
180 canvas->restore();
bsalomon@google.com39149582011-06-13 21:55:32 +0000181 canvas->save();
182 ++i;
183 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
184 SkIntToScalar((i % testsPerCol) * rowHeight));
halcanary96fcdcc2015-08-27 07:41:13 -0700185 canvas->drawTextOnPath(text, textLen, path, nullptr, paint);
bsalomon@google.com39149582011-06-13 21:55:32 +0000186 canvas->restore();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000187 }
188 canvas->restore();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000189 }
190
191private:
reed@google.comf2183392011-04-22 14:10:48 +0000192 typedef SampleView INHERITED;
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000193};
194
195///////////////////////////////////////////////////////////////////////////////
196
197static SkView* MyFactory() { return new ShaderTextView; }
198static SkViewRegister reg(MyFactory);