jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 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 "GrDistanceFieldTextureEffect.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 9 | #include "GrInvariantOutput.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 10 | #include "GrTexture.h" |
| 11 | #include "SkDistanceFieldGen.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 12 | #include "gl/GrGLProcessor.h" |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 13 | #include "gl/GrGLSL.h" |
| 14 | #include "gl/GrGLTexture.h" |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 15 | #include "gl/GrGLGeometryProcessor.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 16 | #include "gl/builders/GrGLProgramBuilder.h" |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 17 | |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 18 | // Assuming a radius of the diagonal of the fragment, hence a factor of sqrt(2)/2 |
| 19 | #define SK_DistanceFieldAAFactor "0.7071" |
| 20 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 21 | struct DistanceFieldBatchTracker { |
| 22 | GrGPInput fInputColorType; |
| 23 | GrColor fColor; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 24 | bool fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 25 | }; |
| 26 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 27 | class GrGLDistanceFieldTextureEffect : public GrGLGeometryProcessor { |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 28 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 29 | GrGLDistanceFieldTextureEffect(const GrGeometryProcessor&, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 30 | const GrBatchTracker&) |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 31 | : fColor(GrColor_ILLEGAL) |
| 32 | , fTextureSize(SkISize::Make(-1,-1)) |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 33 | #ifdef SK_GAMMA_APPLY_TO_A8 |
| 34 | , fLuminance(-1.0f) |
| 35 | #endif |
| 36 | {} |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 37 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 38 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) SK_OVERRIDE{ |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 39 | const GrDistanceFieldTextureEffect& dfTexEffect = |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 40 | args.fGP.cast<GrDistanceFieldTextureEffect>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 41 | const DistanceFieldBatchTracker& local = args.fBT.cast<DistanceFieldBatchTracker>(); |
| 42 | GrGLGPBuilder* pb = args.fPB; |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 43 | GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 44 | SkAssertResult(fsBuilder->enableFeature( |
| 45 | GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 46 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 47 | GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 48 | |
| 49 | // emit attributes |
| 50 | vsBuilder->emitAttributes(dfTexEffect); |
| 51 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 52 | GrGLVertToFrag v(kVec2f_GrSLType); |
| 53 | args.fPB->addVarying("TextureCoords", &v); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 54 | vsBuilder->codeAppendf("%s = %s;", v.vsOut(), dfTexEffect.inTextureCoords()->fName); |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 55 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 56 | // Setup pass through color |
| 57 | this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, |
| 58 | dfTexEffect.inColor(), &fColorUniform); |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 59 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 60 | // setup uniform viewMatrix |
| 61 | this->addUniformViewMatrix(pb); |
| 62 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 63 | // Setup position |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 64 | SetupPosition(vsBuilder, gpArgs, dfTexEffect.inPosition()->fName, |
| 65 | dfTexEffect.viewMatrix(), this->uViewM()); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 66 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 67 | // emit transforms |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 68 | this->emitTransforms(args.fPB, gpArgs->fPositionVar, dfTexEffect.inPosition()->fName, |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 69 | dfTexEffect.localMatrix(), args.fTransformsIn, args.fTransformsOut); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 70 | |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 71 | const char* textureSizeUniName = NULL; |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 72 | fTextureSizeUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 73 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 74 | "TextureSize", &textureSizeUniName); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 75 | |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame^] | 76 | // Use highp to work around aliasing issues |
| 77 | fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision, |
| 78 | pb->ctxInfo().standard())); |
| 79 | fsBuilder->codeAppendf("vec2 uv = %s;\n", v.fsIn()); |
| 80 | |
| 81 | fsBuilder->codeAppend("\tfloat texColor = "); |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 82 | fsBuilder->appendTextureLookup(args.fSamplers[0], |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame^] | 83 | "uv", |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 84 | kVec2f_GrSLType); |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame^] | 85 | fsBuilder->codeAppend(".r;\n"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 86 | fsBuilder->codeAppend("\tfloat distance = " |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame^] | 87 | SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFieldThreshold ");"); |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 88 | |
| 89 | // we adjust for the effect of the transformation on the distance by using |
| 90 | // the length of the gradient of the texture coordinates. We use st coordinates |
| 91 | // to ensure we're mapping 1:1 from texel space to pixel space. |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame^] | 92 | fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision, |
| 93 | pb->ctxInfo().standard())); |
| 94 | fsBuilder->codeAppendf("vec2 st = uv*%s;\n", textureSizeUniName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 95 | fsBuilder->codeAppend("\tfloat afwidth;\n"); |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 96 | if (dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag) { |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 97 | // this gives us a smooth step across approximately one fragment |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 98 | fsBuilder->codeAppend("\tafwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(st.x));\n"); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 99 | } else { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 100 | fsBuilder->codeAppend("\tvec2 Jdx = dFdx(st);\n"); |
| 101 | fsBuilder->codeAppend("\tvec2 Jdy = dFdy(st);\n"); |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 102 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 103 | fsBuilder->codeAppend("\tvec2 uv_grad;\n"); |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 104 | if (args.fPB->ctxInfo().caps()->dropsTileOnZeroDivide()) { |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 105 | // this is to compensate for the Adreno, which likes to drop tiles on division by 0 |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 106 | fsBuilder->codeAppend("\tfloat uv_len2 = dot(uv, uv);\n"); |
| 107 | fsBuilder->codeAppend("\tif (uv_len2 < 0.0001) {\n"); |
| 108 | fsBuilder->codeAppend("\t\tuv_grad = vec2(0.7071, 0.7071);\n"); |
| 109 | fsBuilder->codeAppend("\t} else {\n"); |
| 110 | fsBuilder->codeAppend("\t\tuv_grad = uv*inversesqrt(uv_len2);\n"); |
| 111 | fsBuilder->codeAppend("\t}\n"); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 112 | } else { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 113 | fsBuilder->codeAppend("\tuv_grad = normalize(uv);\n"); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 114 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 115 | fsBuilder->codeAppend("\tvec2 grad = vec2(uv_grad.x*Jdx.x + uv_grad.y*Jdy.x,\n"); |
| 116 | fsBuilder->codeAppend("\t uv_grad.x*Jdx.y + uv_grad.y*Jdy.y);\n"); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 117 | |
| 118 | // this gives us a smooth step across approximately one fragment |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 119 | fsBuilder->codeAppend("\tafwidth = " SK_DistanceFieldAAFactor "*length(grad);\n"); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 120 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 121 | fsBuilder->codeAppend("\tfloat val = smoothstep(-afwidth, afwidth, distance);\n"); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 122 | |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 123 | #ifdef SK_GAMMA_APPLY_TO_A8 |
| 124 | // adjust based on gamma |
| 125 | const char* luminanceUniName = NULL; |
| 126 | // width, height, 1/(3*width) |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 127 | fLuminanceUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 128 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 129 | "Luminance", &luminanceUniName); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 130 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 131 | fsBuilder->codeAppendf("\tuv = vec2(val, %s);\n", luminanceUniName); |
| 132 | fsBuilder->codeAppend("\tvec4 gammaColor = "); |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 133 | fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 134 | fsBuilder->codeAppend(";\n"); |
| 135 | fsBuilder->codeAppend("\tval = gammaColor.r;\n"); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 136 | #endif |
| 137 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 138 | fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 139 | } |
| 140 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 141 | virtual void setData(const GrGLProgramDataManager& pdman, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 142 | const GrPrimitiveProcessor& proc, |
| 143 | const GrBatchTracker& bt) SK_OVERRIDE { |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 144 | SkASSERT(fTextureSizeUni.isValid()); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 145 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 146 | GrTexture* texture = proc.texture(0); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 147 | if (texture->width() != fTextureSize.width() || |
| 148 | texture->height() != fTextureSize.height()) { |
| 149 | fTextureSize = SkISize::Make(texture->width(), texture->height()); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 150 | pdman.set2f(fTextureSizeUni, |
| 151 | SkIntToScalar(fTextureSize.width()), |
| 152 | SkIntToScalar(fTextureSize.height())); |
commit-bot@chromium.org | 66beaf0 | 2014-03-26 18:21:55 +0000 | [diff] [blame] | 153 | } |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 154 | #ifdef SK_GAMMA_APPLY_TO_A8 |
| 155 | const GrDistanceFieldTextureEffect& dfTexEffect = |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 156 | proc.cast<GrDistanceFieldTextureEffect>(); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 157 | float luminance = dfTexEffect.getLuminance(); |
| 158 | if (luminance != fLuminance) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 159 | pdman.set1f(fLuminanceUni, luminance); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 160 | fLuminance = luminance; |
| 161 | } |
| 162 | #endif |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 163 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 164 | this->setUniformViewMatrix(pdman, proc.viewMatrix()); |
| 165 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 166 | const DistanceFieldBatchTracker& local = bt.cast<DistanceFieldBatchTracker>(); |
| 167 | if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) { |
| 168 | GrGLfloat c[4]; |
| 169 | GrColorToRGBAFloat(local.fColor, c); |
| 170 | pdman.set4fv(fColorUniform, 1, c); |
| 171 | fColor = local.fColor; |
| 172 | } |
commit-bot@chromium.org | 8fe2ee1 | 2014-03-26 18:03:05 +0000 | [diff] [blame] | 173 | } |
| 174 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 175 | static inline void GenKey(const GrGeometryProcessor& gp, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 176 | const GrBatchTracker& bt, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 177 | const GrGLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 178 | GrProcessorKeyBuilder* b) { |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 179 | const GrDistanceFieldTextureEffect& dfTexEffect = gp.cast<GrDistanceFieldTextureEffect>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 180 | const DistanceFieldBatchTracker& local = bt.cast<DistanceFieldBatchTracker>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 181 | uint32_t key = dfTexEffect.getFlags(); |
| 182 | key |= local.fInputColorType << 16; |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 183 | key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 << 24: 0x0; |
| 184 | key |= ComputePosKey(gp.viewMatrix()) << 25; |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 185 | b->add32(key); |
commit-bot@chromium.org | 4362a38 | 2014-03-26 19:49:03 +0000 | [diff] [blame] | 186 | } |
| 187 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 188 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 189 | GrColor fColor; |
| 190 | UniformHandle fColorUniform; |
| 191 | UniformHandle fTextureSizeUni; |
| 192 | SkISize fTextureSize; |
| 193 | UniformHandle fLuminanceUni; |
mtklein | 50282b4 | 2015-01-22 07:59:52 -0800 | [diff] [blame] | 194 | #ifdef SK_GAMMA_APPLY_TO_A8 |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 195 | float fLuminance; |
mtklein | 50282b4 | 2015-01-22 07:59:52 -0800 | [diff] [blame] | 196 | #endif |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 197 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 198 | typedef GrGLGeometryProcessor INHERITED; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | /////////////////////////////////////////////////////////////////////////////// |
| 202 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 203 | GrDistanceFieldTextureEffect::GrDistanceFieldTextureEffect(GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 204 | const SkMatrix& viewMatrix, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 205 | GrTexture* texture, |
commit-bot@chromium.org | 6c89c34 | 2014-02-14 21:48:29 +0000 | [diff] [blame] | 206 | const GrTextureParams& params, |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 207 | #ifdef SK_GAMMA_APPLY_TO_A8 |
| 208 | GrTexture* gamma, |
| 209 | const GrTextureParams& gammaParams, |
| 210 | float luminance, |
| 211 | #endif |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 212 | uint32_t flags, bool opaqueVertexColors) |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 213 | : INHERITED(color, viewMatrix, SkMatrix::I(), opaqueVertexColors) |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 214 | , fTextureAccess(texture, params) |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 215 | #ifdef SK_GAMMA_APPLY_TO_A8 |
| 216 | , fGammaTextureAccess(gamma, gammaParams) |
| 217 | , fLuminance(luminance) |
| 218 | #endif |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 219 | , fFlags(flags & kNonLCD_DistanceFieldEffectMask) |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 220 | , fInColor(NULL) { |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 221 | SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask)); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 222 | this->initClassID<GrDistanceFieldTextureEffect>(); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 223 | fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType)); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 224 | if (flags & kColorAttr_DistanceFieldEffectFlag) { |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 225 | fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType)); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 226 | this->setHasVertexColor(); |
| 227 | } |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 228 | fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 229 | kVec2f_GrVertexAttribType)); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 230 | this->addTextureAccess(&fTextureAccess); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 231 | #ifdef SK_GAMMA_APPLY_TO_A8 |
| 232 | this->addTextureAccess(&fGammaTextureAccess); |
| 233 | #endif |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 234 | } |
| 235 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 236 | bool GrDistanceFieldTextureEffect::onIsEqual(const GrGeometryProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 237 | const GrDistanceFieldTextureEffect& cte = other.cast<GrDistanceFieldTextureEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 238 | return |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 239 | #ifdef SK_GAMMA_APPLY_TO_A8 |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 240 | fLuminance == cte.fLuminance && |
| 241 | #endif |
| 242 | fFlags == cte.fFlags; |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 243 | } |
| 244 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 245 | void GrDistanceFieldTextureEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const { |
| 246 | out->setUnknownSingleComponent(); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 247 | } |
| 248 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 249 | void GrDistanceFieldTextureEffect::getGLProcessorKey(const GrBatchTracker& bt, |
| 250 | const GrGLCaps& caps, |
| 251 | GrProcessorKeyBuilder* b) const { |
| 252 | GrGLDistanceFieldTextureEffect::GenKey(*this, bt, caps, b); |
| 253 | } |
| 254 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 255 | GrGLPrimitiveProcessor* |
| 256 | GrDistanceFieldTextureEffect::createGLInstance(const GrBatchTracker& bt, |
| 257 | const GrGLCaps&) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 258 | return SkNEW_ARGS(GrGLDistanceFieldTextureEffect, (*this, bt)); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 259 | } |
| 260 | |
joshualitt | ca0a179 | 2015-01-27 06:41:33 -0800 | [diff] [blame] | 261 | void GrDistanceFieldTextureEffect::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 262 | DistanceFieldBatchTracker* local = bt->cast<DistanceFieldBatchTracker>(); |
| 263 | local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, |
| 264 | SkToBool(fInColor)); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 265 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | bool GrDistanceFieldTextureEffect::onCanMakeEqual(const GrBatchTracker& m, |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 269 | const GrGeometryProcessor& that, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 270 | const GrBatchTracker& t) const { |
| 271 | const DistanceFieldBatchTracker& mine = m.cast<DistanceFieldBatchTracker>(); |
| 272 | const DistanceFieldBatchTracker& theirs = t.cast<DistanceFieldBatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 273 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 274 | that, theirs.fUsesLocalCoords) && |
| 275 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 276 | theirs.fInputColorType, theirs.fColor); |
| 277 | } |
| 278 | |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 279 | /////////////////////////////////////////////////////////////////////////////// |
| 280 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 281 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldTextureEffect); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 282 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 283 | GrGeometryProcessor* GrDistanceFieldTextureEffect::TestCreate(SkRandom* random, |
| 284 | GrContext*, |
| 285 | const GrDrawTargetCaps&, |
| 286 | GrTexture* textures[]) { |
| 287 | int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 288 | GrProcessorUnitTest::kAlphaTextureIdx; |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 289 | #ifdef SK_GAMMA_APPLY_TO_A8 |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 290 | int texIdx2 = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 291 | GrProcessorUnitTest::kAlphaTextureIdx; |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 292 | #endif |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 293 | static const SkShader::TileMode kTileModes[] = { |
| 294 | SkShader::kClamp_TileMode, |
| 295 | SkShader::kRepeat_TileMode, |
| 296 | SkShader::kMirror_TileMode, |
| 297 | }; |
| 298 | SkShader::TileMode tileModes[] = { |
| 299 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 300 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 301 | }; |
| 302 | GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
| 303 | GrTextureParams::kNone_FilterMode); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 304 | #ifdef SK_GAMMA_APPLY_TO_A8 |
| 305 | GrTextureParams params2(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
| 306 | GrTextureParams::kNone_FilterMode); |
| 307 | #endif |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 308 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 309 | return GrDistanceFieldTextureEffect::Create(GrRandomColor(random), |
| 310 | GrProcessorUnitTest::TestMatrix(random), |
| 311 | textures[texIdx], params, |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 312 | #ifdef SK_GAMMA_APPLY_TO_A8 |
| 313 | textures[texIdx2], params2, |
| 314 | random->nextF(), |
| 315 | #endif |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 316 | random->nextBool() ? |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 317 | kSimilarity_DistanceFieldEffectFlag : 0, |
| 318 | random->nextBool()); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /////////////////////////////////////////////////////////////////////////////// |
| 322 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 323 | struct DistanceFieldNoGammaBatchTracker { |
| 324 | GrGPInput fInputColorType; |
| 325 | GrColor fColor; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 326 | bool fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 327 | }; |
| 328 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 329 | class GrGLDistanceFieldNoGammaTextureEffect : public GrGLGeometryProcessor { |
| 330 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 331 | GrGLDistanceFieldNoGammaTextureEffect(const GrGeometryProcessor&, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 332 | const GrBatchTracker&) |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 333 | : fColor(GrColor_ILLEGAL), fTextureSize(SkISize::Make(-1, -1)) {} |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 334 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 335 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) SK_OVERRIDE{ |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 336 | const GrDistanceFieldNoGammaTextureEffect& dfTexEffect = |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 337 | args.fGP.cast<GrDistanceFieldNoGammaTextureEffect>(); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 338 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 339 | const DistanceFieldNoGammaBatchTracker& local = |
| 340 | args.fBT.cast<DistanceFieldNoGammaBatchTracker>(); |
| 341 | GrGLGPBuilder* pb = args.fPB; |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 342 | GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 343 | SkAssertResult(fsBuilder->enableFeature( |
| 344 | GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 345 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 346 | GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 347 | |
| 348 | // emit attributes |
| 349 | vsBuilder->emitAttributes(dfTexEffect); |
| 350 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 351 | GrGLVertToFrag v(kVec2f_GrSLType); |
| 352 | args.fPB->addVarying("TextureCoords", &v); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 353 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 354 | // setup pass through color |
| 355 | this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, |
| 356 | dfTexEffect.inColor(), &fColorUniform); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 357 | |
| 358 | vsBuilder->codeAppendf("%s = %s;", v.vsOut(), dfTexEffect.inTextureCoords()->fName); |
| 359 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 360 | // setup uniform viewMatrix |
| 361 | this->addUniformViewMatrix(pb); |
| 362 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 363 | // Setup position |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 364 | SetupPosition(vsBuilder, gpArgs, dfTexEffect.inPosition()->fName, |
| 365 | dfTexEffect.viewMatrix(), this->uViewM()); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 366 | |
| 367 | // emit transforms |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 368 | this->emitTransforms(args.fPB, gpArgs->fPositionVar, dfTexEffect.inPosition()->fName, |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 369 | dfTexEffect.localMatrix(), args.fTransformsIn, args.fTransformsOut); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 370 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 371 | const char* textureSizeUniName = NULL; |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 372 | fTextureSizeUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 373 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 374 | "TextureSize", &textureSizeUniName); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 375 | |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame^] | 376 | // Use highp to work around aliasing issues |
| 377 | fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision, |
| 378 | pb->ctxInfo().standard())); |
| 379 | fsBuilder->codeAppendf("vec2 uv = %s;", v.fsIn()); |
| 380 | |
| 381 | fsBuilder->codeAppend("float texColor = "); |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 382 | fsBuilder->appendTextureLookup(args.fSamplers[0], |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame^] | 383 | "uv", |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 384 | kVec2f_GrSLType); |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame^] | 385 | fsBuilder->codeAppend(".r;"); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 386 | fsBuilder->codeAppend("float distance = " |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame^] | 387 | SK_DistanceFieldMultiplier "*(texColor - " SK_DistanceFieldThreshold ");"); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 388 | |
| 389 | // we adjust for the effect of the transformation on the distance by using |
| 390 | // the length of the gradient of the texture coordinates. We use st coordinates |
| 391 | // to ensure we're mapping 1:1 from texel space to pixel space. |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame^] | 392 | fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision, |
| 393 | pb->ctxInfo().standard())); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 394 | fsBuilder->codeAppendf("vec2 st = uv*%s;", textureSizeUniName); |
| 395 | fsBuilder->codeAppend("float afwidth;"); |
| 396 | if (dfTexEffect.getFlags() & kSimilarity_DistanceFieldEffectFlag) { |
| 397 | // this gives us a smooth step across approximately one fragment |
| 398 | fsBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dFdx(st.x));"); |
| 399 | } else { |
| 400 | fsBuilder->codeAppend("vec2 Jdx = dFdx(st);"); |
| 401 | fsBuilder->codeAppend("vec2 Jdy = dFdy(st);"); |
| 402 | |
| 403 | fsBuilder->codeAppend("vec2 uv_grad;"); |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 404 | if (args.fPB->ctxInfo().caps()->dropsTileOnZeroDivide()) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 405 | // this is to compensate for the Adreno, which likes to drop tiles on division by 0 |
| 406 | fsBuilder->codeAppend("float uv_len2 = dot(uv, uv);"); |
| 407 | fsBuilder->codeAppend("if (uv_len2 < 0.0001) {"); |
| 408 | fsBuilder->codeAppend("uv_grad = vec2(0.7071, 0.7071);"); |
| 409 | fsBuilder->codeAppend("} else {"); |
| 410 | fsBuilder->codeAppend("uv_grad = uv*inversesqrt(uv_len2);"); |
| 411 | fsBuilder->codeAppend("}"); |
| 412 | } else { |
| 413 | fsBuilder->codeAppend("uv_grad = normalize(uv);"); |
| 414 | } |
| 415 | fsBuilder->codeAppend("vec2 grad = vec2(uv_grad.x*Jdx.x + uv_grad.y*Jdy.x,"); |
| 416 | fsBuilder->codeAppend(" uv_grad.x*Jdx.y + uv_grad.y*Jdy.y);"); |
| 417 | |
| 418 | // this gives us a smooth step across approximately one fragment |
| 419 | fsBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length(grad);"); |
| 420 | } |
| 421 | fsBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distance);"); |
| 422 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 423 | fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | virtual void setData(const GrGLProgramDataManager& pdman, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 427 | const GrPrimitiveProcessor& proc, |
| 428 | const GrBatchTracker& bt) SK_OVERRIDE { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 429 | SkASSERT(fTextureSizeUni.isValid()); |
| 430 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 431 | GrTexture* texture = proc.texture(0); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 432 | if (texture->width() != fTextureSize.width() || |
| 433 | texture->height() != fTextureSize.height()) { |
| 434 | fTextureSize = SkISize::Make(texture->width(), texture->height()); |
| 435 | pdman.set2f(fTextureSizeUni, |
| 436 | SkIntToScalar(fTextureSize.width()), |
| 437 | SkIntToScalar(fTextureSize.height())); |
| 438 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 439 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 440 | this->setUniformViewMatrix(pdman, proc.viewMatrix()); |
| 441 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 442 | const DistanceFieldNoGammaBatchTracker& local = bt.cast<DistanceFieldNoGammaBatchTracker>(); |
| 443 | if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) { |
| 444 | GrGLfloat c[4]; |
| 445 | GrColorToRGBAFloat(local.fColor, c); |
| 446 | pdman.set4fv(fColorUniform, 1, c); |
| 447 | fColor = local.fColor; |
| 448 | } |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 449 | } |
| 450 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 451 | static inline void GenKey(const GrGeometryProcessor& gp, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 452 | const GrBatchTracker& bt, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 453 | const GrGLCaps&, |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 454 | GrProcessorKeyBuilder* b) { |
| 455 | const GrDistanceFieldNoGammaTextureEffect& dfTexEffect = |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 456 | gp.cast<GrDistanceFieldNoGammaTextureEffect>(); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 457 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 458 | const DistanceFieldNoGammaBatchTracker& local = bt.cast<DistanceFieldNoGammaBatchTracker>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 459 | uint32_t key = dfTexEffect.getFlags(); |
| 460 | key |= local.fInputColorType << 16; |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 461 | key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 << 24: 0x0; |
| 462 | key |= ComputePosKey(gp.viewMatrix()) << 25; |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 463 | b->add32(key); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 467 | UniformHandle fColorUniform; |
| 468 | UniformHandle fTextureSizeUni; |
| 469 | GrColor fColor; |
| 470 | SkISize fTextureSize; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 471 | |
| 472 | typedef GrGLGeometryProcessor INHERITED; |
| 473 | }; |
| 474 | |
| 475 | /////////////////////////////////////////////////////////////////////////////// |
| 476 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 477 | GrDistanceFieldNoGammaTextureEffect::GrDistanceFieldNoGammaTextureEffect( |
| 478 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 479 | const SkMatrix& viewMatrix, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 480 | GrTexture* texture, |
| 481 | const GrTextureParams& params, |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 482 | uint32_t flags, |
| 483 | bool opaqueVertexColors) |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 484 | : INHERITED(color, viewMatrix, SkMatrix::I(), opaqueVertexColors) |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 485 | , fTextureAccess(texture, params) |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 486 | , fFlags(flags & kNonLCD_DistanceFieldEffectMask) |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 487 | , fInColor(NULL) { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 488 | SkASSERT(!(flags & ~kNonLCD_DistanceFieldEffectMask)); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 489 | this->initClassID<GrDistanceFieldNoGammaTextureEffect>(); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 490 | fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType)); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 491 | if (flags & kColorAttr_DistanceFieldEffectFlag) { |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 492 | fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType)); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 493 | this->setHasVertexColor(); |
| 494 | } |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 495 | fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 496 | kVec2f_GrVertexAttribType)); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 497 | this->addTextureAccess(&fTextureAccess); |
| 498 | } |
| 499 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 500 | bool GrDistanceFieldNoGammaTextureEffect::onIsEqual(const GrGeometryProcessor& other) const { |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 501 | const GrDistanceFieldNoGammaTextureEffect& cte = |
| 502 | other.cast<GrDistanceFieldNoGammaTextureEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 503 | return fFlags == cte.fFlags; |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 504 | } |
| 505 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 506 | void GrDistanceFieldNoGammaTextureEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const{ |
| 507 | out->setUnknownSingleComponent(); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 508 | } |
| 509 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 510 | void GrDistanceFieldNoGammaTextureEffect::getGLProcessorKey(const GrBatchTracker& bt, |
| 511 | const GrGLCaps& caps, |
| 512 | GrProcessorKeyBuilder* b) const { |
| 513 | GrGLDistanceFieldNoGammaTextureEffect::GenKey(*this, bt, caps, b); |
| 514 | } |
| 515 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 516 | GrGLPrimitiveProcessor* |
| 517 | GrDistanceFieldNoGammaTextureEffect::createGLInstance(const GrBatchTracker& bt, |
| 518 | const GrGLCaps&) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 519 | return SkNEW_ARGS(GrGLDistanceFieldNoGammaTextureEffect, (*this, bt)); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 520 | } |
| 521 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 522 | void GrDistanceFieldNoGammaTextureEffect::initBatchTracker(GrBatchTracker* bt, |
joshualitt | ca0a179 | 2015-01-27 06:41:33 -0800 | [diff] [blame] | 523 | const InitBT& init) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 524 | DistanceFieldNoGammaBatchTracker* local = bt->cast<DistanceFieldNoGammaBatchTracker>(); |
| 525 | local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, |
| 526 | SkToBool(fInColor)); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 527 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | bool GrDistanceFieldNoGammaTextureEffect::onCanMakeEqual(const GrBatchTracker& m, |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 531 | const GrGeometryProcessor& that, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 532 | const GrBatchTracker& t) const { |
| 533 | const DistanceFieldNoGammaBatchTracker& mine = m.cast<DistanceFieldNoGammaBatchTracker>(); |
| 534 | const DistanceFieldNoGammaBatchTracker& theirs = t.cast<DistanceFieldNoGammaBatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 535 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 536 | that, theirs.fUsesLocalCoords) && |
| 537 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 538 | theirs.fInputColorType, theirs.fColor); |
| 539 | } |
| 540 | |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 541 | /////////////////////////////////////////////////////////////////////////////// |
| 542 | |
| 543 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldNoGammaTextureEffect); |
| 544 | |
| 545 | GrGeometryProcessor* GrDistanceFieldNoGammaTextureEffect::TestCreate(SkRandom* random, |
| 546 | GrContext*, |
| 547 | const GrDrawTargetCaps&, |
| 548 | GrTexture* textures[]) { |
| 549 | int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx |
| 550 | : GrProcessorUnitTest::kAlphaTextureIdx; |
| 551 | static const SkShader::TileMode kTileModes[] = { |
| 552 | SkShader::kClamp_TileMode, |
| 553 | SkShader::kRepeat_TileMode, |
| 554 | SkShader::kMirror_TileMode, |
| 555 | }; |
| 556 | SkShader::TileMode tileModes[] = { |
| 557 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 558 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 559 | }; |
| 560 | GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode |
| 561 | : GrTextureParams::kNone_FilterMode); |
| 562 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 563 | return GrDistanceFieldNoGammaTextureEffect::Create(GrRandomColor(random), |
| 564 | GrProcessorUnitTest::TestMatrix(random), |
| 565 | textures[texIdx], |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 566 | params, |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 567 | random->nextBool() ? kSimilarity_DistanceFieldEffectFlag : 0, random->nextBool()); |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /////////////////////////////////////////////////////////////////////////////// |
| 571 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 572 | struct DistanceFieldLCDBatchTracker { |
| 573 | GrGPInput fInputColorType; |
| 574 | GrColor fColor; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 575 | bool fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 576 | }; |
| 577 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 578 | class GrGLDistanceFieldLCDTextureEffect : public GrGLGeometryProcessor { |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 579 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 580 | GrGLDistanceFieldLCDTextureEffect(const GrGeometryProcessor&, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 581 | const GrBatchTracker&) |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 582 | : fColor(GrColor_ILLEGAL) |
| 583 | , fTextureSize(SkISize::Make(-1,-1)) |
jvanverth | 9564ce6 | 2014-09-16 05:45:19 -0700 | [diff] [blame] | 584 | , fTextColor(GrColor_ILLEGAL) {} |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 585 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 586 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) SK_OVERRIDE{ |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 587 | const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 588 | args.fGP.cast<GrDistanceFieldLCDTextureEffect>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 589 | const DistanceFieldLCDBatchTracker& local = args.fBT.cast<DistanceFieldLCDBatchTracker>(); |
| 590 | GrGLGPBuilder* pb = args.fPB; |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 591 | |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 592 | GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 593 | |
| 594 | // emit attributes |
| 595 | vsBuilder->emitAttributes(dfTexEffect); |
| 596 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 597 | GrGLVertToFrag v(kVec2f_GrSLType); |
| 598 | args.fPB->addVarying("TextureCoords", &v); |
| 599 | vsBuilder->codeAppendf("%s = %s;", v.vsOut(), dfTexEffect.inTextureCoords()->fName); |
| 600 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 601 | // setup pass through color |
| 602 | this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, |
| 603 | &fColorUniform); |
| 604 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 605 | // setup uniform viewMatrix |
| 606 | this->addUniformViewMatrix(pb); |
| 607 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 608 | // Setup position |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 609 | SetupPosition(vsBuilder, gpArgs, dfTexEffect.inPosition()->fName, |
| 610 | dfTexEffect.viewMatrix(), this->uViewM()); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 611 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 612 | // emit transforms |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 613 | this->emitTransforms(args.fPB, gpArgs->fPositionVar, dfTexEffect.inPosition()->fName, |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 614 | dfTexEffect.localMatrix(), args.fTransformsIn, args.fTransformsOut); |
| 615 | |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 616 | const char* textureSizeUniName = NULL; |
| 617 | // width, height, 1/(3*width) |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 618 | fTextureSizeUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 619 | kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 620 | "TextureSize", &textureSizeUniName); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 621 | |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 622 | GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 623 | |
| 624 | SkAssertResult(fsBuilder->enableFeature( |
| 625 | GrGLFragmentShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 626 | |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 627 | // create LCD offset adjusted by inverse of transform |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame^] | 628 | // Use highp to work around aliasing issues |
| 629 | fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision, |
| 630 | pb->ctxInfo().standard())); |
| 631 | fsBuilder->codeAppendf("vec2 uv = %s;\n", v.fsIn()); |
| 632 | fsBuilder->codeAppend(GrGLShaderVar::PrecisionString(kHigh_GrSLPrecision, |
| 633 | pb->ctxInfo().standard())); |
| 634 | fsBuilder->codeAppendf("vec2 st = uv*%s.xy;\n", textureSizeUniName); |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 635 | bool isUniformScale = !!(dfTexEffect.getFlags() & kUniformScale_DistanceFieldEffectMask); |
| 636 | if (isUniformScale) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 637 | fsBuilder->codeAppend("\tfloat dx = dFdx(st.x);\n"); |
| 638 | fsBuilder->codeAppendf("\tvec2 offset = vec2(dx*%s.z, 0.0);\n", textureSizeUniName); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 639 | } else { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 640 | fsBuilder->codeAppend("\tvec2 Jdx = dFdx(st);\n"); |
| 641 | fsBuilder->codeAppend("\tvec2 Jdy = dFdy(st);\n"); |
| 642 | fsBuilder->codeAppendf("\tvec2 offset = %s.z*Jdx;\n", textureSizeUniName); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | // green is distance to uv center |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 646 | fsBuilder->codeAppend("\tvec4 texColor = "); |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 647 | fsBuilder->appendTextureLookup(args.fSamplers[0], "uv", kVec2f_GrSLType); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 648 | fsBuilder->codeAppend(";\n"); |
| 649 | fsBuilder->codeAppend("\tvec3 distance;\n"); |
| 650 | fsBuilder->codeAppend("\tdistance.y = texColor.r;\n"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 651 | // red is distance to left offset |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 652 | fsBuilder->codeAppend("\tvec2 uv_adjusted = uv - offset;\n"); |
| 653 | fsBuilder->codeAppend("\ttexColor = "); |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 654 | fsBuilder->appendTextureLookup(args.fSamplers[0], "uv_adjusted", kVec2f_GrSLType); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 655 | fsBuilder->codeAppend(";\n"); |
| 656 | fsBuilder->codeAppend("\tdistance.x = texColor.r;\n"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 657 | // blue is distance to right offset |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 658 | fsBuilder->codeAppend("\tuv_adjusted = uv + offset;\n"); |
| 659 | fsBuilder->codeAppend("\ttexColor = "); |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 660 | fsBuilder->appendTextureLookup(args.fSamplers[0], "uv_adjusted", kVec2f_GrSLType); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 661 | fsBuilder->codeAppend(";\n"); |
| 662 | fsBuilder->codeAppend("\tdistance.z = texColor.r;\n"); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 663 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 664 | fsBuilder->codeAppend("\tdistance = " |
jvanverth | ada68ef | 2014-11-03 14:00:24 -0800 | [diff] [blame] | 665 | "vec3(" SK_DistanceFieldMultiplier ")*(distance - vec3(" SK_DistanceFieldThreshold"));"); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 666 | |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 667 | // we adjust for the effect of the transformation on the distance by using |
| 668 | // the length of the gradient of the texture coordinates. We use st coordinates |
| 669 | // to ensure we're mapping 1:1 from texel space to pixel space. |
| 670 | |
| 671 | // To be strictly correct, we should compute the anti-aliasing factor separately |
| 672 | // for each color component. However, this is only important when using perspective |
| 673 | // transformations, and even then using a single factor seems like a reasonable |
| 674 | // trade-off between quality and speed. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 675 | fsBuilder->codeAppend("\tfloat afwidth;\n"); |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 676 | if (isUniformScale) { |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 677 | // this gives us a smooth step across approximately one fragment |
jvanverth | fa38a30 | 2014-10-06 05:59:05 -0700 | [diff] [blame] | 678 | fsBuilder->codeAppend("\tafwidth = abs(" SK_DistanceFieldAAFactor "*dx);\n"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 679 | } else { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 680 | fsBuilder->codeAppend("\tvec2 uv_grad;\n"); |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 681 | if (args.fPB->ctxInfo().caps()->dropsTileOnZeroDivide()) { |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 682 | // this is to compensate for the Adreno, which likes to drop tiles on division by 0 |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 683 | fsBuilder->codeAppend("\tfloat uv_len2 = dot(uv, uv);\n"); |
| 684 | fsBuilder->codeAppend("\tif (uv_len2 < 0.0001) {\n"); |
| 685 | fsBuilder->codeAppend("\t\tuv_grad = vec2(0.7071, 0.7071);\n"); |
| 686 | fsBuilder->codeAppend("\t} else {\n"); |
| 687 | fsBuilder->codeAppend("\t\tuv_grad = uv*inversesqrt(uv_len2);\n"); |
| 688 | fsBuilder->codeAppend("\t}\n"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 689 | } else { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 690 | fsBuilder->codeAppend("\tuv_grad = normalize(uv);\n"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 691 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 692 | fsBuilder->codeAppend("\tvec2 grad = vec2(uv_grad.x*Jdx.x + uv_grad.y*Jdy.x,\n"); |
| 693 | fsBuilder->codeAppend("\t uv_grad.x*Jdx.y + uv_grad.y*Jdy.y);\n"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 694 | |
| 695 | // this gives us a smooth step across approximately one fragment |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 696 | fsBuilder->codeAppend("\tafwidth = " SK_DistanceFieldAAFactor "*length(grad);\n"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 697 | } |
| 698 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 699 | fsBuilder->codeAppend("\tvec4 val = vec4(smoothstep(vec3(-afwidth), vec3(afwidth), distance), 1.0);\n"); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 700 | |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 701 | // adjust based on gamma |
| 702 | const char* textColorUniName = NULL; |
| 703 | // width, height, 1/(3*width) |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 704 | fTextColorUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 705 | kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 706 | "TextColor", &textColorUniName); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 707 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 708 | fsBuilder->codeAppendf("\tuv = vec2(val.x, %s.x);\n", textColorUniName); |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame^] | 709 | fsBuilder->codeAppend("float gammaColor = "); |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 710 | fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType); |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame^] | 711 | fsBuilder->codeAppend(".r;\n"); |
| 712 | fsBuilder->codeAppend("\tval.x = gammaColor;\n"); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 713 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 714 | fsBuilder->codeAppendf("\tuv = vec2(val.y, %s.y);\n", textColorUniName); |
| 715 | fsBuilder->codeAppend("\tgammaColor = "); |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 716 | fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType); |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame^] | 717 | fsBuilder->codeAppend(".r;\n"); |
| 718 | fsBuilder->codeAppend("\tval.y = gammaColor;\n"); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 719 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 720 | fsBuilder->codeAppendf("\tuv = vec2(val.z, %s.z);\n", textColorUniName); |
| 721 | fsBuilder->codeAppend("\tgammaColor = "); |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 722 | fsBuilder->appendTextureLookup(args.fSamplers[1], "uv", kVec2f_GrSLType); |
jvanverth | fdf7ccc | 2015-01-27 08:19:33 -0800 | [diff] [blame^] | 723 | fsBuilder->codeAppend(".r;\n"); |
| 724 | fsBuilder->codeAppend("\tval.z = gammaColor;\n"); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 725 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 726 | fsBuilder->codeAppendf("%s = vec4(val);", args.fOutputCoverage); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 727 | } |
| 728 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 729 | virtual void setData(const GrGLProgramDataManager& pdman, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 730 | const GrPrimitiveProcessor& processor, |
| 731 | const GrBatchTracker& bt) SK_OVERRIDE { |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 732 | SkASSERT(fTextureSizeUni.isValid()); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 733 | SkASSERT(fTextColorUni.isValid()); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 734 | |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 735 | const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 736 | processor.cast<GrDistanceFieldLCDTextureEffect>(); |
| 737 | GrTexture* texture = processor.texture(0); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 738 | if (texture->width() != fTextureSize.width() || |
| 739 | texture->height() != fTextureSize.height()) { |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 740 | fTextureSize = SkISize::Make(texture->width(), texture->height()); |
| 741 | float delta = 1.0f/(3.0f*texture->width()); |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 742 | if (dfTexEffect.getFlags() & kBGR_DistanceFieldEffectFlag) { |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 743 | delta = -delta; |
| 744 | } |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 745 | pdman.set3f(fTextureSizeUni, |
| 746 | SkIntToScalar(fTextureSize.width()), |
| 747 | SkIntToScalar(fTextureSize.height()), |
| 748 | delta); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 749 | } |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 750 | |
| 751 | GrColor textColor = dfTexEffect.getTextColor(); |
| 752 | if (textColor != fTextColor) { |
| 753 | static const float ONE_OVER_255 = 1.f / 255.f; |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 754 | pdman.set3f(fTextColorUni, |
| 755 | GrColorUnpackR(textColor) * ONE_OVER_255, |
| 756 | GrColorUnpackG(textColor) * ONE_OVER_255, |
| 757 | GrColorUnpackB(textColor) * ONE_OVER_255); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 758 | fTextColor = textColor; |
| 759 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 760 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 761 | this->setUniformViewMatrix(pdman, processor.viewMatrix()); |
| 762 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 763 | const DistanceFieldLCDBatchTracker& local = bt.cast<DistanceFieldLCDBatchTracker>(); |
| 764 | if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) { |
| 765 | GrGLfloat c[4]; |
| 766 | GrColorToRGBAFloat(local.fColor, c); |
| 767 | pdman.set4fv(fColorUniform, 1, c); |
| 768 | fColor = local.fColor; |
| 769 | } |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 770 | } |
| 771 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 772 | static inline void GenKey(const GrGeometryProcessor& gp, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 773 | const GrBatchTracker& bt, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 774 | const GrGLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 775 | GrProcessorKeyBuilder* b) { |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 776 | const GrDistanceFieldLCDTextureEffect& dfTexEffect = |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 777 | gp.cast<GrDistanceFieldLCDTextureEffect>(); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 778 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 779 | const DistanceFieldLCDBatchTracker& local = bt.cast<DistanceFieldLCDBatchTracker>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 780 | uint32_t key = dfTexEffect.getFlags(); |
| 781 | key |= local.fInputColorType << 16; |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 782 | key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 << 24: 0x0; |
| 783 | key |= ComputePosKey(gp.viewMatrix()) << 25; |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 784 | b->add32(key); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 788 | GrColor fColor; |
| 789 | UniformHandle fColorUniform; |
| 790 | UniformHandle fTextureSizeUni; |
| 791 | SkISize fTextureSize; |
| 792 | UniformHandle fTextColorUni; |
| 793 | SkColor fTextColor; |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 794 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 795 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 796 | }; |
| 797 | |
| 798 | /////////////////////////////////////////////////////////////////////////////// |
| 799 | |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 800 | GrDistanceFieldLCDTextureEffect::GrDistanceFieldLCDTextureEffect( |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 801 | GrColor color, const SkMatrix& viewMatrix, |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 802 | GrTexture* texture, const GrTextureParams& params, |
| 803 | GrTexture* gamma, const GrTextureParams& gParams, |
| 804 | SkColor textColor, |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 805 | uint32_t flags) |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 806 | : INHERITED(color, viewMatrix, SkMatrix::I()) |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 807 | , fTextureAccess(texture, params) |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 808 | , fGammaTextureAccess(gamma, gParams) |
| 809 | , fTextColor(textColor) |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 810 | , fFlags(flags & kLCD_DistanceFieldEffectMask){ |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 811 | SkASSERT(!(flags & ~kLCD_DistanceFieldEffectMask) && (flags & kUseLCD_DistanceFieldEffectFlag)); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 812 | this->initClassID<GrDistanceFieldLCDTextureEffect>(); |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 813 | fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType)); |
| 814 | fInTextureCoords = &this->addVertexAttrib(Attribute("inTextureCoords", |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 815 | kVec2f_GrVertexAttribType)); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 816 | this->addTextureAccess(&fTextureAccess); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 817 | this->addTextureAccess(&fGammaTextureAccess); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 818 | } |
| 819 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 820 | bool GrDistanceFieldLCDTextureEffect::onIsEqual(const GrGeometryProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 821 | const GrDistanceFieldLCDTextureEffect& cte = other.cast<GrDistanceFieldLCDTextureEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 822 | return (fTextColor == cte.fTextColor && |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 823 | fFlags == cte.fFlags); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 824 | } |
| 825 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 826 | void GrDistanceFieldLCDTextureEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const { |
| 827 | out->setUnknownFourComponents(); |
| 828 | out->setUsingLCDCoverage(); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 829 | } |
| 830 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 831 | void GrDistanceFieldLCDTextureEffect::getGLProcessorKey(const GrBatchTracker& bt, |
| 832 | const GrGLCaps& caps, |
| 833 | GrProcessorKeyBuilder* b) const { |
| 834 | GrGLDistanceFieldLCDTextureEffect::GenKey(*this, bt, caps, b); |
| 835 | } |
| 836 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 837 | GrGLPrimitiveProcessor* |
| 838 | GrDistanceFieldLCDTextureEffect::createGLInstance(const GrBatchTracker& bt, |
| 839 | const GrGLCaps&) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 840 | return SkNEW_ARGS(GrGLDistanceFieldLCDTextureEffect, (*this, bt)); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 841 | } |
| 842 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 843 | void GrDistanceFieldLCDTextureEffect::initBatchTracker(GrBatchTracker* bt, |
joshualitt | ca0a179 | 2015-01-27 06:41:33 -0800 | [diff] [blame] | 844 | const InitBT& init) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 845 | DistanceFieldLCDBatchTracker* local = bt->cast<DistanceFieldLCDBatchTracker>(); |
| 846 | local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 847 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | bool GrDistanceFieldLCDTextureEffect::onCanMakeEqual(const GrBatchTracker& m, |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 851 | const GrGeometryProcessor& that, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 852 | const GrBatchTracker& t) const { |
| 853 | const DistanceFieldLCDBatchTracker& mine = m.cast<DistanceFieldLCDBatchTracker>(); |
| 854 | const DistanceFieldLCDBatchTracker& theirs = t.cast<DistanceFieldLCDBatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 855 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 856 | that, theirs.fUsesLocalCoords) && |
| 857 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 858 | theirs.fInputColorType, theirs.fColor); |
| 859 | } |
| 860 | |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 861 | /////////////////////////////////////////////////////////////////////////////// |
| 862 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 863 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrDistanceFieldLCDTextureEffect); |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 864 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 865 | GrGeometryProcessor* GrDistanceFieldLCDTextureEffect::TestCreate(SkRandom* random, |
| 866 | GrContext*, |
| 867 | const GrDrawTargetCaps&, |
| 868 | GrTexture* textures[]) { |
| 869 | int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 870 | GrProcessorUnitTest::kAlphaTextureIdx; |
| 871 | int texIdx2 = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 872 | GrProcessorUnitTest::kAlphaTextureIdx; |
commit-bot@chromium.org | 609ced4 | 2014-04-03 18:25:48 +0000 | [diff] [blame] | 873 | static const SkShader::TileMode kTileModes[] = { |
| 874 | SkShader::kClamp_TileMode, |
| 875 | SkShader::kRepeat_TileMode, |
| 876 | SkShader::kMirror_TileMode, |
| 877 | }; |
| 878 | SkShader::TileMode tileModes[] = { |
| 879 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 880 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 881 | }; |
| 882 | GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
| 883 | GrTextureParams::kNone_FilterMode); |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 884 | GrTextureParams params2(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
| 885 | GrTextureParams::kNone_FilterMode); |
| 886 | GrColor textColor = GrColorPackRGBA(random->nextULessThan(256), |
| 887 | random->nextULessThan(256), |
| 888 | random->nextULessThan(256), |
| 889 | random->nextULessThan(256)); |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 890 | uint32_t flags = kUseLCD_DistanceFieldEffectFlag; |
| 891 | flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; |
| 892 | flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 893 | return GrDistanceFieldLCDTextureEffect::Create(GrRandomColor(random), |
| 894 | GrProcessorUnitTest::TestMatrix(random), |
| 895 | textures[texIdx], params, |
jvanverth | 2d2a68c | 2014-06-10 06:42:56 -0700 | [diff] [blame] | 896 | textures[texIdx2], params2, |
| 897 | textColor, |
jvanverth | 78f0718 | 2014-07-30 06:17:59 -0700 | [diff] [blame] | 898 | flags); |
jvanverth@google.com | d830d13 | 2013-11-11 20:54:09 +0000 | [diff] [blame] | 899 | } |