| 1 | /* |
| 2 | * Copyright 2014 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 "src/gpu/effects/GrBicubicEffect.h" |
| 9 | |
| 10 | #include "src/core/SkMatrixPriv.h" |
| 11 | #include "src/gpu/GrTexture.h" |
| 12 | #include "src/gpu/effects/GrTextureEffect.h" |
| 13 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 14 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
| 15 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
| 16 | |
| 17 | class GrBicubicEffect::Impl : public GrGLSLFragmentProcessor { |
| 18 | public: |
| 19 | void emitCode(EmitArgs&) override; |
| 20 | |
| 21 | private: |
| 22 | typedef GrGLSLFragmentProcessor INHERITED; |
| 23 | }; |
| 24 | |
| 25 | void GrBicubicEffect::Impl::emitCode(EmitArgs& args) { |
| 26 | const GrBicubicEffect& bicubicEffect = args.fFp.cast<GrBicubicEffect>(); |
| 27 | |
| 28 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 29 | SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint); |
| 30 | |
| 31 | /* |
| 32 | * Filter weights come from Don Mitchell & Arun Netravali's 'Reconstruction Filters in Computer |
| 33 | * Graphics', ACM SIGGRAPH Computer Graphics 22, 4 (Aug. 1988). |
| 34 | * ACM DL: http://dl.acm.org/citation.cfm?id=378514 |
| 35 | * Free : http://www.cs.utexas.edu/users/fussell/courses/cs384g/lectures/mitchell/Mitchell.pdf |
| 36 | * |
| 37 | * The authors define a family of cubic filters with two free parameters (B and C): |
| 38 | * |
| 39 | * { (12 - 9B - 6C)|x|^3 + (-18 + 12B + 6C)|x|^2 + (6 - 2B) if |x| < 1 |
| 40 | * k(x) = 1/6 { (-B - 6C)|x|^3 + (6B + 30C)|x|^2 + (-12B - 48C)|x| + (8B + 24C) if 1 <= |x| < 2 |
| 41 | * { 0 otherwise |
| 42 | * |
| 43 | * Various well-known cubic splines can be generated, and the authors select (1/3, 1/3) as their |
| 44 | * favorite overall spline - this is now commonly known as the Mitchell filter, and is the |
| 45 | * source of the specific weights below. |
| 46 | * |
| 47 | * This is SkSL, so the matrix is column-major (transposed from standard matrix notation). |
| 48 | */ |
| 49 | fragBuilder->codeAppend("half4x4 kMitchellCoefficients = half4x4(" |
| 50 | " 1.0 / 18.0, 16.0 / 18.0, 1.0 / 18.0, 0.0 / 18.0," |
| 51 | "-9.0 / 18.0, 0.0 / 18.0, 9.0 / 18.0, 0.0 / 18.0," |
| 52 | "15.0 / 18.0, -36.0 / 18.0, 27.0 / 18.0, -6.0 / 18.0," |
| 53 | "-7.0 / 18.0, 21.0 / 18.0, -21.0 / 18.0, 7.0 / 18.0);" ); |
| 54 | // We determine our fractional offset (f) within the texel. We then snap coord to a texel |
| 55 | // center. The snap prevents cases where the starting coords are near a texel boundary and |
| 56 | // offsets with imperfect precision would cause us to skip/double hit a texel. |
| 57 | // The use of "texel" above is somewhat abstract as we're sampling a child processor. It is |
| 58 | // assumed the child processor represents something akin to a nearest neighbor sampled texture. |
| 59 | if (bicubicEffect.fDirection == GrBicubicEffect::Direction::kXY) { |
| 60 | fragBuilder->codeAppendf("float2 coord = %s - float2(0.5);" , coords2D.c_str()); |
| 61 | fragBuilder->codeAppend("half2 f = half2(fract(coord));" ); |
| 62 | fragBuilder->codeAppend("coord += 0.5 - f;" ); |
| 63 | fragBuilder->codeAppend( |
| 64 | "half4 wx = kMitchellCoefficients * half4(1.0, f.x, f.x * f.x, f.x * f.x * f.x);" ); |
| 65 | fragBuilder->codeAppend( |
| 66 | "half4 wy = kMitchellCoefficients * half4(1.0, f.y, f.y * f.y, f.y * f.y * f.y);" ); |
| 67 | fragBuilder->codeAppend("half4 rowColors[4];" ); |
| 68 | for (int y = 0; y < 4; ++y) { |
| 69 | for (int x = 0; x < 4; ++x) { |
| 70 | SkString coord; |
| 71 | coord.printf("coord + float2(%d, %d)" , x - 1, y - 1); |
| 72 | auto childStr = |
| 73 | this->invokeChild(0, args, SkSL::String(coord.c_str(), coord.size())); |
| 74 | fragBuilder->codeAppendf("rowColors[%d] = %s;" , x, childStr.c_str()); |
| 75 | } |
| 76 | fragBuilder->codeAppendf( |
| 77 | "half4 s%d = wx.x * rowColors[0] + wx.y * rowColors[1] + wx.z * rowColors[2] + " |
| 78 | "wx.w * rowColors[3];" , |
| 79 | y); |
| 80 | } |
| 81 | fragBuilder->codeAppend( |
| 82 | "half4 bicubicColor = wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3;" ); |
| 83 | } else { |
| 84 | const char* d = bicubicEffect.fDirection == Direction::kX ? "x" : "y" ; |
| 85 | fragBuilder->codeAppendf("float coord = %s.%s - 0.5;" , coords2D.c_str(), d); |
| 86 | fragBuilder->codeAppend("half f = half(fract(coord));" ); |
| 87 | fragBuilder->codeAppend("coord += 0.5 - f;" ); |
| 88 | fragBuilder->codeAppend("half f2 = f * f;" ); |
| 89 | fragBuilder->codeAppend("half4 w = kMitchellCoefficients * half4(1.0, f, f2, f2 * f);" ); |
| 90 | fragBuilder->codeAppend("half4 c[4];" ); |
| 91 | for (int i = 0; i < 4; ++i) { |
| 92 | SkString coord; |
| 93 | if (bicubicEffect.fDirection == Direction::kX) { |
| 94 | coord.printf("float2(coord + %d, %s.y)" , i - 1, coords2D.c_str()); |
| 95 | } else { |
| 96 | coord.printf("float2(%s.x, coord + %d)" , coords2D.c_str(), i - 1); |
| 97 | } |
| 98 | auto childStr = this->invokeChild(0, args, SkSL::String(coord.c_str(), coord.size())); |
| 99 | fragBuilder->codeAppendf("c[%d] = %s;" , i, childStr.c_str()); |
| 100 | } |
| 101 | fragBuilder->codeAppend( |
| 102 | "half4 bicubicColor = c[0] * w.x + c[1] * w.y + c[2] * w.z + c[3] * w.w;" ); |
| 103 | } |
| 104 | // Bicubic can send colors out of range, so clamp to get them back in (source) gamut. |
| 105 | // The kind of clamp we have to do depends on the alpha type. |
| 106 | switch (bicubicEffect.fClamp) { |
| 107 | case Clamp::kUnpremul: |
| 108 | fragBuilder->codeAppend("bicubicColor = saturate(bicubicColor);" ); |
| 109 | break; |
| 110 | case Clamp::kPremul: |
| 111 | fragBuilder->codeAppend( |
| 112 | "bicubicColor.rgb = max(half3(0.0), min(bicubicColor.rgb, bicubicColor.aaa));" ); |
| 113 | break; |
| 114 | } |
| 115 | fragBuilder->codeAppendf("%s = bicubicColor * %s;" , args.fOutputColor, args.fInputColor); |
| 116 | } |
| 117 | |
| 118 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(GrSurfaceProxyView view, |
| 119 | SkAlphaType alphaType, |
| 120 | const SkMatrix& matrix, |
| 121 | Direction direction) { |
| 122 | auto fp = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I()); |
| 123 | auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
| 124 | return std::unique_ptr<GrFragmentProcessor>( |
| 125 | new GrBicubicEffect(std::move(fp), matrix, direction, clamp)); |
| 126 | } |
| 127 | |
| 128 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(GrSurfaceProxyView view, |
| 129 | SkAlphaType alphaType, |
| 130 | const SkMatrix& matrix, |
| 131 | const GrSamplerState::WrapMode wrapX, |
| 132 | const GrSamplerState::WrapMode wrapY, |
| 133 | Direction direction, |
| 134 | const GrCaps& caps) { |
| 135 | GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest); |
| 136 | std::unique_ptr<GrFragmentProcessor> fp; |
| 137 | fp = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I(), sampler, caps); |
| 138 | auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
| 139 | return std::unique_ptr<GrFragmentProcessor>( |
| 140 | new GrBicubicEffect(std::move(fp), matrix, direction, clamp)); |
| 141 | } |
| 142 | |
| 143 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::MakeSubset( |
| 144 | GrSurfaceProxyView view, |
| 145 | SkAlphaType alphaType, |
| 146 | const SkMatrix& matrix, |
| 147 | const GrSamplerState::WrapMode wrapX, |
| 148 | const GrSamplerState::WrapMode wrapY, |
| 149 | const SkRect& subset, |
| 150 | Direction direction, |
| 151 | const GrCaps& caps) { |
| 152 | GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest); |
| 153 | std::unique_ptr<GrFragmentProcessor> fp; |
| 154 | fp = GrTextureEffect::MakeSubset( |
| 155 | std::move(view), alphaType, SkMatrix::I(), sampler, subset, caps); |
| 156 | auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
| 157 | return std::unique_ptr<GrFragmentProcessor>( |
| 158 | new GrBicubicEffect(std::move(fp), matrix, direction, clamp)); |
| 159 | } |
| 160 | |
| 161 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(std::unique_ptr<GrFragmentProcessor> fp, |
| 162 | SkAlphaType alphaType, |
| 163 | const SkMatrix& matrix, |
| 164 | Direction direction) { |
| 165 | auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
| 166 | return std::unique_ptr<GrFragmentProcessor>( |
| 167 | new GrBicubicEffect(std::move(fp), matrix, direction, clamp)); |
| 168 | } |
| 169 | |
| 170 | GrBicubicEffect::GrBicubicEffect(std::unique_ptr<GrFragmentProcessor> fp, |
| 171 | const SkMatrix& matrix, |
| 172 | Direction direction, |
| 173 | Clamp clamp) |
| 174 | : INHERITED(kGrBicubicEffect_ClassID, ProcessorOptimizationFlags(fp.get())) |
| 175 | , fCoordTransform(matrix) |
| 176 | , fDirection(direction) |
| 177 | , fClamp(clamp) { |
| 178 | fp->setSampledWithExplicitCoords(true); |
| 179 | this->addCoordTransform(&fCoordTransform); |
| 180 | this->registerChildProcessor(std::move(fp)); |
| 181 | } |
| 182 | |
| 183 | GrBicubicEffect::GrBicubicEffect(const GrBicubicEffect& that) |
| 184 | : INHERITED(kGrBicubicEffect_ClassID, that.optimizationFlags()) |
| 185 | , fCoordTransform(that.fCoordTransform) |
| 186 | , fDirection(that.fDirection) |
| 187 | , fClamp(that.fClamp) { |
| 188 | this->addCoordTransform(&fCoordTransform); |
| 189 | auto child = that.childProcessor(0).clone(); |
| 190 | child->setSampledWithExplicitCoords(true); |
| 191 | this->registerChildProcessor(std::move(child)); |
| 192 | } |
| 193 | |
| 194 | void GrBicubicEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 195 | GrProcessorKeyBuilder* b) const { |
| 196 | uint32_t key = static_cast<uint32_t>(fDirection) | (static_cast<uint32_t>(fClamp) << 2); |
| 197 | b->add32(key); |
| 198 | } |
| 199 | |
| 200 | GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const { return new Impl(); } |
| 201 | |
| 202 | bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 203 | const auto& that = other.cast<GrBicubicEffect>(); |
| 204 | return fDirection == that.fDirection && fClamp == that.fClamp; |
| 205 | } |
| 206 | |
| 207 | SkPMColor4f GrBicubicEffect::constantOutputForConstantInput(const SkPMColor4f& input) const { |
| 208 | return GrFragmentProcessor::ConstantOutputForConstantInput(this->childProcessor(0), input); |
| 209 | } |
| 210 | |
| 211 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect); |
| 212 | |
| 213 | #if GR_TEST_UTILS |
| 214 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) { |
| 215 | Direction direction = Direction::kX; |
| 216 | switch (d->fRandom->nextULessThan(3)) { |
| 217 | case 0: |
| 218 | direction = Direction::kX; |
| 219 | break; |
| 220 | case 1: |
| 221 | direction = Direction::kY; |
| 222 | break; |
| 223 | case 2: |
| 224 | direction = Direction::kXY; |
| 225 | break; |
| 226 | } |
| 227 | auto m = GrTest::TestMatrix(d->fRandom); |
| 228 | switch (d->fRandom->nextULessThan(3)) { |
| 229 | case 0: { |
| 230 | auto [view, ct, at] = d->randomView(); |
| 231 | GrSamplerState::WrapMode wm[2]; |
| 232 | GrTest::TestWrapModes(d->fRandom, wm); |
| 233 | |
| 234 | if (d->fRandom->nextBool()) { |
| 235 | SkRect subset; |
| 236 | subset.fLeft = d->fRandom->nextSScalar1() * view.width(); |
| 237 | subset.fTop = d->fRandom->nextSScalar1() * view.height(); |
| 238 | subset.fRight = d->fRandom->nextSScalar1() * view.width(); |
| 239 | subset.fBottom = d->fRandom->nextSScalar1() * view.height(); |
| 240 | subset.sort(); |
| 241 | return MakeSubset( |
| 242 | std::move(view), at, m, wm[0], wm[1], subset, direction, *d->caps()); |
| 243 | } |
| 244 | return Make(std::move(view), at, m, wm[0], wm[1], direction, *d->caps()); |
| 245 | } |
| 246 | case 1: { |
| 247 | auto [view, ct, at] = d->randomView(); |
| 248 | return Make(std::move(view), at, m, direction); |
| 249 | } |
| 250 | default: { |
| 251 | SkAlphaType at; |
| 252 | do { |
| 253 | at = static_cast<SkAlphaType>(d->fRandom->nextULessThan(kLastEnum_SkAlphaType + 1)); |
| 254 | } while (at != kUnknown_SkAlphaType); |
| 255 | std::unique_ptr<GrFragmentProcessor> fp; |
| 256 | // We have a restriction that explicit coords only work for FPs with zero or one |
| 257 | // coord transform. |
| 258 | do { |
| 259 | fp = GrProcessorUnitTest::MakeChildFP(d); |
| 260 | } while (fp->numCoordTransforms() > 1); |
| 261 | return Make(std::move(fp), at, m, direction); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | #endif |
| 266 | |
| 267 | ////////////////////////////////////////////////////////////////////////////// |
| 268 | |
| 269 | bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix, GrSamplerState::Filter* filterMode) { |
| 270 | switch (SkMatrixPriv::AdjustHighQualityFilterLevel(matrix)) { |
| 271 | case kNone_SkFilterQuality: |
| 272 | *filterMode = GrSamplerState::Filter::kNearest; |
| 273 | break; |
| 274 | case kLow_SkFilterQuality: |
| 275 | *filterMode = GrSamplerState::Filter::kBilerp; |
| 276 | break; |
| 277 | case kMedium_SkFilterQuality: |
| 278 | *filterMode = GrSamplerState::Filter::kMipMap; |
| 279 | break; |
| 280 | case kHigh_SkFilterQuality: |
| 281 | // When we use the bicubic filtering effect each sample is read from the texture using |
| 282 | // nearest neighbor sampling. |
| 283 | *filterMode = GrSamplerState::Filter::kNearest; |
| 284 | return true; |
| 285 | } |
| 286 | return false; |
| 287 | } |
| 288 | |