| 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/GrMatrixEffect.h" |
| 13 | #include "src/gpu/effects/GrTextureEffect.h" |
| 14 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 15 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
| 16 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
| 17 | #include <cmath> |
| 18 | |
| 19 | class GrBicubicEffect::Impl : public GrGLSLFragmentProcessor { |
| 20 | public: |
| 21 | Impl() : fCoefficients(SkM44::kNaN_Constructor) {} |
| 22 | void emitCode(EmitArgs&) override; |
| 23 | |
| 24 | protected: |
| 25 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
| 26 | |
| 27 | private: |
| 28 | SkM44 fCoefficients; |
| 29 | UniformHandle fCoefficientUni; |
| 30 | typedef GrGLSLFragmentProcessor INHERITED; |
| 31 | }; |
| 32 | |
| 33 | void GrBicubicEffect::Impl::emitCode(EmitArgs& args) { |
| 34 | const GrBicubicEffect& bicubicEffect = args.fFp.cast<GrBicubicEffect>(); |
| 35 | |
| 36 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 37 | |
| 38 | const char* coeffs; |
| 39 | fCoefficientUni = args.fUniformHandler->addUniform(&args.fFp, kFragment_GrShaderFlag, |
| 40 | kHalf4x4_GrSLType, "coefficients" , &coeffs); |
| 41 | // We determine our fractional offset (f) within the texel. We then snap coord to a texel |
| 42 | // center. The snap prevents cases where the starting coords are near a texel boundary and |
| 43 | // offsets with imperfect precision would cause us to skip/double hit a texel. |
| 44 | // The use of "texel" above is somewhat abstract as we're sampling a child processor. It is |
| 45 | // assumed the child processor represents something akin to a nearest neighbor sampled texture. |
| 46 | if (bicubicEffect.fDirection == GrBicubicEffect::Direction::kXY) { |
| 47 | fragBuilder->codeAppendf("float2 coord = %s - float2(0.5);" , args.fSampleCoord); |
| 48 | fragBuilder->codeAppend("half2 f = half2(fract(coord));" ); |
| 49 | fragBuilder->codeAppend("coord += 0.5 - f;" ); |
| 50 | fragBuilder->codeAppendf("half4 wx = %s * half4(1.0, f.x, f.x * f.x, f.x * f.x * f.x);" , |
| 51 | coeffs); |
| 52 | fragBuilder->codeAppendf("half4 wy = %s * half4(1.0, f.y, f.y * f.y, f.y * f.y * f.y);" , |
| 53 | coeffs); |
| 54 | fragBuilder->codeAppend("half4 rowColors[4];" ); |
| 55 | for (int y = 0; y < 4; ++y) { |
| 56 | for (int x = 0; x < 4; ++x) { |
| 57 | SkString coord; |
| 58 | coord.printf("coord + float2(%d, %d)" , x - 1, y - 1); |
| 59 | auto childStr = |
| 60 | this->invokeChild(0, args, SkSL::String(coord.c_str(), coord.size())); |
| 61 | fragBuilder->codeAppendf("rowColors[%d] = %s;" , x, childStr.c_str()); |
| 62 | } |
| 63 | fragBuilder->codeAppendf( |
| 64 | "half4 s%d = wx.x * rowColors[0] + wx.y * rowColors[1] + wx.z * rowColors[2] + " |
| 65 | "wx.w * rowColors[3];" , |
| 66 | y); |
| 67 | } |
| 68 | fragBuilder->codeAppend( |
| 69 | "half4 bicubicColor = wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3;" ); |
| 70 | } else { |
| 71 | const char* d = bicubicEffect.fDirection == Direction::kX ? "x" : "y" ; |
| 72 | fragBuilder->codeAppendf("float coord = %s.%s - 0.5;" , args.fSampleCoord, d); |
| 73 | fragBuilder->codeAppend("half f = half(fract(coord));" ); |
| 74 | fragBuilder->codeAppend("coord += 0.5 - f;" ); |
| 75 | fragBuilder->codeAppend("half f2 = f * f;" ); |
| 76 | fragBuilder->codeAppendf("half4 w = %s * half4(1.0, f, f2, f2 * f);" , coeffs); |
| 77 | fragBuilder->codeAppend("half4 c[4];" ); |
| 78 | for (int i = 0; i < 4; ++i) { |
| 79 | SkString coord; |
| 80 | if (bicubicEffect.fDirection == Direction::kX) { |
| 81 | coord.printf("float2(coord + %d, %s.y)" , i - 1, args.fSampleCoord); |
| 82 | } else { |
| 83 | coord.printf("float2(%s.x, coord + %d)" , args.fSampleCoord, i - 1); |
| 84 | } |
| 85 | auto childStr = this->invokeChild(0, args, SkSL::String(coord.c_str(), coord.size())); |
| 86 | fragBuilder->codeAppendf("c[%d] = %s;" , i, childStr.c_str()); |
| 87 | } |
| 88 | fragBuilder->codeAppend( |
| 89 | "half4 bicubicColor = c[0] * w.x + c[1] * w.y + c[2] * w.z + c[3] * w.w;" ); |
| 90 | } |
| 91 | // Bicubic can send colors out of range, so clamp to get them back in (source) gamut. |
| 92 | // The kind of clamp we have to do depends on the alpha type. |
| 93 | switch (bicubicEffect.fClamp) { |
| 94 | case Clamp::kUnpremul: |
| 95 | fragBuilder->codeAppend("bicubicColor = saturate(bicubicColor);" ); |
| 96 | break; |
| 97 | case Clamp::kPremul: |
| 98 | fragBuilder->codeAppend( |
| 99 | "bicubicColor.rgb = max(half3(0.0), min(bicubicColor.rgb, bicubicColor.aaa));" ); |
| 100 | break; |
| 101 | } |
| 102 | fragBuilder->codeAppendf("%s = bicubicColor;" , args.fOutputColor); |
| 103 | } |
| 104 | |
| 105 | void GrBicubicEffect::Impl::onSetData(const GrGLSLProgramDataManager& pdm, |
| 106 | const GrFragmentProcessor& fp) { |
| 107 | auto& bicubicEffect = fp.cast<GrBicubicEffect>(); |
| 108 | const SkM44* coeffs = nullptr; |
| 109 | switch (bicubicEffect.fKernel) { |
| 110 | case Kernel::kMitchell: { |
| 111 | /* |
| 112 | Filter weights come from Don Mitchell & Arun Netravali's 'Reconstruction Filters in\ |
| 113 | Computer * Graphics', ACM SIGGRAPH Computer Graphics 22, 4 (Aug. 1988). |
| 114 | ACM DL: http://dl.acm.org/citation.cfm?id=378514 |
| 115 | |
| 116 | The authors define a family of cubic filters with two free parameters (B and C): |
| 117 | {(12 - 9B - 6C)|x|^3 + (-18 + 12B + 6C)|x|^2 + (6 - 2B) |x| < 1 |
| 118 | k(x) = 1/6 {(-B - 6C)|x|^3 + (6B + 30C)|x|^2 + (-12B - 48C)|x| + (8B + 24C) 1 <= |x| < 2 |
| 119 | {0 otherwise |
| 120 | |
| 121 | Various well-known cubic splines can be generated, and the authors select (1/3, 1/3) as |
| 122 | their favorite overall spline - this is now commonly known as the Mitchell filter, and |
| 123 | is the source of the specific weights below. |
| 124 | */ |
| 125 | static constexpr SkM44 kMitchell( 1.f/18.f, -9.f/18.f, 15.f/18.f, -7.f/18.f, |
| 126 | 16.f/18.f, 0.f/18.f, -36.f/18.f, 21.f/18.f, |
| 127 | 1.f/18.f, 9.f/18.f, 27.f/18.f, -21.f/18.f, |
| 128 | 0.f/18.f, 0.f/18.f, -6.f/18.f, 7.f/18.f); |
| 129 | coeffs = &kMitchell; |
| 130 | break; |
| 131 | } |
| 132 | case Kernel::kCatmullRom: { |
| 133 | /* |
| 134 | Centripetal Catmull-Rom filter. From the same family with (B, C) = (0, 1/2). |
| 135 | Catmull, Edwin; Rom, Raphael (1974). "A class of local interpolating splines". In |
| 136 | Barnhill, Robert E.; Riesenfeld, Richard F. (eds.). Computer Aided Geometric Design. |
| 137 | pp. 317–326. |
| 138 | */ |
| 139 | static constexpr SkM44 kCatmullRom(0.0f, -0.5f, 1.0f, -0.5f, |
| 140 | 1.0f, 0.0f, -2.5f, 1.5f, |
| 141 | 0.0f, 0.5f, 2.0f, -1.5f, |
| 142 | 0.0f, 0.0f, -0.5f, 0.5f); |
| 143 | coeffs = &kCatmullRom; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | if (*coeffs != fCoefficients) { |
| 148 | pdm.setSkM44(fCoefficientUni, *coeffs); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(GrSurfaceProxyView view, |
| 153 | SkAlphaType alphaType, |
| 154 | const SkMatrix& matrix, |
| 155 | Kernel kernel, |
| 156 | Direction direction) { |
| 157 | auto fp = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I()); |
| 158 | auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
| 159 | return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>( |
| 160 | new GrBicubicEffect(std::move(fp), kernel, direction, clamp))); |
| 161 | } |
| 162 | |
| 163 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(GrSurfaceProxyView view, |
| 164 | SkAlphaType alphaType, |
| 165 | const SkMatrix& matrix, |
| 166 | const GrSamplerState::WrapMode wrapX, |
| 167 | const GrSamplerState::WrapMode wrapY, |
| 168 | Kernel kernel, |
| 169 | Direction direction, |
| 170 | const GrCaps& caps) { |
| 171 | GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest); |
| 172 | std::unique_ptr<GrFragmentProcessor> fp; |
| 173 | fp = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I(), sampler, caps); |
| 174 | auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
| 175 | return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>( |
| 176 | new GrBicubicEffect(std::move(fp), kernel, direction, clamp))); |
| 177 | } |
| 178 | |
| 179 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::MakeSubset( |
| 180 | GrSurfaceProxyView view, |
| 181 | SkAlphaType alphaType, |
| 182 | const SkMatrix& matrix, |
| 183 | const GrSamplerState::WrapMode wrapX, |
| 184 | const GrSamplerState::WrapMode wrapY, |
| 185 | const SkRect& subset, |
| 186 | Kernel kernel, |
| 187 | Direction direction, |
| 188 | const GrCaps& caps) { |
| 189 | GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest); |
| 190 | std::unique_ptr<GrFragmentProcessor> fp; |
| 191 | fp = GrTextureEffect::MakeSubset( |
| 192 | std::move(view), alphaType, SkMatrix::I(), sampler, subset, caps); |
| 193 | auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
| 194 | return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>( |
| 195 | new GrBicubicEffect(std::move(fp), kernel, direction, clamp))); |
| 196 | } |
| 197 | |
| 198 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::MakeSubset( |
| 199 | GrSurfaceProxyView view, |
| 200 | SkAlphaType alphaType, |
| 201 | const SkMatrix& matrix, |
| 202 | const GrSamplerState::WrapMode wrapX, |
| 203 | const GrSamplerState::WrapMode wrapY, |
| 204 | const SkRect& subset, |
| 205 | const SkRect& domain, |
| 206 | Kernel kernel, |
| 207 | Direction direction, |
| 208 | const GrCaps& caps) { |
| 209 | auto lowerBound = [](float x) { return std::floor(x - 1.5f) + 0.5f; }; |
| 210 | auto upperBound = [](float x) { return std::floor(x + 1.5f) - 0.5f; }; |
| 211 | SkRect expandedDomain { |
| 212 | lowerBound(domain.fLeft) , |
| 213 | upperBound(domain.fRight) , |
| 214 | lowerBound(domain.fTop) , |
| 215 | upperBound(domain.fBottom) |
| 216 | }; |
| 217 | GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest); |
| 218 | std::unique_ptr<GrFragmentProcessor> fp; |
| 219 | fp = GrTextureEffect::MakeSubset( |
| 220 | std::move(view), alphaType, SkMatrix::I(), sampler, subset, expandedDomain, caps); |
| 221 | auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
| 222 | return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>( |
| 223 | new GrBicubicEffect(std::move(fp), kernel, direction, clamp))); |
| 224 | } |
| 225 | |
| 226 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(std::unique_ptr<GrFragmentProcessor> fp, |
| 227 | SkAlphaType alphaType, |
| 228 | const SkMatrix& matrix, |
| 229 | Kernel kernel, |
| 230 | Direction direction) { |
| 231 | auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
| 232 | return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>( |
| 233 | new GrBicubicEffect(std::move(fp), kernel, direction, clamp))); |
| 234 | } |
| 235 | |
| 236 | GrBicubicEffect::GrBicubicEffect(std::unique_ptr<GrFragmentProcessor> fp, |
| 237 | Kernel kernel, |
| 238 | Direction direction, |
| 239 | Clamp clamp) |
| 240 | : INHERITED(kGrBicubicEffect_ClassID, ProcessorOptimizationFlags(fp.get())) |
| 241 | , fKernel(kernel) |
| 242 | , fDirection(direction) |
| 243 | , fClamp(clamp) { |
| 244 | this->setUsesSampleCoordsDirectly(); |
| 245 | this->registerChild(std::move(fp), SkSL::SampleUsage::Explicit()); |
| 246 | } |
| 247 | |
| 248 | GrBicubicEffect::GrBicubicEffect(const GrBicubicEffect& that) |
| 249 | : INHERITED(kGrBicubicEffect_ClassID, that.optimizationFlags()) |
| 250 | , fKernel(that.fKernel) |
| 251 | , fDirection(that.fDirection) |
| 252 | , fClamp(that.fClamp) { |
| 253 | this->setUsesSampleCoordsDirectly(); |
| 254 | this->cloneAndRegisterAllChildProcessors(that); |
| 255 | } |
| 256 | |
| 257 | void GrBicubicEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 258 | GrProcessorKeyBuilder* b) const { |
| 259 | uint32_t key = (static_cast<uint32_t>(fDirection) << 0) | (static_cast<uint32_t>(fClamp) << 2); |
| 260 | b->add32(key); |
| 261 | } |
| 262 | |
| 263 | GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const { return new Impl(); } |
| 264 | |
| 265 | bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 266 | const auto& that = other.cast<GrBicubicEffect>(); |
| 267 | return fDirection == that.fDirection && fClamp == that.fClamp; |
| 268 | } |
| 269 | |
| 270 | SkPMColor4f GrBicubicEffect::constantOutputForConstantInput(const SkPMColor4f& input) const { |
| 271 | return GrFragmentProcessor::ConstantOutputForConstantInput(this->childProcessor(0), input); |
| 272 | } |
| 273 | |
| 274 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect); |
| 275 | |
| 276 | #if GR_TEST_UTILS |
| 277 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) { |
| 278 | Direction direction = Direction::kX; |
| 279 | switch (d->fRandom->nextULessThan(3)) { |
| 280 | case 0: |
| 281 | direction = Direction::kX; |
| 282 | break; |
| 283 | case 1: |
| 284 | direction = Direction::kY; |
| 285 | break; |
| 286 | case 2: |
| 287 | direction = Direction::kXY; |
| 288 | break; |
| 289 | } |
| 290 | auto kernel = d->fRandom->nextBool() ? GrBicubicEffect::Kernel::kMitchell |
| 291 | : GrBicubicEffect::Kernel::kCatmullRom; |
| 292 | auto m = GrTest::TestMatrix(d->fRandom); |
| 293 | switch (d->fRandom->nextULessThan(3)) { |
| 294 | case 0: { |
| 295 | auto [view, ct, at] = d->randomView(); |
| 296 | GrSamplerState::WrapMode wm[2]; |
| 297 | GrTest::TestWrapModes(d->fRandom, wm); |
| 298 | |
| 299 | if (d->fRandom->nextBool()) { |
| 300 | SkRect subset; |
| 301 | subset.fLeft = d->fRandom->nextSScalar1() * view.width(); |
| 302 | subset.fTop = d->fRandom->nextSScalar1() * view.height(); |
| 303 | subset.fRight = d->fRandom->nextSScalar1() * view.width(); |
| 304 | subset.fBottom = d->fRandom->nextSScalar1() * view.height(); |
| 305 | subset.sort(); |
| 306 | return MakeSubset(std::move(view), |
| 307 | at, |
| 308 | m, |
| 309 | wm[0], |
| 310 | wm[1], |
| 311 | subset, |
| 312 | kernel, |
| 313 | direction, |
| 314 | *d->caps()); |
| 315 | } |
| 316 | return Make(std::move(view), at, m, wm[0], wm[1], kernel, direction, *d->caps()); |
| 317 | } |
| 318 | case 1: { |
| 319 | auto [view, ct, at] = d->randomView(); |
| 320 | return Make(std::move(view), at, m, kernel, direction); |
| 321 | } |
| 322 | default: { |
| 323 | SkAlphaType at; |
| 324 | do { |
| 325 | at = static_cast<SkAlphaType>(d->fRandom->nextULessThan(kLastEnum_SkAlphaType + 1)); |
| 326 | } while (at == kUnknown_SkAlphaType); |
| 327 | return Make(GrProcessorUnitTest::MakeChildFP(d), at, m, kernel, direction); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | #endif |
| 332 | |