Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Functions
drawatlas.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorFilter.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontMgr.h"
#include "include/core/SkFontTypes.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPath.h"
#include "include/core/SkPathMeasure.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRSXform.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkShader.h"
#include "include/core/SkSize.h"
#include "include/core/SkStream.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTextBlob.h"
#include "include/core/SkTileMode.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkTypes.h"
#include "include/core/SkVertices.h"
#include "include/effects/SkGradientShader.h"
#include "include/private/base/SkTemplates.h"
#include "src/base/SkAutoMalloc.h"
#include "src/core/SkFontPriv.h"
#include "tools/DecodeUtils.h"
#include "tools/Resources.h"
#include "tools/ToolUtils.h"
#include "tools/fonts/FontToolUtils.h"
#include <initializer_list>

Go to the source code of this file.

Classes

class  DrawAtlasGM
 

Functions

static void draw_text_on_path (SkCanvas *canvas, const void *text, size_t length, const SkPoint xy[], const SkPath &path, const SkFont &font, const SkPaint &paint, float baseline_offset)
 
static sk_sp< SkShadermake_shader ()
 
static void drawTextPath (SkCanvas *canvas, bool doStroke)
 
 DEF_SIMPLE_GM (drawTextRSXform, canvas, 430, 860)
 
 DEF_SIMPLE_GM (blob_rsxform, canvas, 500, 100)
 
 DEF_SIMPLE_GM (blob_rsxform_distortable, canvas, 500, 100)
 
static sk_sp< SkVerticesmake_vertices (sk_sp< SkImage > image, const SkRect &r, SkColor color)
 
 DEF_SIMPLE_GM (compare_atlas_vertices, canvas, 560, 585)
 

Function Documentation

◆ DEF_SIMPLE_GM() [1/4]

DEF_SIMPLE_GM ( blob_rsxform  ,
canvas  ,
500  ,
100   
)

Definition at line 237 of file drawatlas.cpp.

237 {
239 font.setSize(50);
240
241 const char text[] = "CrazyXform";
242 constexpr size_t len = sizeof(text) - 1;
243
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 };
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}
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
#define SkScalarSin(radians)
Definition SkScalar.h:45
#define SK_ScalarPI
Definition SkScalar.h:21
static sk_sp< SkTextBlob > MakeFromRSXform(const void *text, size_t byteLength, const SkRSXform xform[], const SkFont &font, SkTextEncoding encoding=SkTextEncoding::kUTF8)
const Paint & paint
float SkScalar
Definition extension.cpp:12
std::u16string text
double y
double x
PODArray< SkRSXform > xforms
Definition SkRecords.h:332
SkFont DefaultPortableFont()
font
Font Metadata and Metrics.
const Scalar scale
Point offset
static SkRSXform Make(SkScalar scos, SkScalar ssin, SkScalar tx, SkScalar ty)
Definition SkRSXform.h:24

◆ DEF_SIMPLE_GM() [2/4]

DEF_SIMPLE_GM ( blob_rsxform_distortable  ,
canvas  ,
500  ,
100   
)

Definition at line 264 of file drawatlas.cpp.

264 {
265 sk_sp<SkTypeface> typeface;
266 std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf"));
267 if (distortable) {
270 { SkSetFourByteTag('w','g','h','t'), 1.618033988749895f }
271 };
273 params.setVariationDesignPosition({position, std::size(position)});
274 typeface = fm->makeFromStream(std::move(distortable), params);
275 }
276 if (!typeface) {
278 }
279
280 SkFont font(typeface, 50);
281
282 const char text[] = "abcabcabc";
283 constexpr size_t len = sizeof(text) - 1;
284
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 };
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}
std::unique_ptr< SkStreamAsset > GetResourceAsStream(const char *resource, bool useFileStream)
Definition Resources.cpp:31
static constexpr SkFourByteTag SkSetFourByteTag(char a, char b, char c, char d)
Definition SkTypes.h:167
const EmbeddedViewParams * params
sk_sp< SkTypeface > DefaultPortableTypeface()
sk_sp< SkFontMgr > TestFontMgr()
SkFontArguments & setVariationDesignPosition(VariationPosition position)

◆ DEF_SIMPLE_GM() [3/4]

DEF_SIMPLE_GM ( compare_atlas_vertices  ,
canvas  ,
560  ,
585   
)

Definition at line 324 of file drawatlas.cpp.

