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#ifndef GrCircleBlurFragmentProcessor_DEFINED
9#define GrCircleBlurFragmentProcessor_DEFINED
10
11#include "SkString.h"
12#include "SkTypes.h"
13
14#if SK_SUPPORT_GPU
15
16#include "GrFragmentProcessor.h"
17#include "GrProcessorUnitTest.h"
18
19class GrTextureProvider;
20
21// This FP handles the special case of a blurred circle. It uses a 1D
22// profile that is just rotated about the origin of the circle.
23class GrCircleBlurFragmentProcessor : public GrFragmentProcessor {
24public:
25    ~GrCircleBlurFragmentProcessor() override {};
26
27    const char* name() const override { return "CircleBlur"; }
28
29    SkString dumpInfo() const override {
30        SkString str;
31        str.appendf("Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Sigma %.2f, Offset: %.2f",
32                    fCircle.fLeft, fCircle.fTop, fCircle.fRight, fCircle.fBottom,
33                    fSigma, fOffset);
34        return str;
35    }
36
37    static const GrFragmentProcessor* Create(GrTextureProvider*textureProvider,
38                                             const SkRect& circle, float sigma) {
39        float offset;
40
41        SkAutoTUnref<GrTexture> blurProfile(CreateCircleBlurProfileTexture(textureProvider,
42                                                                           circle,
43                                                                           sigma,
44                                                                           &offset));
45        if (!blurProfile) {
46           return nullptr;
47        }
48        return new GrCircleBlurFragmentProcessor(circle, sigma, offset, blurProfile);
49    }
50
51    const SkRect& circle() const { return fCircle; }
52    float sigma() const { return fSigma; }
53    float offset() const { return fOffset; }
54    int profileSize() const { return fBlurProfileAccess.getTexture()->width(); }
55
56private:
57    GrCircleBlurFragmentProcessor(const SkRect& circle, float sigma,
58                                  float offset, GrTexture* blurProfile);
59
60    GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
61
62    void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
63
64    bool onIsEqual(const GrFragmentProcessor& other) const override {
65        const GrCircleBlurFragmentProcessor& cbfp = other.cast<GrCircleBlurFragmentProcessor>();
66        // fOffset is computed from the circle width and the sigma
67        return this->circle().width() == cbfp.circle().width() && fSigma == cbfp.fSigma;
68    }
69
70    void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
71
72    static GrTexture* CreateCircleBlurProfileTexture(GrTextureProvider*,
73                                                     const SkRect& circle,
74                                                     float sigma, float* offset);
75
76    SkRect              fCircle;
77    float               fSigma;
78    float               fOffset;
79    GrTextureAccess     fBlurProfileAccess;
80
81    GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
82
83    typedef GrFragmentProcessor INHERITED;
84};
85
86#endif
87#endif
88