blob: a51413869ba5aede9e5c2b736eaffe4133556eeb [file] [log] [blame]
reed71c3c762015-06-24 10:29:17 -07001/*
2 * Copyright 2015 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
8#include "gm.h"
Hal Canary95e3c052017-01-11 12:44:43 -05009
10#include "SkAutoMalloc.h"
reed71c3c762015-06-24 10:29:17 -070011#include "SkCanvas.h"
12#include "SkRSXform.h"
Mike Reeda62d0362018-08-28 16:26:06 -040013#include "SkTextOnPath.h"
reed71c3c762015-06-24 10:29:17 -070014#include "SkSurface.h"
Mike Reed46596ae2018-01-02 15:40:29 -050015#include "sk_tool_utils.h"
reed71c3c762015-06-24 10:29:17 -070016
17class DrawAtlasGM : public skiagm::GM {
reed9ce9d672016-03-17 10:51:11 -070018 static sk_sp<SkImage> MakeAtlas(SkCanvas* caller, const SkRect& target) {
reed71c3c762015-06-24 10:29:17 -070019 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
Cary Clarka24712e2018-09-05 18:41:40 +000020 auto surface(sk_tool_utils::makeSurface(caller, info));
reed71c3c762015-06-24 10:29:17 -070021 SkCanvas* canvas = surface->getCanvas();
22 // draw red everywhere, but we don't expect to see it in the draw, testing the notion
23 // that drawAtlas draws a subset-region of the atlas.
24 canvas->clear(SK_ColorRED);
25
26 SkPaint paint;
reed374772b2016-10-05 17:33:02 -070027 paint.setBlendMode(SkBlendMode::kClear);
reed71c3c762015-06-24 10:29:17 -070028 SkRect r(target);
29 r.inset(-1, -1);
30 // zero out a place (with a 1-pixel border) to land our drawing.
31 canvas->drawRect(r, paint);
reed374772b2016-10-05 17:33:02 -070032 paint.setBlendMode(SkBlendMode::kSrcOver);
reed71c3c762015-06-24 10:29:17 -070033 paint.setColor(SK_ColorBLUE);
34 paint.setAntiAlias(true);
35 canvas->drawOval(target, paint);
reed9ce9d672016-03-17 10:51:11 -070036 return surface->makeImageSnapshot();
reed71c3c762015-06-24 10:29:17 -070037 }
38
reed71c3c762015-06-24 10:29:17 -070039public:
40 DrawAtlasGM() {}
halcanary9d524f22016-03-29 09:03:52 -070041
reed71c3c762015-06-24 10:29:17 -070042protected:
halcanary9d524f22016-03-29 09:03:52 -070043
reed71c3c762015-06-24 10:29:17 -070044 SkString onShortName() override {
45 return SkString("draw-atlas");
46 }
halcanary9d524f22016-03-29 09:03:52 -070047
reed71c3c762015-06-24 10:29:17 -070048 SkISize onISize() override {
49 return SkISize::Make(640, 480);
50 }
halcanary9d524f22016-03-29 09:03:52 -070051
reed71c3c762015-06-24 10:29:17 -070052 void onDraw(SkCanvas* canvas) override {
53 const SkRect target = { 50, 50, 80, 90 };
brianosman95e8d0a2016-09-29 13:43:49 -070054 auto atlas = MakeAtlas(canvas, target);
reed71c3c762015-06-24 10:29:17 -070055
56 const struct {
57 SkScalar fScale;
58 SkScalar fDegrees;
59 SkScalar fTx;
60 SkScalar fTy;
halcanary9d524f22016-03-29 09:03:52 -070061
reed71c3c762015-06-24 10:29:17 -070062 void apply(SkRSXform* xform) const {
63 const SkScalar rad = SkDegreesToRadians(fDegrees);
64 xform->fSCos = fScale * SkScalarCos(rad);
65 xform->fSSin = fScale * SkScalarSin(rad);
66 xform->fTx = fTx;
67 xform->fTy = fTy;
68 }
69 } rec[] = {
70 { 1, 0, 10, 10 }, // just translate
71 { 2, 0, 110, 10 }, // scale + translate
72 { 1, 30, 210, 10 }, // rotate + translate
73 { 2, -30, 310, 30 }, // scale + rotate + translate
74 };
75
76 const int N = SK_ARRAY_COUNT(rec);
77 SkRSXform xform[N];
78 SkRect tex[N];
79 SkColor colors[N];
80
81 for (int i = 0; i < N; ++i) {
82 rec[i].apply(&xform[i]);
83 tex[i] = target;
84 colors[i] = 0x80FF0000 + (i * 40 * 256);
85 }
86
87 SkPaint paint;
88 paint.setFilterQuality(kLow_SkFilterQuality);
89 paint.setAntiAlias(true);
90
brianosman95e8d0a2016-09-29 13:43:49 -070091 canvas->drawAtlas(atlas.get(), xform, tex, N, nullptr, &paint);
reed71c3c762015-06-24 10:29:17 -070092 canvas->translate(0, 100);
Mike Reed7d954ad2016-10-28 15:42:34 -040093 canvas->drawAtlas(atlas.get(), xform, tex, colors, N, SkBlendMode::kSrcIn, nullptr, &paint);
reed71c3c762015-06-24 10:29:17 -070094 }
halcanary9d524f22016-03-29 09:03:52 -070095
reed71c3c762015-06-24 10:29:17 -070096private:
97 typedef GM INHERITED;
98};
99DEF_GM( return new DrawAtlasGM; )
reed45561a02016-07-07 12:47:17 -0700100
101///////////////////////////////////////////////////////////////////////////////////////////////////
102#include "SkPath.h"
103#include "SkPathMeasure.h"
104
Mike Reed488fbd12018-01-29 13:33:06 -0500105static void draw_text_on_path(SkCanvas* canvas, const void* text, size_t length,
106 const SkPoint xy[], const SkPath& path, const SkPaint& paint,
107 float baseline_offset, bool useRSX) {
reed45561a02016-07-07 12:47:17 -0700108 SkPathMeasure meas(path, false);
109
110 int count = paint.countText(text, length);
reed7c70d7c2016-07-12 15:06:33 -0700111 size_t size = count * (sizeof(SkRSXform) + sizeof(SkScalar));
112 SkAutoSMalloc<512> storage(size);
113 SkRSXform* xform = (SkRSXform*)storage.get();
114 SkScalar* widths = (SkScalar*)(xform + count);
115
reed7c70d7c2016-07-12 15:06:33 -0700116 // Compute a conservative bounds so we can cull the draw
117 const SkRect font = paint.getFontBounds();
118 const SkScalar max = SkTMax(SkTMax(SkScalarAbs(font.fLeft), SkScalarAbs(font.fRight)),
119 SkTMax(SkScalarAbs(font.fTop), SkScalarAbs(font.fBottom)));
120 const SkRect bounds = path.getBounds().makeOutset(max, max);
121
Mike Reed488fbd12018-01-29 13:33:06 -0500122 if (useRSX) {
123 paint.getTextWidths(text, length, widths);
124
125 for (int i = 0; i < count; ++i) {
126 // we want to position each character on the center of its advance
127 const SkScalar offset = SkScalarHalf(widths[i]);
128 SkPoint pos;
129 SkVector tan;
130 if (!meas.getPosTan(xy[i].x() + offset, &pos, &tan)) {
131 pos = xy[i];
132 tan.set(1, 0);
133 }
134 pos += SkVector::Make(-tan.fY, tan.fX) * baseline_offset;
135
136 xform[i].fSCos = tan.x();
137 xform[i].fSSin = tan.y();
138 xform[i].fTx = pos.x() - tan.y() * xy[i].y() - tan.x() * offset;
139 xform[i].fTy = pos.y() + tan.x() * xy[i].y() - tan.y() * offset;
140 }
141
142 canvas->drawTextRSXform(text, length, &xform[0], &bounds, paint);
143 } else {
Mike Reeda62d0362018-08-28 16:26:06 -0400144 SkDrawTextOnPathHV(text, length, paint, path, 0, baseline_offset, canvas);
Mike Reed488fbd12018-01-29 13:33:06 -0500145 }
reed7c70d7c2016-07-12 15:06:33 -0700146
147 if (true) {
148 SkPaint p;
149 p.setStyle(SkPaint::kStroke_Style);
150 canvas->drawRect(bounds, p);
151 }
reed45561a02016-07-07 12:47:17 -0700152}
153
Mike Reed9c2916e2018-03-15 13:37:08 -0400154#include "SkGradientShader.h"
155static sk_sp<SkShader> make_shader() {
156 SkPoint pts[2] = {{0, 0}, {220, 0}};
157 SkColor colors[2] = {SK_ColorRED, SK_ColorBLUE};
158 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kMirror_TileMode);
159}
160
Mike Reedcaaf2112018-01-29 14:32:38 -0500161static void drawTextPath(SkCanvas* canvas, bool useRSX, bool doStroke) {
reeddde2b1f2016-07-08 03:31:09 -0700162 const char text0[] = "ABCDFGHJKLMNOPQRSTUVWXYZ";
reeddde2b1f2016-07-08 03:31:09 -0700163 const int N = sizeof(text0) - 1;
reed45561a02016-07-07 12:47:17 -0700164 SkPoint pos[N];
reed45561a02016-07-07 12:47:17 -0700165
166 SkPaint paint;
Mike Reed9c2916e2018-03-15 13:37:08 -0400167 paint.setShader(make_shader());
reed45561a02016-07-07 12:47:17 -0700168 paint.setAntiAlias(true);
reed7c70d7c2016-07-12 15:06:33 -0700169 paint.setTextSize(100);
Mike Reedcaaf2112018-01-29 14:32:38 -0500170 if (doStroke) {
171 paint.setStyle(SkPaint::kStroke_Style);
172 paint.setStrokeWidth(2.25f);
173 paint.setStrokeJoin(SkPaint::kRound_Join);
174 }
reed7c70d7c2016-07-12 15:06:33 -0700175
176 SkScalar x = 0;
177 for (int i = 0; i < N; ++i) {
178 pos[i].set(x, 0);
179 x += paint.measureText(&text0[i], 1);
180 }
reed45561a02016-07-07 12:47:17 -0700181
182 SkPath path;
Mike Reed488fbd12018-01-29 13:33:06 -0500183 const float baseline_offset = -5;
reed45561a02016-07-07 12:47:17 -0700184
Mike Reed488fbd12018-01-29 13:33:06 -0500185 const SkPath::Direction dirs[] = {
186 SkPath::kCW_Direction, SkPath::kCCW_Direction,
187 };
188 for (auto d : dirs) {
189 path.reset();
190 path.addOval(SkRect::MakeXYWH(160, 160, 540, 540), d);
191 draw_text_on_path(canvas, text0, N, pos, path, paint, baseline_offset, useRSX);
192 }
reed45561a02016-07-07 12:47:17 -0700193
Mike Reedcaaf2112018-01-29 14:32:38 -0500194 paint.reset();
reed45561a02016-07-07 12:47:17 -0700195 paint.setStyle(SkPaint::kStroke_Style);
196 canvas->drawPath(path, paint);
197}
198
Mike Reedcaaf2112018-01-29 14:32:38 -0500199DEF_SIMPLE_GM(drawTextRSXform, canvas, 860, 860) {
Mike Reed488fbd12018-01-29 13:33:06 -0500200 canvas->scale(0.5f, 0.5f);
Mike Reedcaaf2112018-01-29 14:32:38 -0500201 const bool doStroke[] = { false, true };
202 for (auto st : doStroke) {
203 canvas->save();
204 drawTextPath(canvas, false, st);
205 canvas->translate(860, 0);
206 drawTextPath(canvas, true, st);
207 canvas->restore();
208 canvas->translate(0, 860);
209 }
Mike Reed488fbd12018-01-29 13:33:06 -0500210}
211
Mike Reed93cb2522017-04-28 10:02:47 -0400212#include "Resources.h"
213#include "SkColorFilter.h"
214#include "SkVertices.h"
reed45561a02016-07-07 12:47:17 -0700215
Mike Reed93cb2522017-04-28 10:02:47 -0400216static sk_sp<SkVertices> make_vertices(sk_sp<SkImage> image, const SkRect& r,
217 SkColor color) {
218 SkPoint pos[4];
219 r.toQuad(pos);
220 SkColor colors[4] = { color, color, color, color };
221 return SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4,
222 pos, pos, colors);
223}
224
225/*
226 * drawAtlas and drawVertices have several things in common:
227 * - can create compound "shaders", combining texture and colors
228 * - these are combined via an explicit blendmode
229 * - like drawImage, they only respect parts of the paint
230 * - colorfilter, imagefilter, blendmode, alpha
231 *
232 * This GM produces a series of pairs of images (atlas | vertices).
233 * Each pair should look the same, and each set shows a different combination
234 * of alpha | colorFilter | mode
235 */
236DEF_SIMPLE_GM(compare_atlas_vertices, canvas, 560, 585) {
237 const SkRect tex = SkRect::MakeWH(128, 128);
238 const SkRSXform xform = SkRSXform::Make(1, 0, 0, 0);
239 const SkColor color = 0x884488CC;
240
Hal Canaryc465d132017-12-08 10:21:31 -0500241 auto image = GetResourceAsImage("images/mandrill_128.png");
Mike Reed93cb2522017-04-28 10:02:47 -0400242 auto verts = make_vertices(image, tex, color);
243 const sk_sp<SkColorFilter> filters[] = {
244 nullptr,
245 SkColorFilter::MakeModeFilter(0xFF00FF88, SkBlendMode::kModulate),
246 };
247 const SkBlendMode modes[] = {
248 SkBlendMode::kSrcOver,
249 SkBlendMode::kPlus,
250 };
251
252 canvas->translate(10, 10);
253 SkPaint paint;
254 for (SkBlendMode mode : modes) {
255 for (int alpha : { 0xFF, 0x7F }) {
256 paint.setAlpha(alpha);
257 canvas->save();
258 for (auto cf : filters) {
259 paint.setColorFilter(cf);
260 canvas->drawAtlas(image, &xform, &tex, &color, 1,
261 mode, &tex, &paint);
262 canvas->translate(128, 0);
Mike Reed0acd7952017-04-28 11:12:19 -0400263 paint.setShader(image->makeShader());
Mike Reed93cb2522017-04-28 10:02:47 -0400264 canvas->drawVertices(verts, mode, paint);
265 paint.setShader(nullptr);
266 canvas->translate(145, 0);
267 }
268 canvas->restore();
269 canvas->translate(0, 145);
270 }
271 }
272}