Flutter Engine
The Flutter Engine
BitmapTextRenderStep.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2022 Google LLC
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
9
10#include "include/core/SkM44.h"
20#include "src/sksl/SkSLString.h"
23
25
26namespace skgpu::graphite {
27
28namespace {
29
30// We are expecting to sample from up to 4 textures
31constexpr int kNumTextAtlasTextures = 4;
32
33} // namespace
34
36 : RenderStep("BitmapTextRenderStep",
37 "",
38 isLCD ? Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage |
39 Flags::kLCDCoverage
40 : Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage,
41 /*uniforms=*/{{"subRunDeviceMatrix", SkSLType::kFloat4x4},
42 {"deviceToLocal" , SkSLType::kFloat4x4},
43 {"atlasSizeInv" , SkSLType::kFloat2}},
46 /*vertexAttrs=*/ {},
47 /*instanceAttrs=*/
52 {"strikeToSourceScale", VertexAttribType::kFloat, SkSLType::kFloat},
55 /*varyings=*/
56 {{"textureCoords", SkSLType::kFloat2},
57 {"texIndex", SkSLType::kHalf},
58 {"maskFormat", SkSLType::kHalf}}) {}
59
61
63 // Returns the body of a vertex function, which must define a float4 devPosition variable and
64 // must write to an already-defined float2 stepLocalCoords variable.
65 return "texIndex = half(indexAndFlags.x);"
66 "maskFormat = half(indexAndFlags.y);"
67 "float2 unormTexCoords;"
68 "float4 devPosition = text_vertex_fn(float2(sk_VertexID >> 1, sk_VertexID & 1), "
69 "subRunDeviceMatrix, "
70 "deviceToLocal, "
71 "atlasSizeInv, "
72 "float2(size), "
73 "float2(uvPos), "
74 "xyPos, "
75 "strikeToSourceScale, "
76 "depth, "
77 "textureCoords, "
78 "unormTexCoords, "
79 "stepLocalCoords);";
80}
81
83 const ResourceBindingRequirements& bindingReqs, int* nextBindingIndex) const {
84 std::string result;
85
86 for (unsigned int i = 0; i < kNumTextAtlasTextures; ++i) {
87 result += EmitSamplerLayout(bindingReqs, nextBindingIndex);
88 SkSL::String::appendf(&result, " sampler2D text_atlas_%u;\n", i);
89 }
90
91 return result;
92}
93
95 // The returned SkSL must write its coverage into a 'half4 outputCoverage' variable (defined in
96 // the calling code) with the actual coverage splatted out into all four channels.
97 static_assert(kNumTextAtlasTextures == 4);
98 return "outputCoverage = bitmap_text_coverage_fn(sample_indexed_atlas(textureCoords, "
99 "int(texIndex), "
100 "text_atlas_0, "
101 "text_atlas_1, "
102 "text_atlas_2, "
103 "text_atlas_3), "
104 "int(maskFormat));";
105}
106
108 const DrawParams& params,
109 skvx::ushort2 ssboIndices) const {
110 const SubRunData& subRunData = params.geometry().subRunData();
111
112 subRunData.subRun()->vertexFiller().fillInstanceData(dw,
113 subRunData.startGlyphIndex(),
114 subRunData.glyphCount(),
115 subRunData.subRun()->instanceFlags(),
116 ssboIndices,
117 subRunData.subRun()->glyphs(),
118 params.order().depthAsFloat());
119}
120
122 PipelineDataGatherer* gatherer) const {
123 SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());)
124
125 const SubRunData& subRunData = params.geometry().subRunData();
126 unsigned int numProxies;
127 Recorder* recorder = subRunData.recorder();
128 const sk_sp<TextureProxy>* proxies =
130 subRunData.subRun()->maskFormat(), &numProxies);
131 SkASSERT(proxies && numProxies > 0);
132
133 // write uniforms
134 gatherer->write(params.transform().matrix()); // subRunDeviceMatrix
135 gatherer->write(subRunData.deviceToLocal());
136 SkV2 atlasDimensionsInverse = {1.f/proxies[0]->dimensions().width(),
137 1.f/proxies[0]->dimensions().height()};
138 gatherer->write(atlasDimensionsInverse);
139
140 // write textures and samplers
141 const SkSamplingOptions kSamplingOptions(SkFilterMode::kNearest);
142 constexpr SkTileMode kTileModes[2] = { SkTileMode::kClamp, SkTileMode::kClamp };
143 for (unsigned int i = 0; i < numProxies; ++i) {
144 gatherer->add(proxies[i], {kSamplingOptions, kTileModes});
145 }
146 // If the atlas has less than 4 active proxies we still need to set up samplers for the shader.
147 for (unsigned int i = numProxies; i < kNumTextAtlasTextures; ++i) {
148 gatherer->add(proxies[0], {kSamplingOptions, kTileModes});
149 }
150}
151
152} // namespace skgpu::graphite
sktext::gpu::AtlasSubRun AtlasSubRun
#define SkASSERT(cond)
Definition: SkAssert.h:116
static bool isLCD(const SkScalerContextRec &rec)
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()
SkTileMode
Definition: SkTileMode.h:13
TextAtlasManager * textAtlasManager() const
Definition: AtlasProvider.h:53
const char * fragmentCoverageSkSL() const override
void writeUniformsAndTextures(const DrawParams &, PipelineDataGatherer *) const override
void writeVertices(DrawWriter *, const DrawParams &, skvx::ushort2 ssboIndices) const override
std::string texturesAndSamplersSkSL(const ResourceBindingRequirements &, int *nextBindingIndex) const override
std::string vertexSkSL() const override
AtlasProvider * atlasProvider()
Definition: RecorderPriv.h:61
SkSpan< const Uniform > uniforms() const
Definition: Renderer.h:143
const sktext::gpu::AtlasSubRun * subRun() const
Definition: SubRunData.h:79
const sk_sp< TextureProxy > * getProxies(MaskFormat format, unsigned int *numActiveProxies)
virtual const VertexFiller & vertexFiller() const =0
virtual unsigned short instanceFlags() const =0
virtual SkSpan< const Glyph * > glyphs() const =0
void fillInstanceData(skgpu::graphite::DrawWriter *dw, int offset, int count, unsigned short flags, skvx::ushort2 ssboIndex, SkSpan< const Glyph * > glyphs, SkScalar depth) const
const EmbeddedViewParams * params
GAsyncResult * result
std::string void appendf(std::string *str, const char *fmt,...) SK_PRINTF_LIKE(2
Definition: SkSLString.cpp:92
static constexpr DepthStencilSettings kDirectDepthGEqualPass
std::string EmitSamplerLayout(const ResourceBindingRequirements &bindingReqs, int *binding)
Definition: SkM44.h:19
Definition: SkVx.h:83