| 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 |
|
| 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 |
|
| 59 |
BitmapTextRenderStep::BitmapTextRenderStep(Layout layout, skgpu::MaskFormat variant) |
| 60 |
: RenderStep(layout, |
| 61 |
variant_id(variant), |
| 62 |
Flags(variant) | Flags::kAppendInstances, |
| 63 |
/*uniforms=*/{{"subRunDeviceMatrix", SkSLType::kFloat4x4}, |
| 64 |
{"deviceToLocal" , SkSLType::kFloat4x4}, |
| 65 |
{"atlasSizeInv" , SkSLType::kFloat2}}, |
| 66 |
PrimitiveType::kTriangleStrip, |
| 67 |
kDirectDepthLEqualPass, |
| 68 |
/*staticAttrs=*/ {}, |
| 69 |
/*appendAttrs=*/ |
| 70 |
{{{"size", VertexAttribType::kUShort2, SkSLType::kUShort2}, |
| 71 |
{"uvPos", VertexAttribType::kUShort2, SkSLType::kUShort2}, |
| 72 |
{"xyPos", VertexAttribType::kFloat2, SkSLType::kFloat2}, |
| 73 |
{"indexAndFlags", VertexAttribType::kUShort2, SkSLType::kUShort2}, |
| 74 |
{"strikeToSourceScale", VertexAttribType::kFloat, SkSLType::kFloat}, |
| 75 |
{"depth", VertexAttribType::kFloat, SkSLType::kFloat}, |
| 76 |
{"ssboIndex", VertexAttribType::kUInt, SkSLType::kUInt}}}, |
| 77 |
/*varyings=*/ |
| 78 |
{{{"textureCoords", SkSLType::kFloat2}, |
| 79 |
{"texIndex", SkSLType::kHalf}, |
| 80 |
{"maskFormat", SkSLType::kHalf}}}) {} |
| 81 |
|
| 82 |
BitmapTextRenderStep::~BitmapTextRenderStep() {} |
| 83 |
|
| 84 |
SkEnumBitMask<RenderStep::Flags> BitmapTextRenderStep::Flags(skgpu::MaskFormat variant) { |
| 85 |
switch (variant) { |
| 86 |
case skgpu::MaskFormat::kA8: |
| 87 |
return Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage; |
| 88 |
case skgpu::MaskFormat::kA565: |
| 89 |
return Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage | |
| 90 |
Flags::kLCDCoverage; |
| 91 |
case skgpu::MaskFormat::kARGB: |
| 92 |
return Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsPrimitiveColor; |
| 93 |
default: |
| 94 |
SkUNREACHABLE; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
std::string BitmapTextRenderStep::vertexSkSL() const { |
| 99 |
// Returns the body of a vertex function, which must define a float4 devPosition variable and |
| 100 |
// must write to an already-defined float2 stepLocalCoords variable. |
| 101 |
return "texIndex = half(indexAndFlags.x);" |
| 102 |
"maskFormat = half(indexAndFlags.y);" |
| 103 |
"float2 unormTexCoords;" |
| 104 |
"float4 devPosition = text_vertex_fn(float2(sk_VertexID >> 1, sk_VertexID & 1), " |
| 105 |
"subRunDeviceMatrix, " |
| 106 |
"deviceToLocal, " |
| 107 |
"atlasSizeInv, " |
| 108 |
"float2(size), " |
| 109 |
"float2(uvPos), " |
| 110 |
"xyPos, " |
| 111 |
"strikeToSourceScale, " |
| 112 |
"depth, " |
| 113 |
"textureCoords, " |
| 114 |
"unormTexCoords, " |
| 115 |
"stepLocalCoords);"; |
| 116 |
} |
| 117 |
|
| 118 |
std::string BitmapTextRenderStep::texturesAndSamplersSkSL( |
| 119 |
const ResourceBindingRequirements& bindingReqs, int* nextBindingIndex) const { |
| 120 |
std::string result; |
| 121 |
|
| 122 |
for (unsigned int i = 0; i < kNumTextAtlasTextures; ++i) { |
| 123 |
result += EmitSamplerLayout(bindingReqs, nextBindingIndex); |
| 124 |
SkSL::String::appendf(&result, " sampler2D text_atlas_%u;\n", i); |
| 125 |
} |
| 126 |
|
| 127 |
return result; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
const char* BitmapTextRenderStep::fragmentColorSkSL() const { |
| 132 |
// The returned SkSL must write its color into a 'half4 primitiveColor' variable |
| 133 |
// (defined in the calling code). |
| 134 |
static_assert(kNumTextAtlasTextures == 4); |
| 135 |
return "primitiveColor = sample_indexed_atlas(textureCoords, " |
| 136 |
"int(texIndex), " |
| 137 |
"text_atlas_0, " |
| 138 |
"text_atlas_1, " |
| 139 |
"text_atlas_2, " |
| 140 |
"text_atlas_3);"; |
| 141 |
} |
| 142 |
|
| 143 |
const char* BitmapTextRenderStep::fragmentCoverageSkSL() const { |
| 144 |
// The returned SkSL must write its coverage into a 'half4 outputCoverage' variable (defined in |
| 145 |
// the calling code) with the actual coverage splatted out into all four channels. |
| 146 |
static_assert(kNumTextAtlasTextures == 4); |
| 147 |
return "outputCoverage = bitmap_text_coverage_fn(sample_indexed_atlas(textureCoords, " |
| 148 |
"int(texIndex), " |
| 149 |
"text_atlas_0, " |
| 150 |
"text_atlas_1, " |
| 151 |
"text_atlas_2, " |
| 152 |
"text_atlas_3), " |
| 153 |
"int(maskFormat));"; |
| 154 |
} |
| 155 |
|
| 156 |
bool BitmapTextRenderStep::usesUniformsInFragmentSkSL() const { return false; } |
| 157 |
|
| 158 |
void BitmapTextRenderStep::writeVertices(DrawWriter* dw, |
| 159 |
const DrawParams& params, |
| 160 |
uint32_t ssboIndex) const { |
| 161 |
const SubRunData& subRunData = params.geometry().subRunData(); |
| 162 |
|
| 163 |
subRunData.subRun()->vertexFiller().fillInstanceData(dw, |
| 164 |
subRunData.startGlyphIndex(), |
| 165 |
subRunData.glyphCount(), |
| 166 |
subRunData.subRun()->instanceFlags(), |
| 167 |
ssboIndex, |
| 168 |
subRunData.subRun()->glyphs(), |
| 169 |
params.order().depthAsFloat()); |
| 170 |
} |
| 171 |
|
| 172 |
void BitmapTextRenderStep::writeUniformsAndTextures(const DrawParams& params, |
| 173 |
PipelineDataGatherer* gatherer) const { |
| 174 |
SkDEBUGCODE(gatherer->checkRewind()); |
| 175 |
SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());) |
| 176 |
|
| 177 |
const SubRunData& subRunData = params.geometry().subRunData(); |
| 178 |
unsigned int numProxies; |
| 179 |
Recorder* recorder = subRunData.recorder(); |
| 180 |
const sk_sp<TextureProxy>* proxies = |
| 181 |
recorder->priv().atlasProvider()->textAtlasManager()->getProxies( |
| 182 |
subRunData.subRun()->maskFormat(), &numProxies); |
| 183 |
SkASSERT(proxies && numProxies > 0); |
| 184 |
|
| 185 |
// write uniforms |
| 186 |
gatherer->write(params.transform().matrix()); // subRunDeviceMatrix |
| 187 |
gatherer->write(subRunData.deviceToLocal()); |
| 188 |
SkV2 atlasDimensionsInverse = {1.f/proxies[0]->dimensions().width(), |
| 189 |
1.f/proxies[0]->dimensions().height()}; |
| 190 |
gatherer->write(atlasDimensionsInverse); |
| 191 |
|
| 192 |
// write textures and samplers |
| 193 |
for (unsigned int i = 0; i < numProxies; ++i) { |
| 194 |
gatherer->add(proxies[i], {SkFilterMode::kNearest, SkTileMode::kClamp}); |
| 195 |
} |
| 196 |
// If the atlas has less than 4 active proxies we still need to set up samplers for the shader. |
| 197 |
for (unsigned int i = numProxies; i < kNumTextAtlasTextures; ++i) { |
| 198 |
gatherer->add(proxies[0], {SkFilterMode::kNearest, SkTileMode::kClamp}); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
} // namespace skgpu::graphite |