aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/effects/GrRectBlurEffect.h
blob: 7a8bc2c005dac9d560a3fa6fa020f9cb3b371466 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/*
 * This file was autogenerated from GrRectBlurEffect.fp; do not modify.
 */
#ifndef GrRectBlurEffect_DEFINED
#define GrRectBlurEffect_DEFINED
#include "SkTypes.h"
#if SK_SUPPORT_GPU

#include "GrResourceProvider.h"
#include "../effects/SkBlurMask.h"
#include "GrFragmentProcessor.h"
#include "GrCoordTransform.h"
#include "GrColorSpaceXform.h"
class GrRectBlurEffect : public GrFragmentProcessor {
public:
    static sk_sp<GrTextureProxy> CreateBlurProfileTexture(GrResourceProvider* resourceProvider,
                                                          float sigma) {
        unsigned int profileSize = SkScalarCeilToInt(6 * sigma);

        static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
        GrUniqueKey key;
        GrUniqueKey::Builder builder(&key, kDomain, 1);
        builder[0] = profileSize;
        builder.finish();

        sk_sp<GrTextureProxy> blurProfile(
                resourceProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin));
        if (!blurProfile) {
            GrSurfaceDesc texDesc;
            texDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
            texDesc.fWidth = profileSize;
            texDesc.fHeight = 1;
            texDesc.fConfig = kAlpha_8_GrPixelConfig;

            std::unique_ptr<uint8_t[]> profile(SkBlurMask::ComputeBlurProfile(sigma));

            blurProfile = GrSurfaceProxy::MakeDeferred(resourceProvider, texDesc, SkBudgeted::kYes,
                                                       profile.get(), 0);
            if (!blurProfile) {
                return nullptr;
            }

            SkASSERT(blurProfile->origin() == kTopLeft_GrSurfaceOrigin);
            resourceProvider->assignUniqueKeyToProxy(key, blurProfile.get());
        }

        return blurProfile;
    }
    SkRect rect() const { return fRect; }
    float sigma() const { return fSigma; }

    static std::unique_ptr<GrFragmentProcessor> Make(GrResourceProvider* resourceProvider,
                                                     const SkRect& rect, float sigma) {
        int doubleProfileSize = SkScalarCeilToInt(12 * sigma);

        if (doubleProfileSize >= rect.width() || doubleProfileSize >= rect.height()) {
            // if the blur sigma is too large so the gaussian overlaps the whole
            // rect in either direction, fall back to CPU path for now.
            return nullptr;
        }

        sk_sp<GrTextureProxy> blurProfile(CreateBlurProfileTexture(resourceProvider, sigma));
        if (!blurProfile) {
            return nullptr;
        }

        return std::unique_ptr<GrFragmentProcessor>(
                new GrRectBlurEffect(rect, sigma, std::move(blurProfile)));
    }
    GrRectBlurEffect(const GrRectBlurEffect& src);
    std::unique_ptr<GrFragmentProcessor> clone() const override;
    const char* name() const override { return "RectBlurEffect"; }

private:
    GrRectBlurEffect(SkRect rect, float sigma, sk_sp<GrTextureProxy> blurProfile)
            : INHERITED(kGrRectBlurEffect_ClassID,
                        (OptimizationFlags)kCompatibleWithCoverageAsAlpha_OptimizationFlag)
            , fRect(rect)
            , fSigma(sigma)
            , fBlurProfile(std::move(blurProfile)) {
        this->addTextureSampler(&fBlurProfile);
    }
    GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
    void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
    bool onIsEqual(const GrFragmentProcessor&) const override;
    GR_DECLARE_FRAGMENT_PROCESSOR_TEST
    SkRect fRect;
    float fSigma;
    TextureSampler fBlurProfile;
    typedef GrFragmentProcessor INHERITED;
};
#endif
#endif