blob: e4b4f0bea2bb9c6c24f0fbf9bce321b0478b8f6d [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"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkColor.h"
12#include "include/core/SkColorFilter.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkFont.h"
Ben Wagnerfcfd0af2020-07-09 10:51:27 -040014#include "include/core/SkFontMgr.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkFontTypes.h"
16#include "include/core/SkImage.h"
17#include "include/core/SkImageInfo.h"
18#include "include/core/SkPaint.h"
19#include "include/core/SkPath.h"
20#include "include/core/SkPathMeasure.h"
21#include "include/core/SkPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkRSXform.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include "include/core/SkRect.h"
24#include "include/core/SkRefCnt.h"
25#include "include/core/SkScalar.h"
26#include "include/core/SkShader.h"
27#include "include/core/SkSize.h"
28#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "include/core/SkSurface.h"
30#include "include/core/SkTextBlob.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040031#include "include/core/SkTileMode.h"
32#include "include/core/SkTypeface.h"
33#include "include/core/SkTypes.h"
34#include "include/core/SkVertices.h"
35#include "include/effects/SkGradientShader.h"
36#include "include/private/SkTemplates.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/core/SkAutoMalloc.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040038#include "src/core/SkFontPriv.h"
39#include "tools/Resources.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "tools/ToolUtils.h"
reed71c3c762015-06-24 10:29:17 -070041
Ben Wagner7fde8e12019-05-01 17:28:53 -040042#include <initializer_list>
43
reed71c3c762015-06-24 10:29:17 -070044class DrawAtlasGM : public skiagm::GM {
reed9ce9d672016-03-17 10:51:11 -070045 static sk_sp<SkImage> MakeAtlas(SkCanvas* caller, const SkRect& target) {
reed71c3c762015-06-24 10:29:17 -070046 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
Mike Kleinea3f0142019-03-20 11:12:10 -050047 auto surface(ToolUtils::makeSurface(caller, info));
reed71c3c762015-06-24 10:29:17 -070048 SkCanvas* canvas = surface->getCanvas();
49 // draw red everywhere, but we don't expect to see it in the draw, testing the notion
50 // that drawAtlas draws a subset-region of the atlas.
51 canvas->clear(SK_ColorRED);
52
53 SkPaint paint;
reed374772b2016-10-05 17:33:02 -070054 paint.setBlendMode(SkBlendMode::kClear);
reed71c3c762015-06-24 10:29:17 -070055 SkRect r(target);
56 r.inset(-1, -1);
57 // zero out a place (with a 1-pixel border) to land our drawing.
58 canvas->drawRect(r, paint);
reed374772b2016-10-05 17:33:02 -070059 paint.setBlendMode(SkBlendMode::kSrcOver);
reed71c3c762015-06-24 10:29:17 -070060 paint.setColor(SK_ColorBLUE);
61 paint.setAntiAlias(true);
62 canvas->drawOval(target, paint);
reed9ce9d672016-03-17 10:51:11 -070063 return surface->makeImageSnapshot();
reed71c3c762015-06-24 10:29:17 -070064 }
65
reed71c3c762015-06-24 10:29:17 -070066public:
67 DrawAtlasGM() {}
halcanary9d524f22016-03-29 09:03:52 -070068
reed71c3c762015-06-24 10:29:17 -070069protected:
halcanary9d524f22016-03-29 09:03:52 -070070
reed71c3c762015-06-24 10:29:17 -070071 SkString onShortName() override {
72 return SkString("draw-atlas");
73 }
halcanary9d524f22016-03-29 09:03:52 -070074
reed71c3c762015-06-24 10:29:17 -070075 SkISize onISize() override {
76 return SkISize::Make(640, 480);
77 }
halcanary9d524f22016-03-29 09:03:52 -070078
reed71c3c762015-06-24 10:29:17 -070079 void onDraw(SkCanvas* canvas) override {
80 const SkRect target = { 50, 50, 80, 90 };
brianosman95e8d0a2016-09-29 13:43:49 -070081 auto atlas = MakeAtlas(canvas, target);
reed71c3c762015-06-24 10:29:17 -070082
83 const struct {
84 SkScalar fScale;
85 SkScalar fDegrees;
86 SkScalar fTx;
87 SkScalar fTy;
halcanary9d524f22016-03-29 09:03:52 -070088
reed71c3c762015-06-24 10:29:17 -070089 void apply(SkRSXform* xform) const {
90 const SkScalar rad = SkDegreesToRadians(fDegrees);
91 xform->fSCos = fScale * SkScalarCos(rad);
92 xform->fSSin = fScale * SkScalarSin(rad);
93 xform->fTx = fTx;
94 xform->fTy = fTy;
95 }
96 } rec[] = {
97 { 1, 0, 10, 10 }, // just translate
98 { 2, 0, 110, 10 }, // scale + translate
99 { 1, 30, 210, 10 }, // rotate + translate
100 { 2, -30, 310, 30 }, // scale + rotate + translate
101 };
102
103 const int N = SK_ARRAY_COUNT(rec);
104 SkRSXform xform[N];
105 SkRect tex[N];
106 SkColor colors[N];
107
108 for (int i = 0; i < N; ++i) {
109 rec[i].apply(&xform[i]);
110 tex[i] = target;
111 colors[i] = 0x80FF0000 + (i * 40 * 256);
112 }
113
114 SkPaint paint;
reed71c3c762015-06-24 10:29:17 -0700115 paint.setAntiAlias(true);
Mike Reed99116302021-01-25 11:37:10 -0500116 SkSamplingOptions sampling(SkFilterMode::kLinear);
reed71c3c762015-06-24 10:29:17 -0700117
Mike Reed99116302021-01-25 11:37:10 -0500118 canvas->drawAtlas(atlas.get(), xform, tex, nullptr, N, SkBlendMode::kDst,
119 sampling, nullptr, &paint);
reed71c3c762015-06-24 10:29:17 -0700120 canvas->translate(0, 100);
Mike Reed99116302021-01-25 11:37:10 -0500121 canvas->drawAtlas(atlas.get(), xform, tex, colors, N, SkBlendMode::kSrcIn,
122 sampling, nullptr, &paint);
reed71c3c762015-06-24 10:29:17 -0700123 }
halcanary9d524f22016-03-29 09:03:52 -0700124
reed71c3c762015-06-24 10:29:17 -0700125private:
John Stiles7571f9e2020-09-02 22:42:33 -0400126 using INHERITED = GM;
reed71c3c762015-06-24 10:29:17 -0700127};
128DEF_GM( return new DrawAtlasGM; )
reed45561a02016-07-07 12:47:17 -0700129
130///////////////////////////////////////////////////////////////////////////////////////////////////
reed45561a02016-07-07 12:47:17 -0700131
Mike Reed488fbd12018-01-29 13:33:06 -0500132static void draw_text_on_path(SkCanvas* canvas, const void* text, size_t length,
Mike Reeddf3d2252018-12-20 17:10:27 -0500133 const SkPoint xy[], const SkPath& path, const SkFont& font, const SkPaint& paint,
Mike Reed1eb9af92018-10-01 12:16:59 -0400134 float baseline_offset) {
reed45561a02016-07-07 12:47:17 -0700135 SkPathMeasure meas(path, false);
136
Ben Wagner51e15a62019-05-07 15:38:46 -0400137 int count = font.countText(text, length, SkTextEncoding::kUTF8);
reed7c70d7c2016-07-12 15:06:33 -0700138 size_t size = count * (sizeof(SkRSXform) + sizeof(SkScalar));
139 SkAutoSMalloc<512> storage(size);
140 SkRSXform* xform = (SkRSXform*)storage.get();
141 SkScalar* widths = (SkScalar*)(xform + count);
142
reed7c70d7c2016-07-12 15:06:33 -0700143 // Compute a conservative bounds so we can cull the draw
Mike Reed7d1eb332018-12-04 17:35:56 -0500144 const SkRect fontb = SkFontPriv::GetFontBounds(font);
Brian Osman788b9162020-02-07 10:36:46 -0500145 const SkScalar max = std::max(std::max(SkScalarAbs(fontb.fLeft), SkScalarAbs(fontb.fRight)),
146 std::max(SkScalarAbs(fontb.fTop), SkScalarAbs(fontb.fBottom)));
reed7c70d7c2016-07-12 15:06:33 -0700147 const SkRect bounds = path.getBounds().makeOutset(max, max);
148
Mike Reeddf3d2252018-12-20 17:10:27 -0500149 SkAutoTArray<SkGlyphID> glyphs(count);
Ben Wagner51e15a62019-05-07 15:38:46 -0400150 font.textToGlyphs(text, length, SkTextEncoding::kUTF8, glyphs.get(), count);
Mike Reeddf3d2252018-12-20 17:10:27 -0500151 font.getWidths(glyphs.get(), count, widths);
Mike Reed488fbd12018-01-29 13:33:06 -0500152
Mike Reed1eb9af92018-10-01 12:16:59 -0400153 for (int i = 0; i < count; ++i) {
154 // we want to position each character on the center of its advance
155 const SkScalar offset = SkScalarHalf(widths[i]);
156 SkPoint pos;
157 SkVector tan;
158 if (!meas.getPosTan(xy[i].x() + offset, &pos, &tan)) {
159 pos = xy[i];
160 tan.set(1, 0);
Mike Reed488fbd12018-01-29 13:33:06 -0500161 }
Mike Reed1eb9af92018-10-01 12:16:59 -0400162 pos += SkVector::Make(-tan.fY, tan.fX) * baseline_offset;
Mike Reed488fbd12018-01-29 13:33:06 -0500163
Mike Reed1eb9af92018-10-01 12:16:59 -0400164 xform[i].fSCos = tan.x();
165 xform[i].fSSin = tan.y();
166 xform[i].fTx = pos.x() - tan.y() * xy[i].y() - tan.x() * offset;
167 xform[i].fTy = pos.y() + tan.x() * xy[i].y() - tan.y() * offset;
Mike Reed488fbd12018-01-29 13:33:06 -0500168 }
reed7c70d7c2016-07-12 15:06:33 -0700169
Mike Reeddf3d2252018-12-20 17:10:27 -0500170 canvas->drawTextBlob(SkTextBlob::MakeFromRSXform(glyphs.get(), count * sizeof(SkGlyphID),
Ben Wagner51e15a62019-05-07 15:38:46 -0400171 &xform[0], font, SkTextEncoding::kGlyphID),
Mike Reeddf3d2252018-12-20 17:10:27 -0500172 0, 0, paint);
Mike Reed1eb9af92018-10-01 12:16:59 -0400173
reed7c70d7c2016-07-12 15:06:33 -0700174 if (true) {
175 SkPaint p;
176 p.setStyle(SkPaint::kStroke_Style);
177 canvas->drawRect(bounds, p);
178 }
reed45561a02016-07-07 12:47:17 -0700179}
180
Mike Reed9c2916e2018-03-15 13:37:08 -0400181static sk_sp<SkShader> make_shader() {
182 SkPoint pts[2] = {{0, 0}, {220, 0}};
183 SkColor colors[2] = {SK_ColorRED, SK_ColorBLUE};
Mike Reedfae8fce2019-04-03 10:27:45 -0400184 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kMirror);
Mike Reed9c2916e2018-03-15 13:37:08 -0400185}
186
Mike Reed1eb9af92018-10-01 12:16:59 -0400187static void drawTextPath(SkCanvas* canvas, bool doStroke) {
reeddde2b1f2016-07-08 03:31:09 -0700188 const char text0[] = "ABCDFGHJKLMNOPQRSTUVWXYZ";
reeddde2b1f2016-07-08 03:31:09 -0700189 const int N = sizeof(text0) - 1;
reed45561a02016-07-07 12:47:17 -0700190 SkPoint pos[N];
reed45561a02016-07-07 12:47:17 -0700191
Mike Reeddf3d2252018-12-20 17:10:27 -0500192 SkFont font;
193 font.setSize(100);
194
reed45561a02016-07-07 12:47:17 -0700195 SkPaint paint;
Mike Reed9c2916e2018-03-15 13:37:08 -0400196 paint.setShader(make_shader());
reed45561a02016-07-07 12:47:17 -0700197 paint.setAntiAlias(true);
Mike Reedcaaf2112018-01-29 14:32:38 -0500198 if (doStroke) {
199 paint.setStyle(SkPaint::kStroke_Style);
200 paint.setStrokeWidth(2.25f);
201 paint.setStrokeJoin(SkPaint::kRound_Join);
202 }
reed7c70d7c2016-07-12 15:06:33 -0700203
204 SkScalar x = 0;
205 for (int i = 0; i < N; ++i) {
206 pos[i].set(x, 0);
Ben Wagner51e15a62019-05-07 15:38:46 -0400207 x += font.measureText(&text0[i], 1, SkTextEncoding::kUTF8, nullptr, &paint);
reed7c70d7c2016-07-12 15:06:33 -0700208 }
reed45561a02016-07-07 12:47:17 -0700209
210 SkPath path;
Mike Reed488fbd12018-01-29 13:33:06 -0500211 const float baseline_offset = -5;
reed45561a02016-07-07 12:47:17 -0700212
Mike Reed30bc5272019-11-22 18:34:02 +0000213 const SkPathDirection dirs[] = {
214 SkPathDirection::kCW, SkPathDirection::kCCW,
Mike Reed488fbd12018-01-29 13:33:06 -0500215 };
216 for (auto d : dirs) {
217 path.reset();
218 path.addOval(SkRect::MakeXYWH(160, 160, 540, 540), d);
Mike Reeddf3d2252018-12-20 17:10:27 -0500219 draw_text_on_path(canvas, text0, N, pos, path, font, paint, baseline_offset);
Mike Reed488fbd12018-01-29 13:33:06 -0500220 }
reed45561a02016-07-07 12:47:17 -0700221
Mike Reedcaaf2112018-01-29 14:32:38 -0500222 paint.reset();
reed45561a02016-07-07 12:47:17 -0700223 paint.setStyle(SkPaint::kStroke_Style);
224 canvas->drawPath(path, paint);
225}
226
Mike Reed1eb9af92018-10-01 12:16:59 -0400227DEF_SIMPLE_GM(drawTextRSXform, canvas, 430, 860) {
Mike Reed488fbd12018-01-29 13:33:06 -0500228 canvas->scale(0.5f, 0.5f);
Mike Reedcaaf2112018-01-29 14:32:38 -0500229 const bool doStroke[] = { false, true };
230 for (auto st : doStroke) {
Mike Reed1eb9af92018-10-01 12:16:59 -0400231 drawTextPath(canvas, st);
Mike Reedcaaf2112018-01-29 14:32:38 -0500232 canvas->translate(0, 860);
233 }
Mike Reed488fbd12018-01-29 13:33:06 -0500234}
235
Mike Reededce0aa2018-12-20 15:24:21 -0500236// Exercise xform blob and its bounds
237DEF_SIMPLE_GM(blob_rsxform, canvas, 500, 100) {
238 SkFont font;
Mike Kleinea3f0142019-03-20 11:12:10 -0500239 font.setTypeface(ToolUtils::create_portable_typeface());
Mike Reededce0aa2018-12-20 15:24:21 -0500240 font.setSize(50);
241
242 const char text[] = "CrazyXform";
243 constexpr size_t len = sizeof(text) - 1;
244
245 SkRSXform xforms[len];
246 SkScalar scale = 1;
247 SkScalar x = 0, y = 0;
248 for (size_t i = 0; i < len; ++i) {
249 scale = SkScalarSin(i * SK_ScalarPI / (len-1)) * 0.75f + 0.5f;
250 xforms[i] = SkRSXform::Make(scale, 0, x, y);
251 x += 50 * scale;
252 }
253
254 auto blob = SkTextBlob::MakeFromRSXform(text, len, xforms, font);
255
256 SkPoint offset = { 20, 70 };
257 SkPaint paint;
258 paint.setColor(0xFFCCCCCC);
259 canvas->drawRect(blob->bounds().makeOffset(offset.fX, offset.fY), paint);
260 paint.setColor(SK_ColorBLACK);
261 canvas->drawTextBlob(blob, offset.fX, offset.fY, paint);
262}
263
Ben Wagnerfcfd0af2020-07-09 10:51:27 -0400264// Exercise xform blob and its tight bounds
265DEF_SIMPLE_GM(blob_rsxform_distortable, canvas, 500, 100) {
266 sk_sp<SkTypeface> typeface;
267 std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf"));
268 if (distortable) {
269 sk_sp<SkFontMgr> fm = SkFontMgr::RefDefault();
270 const SkFontArguments::VariationPosition::Coordinate position[] = {
271 { SkSetFourByteTag('w','g','h','t'), 1.618033988749895f }
272 };
273 SkFontArguments params;
274 params.setVariationDesignPosition({position, SK_ARRAY_COUNT(position)});
275 typeface = fm->makeFromStream(std::move(distortable), params);
276 }
277
278 SkFont font(typeface, 50);
279
280 const char text[] = "abcabcabc";
281 constexpr size_t len = sizeof(text) - 1;
282
283 SkRSXform xforms[len];
284 SkScalar scale = 1;
285 SkScalar x = 0, y = 0;
286 for (size_t i = 0; i < len; ++i) {
287 scale = SkScalarSin(i * SK_ScalarPI / (len-1)) * 0.75f + 0.5f;
288 xforms[i] = SkRSXform::Make(scale, 0, x, y);
289 x += 50 * scale;
290 }
291
292 auto blob = SkTextBlob::MakeFromRSXform(text, len, xforms, font);
293
294 SkPoint offset = { 20, 70 };
295 SkPaint paint;
296 paint.setColor(0xFFCCCCCC);
297 canvas->drawRect(blob->bounds().makeOffset(offset.fX, offset.fY), paint);
298 paint.setColor(SK_ColorBLACK);
299 canvas->drawTextBlob(blob, offset.fX, offset.fY, paint);
300}
301
Mike Reed93cb2522017-04-28 10:02:47 -0400302static sk_sp<SkVertices> make_vertices(sk_sp<SkImage> image, const SkRect& r,
303 SkColor color) {
304 SkPoint pos[4];
305 r.toQuad(pos);
306 SkColor colors[4] = { color, color, color, color };
307 return SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4,
308 pos, pos, colors);
309}
310
311/*
312 * drawAtlas and drawVertices have several things in common:
313 * - can create compound "shaders", combining texture and colors
314 * - these are combined via an explicit blendmode
315 * - like drawImage, they only respect parts of the paint
316 * - colorfilter, imagefilter, blendmode, alpha
317 *
318 * This GM produces a series of pairs of images (atlas | vertices).
319 * Each pair should look the same, and each set shows a different combination
320 * of alpha | colorFilter | mode
321 */
322DEF_SIMPLE_GM(compare_atlas_vertices, canvas, 560, 585) {
323 const SkRect tex = SkRect::MakeWH(128, 128);
324 const SkRSXform xform = SkRSXform::Make(1, 0, 0, 0);
325 const SkColor color = 0x884488CC;
326
Hal Canaryc465d132017-12-08 10:21:31 -0500327 auto image = GetResourceAsImage("images/mandrill_128.png");
Mike Reed93cb2522017-04-28 10:02:47 -0400328 auto verts = make_vertices(image, tex, color);
329 const sk_sp<SkColorFilter> filters[] = {
330 nullptr,
Mike Reedb286bc22019-04-08 16:23:20 -0400331 SkColorFilters::Blend(0xFF00FF88, SkBlendMode::kModulate),
Mike Reed93cb2522017-04-28 10:02:47 -0400332 };
333 const SkBlendMode modes[] = {
334 SkBlendMode::kSrcOver,
335 SkBlendMode::kPlus,
336 };
337
338 canvas->translate(10, 10);
339 SkPaint paint;
340 for (SkBlendMode mode : modes) {
Mike Reed9407e242019-02-15 16:13:57 -0500341 for (float alpha : { 1.0f, 0.5f }) {
342 paint.setAlphaf(alpha);
Mike Reed93cb2522017-04-28 10:02:47 -0400343 canvas->save();
John Stilesbd3ffa42020-07-30 20:24:57 -0400344 for (const sk_sp<SkColorFilter>& cf : filters) {
Mike Reed93cb2522017-04-28 10:02:47 -0400345 paint.setColorFilter(cf);
Mike Reed99116302021-01-25 11:37:10 -0500346 canvas->drawAtlas(image.get(), &xform, &tex, &color, 1, mode,
347 SkSamplingOptions(), &tex, &paint);
Mike Reed93cb2522017-04-28 10:02:47 -0400348 canvas->translate(128, 0);
Mike Reedb612b6c2020-12-08 21:58:35 -0500349 paint.setShader(image->makeShader(SkSamplingOptions()));
Mike Reed93cb2522017-04-28 10:02:47 -0400350 canvas->drawVertices(verts, mode, paint);
351 paint.setShader(nullptr);
352 canvas->translate(145, 0);
353 }
354 canvas->restore();
355 canvas->translate(0, 145);
356 }
357 }
358}