324 {
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,
334 };
335 const SkBlendMode modes[] = {
338 };
339
340 canvas->translate(10, 10);
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);
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}
SkColor4f color
SkBlendMode
Definition SkBlendMode.h:38
@ kPlus
r = min(s + d, 1)
@ kModulate
r = s*d
@ kSrcOver
r = s + (1-sa)*d
uint32_t SkColor
Definition SkColor.h:37
static sk_sp< SkColorFilter > Blend(const SkColor4f &c, sk_sp< SkColorSpace >, SkBlendMode mode)
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition SkImage.cpp:179
T * get() const
Definition SkRefCnt.h:303
static sk_sp< SkVertices > make_vertices(sk_sp< SkImage > image, const SkRect &r, SkColor color)
sk_sp< SkImage > image
Definition examples.cpp:29
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition DecodeUtils.h:25
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609

◆ DEF_SIMPLE_GM() [4/4]

DEF_SIMPLE_GM ( drawTextRSXform  ,
canvas  ,
430  ,
860   
)

Definition at line 227 of file drawatlas.cpp.

227 {
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}
static void drawTextPath(SkCanvas *canvas, bool doStroke)

◆ draw_text_on_path()

static void draw_text_on_path ( SkCanvas canvas,
const void *  text,
size_t  length,
const SkPoint  xy[],
const SkPath path,
const SkFont font,
const SkPaint paint,
float  baseline_offset 
)
static

Definition at line 132 of file drawatlas.cpp.

134 {
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
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
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
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}
uint16_t glyphs[5]
int count
SkPoint pos
@ kUTF8
uses bytes to represent UTF-8 or ASCII
@ kGlyphID
uses two byte words to represent glyph indices
#define SkScalarHalf(a)
Definition SkScalar.h:75
#define SkScalarAbs(x)
Definition SkScalar.h:39
uint16_t SkGlyphID
Definition SkTypes.h:179
const SkScalar widths[]
void drawRect(const SkRect &rect, const SkPaint &paint)
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
static SkRect GetFontBounds(const SkFont &)
Definition SkFont.cpp:354
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
static float max(float r, float g, float b)
Definition hsl.cpp:49
size_t length
Optional< SkRect > bounds
Definition SkRecords.h:189
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition switches.h:57
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition switches.h:259
float fX
x-axis value
static constexpr SkPoint Make(float x, float y)
void set(float x, float y)
float fY
y-axis value
constexpr float y() const
constexpr float x() const
SkScalar fTy
Definition SkRSXform.h:45
SkScalar fSCos
Definition SkRSXform.h:42
SkScalar fTx
Definition SkRSXform.h:44
SkScalar fSSin
Definition SkRSXform.h:43
SkScalar fBottom
larger y-axis bounds
Definition extension.cpp:17
SkScalar fLeft
smaller x-axis bounds
Definition extension.cpp:14
SkScalar fRight
larger x-axis bounds
Definition extension.cpp:16
SkScalar fTop
smaller y-axis bounds
Definition extension.cpp:15

◆ drawTextPath()

static void drawTextPath ( SkCanvas canvas,
bool  doStroke 
)
static

Definition at line 187 of file drawatlas.cpp.

187 {
188 const char text0[] = "ABCDFGHJKLMNOPQRSTUVWXYZ";
189 const int N = sizeof(text0) - 1;
190 SkPoint pos[N];
191
193 font.setSize(100);
194
196 paint.setShader(make_shader());
197 paint.setAntiAlias(true);
198 if (doStroke) {
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[] = {
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();
224 canvas->drawPath(path, paint);
225}
SkPathDirection
Definition SkPathTypes.h:34
#define N
Definition beziers.cpp:19
void drawPath(const SkPath &path, const SkPaint &paint)
@ kRound_Join
adds circle
Definition SkPaint.h:360
static void draw_text_on_path(SkCanvas *canvas, const void *text, size_t length, const SkPoint xy[], const SkPath &path, const SkFont &font, const SkPaint &paint, float baseline_offset)
static sk_sp< SkShader > make_shader()
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659

◆ make_shader()

static sk_sp< SkShader > make_shader ( )
static

Definition at line 181 of file drawatlas.cpp.

181 {
182 SkPoint pts[2] = {{0, 0}, {220, 0}};
184 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kMirror);
185}
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
PODArray< SkColor > colors
Definition SkRecords.h:276

◆ make_vertices()

static sk_sp< SkVertices > make_vertices ( sk_sp< SkImage image,
const SkRect r,
SkColor  color 
)
static

Definition at line 304 of file drawatlas.cpp.

305 {
306 SkPoint pos[4];
307 r.toQuad(pos);
308 SkColor colors[4] = { color, color, color, color };
310 pos, pos, colors);
311}
static sk_sp< SkVertices > MakeCopy(VertexMode mode, int vertexCount, const SkPoint positions[], const SkPoint texs[], const SkColor colors[], int indexCount, const uint16_t indices[])
@ kTriangleFan_VertexMode
Definition SkVertices.h:33
void toQuad(SkPoint quad[4]) const
Definition SkRect.cpp:50