bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/effects/GrBicubicEffect.h" |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 9 | |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 10 | #include "src/core/SkMatrixPriv.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 11 | #include "src/gpu/GrTexture.h" |
Michael Ludwig | b5bd1b3 | 2020-06-26 10:23:23 -0400 | [diff] [blame] | 12 | #include "src/gpu/effects/GrMatrixEffect.h" |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 13 | #include "src/gpu/effects/GrTextureEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 15 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
| 16 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 17 | |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 18 | class GrBicubicEffect::Impl : public GrGLSLFragmentProcessor { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 19 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 20 | void emitCode(EmitArgs&) override; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 21 | |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 22 | private: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 23 | typedef GrGLSLFragmentProcessor INHERITED; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 24 | }; |
| 25 | |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 26 | void GrBicubicEffect::Impl::emitCode(EmitArgs& args) { |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 27 | const GrBicubicEffect& bicubicEffect = args.fFp.cast<GrBicubicEffect>(); |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 28 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 29 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Brian Osman | a3fa2ef | 2017-01-06 16:13:06 +0000 | [diff] [blame] | 30 | |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 31 | if (bicubicEffect.fKernel == GrBicubicEffect::Kernel::kMitchell) { |
| 32 | /* |
| 33 | * Filter weights come from Don Mitchell & Arun Netravali's 'Reconstruction Filters in\ |
| 34 | * Computer * Graphics', ACM SIGGRAPH Computer Graphics 22, 4 (Aug. 1988). |
| 35 | * ACM DL: http://dl.acm.org/citation.cfm?id=378514 |
| 36 | * Free: |
| 37 | * http://www.cs.utexas.edu/users/fussell/courses/cs384g/lectures/mitchell/Mitchell.pdf |
| 38 | * |
| 39 | * The authors define a family of cubic filters with two free parameters (B and C): |
| 40 | * |
| 41 | * { (12 - 9B - 6C)|x|^3 + (-18 + 12B + 6C)|x|^2 + (6 - 2B) |x| < 1 |
| 42 | * k(x) = 1/6 { (-B - 6C)|x|^3 + (6B + 30C)|x|^2 + (-12B - 48C)|x| + (8B + 24C) 1 <= |x| < 2 |
| 43 | * { 0 otherwise |
| 44 | * |
| 45 | * Various well-known cubic splines can be generated, and the authors select (1/3, 1/3) as |
| 46 | * their favorite overall spline - this is now commonly known as the Mitchell filter, and |
| 47 | * is the source of the specific weights below. |
| 48 | * |
| 49 | * This is SkSL, so the matrix is column-major (transposed from standard matrix notation). |
| 50 | */ |
| 51 | fragBuilder->codeAppend( |
| 52 | "half4x4 kCoefficients = half4x4(" |
| 53 | " 1.0 / 18.0, 16.0 / 18.0, 1.0 / 18.0, 0.0 / 18.0," |
| 54 | "-9.0 / 18.0, 0.0 / 18.0, 9.0 / 18.0, 0.0 / 18.0," |
| 55 | "15.0 / 18.0, -36.0 / 18.0, 27.0 / 18.0, -6.0 / 18.0," |
| 56 | "-7.0 / 18.0, 21.0 / 18.0, -21.0 / 18.0, 7.0 / 18.0);"); |
| 57 | } else { |
| 58 | /* |
| 59 | * Centripetal variant of the Catmull-Rom spline. |
| 60 | * |
| 61 | * Catmull, Edwin; Rom, Raphael (1974). "A class of local interpolating splines". In |
| 62 | * Barnhill, Robert E.; Riesenfeld, Richard F. (eds.). Computer Aided Geometric Design. |
| 63 | * pp. 317–326. |
| 64 | */ |
| 65 | SkASSERT(bicubicEffect.fKernel == GrBicubicEffect::Kernel::kCatmullRom); |
| 66 | fragBuilder->codeAppend( |
| 67 | "half4x4 kCoefficients = 0.5 * half4x4(" |
| 68 | " 0, 2, 0, 0," |
| 69 | "-1, 0, 1, 0," |
| 70 | " 2, -5, 4, -1," |
| 71 | "-1, 3, -3, 1);"); |
| 72 | } |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 73 | // We determine our fractional offset (f) within the texel. We then snap coord to a texel |
| 74 | // center. The snap prevents cases where the starting coords are near a texel boundary and |
| 75 | // offsets with imperfect precision would cause us to skip/double hit a texel. |
| 76 | // The use of "texel" above is somewhat abstract as we're sampling a child processor. It is |
| 77 | // assumed the child processor represents something akin to a nearest neighbor sampled texture. |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 78 | if (bicubicEffect.fDirection == GrBicubicEffect::Direction::kXY) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 79 | fragBuilder->codeAppendf("float2 coord = %s - float2(0.5);", args.fSampleCoord); |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 80 | fragBuilder->codeAppend("half2 f = half2(fract(coord));"); |
| 81 | fragBuilder->codeAppend("coord += 0.5 - f;"); |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 82 | fragBuilder->codeAppend( |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 83 | "half4 wx = kCoefficients * half4(1.0, f.x, f.x * f.x, f.x * f.x * f.x);"); |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 84 | fragBuilder->codeAppend( |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 85 | "half4 wy = kCoefficients * half4(1.0, f.y, f.y * f.y, f.y * f.y * f.y);"); |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 86 | fragBuilder->codeAppend("half4 rowColors[4];"); |
| 87 | for (int y = 0; y < 4; ++y) { |
| 88 | for (int x = 0; x < 4; ++x) { |
| 89 | SkString coord; |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 90 | coord.printf("coord + float2(%d, %d)", x - 1, y - 1); |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 91 | auto childStr = |
| 92 | this->invokeChild(0, args, SkSL::String(coord.c_str(), coord.size())); |
| 93 | fragBuilder->codeAppendf("rowColors[%d] = %s;", x, childStr.c_str()); |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 94 | } |
| 95 | fragBuilder->codeAppendf( |
| 96 | "half4 s%d = wx.x * rowColors[0] + wx.y * rowColors[1] + wx.z * rowColors[2] + " |
| 97 | "wx.w * rowColors[3];", |
| 98 | y); |
| 99 | } |
| 100 | fragBuilder->codeAppend( |
| 101 | "half4 bicubicColor = wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3;"); |
| 102 | } else { |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 103 | const char* d = bicubicEffect.fDirection == Direction::kX ? "x" : "y"; |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 104 | fragBuilder->codeAppendf("float coord = %s.%s - 0.5;", args.fSampleCoord, d); |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 105 | fragBuilder->codeAppend("half f = half(fract(coord));"); |
| 106 | fragBuilder->codeAppend("coord += 0.5 - f;"); |
| 107 | fragBuilder->codeAppend("half f2 = f * f;"); |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 108 | fragBuilder->codeAppend("half4 w = kCoefficients * half4(1.0, f, f2, f2 * f);"); |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 109 | fragBuilder->codeAppend("half4 c[4];"); |
| 110 | for (int i = 0; i < 4; ++i) { |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 111 | SkString coord; |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 112 | if (bicubicEffect.fDirection == Direction::kX) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 113 | coord.printf("float2(coord + %d, %s.y)", i - 1, args.fSampleCoord); |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 114 | } else { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 115 | coord.printf("float2(%s.x, coord + %d)", args.fSampleCoord, i - 1); |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 116 | } |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 117 | auto childStr = this->invokeChild(0, args, SkSL::String(coord.c_str(), coord.size())); |
| 118 | fragBuilder->codeAppendf("c[%d] = %s;", i, childStr.c_str()); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 119 | } |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 120 | fragBuilder->codeAppend( |
| 121 | "half4 bicubicColor = c[0] * w.x + c[1] * w.y + c[2] * w.z + c[3] * w.w;"); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 122 | } |
Brian Osman | e22dba8 | 2019-03-13 10:22:28 -0400 | [diff] [blame] | 123 | // Bicubic can send colors out of range, so clamp to get them back in (source) gamut. |
| 124 | // The kind of clamp we have to do depends on the alpha type. |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 125 | switch (bicubicEffect.fClamp) { |
| 126 | case Clamp::kUnpremul: |
| 127 | fragBuilder->codeAppend("bicubicColor = saturate(bicubicColor);"); |
| 128 | break; |
| 129 | case Clamp::kPremul: |
| 130 | fragBuilder->codeAppend( |
| 131 | "bicubicColor.rgb = max(half3(0.0), min(bicubicColor.rgb, bicubicColor.aaa));"); |
| 132 | break; |
Brian Osman | e22dba8 | 2019-03-13 10:22:28 -0400 | [diff] [blame] | 133 | } |
| 134 | fragBuilder->codeAppendf("%s = bicubicColor * %s;", args.fOutputColor, args.fInputColor); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 137 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(GrSurfaceProxyView view, |
| 138 | SkAlphaType alphaType, |
| 139 | const SkMatrix& matrix, |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 140 | Kernel kernel, |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 141 | Direction direction) { |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 142 | auto fp = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I()); |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 143 | auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
Michael Ludwig | b5bd1b3 | 2020-06-26 10:23:23 -0400 | [diff] [blame] | 144 | return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>( |
| 145 | new GrBicubicEffect(std::move(fp), kernel, direction, clamp))); |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(GrSurfaceProxyView view, |
| 149 | SkAlphaType alphaType, |
| 150 | const SkMatrix& matrix, |
| 151 | const GrSamplerState::WrapMode wrapX, |
| 152 | const GrSamplerState::WrapMode wrapY, |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 153 | Kernel kernel, |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 154 | Direction direction, |
| 155 | const GrCaps& caps) { |
| 156 | GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest); |
| 157 | std::unique_ptr<GrFragmentProcessor> fp; |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 158 | fp = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I(), sampler, caps); |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 159 | auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
Michael Ludwig | b5bd1b3 | 2020-06-26 10:23:23 -0400 | [diff] [blame] | 160 | return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>( |
| 161 | new GrBicubicEffect(std::move(fp), kernel, direction, clamp))); |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::MakeSubset( |
| 165 | GrSurfaceProxyView view, |
| 166 | SkAlphaType alphaType, |
| 167 | const SkMatrix& matrix, |
| 168 | const GrSamplerState::WrapMode wrapX, |
| 169 | const GrSamplerState::WrapMode wrapY, |
| 170 | const SkRect& subset, |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 171 | Kernel kernel, |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 172 | Direction direction, |
| 173 | const GrCaps& caps) { |
| 174 | GrSamplerState sampler(wrapX, wrapY, GrSamplerState::Filter::kNearest); |
| 175 | std::unique_ptr<GrFragmentProcessor> fp; |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 176 | fp = GrTextureEffect::MakeSubset( |
| 177 | std::move(view), alphaType, SkMatrix::I(), sampler, subset, caps); |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 178 | auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
Michael Ludwig | b5bd1b3 | 2020-06-26 10:23:23 -0400 | [diff] [blame] | 179 | return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>( |
| 180 | new GrBicubicEffect(std::move(fp), kernel, direction, clamp))); |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::Make(std::unique_ptr<GrFragmentProcessor> fp, |
| 184 | SkAlphaType alphaType, |
| 185 | const SkMatrix& matrix, |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 186 | Kernel kernel, |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 187 | Direction direction) { |
| 188 | auto clamp = kPremul_SkAlphaType == alphaType ? Clamp::kPremul : Clamp::kUnpremul; |
Michael Ludwig | b5bd1b3 | 2020-06-26 10:23:23 -0400 | [diff] [blame] | 189 | return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>( |
| 190 | new GrBicubicEffect(std::move(fp), kernel, direction, clamp))); |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | GrBicubicEffect::GrBicubicEffect(std::unique_ptr<GrFragmentProcessor> fp, |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 194 | Kernel kernel, |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 195 | Direction direction, |
| 196 | Clamp clamp) |
| 197 | : INHERITED(kGrBicubicEffect_ClassID, ProcessorOptimizationFlags(fp.get())) |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 198 | , fKernel(kernel) |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 199 | , fDirection(direction) |
| 200 | , fClamp(clamp) { |
Michael Ludwig | b5bd1b3 | 2020-06-26 10:23:23 -0400 | [diff] [blame] | 201 | this->setUsesSampleCoordsDirectly(); |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 202 | this->registerChild(std::move(fp), SkSL::SampleUsage::Explicit()); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 205 | GrBicubicEffect::GrBicubicEffect(const GrBicubicEffect& that) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 206 | : INHERITED(kGrBicubicEffect_ClassID, that.optimizationFlags()) |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 207 | , fKernel(that.fKernel) |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 208 | , fDirection(that.fDirection) |
| 209 | , fClamp(that.fClamp) { |
Michael Ludwig | b5bd1b3 | 2020-06-26 10:23:23 -0400 | [diff] [blame] | 210 | this->setUsesSampleCoordsDirectly(); |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 211 | this->cloneAndRegisterAllChildProcessors(that); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 214 | void GrBicubicEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 215 | GrProcessorKeyBuilder* b) const { |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 216 | uint32_t key = (static_cast<uint32_t>(fKernel) << 0) |
| 217 | | (static_cast<uint32_t>(fDirection) << 1) |
| 218 | | (static_cast<uint32_t>(fClamp) << 3); |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 219 | b->add32(key); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 222 | GrGLSLFragmentProcessor* GrBicubicEffect::onCreateGLSLInstance() const { return new Impl(); } |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 223 | |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 224 | bool GrBicubicEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 225 | const auto& that = other.cast<GrBicubicEffect>(); |
| 226 | return fDirection == that.fDirection && fClamp == that.fClamp; |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 229 | SkPMColor4f GrBicubicEffect::constantOutputForConstantInput(const SkPMColor4f& input) const { |
| 230 | return GrFragmentProcessor::ConstantOutputForConstantInput(this->childProcessor(0), input); |
| 231 | } |
| 232 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 233 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrBicubicEffect); |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 234 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 235 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 236 | std::unique_ptr<GrFragmentProcessor> GrBicubicEffect::TestCreate(GrProcessorTestData* d) { |
Brian Salomon | a86fc7a | 2019-05-28 20:42:58 -0400 | [diff] [blame] | 237 | Direction direction = Direction::kX; |
| 238 | switch (d->fRandom->nextULessThan(3)) { |
| 239 | case 0: |
| 240 | direction = Direction::kX; |
| 241 | break; |
| 242 | case 1: |
| 243 | direction = Direction::kY; |
| 244 | break; |
| 245 | case 2: |
| 246 | direction = Direction::kXY; |
| 247 | break; |
| 248 | } |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 249 | auto kernel = d->fRandom->nextBool() ? GrBicubicEffect::Kernel::kMitchell |
| 250 | : GrBicubicEffect::Kernel::kCatmullRom; |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 251 | auto m = GrTest::TestMatrix(d->fRandom); |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 252 | switch (d->fRandom->nextULessThan(3)) { |
| 253 | case 0: { |
| 254 | auto [view, ct, at] = d->randomView(); |
| 255 | GrSamplerState::WrapMode wm[2]; |
| 256 | GrTest::TestWrapModes(d->fRandom, wm); |
Greg Daniel | ba51d68 | 2020-02-05 13:12:13 -0500 | [diff] [blame] | 257 | |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 258 | if (d->fRandom->nextBool()) { |
| 259 | SkRect subset; |
| 260 | subset.fLeft = d->fRandom->nextSScalar1() * view.width(); |
| 261 | subset.fTop = d->fRandom->nextSScalar1() * view.height(); |
| 262 | subset.fRight = d->fRandom->nextSScalar1() * view.width(); |
| 263 | subset.fBottom = d->fRandom->nextSScalar1() * view.height(); |
| 264 | subset.sort(); |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 265 | return MakeSubset(std::move(view), |
| 266 | at, |
| 267 | m, |
| 268 | wm[0], |
| 269 | wm[1], |
| 270 | subset, |
| 271 | kernel, |
| 272 | direction, |
| 273 | *d->caps()); |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 274 | } |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 275 | return Make(std::move(view), at, m, wm[0], wm[1], kernel, direction, *d->caps()); |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 276 | } |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 277 | case 1: { |
| 278 | auto [view, ct, at] = d->randomView(); |
Brian Salomon | 1af72d1 | 2020-06-25 10:47:26 -0400 | [diff] [blame] | 279 | return Make(std::move(view), at, m, kernel, direction); |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 280 | } |
| 281 | default: { |
| 282 | SkAlphaType at; |
| 283 | do { |
| 284 | at = static_cast<SkAlphaType>(d->fRandom->nextULessThan(kLastEnum_SkAlphaType + 1)); |
Brian Osman | 609f159 | 2020-07-01 15:14:39 -0400 | [diff] [blame] | 285 | } while (at == kUnknown_SkAlphaType); |
| 286 | return Make(GrProcessorUnitTest::MakeChildFP(d), at, m, kernel, direction); |
Brian Salomon | 5c4c61e | 2020-03-25 14:26:01 -0400 | [diff] [blame] | 287 | } |
Brian Salomon | d0d033a | 2020-02-18 16:59:28 -0500 | [diff] [blame] | 288 | } |
humper@google.com | 3aad3b0 | 2013-09-04 19:23:53 +0000 | [diff] [blame] | 289 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 290 | #endif |
commit-bot@chromium.org | 9927bd3 | 2014-05-20 17:51:13 +0000 | [diff] [blame] | 291 | |
| 292 | ////////////////////////////////////////////////////////////////////////////// |
| 293 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 294 | bool GrBicubicEffect::ShouldUseBicubic(const SkMatrix& matrix, GrSamplerState::Filter* filterMode) { |
Mike Reed | b2e3c64 | 2019-08-02 11:57:10 -0400 | [diff] [blame] | 295 | switch (SkMatrixPriv::AdjustHighQualityFilterLevel(matrix)) { |
Mike Reed | d2f4be3 | 2019-08-01 13:52:38 -0400 | [diff] [blame] | 296 | case kNone_SkFilterQuality: |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 297 | *filterMode = GrSamplerState::Filter::kNearest; |
Mike Reed | d2f4be3 | 2019-08-01 13:52:38 -0400 | [diff] [blame] | 298 | break; |
| 299 | case kLow_SkFilterQuality: |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 300 | *filterMode = GrSamplerState::Filter::kBilerp; |
Mike Reed | d2f4be3 | 2019-08-01 13:52:38 -0400 | [diff] [blame] | 301 | break; |
| 302 | case kMedium_SkFilterQuality: |
| 303 | *filterMode = GrSamplerState::Filter::kMipMap; |
| 304 | break; |
| 305 | case kHigh_SkFilterQuality: |
| 306 | // When we use the bicubic filtering effect each sample is read from the texture using |
| 307 | // nearest neighbor sampling. |
| 308 | *filterMode = GrSamplerState::Filter::kNearest; |
| 309 | return true; |
commit-bot@chromium.org | 9927bd3 | 2014-05-20 17:51:13 +0000 | [diff] [blame] | 310 | } |
Mike Reed | d2f4be3 | 2019-08-01 13:52:38 -0400 | [diff] [blame] | 311 | return false; |
commit-bot@chromium.org | 9927bd3 | 2014-05-20 17:51:13 +0000 | [diff] [blame] | 312 | } |