blob: 87fe1b29f4ce534e2c223d70e43da60a9bb2d250 [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 SkPaint fillPaint;
99 fillPaint.setAntiAlias(true);
100 fillPaint.setTextSize(SkIntToScalar(kPointSize));
reed@google.com44699382013-10-31 17:28:30 +0000101 fillPaint.setFilterLevel(SkPaint::kLow_FilterLevel);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000102
103 SkPaint outlinePaint;
104 outlinePaint.setAntiAlias(true);
105 outlinePaint.setTextSize(SkIntToScalar(kPointSize));
106 outlinePaint.setStyle(SkPaint::kStroke_Style);
107 outlinePaint.setStrokeWidth(0.f);
108
109 SkScalar w = fillPaint.measureText(kText, kTextLen);
110 static SkScalar kPadY = 0.5f * kPointSize;
111 static SkScalar kPadX = 1.5f * kPointSize;
112
113 SkPaint strokePaint(fillPaint);
114 strokePaint.setStyle(SkPaint::kStroke_Style);
115 strokePaint.setStrokeWidth(kPointSize * 0.1f);
116
117 SkPaint labelPaint;
118 labelPaint.setColor(0xff000000);
119 labelPaint.setAntiAlias(true);
120 labelPaint.setTextSize(12.f);
121
122 canvas->translate(15.f, 15.f);
123 canvas->drawBitmap(bmp, 0, 0);
124 canvas->translate(0, bmp.height() + labelPaint.getTextSize() + 15.f);
125
126 static const char kLabelLabel[] = "localM / canvasM";
127 canvas->drawText(kLabelLabel, strlen(kLabelLabel), 0, 0, labelPaint);
128 canvas->translate(0, 15.f);
129
130 canvas->save();
131 SkScalar maxLabelW = 0;
132 canvas->translate(0, kPadY / 2 + kPointSize);
133 for (int lm = 0; lm < localMatrices.count(); ++lm) {
134 canvas->drawText(matrices[lm].fLabel, strlen(matrices[lm].fLabel),
135 0, labelPaint.getTextSize() - 1, labelPaint);
136 SkScalar labelW = labelPaint.measureText(matrices[lm].fLabel,
137 strlen(matrices[lm].fLabel));
138 maxLabelW = SkMaxScalar(maxLabelW, labelW);
139 canvas->translate(0.f, 2 * kPointSize + 2.5f * kPadY);
140 }
141 canvas->restore();
142
143 canvas->translate(maxLabelW + kPadX / 2.f, 0.f);
144
145 for (int s = 0; s < 2; ++s) {
146 SkPaint& paint = s ? strokePaint : fillPaint;
147
commit-bot@chromium.org35d48722014-02-13 18:36:36 +0000148 SkScalar columnH = 0;
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000149 for (int m = 0; m < matrices.count(); ++m) {
150 columnH = 0;
151 canvas->save();
152 canvas->drawText(matrices[m].fLabel, strlen(matrices[m].fLabel),
153 0, labelPaint.getTextSize() - 1, labelPaint);
154 canvas->translate(0, kPadY / 2 + kPointSize);
155 columnH += kPadY / 2 + kPointSize;
156 for (int lm = 0; lm < localMatrices.count(); ++lm) {
commit-bot@chromium.orgbd090452014-04-29 21:46:59 +0000157 paint.setShader(
158 SkShader::CreateBitmapShader(bmp,
159 SkShader::kMirror_TileMode,
160 SkShader::kRepeat_TileMode,
161 &localMatrices[lm].fMatrix))->unref();
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000162
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
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000217static GM* MyFactory(void*) { return new ShaderText2GM; }
218static GMRegistry reg(MyFactory);
bsalomon@google.com41fe45b2012-10-10 13:35:23 +0000219}