robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "GrCircleBlurFragmentProcessor.h" |
| 9 | |
| 10 | #if SK_SUPPORT_GPU |
| 11 | |
| 12 | #include "GrContext.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 13 | #include "GrInvariantOutput.h" |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 14 | #include "GrTextureProvider.h" |
| 15 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 16 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 17 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 18 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 19 | #include "glsl/GrGLSLUniformHandler.h" |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 20 | |
benjaminwagner | 6c71e0a | 2016-04-07 08:49:31 -0700 | [diff] [blame] | 21 | #include "SkFixed.h" |
| 22 | |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 23 | class GrCircleBlurFragmentProcessor::GLSLProcessor : public GrGLSLFragmentProcessor { |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 24 | public: |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 25 | void emitCode(EmitArgs&) override; |
| 26 | |
| 27 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 28 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 29 | |
| 30 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 31 | GrGLSLProgramDataManager::UniformHandle fDataUniform; |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 32 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 33 | typedef GrGLSLFragmentProcessor INHERITED; |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 36 | void GrCircleBlurFragmentProcessor::GLSLProcessor::emitCode(EmitArgs& args) { |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 37 | const char *dataName; |
| 38 | |
| 39 | // The data is formatted as: |
| 40 | // x,y - the center of the circle |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 41 | // z - inner radius that should map to 0th entry in the texture. |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 42 | // w - the inverse of the distance over which the texture is stretched. |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 43 | fDataUniform = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 44 | kVec4f_GrSLType, |
| 45 | kDefault_GrSLPrecision, |
| 46 | "data", |
| 47 | &dataName); |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 48 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 49 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 50 | const char *fragmentPos = fragBuilder->fragmentPosition(); |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 51 | |
| 52 | if (args.fInputColor) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 53 | fragBuilder->codeAppendf("vec4 src=%s;", args.fInputColor); |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 54 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 55 | fragBuilder->codeAppendf("vec4 src=vec4(1);"); |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 56 | } |
| 57 | |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 58 | // We just want to compute "(length(vec) - %s.z + 0.5) * %s.w" but need to rearrange |
| 59 | // for precision. |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 60 | fragBuilder->codeAppendf("vec2 vec = vec2( (%s.x - %s.x) * %s.w , (%s.y - %s.y) * %s.w );", |
robertphillips | 4e56772 | 2015-12-10 13:29:14 -0800 | [diff] [blame] | 61 | fragmentPos, dataName, dataName, |
| 62 | fragmentPos, dataName, dataName); |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 63 | fragBuilder->codeAppendf("float dist = length(vec) + (0.5 - %s.z) * %s.w;", |
robertphillips | 4e56772 | 2015-12-10 13:29:14 -0800 | [diff] [blame] | 64 | dataName, dataName); |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 65 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 66 | fragBuilder->codeAppendf("float intensity = "); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 67 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], "vec2(dist, 0.5)"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 68 | fragBuilder->codeAppend(".a;"); |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 69 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 70 | fragBuilder->codeAppendf("%s = src * intensity;\n", args.fOutputColor ); |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 71 | } |
| 72 | |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 73 | void GrCircleBlurFragmentProcessor::GLSLProcessor::onSetData(const GrGLSLProgramDataManager& pdman, |
| 74 | const GrProcessor& proc) { |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 75 | const GrCircleBlurFragmentProcessor& cbfp = proc.cast<GrCircleBlurFragmentProcessor>(); |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 76 | const SkRect& circle = cbfp.fCircle; |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 77 | |
| 78 | // The data is formatted as: |
| 79 | // x,y - the center of the circle |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 80 | // z - inner radius that should map to 0th entry in the texture. |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 81 | // w - the inverse of the distance over which the profile texture is stretched. |
| 82 | pdman.set4f(fDataUniform, circle.centerX(), circle.centerY(), cbfp.fSolidRadius, |
| 83 | 1.f / cbfp.fTextureRadius); |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /////////////////////////////////////////////////////////////////////////////// |
| 87 | |
| 88 | GrCircleBlurFragmentProcessor::GrCircleBlurFragmentProcessor(const SkRect& circle, |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 89 | float textureRadius, |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 90 | float solidRadius, |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 91 | GrTexture* blurProfile) |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 92 | : fCircle(circle) |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 93 | , fSolidRadius(solidRadius) |
| 94 | , fTextureRadius(textureRadius) |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 95 | , fBlurProfileAccess(blurProfile, GrTextureParams::kBilerp_FilterMode) { |
| 96 | this->initClassID<GrCircleBlurFragmentProcessor>(); |
| 97 | this->addTextureAccess(&fBlurProfileAccess); |
| 98 | this->setWillReadFragmentPosition(); |
| 99 | } |
| 100 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 101 | GrGLSLFragmentProcessor* GrCircleBlurFragmentProcessor::onCreateGLSLInstance() const { |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 102 | return new GLSLProcessor; |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 103 | } |
| 104 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 105 | void GrCircleBlurFragmentProcessor::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 106 | GrProcessorKeyBuilder* b) const { |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 107 | // The code for this processor is always the same so there is nothing to add to the key. |
| 108 | return; |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void GrCircleBlurFragmentProcessor::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
| 112 | inout->mulByUnknownSingleComponent(); |
| 113 | } |
| 114 | |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 115 | // Computes an unnormalized half kernel (right side). Returns the summation of all the half kernel |
| 116 | // values. |
| 117 | static float make_unnormalized_half_kernel(float* halfKernel, int halfKernelSize, float sigma) { |
bsalomon | b525721 | 2016-05-19 15:52:34 -0700 | [diff] [blame] | 118 | const float invSigma = 1.f / sigma; |
| 119 | const float b = -0.5f * invSigma * invSigma; |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 120 | float tot = 0.0f; |
bsalomon | b525721 | 2016-05-19 15:52:34 -0700 | [diff] [blame] | 121 | // Compute half kernel values at half pixel steps out from the center. |
| 122 | float t = 0.5f; |
| 123 | for (int i = 0; i < halfKernelSize; ++i) { |
| 124 | float value = expf(t * t * b); |
| 125 | tot += value; |
| 126 | halfKernel[i] = value; |
| 127 | t += 1.f; |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 128 | } |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 129 | return tot; |
| 130 | } |
| 131 | |
| 132 | // Create a Gaussian half-kernel (right side) and a summed area table given a sigma and number of |
| 133 | // discrete steps. The half kernel is normalized to sum to 0.5. |
| 134 | static void make_half_kernel_and_summed_table(float* halfKernel, float* summedHalfKernel, |
| 135 | int halfKernelSize, float sigma) { |
bsalomon | b525721 | 2016-05-19 15:52:34 -0700 | [diff] [blame] | 136 | // The half kernel should sum to 0.5 not 1.0. |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 137 | const float tot = 2.f * make_unnormalized_half_kernel(halfKernel, halfKernelSize, sigma); |
| 138 | float sum = 0.f; |
bsalomon | b525721 | 2016-05-19 15:52:34 -0700 | [diff] [blame] | 139 | for (int i = 0; i < halfKernelSize; ++i) { |
| 140 | halfKernel[i] /= tot; |
| 141 | sum += halfKernel[i]; |
| 142 | summedHalfKernel[i] = sum; |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
bsalomon | 82ad93c | 2016-05-20 09:11:17 -0700 | [diff] [blame] | 146 | // Applies the 1D half kernel vertically at points along the x axis to a circle centered at the |
| 147 | // origin with radius circleR. |
| 148 | void apply_kernel_in_y(float* results, int numSteps, float firstX, float circleR, |
| 149 | int halfKernelSize, const float* summedHalfKernelTable) { |
| 150 | float x = firstX; |
| 151 | for (int i = 0; i < numSteps; ++i, x += 1.f) { |
| 152 | if (x < -circleR || x > circleR) { |
| 153 | results[i] = 0; |
| 154 | continue; |
| 155 | } |
| 156 | float y = sqrtf(circleR * circleR - x * x); |
| 157 | // In the column at x we exit the circle at +y and -y |
| 158 | // The summed table entry j is actually reflects an offset of j + 0.5. |
| 159 | y -= 0.5f; |
| 160 | int yInt = SkScalarFloorToInt(y); |
| 161 | SkASSERT(yInt >= -1); |
| 162 | if (y < 0) { |
| 163 | results[i] = (y + 0.5f) * summedHalfKernelTable[0]; |
| 164 | } else if (yInt >= halfKernelSize - 1) { |
| 165 | results[i] = 0.5f; |
| 166 | } else { |
| 167 | float yFrac = y - yInt; |
| 168 | results[i] = (1.f - yFrac) * summedHalfKernelTable[yInt] + |
| 169 | yFrac * summedHalfKernelTable[yInt + 1]; |
| 170 | } |
bsalomon | b525721 | 2016-05-19 15:52:34 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 173 | |
bsalomon | 82ad93c | 2016-05-20 09:11:17 -0700 | [diff] [blame] | 174 | // Apply a Gaussian at point (evalX, 0) to a circle centered at the origin with radius circleR. |
| 175 | // This relies on having a half kernel computed for the Gaussian and a table of applications of |
| 176 | // the half kernel in y to columns at (evalX - halfKernel, evalX - halfKernel + 1, ..., evalX + |
| 177 | // halfKernel) passed in as yKernelEvaluations. |
| 178 | static uint8_t eval_at(float evalX, float circleR, const float* halfKernel, int halfKernelSize, |
| 179 | const float* yKernelEvaluations) { |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 180 | float acc = 0; |
| 181 | |
bsalomon | 82ad93c | 2016-05-20 09:11:17 -0700 | [diff] [blame] | 182 | float x = evalX - halfKernelSize; |
| 183 | for (int i = 0; i < halfKernelSize; ++i, x += 1.f) { |
bsalomon | b525721 | 2016-05-19 15:52:34 -0700 | [diff] [blame] | 184 | if (x < -circleR || x > circleR) { |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 185 | continue; |
| 186 | } |
bsalomon | 82ad93c | 2016-05-20 09:11:17 -0700 | [diff] [blame] | 187 | float verticalEval = yKernelEvaluations[i]; |
| 188 | acc += verticalEval * halfKernel[halfKernelSize - i - 1]; |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 189 | } |
bsalomon | 82ad93c | 2016-05-20 09:11:17 -0700 | [diff] [blame] | 190 | for (int i = 0; i < halfKernelSize; ++i, x += 1.f) { |
bsalomon | b525721 | 2016-05-19 15:52:34 -0700 | [diff] [blame] | 191 | if (x < -circleR || x > circleR) { |
| 192 | continue; |
| 193 | } |
bsalomon | 82ad93c | 2016-05-20 09:11:17 -0700 | [diff] [blame] | 194 | float verticalEval = yKernelEvaluations[i + halfKernelSize]; |
bsalomon | b525721 | 2016-05-19 15:52:34 -0700 | [diff] [blame] | 195 | acc += verticalEval * halfKernel[i]; |
| 196 | } |
| 197 | // Since we applied a half kernel in y we multiply acc by 2 (the circle is symmetric about the |
| 198 | // x axis). |
| 199 | return SkUnitScalarClampToByte(2.f * acc); |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 200 | } |
| 201 | |
bsalomon | b525721 | 2016-05-19 15:52:34 -0700 | [diff] [blame] | 202 | // This function creates a profile of a blurred circle. It does this by computing a kernel for |
bsalomon | 82ad93c | 2016-05-20 09:11:17 -0700 | [diff] [blame] | 203 | // half the Gaussian and a matching summed area table. The summed area table is used to compute |
| 204 | // an array of vertical applications of the half kernel to the circle along the x axis. The table |
| 205 | // of y evaluations has 2 * k + n entries where k is the size of the half kernel and n is the size |
| 206 | // of the profile being computed. Then for each of the n profile entries we walk out k steps in each |
| 207 | // horizontal direction multiplying the corresponding y evaluation by the half kernel entry and |
| 208 | // sum these values to compute the profile entry. |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 209 | static uint8_t* create_circle_profile(float sigma, float circleR, int profileTextureWidth) { |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 210 | const int numSteps = profileTextureWidth; |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 211 | uint8_t* weights = new uint8_t[numSteps]; |
bsalomon | b525721 | 2016-05-19 15:52:34 -0700 | [diff] [blame] | 212 | |
| 213 | // The full kernel is 6 sigmas wide. |
| 214 | int halfKernelSize = SkScalarCeilToInt(6.0f*sigma); |
| 215 | // round up to next multiple of 2 and then divide by 2 |
| 216 | halfKernelSize = ((halfKernelSize + 1) & ~1) >> 1; |
bsalomon | 82ad93c | 2016-05-20 09:11:17 -0700 | [diff] [blame] | 217 | |
| 218 | // Number of x steps at which to apply kernel in y to cover all the profile samples in x. |
| 219 | int numYSteps = numSteps + 2 * halfKernelSize; |
| 220 | |
| 221 | SkAutoTArray<float> bulkAlloc(halfKernelSize + halfKernelSize + numYSteps); |
| 222 | float* halfKernel = bulkAlloc.get(); |
| 223 | float* summedKernel = bulkAlloc.get() + halfKernelSize; |
| 224 | float* yEvals = bulkAlloc.get() + 2 * halfKernelSize; |
| 225 | make_half_kernel_and_summed_table(halfKernel, summedKernel, halfKernelSize, sigma); |
| 226 | |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 227 | float firstX = -halfKernelSize + 0.5f; |
bsalomon | 82ad93c | 2016-05-20 09:11:17 -0700 | [diff] [blame] | 228 | apply_kernel_in_y(yEvals, numYSteps, firstX, circleR, halfKernelSize, summedKernel); |
| 229 | |
benjaminwagner | 9d24023 | 2016-02-24 07:51:33 -0800 | [diff] [blame] | 230 | for (int i = 0; i < numSteps - 1; ++i) { |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 231 | float evalX = i + 0.5f; |
bsalomon | 82ad93c | 2016-05-20 09:11:17 -0700 | [diff] [blame] | 232 | weights[i] = eval_at(evalX, circleR, halfKernel, halfKernelSize, yEvals + i); |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 233 | } |
benjaminwagner | 9d24023 | 2016-02-24 07:51:33 -0800 | [diff] [blame] | 234 | // Ensure the tail of the Gaussian goes to zero. |
bsalomon | 3ab53d0 | 2016-05-18 07:15:46 -0700 | [diff] [blame] | 235 | weights[numSteps - 1] = 0; |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 236 | return weights; |
| 237 | } |
| 238 | |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 239 | static uint8_t* create_half_plane_profile(int profileWidth) { |
| 240 | SkASSERT(!(profileWidth & 0x1)); |
| 241 | // The full kernel is 6 sigmas wide. |
| 242 | float sigma = profileWidth / 6.f; |
| 243 | int halfKernelSize = profileWidth / 2; |
| 244 | |
| 245 | SkAutoTArray<float> halfKernel(halfKernelSize); |
| 246 | uint8_t* profile = new uint8_t[profileWidth]; |
| 247 | |
| 248 | // The half kernel should sum to 0.5. |
| 249 | const float tot = 2.f * make_unnormalized_half_kernel(halfKernel.get(), halfKernelSize, sigma); |
| 250 | float sum = 0.f; |
| 251 | // Populate the profile from the right edge to the middle. |
| 252 | for (int i = 0; i < halfKernelSize; ++i) { |
| 253 | halfKernel[halfKernelSize - i - 1] /= tot; |
| 254 | sum += halfKernel[halfKernelSize - i - 1]; |
| 255 | profile[profileWidth - i - 1] = SkUnitScalarClampToByte(sum); |
| 256 | } |
| 257 | // Populate the profile from the middle to the left edge (by flipping the half kernel and |
| 258 | // continuing the summation). |
| 259 | for (int i = 0; i < halfKernelSize; ++i) { |
| 260 | sum += halfKernel[i]; |
| 261 | profile[halfKernelSize - i - 1] = SkUnitScalarClampToByte(sum); |
| 262 | } |
| 263 | // Ensure tail goes to 0. |
| 264 | profile[profileWidth - 1] = 0; |
| 265 | return profile; |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 266 | } |
| 267 | |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 268 | static GrTexture* create_profile_texture(GrTextureProvider* textureProvider, const SkRect& circle, |
| 269 | float sigma, float* solidRadius, float* textureRadius) { |
bsalomon | 3ab53d0 | 2016-05-18 07:15:46 -0700 | [diff] [blame] | 270 | float circleR = circle.width() / 2.0f; |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 271 | // Profile textures are cached by the ratio of sigma to circle radius and by the size of the |
| 272 | // profile texture (binned by powers of 2). |
| 273 | SkScalar sigmaToCircleRRatio = sigma / circleR; |
| 274 | // When sigma is really small this becomes a equivalent to convolving a Gaussian with a half- |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 275 | // plane. Similarly, in the extreme high ratio cases circle becomes a point WRT to the Guassian |
| 276 | // and the profile texture is a just a Gaussian evaluation. However, we haven't yet implemented |
| 277 | // this latter optimization. |
| 278 | sigmaToCircleRRatio = SkTMin(sigmaToCircleRRatio, 8.f); |
| 279 | SkFixed sigmaToCircleRRatioFixed; |
| 280 | static const SkScalar kHalfPlaneThreshold = 0.1f; |
| 281 | bool useHalfPlaneApprox = false; |
| 282 | if (sigmaToCircleRRatio <= kHalfPlaneThreshold) { |
| 283 | useHalfPlaneApprox = true; |
| 284 | sigmaToCircleRRatioFixed = 0; |
| 285 | *solidRadius = circleR - 3 * sigma; |
| 286 | *textureRadius = 6 * sigma; |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 287 | } else { |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 288 | // Convert to fixed point for the key. |
| 289 | sigmaToCircleRRatioFixed = SkScalarToFixed(sigmaToCircleRRatio); |
| 290 | // We shave off some bits to reduce the number of unique entries. We could probably shave |
| 291 | // off more than we do. |
| 292 | sigmaToCircleRRatioFixed &= ~0xff; |
| 293 | sigmaToCircleRRatio = SkFixedToScalar(sigmaToCircleRRatioFixed); |
| 294 | sigma = circleR * sigmaToCircleRRatio; |
| 295 | *solidRadius = 0; |
| 296 | *textureRadius = circleR + 3 * sigma; |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 297 | } |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 298 | |
| 299 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 300 | GrUniqueKey key; |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 301 | GrUniqueKey::Builder builder(&key, kDomain, 1); |
bsalomon | f7fcdb2 | 2016-06-14 14:37:12 -0700 | [diff] [blame] | 302 | builder[0] = sigmaToCircleRRatioFixed; |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 303 | builder.finish(); |
| 304 | |
| 305 | GrTexture *blurProfile = textureProvider->findAndRefTextureByUniqueKey(key); |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 306 | if (!blurProfile) { |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 307 | static constexpr int kProfileTextureWidth = 512; |
bsalomon | 3ab53d0 | 2016-05-18 07:15:46 -0700 | [diff] [blame] | 308 | GrSurfaceDesc texDesc; |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 309 | texDesc.fWidth = kProfileTextureWidth; |
bsalomon | 3ab53d0 | 2016-05-18 07:15:46 -0700 | [diff] [blame] | 310 | texDesc.fHeight = 1; |
| 311 | texDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 312 | |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 313 | SkAutoTDeleteArray<uint8_t> profile(nullptr); |
| 314 | if (useHalfPlaneApprox) { |
| 315 | profile.reset(create_half_plane_profile(kProfileTextureWidth)); |
| 316 | } else { |
| 317 | // Rescale params to the size of the texture we're creating. |
| 318 | SkScalar scale = kProfileTextureWidth / *textureRadius; |
| 319 | profile.reset(create_circle_profile(sigma * scale, circleR * scale, |
| 320 | kProfileTextureWidth)); |
| 321 | } |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 322 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 323 | blurProfile = textureProvider->createTexture(texDesc, SkBudgeted::kYes, profile.get(), 0); |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 324 | if (blurProfile) { |
| 325 | textureProvider->assignUniqueKeyToTexture(key, blurProfile); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | return blurProfile; |
| 330 | } |
| 331 | |
bsalomon | af68fa1 | 2016-08-16 09:24:57 -0700 | [diff] [blame] | 332 | ////////////////////////////////////////////////////////////////////////////// |
| 333 | |
| 334 | sk_sp<GrFragmentProcessor> GrCircleBlurFragmentProcessor::Make(GrTextureProvider*textureProvider, |
| 335 | const SkRect& circle, float sigma) { |
| 336 | float solidRadius; |
| 337 | float textureRadius; |
| 338 | SkAutoTUnref<GrTexture> profile(create_profile_texture(textureProvider, circle, sigma, |
| 339 | &solidRadius, &textureRadius)); |
| 340 | if (!profile) { |
| 341 | return nullptr; |
| 342 | } |
| 343 | return sk_sp<GrFragmentProcessor>(new GrCircleBlurFragmentProcessor(circle, textureRadius, |
| 344 | solidRadius, profile)); |
| 345 | } |
| 346 | |
| 347 | ////////////////////////////////////////////////////////////////////////////// |
| 348 | |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 349 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrCircleBlurFragmentProcessor); |
| 350 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 351 | sk_sp<GrFragmentProcessor> GrCircleBlurFragmentProcessor::TestCreate(GrProcessorTestData* d) { |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 352 | SkScalar wh = d->fRandom->nextRangeScalar(100.f, 1000.f); |
| 353 | SkScalar sigma = d->fRandom->nextRangeF(1.f,10.f); |
| 354 | SkRect circle = SkRect::MakeWH(wh, wh); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 355 | return GrCircleBlurFragmentProcessor::Make(d->fContext->textureProvider(), circle, sigma); |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | #endif |