xref: /aosp_15_r20/external/skia/src/core/SkBlurMask.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2006 The Android Open Source Project
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 SkBlurMask_DEFINED
9 #define SkBlurMask_DEFINED
10 
11 #include "include/core/SkScalar.h"
12 #include "include/core/SkTypes.h"
13 #include "src/core/SkMask.h"
14 
15 #include <cstdint>
16 
17 class SkRRect;
18 enum SkBlurStyle : int;
19 struct SkIPoint;
20 struct SkRect;
21 
22 class SkBlurMask {
23 public:
24     [[nodiscard]] static bool BlurRect(SkScalar sigma, SkMaskBuilder *dst, const SkRect &src,
25                                        SkBlurStyle, SkIPoint *margin = nullptr,
26                                        SkMaskBuilder::CreateMode createMode =
27                                            SkMaskBuilder::kComputeBoundsAndRenderImage_CreateMode);
28     [[nodiscard]] static bool BlurRRect(SkScalar sigma, SkMaskBuilder *dst, const SkRRect &src,
29                                         SkBlurStyle, SkIPoint *margin = nullptr,
30                                         SkMaskBuilder::CreateMode createMode =
31                                             SkMaskBuilder::kComputeBoundsAndRenderImage_CreateMode);
32 
33     // forceQuality will prevent BoxBlur from falling back to the low quality approach when sigma
34     // is very small -- this can be used predict the margin bump ahead of time without completely
35     // replicating the internal logic.  This permits not only simpler caching of blurred results,
36     // but also being able to predict precisely at what pixels the blurred profile of e.g. a
37     // rectangle will lie.
38     //
39     // Calling details:
40     // * calculate margin - if src.fImage is null, then this call only calculates the border.
41     // * failure          - if src.fImage is not null, failure is signal with dst->fImage being
42     //                      null.
43 
44     [[nodiscard]] static bool BoxBlur(SkMaskBuilder* dst, const SkMask& src,
45                                       SkScalar sigma, SkBlurStyle style,
46                                       SkIPoint* margin = nullptr);
47 
48     // the "ground truth" blur does a gaussian convolution; it's slow
49     // but useful for comparison purposes.
50     [[nodiscard]] static bool BlurGroundTruth(SkScalar sigma, SkMaskBuilder* dst,
51                                               const SkMask& src,
52                                               SkBlurStyle, SkIPoint* margin = nullptr);
53 
54     // If radius > 0, return the corresponding sigma, else return 0
55     static SkScalar SK_SPI ConvertRadiusToSigma(SkScalar radius);
56     // If sigma > 0.5, return the corresponding radius, else return 0
57     static SkScalar SK_SPI ConvertSigmaToRadius(SkScalar sigma);
58 
59     /* Helper functions for analytic rectangle blurs */
60 
61     /** Look up the intensity of the (one dimnensional) blurred half-plane.
62         @param profile The precomputed 1D blur profile; initialized by ComputeBlurProfile below.
63         @param loc the location to look up; The lookup will clamp invalid inputs, but
64                    meaningful data are available between 0 and blurred_width
65         @param blurred_width The width of the final, blurred rectangle
66         @param sharp_width The width of the original, unblurred rectangle.
67     */
68     static uint8_t ProfileLookup(const uint8_t* profile, int loc, int blurredWidth, int sharpWidth);
69 
70     /** Populate the profile of a 1D blurred halfplane.
71         @param profile The 1D table to fill in
72         @param size    Should be 6*sigma bytes
73         @param sigma   The standard deviation of the gaussian blur kernel
74     */
75     static void ComputeBlurProfile(uint8_t* profile, int size, SkScalar sigma);
76 
77     /** Compute an entire scanline of a blurred step function.  This is a 1D helper that
78         will produce both the horizontal and vertical profiles of the blurry rectangle.
79         @param pixels Location to store the resulting pixel data; allocated and managed by caller
80         @param profile Precomputed blur profile computed by ComputeBlurProfile above.
81         @param width Size of the pixels array.
82         @param sigma Standard deviation of the gaussian blur kernel used to compute the profile;
83                      this implicitly gives the size of the pixels array.
84     */
85 
86     static void ComputeBlurredScanline(uint8_t* pixels, const uint8_t* profile,
87                                        unsigned int width, SkScalar sigma);
88 };
89 
90 #endif
91