blob: 78efe8752444800f4561cd1e1272870ab66b23c5 [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"
9#include "SkCanvas.h"
10#include "SkGradientShader.h"
bsalomon@google.com42316092012-10-12 19:26:15 +000011
12namespace skiagm {
13
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000014static void makebm(SkBitmap* bm, int w, int h) {
15 bm->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000016 bm->eraseColor(SK_ColorTRANSPARENT);
bsalomon@google.com42316092012-10-12 19:26:15 +000017
18 SkCanvas canvas(*bm);
19 SkScalar s = SkIntToScalar(SkMin32(w, h));
20 static const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
21 static const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
22 static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
23 static const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
24 static const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000025
bsalomon@google.com42316092012-10-12 19:26:15 +000026
27 SkPaint paint;
28
bsalomon@google.com42316092012-10-12 19:26:15 +000029 paint.setShader(SkGradientShader::CreateLinear(kPts0, kColors0, kPos,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000030 SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode))->unref();
bsalomon@google.com42316092012-10-12 19:26:15 +000031 canvas.drawPaint(paint);
32 paint.setShader(SkGradientShader::CreateLinear(kPts1, kColors1, kPos,
33 SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode))->unref();
34 canvas.drawPaint(paint);
35}
36
37///////////////////////////////////////////////////////////////////////////////
38
39struct LabeledMatrix {
40 SkMatrix fMatrix;
41 const char* fLabel;
42};
43
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000044static const char kText[] = "B";
45static const int kTextLen = SK_ARRAY_COUNT(kText) - 1;
46static const int kPointSize = 300;
47
bsalomon@google.com42316092012-10-12 19:26:15 +000048class ShaderText3GM : public GM {
49public:
50 ShaderText3GM() {
51 this->setBGColor(0xFFDDDDDD);
52 }
53
54protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000055 virtual uint32_t onGetFlags() const SK_OVERRIDE {
56 return kSkipTiled_Flag;
57 }
bsalomon@google.com42316092012-10-12 19:26:15 +000058
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000059 virtual SkString onShortName() SK_OVERRIDE {
bsalomon@google.com42316092012-10-12 19:26:15 +000060 return SkString("shadertext3");
61 }
62
tfarinaf5393182014-06-09 23:59:03 -070063 virtual SkISize onISize() SK_OVERRIDE{ return SkISize::Make(800, 1000); }
bsalomon@google.com42316092012-10-12 19:26:15 +000064
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000065 virtual void onOnceBeforeDraw() SK_OVERRIDE {
66 makebm(&fBmp, kPointSize / 4, kPointSize / 4);
67 }
bsalomon@google.com42316092012-10-12 19:26:15 +000068
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000069 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000070
bsalomon@google.com42316092012-10-12 19:26:15 +000071 SkPaint bmpPaint;
72 bmpPaint.setAntiAlias(true);
reed@google.com44699382013-10-31 17:28:30 +000073 bmpPaint.setFilterLevel(SkPaint::kLow_FilterLevel);
bsalomon@google.com42316092012-10-12 19:26:15 +000074 bmpPaint.setAlpha(0x80);
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000075 canvas->drawBitmap(fBmp, 5.f, 5.f, &bmpPaint);
bsalomon@google.com42316092012-10-12 19:26:15 +000076
77 SkPaint outlinePaint;
78 outlinePaint.setAntiAlias(true);
79 outlinePaint.setTextSize(SkIntToScalar(kPointSize));
80 outlinePaint.setStyle(SkPaint::kStroke_Style);
81 outlinePaint.setStrokeWidth(0.f);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000082
bsalomon@google.com42316092012-10-12 19:26:15 +000083 canvas->translate(15.f, 15.f);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000084
bsalomon@google.com42316092012-10-12 19:26:15 +000085 // draw glyphs scaled up
86 canvas->scale(2.f, 2.f);
87
88 static const SkShader::TileMode kTileModes[] = {
89 SkShader::kRepeat_TileMode,
90 SkShader::kMirror_TileMode,
91 };
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000092
bsalomon@google.com42316092012-10-12 19:26:15 +000093 // position the baseline of the first run
94 canvas->translate(0.f, 0.75f * kPointSize);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000095
bsalomon@google.com42316092012-10-12 19:26:15 +000096 canvas->save();
97 int i = 0;
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000098 for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) {
bsalomon@google.com42316092012-10-12 19:26:15 +000099 for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) {
bsalomon@google.com42316092012-10-12 19:26:15 +0000100 SkMatrix localM;
101 localM.setTranslate(5.f, 5.f);
102 localM.postRotate(20);
103 localM.postScale(1.15f, .85f);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000104
105 SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(fBmp,
106 kTileModes[tm0],
107 kTileModes[tm1],
108 &localM));
bsalomon@google.com42316092012-10-12 19:26:15 +0000109
110 SkPaint fillPaint;
111 fillPaint.setAntiAlias(true);
112 fillPaint.setTextSize(SkIntToScalar(kPointSize));
reed@google.com44699382013-10-31 17:28:30 +0000113 fillPaint.setFilterLevel(SkPaint::kLow_FilterLevel);
bsalomon@google.com42316092012-10-12 19:26:15 +0000114 fillPaint.setShader(shader);
115
116 canvas->drawText(kText, kTextLen, 0, 0, fillPaint);
117 canvas->drawText(kText, kTextLen, 0, 0, outlinePaint);
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000118 SkScalar w = fillPaint.measureText(kText, kTextLen);
bsalomon@google.com42316092012-10-12 19:26:15 +0000119 canvas->translate(w + 10.f, 0.f);
120 ++i;
121 if (!(i % 2)) {
122 canvas->restore();
123 canvas->translate(0, 0.75f * kPointSize);
124 canvas->save();
125 }
126 }
127 }
128 canvas->restore();
129 }
130
131private:
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +0000132 SkBitmap fBmp;
bsalomon@google.com42316092012-10-12 19:26:15 +0000133 typedef GM INHERITED;
134};
135
136///////////////////////////////////////////////////////////////////////////////
137
bsalomon@google.com42316092012-10-12 19:26:15 +0000138static GM* MyFactory(void*) { return new ShaderText3GM; }
139static GMRegistry reg(MyFactory);
bsalomon@google.com42316092012-10-12 19:26:15 +0000140}