Flutter Engine
The Flutter Engine
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);
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,
39 font.getWidths(glyphs, glyph_count, widths);
40
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;)
uint16_t glyphs[5]
Definition: FontMgrTest.cpp:46
SkPoint pos
@ kUTF8
uses bytes to represent UTF-8 or ASCII
static void copy(void *dst, const uint8_t *src, int width, int bpp, int deltaSrc, int offset, const SkPMColor ctable[])
Definition: SkSwizzler.cpp:31
uint16_t SkGlyphID
Definition: SkTypes.h:179
const SkScalar widths[]
Definition: StrokerTest.cpp:39
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2484
@ kExtraBlack_Weight
Definition: SkFontStyle.h:29
Definition: SkFont.h:35
@ 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()
Definition: SkMatrix.cpp:1544
Definition: gm.h:110
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)
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
font
Font Metadata and Metrics.
SkSamplingOptions(SkFilterMode::kLinear))
Definition: SkSize.h:16
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
Definition: SkPoint_impl.h:164
float fY
y-axis value
Definition: SkPoint_impl.h:165
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609