blob: 36796b924bc62c9b93923893bf32bd95ccbc81f1 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkColor.h"
12#include "include/core/SkFilterQuality.h"
13#include "include/core/SkFont.h"
14#include "include/core/SkFontTypes.h"
15#include "include/core/SkMatrix.h"
16#include "include/core/SkPaint.h"
17#include "include/core/SkPoint.h"
18#include "include/core/SkScalar.h"
19#include "include/core/SkShader.h"
20#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
22#include "include/core/SkTileMode.h"
23#include "include/core/SkTypeface.h"
24#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/effects/SkGradientShader.h"
26#include "tools/ToolUtils.h"
bsalomon@google.com42316092012-10-12 19:26:15 +000027
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include <string.h>
29
bsalomon@google.com42316092012-10-12 19:26:15 +000030namespace skiagm {
31
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000032static void makebm(SkBitmap* bm, int w, int h) {
33 bm->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000034 bm->eraseColor(SK_ColorTRANSPARENT);
bsalomon@google.com42316092012-10-12 19:26:15 +000035
36 SkCanvas canvas(*bm);
Brian Osman7f364052020-02-06 11:25:43 -050037 SkScalar s = SkIntToScalar(std::min(w, h));
mtkleindbfd7ab2016-09-01 11:24:54 -070038 const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
39 const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
40 const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
41 const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
42 const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000043
bsalomon@google.com42316092012-10-12 19:26:15 +000044
45 SkPaint paint;
46
reed1a9b9642016-03-13 14:13:58 -070047 paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos,
Mike Reedfae8fce2019-04-03 10:27:45 -040048 SK_ARRAY_COUNT(kColors0), SkTileMode::kClamp));
bsalomon@google.com42316092012-10-12 19:26:15 +000049 canvas.drawPaint(paint);
reed1a9b9642016-03-13 14:13:58 -070050 paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos,
Mike Reedfae8fce2019-04-03 10:27:45 -040051 SK_ARRAY_COUNT(kColors1), SkTileMode::kClamp));
bsalomon@google.com42316092012-10-12 19:26:15 +000052 canvas.drawPaint(paint);
53}
54
55///////////////////////////////////////////////////////////////////////////////
56
57struct LabeledMatrix {
58 SkMatrix fMatrix;
59 const char* fLabel;
60};
61
mtkleindbfd7ab2016-09-01 11:24:54 -070062constexpr int kPointSize = 300;
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000063
bsalomon@google.com42316092012-10-12 19:26:15 +000064class ShaderText3GM : public GM {
65public:
66 ShaderText3GM() {
Mike Kleind46dce32018-08-16 10:17:03 -040067 this->setBGColor(0xFFDDDDDD);
bsalomon@google.com42316092012-10-12 19:26:15 +000068 }
69
70protected:
71
mtklein36352bf2015-03-25 18:17:31 -070072 SkString onShortName() override {
bsalomon@google.com42316092012-10-12 19:26:15 +000073 return SkString("shadertext3");
74 }
75
Hal Canaryfa3305a2019-07-18 12:36:54 -040076 SkISize onISize() override { return SkISize::Make(820, 930); }
bsalomon@google.com42316092012-10-12 19:26:15 +000077
mtklein36352bf2015-03-25 18:17:31 -070078 void onOnceBeforeDraw() override {
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000079 makebm(&fBmp, kPointSize / 4, kPointSize / 4);
80 }
bsalomon@google.com42316092012-10-12 19:26:15 +000081
mtklein36352bf2015-03-25 18:17:31 -070082 void onDraw(SkCanvas* canvas) override {
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000083
bsalomon@google.com42316092012-10-12 19:26:15 +000084 SkPaint bmpPaint;
85 bmpPaint.setAntiAlias(true);
Mike Reed9407e242019-02-15 16:13:57 -050086 bmpPaint.setAlphaf(0.5f);
Mike Reed607a3822021-01-24 19:49:21 -050087 SkSamplingOptions sampling(SkFilterMode::kLinear);
88
89 canvas->drawImage(fBmp.asImage(), 5.f, 5.f, sampling, &bmpPaint);
bsalomon@google.com42316092012-10-12 19:26:15 +000090
Mike Kleinea3f0142019-03-20 11:12:10 -050091 SkFont font(ToolUtils::create_portable_typeface(), SkIntToScalar(kPointSize));
bsalomon@google.com42316092012-10-12 19:26:15 +000092 SkPaint outlinePaint;
bsalomon@google.com42316092012-10-12 19:26:15 +000093 outlinePaint.setStyle(SkPaint::kStroke_Style);
94 outlinePaint.setStrokeWidth(0.f);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000095
bsalomon@google.com42316092012-10-12 19:26:15 +000096 canvas->translate(15.f, 15.f);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000097
bsalomon@google.com42316092012-10-12 19:26:15 +000098 // draw glyphs scaled up
99 canvas->scale(2.f, 2.f);
100
Mike Reedfae8fce2019-04-03 10:27:45 -0400101 constexpr SkTileMode kTileModes[] = {
102 SkTileMode::kRepeat,
103 SkTileMode::kMirror,
bsalomon@google.com42316092012-10-12 19:26:15 +0000104 };
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000105
bsalomon@google.com42316092012-10-12 19:26:15 +0000106 // position the baseline of the first run
107 canvas->translate(0.f, 0.75f * kPointSize);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000108
bsalomon@google.com42316092012-10-12 19:26:15 +0000109 canvas->save();
110 int i = 0;
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000111 for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) {
bsalomon@google.com42316092012-10-12 19:26:15 +0000112 for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) {
bsalomon@google.com42316092012-10-12 19:26:15 +0000113 SkMatrix localM;
114 localM.setTranslate(5.f, 5.f);
115 localM.postRotate(20);
116 localM.postScale(1.15f, .85f);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000117
bsalomon@google.com42316092012-10-12 19:26:15 +0000118 SkPaint fillPaint;
119 fillPaint.setAntiAlias(true);
Mike Reed057fcbe2020-12-12 14:31:25 -0500120 fillPaint.setShader(fBmp.makeShader(kTileModes[tm0], kTileModes[tm1],
Mike Reed607a3822021-01-24 19:49:21 -0500121 sampling, localM));
bsalomon@google.com42316092012-10-12 19:26:15 +0000122
Hal Canarydf2d27e2019-01-08 09:38:02 -0500123 constexpr char kText[] = "B";
124 canvas->drawString(kText, 0, 0, font, fillPaint);
125 canvas->drawString(kText, 0, 0, font, outlinePaint);
Ben Wagner51e15a62019-05-07 15:38:46 -0400126 SkScalar w = font.measureText(kText, strlen(kText), SkTextEncoding::kUTF8);
bsalomon@google.com42316092012-10-12 19:26:15 +0000127 canvas->translate(w + 10.f, 0.f);
128 ++i;
129 if (!(i % 2)) {
130 canvas->restore();
131 canvas->translate(0, 0.75f * kPointSize);
132 canvas->save();
133 }
134 }
135 }
136 canvas->restore();
137 }
138
139private:
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +0000140 SkBitmap fBmp;
John Stiles7571f9e2020-09-02 22:42:33 -0400141 using INHERITED = GM;
bsalomon@google.com42316092012-10-12 19:26:15 +0000142};
143
144///////////////////////////////////////////////////////////////////////////////
145
Hal Canarye964c182019-01-23 10:22:01 -0500146DEF_GM( return new ShaderText3GM; )
John Stilesa6841be2020-08-06 14:11:56 -0400147} // namespace skiagm