blob: 36abf9f8e58ae1784acd4068c9e4fca9f3776514 [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"
10#include "SkUnitMappers.h"
11
12namespace skiagm {
13
14static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) {
15 bm->setConfig(config, w, h);
16 bm->allocPixels();
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));
21 static const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
22 static const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
23 static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
24 static const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
25 static 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
30 SkUnitMapper* um = NULL;
31
32 um = new SkCosineMapper;
33
34 SkAutoUnref au(um);
35
36 paint.setShader(SkGradientShader::CreateLinear(kPts0, kColors0, kPos,
37 SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode, um))->unref();
38 canvas.drawPaint(paint);
39 paint.setShader(SkGradientShader::CreateLinear(kPts1, kColors1, kPos,
40 SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode))->unref();
41 canvas.drawPaint(paint);
42}
43
44///////////////////////////////////////////////////////////////////////////////
45
46struct LabeledMatrix {
47 SkMatrix fMatrix;
48 const char* fLabel;
49};
50
51class ShaderText3GM : public GM {
52public:
53 ShaderText3GM() {
54 this->setBGColor(0xFFDDDDDD);
55 }
56
57protected:
58
59 SkString onShortName() {
60 return SkString("shadertext3");
61 }
62
63 SkISize onISize() { return make_isize(800, 1000); }
64
65 virtual void onDraw(SkCanvas* canvas) {
66 static const char kText[] = "B";
67 static const int kTextLen = SK_ARRAY_COUNT(kText) - 1;
68 static const int kPointSize = 300;
69
70 static SkBitmap bmp;
71 if (bmp.isNull()) {
72 makebm(&bmp, SkBitmap::kARGB_8888_Config, kPointSize / 4, kPointSize / 4);
73 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000074
bsalomon@google.com42316092012-10-12 19:26:15 +000075 SkPaint bmpPaint;
76 bmpPaint.setAntiAlias(true);
reed@google.com44699382013-10-31 17:28:30 +000077 bmpPaint.setFilterLevel(SkPaint::kLow_FilterLevel);
bsalomon@google.com42316092012-10-12 19:26:15 +000078 bmpPaint.setAlpha(0x80);
79 canvas->drawBitmap(bmp, 5.f, 5.f, &bmpPaint);
80
81 SkPaint outlinePaint;
82 outlinePaint.setAntiAlias(true);
83 outlinePaint.setTextSize(SkIntToScalar(kPointSize));
84 outlinePaint.setStyle(SkPaint::kStroke_Style);
85 outlinePaint.setStrokeWidth(0.f);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000086
bsalomon@google.com42316092012-10-12 19:26:15 +000087 canvas->translate(15.f, 15.f);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000088
bsalomon@google.com42316092012-10-12 19:26:15 +000089 // draw glyphs scaled up
90 canvas->scale(2.f, 2.f);
91
92 static const SkShader::TileMode kTileModes[] = {
93 SkShader::kRepeat_TileMode,
94 SkShader::kMirror_TileMode,
95 };
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000096
bsalomon@google.com42316092012-10-12 19:26:15 +000097 // position the baseline of the first run
98 canvas->translate(0.f, 0.75f * kPointSize);
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +000099
bsalomon@google.com42316092012-10-12 19:26:15 +0000100 canvas->save();
101 int i = 0;
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000102 for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) {
bsalomon@google.com42316092012-10-12 19:26:15 +0000103 for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) {
104 SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(bmp,
105 kTileModes[tm0],
106 kTileModes[tm1]));
107 SkMatrix localM;
108 localM.setTranslate(5.f, 5.f);
109 localM.postRotate(20);
110 localM.postScale(1.15f, .85f);
111 shader->setLocalMatrix(localM);
112
113 SkPaint fillPaint;
114 fillPaint.setAntiAlias(true);
115 fillPaint.setTextSize(SkIntToScalar(kPointSize));
reed@google.com44699382013-10-31 17:28:30 +0000116 fillPaint.setFilterLevel(SkPaint::kLow_FilterLevel);
bsalomon@google.com42316092012-10-12 19:26:15 +0000117 fillPaint.setShader(shader);
118
119 canvas->drawText(kText, kTextLen, 0, 0, fillPaint);
120 canvas->drawText(kText, kTextLen, 0, 0, outlinePaint);
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000121 SkScalar w = fillPaint.measureText(kText, kTextLen);
bsalomon@google.com42316092012-10-12 19:26:15 +0000122 canvas->translate(w + 10.f, 0.f);
123 ++i;
124 if (!(i % 2)) {
125 canvas->restore();
126 canvas->translate(0, 0.75f * kPointSize);
127 canvas->save();
128 }
129 }
130 }
131 canvas->restore();
132 }
133
134private:
135 typedef GM INHERITED;
136};
137
138///////////////////////////////////////////////////////////////////////////////
139
140#ifndef SK_BUILD_FOR_ANDROID
141static GM* MyFactory(void*) { return new ShaderText3GM; }
142static GMRegistry reg(MyFactory);
143#endif
144}