blob: 3fba8cfc68488552bfb50d4590e24a65d2741d13 [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);
37 SkScalar s = SkIntToScalar(SkMin32(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);
reed93a12152015-03-16 10:08:34 -070086 bmpPaint.setFilterQuality(kLow_SkFilterQuality);
Mike Reed9407e242019-02-15 16:13:57 -050087 bmpPaint.setAlphaf(0.5f);
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000088 canvas->drawBitmap(fBmp, 5.f, 5.f, &bmpPaint);
bsalomon@google.com42316092012-10-12 19:26:15 +000089
Mike Kleinea3f0142019-03-20 11:12:10 -050090 SkFont font(ToolUtils::create_portable_typeface(), SkIntToScalar(kPointSize));
bsalomon@google.com42316092012-10-12 19:26:15 +000091 SkPaint outlinePaint;
bsalomon@google.com42316092012-10-12 19:26:15 +000092 outlinePaint.setStyle(SkPaint::kStroke_Style);
93 outlinePaint.setStrokeWidth(0.f);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000094
bsalomon@google.com42316092012-10-12 19:26:15 +000095 canvas->translate(15.f, 15.f);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000096
bsalomon@google.com42316092012-10-12 19:26:15 +000097 // draw glyphs scaled up
98 canvas->scale(2.f, 2.f);
99
Mike Reedfae8fce2019-04-03 10:27:45 -0400100 constexpr SkTileMode kTileModes[] = {
101 SkTileMode::kRepeat,
102 SkTileMode::kMirror,
bsalomon@google.com42316092012-10-12 19:26:15 +0000103 };
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000104
bsalomon@google.com42316092012-10-12 19:26:15 +0000105 // position the baseline of the first run
106 canvas->translate(0.f, 0.75f * kPointSize);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000107
bsalomon@google.com42316092012-10-12 19:26:15 +0000108 canvas->save();
109 int i = 0;
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000110 for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) {
bsalomon@google.com42316092012-10-12 19:26:15 +0000111 for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) {
bsalomon@google.com42316092012-10-12 19:26:15 +0000112 SkMatrix localM;
113 localM.setTranslate(5.f, 5.f);
114 localM.postRotate(20);
115 localM.postScale(1.15f, .85f);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000116
bsalomon@google.com42316092012-10-12 19:26:15 +0000117 SkPaint fillPaint;
118 fillPaint.setAntiAlias(true);
reed93a12152015-03-16 10:08:34 -0700119 fillPaint.setFilterQuality(kLow_SkFilterQuality);
Mike Reed50acf8f2019-04-08 13:20:23 -0400120 fillPaint.setShader(fBmp.makeShader(kTileModes[tm0], kTileModes[tm1], &localM));
bsalomon@google.com42316092012-10-12 19:26:15 +0000121
Hal Canarydf2d27e2019-01-08 09:38:02 -0500122 constexpr char kText[] = "B";
123 canvas->drawString(kText, 0, 0, font, fillPaint);
124 canvas->drawString(kText, 0, 0, font, outlinePaint);
Ben Wagner51e15a62019-05-07 15:38:46 -0400125 SkScalar w = font.measureText(kText, strlen(kText), SkTextEncoding::kUTF8);
bsalomon@google.com42316092012-10-12 19:26:15 +0000126 canvas->translate(w + 10.f, 0.f);
127 ++i;
128 if (!(i % 2)) {
129 canvas->restore();
130 canvas->translate(0, 0.75f * kPointSize);
131 canvas->save();
132 }
133 }
134 }
135 canvas->restore();
136 }
137
138private:
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +0000139 SkBitmap fBmp;
bsalomon@google.com42316092012-10-12 19:26:15 +0000140 typedef GM INHERITED;
141};
142
143///////////////////////////////////////////////////////////////////////////////
144
Hal Canarye964c182019-01-23 10:22:01 -0500145DEF_GM( return new ShaderText3GM; )
bsalomon@google.com42316092012-10-12 19:26:15 +0000146}