blob: 8aa6e6965c432685eb2bbdb8387b17d831d00ab5 [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 */
7#include "gm.h"
8#include "SkCanvas.h"
9#include "SkGradientShader.h"
commit-bot@chromium.org3339ac52014-05-22 02:55:59 +000010#include "SkUnitMappers.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
commit-bot@chromium.org3339ac52014-05-22 02:55:59 +000029 SkUnitMapper* um = NULL;
30
31 um = new SkCosineMapper;
32
33 SkAutoUnref au(um);
34
bsalomon@google.com42316092012-10-12 19:26:15 +000035 paint.setShader(SkGradientShader::CreateLinear(kPts0, kColors0, kPos,
commit-bot@chromium.org3339ac52014-05-22 02:55:59 +000036 SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode, um))->unref();
bsalomon@google.com42316092012-10-12 19:26:15 +000037 canvas.drawPaint(paint);
38 paint.setShader(SkGradientShader::CreateLinear(kPts1, kColors1, kPos,
39 SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode))->unref();
40 canvas.drawPaint(paint);
41}
42
43///////////////////////////////////////////////////////////////////////////////
44
45struct LabeledMatrix {
46 SkMatrix fMatrix;
47 const char* fLabel;
48};
49
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000050static const char kText[] = "B";
51static const int kTextLen = SK_ARRAY_COUNT(kText) - 1;
52static const int kPointSize = 300;
53
bsalomon@google.com42316092012-10-12 19:26:15 +000054class ShaderText3GM : public GM {
55public:
56 ShaderText3GM() {
57 this->setBGColor(0xFFDDDDDD);
58 }
59
60protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000061 virtual uint32_t onGetFlags() const SK_OVERRIDE {
62 return kSkipTiled_Flag;
63 }
bsalomon@google.com42316092012-10-12 19:26:15 +000064
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000065 virtual SkString onShortName() SK_OVERRIDE {
bsalomon@google.com42316092012-10-12 19:26:15 +000066 return SkString("shadertext3");
67 }
68
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000069 virtual SkISize onISize() SK_OVERRIDE{ return make_isize(800, 1000); }
bsalomon@google.com42316092012-10-12 19:26:15 +000070
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000071 virtual void onOnceBeforeDraw() SK_OVERRIDE {
72 makebm(&fBmp, kPointSize / 4, kPointSize / 4);
73 }
bsalomon@google.com42316092012-10-12 19:26:15 +000074
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000075 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000076
bsalomon@google.com42316092012-10-12 19:26:15 +000077 SkPaint bmpPaint;
78 bmpPaint.setAntiAlias(true);
reed@google.com44699382013-10-31 17:28:30 +000079 bmpPaint.setFilterLevel(SkPaint::kLow_FilterLevel);
bsalomon@google.com42316092012-10-12 19:26:15 +000080 bmpPaint.setAlpha(0x80);
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +000081 canvas->drawBitmap(fBmp, 5.f, 5.f, &bmpPaint);
bsalomon@google.com42316092012-10-12 19:26:15 +000082
83 SkPaint outlinePaint;
84 outlinePaint.setAntiAlias(true);
85 outlinePaint.setTextSize(SkIntToScalar(kPointSize));
86 outlinePaint.setStyle(SkPaint::kStroke_Style);
87 outlinePaint.setStrokeWidth(0.f);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000088
bsalomon@google.com42316092012-10-12 19:26:15 +000089 canvas->translate(15.f, 15.f);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000090
bsalomon@google.com42316092012-10-12 19:26:15 +000091 // draw glyphs scaled up
92 canvas->scale(2.f, 2.f);
93
94 static const SkShader::TileMode kTileModes[] = {
95 SkShader::kRepeat_TileMode,
96 SkShader::kMirror_TileMode,
97 };
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000098
bsalomon@google.com42316092012-10-12 19:26:15 +000099 // position the baseline of the first run
100 canvas->translate(0.f, 0.75f * kPointSize);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000101
bsalomon@google.com42316092012-10-12 19:26:15 +0000102 canvas->save();
103 int i = 0;
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000104 for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) {
bsalomon@google.com42316092012-10-12 19:26:15 +0000105 for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) {
bsalomon@google.com42316092012-10-12 19:26:15 +0000106 SkMatrix localM;
107 localM.setTranslate(5.f, 5.f);
108 localM.postRotate(20);
109 localM.postScale(1.15f, .85f);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000110
111 SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(fBmp,
112 kTileModes[tm0],
113 kTileModes[tm1],
114 &localM));
bsalomon@google.com42316092012-10-12 19:26:15 +0000115
116 SkPaint fillPaint;
117 fillPaint.setAntiAlias(true);
118 fillPaint.setTextSize(SkIntToScalar(kPointSize));
reed@google.com44699382013-10-31 17:28:30 +0000119 fillPaint.setFilterLevel(SkPaint::kLow_FilterLevel);
bsalomon@google.com42316092012-10-12 19:26:15 +0000120 fillPaint.setShader(shader);
121
122 canvas->drawText(kText, kTextLen, 0, 0, fillPaint);
123 canvas->drawText(kText, kTextLen, 0, 0, outlinePaint);
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000124 SkScalar w = fillPaint.measureText(kText, kTextLen);
bsalomon@google.com42316092012-10-12 19:26:15 +0000125 canvas->translate(w + 10.f, 0.f);
126 ++i;
127 if (!(i % 2)) {
128 canvas->restore();
129 canvas->translate(0, 0.75f * kPointSize);
130 canvas->save();
131 }
132 }
133 }
134 canvas->restore();
135 }
136
137private:
commit-bot@chromium.orga54abdc2014-03-26 21:21:40 +0000138 SkBitmap fBmp;
bsalomon@google.com42316092012-10-12 19:26:15 +0000139 typedef GM INHERITED;
140};
141
142///////////////////////////////////////////////////////////////////////////////
143
bsalomon@google.com42316092012-10-12 19:26:15 +0000144static GM* MyFactory(void*) { return new ShaderText3GM; }
145static GMRegistry reg(MyFactory);
bsalomon@google.com42316092012-10-12 19:26:15 +0000146}