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
8 #include "src/gpu/graphite/render/BitmapTextRenderStep.h"
9
10 #include "include/core/SkM44.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkSamplingOptions.h"
13 #include "include/core/SkSize.h"
14 #include "include/core/SkTileMode.h"
15 #include "include/gpu/graphite/Recorder.h"
16 #include "include/private/base/SkAssert.h"
17 #include "include/private/base/SkDebug.h"
18 #include "src/core/SkSLTypeShared.h"
19 #include "src/gpu/AtlasTypes.h"
20 #include "src/gpu/graphite/AtlasProvider.h"
21 #include "src/gpu/graphite/Attribute.h"
22 #include "src/gpu/graphite/ContextUtils.h"
23 #include "src/gpu/graphite/DrawOrder.h"
24 #include "src/gpu/graphite/DrawParams.h"
25 #include "src/gpu/graphite/DrawTypes.h"
26 #include "src/gpu/graphite/PipelineData.h"
27 #include "src/gpu/graphite/RecorderPriv.h"
28 #include "src/gpu/graphite/TextureProxy.h"
29 #include "src/gpu/graphite/geom/Geometry.h"
30 #include "src/gpu/graphite/geom/SubRunData.h"
31 #include "src/gpu/graphite/geom/Transform.h"
32 #include "src/gpu/graphite/render/CommonDepthStencilSettings.h"
33 #include "src/gpu/graphite/text/TextAtlasManager.h"
34 #include "src/sksl/SkSLString.h"
35 #include "src/text/gpu/SubRunContainer.h"
36 #include "src/text/gpu/VertexFiller.h"
37
38 using AtlasSubRun = sktext::gpu::AtlasSubRun;
39
40 namespace skgpu::graphite {
41
42 namespace {
43
44 // We are expecting to sample from up to 4 textures
45 constexpr int kNumTextAtlasTextures = 4;
46
variant_id(skgpu::MaskFormat variant)47 RenderStep::RenderStepID variant_id(skgpu::MaskFormat variant) {
48 switch (variant) {
49 case skgpu::MaskFormat::kA8: return RenderStep::RenderStepID::kBitmapText_Mask;
50 case skgpu::MaskFormat::kA565: return RenderStep::RenderStepID::kBitmapText_LCD;
51 case skgpu::MaskFormat::kARGB: return RenderStep::RenderStepID::kBitmapText_Color;
52 }
53
54 SkUNREACHABLE;
55 }
56
57 } // namespace
58
BitmapTextRenderStep(skgpu::MaskFormat variant)59 BitmapTextRenderStep::BitmapTextRenderStep(skgpu::MaskFormat variant)
60 : RenderStep(variant_id(variant),
61 Flags(variant),
62 /*uniforms=*/{{"subRunDeviceMatrix", SkSLType::kFloat4x4},
63 {"deviceToLocal" , SkSLType::kFloat4x4},
64 {"atlasSizeInv" , SkSLType::kFloat2}},
65 PrimitiveType::kTriangleStrip,
66 kDirectDepthGEqualPass,
67 /*vertexAttrs=*/ {},
68 /*instanceAttrs=*/
69 {{"size", VertexAttribType::kUShort2, SkSLType::kUShort2},
70 {"uvPos", VertexAttribType::kUShort2, SkSLType::kUShort2},
71 {"xyPos", VertexAttribType::kFloat2, SkSLType::kFloat2},
72 {"indexAndFlags", VertexAttribType::kUShort2, SkSLType::kUShort2},
73 {"strikeToSourceScale", VertexAttribType::kFloat, SkSLType::kFloat},
74 {"depth", VertexAttribType::kFloat, SkSLType::kFloat},
75 {"ssboIndices", VertexAttribType::kUInt2, SkSLType::kUInt2}},
76 /*varyings=*/
77 {{"textureCoords", SkSLType::kFloat2},
78 {"texIndex", SkSLType::kHalf},
79 {"maskFormat", SkSLType::kHalf}}) {}
80
~BitmapTextRenderStep()81 BitmapTextRenderStep::~BitmapTextRenderStep() {}
82
Flags(skgpu::MaskFormat variant)83 SkEnumBitMask<RenderStep::Flags> BitmapTextRenderStep::Flags(skgpu::MaskFormat variant) {
84 switch (variant) {
85 case skgpu::MaskFormat::kA8:
86 return Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage;
87 case skgpu::MaskFormat::kA565:
88 return Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage |
89 Flags::kLCDCoverage;
90 case skgpu::MaskFormat::kARGB:
91 return Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsPrimitiveColor;
92 default:
93 SkUNREACHABLE;
94 }
95 }
96
vertexSkSL() const97 std::string BitmapTextRenderStep::vertexSkSL() const {
98 // Returns the body of a vertex function, which must define a float4 devPosition variable and
99 // must write to an already-defined float2 stepLocalCoords variable.
100 return "texIndex = half(indexAndFlags.x);"
101 "maskFormat = half(indexAndFlags.y);"
102 "float2 unormTexCoords;"
103 "float4 devPosition = text_vertex_fn(float2(sk_VertexID >> 1, sk_VertexID & 1), "
104 "subRunDeviceMatrix, "
105 "deviceToLocal, "
106 "atlasSizeInv, "
107 "float2(size), "
108 "float2(uvPos), "
109 "xyPos, "
110 "strikeToSourceScale, "
111 "depth, "
112 "textureCoords, "
113 "unormTexCoords, "
114 "stepLocalCoords);";
115 }
116
texturesAndSamplersSkSL(const ResourceBindingRequirements & bindingReqs,int * nextBindingIndex) const117 std::string BitmapTextRenderStep::texturesAndSamplersSkSL(
118 const ResourceBindingRequirements& bindingReqs, int* nextBindingIndex) const {
119 std::string result;
120
121 for (unsigned int i = 0; i < kNumTextAtlasTextures; ++i) {
122 result += EmitSamplerLayout(bindingReqs, nextBindingIndex);
123 SkSL::String::appendf(&result, " sampler2D text_atlas_%u;\n", i);
124 }
125
126 return result;
127 }
128
129
fragmentColorSkSL() const130 const char* BitmapTextRenderStep::fragmentColorSkSL() const {
131 // The returned SkSL must write its color into a 'half4 primitiveColor' variable
132 // (defined in the calling code).
133 static_assert(kNumTextAtlasTextures == 4);
134 return "primitiveColor = sample_indexed_atlas(textureCoords, "
135 "int(texIndex), "
136 "text_atlas_0, "
137 "text_atlas_1, "
138 "text_atlas_2, "
139 "text_atlas_3);";
140 }
141
fragmentCoverageSkSL() const142 const char* BitmapTextRenderStep::fragmentCoverageSkSL() const {
143 // The returned SkSL must write its coverage into a 'half4 outputCoverage' variable (defined in
144 // the calling code) with the actual coverage splatted out into all four channels.
145 static_assert(kNumTextAtlasTextures == 4);
146 return "outputCoverage = bitmap_text_coverage_fn(sample_indexed_atlas(textureCoords, "
147 "int(texIndex), "
148 "text_atlas_0, "
149 "text_atlas_1, "
150 "text_atlas_2, "
151 "text_atlas_3), "
152 "int(maskFormat));";
153 }
154
writeVertices(DrawWriter * dw,const DrawParams & params,skvx::uint2 ssboIndices) const155 void BitmapTextRenderStep::writeVertices(DrawWriter* dw,
156 const DrawParams& params,
157 skvx::uint2 ssboIndices) const {
158 const SubRunData& subRunData = params.geometry().subRunData();
159
160 subRunData.subRun()->vertexFiller().fillInstanceData(dw,
161 subRunData.startGlyphIndex(),
162 subRunData.glyphCount(),
163 subRunData.subRun()->instanceFlags(),
164 ssboIndices,
165 subRunData.subRun()->glyphs(),
166 params.order().depthAsFloat());
167 }
168
writeUniformsAndTextures(const DrawParams & params,PipelineDataGatherer * gatherer) const169 void BitmapTextRenderStep::writeUniformsAndTextures(const DrawParams& params,
170 PipelineDataGatherer* gatherer) const {
171 SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());)
172
173 const SubRunData& subRunData = params.geometry().subRunData();
174 unsigned int numProxies;
175 Recorder* recorder = subRunData.recorder();
176 const sk_sp<TextureProxy>* proxies =
177 recorder->priv().atlasProvider()->textAtlasManager()->getProxies(
178 subRunData.subRun()->maskFormat(), &numProxies);
179 SkASSERT(proxies && numProxies > 0);
180
181 // write uniforms
182 gatherer->write(params.transform().matrix()); // subRunDeviceMatrix
183 gatherer->write(subRunData.deviceToLocal());
184 SkV2 atlasDimensionsInverse = {1.f/proxies[0]->dimensions().width(),
185 1.f/proxies[0]->dimensions().height()};
186 gatherer->write(atlasDimensionsInverse);
187
188 // write textures and samplers
189 for (unsigned int i = 0; i < numProxies; ++i) {
190 gatherer->add(proxies[i], {SkFilterMode::kNearest, SkTileMode::kClamp});
191 }
192 // If the atlas has less than 4 active proxies we still need to set up samplers for the shader.
193 for (unsigned int i = numProxies; i < kNumTextAtlasTextures; ++i) {
194 gatherer->add(proxies[0], {SkFilterMode::kNearest, SkTileMode::kClamp});
195 }
196 }
197
198 } // namespace skgpu::graphite
199