blob: 89abfc234549d0b355f64e2e3b346e823a07a8ab [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"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkGradientShader.h"
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000012
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000013static void makebm(SkBitmap* bm, int w, int h) {
14 bm->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000015 bm->eraseColor(SK_ColorTRANSPARENT);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000016
17 SkCanvas canvas(*bm);
reed@google.com261b8e22011-04-14 17:53:24 +000018 SkScalar s = SkIntToScalar(w < h ? w : h);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000019 SkPoint pts[] = { { 0, 0 }, { s, s } };
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000020 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
21 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
22 SkPaint paint;
23
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000024 paint.setDither(true);
25 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000026 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000027 canvas.drawPaint(paint);
28}
29
caryclark@google.com02939ce2012-06-06 12:09:51 +000030static SkShader* MakeBitmapShader(SkShader::TileMode tx, SkShader::TileMode ty,
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000031 int w, int h) {
32 static SkBitmap bmp;
33 if (bmp.isNull()) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000034 makebm(&bmp, w/2, h/4);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000035 }
36 return SkShader::CreateBitmapShader(bmp, tx, ty);
37}
38
39///////////////////////////////////////////////////////////////////////////////
40
41struct GradData {
42 int fCount;
43 const SkColor* fColors;
44 const SkScalar* fPos;
45};
46
47static const SkColor gColors[] = {
48 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
49};
50
51static const GradData gGradData[] = {
52 { 2, gColors, NULL },
53 { 5, gColors, NULL },
54};
55
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000056static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
57 return SkGradientShader::CreateLinear(pts, data.fColors, 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* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
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));
64 return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000065 data.fPos, data.fCount, tm);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000066}
67
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000068static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000069 SkPoint center;
70 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
71 SkScalarAve(pts[0].fY, pts[1].fY));
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000072 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000073}
74
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000075static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000076 SkPoint center0, center1;
77 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
78 SkScalarAve(pts[0].fY, pts[1].fY));
79 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
80 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
81 return SkGradientShader::CreateTwoPointRadial(
82 center1, (pts[1].fX - pts[0].fX) / 7,
83 center0, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000084 data.fColors, data.fPos, data.fCount, tm);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000085}
86
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000087typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm);
88
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000089static const GradMaker gGradMakers[] = {
90 MakeLinear, MakeRadial, MakeSweep, Make2Radial
91};
92
93///////////////////////////////////////////////////////////////////////////////
94
reed@google.comf2183392011-04-22 14:10:48 +000095class ShaderTextView : public SampleView {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000096public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000097 ShaderTextView() {
reed@google.comf2183392011-04-22 14:10:48 +000098 this->setBGColor(0xFFDDDDDD);
99 }
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000100
101protected:
102 // overrides from SkEventSink
103 virtual bool onQuery(SkEvent* evt) {
104 if (SampleCode::TitleQ(*evt)) {
105 SampleCode::TitleR(evt, "Shader Text");
106 return true;
107 }
108 return this->INHERITED::onQuery(evt);
109 }
110
reed@google.comf2183392011-04-22 14:10:48 +0000111 virtual void onDrawContent(SkCanvas* canvas) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000112 const char text[] = "Shaded Text";
113 const int textLen = SK_ARRAY_COUNT(text) - 1;
bsalomon@google.com39149582011-06-13 21:55:32 +0000114 static int pointSize = 36;
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000115
116 int w = pointSize * textLen;
117 int h = pointSize;
118
119 SkPoint pts[2] = {
120 { 0, 0 },
121 { SkIntToScalar(w), SkIntToScalar(h) }
122 };
123 SkScalar textBase = SkIntToScalar(h/2);
124
125 SkShader::TileMode tileModes[] = {
126 SkShader::kClamp_TileMode,
127 SkShader::kRepeat_TileMode,
128 SkShader::kMirror_TileMode
129 };
130
131 static const int gradCount = SK_ARRAY_COUNT(gGradData) *
132 SK_ARRAY_COUNT(gGradMakers);
133 static const int bmpCount = SK_ARRAY_COUNT(tileModes) *
134 SK_ARRAY_COUNT(tileModes);
135 SkShader* shaders[gradCount + bmpCount];
136
137 int shdIdx = 0;
138 for (size_t d = 0; d < SK_ARRAY_COUNT(gGradData); ++d) {
139 for (size_t m = 0; m < SK_ARRAY_COUNT(gGradMakers); ++m) {
140 shaders[shdIdx++] = gGradMakers[m](pts,
141 gGradData[d],
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000142 SkShader::kClamp_TileMode);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000143 }
144 }
145 for (size_t tx = 0; tx < SK_ARRAY_COUNT(tileModes); ++tx) {
146 for (size_t ty = 0; ty < SK_ARRAY_COUNT(tileModes); ++ty) {
147 shaders[shdIdx++] = MakeBitmapShader(tileModes[tx],
148 tileModes[ty],
149 w/8, h);
150 }
151 }
152
153 SkPaint paint;
154 paint.setDither(true);
155 paint.setAntiAlias(true);
156 paint.setTextSize(SkIntToScalar(pointSize));
157
158 canvas->save();
159 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
160
bsalomon@google.com39149582011-06-13 21:55:32 +0000161 SkPath path;
bsalomon@google.com137c4282011-06-14 17:13:22 +0000162 path.arcTo(SkRect::MakeXYWH(SkIntToScalar(-40), SkIntToScalar(15),
rmistry@google.comae933ce2012-08-23 18:19:56 +0000163 SkIntToScalar(300), SkIntToScalar(90)),
164 SkIntToScalar(225), SkIntToScalar(90),
bsalomon@google.com137c4282011-06-14 17:13:22 +0000165 false);
bsalomon@google.com39149582011-06-13 21:55:32 +0000166 path.close();
167
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000168 static const int testsPerCol = 8;
169 static const int rowHeight = 60;
170 static const int colWidth = 300;
171 canvas->save();
172 for (size_t s = 0; s < SK_ARRAY_COUNT(shaders); s++) {
173 canvas->save();
reed@google.com7fa2a652014-01-27 13:42:58 +0000174 size_t i = 2*s;
bsalomon@google.com39149582011-06-13 21:55:32 +0000175 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
176 SkIntToScalar((i % testsPerCol) * rowHeight));
senorblanco@chromium.orge580bc12011-05-19 18:33:33 +0000177 paint.setShader(shaders[s])->unref();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000178 canvas->drawText(text, textLen, 0, textBase, paint);
179 canvas->restore();
bsalomon@google.com39149582011-06-13 21:55:32 +0000180 canvas->save();
181 ++i;
182 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
183 SkIntToScalar((i % testsPerCol) * rowHeight));
184 canvas->drawTextOnPath(text, textLen, path, NULL, paint);
185 canvas->restore();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000186 }
187 canvas->restore();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000188 }
189
190private:
reed@google.comf2183392011-04-22 14:10:48 +0000191 typedef SampleView INHERITED;
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000192};
193
194///////////////////////////////////////////////////////////////////////////////
195
196static SkView* MyFactory() { return new ShaderTextView; }
197static SkViewRegister reg(MyFactory);