blob: 1e6ae5ca2cbc52a3945a339e69626ce9e0cf2f9d [file] [log] [blame]
bsalomon@google.com41fe45b2012-10-10 13:35:23 +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
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.com41fe45b2012-10-10 13:35:23 +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, 0 }, { 0, s } };
22 static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
23 static const SkColor kColors0[] = {0x40FF00FF, 0xF0FFFF00, 0x4000FFFF };
24 static const SkColor kColors1[] = {0xF0FF00FF, 0x80FFFF00, 0xF000FFFF };
skia.committer@gmail.comfc843592012-10-11 02:01:14 +000025
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000026
27 SkPaint paint;
28
29 SkUnitMapper* um = NULL;
30
31 um = new SkCosineMapper;
32
33 SkAutoUnref au(um);
34
35 paint.setShader(SkGradientShader::CreateLinear(kPts0, kColors0, kPos,
36 SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode, um))->unref();
37 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
50class ShaderText2GM : public GM {
51public:
52 ShaderText2GM() {
53 this->setBGColor(0xFFDDDDDD);
54 }
55
56protected:
57
58 SkString onShortName() {
59 return SkString("shadertext2");
60 }
61
62 SkISize onISize() { return make_isize(1800, 900); }
63
64 virtual void onDraw(SkCanvas* canvas) {
65 static const char kText[] = "SKIA";
66 static const int kTextLen = SK_ARRAY_COUNT(kText) - 1;
67 static const int kPointSize = 55;
68
69 SkTDArray<LabeledMatrix> matrices;
70 matrices.append()->fMatrix.reset();
71 matrices.top().fLabel = "Identity";
72 matrices.append()->fMatrix.setScale(1.2f, 0.8f);
73 matrices.top().fLabel = "Scale";
74 matrices.append()->fMatrix.setRotate(10.f);
75 matrices.top().fLabel = "Rotate";
76 matrices.append()->fMatrix.reset();
77 matrices.top().fMatrix.setPerspX(-0.0015f);
78 matrices.top().fMatrix.setPerspY(+0.0015f);
79 matrices.top().fLabel = "Persp";
80
81 SkTDArray<LabeledMatrix> localMatrices;
82 localMatrices.append()->fMatrix.reset();
83 localMatrices.top().fLabel = "Identity";
84 localMatrices.append()->fMatrix.setScale(2.5f, 0.2f);
85 localMatrices.top().fLabel = "Scale";
86 localMatrices.append()->fMatrix.setRotate(45.f);
87 localMatrices.top().fLabel = "Rotate";
88 localMatrices.append()->fMatrix.reset();
89 localMatrices.top().fMatrix.setPerspX(-0.007f);
90 localMatrices.top().fMatrix.setPerspY(+0.008f);
91 localMatrices.top().fLabel = "Persp";
92
93 static SkBitmap bmp;
94 if (bmp.isNull()) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000095 makebm(&bmp, kPointSize / 2, kPointSize / 2);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000096 }
skia.committer@gmail.comfc843592012-10-11 02:01:14 +000097
bsalomon@google.com41fe45b2012-10-10 13:35:23 +000098 SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(bmp,
99 SkShader::kMirror_TileMode,
100 SkShader::kRepeat_TileMode));
101 SkPaint fillPaint;
102 fillPaint.setAntiAlias(true);
103 fillPaint.setTextSize(SkIntToScalar(kPointSize));
reed@google.com44699382013-10-31 17:28:30 +0000104 fillPaint.setFilterLevel(SkPaint::kLow_FilterLevel);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000105 fillPaint.setShader(shader);
106
107 SkPaint outlinePaint;
108 outlinePaint.setAntiAlias(true);
109 outlinePaint.setTextSize(SkIntToScalar(kPointSize));
110 outlinePaint.setStyle(SkPaint::kStroke_Style);
111 outlinePaint.setStrokeWidth(0.f);
112
113 SkScalar w = fillPaint.measureText(kText, kTextLen);
114 static SkScalar kPadY = 0.5f * kPointSize;
115 static SkScalar kPadX = 1.5f * kPointSize;
116
117 SkPaint strokePaint(fillPaint);
118 strokePaint.setStyle(SkPaint::kStroke_Style);
119 strokePaint.setStrokeWidth(kPointSize * 0.1f);
120
121 SkPaint labelPaint;
122 labelPaint.setColor(0xff000000);
123 labelPaint.setAntiAlias(true);
124 labelPaint.setTextSize(12.f);
125
126 canvas->translate(15.f, 15.f);
127 canvas->drawBitmap(bmp, 0, 0);
128 canvas->translate(0, bmp.height() + labelPaint.getTextSize() + 15.f);
129
130 static const char kLabelLabel[] = "localM / canvasM";
131 canvas->drawText(kLabelLabel, strlen(kLabelLabel), 0, 0, labelPaint);
132 canvas->translate(0, 15.f);
133
134 canvas->save();
135 SkScalar maxLabelW = 0;
136 canvas->translate(0, kPadY / 2 + kPointSize);
137 for (int lm = 0; lm < localMatrices.count(); ++lm) {
138 canvas->drawText(matrices[lm].fLabel, strlen(matrices[lm].fLabel),
139 0, labelPaint.getTextSize() - 1, labelPaint);
140 SkScalar labelW = labelPaint.measureText(matrices[lm].fLabel,
141 strlen(matrices[lm].fLabel));
142 maxLabelW = SkMaxScalar(maxLabelW, labelW);
143 canvas->translate(0.f, 2 * kPointSize + 2.5f * kPadY);
144 }
145 canvas->restore();
146
147 canvas->translate(maxLabelW + kPadX / 2.f, 0.f);
148
149 for (int s = 0; s < 2; ++s) {
150 SkPaint& paint = s ? strokePaint : fillPaint;
151
commit-bot@chromium.org35d48722014-02-13 18:36:36 +0000152 SkScalar columnH = 0;
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000153 for (int m = 0; m < matrices.count(); ++m) {
154 columnH = 0;
155 canvas->save();
156 canvas->drawText(matrices[m].fLabel, strlen(matrices[m].fLabel),
157 0, labelPaint.getTextSize() - 1, labelPaint);
158 canvas->translate(0, kPadY / 2 + kPointSize);
159 columnH += kPadY / 2 + kPointSize;
160 for (int lm = 0; lm < localMatrices.count(); ++lm) {
161 shader->setLocalMatrix(localMatrices[lm].fMatrix);
162
163 canvas->save();
164 canvas->concat(matrices[m].fMatrix);
165 canvas->drawText(kText, kTextLen, 0, 0, paint);
166 canvas->drawText(kText, kTextLen, 0, 0, outlinePaint);
167 canvas->restore();
168
169 SkPath path;
170 path.arcTo(SkRect::MakeXYWH(-0.1f * w, 0.f,
171 1.2f * w, 2.f * kPointSize),
172 225.f, 359.f,
173 false);
174 path.close();
175
176 canvas->translate(0.f, kPointSize + kPadY);
177 columnH += kPointSize + kPadY;
178
179 canvas->save();
180 canvas->concat(matrices[m].fMatrix);
181 canvas->drawTextOnPath(kText, kTextLen, path, NULL, paint);
182 canvas->drawTextOnPath(kText, kTextLen, path, NULL, outlinePaint);
183 canvas->restore();
184 SkPaint stroke;
185 stroke.setStyle(SkPaint::kStroke_Style);
186 canvas->translate(0.f, kPointSize + kPadY);
187 columnH += kPointSize + kPadY;
188 }
189 canvas->restore();
190 canvas->translate(w + kPadX, 0.f);
191 }
192 if (0 == s) {
193 canvas->drawLine(0.f, -kPadY, 0.f, columnH + kPadY, outlinePaint);
194 canvas->translate(kPadX / 2, 0.f);
195 static const char kFillLabel[] = "Filled";
196 static const char kStrokeLabel[] = "Stroked";
197 SkScalar y = columnH + kPadY / 2;
198 SkScalar fillX = -outlinePaint.measureText(kFillLabel, strlen(kFillLabel)) - kPadX;
199 SkScalar strokeX = kPadX;
200 canvas->drawText(kFillLabel, strlen(kFillLabel), fillX, y, labelPaint);
201 canvas->drawText(kStrokeLabel, strlen(kStrokeLabel), strokeX, y, labelPaint);
202 }
203 }
204 }
205
reed@google.com1b6c73d2012-10-10 15:17:24 +0000206 virtual uint32_t onGetFlags() const SK_OVERRIDE {
207 // disable 565 for now, til mike fixes the debug assert
208 return this->INHERITED::onGetFlags() | kSkip565_Flag;
209 }
210
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000211private:
212 typedef GM INHERITED;
213};
214
215///////////////////////////////////////////////////////////////////////////////
216
217#ifndef SK_BUILD_FOR_ANDROID
218static GM* MyFactory(void*) { return new ShaderText2GM; }
219static GMRegistry reg(MyFactory);
220#endif
221}