1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3cb93a386Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cb93a386Sopenharmony_ci * you may not use this file except in compliance with the License.
5cb93a386Sopenharmony_ci * You may obtain a copy of the License at
6cb93a386Sopenharmony_ci *
7cb93a386Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cb93a386Sopenharmony_ci *
9cb93a386Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cb93a386Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cb93a386Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb93a386Sopenharmony_ci * See the License for the specific language governing permissions and
13cb93a386Sopenharmony_ci * limitations under the License.
14cb93a386Sopenharmony_ci */
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_ci#include "src/gpu/effects/GrSDFBlurEffect.h"
17cb93a386Sopenharmony_ci#include "src/gpu/effects/GrMatrixEffect.h"
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_cistd::unique_ptr<GrFragmentProcessor> GrSDFBlurEffect::Make(GrRecordingContext* context,
20cb93a386Sopenharmony_ci    float noxFormedSigma, const SkRRect& srcRRect)
21cb93a386Sopenharmony_ci{
22cb93a386Sopenharmony_ci    float blurRadius = noxFormedSigma;
23cb93a386Sopenharmony_ci    SkV2 wh = {srcRRect.width(), srcRRect.height()};
24cb93a386Sopenharmony_ci    SkVector rr = srcRRect.getSimpleRadii();
25cb93a386Sopenharmony_ci    float r = rr.x();
26cb93a386Sopenharmony_ci
27cb93a386Sopenharmony_ci    static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
28cb93a386Sopenharmony_ci        "uniform half blurRadius;"
29cb93a386Sopenharmony_ci        "uniform vec2 wh;"
30cb93a386Sopenharmony_ci        "uniform half r;"
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_ci        "float myRoundBoxSDF(vec2 p, vec2 a, float r) {"
33cb93a386Sopenharmony_ci            "vec2 q = abs(p)-a + r;"
34cb93a386Sopenharmony_ci            "return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r;"
35cb93a386Sopenharmony_ci        "}"
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_ci        "half4 main(float2 pos) {"
38cb93a386Sopenharmony_ci            "vec2 a = vec2(wh.x / 2, wh.y / 2);"
39cb93a386Sopenharmony_ci            "float d = myRoundBoxSDF(pos, a, r);"
40cb93a386Sopenharmony_ci            "float alpha = smoothstep( blurRadius / 2, -blurRadius / 2, d );"
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ci            "return half4(alpha);"
43cb93a386Sopenharmony_ci        "}"
44cb93a386Sopenharmony_ci    );
45cb93a386Sopenharmony_ci
46cb93a386Sopenharmony_ci    std::unique_ptr<GrFragmentProcessor> fp = GrSkSLFP::Make(effect, "RRectSDFBlur", nullptr,
47cb93a386Sopenharmony_ci        GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha, "blurRadius", blurRadius, "wh", wh, "r", r);
48cb93a386Sopenharmony_ci
49cb93a386Sopenharmony_ci    if (!fp) {
50cb93a386Sopenharmony_ci        return nullptr;
51cb93a386Sopenharmony_ci    }
52cb93a386Sopenharmony_ci
53cb93a386Sopenharmony_ci    SkMatrix matrix;
54cb93a386Sopenharmony_ci    matrix.setTranslateX(-noxFormedSigma - srcRRect.rect().fLeft - srcRRect.width() / kHalfFactor);
55cb93a386Sopenharmony_ci    matrix.setTranslateY(-noxFormedSigma - srcRRect.rect().fTop - srcRRect.height() / kHalfFactor);
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ci    fp = GrMatrixEffect::Make(matrix, std::move(fp));
58cb93a386Sopenharmony_ci
59cb93a386Sopenharmony_ci    return fp;
60cb93a386Sopenharmony_ci}