xref: /aosp_15_r20/external/skia/src/gpu/BlurUtils.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2023 Google LLC
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 skgpu_BlurUtils_DEFINED
9 #define skgpu_BlurUtils_DEFINED
10 
11 #include "include/core/SkSize.h"
12 #include "include/core/SkSpan.h"
13 #include "src/core/SkBlurEngine.h"
14 
15 #include <array>
16 
17 class SkBitmap;
18 class SkRRect;
19 class SkRuntimeEffect;
20 struct SkV4;
21 
22 namespace skgpu {
23 
24 // TODO: Remove functions that just wrap SkBlurEngine utilities once calling code is updated to
25 // use SkBlurEngine directly or is deleted entirely in favor of SkShaderBlurAlgorithm.
BlurKernelWidth(int radius)26 constexpr int BlurKernelWidth(int radius) { return SkShaderBlurAlgorithm::KernelWidth(radius); }
BlurLinearKernelWidth(int radius)27 constexpr int BlurLinearKernelWidth(int radius) {
28     return SkShaderBlurAlgorithm::LinearKernelWidth(radius);
29 }
BlurIsEffectivelyIdentity(float sigma)30 constexpr bool BlurIsEffectivelyIdentity(float sigma) {
31     return SkBlurEngine::IsEffectivelyIdentity(sigma);
32 }
BlurSigmaRadius(float sigma)33 inline int BlurSigmaRadius(float sigma) { return SkBlurEngine::SigmaToRadius(sigma); }
34 
35 static constexpr int kMaxBlurSamples = SkShaderBlurAlgorithm::kMaxSamples;
36 static constexpr float kMaxLinearBlurSigma = SkShaderBlurAlgorithm::kMaxLinearSigma;
37 
GetBlur2DEffect(const SkISize & radii)38 inline const SkRuntimeEffect* GetBlur2DEffect(const SkISize& radii) {
39     return SkShaderBlurAlgorithm::GetBlur2DEffect(radii);
40 }
41 
GetLinearBlur1DEffect(int radius)42 inline const SkRuntimeEffect* GetLinearBlur1DEffect(int radius) {
43     return SkShaderBlurAlgorithm::GetLinearBlur1DEffect(radius);
44 }
45 
Compute2DBlurKernel(SkSize sigma,SkISize radius,SkSpan<float> kernel)46 inline void Compute2DBlurKernel(SkSize sigma, SkISize radius, SkSpan<float> kernel) {
47     SkShaderBlurAlgorithm::Compute2DBlurKernel(sigma, radius, kernel);
48 }
49 
Compute2DBlurKernel(SkSize sigma,SkISize radius,std::array<SkV4,kMaxBlurSamples/4> & kernel)50 inline void Compute2DBlurKernel(SkSize sigma,
51                                 SkISize radius,
52                                 std::array<SkV4, kMaxBlurSamples/4>& kernel) {
53     SkShaderBlurAlgorithm::Compute2DBlurKernel(sigma, radius, kernel);
54 }
55 
Compute1DBlurKernel(float sigma,int radius,SkSpan<float> kernel)56 inline void Compute1DBlurKernel(float sigma, int radius, SkSpan<float> kernel) {
57     SkShaderBlurAlgorithm::Compute1DBlurKernel(sigma, radius, kernel);
58 }
59 
Compute2DBlurOffsets(SkISize radius,std::array<SkV4,kMaxBlurSamples/2> & offsets)60 inline void Compute2DBlurOffsets(SkISize radius, std::array<SkV4, kMaxBlurSamples/2>& offsets) {
61     SkShaderBlurAlgorithm::Compute2DBlurOffsets(radius, offsets);
62 }
63 
Compute1DBlurLinearKernel(float sigma,int radius,std::array<SkV4,kMaxBlurSamples/2> & offsetsAndKernel)64 inline void Compute1DBlurLinearKernel(float sigma,
65                                       int radius,
66                                       std::array<SkV4, kMaxBlurSamples/2>& offsetsAndKernel) {
67     SkShaderBlurAlgorithm::Compute1DBlurLinearKernel(sigma, radius, offsetsAndKernel);
68 }
69 
70 ///////////////////////////////////////////////////////////////////////////////////////////////////
71 
72 // Calculates the integral table for an analytic rectangle blur. The integral values are stored in
73 // the red channel of the provided bitmap, which will be 1D with a 1-pixel height.
74 SkBitmap CreateIntegralTable(int width);
75 
76 // Returns the width of an integral table we will create for the given 6*sigma.
77 int ComputeIntegralTableWidth(float sixSigma);
78 
79 // Creates a profile of a blurred circle.
80 SkBitmap CreateCircleProfile(float sigma, float radius, int profileWidth);
81 
82 // Creates a half plane approximation profile of a blurred circle.
83 SkBitmap CreateHalfPlaneProfile(int profileWidth);
84 
85 // Creates a blurred rounded rectangle mask. 'rrectToDraw' is the original rrect centered within
86 // bounds defined by 'dimensions', which encompass the entire blurred rrect.
87 SkBitmap CreateRRectBlurMask(const SkRRect& rrectToDraw, const SkISize& dimensions, float sigma);
88 
89 } // namespace skgpu
90 
91 #endif // skgpu_BlurUtils_DEFINED
92