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