Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
rsxtext.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 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"
15#include "tools/ToolUtils.h"
17
18// Exercises RSX text blobs + shader with various local matrix combinations.
19// Yellow grid should stay aligned for text vs. background.
20class RSXShaderGM : public skiagm::GM {
21public:
22private:
23 SkString getName() const override { return SkString("rsx_blob_shader"); }
24
25 SkISize getISize() override { return SkISize::Make(kSZ * kScale * 2.1f, kSZ * kScale * 2.1f); }
26
27 void onOnceBeforeDraw() override {
31 SkFont font(ToolUtils::CreatePortableTypeface("Sans", style), kFontSZ);
32 font.setEdging(SkFont::Edging::kAntiAlias);
33
34 static constexpr char txt[] = "TEST";
35 SkGlyphID glyphs[16];
36 float widths[16];
37 const auto glyph_count = font.textToGlyphs(txt, strlen(txt), SkTextEncoding::kUTF8,
38 glyphs, std::size(glyphs));
39 font.getWidths(glyphs, glyph_count, widths);
40
41 SkTextBlobBuilder builder;
42 const auto& buf = builder.allocRunRSXform(font, glyph_count);
43 std::copy(glyphs, glyphs + glyph_count, buf.glyphs);
44
45 float x = 0;
46 for (int i = 0; i < glyph_count; ++i) {
47 buf.xforms()[i] = {
48 1, 0,
49 x, 0,
50 };
51 x += widths[i];
52 }
53
54 fBlob = builder.make();
55 }
56
57 void onDraw(SkCanvas* canvas) override {
58 canvas->scale(kScale, kScale);
59 this->draw_one(canvas,
60 {0, 0}, SkMatrix::I(), SkMatrix::I());
61 this->draw_one(canvas,
62 {kSZ*1.1f, 0}, SkMatrix::Scale(2, 2), SkMatrix::I());
63 this->draw_one(canvas,
64 {0, kSZ*1.1f}, SkMatrix::I(), SkMatrix::RotateDeg(45));
65 this->draw_one(canvas,
66 {kSZ*1.1f, kSZ*1.1f}, SkMatrix::Scale(2, 2), SkMatrix::RotateDeg(45));
67 }
68
69 void draw_one(SkCanvas* canvas, SkPoint pos, const SkMatrix& lm,
70 const SkMatrix& outer_lm) const {
71 SkAutoCanvasRestore acr(canvas, true);
72 canvas->translate(pos.fX, pos.fY);
73
74 SkPaint p;
75 p.setShader(make_shader(lm, outer_lm));
76 p.setAlphaf(0.75f);
77 canvas->drawRect(SkRect::MakeWH(kSZ, kSZ), p);
78
79 p.setAlphaf(1);
80 canvas->drawTextBlob(fBlob, 0, kFontSZ*1, p);
81 canvas->drawTextBlob(fBlob, 0, kFontSZ*2, p);
82 }
83
84 static sk_sp<SkShader> make_shader(const SkMatrix& lm, const SkMatrix& outer_lm) {
85 static constexpr SkISize kTileSize = { 30, 30 };
87 SkImageInfo::MakeN32Premul(kTileSize.width(), kTileSize.height()));
88
89 SkPaint p;
90 p.setColor(0xffffff00);
91 surface->getCanvas()->drawPaint(p);
92 p.setColor(0xff008000);
93 surface->getCanvas()
94 ->drawRect({0, 0, kTileSize.width()*0.9f, kTileSize.height()*0.9f}, p);
95
96 return surface->makeImageSnapshot()
99 ->makeWithLocalMatrix(outer_lm);
100 }
101
102 inline static constexpr float kSZ = 300,
103 kFontSZ = kSZ * 0.38,
104 kScale = 1.4f;
105
106 sk_sp<SkTextBlob> fBlob;
107};
108
109DEF_GM(return new RSXShaderGM;)
static sk_sp< SkShader > make_shader()
uint16_t glyphs[5]
SkPoint pos
@ kUTF8
uses bytes to represent UTF-8 or ASCII
uint16_t SkGlyphID
Definition SkTypes.h:179
const SkScalar widths[]
void onOnceBeforeDraw() override
Definition rsxtext.cpp:27
SkString getName() const override
Definition rsxtext.cpp:23
void onDraw(SkCanvas *canvas) override
Definition rsxtext.cpp:57
SkISize getISize() override
Definition rsxtext.cpp:25
void drawRect(const SkRect &rect, const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
void scale(SkScalar sx, SkScalar sy)
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
@ kAntiAlias
may have transparent pixels on glyph edges
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition SkMatrix.h:75
static SkMatrix RotateDeg(SkScalar deg)
Definition SkMatrix.h:104
static const SkMatrix & I()
VkSurfaceKHR surface
Definition main.cc:49
#define DEF_GM(CODE)
Definition gm.h:40
double x
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
sk_sp< SkTypeface > CreatePortableTypeface(const char *name, SkFontStyle style)
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
constexpr int32_t width() const
Definition SkSize.h:36
constexpr int32_t height() const
Definition SkSize.h:37
static SkImageInfo MakeN32Premul(int width, int height)
float fX
x-axis value
float fY
y-axis value
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609