blob: 237a1496ff18790d01b14148f210f27b8c8b7a40 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000010#include "SkCanvas.h"
11#include "SkGradientShader.h"
bungemand3ebb482015-08-05 13:57:49 -070012#include "SkPath.h"
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000013
14namespace skiagm {
15
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000016static void makebm(SkBitmap* bm, int w, int h) {
17 bm->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000018 bm->eraseColor(SK_ColorTRANSPARENT);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000019
20 SkCanvas canvas(*bm);
epoger@google.comb28b5e42011-05-24 14:51:57 +000021 SkScalar s = SkIntToScalar(SkMin32(w, h));
reed@google.com38690072011-01-26 01:44:18 +000022 SkPoint pts[] = { { 0, 0 }, { s, s } };
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000023 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
24 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
25 SkPaint paint;
26
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000027 paint.setDither(true);
reed1a9b9642016-03-13 14:13:58 -070028 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, SK_ARRAY_COUNT(colors),
29 SkShader::kClamp_TileMode));
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000030 canvas.drawPaint(paint);
31}
32
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000033///////////////////////////////////////////////////////////////////////////////
34
35struct GradData {
36 int fCount;
37 const SkColor* fColors;
38 const SkScalar* fPos;
39};
40
mtkleindbfd7ab2016-09-01 11:24:54 -070041constexpr SkColor gColors[] = {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000042 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
43};
44
mtkleindbfd7ab2016-09-01 11:24:54 -070045constexpr GradData gGradData[] = {
halcanary96fcdcc2015-08-27 07:41:13 -070046 { 2, gColors, nullptr },
47 { 5, gColors, nullptr },
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000048};
49
reed1a9b9642016-03-13 14:13:58 -070050static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
51 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000052}
53
reed1a9b9642016-03-13 14:13:58 -070054static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000055 SkPoint center;
56 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
57 SkScalarAve(pts[0].fY, pts[1].fY));
reed1a9b9642016-03-13 14:13:58 -070058 return SkGradientShader::MakeRadial(center, center.fX, data.fColors, data.fPos, data.fCount,
59 tm);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000060}
61
reed1a9b9642016-03-13 14:13:58 -070062static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000063 SkPoint center;
64 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
65 SkScalarAve(pts[0].fY, pts[1].fY));
reed1a9b9642016-03-13 14:13:58 -070066 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000067}
68
reed1a9b9642016-03-13 14:13:58 -070069static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000070 SkPoint center0, center1;
71 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
72 SkScalarAve(pts[0].fY, pts[1].fY));
73 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
74 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed1a9b9642016-03-13 14:13:58 -070075 return SkGradientShader::MakeTwoPointConical(
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000076 center1, (pts[1].fX - pts[0].fX) / 7,
77 center0, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000078 data.fColors, data.fPos, data.fCount, tm);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000079}
80
reed1a9b9642016-03-13 14:13:58 -070081typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm);
skia.committer@gmail.com7693dbf2014-05-23 03:03:34 +000082
mtkleindbfd7ab2016-09-01 11:24:54 -070083constexpr GradMaker gGradMakers[] = {
reed71a6cbf2015-05-04 08:32:51 -070084 MakeLinear, MakeRadial, MakeSweep, Make2Conical
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000085};
86
87///////////////////////////////////////////////////////////////////////////////
88
89class ShaderTextGM : public GM {
90public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000091 ShaderTextGM() {
caryclark65cdba62015-06-15 06:51:08 -070092 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000093 }
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000094
95protected:
96
mtklein36352bf2015-03-25 18:17:31 -070097 SkString onShortName() override {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +000098 return SkString("shadertext");
99 }
100
mtklein36352bf2015-03-25 18:17:31 -0700101 SkISize onISize() override { return SkISize::Make(1450, 500); }
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000102
mtklein36352bf2015-03-25 18:17:31 -0700103 void onDraw(SkCanvas* canvas) override {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000104 const char text[] = "Shaded Text";
105 const int textLen = SK_ARRAY_COUNT(text) - 1;
bsalomon@google.com39149582011-06-13 21:55:32 +0000106 const int pointSize = 36;
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000107
reedda8cf5a2014-09-04 13:18:36 -0700108 const int w = pointSize * textLen;
109 const int h = pointSize;
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000110
111 SkPoint pts[2] = {
112 { 0, 0 },
113 { SkIntToScalar(w), SkIntToScalar(h) }
114 };
115 SkScalar textBase = SkIntToScalar(h/2);
116
117 SkShader::TileMode tileModes[] = {
118 SkShader::kClamp_TileMode,
119 SkShader::kRepeat_TileMode,
120 SkShader::kMirror_TileMode
121 };
122
mtkleindbfd7ab2016-09-01 11:24:54 -0700123 constexpr int gradCount = SK_ARRAY_COUNT(gGradData) *
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000124 SK_ARRAY_COUNT(gGradMakers);
mtkleindbfd7ab2016-09-01 11:24:54 -0700125 constexpr int bmpCount = SK_ARRAY_COUNT(tileModes) *
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000126 SK_ARRAY_COUNT(tileModes);
reed1a9b9642016-03-13 14:13:58 -0700127 sk_sp<SkShader> shaders[gradCount + bmpCount];
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000128
129 int shdIdx = 0;
130 for (size_t d = 0; d < SK_ARRAY_COUNT(gGradData); ++d) {
131 for (size_t m = 0; m < SK_ARRAY_COUNT(gGradMakers); ++m) {
132 shaders[shdIdx++] = gGradMakers[m](pts,
133 gGradData[d],
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000134 SkShader::kClamp_TileMode);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000135 }
136 }
halcanary9d524f22016-03-29 09:03:52 -0700137
reedda8cf5a2014-09-04 13:18:36 -0700138 SkBitmap bm;
139 makebm(&bm, w/16, h/4);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000140 for (size_t tx = 0; tx < SK_ARRAY_COUNT(tileModes); ++tx) {
141 for (size_t ty = 0; ty < SK_ARRAY_COUNT(tileModes); ++ty) {
reed1a9b9642016-03-13 14:13:58 -0700142 shaders[shdIdx++] = SkShader::MakeBitmapShader(bm, tileModes[tx], tileModes[ty]);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000143 }
144 }
145
146 SkPaint paint;
147 paint.setDither(true);
148 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700149 sk_tool_utils::set_portable_typeface(&paint);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000150 paint.setTextSize(SkIntToScalar(pointSize));
151
152 canvas->save();
153 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
154
bsalomon@google.com39149582011-06-13 21:55:32 +0000155 SkPath path;
bsalomon@google.com137c4282011-06-14 17:13:22 +0000156 path.arcTo(SkRect::MakeXYWH(SkIntToScalar(-40), SkIntToScalar(15),
rmistry@google.comae933ce2012-08-23 18:19:56 +0000157 SkIntToScalar(300), SkIntToScalar(90)),
158 SkIntToScalar(225), SkIntToScalar(90),
bsalomon@google.com137c4282011-06-14 17:13:22 +0000159 false);
bsalomon@google.com39149582011-06-13 21:55:32 +0000160 path.close();
161
mtkleindbfd7ab2016-09-01 11:24:54 -0700162 constexpr int testsPerCol = 8;
163 constexpr int rowHeight = 60;
164 constexpr int colWidth = 300;
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000165 canvas->save();
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000166 for (int s = 0; s < static_cast<int>(SK_ARRAY_COUNT(shaders)); s++) {
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000167 canvas->save();
bsalomon@google.com39149582011-06-13 21:55:32 +0000168 int i = 2*s;
169 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
170 SkIntToScalar((i % testsPerCol) * rowHeight));
reed1a9b9642016-03-13 14:13:58 -0700171 paint.setShader(shaders[s]);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000172 canvas->drawText(text, textLen, 0, textBase, paint);
173 canvas->restore();
bsalomon@google.com39149582011-06-13 21:55:32 +0000174 canvas->save();
175 ++i;
176 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
177 SkIntToScalar((i % testsPerCol) * rowHeight));
halcanary96fcdcc2015-08-27 07:41:13 -0700178 canvas->drawTextOnPath(text, textLen, path, nullptr, paint);
bsalomon@google.com39149582011-06-13 21:55:32 +0000179 canvas->restore();
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000180 }
181 canvas->restore();
bsalomon@google.com39149582011-06-13 21:55:32 +0000182
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000183 }
184
185private:
186 typedef GM INHERITED;
187};
188
189///////////////////////////////////////////////////////////////////////////////
190
191static GM* MyFactory(void*) { return new ShaderTextGM; }
192static GMRegistry reg(MyFactory);
bsalomon@google.com0fdaa222011-01-25 18:35:26 +0000193}