tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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/GrGaussianConvolutionFragmentProcessor.h" |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 9 | |
Michael Ludwig | e267464 | 2020-10-21 13:01:34 -0400 | [diff] [blame] | 10 | #include "src/core/SkGpuBlurUtils.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 11 | #include "src/gpu/GrTexture.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrTextureProxy.h" |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [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/GrGLSLFragmentProcessor.h" |
| 15 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 16 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
| 17 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
Ethan Nicholas | 4885057 | 2021-02-25 14:45:38 -0500 | [diff] [blame] | 18 | #include "src/sksl/dsl/priv/DSLFPs.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 19 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 20 | // For brevity |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 21 | using UniformHandle = GrGLSLProgramDataManager::UniformHandle; |
| 22 | using Direction = GrGaussianConvolutionFragmentProcessor::Direction; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 23 | |
Brian Salomon | 08cb4bf | 2020-05-07 15:34:15 -0400 | [diff] [blame] | 24 | class GrGaussianConvolutionFragmentProcessor::Impl : public GrGLSLFragmentProcessor { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 25 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 26 | void emitCode(EmitArgs&) override; |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 27 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 28 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 29 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 30 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 31 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 32 | |
ericrk | 0f38612 | 2015-07-21 13:15:47 -0700 | [diff] [blame] | 33 | private: |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 34 | UniformHandle fKernelUni; |
Albert Chaulk | 52b9e81 | 2021-04-12 17:23:25 -0400 | [diff] [blame] | 35 | UniformHandle fOffsetsUni; |
Brian Salomon | 3036def | 2021-04-21 19:30:57 -0400 | [diff] [blame^] | 36 | UniformHandle fKernelWidthUni; |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 37 | UniformHandle fIncrementUni; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 38 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 39 | using INHERITED = GrGLSLFragmentProcessor; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
Brian Salomon | 3036def | 2021-04-21 19:30:57 -0400 | [diff] [blame^] | 42 | enum class LoopType { |
| 43 | kUnrolled, |
| 44 | kFixedLength, |
| 45 | kVariableLength, |
| 46 | }; |
| 47 | |
| 48 | static LoopType loop_type(const GrShaderCaps& caps) { |
| 49 | // This checks that bitwise integer operations and array indexing by non-consts are allowed. |
| 50 | if (caps.generation() < k130_GrGLSLGeneration) { |
| 51 | return LoopType::kUnrolled; |
| 52 | } |
| 53 | // If we're in reduced shader mode and we can have a loop then use a uniform to limit the |
| 54 | // number of iterations so we don't need a code variation for each width. |
| 55 | return caps.reducedShaderMode() ? LoopType::kVariableLength : LoopType::kFixedLength; |
| 56 | } |
| 57 | |
Brian Salomon | 08cb4bf | 2020-05-07 15:34:15 -0400 | [diff] [blame] | 58 | void GrGaussianConvolutionFragmentProcessor::Impl::emitCode(EmitArgs& args) { |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 59 | const GrGaussianConvolutionFragmentProcessor& ce = |
| 60 | args.fFp.cast<GrGaussianConvolutionFragmentProcessor>(); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 61 | |
Ethan Nicholas | 4885057 | 2021-02-25 14:45:38 -0500 | [diff] [blame] | 62 | using namespace SkSL::dsl; |
| 63 | StartFragmentProcessor(this, &args); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 64 | Var increment(kUniform_Modifier, kHalf2_Type, "Increment"); |
Ethan Nicholas | 4885057 | 2021-02-25 14:45:38 -0500 | [diff] [blame] | 65 | fIncrementUni = VarUniformHandle(increment); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 66 | |
Albert Chaulk | 52b9e81 | 2021-04-12 17:23:25 -0400 | [diff] [blame] | 67 | int width = SkGpuBlurUtils::LinearKernelWidth(ce.fRadius); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 68 | |
Brian Salomon | 3036def | 2021-04-21 19:30:57 -0400 | [diff] [blame^] | 69 | LoopType loopType = loop_type(*args.fShaderCaps); |
| 70 | |
| 71 | int arrayCount; |
| 72 | if (loopType == LoopType::kVariableLength) { |
| 73 | // Size the kernel uniform for the maximum width. |
| 74 | arrayCount = (SkGpuBlurUtils::LinearKernelWidth(kMaxKernelRadius) + 3) / 4; |
| 75 | } else { |
| 76 | arrayCount = (width + 3) / 4; |
| 77 | SkASSERT(4 * arrayCount >= width); |
| 78 | } |
jvanverth | 78d6eb0 | 2016-03-02 13:21:16 -0800 | [diff] [blame] | 79 | |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 80 | Var kernel(kUniform_Modifier, Array(kHalf4_Type, arrayCount), "Kernel"); |
Ethan Nicholas | 4885057 | 2021-02-25 14:45:38 -0500 | [diff] [blame] | 81 | fKernelUni = VarUniformHandle(kernel); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 82 | |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 83 | |
Albert Chaulk | 52b9e81 | 2021-04-12 17:23:25 -0400 | [diff] [blame] | 84 | Var offsets(kUniform_Modifier, Array(kHalf4_Type, arrayCount), "Offsets"); |
| 85 | fOffsetsUni = VarUniformHandle(offsets); |
| 86 | |
Brian Salomon | 3036def | 2021-04-21 19:30:57 -0400 | [diff] [blame^] | 87 | Var color(kHalf4_Type, "color", Half4(0)); |
| 88 | Declare(color); |
| 89 | |
Albert Chaulk | 52b9e81 | 2021-04-12 17:23:25 -0400 | [diff] [blame] | 90 | Var coord(kFloat2_Type, "coord", sk_SampleCoord()); |
| 91 | Declare(coord); |
| 92 | |
Brian Salomon | 3036def | 2021-04-21 19:30:57 -0400 | [diff] [blame^] | 93 | switch (loopType) { |
| 94 | case LoopType::kUnrolled: |
| 95 | for (int i = 0; i < width; i++) { |
| 96 | color += SampleChild(/*index=*/0, coord + offsets[i / 4][i & 3] * increment) * |
| 97 | kernel[i / 4][i & 0x3]; |
| 98 | } |
| 99 | break; |
| 100 | case LoopType::kFixedLength: { |
| 101 | Var i(kInt_Type, "i", 0); |
| 102 | For(Declare(i), i < width, i++, |
| 103 | color += SampleChild(/*index=*/0, coord + offsets[i / 4][i & 3] * increment) * |
| 104 | kernel[i / 4][i & 0x3]); |
| 105 | break; |
| 106 | } |
| 107 | case LoopType::kVariableLength: { |
| 108 | Var kernelWidth(kUniform_Modifier, kInt_Type, "kernelWidth"); |
| 109 | fKernelWidthUni = VarUniformHandle(kernelWidth); |
| 110 | Var i(kInt_Type, "i", 0); |
| 111 | For(Declare(i), i < kernelWidth, i++, |
| 112 | color += SampleChild(/*index=*/0, coord + offsets[i / 4][i & 3] * increment) * |
| 113 | kernel[i / 4][i & 0x3]); |
| 114 | break; |
Brian Salomon | 80a1f94 | 2021-04-21 10:56:41 -0400 | [diff] [blame] | 115 | } |
Albert Chaulk | 52b9e81 | 2021-04-12 17:23:25 -0400 | [diff] [blame] | 116 | } |
Brian Salomon | 5d627f3 | 2021-04-21 09:26:24 -0400 | [diff] [blame] | 117 | |
Ethan Nicholas | 4885057 | 2021-02-25 14:45:38 -0500 | [diff] [blame] | 118 | Return(color); |
| 119 | EndFragmentProcessor(); |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Brian Salomon | 08cb4bf | 2020-05-07 15:34:15 -0400 | [diff] [blame] | 122 | void GrGaussianConvolutionFragmentProcessor::Impl::onSetData(const GrGLSLProgramDataManager& pdman, |
| 123 | const GrFragmentProcessor& processor) { |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 124 | const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>(); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 125 | |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 126 | float increment[2] = {}; |
Brian Salomon | 08cb4bf | 2020-05-07 15:34:15 -0400 | [diff] [blame] | 127 | increment[static_cast<int>(conv.fDirection)] = 1; |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 128 | pdman.set2fv(fIncrementUni, 1, increment); |
Robert Phillips | eaded9d | 2018-05-01 15:17:50 -0400 | [diff] [blame] | 129 | |
Albert Chaulk | 52b9e81 | 2021-04-12 17:23:25 -0400 | [diff] [blame] | 130 | int width = SkGpuBlurUtils::LinearKernelWidth(conv.fRadius); |
Brian Salomon | 08cb4bf | 2020-05-07 15:34:15 -0400 | [diff] [blame] | 131 | int arrayCount = (width + 3)/4; |
| 132 | SkDEBUGCODE(size_t arraySize = 4*arrayCount;) |
| 133 | SkASSERT(arraySize >= static_cast<size_t>(width)); |
| 134 | SkASSERT(arraySize <= SK_ARRAY_COUNT(GrGaussianConvolutionFragmentProcessor::fKernel)); |
| 135 | pdman.set4fv(fKernelUni, arrayCount, conv.fKernel); |
Albert Chaulk | 52b9e81 | 2021-04-12 17:23:25 -0400 | [diff] [blame] | 136 | pdman.set4fv(fOffsetsUni, arrayCount, conv.fOffsets); |
Brian Salomon | 3036def | 2021-04-21 19:30:57 -0400 | [diff] [blame^] | 137 | if (fKernelWidthUni.isValid()) { |
| 138 | pdman.set1i(fKernelWidthUni, width); |
| 139 | } |
ericrk | 7a787b4 | 2015-07-21 14:06:16 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Brian Salomon | 08cb4bf | 2020-05-07 15:34:15 -0400 | [diff] [blame] | 142 | void GrGaussianConvolutionFragmentProcessor::Impl::GenKey(const GrProcessor& processor, |
Brian Salomon | 3036def | 2021-04-21 19:30:57 -0400 | [diff] [blame^] | 143 | const GrShaderCaps& shaderCaps, |
Brian Salomon | 08cb4bf | 2020-05-07 15:34:15 -0400 | [diff] [blame] | 144 | GrProcessorKeyBuilder* b) { |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 145 | const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>(); |
Brian Salomon | 3036def | 2021-04-21 19:30:57 -0400 | [diff] [blame^] | 146 | if (loop_type(shaderCaps) != LoopType::kVariableLength) { |
| 147 | b->add32(conv.fRadius); |
| 148 | } |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 149 | } |
| 150 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 151 | /////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 08cb4bf | 2020-05-07 15:34:15 -0400 | [diff] [blame] | 152 | |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 153 | std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::Make( |
Greg Daniel | 5c08249 | 2020-01-29 15:06:49 -0500 | [diff] [blame] | 154 | GrSurfaceProxyView view, |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 155 | SkAlphaType alphaType, |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 156 | Direction dir, |
| 157 | int halfWidth, |
| 158 | float gaussianSigma, |
| 159 | GrSamplerState::WrapMode wm, |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 160 | const SkIRect& subset, |
| 161 | const SkIRect* pixelDomain, |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 162 | const GrCaps& caps) { |
| 163 | std::unique_ptr<GrFragmentProcessor> child; |
Albert Chaulk | 52b9e81 | 2021-04-12 17:23:25 -0400 | [diff] [blame] | 164 | bool is_zero_sigma = SkGpuBlurUtils::IsEffectivelyZeroSigma(gaussianSigma); |
| 165 | // We should sample as nearest if there will be no shader to preserve existing behaviour, but |
| 166 | // the linear blur requires a linear sample. |
| 167 | GrSamplerState::Filter filter = is_zero_sigma ? |
| 168 | GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kLinear; |
Albert Chaulk | 52b9e81 | 2021-04-12 17:23:25 -0400 | [diff] [blame] | 169 | GrSamplerState sampler(wm, filter); |
| 170 | if (is_zero_sigma) { |
Michael Ludwig | e267464 | 2020-10-21 13:01:34 -0400 | [diff] [blame] | 171 | halfWidth = 0; |
| 172 | } |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 173 | if (pixelDomain) { |
| 174 | // Inset because we expect to be invoked at pixel centers. |
| 175 | SkRect domain = SkRect::Make(*pixelDomain).makeInset(0.5, 0.5f); |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 176 | switch (dir) { |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 177 | case Direction::kX: domain.outset(halfWidth, 0); break; |
| 178 | case Direction::kY: domain.outset(0, halfWidth); break; |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 179 | } |
| 180 | child = GrTextureEffect::MakeSubset(std::move(view), alphaType, SkMatrix::I(), sampler, |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 181 | SkRect::Make(subset), domain, caps); |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 182 | } else { |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 183 | child = GrTextureEffect::MakeSubset(std::move(view), alphaType, SkMatrix::I(), sampler, |
| 184 | SkRect::Make(subset), caps); |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 185 | } |
Michael Ludwig | e267464 | 2020-10-21 13:01:34 -0400 | [diff] [blame] | 186 | |
Albert Chaulk | 52b9e81 | 2021-04-12 17:23:25 -0400 | [diff] [blame] | 187 | if (is_zero_sigma) { |
Michael Ludwig | e267464 | 2020-10-21 13:01:34 -0400 | [diff] [blame] | 188 | return child; |
| 189 | } |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 190 | return std::unique_ptr<GrFragmentProcessor>(new GrGaussianConvolutionFragmentProcessor( |
| 191 | std::move(child), dir, halfWidth, gaussianSigma)); |
| 192 | } |
| 193 | |
| 194 | GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor( |
| 195 | std::unique_ptr<GrFragmentProcessor> child, |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 196 | Direction direction, |
| 197 | int radius, |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 198 | float gaussianSigma) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 199 | : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID, |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 200 | ProcessorOptimizationFlags(child.get())) |
Brian Salomon | b133ffe | 2017-07-27 11:53:21 -0400 | [diff] [blame] | 201 | , fRadius(radius) |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 202 | , fDirection(direction) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 203 | this->registerChild(std::move(child), SkSL::SampleUsage::Explicit()); |
Brian Salomon | 6862643 | 2020-04-15 12:56:13 +0000 | [diff] [blame] | 204 | SkASSERT(radius <= kMaxKernelRadius); |
Albert Chaulk | 52b9e81 | 2021-04-12 17:23:25 -0400 | [diff] [blame] | 205 | SkGpuBlurUtils::Compute1DLinearGaussianKernel(fKernel, fOffsets, gaussianSigma, fRadius); |
Michael Ludwig | fbe2859 | 2020-06-26 16:02:15 -0400 | [diff] [blame] | 206 | this->setUsesSampleCoordsDirectly(); |
tomhudson@google.com | fde2c0a | 2012-07-16 12:23:32 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 209 | GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor( |
| 210 | const GrGaussianConvolutionFragmentProcessor& that) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 211 | : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID, that.optimizationFlags()) |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 212 | , fRadius(that.fRadius) |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 213 | , fDirection(that.fDirection) { |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 214 | this->cloneAndRegisterAllChildProcessors(that); |
Albert Chaulk | 52b9e81 | 2021-04-12 17:23:25 -0400 | [diff] [blame] | 215 | memcpy(fKernel, that.fKernel, SkGpuBlurUtils::LinearKernelWidth(fRadius) * sizeof(float)); |
| 216 | memcpy(fOffsets, that.fOffsets, SkGpuBlurUtils::LinearKernelWidth(fRadius) * sizeof(float)); |
Michael Ludwig | fbe2859 | 2020-06-26 16:02:15 -0400 | [diff] [blame] | 217 | this->setUsesSampleCoordsDirectly(); |
Brian Salomon | 3f6f965 | 2017-07-28 07:34:05 -0400 | [diff] [blame] | 218 | } |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 219 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 220 | void GrGaussianConvolutionFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 221 | GrProcessorKeyBuilder* b) const { |
Brian Salomon | 08cb4bf | 2020-05-07 15:34:15 -0400 | [diff] [blame] | 222 | Impl::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Brian Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 225 | std::unique_ptr<GrGLSLFragmentProcessor> |
| 226 | GrGaussianConvolutionFragmentProcessor::onMakeProgramImpl() const { |
| 227 | return std::make_unique<Impl>(); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 230 | bool GrGaussianConvolutionFragmentProcessor::onIsEqual(const GrFragmentProcessor& sBase) const { |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 231 | const auto& that = sBase.cast<GrGaussianConvolutionFragmentProcessor>(); |
Albert Chaulk | 52b9e81 | 2021-04-12 17:23:25 -0400 | [diff] [blame] | 232 | return fRadius == that.fRadius && fDirection == that.fDirection && |
| 233 | std::equal(fKernel, fKernel + SkGpuBlurUtils::LinearKernelWidth(fRadius), that.fKernel) && |
| 234 | std::equal(fOffsets, fOffsets + SkGpuBlurUtils::LinearKernelWidth(fRadius), that.fOffsets); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 235 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 236 | |
| 237 | /////////////////////////////////////////////////////////////////////////////// |
| 238 | |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 239 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrGaussianConvolutionFragmentProcessor); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 240 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 241 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 242 | std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::TestCreate( |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 243 | GrProcessorTestData* d) { |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 244 | auto [view, ct, at] = d->randomView(); |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 245 | |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 246 | Direction dir = d->fRandom->nextBool() ? Direction::kY : Direction::kX; |
| 247 | SkIRect subset{ |
| 248 | static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)), |
| 249 | static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)), |
| 250 | static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)), |
| 251 | static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)), |
| 252 | }; |
| 253 | subset.sort(); |
Brian Salomon | 6862643 | 2020-04-15 12:56:13 +0000 | [diff] [blame] | 254 | |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 255 | auto wm = static_cast<GrSamplerState::WrapMode>( |
| 256 | d->fRandom->nextULessThan(GrSamplerState::kWrapModeCount)); |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 257 | int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius); |
Brian Salomon | aee504b | 2017-01-24 12:29:36 -0500 | [diff] [blame] | 258 | float sigma = radius / 3.f; |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 259 | SkIRect temp; |
| 260 | SkIRect* domain = nullptr; |
| 261 | if (d->fRandom->nextBool()) { |
| 262 | temp = { |
| 263 | static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)), |
| 264 | static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)), |
| 265 | static_cast<int>(d->fRandom->nextRangeU(0, view.width() - 1)), |
| 266 | static_cast<int>(d->fRandom->nextRangeU(0, view.height() - 1)), |
| 267 | }; |
| 268 | temp.sort(); |
| 269 | domain = &temp; |
| 270 | } |
Robert Phillips | 08c5ec7 | 2017-01-30 12:26:47 -0500 | [diff] [blame] | 271 | |
Brian Salomon | 5ed3c2f | 2020-04-13 16:51:39 -0400 | [diff] [blame] | 272 | return GrGaussianConvolutionFragmentProcessor::Make(std::move(view), at, dir, radius, sigma, wm, |
Brian Salomon | 11ad4cc | 2020-05-15 12:07:59 -0400 | [diff] [blame] | 273 | subset, domain, *d->caps()); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 274 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 275 | #endif |