blob: 150e205af89bf273fa498cd92886041c5d61dcf6 [file] [log] [blame]
bsalomon@google.com42316092012-10-12 19:26:15 +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.com42316092012-10-12 19:26:15 +00008#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
bsalomon@google.com42316092012-10-12 19:26:15 +000010#include "SkCanvas.h"
11#include "SkGradientShader.h"
bsalomon@google.com42316092012-10-12 19:26:15 +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.com42316092012-10-12 19:26:15 +000018
19 SkCanvas canvas(*bm);
20 SkScalar s = SkIntToScalar(SkMin32(w, h));
mtkleindbfd7ab2016-09-01 11:24:54 -070021 const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
22 const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
23 const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
24 const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
25 const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000026
bsalomon@google.com42316092012-10-12 19:26:15 +000027
28 SkPaint paint;
29
reed1a9b9642016-03-13 14:13:58 -070030 paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos,
31 SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode));
bsalomon@google.com42316092012-10-12 19:26:15 +000032 canvas.drawPaint(paint);
reed1a9b9642016-03-13 14:13:58 -070033 paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos,
34 SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode));
bsalomon@google.com42316092012-10-12 19:26:15 +000035 canvas.drawPaint(paint);
36}
37
38///////////////////////////////////////////////////////////////////////////////
39
40struct LabeledMatrix {
41 SkMatrix fMatrix;
42 const char* fLabel;
43};
44
mtkleindbfd7ab2016-09-01 11:24:54 -070045constexpr int kPointSize = 300;
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000046
bsalomon@google.com42316092012-10-12 19:26:15 +000047class ShaderText3GM : public GM {
48public:
49 ShaderText3GM() {
Mike Kleind46dce32018-08-16 10:17:03 -040050 this->setBGColor(0xFFDDDDDD);
bsalomon@google.com42316092012-10-12 19:26:15 +000051 }
52
53protected:
54
mtklein36352bf2015-03-25 18:17:31 -070055 SkString onShortName() override {
bsalomon@google.com42316092012-10-12 19:26:15 +000056 return SkString("shadertext3");
57 }
58
reed6dc14aa2016-04-11 07:46:38 -070059 SkISize onISize() override{ return SkISize::Make(820, 930); }
bsalomon@google.com42316092012-10-12 19:26:15 +000060
mtklein36352bf2015-03-25 18:17:31 -070061 void onOnceBeforeDraw() override {
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000062 makebm(&fBmp, kPointSize / 4, kPointSize / 4);
63 }
bsalomon@google.com42316092012-10-12 19:26:15 +000064
mtklein36352bf2015-03-25 18:17:31 -070065 void onDraw(SkCanvas* canvas) override {
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000066
bsalomon@google.com42316092012-10-12 19:26:15 +000067 SkPaint bmpPaint;
68 bmpPaint.setAntiAlias(true);
reed93a12152015-03-16 10:08:34 -070069 bmpPaint.setFilterQuality(kLow_SkFilterQuality);
bsalomon@google.com42316092012-10-12 19:26:15 +000070 bmpPaint.setAlpha(0x80);
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000071 canvas->drawBitmap(fBmp, 5.f, 5.f, &bmpPaint);
bsalomon@google.com42316092012-10-12 19:26:15 +000072
Hal Canarydf2d27e2019-01-08 09:38:02 -050073 SkFont font(sk_tool_utils::create_portable_typeface(), SkIntToScalar(kPointSize));
bsalomon@google.com42316092012-10-12 19:26:15 +000074 SkPaint outlinePaint;
bsalomon@google.com42316092012-10-12 19:26:15 +000075 outlinePaint.setStyle(SkPaint::kStroke_Style);
76 outlinePaint.setStrokeWidth(0.f);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000077
bsalomon@google.com42316092012-10-12 19:26:15 +000078 canvas->translate(15.f, 15.f);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000079
bsalomon@google.com42316092012-10-12 19:26:15 +000080 // draw glyphs scaled up
81 canvas->scale(2.f, 2.f);
82
mtkleindbfd7ab2016-09-01 11:24:54 -070083 constexpr SkShader::TileMode kTileModes[] = {
bsalomon@google.com42316092012-10-12 19:26:15 +000084 SkShader::kRepeat_TileMode,
85 SkShader::kMirror_TileMode,
86 };
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000087
bsalomon@google.com42316092012-10-12 19:26:15 +000088 // position the baseline of the first run
89 canvas->translate(0.f, 0.75f * kPointSize);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000090
bsalomon@google.com42316092012-10-12 19:26:15 +000091 canvas->save();
92 int i = 0;
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000093 for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) {
bsalomon@google.com42316092012-10-12 19:26:15 +000094 for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) {
bsalomon@google.com42316092012-10-12 19:26:15 +000095 SkMatrix localM;
96 localM.setTranslate(5.f, 5.f);
97 localM.postRotate(20);
98 localM.postScale(1.15f, .85f);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000099
bsalomon@google.com42316092012-10-12 19:26:15 +0000100 SkPaint fillPaint;
101 fillPaint.setAntiAlias(true);
reed93a12152015-03-16 10:08:34 -0700102 fillPaint.setFilterQuality(kLow_SkFilterQuality);
reed1a9b9642016-03-13 14:13:58 -0700103 fillPaint.setShader(SkShader::MakeBitmapShader(fBmp, kTileModes[tm0],
104 kTileModes[tm1], &localM));
bsalomon@google.com42316092012-10-12 19:26:15 +0000105
Hal Canarydf2d27e2019-01-08 09:38:02 -0500106 constexpr char kText[] = "B";
107 canvas->drawString(kText, 0, 0, font, fillPaint);
108 canvas->drawString(kText, 0, 0, font, outlinePaint);
109 SkScalar w = font.measureText(kText, strlen(kText), kUTF8_SkTextEncoding);
bsalomon@google.com42316092012-10-12 19:26:15 +0000110 canvas->translate(w + 10.f, 0.f);
111 ++i;
112 if (!(i % 2)) {
113 canvas->restore();
114 canvas->translate(0, 0.75f * kPointSize);
115 canvas->save();
116 }
117 }
118 }
119 canvas->restore();
120 }
121
122private:
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +0000123 SkBitmap fBmp;
bsalomon@google.com42316092012-10-12 19:26:15 +0000124 typedef GM INHERITED;
125};
126
127///////////////////////////////////////////////////////////////////////////////
128
Hal Canarye964c182019-01-23 10:22:01 -0500129DEF_GM( return new ShaderText3GM; )
bsalomon@google.com42316092012-10-12 19:26:15 +0000130}