34#if !defined(SK_DISABLE_SDF_TEXT)
37#define SK_DistanceFieldAAFactor "0.65"
46#ifdef SK_GAMMA_APPLY_TO_A8
47 float distanceAdjust = dfa8gp.fDistanceAdjust;
48 if (distanceAdjust != fDistanceAdjust) {
49 fDistanceAdjust = distanceAdjust;
50 pdman.
set1f(fDistanceAdjustUni, distanceAdjust);
54 const SkISize& atlasDimensions = dfa8gp.fAtlasDimensions;
57 if (fAtlasDimensions != atlasDimensions) {
58 pdman.
set2f(fAtlasDimensionsInvUniform,
59 1.0f / atlasDimensions.
fWidth,
60 1.0f / atlasDimensions.
fHeight);
61 fAtlasDimensions = atlasDimensions;
63 SetTransform(pdman, shaderCaps, fLocalMatrixUniform, dfa8gp.fLocalMatrix, &fLocalMatrix);
67 void onEmitCode(EmitArgs&
args, GrGPArgs* gpArgs)
override{
79 const char* atlasDimensionsInvName;
80 fAtlasDimensionsInvUniform = uniformHandler->
addUniform(
nullptr,
84 &atlasDimensionsInvName);
85#ifdef SK_GAMMA_APPLY_TO_A8
87 const char* distanceAdjustUniName =
nullptr;
91 &distanceAdjustUniName);
95 fragBuilder->codeAppendf(
"half4 %s;\n",
args.fOutputColor);
100 gpArgs->fPositionVar = dfTexEffect.fInPosition.
asShaderVar();
105 gpArgs->fPositionVar,
106 dfTexEffect.fLocalMatrix,
107 &fLocalMatrixUniform);
113 dfTexEffect.fInTextureCoords.
name(),
114 atlasDimensionsInvName,
126 fragBuilder->codeAppendf(
"float2 uv = %s;\n", uv.
fsIn());
127 fragBuilder->codeAppend(
"half4 texColor;");
129 texIdx,
"uv",
"texColor");
131 fragBuilder->codeAppend(
"half distance = "
133#ifdef SK_GAMMA_APPLY_TO_A8
135 fragBuilder->codeAppendf(
"distance -= %s;", distanceAdjustUniName);
138 fragBuilder->codeAppend(
"half afwidth;");
139 if (isUniformScale) {
145 if (
args.fShaderCaps->fAvoidDfDxForGradientsWhenPossible) {
146 fragBuilder->codeAppendf(
149 fragBuilder->codeAppendf(
152 }
else if (isSimilarity) {
159 if (
args.fShaderCaps->fAvoidDfDxForGradientsWhenPossible) {
160 fragBuilder->codeAppendf(
"half st_grad_len = length(half2(dFdy(%s)));", st.
fsIn());
162 fragBuilder->codeAppendf(
"half st_grad_len = length(half2(dFdx(%s)));", st.
fsIn());
169 fragBuilder->codeAppend(
"half2 dist_grad = half2(dFdx(distance), dFdy(distance));");
172 fragBuilder->codeAppend(
"half dg_len2 = dot(dist_grad, dist_grad);"
173 "if (dg_len2 < 0.0001) {"
174 "dist_grad = half2(0.7071, 0.7071);"
176 "dist_grad = dist_grad*half(inversesqrt(dg_len2));"
179 fragBuilder->codeAppendf(
"float2x2 jacobian = float2x2(dFdx(%s), dFdy(%s));",
181 fragBuilder->codeAppend(
"half2 grad = half2(jacobian * dist_grad);");
188 fragBuilder->codeAppend(
"half val = distance > 0 ? 1.0 : 0.0;");
189 }
else if (isGammaCorrect) {
193 fragBuilder->codeAppend(
194 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
196 fragBuilder->codeAppend(
"half val = smoothstep(-afwidth, afwidth, distance);");
199 fragBuilder->codeAppendf(
"half4 %s = half4(val);",
args.fOutputCoverage);
203#ifdef SK_GAMMA_APPLY_TO_A8
204 float fDistanceAdjust = -1.f;
206 SkISize fAtlasDimensions = {-1, -1};
218GrDistanceFieldA8TextGeoProc::GrDistanceFieldA8TextGeoProc(
const GrShaderCaps& caps,
222#ifdef SK_GAMMA_APPLY_TO_A8
223 float distanceAdjust,
227 :
INHERITED(kGrDistanceFieldA8TextGeoProc_ClassID)
228 , fLocalMatrix(localMatrix)
230#ifdef SK_GAMMA_APPLY_TO_A8
231 , fDistanceAdjust(distanceAdjust)
250 for (
int i = 0;
i < numViews; ++
i) {
266 if (!fTextureSamplers[0].isInitialized()) {
270 for (
int i = 0;
i < numViews; ++
i) {
274 if (!fTextureSamplers[
i].isInitialized()) {
292 return std::make_unique<Impl>();
299#if defined(GR_TEST_UTILS)
301 auto [view, ct, at] =
d->randomAlphaOnlyView();
304 GrTest::TestWrapModes(
d->fRandom, wrapModes);
314 SkMatrix localMatrix = GrTest::TestMatrix(
d->fRandom);
315#ifdef SK_GAMMA_APPLY_TO_A8
316 float lum =
d->fRandom->nextF();
321#ifdef SK_GAMMA_APPLY_TO_A8
339 SetTransform(pdman, shaderCaps, fLocalMatrixUniform, dfpgp.fLocalMatrix, &fLocalMatrix);
341 const SkISize& atlasDimensions = dfpgp.fAtlasDimensions;
343 if (fAtlasDimensions != atlasDimensions) {
344 pdman.
set2f(fAtlasDimensionsInvUniform,
345 1.0f / atlasDimensions.
fWidth,
346 1.0f / atlasDimensions.
fHeight);
347 fAtlasDimensions = atlasDimensions;
352 void onEmitCode(EmitArgs&
args, GrGPArgs* gpArgs)
override{
365 const char* atlasDimensionsInvName;
366 fAtlasDimensionsInvUniform = uniformHandler->
addUniform(
nullptr,
369 "AtlasDimensionsInv",
370 &atlasDimensionsInvName);
375 dfPathEffect.fInTextureCoords.
name(),
376 atlasDimensionsInvName,
382 fragBuilder->codeAppendf(
"half4 %s;",
args.fOutputColor);
387 gpArgs->fPositionVar = dfPathEffect.fInPosition.
asShaderVar();
388 WriteLocalCoord(vertBuilder,
392 gpArgs->fPositionVar,
393 dfPathEffect.fLocalMatrix,
394 &fLocalMatrixUniform);
397 fragBuilder->codeAppendf(
"float2 uv = %s;", uv.
fsIn());
398 fragBuilder->codeAppend(
"half4 texColor;");
402 fragBuilder->codeAppend(
"half distance = "
405 fragBuilder->codeAppend(
"half afwidth;");
410 if (isUniformScale) {
416 if (
args.fShaderCaps->fAvoidDfDxForGradientsWhenPossible) {
417 fragBuilder->codeAppendf(
420 fragBuilder->codeAppendf(
423 }
else if (isSimilarity) {
429 if (
args.fShaderCaps->fAvoidDfDxForGradientsWhenPossible) {
430 fragBuilder->codeAppendf(
"half st_grad_len = half(length(dFdy(%s)));", st.
fsIn());
432 fragBuilder->codeAppendf(
"half st_grad_len = half(length(dFdx(%s)));", st.
fsIn());
439 fragBuilder->codeAppend(
"half2 dist_grad = half2(dFdx(distance), "
443 fragBuilder->codeAppend(
"half dg_len2 = dot(dist_grad, dist_grad);"
444 "if (dg_len2 < 0.0001) {"
445 "dist_grad = half2(0.7071, 0.7071);"
447 "dist_grad = dist_grad*half(inversesqrt(dg_len2));"
450 fragBuilder->codeAppendf(
"float2x2 jacobian = float2x2(dFdx(%s), dFdy(%s));",
452 fragBuilder->codeAppend(
"half2 grad = half2(jacobian * dist_grad);");
460 if (isGammaCorrect) {
461 fragBuilder->codeAppend(
462 "half val = saturate((distance + afwidth) / (2.0 * afwidth));");
464 fragBuilder->codeAppend(
"half val = smoothstep(-afwidth, afwidth, distance);");
467 fragBuilder->codeAppendf(
"half4 %s = half4(val);",
args.fOutputCoverage);
471 UniformHandle fLocalMatrixUniform;
474 UniformHandle fAtlasDimensionsInvUniform;
481GrDistanceFieldPathGeoProc::GrDistanceFieldPathGeoProc(
const GrShaderCaps& caps,
488 , fLocalMatrix(localMatrix)
503 for (
int i = 0;
i < numViews; ++
i) {
519 if (!fTextureSamplers[0].isInitialized()) {
523 for (
int i = 0;
i < numViews; ++
i) {
527 if (!fTextureSamplers[
i].isInitialized()) {
536 uint32_t
key = fFlags;
545 return std::make_unique<Impl>();
552#if defined(GR_TEST_UTILS)
554 auto [view, ct, at] =
d->randomAlphaOnlyView();
557 GrTest::TestWrapModes(
d->fRandom, wrapModes);
568 SkMatrix localMatrix = GrTest::TestMatrix(
d->fRandom);
585 SkASSERT(fDistanceAdjustUni.isValid());
589 if (wa != fDistanceAdjust) {
590 pdman.
set3f(fDistanceAdjustUni, wa.fR, wa.fG, wa.fB);
591 fDistanceAdjust = wa;
594 const SkISize& atlasDimensions = dflcd.fAtlasDimensions;
596 if (fAtlasDimensions != atlasDimensions) {
597 pdman.
set2f(fAtlasDimensionsInvUniform,
598 1.0f / atlasDimensions.
fWidth,
599 1.0f / atlasDimensions.
fHeight);
600 fAtlasDimensions = atlasDimensions;
602 SetTransform(pdman, shaderCaps, fLocalMatrixUniform, dflcd.fLocalMatrix, &fLocalMatrix);
606 void onEmitCode(EmitArgs&
args, GrGPArgs* gpArgs)
override {
617 const char* atlasDimensionsInvName;
618 fAtlasDimensionsInvUniform = uniformHandler->
addUniform(
nullptr,
621 "AtlasDimensionsInv",
622 &atlasDimensionsInvName);
632 gpArgs->fPositionVar = dfTexEffect.fInPosition.
asShaderVar();
633 WriteLocalCoord(vertBuilder,
638 dfTexEffect.fLocalMatrix,
639 &fLocalMatrixUniform);
645 dfTexEffect.fInTextureCoords.
name(),
646 atlasDimensionsInvName,
655 vertBuilder->codeAppendf(
"%s = -%s.y/3.0;",
delta.vsOut(), atlasDimensionsInvName);
657 vertBuilder->codeAppendf(
"%s = %s.y/3.0;",
delta.vsOut(), atlasDimensionsInvName);
661 vertBuilder->codeAppendf(
"%s = -%s.x/3.0;",
delta.vsOut(), atlasDimensionsInvName);
663 vertBuilder->codeAppendf(
"%s = %s.x/3.0;",
delta.vsOut(), atlasDimensionsInvName);
677 if (isUniformScale) {
678 if (
args.fShaderCaps->fAvoidDfDxForGradientsWhenPossible) {
679 fragBuilder->
codeAppendf(
"half st_grad_len = half(abs(dFdy(%s.y)));", st.
fsIn());
681 fragBuilder->
codeAppendf(
"half st_grad_len = half(abs(dFdx(%s.x)));", st.
fsIn());
684 fragBuilder->
codeAppendf(
"half2 offset = half2(0.0, half(st_grad_len*%s));",
687 fragBuilder->
codeAppendf(
"half2 offset = half2(half(st_grad_len*%s), 0.0);",
690 }
else if (isSimilarity) {
693 if (
args.fShaderCaps->fAvoidDfDxForGradientsWhenPossible) {
696 fragBuilder->
codeAppendf(
"half2 st_grad = half2(dFdy(%s));", st.
fsIn());
698 fragBuilder->
codeAppendf(
"half2 offset = half2(%s)*st_grad;",
delta.fsIn());
701 "half2 offset = half2(%s*float2(st_grad.y,-st_grad.x));",
delta.fsIn());
704 fragBuilder->
codeAppendf(
"half2 st_grad = half2(dFdx(%s));", st.
fsIn());
707 "half2 offset = half2(%s*float2(-st_grad.y,st_grad.x));",
delta.fsIn());
712 fragBuilder->
codeAppend(
"half st_grad_len = length(st_grad);");
716 fragBuilder->
codeAppend(
"float2x2 jacobian = float2x2(dFdx(st), dFdy(st));");
718 fragBuilder->
codeAppendf(
"half2 offset = half2(jacobian * half2(0, %s));",
721 fragBuilder->
codeAppendf(
"half2 offset = half2(jacobian * half2(%s, 0));",
729 texIdx,
"uv",
"offset",
"distance");
734 const char* distanceAdjustUniName =
nullptr;
737 &distanceAdjustUniName);
738 fragBuilder->
codeAppendf(
"distance -= %s;", distanceAdjustUniName);
757 fragBuilder->
codeAppend(
"half2 dist_grad = half2(dFdx(distance.r), dFdy(distance.r));");
760 fragBuilder->
codeAppend(
"half dg_len2 = dot(dist_grad, dist_grad);"
761 "if (dg_len2 < 0.0001) {"
762 "dist_grad = half2(0.7071, 0.7071);"
764 "dist_grad = dist_grad*half(inversesqrt(dg_len2));"
766 "half2 grad = half2(jacobian * dist_grad);");
775 if (isGammaCorrect) {
777 "half4(saturate((distance + half3(afwidth)) / half3(2.0 * afwidth)), 1.0);",
778 args.fOutputCoverage);
781 "half4 %s = half4(smoothstep(half3(-afwidth), half3(afwidth), distance), 1.0);",
782 args.fOutputCoverage);
788 SkISize fAtlasDimensions = {-1, -1};
791 UniformHandle fDistanceAdjustUni;
792 UniformHandle fAtlasDimensionsInvUniform;
793 UniformHandle fLocalMatrixUniform;
798GrDistanceFieldLCDTextGeoProc::GrDistanceFieldLCDTextGeoProc(
const GrShaderCaps& caps,
802 DistanceAdjust distanceAdjust,
806 , fLocalMatrix(localMatrix)
807 , fDistanceAdjust(distanceAdjust)
826 for (
int i = 0;
i < numViews; ++
i) {
842 if (!fTextureSamplers[0].isInitialized()) {
846 for (
int i = 0;
i < numViews; ++
i) {
850 if (!fTextureSamplers[
i].isInitialized()) {
868 return std::make_unique<Impl>();
875#if defined(GR_TEST_UTILS)
877 auto [view, ct, at] =
d->randomView();
880 GrTest::TestWrapModes(
d->fRandom, wrapModes);
884 DistanceAdjust wa = { 0.0f, 0.1f, -0.1f };
891 SkMatrix localMatrix = GrTest::TestMatrix(
d->fRandom);
894 1, samplerState, wa,
flags, localMatrix);
static void append_index_uv_varyings(GrGeometryProcessor::ProgramImpl::EmitArgs &args, int numTextureSamplers, const char *inTexCoordsName, const char *atlasDimensionsInvName, GrGLSLVarying *uv, GrGLSLVarying *texIdx, GrGLSLVarying *st)
static void append_multitexture_lookup_lcd(GrGeometryProcessor::ProgramImpl::EmitArgs &args, int numTextureSamplers, const GrGLSLVarying &texIdx, const char *coordName, const char *offsetName, const char *distanceName)
static void append_multitexture_lookup(GrGeometryProcessor::ProgramImpl::EmitArgs &args, int numTextureSamplers, const GrGLSLVarying &texIdx, const char *coordName, const char *colorName)
#define SK_DistanceFieldAAFactor
@ kGammaCorrect_DistanceFieldEffectFlag
@ kWideColor_DistanceFieldEffectFlag
@ kUseLCD_DistanceFieldEffectFlag
@ kPerspective_DistanceFieldEffectFlag
@ kSimilarity_DistanceFieldEffectFlag
@ kBGR_DistanceFieldEffectFlag
@ kScaleOnly_DistanceFieldEffectFlag
@ kAliased_DistanceFieldEffectFlag
@ kUniformScale_DistanceFieldEffectMask
@ kPath_DistanceFieldEffectMask
@ kNonLCD_DistanceFieldEffectMask
@ kPortrait_DistanceFieldEffectFlag
@ kLCD_DistanceFieldEffectMask
GrFragmentProcessor::ProgramImpl ProgramImpl
#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
@ kFloat2_GrVertexAttribType
@ kUShort2_GrVertexAttribType
@ kUByte4_norm_GrVertexAttribType
@ kFloat3_GrVertexAttribType
#define SK_DistanceFieldMultiplier
#define SK_DistanceFieldThreshold
constexpr bool SkIsPow2(T value)
#define INHERITED(method,...)
static constexpr bool SkToBool(const T &x)
void setData(const GrGLSLProgramDataManager &pdman, const GrShaderCaps &shaderCaps, const GrGeometryProcessor &geomProc) override
static GrGeometryProcessor * Make(SkArenaAlloc *arena, const GrShaderCaps &caps, const GrSurfaceProxyView *views, int numActiveViews, GrSamplerState params, uint32_t flags, const SkMatrix &localMatrixIfUsesLocalCoords)
void addNewViews(const GrSurfaceProxyView *views, int numViews, GrSamplerState)
void addToKey(const GrShaderCaps &, skgpu::KeyBuilder *) const override
std::unique_ptr< ProgramImpl > makeProgramImpl(const GrShaderCaps &) const override
static constexpr int kMaxTextures
void setData(const GrGLSLProgramDataManager &pdman, const GrShaderCaps &shaderCaps, const GrGeometryProcessor &geomProc) override
static GrGeometryProcessor * Make(SkArenaAlloc *arena, const GrShaderCaps &caps, const GrSurfaceProxyView *views, int numActiveViews, GrSamplerState params, DistanceAdjust distanceAdjust, uint32_t flags, const SkMatrix &localMatrixIfUsesLocalCoords)
void addNewViews(const GrSurfaceProxyView *, int numActiveViews, GrSamplerState)
std::unique_ptr< ProgramImpl > makeProgramImpl(const GrShaderCaps &) const override
static constexpr int kMaxTextures
void addToKey(const GrShaderCaps &, skgpu::KeyBuilder *) const override
void setData(const GrGLSLProgramDataManager &pdman, const GrShaderCaps &shaderCaps, const GrGeometryProcessor &geomProc) override
void addNewViews(const GrSurfaceProxyView *, int numActiveViews, GrSamplerState)
void addToKey(const GrShaderCaps &, skgpu::KeyBuilder *) const override
static GrGeometryProcessor * Make(SkArenaAlloc *arena, const GrShaderCaps &caps, const GrSurfaceProxyView *views, int numActiveViews, GrSamplerState params, const SkMatrix &localMatrix, uint32_t flags)
static constexpr int kMaxTextures
std::unique_ptr< ProgramImpl > makeProgramImpl(const GrShaderCaps &) const override
virtual void set2f(UniformHandle, float, float) const =0
virtual void set3f(UniformHandle, float, float, float) const =0
virtual void set1f(UniformHandle, float v0) const =0
void codeAppend(const char *str)
void codeAppendf(const char format[],...) SK_PRINTF_LIKE(2
void emitAttributes(const GrGeometryProcessor &)
void addPassThroughAttribute(const GrShaderVar &vsVar, const char *output, Interpolation=Interpolation::kInterpolated)
void addVarying(const char *name, GrGLSLVarying *varying, Interpolation=Interpolation::kInterpolated)
const char * fsIn() const
GrShaderVar asShaderVar() const
constexpr const char * name() const
GrGLSLProgramDataManager::UniformHandle UniformHandle
static void WriteLocalCoord(GrGLSLVertexBuilder *, GrGLSLUniformHandler *, const GrShaderCaps &, GrGPArgs *, GrShaderVar localVar, const SkMatrix &localMatrix, UniformHandle *localMatrixUniform)
static constexpr int kMatrixKeyBits
static uint32_t ComputeMatrixKey(const GrShaderCaps &caps, const SkMatrix &mat)
static void SetTransform(const GrGLSLProgramDataManager &, const GrShaderCaps &, const UniformHandle &uniform, const SkMatrix &matrix, SkMatrix *state=nullptr)
void reset(GrSamplerState, const GrBackendFormat &, const skgpu::Swizzle &)
int numTextureSamplers() const
void setTextureSamplerCnt(int cnt)
static Attribute MakeColorAttribute(const char *name, bool wideColor)
void setVertexAttributesWithImplicitOffsets(const Attribute *attrs, int attrCount)
@ kGrDistanceFieldLCDTextGeoProc_ClassID
@ kGrDistanceFieldPathGeoProc_ClassID
skgpu::Swizzle swizzle() const
GrSurfaceProxy * proxy() const
const GrBackendFormat & backendFormat() const
SkISize dimensions() const
bool hasPerspective() const
static const SkMatrix & InvalidMatrix()
const EmbeddedViewParams * params
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
FlutterSemanticsFlag flags
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static float lum(float r, float g, float b)
static float min(float r, float g, float b)
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)