xref: /aosp_15_r20/external/skia/src/gpu/graphite/render/SDFTextRenderStep.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/SDFTextRenderStep.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/base/SkEnumBitMask.h"
19 #include "src/core/SkSLTypeShared.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_graphite.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 #include <string_view>
39 
40 #if defined(SK_GAMMA_APPLY_TO_A8)
41 #include "include/private/base/SkCPUTypes.h"
42 #include "src/core/SkMaskGamma.h"
43 #include "src/text/gpu/DistanceFieldAdjustTable.h"
44 #endif
45 
46 namespace skgpu::graphite {
47 
48 namespace {
49 
50 // We are expecting to sample from up to 4 textures
51 constexpr int kNumSDFAtlasTextures = 4;
52 
53 }  // namespace
54 
SDFTextRenderStep()55 SDFTextRenderStep::SDFTextRenderStep()
56         : RenderStep("SDFTextRenderStep",
57                      "",
58                      Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage,
59                      /*uniforms=*/{{"subRunDeviceMatrix", SkSLType::kFloat4x4},
60                                    {"deviceToLocal", SkSLType::kFloat4x4},
61                                    {"atlasSizeInv", SkSLType::kFloat2},
62                                    {"gammaParams", SkSLType::kHalf2}},
63                      PrimitiveType::kTriangleStrip,
64                      kDirectDepthGEqualPass,
65                      /*vertexAttrs=*/ {},
66                      /*instanceAttrs=*/
67                      {{"size", VertexAttribType::kUShort2, SkSLType::kUShort2},
68                       {"uvPos", VertexAttribType::kUShort2, SkSLType::kUShort2},
69                       {"xyPos", VertexAttribType::kFloat2, SkSLType::kFloat2},
70                       {"indexAndFlags", VertexAttribType::kUShort2, SkSLType::kUShort2},
71                       {"strikeToSourceScale", VertexAttribType::kFloat, SkSLType::kFloat},
72                       {"depth", VertexAttribType::kFloat, SkSLType::kFloat},
73                       {"ssboIndices", VertexAttribType::kUInt2, SkSLType::kUInt2}},
74                      /*varyings=*/
75                      {{"unormTexCoords", SkSLType::kFloat2},
76                       {"textureCoords", SkSLType::kFloat2},
77                       {"texIndex", SkSLType::kFloat}}) {}
78 
~SDFTextRenderStep()79 SDFTextRenderStep::~SDFTextRenderStep() {}
80 
vertexSkSL() const81 std::string SDFTextRenderStep::vertexSkSL() const {
82     // Returns the body of a vertex function, which must define a float4 devPosition variable and
83     // must write to an already-defined float2 stepLocalCoords variable.
84     return "texIndex = half(indexAndFlags.x);"
85            "float4 devPosition = text_vertex_fn(float2(sk_VertexID >> 1, sk_VertexID & 1), "
86                                                "subRunDeviceMatrix, "
87                                                "deviceToLocal, "
88                                                "atlasSizeInv, "
89                                                "float2(size), "
90                                                "float2(uvPos), "
91                                                "xyPos, "
92                                                "strikeToSourceScale, "
93                                                "depth, "
94                                                "textureCoords, "
95                                                "unormTexCoords, "
96                                                "stepLocalCoords);";
97 }
98 
texturesAndSamplersSkSL(const ResourceBindingRequirements & bindingReqs,int * nextBindingIndex) const99 std::string SDFTextRenderStep::texturesAndSamplersSkSL(
100         const ResourceBindingRequirements& bindingReqs, int* nextBindingIndex) const {
101     std::string result;
102 
103     for (unsigned int i = 0; i < kNumSDFAtlasTextures; ++i) {
104         result += EmitSamplerLayout(bindingReqs, nextBindingIndex);
105         SkSL::String::appendf(&result, " sampler2D sdf_atlas_%u;\n", i);
106     }
107 
108     return result;
109 }
110 
fragmentCoverageSkSL() const111 const char* SDFTextRenderStep::fragmentCoverageSkSL() const {
112     // The returned SkSL must write its coverage into a 'half4 outputCoverage' variable (defined in
113     // the calling code) with the actual coverage splatted out into all four channels.
114 
115     // TODO: To minimize the number of shaders generated this is the full affine shader.
116     // For best performance it may be worth creating the uniform scale shader as well,
117     // as that's the most common case.
118     // TODO: Need to add 565 support.
119     // TODO: Need aliased and possibly sRGB support.
120     static_assert(kNumSDFAtlasTextures == 4);
121     return "outputCoverage = sdf_text_coverage_fn(sample_indexed_atlas(textureCoords, "
122                                                                       "int(texIndex), "
123                                                                       "sdf_atlas_0, "
124                                                                       "sdf_atlas_1, "
125                                                                       "sdf_atlas_2, "
126                                                                       "sdf_atlas_3).r, "
127                                                  "gammaParams, "
128                                                  "unormTexCoords);";
129 }
130 
writeVertices(DrawWriter * dw,const DrawParams & params,skvx::uint2 ssboIndices) const131 void SDFTextRenderStep::writeVertices(DrawWriter* dw,
132                                       const DrawParams& params,
133                                       skvx::uint2 ssboIndices) const {
134     const SubRunData& subRunData = params.geometry().subRunData();
135     subRunData.subRun()->vertexFiller().fillInstanceData(dw,
136                                                          subRunData.startGlyphIndex(),
137                                                          subRunData.glyphCount(),
138                                                          subRunData.subRun()->instanceFlags(),
139                                                          ssboIndices,
140                                                          subRunData.subRun()->glyphs(),
141                                                          params.order().depthAsFloat());
142 }
143 
writeUniformsAndTextures(const DrawParams & params,PipelineDataGatherer * gatherer) const144 void SDFTextRenderStep::writeUniformsAndTextures(const DrawParams& params,
145                                                  PipelineDataGatherer* gatherer) const {
146     SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());)
147 
148     const SubRunData& subRunData = params.geometry().subRunData();
149     unsigned int numProxies;
150     Recorder* recorder = subRunData.recorder();
151     const sk_sp<TextureProxy>* proxies =
152             recorder->priv().atlasProvider()->textAtlasManager()->getProxies(
153                     subRunData.subRun()->maskFormat(), &numProxies);
154     SkASSERT(proxies && numProxies > 0);
155 
156     // write uniforms
157     gatherer->write(params.transform().matrix());  // subRunDeviceMatrix
158     gatherer->write(subRunData.deviceToLocal());
159     SkV2 atlasDimensionsInverse = {1.f/proxies[0]->dimensions().width(),
160                                    1.f/proxies[0]->dimensions().height()};
161     gatherer->write(atlasDimensionsInverse);
162 
163     float gammaAdjustment = 0;
164     // TODO: generate LCD adjustment
165 #if defined(SK_GAMMA_APPLY_TO_A8)
166     auto dfAdjustTable = sktext::gpu::DistanceFieldAdjustTable::Get();
167     // TODO: don't do this for aliased text
168     U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
169                                                         subRunData.luminanceColor());
170     gammaAdjustment = dfAdjustTable->getAdjustment(lum, subRunData.useGammaCorrectDistanceTable());
171 #endif
172     SkV2 gammaParams = {gammaAdjustment, subRunData.useGammaCorrectDistanceTable() ? 1.f : 0.f};
173     gatherer->writeHalf(gammaParams);
174 
175     // write textures and samplers
176     for (unsigned int i = 0; i < numProxies; ++i) {
177         gatherer->add(proxies[i], {SkFilterMode::kLinear, SkTileMode::kClamp});
178     }
179     // If the atlas has less than 4 active proxies we still need to set up samplers for the shader.
180     for (unsigned int i = numProxies; i < kNumSDFAtlasTextures; ++i) {
181         gatherer->add(proxies[0], {SkFilterMode::kLinear, SkTileMode::kClamp});
182     }
183 }
184 
185 }  // namespace skgpu::graphite
186