blob: 7a3c7e98b295b2b579ce118fc9fd197f2d7c3fd7 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Hal Canary95e3c052017-01-11 12:44:43 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkRSXform.h"
12#include "include/core/SkSurface.h"
13#include "include/core/SkTextBlob.h"
14#include "src/core/SkAutoMalloc.h"
15#include "tools/ToolUtils.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);
Mike Kleinea3f0142019-03-20 11:12:10 -050020 auto surface(ToolUtils::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///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500102#include "include/core/SkFont.h"
103#include "include/core/SkPath.h"
104#include "include/core/SkPathMeasure.h"
105#include "src/core/SkFontPriv.h"
reed45561a02016-07-07 12:47:17 -0700106
Mike Reed488fbd12018-01-29 13:33:06 -0500107static void draw_text_on_path(SkCanvas* canvas, const void* text, size_t length,
Mike Reeddf3d2252018-12-20 17:10:27 -0500108 const SkPoint xy[], const SkPath& path, const SkFont& font, const SkPaint& paint,
Mike Reed1eb9af92018-10-01 12:16:59 -0400109 float baseline_offset) {
reed45561a02016-07-07 12:47:17 -0700110 SkPathMeasure meas(path, false);
111
Mike Reed7d1eb332018-12-04 17:35:56 -0500112 int count = font.countText(text, length, kUTF8_SkTextEncoding);
reed7c70d7c2016-07-12 15:06:33 -0700113 size_t size = count * (sizeof(SkRSXform) + sizeof(SkScalar));
114 SkAutoSMalloc<512> storage(size);
115 SkRSXform* xform = (SkRSXform*)storage.get();
116 SkScalar* widths = (SkScalar*)(xform + count);
117
reed7c70d7c2016-07-12 15:06:33 -0700118 // Compute a conservative bounds so we can cull the draw
Mike Reed7d1eb332018-12-04 17:35:56 -0500119 const SkRect fontb = SkFontPriv::GetFontBounds(font);
120 const SkScalar max = SkTMax(SkTMax(SkScalarAbs(fontb.fLeft), SkScalarAbs(fontb.fRight)),
121 SkTMax(SkScalarAbs(fontb.fTop), SkScalarAbs(fontb.fBottom)));
reed7c70d7c2016-07-12 15:06:33 -0700122 const SkRect bounds = path.getBounds().makeOutset(max, max);
123
Mike Reeddf3d2252018-12-20 17:10:27 -0500124 SkAutoTArray<SkGlyphID> glyphs(count);
125 font.textToGlyphs(text, length, kUTF8_SkTextEncoding, glyphs.get(), count);
126 font.getWidths(glyphs.get(), count, widths);
Mike Reed488fbd12018-01-29 13:33:06 -0500127
Mike Reed1eb9af92018-10-01 12:16:59 -0400128 for (int i = 0; i < count; ++i) {
129 // we want to position each character on the center of its advance
130 const SkScalar offset = SkScalarHalf(widths[i]);
131 SkPoint pos;
132 SkVector tan;
133 if (!meas.getPosTan(xy[i].x() + offset, &pos, &tan)) {
134 pos = xy[i];
135 tan.set(1, 0);
Mike Reed488fbd12018-01-29 13:33:06 -0500136 }
Mike Reed1eb9af92018-10-01 12:16:59 -0400137 pos += SkVector::Make(-tan.fY, tan.fX) * baseline_offset;
Mike Reed488fbd12018-01-29 13:33:06 -0500138
Mike Reed1eb9af92018-10-01 12:16:59 -0400139 xform[i].fSCos = tan.x();
140 xform[i].fSSin = tan.y();
141 xform[i].fTx = pos.x() - tan.y() * xy[i].y() - tan.x() * offset;
142 xform[i].fTy = pos.y() + tan.x() * xy[i].y() - tan.y() * offset;
Mike Reed488fbd12018-01-29 13:33:06 -0500143 }
reed7c70d7c2016-07-12 15:06:33 -0700144
Mike Reeddf3d2252018-12-20 17:10:27 -0500145 canvas->drawTextBlob(SkTextBlob::MakeFromRSXform(glyphs.get(), count * sizeof(SkGlyphID),
146 &xform[0], font, kGlyphID_SkTextEncoding),
147 0, 0, paint);
Mike Reed1eb9af92018-10-01 12:16:59 -0400148
reed7c70d7c2016-07-12 15:06:33 -0700149 if (true) {
150 SkPaint p;
151 p.setStyle(SkPaint::kStroke_Style);
152 canvas->drawRect(bounds, p);
153 }
reed45561a02016-07-07 12:47:17 -0700154}
155
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500156#include "include/effects/SkGradientShader.h"
Mike Reed9c2916e2018-03-15 13:37:08 -0400157static sk_sp<SkShader> make_shader() {
158 SkPoint pts[2] = {{0, 0}, {220, 0}};
159 SkColor colors[2] = {SK_ColorRED, SK_ColorBLUE};
Mike Reedfae8fce2019-04-03 10:27:45 -0400160 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kMirror);
Mike Reed9c2916e2018-03-15 13:37:08 -0400161}
162
Mike Reed1eb9af92018-10-01 12:16:59 -0400163static void drawTextPath(SkCanvas* canvas, bool doStroke) {
reeddde2b1f2016-07-08 03:31:09 -0700164 const char text0[] = "ABCDFGHJKLMNOPQRSTUVWXYZ";
reeddde2b1f2016-07-08 03:31:09 -0700165 const int N = sizeof(text0) - 1;
reed45561a02016-07-07 12:47:17 -0700166 SkPoint pos[N];
reed45561a02016-07-07 12:47:17 -0700167
Mike Reeddf3d2252018-12-20 17:10:27 -0500168 SkFont font;
169 font.setSize(100);
170
reed45561a02016-07-07 12:47:17 -0700171 SkPaint paint;
Mike Reed9c2916e2018-03-15 13:37:08 -0400172 paint.setShader(make_shader());
reed45561a02016-07-07 12:47:17 -0700173 paint.setAntiAlias(true);
Mike Reedcaaf2112018-01-29 14:32:38 -0500174 if (doStroke) {
175 paint.setStyle(SkPaint::kStroke_Style);
176 paint.setStrokeWidth(2.25f);
177 paint.setStrokeJoin(SkPaint::kRound_Join);
178 }
reed7c70d7c2016-07-12 15:06:33 -0700179
180 SkScalar x = 0;
181 for (int i = 0; i < N; ++i) {
182 pos[i].set(x, 0);
Mike Reeddf3d2252018-12-20 17:10:27 -0500183 x += font.measureText(&text0[i], 1, kUTF8_SkTextEncoding, nullptr, &paint);
reed7c70d7c2016-07-12 15:06:33 -0700184 }
reed45561a02016-07-07 12:47:17 -0700185
186 SkPath path;
Mike Reed488fbd12018-01-29 13:33:06 -0500187 const float baseline_offset = -5;
reed45561a02016-07-07 12:47:17 -0700188
Mike Reed488fbd12018-01-29 13:33:06 -0500189 const SkPath::Direction dirs[] = {
190 SkPath::kCW_Direction, SkPath::kCCW_Direction,
191 };
192 for (auto d : dirs) {
193 path.reset();
194 path.addOval(SkRect::MakeXYWH(160, 160, 540, 540), d);
Mike Reeddf3d2252018-12-20 17:10:27 -0500195 draw_text_on_path(canvas, text0, N, pos, path, font, paint, baseline_offset);
Mike Reed488fbd12018-01-29 13:33:06 -0500196 }
reed45561a02016-07-07 12:47:17 -0700197
Mike Reedcaaf2112018-01-29 14:32:38 -0500198 paint.reset();
reed45561a02016-07-07 12:47:17 -0700199 paint.setStyle(SkPaint::kStroke_Style);
200 canvas->drawPath(path, paint);
201}
202
Mike Reed1eb9af92018-10-01 12:16:59 -0400203DEF_SIMPLE_GM(drawTextRSXform, canvas, 430, 860) {
Mike Reed488fbd12018-01-29 13:33:06 -0500204 canvas->scale(0.5f, 0.5f);
Mike Reedcaaf2112018-01-29 14:32:38 -0500205 const bool doStroke[] = { false, true };
206 for (auto st : doStroke) {
Mike Reed1eb9af92018-10-01 12:16:59 -0400207 drawTextPath(canvas, st);
Mike Reedcaaf2112018-01-29 14:32:38 -0500208 canvas->translate(0, 860);
209 }
Mike Reed488fbd12018-01-29 13:33:06 -0500210}
211
Mike Reededce0aa2018-12-20 15:24:21 -0500212// Exercise xform blob and its bounds
213DEF_SIMPLE_GM(blob_rsxform, canvas, 500, 100) {
214 SkFont font;
Mike Kleinea3f0142019-03-20 11:12:10 -0500215 font.setTypeface(ToolUtils::create_portable_typeface());
Mike Reededce0aa2018-12-20 15:24:21 -0500216 font.setSize(50);
217
218 const char text[] = "CrazyXform";
219 constexpr size_t len = sizeof(text) - 1;
220
221 SkRSXform xforms[len];
222 SkScalar scale = 1;
223 SkScalar x = 0, y = 0;
224 for (size_t i = 0; i < len; ++i) {
225 scale = SkScalarSin(i * SK_ScalarPI / (len-1)) * 0.75f + 0.5f;
226 xforms[i] = SkRSXform::Make(scale, 0, x, y);
227 x += 50 * scale;
228 }
229
230 auto blob = SkTextBlob::MakeFromRSXform(text, len, xforms, font);
231
232 SkPoint offset = { 20, 70 };
233 SkPaint paint;
234 paint.setColor(0xFFCCCCCC);
235 canvas->drawRect(blob->bounds().makeOffset(offset.fX, offset.fY), paint);
236 paint.setColor(SK_ColorBLACK);
237 canvas->drawTextBlob(blob, offset.fX, offset.fY, paint);
238}
239
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500240#include "include/core/SkColorFilter.h"
241#include "include/core/SkVertices.h"
242#include "tools/Resources.h"
reed45561a02016-07-07 12:47:17 -0700243
Mike Reed93cb2522017-04-28 10:02:47 -0400244static sk_sp<SkVertices> make_vertices(sk_sp<SkImage> image, const SkRect& r,
245 SkColor color) {
246 SkPoint pos[4];
247 r.toQuad(pos);
248 SkColor colors[4] = { color, color, color, color };
249 return SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4,
250 pos, pos, colors);
251}
252
253/*
254 * drawAtlas and drawVertices have several things in common:
255 * - can create compound "shaders", combining texture and colors
256 * - these are combined via an explicit blendmode
257 * - like drawImage, they only respect parts of the paint
258 * - colorfilter, imagefilter, blendmode, alpha
259 *
260 * This GM produces a series of pairs of images (atlas | vertices).
261 * Each pair should look the same, and each set shows a different combination
262 * of alpha | colorFilter | mode
263 */
264DEF_SIMPLE_GM(compare_atlas_vertices, canvas, 560, 585) {
265 const SkRect tex = SkRect::MakeWH(128, 128);
266 const SkRSXform xform = SkRSXform::Make(1, 0, 0, 0);
267 const SkColor color = 0x884488CC;
268
Hal Canaryc465d132017-12-08 10:21:31 -0500269 auto image = GetResourceAsImage("images/mandrill_128.png");
Mike Reed93cb2522017-04-28 10:02:47 -0400270 auto verts = make_vertices(image, tex, color);
271 const sk_sp<SkColorFilter> filters[] = {
272 nullptr,
Mike Reedb286bc22019-04-08 16:23:20 -0400273 SkColorFilters::Blend(0xFF00FF88, SkBlendMode::kModulate),
Mike Reed93cb2522017-04-28 10:02:47 -0400274 };
275 const SkBlendMode modes[] = {
276 SkBlendMode::kSrcOver,
277 SkBlendMode::kPlus,
278 };
279
280 canvas->translate(10, 10);
281 SkPaint paint;
282 for (SkBlendMode mode : modes) {
Mike Reed9407e242019-02-15 16:13:57 -0500283 for (float alpha : { 1.0f, 0.5f }) {
284 paint.setAlphaf(alpha);
Mike Reed93cb2522017-04-28 10:02:47 -0400285 canvas->save();
286 for (auto cf : filters) {
287 paint.setColorFilter(cf);
288 canvas->drawAtlas(image, &xform, &tex, &color, 1,
289 mode, &tex, &paint);
290 canvas->translate(128, 0);
Mike Reed0acd7952017-04-28 11:12:19 -0400291 paint.setShader(image->makeShader());
Mike Reed93cb2522017-04-28 10:02:47 -0400292 canvas->drawVertices(verts, mode, paint);
293 paint.setShader(nullptr);
294 canvas->translate(145, 0);
295 }
296 canvas->restore();
297 canvas->translate(0, 145);
298 }
299 }
300}