xref: /aosp_15_r20/external/skia/gm/drawatlas.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
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/gm.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorFilter.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkFontMgr.h"
15 #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"
22 #include "include/core/SkRSXform.h"
23 #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/SkStream.h"
29 #include "include/core/SkString.h"
30 #include "include/core/SkSurface.h"
31 #include "include/core/SkTextBlob.h"
32 #include "include/core/SkTileMode.h"
33 #include "include/core/SkTypeface.h"
34 #include "include/core/SkTypes.h"
35 #include "include/core/SkVertices.h"
36 #include "include/effects/SkGradientShader.h"
37 #include "include/private/base/SkTemplates.h"
38 #include "src/base/SkAutoMalloc.h"
39 #include "src/core/SkFontPriv.h"
40 #include "tools/DecodeUtils.h"
41 #include "tools/Resources.h"
42 #include "tools/ToolUtils.h"
43 #include "tools/fonts/FontToolUtils.h"
44 
45 #include <initializer_list>
46 
47 using namespace skia_private;
48 
49 class DrawAtlasGM : public skiagm::GM {
MakeAtlas(SkCanvas * caller,const SkRect & target)50     static sk_sp<SkImage> MakeAtlas(SkCanvas* caller, const SkRect& target) {
51         SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
52         auto        surface(ToolUtils::makeSurface(caller, info));
53         SkCanvas* canvas = surface->getCanvas();
54         // draw red everywhere, but we don't expect to see it in the draw, testing the notion
55         // that drawAtlas draws a subset-region of the atlas.
56         canvas->clear(SK_ColorRED);
57 
58         SkPaint paint;
59         paint.setBlendMode(SkBlendMode::kClear);
60         SkRect r(target);
61         r.inset(-1, -1);
62         // zero out a place (with a 1-pixel border) to land our drawing.
63         canvas->drawRect(r, paint);
64         paint.setBlendMode(SkBlendMode::kSrcOver);
65         paint.setColor(SK_ColorBLUE);
66         paint.setAntiAlias(true);
67         canvas->drawOval(target, paint);
68         return surface->makeImageSnapshot();
69     }
70 
71 public:
DrawAtlasGM()72     DrawAtlasGM() {}
73 
74 protected:
getName() const75     SkString getName() const override { return SkString("draw-atlas"); }
76 
getISize()77     SkISize getISize() override { return SkISize::Make(640, 480); }
78 
onDraw(SkCanvas * canvas)79     void onDraw(SkCanvas* canvas) override {
80         const SkRect target = { 50, 50, 80, 90 };
81         auto atlas = MakeAtlas(canvas, target);
82 
83         const struct {
84             SkScalar fScale;
85             SkScalar fDegrees;
86             SkScalar fTx;
87             SkScalar fTy;
88 
89             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 = std::size(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;
115         paint.setAntiAlias(true);
116         SkSamplingOptions sampling(SkFilterMode::kLinear);
117 
118         canvas->drawAtlas(atlas.get(), xform, tex, nullptr, N, SkBlendMode::kDst,
119                           sampling, nullptr, &paint);
120         canvas->translate(0, 100);
121         canvas->drawAtlas(atlas.get(), xform, tex, colors, N, SkBlendMode::kSrcIn,
122                           sampling, nullptr, &paint);
123     }
124 
125 private:
126     using INHERITED = GM;
127 };
DEF_GM(return new DrawAtlasGM;)128 DEF_GM( return new DrawAtlasGM; )
129 
130 ///////////////////////////////////////////////////////////////////////////////////////////////////
131 
132 static void draw_text_on_path(SkCanvas* canvas, const void* text, size_t length,
133                               const SkPoint xy[], const SkPath& path, const SkFont& font, const SkPaint& paint,
134                               float baseline_offset) {
135     SkPathMeasure meas(path, false);
136 
137     int count = font.countText(text, length, SkTextEncoding::kUTF8);
138     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 
143     // Compute a conservative bounds so we can cull the draw
144     const SkRect fontb = SkFontPriv::GetFontBounds(font);
145     const SkScalar max = std::max(std::max(SkScalarAbs(fontb.fLeft), SkScalarAbs(fontb.fRight)),
146                                 std::max(SkScalarAbs(fontb.fTop), SkScalarAbs(fontb.fBottom)));
147     const SkRect bounds = path.getBounds().makeOutset(max, max);
148 
149     AutoTArray<SkGlyphID> glyphs(count);
150     font.textToGlyphs(text, length, SkTextEncoding::kUTF8, glyphs.get(), count);
151     font.getWidths(glyphs.get(), count, widths);
152 
153     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);
161         }
162         pos += SkVector::Make(-tan.fY, tan.fX) * baseline_offset;
163 
164         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;
168     }
169 
170     canvas->drawTextBlob(SkTextBlob::MakeFromRSXform(glyphs.get(), count * sizeof(SkGlyphID),
171                                          &xform[0], font, SkTextEncoding::kGlyphID),
172                          0, 0, paint);
173 
174     if (true) {
175         SkPaint p;
176         p.setStyle(SkPaint::kStroke_Style);
177         canvas->drawRect(bounds, p);
178     }
179 }
180 
make_shader()181 static sk_sp<SkShader> make_shader() {
182     SkPoint pts[2] = {{0, 0}, {220, 0}};
183     SkColor colors[2] = {SK_ColorRED, SK_ColorBLUE};
184     return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kMirror);
185 }
186 
drawTextPath(SkCanvas * canvas,bool doStroke)187 static void drawTextPath(SkCanvas* canvas, bool doStroke) {
188     const char text0[] = "ABCDFGHJKLMNOPQRSTUVWXYZ";
189     const int N = sizeof(text0) - 1;
190     SkPoint pos[N];
191 
192     SkFont font = ToolUtils::DefaultPortableFont();
193     font.setSize(100);
194 
195     SkPaint paint;
196     paint.setShader(make_shader());
197     paint.setAntiAlias(true);
198     if (doStroke) {
199         paint.setStyle(SkPaint::kStroke_Style);
200         paint.setStrokeWidth(2.25f);
201         paint.setStrokeJoin(SkPaint::kRound_Join);
202     }
203 
204     SkScalar x = 0;
205     for (int i = 0; i < N; ++i) {
206         pos[i].set(x, 0);
207         x += font.measureText(&text0[i], 1, SkTextEncoding::kUTF8, nullptr, &paint);
208     }
209 
210     SkPath path;
211     const float baseline_offset = -5;
212 
213     const SkPathDirection dirs[] = {
214         SkPathDirection::kCW, SkPathDirection::kCCW,
215     };
216     for (auto d : dirs) {
217         path.reset();
218         path.addOval(SkRect::MakeXYWH(160, 160, 540, 540), d);
219         draw_text_on_path(canvas, text0, N, pos, path, font, paint, baseline_offset);
220     }
221 
222     paint.reset();
223     paint.setStyle(SkPaint::kStroke_Style);
224     canvas->drawPath(path, paint);
225 }
226 
227 DEF_SIMPLE_GM(drawTextRSXform, canvas, 430, 860) {
228     canvas->scale(0.5f, 0.5f);
229     const bool doStroke[] = { false, true };
230     for (auto st : doStroke) {
231         drawTextPath(canvas, st);
232         canvas->translate(0, 860);
233     }
234 }
235 
236 // Exercise xform blob and its bounds
237 DEF_SIMPLE_GM(blob_rsxform, canvas, 500, 100) {
238     SkFont font = ToolUtils::DefaultPortableFont();
239     font.setSize(50);
240 
241     const char text[] = "CrazyXform";
242     constexpr size_t len = sizeof(text) - 1;
243 
244     SkRSXform xforms[len];
245     SkScalar scale = 1;
246     SkScalar x = 0, y = 0;
247     for (size_t i = 0; i < len; ++i) {
248         scale = SkScalarSin(i * SK_ScalarPI / (len-1)) * 0.75f + 0.5f;
249         xforms[i] = SkRSXform::Make(scale, 0, x, y);
250         x += 50 * scale;
251     }
252 
253     auto blob = SkTextBlob::MakeFromRSXform(text, len, xforms, font);
254 
255     SkPoint offset = { 20, 70 };
256     SkPaint paint;
257     paint.setColor(0xFFCCCCCC);
258     canvas->drawRect(blob->bounds().makeOffset(offset.fX, offset.fY), paint);
259     paint.setColor(SK_ColorBLACK);
260     canvas->drawTextBlob(blob, offset.fX, offset.fY, paint);
261 }
262 
263 // Exercise xform blob and its tight bounds
264 DEF_SIMPLE_GM(blob_rsxform_distortable, canvas, 500, 100) {
265     sk_sp<SkTypeface> typeface;
266     std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf"));
267     if (distortable) {
268         sk_sp<SkFontMgr> fm = ToolUtils::TestFontMgr();
269         const SkFontArguments::VariationPosition::Coordinate position[] = {
270             { SkSetFourByteTag('w','g','h','t'), 1.618033988749895f }
271         };
272         SkFontArguments params;
273         params.setVariationDesignPosition({position, std::size(position)});
274         typeface = fm->makeFromStream(std::move(distortable), params);
275     }
276     if (!typeface) {
277         typeface = ToolUtils::DefaultPortableTypeface();
278     }
279 
280     SkFont font(typeface, 50);
281 
282     const char text[] = "abcabcabc";
283     constexpr size_t len = sizeof(text) - 1;
284 
285     SkRSXform xforms[len];
286     SkScalar scale = 1;
287     SkScalar x = 0, y = 0;
288     for (size_t i = 0; i < len; ++i) {
289         scale = SkScalarSin(i * SK_ScalarPI / (len-1)) * 0.75f + 0.5f;
290         xforms[i] = SkRSXform::Make(scale, 0, x, y);
291         x += 50 * scale;
292     }
293 
294     auto blob = SkTextBlob::MakeFromRSXform(text, len, xforms, font);
295 
296     SkPoint offset = { 20, 70 };
297     SkPaint paint;
298     paint.setColor(0xFFCCCCCC);
299     canvas->drawRect(blob->bounds().makeOffset(offset.fX, offset.fY), paint);
300     paint.setColor(SK_ColorBLACK);
301     canvas->drawTextBlob(blob, offset.fX, offset.fY, paint);
302 }
303 
make_vertices(sk_sp<SkImage> image,const SkRect & r,SkColor color)304 static sk_sp<SkVertices> make_vertices(sk_sp<SkImage> image, const SkRect& r,
305                                        SkColor color) {
306     SkPoint pos[4];
307     r.toQuad(pos);
308     SkColor colors[4] = { color, color, color, color };
309     return SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, 4,
310                                 pos, pos, colors);
311 }
312 
313 /*
314  *  drawAtlas and drawVertices have several things in common:
315  *  - can create compound "shaders", combining texture and colors
316  *      - these are combined via an explicit blendmode
317  *  - like drawImage, they only respect parts of the paint
318  *      - colorfilter, imagefilter, blendmode, alpha
319  *
320  *  This GM produces a series of pairs of images (atlas | vertices).
321  *  Each pair should look the same, and each set shows a different combination
322  *  of alpha | colorFilter | mode
323  */
324 DEF_SIMPLE_GM(compare_atlas_vertices, canvas, 560, 585) {
325     const SkRect tex = SkRect::MakeWH(128, 128);
326     const SkRSXform xform = SkRSXform::Make(1, 0, 0, 0);
327     const SkColor color = 0x884488CC;
328 
329     auto image = ToolUtils::GetResourceAsImage("images/mandrill_128.png");
330     auto verts = make_vertices(image, tex, color);
331     const sk_sp<SkColorFilter> filters[] = {
332         nullptr,
333         SkColorFilters::Blend(0xFF00FF88, SkBlendMode::kModulate),
334     };
335     const SkBlendMode modes[] = {
336         SkBlendMode::kSrcOver,
337         SkBlendMode::kPlus,
338     };
339 
340     canvas->translate(10, 10);
341     SkPaint paint;
342     for (SkBlendMode mode : modes) {
343         for (float alpha : { 1.0f, 0.5f }) {
344             paint.setAlphaf(alpha);
345             canvas->save();
346             for (const sk_sp<SkColorFilter>& cf : filters) {
347                 paint.setColorFilter(cf);
348                 canvas->drawAtlas(image.get(), &xform, &tex, &color, 1, mode,
349                                   SkSamplingOptions(), &tex, &paint);
350                 canvas->translate(128, 0);
351                 paint.setShader(image->makeShader(SkSamplingOptions()));
352                 canvas->drawVertices(verts, mode, paint);
353                 paint.setShader(nullptr);
354                 canvas->translate(145, 0);
355             }
356             canvas->restore();
357             canvas->translate(0, 145);
358         }
359     }
360 }
361