xref: /aosp_15_r20/external/skia/src/gpu/ganesh/GrBlurUtils.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 GrBlurUtils_DEFINED
9 #define GrBlurUtils_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkScalar.h"
13 #include "src/gpu/SkBackingFit.h"
14 
15 #include <memory>
16 
17 class GrClip;
18 class GrPaint;
19 class GrRecordingContext;
20 class GrStyledShape;
21 class GrSurfaceProxyView;
22 class SkColorSpace;
23 class SkMaskFilter;
24 class SkMatrix;
25 class SkPaint;
26 class SkRRect;
27 enum SkAlphaType : int;
28 enum class GrColorType;
29 enum class SkTileMode;
30 namespace skgpu { namespace ganesh { class SurfaceDrawContext; } }
31 struct SkIRect;
32 struct SkISize;
33 
34 /**
35  *  Blur utilities.
36  */
37 namespace GrBlurUtils {
38 
39 static constexpr int kBlurRRectMaxDivisions = 6;
40 
41 /**
42  * This method computes all the parameters for drawing a partially occluded nine-patched
43  * blurred rrect mask:
44  *   rrectToDraw - the integerized rrect to draw in the mask
45  *   widthHeight - how large to make the mask (rrectToDraw will be centered in this coord system).
46  *   rectXs, rectYs - the x & y coordinates of the covering geometry lattice
47  *   texXs, texYs - the texture coordinate at each point in rectXs & rectYs
48  * It returns true if 'devRRect' is nine-patchable
49  */
50 bool ComputeBlurredRRectParams(const SkRRect& srcRRect,
51                                const SkRRect& devRRect,
52                                SkScalar sigma,
53                                SkScalar xformedSigma,
54                                SkRRect* rrectToDraw,
55                                SkISize* widthHeight,
56                                SkScalar rectXs[kBlurRRectMaxDivisions],
57                                SkScalar rectYs[kBlurRRectMaxDivisions],
58                                SkScalar texXs[kBlurRRectMaxDivisions],
59                                SkScalar texYs[kBlurRRectMaxDivisions]);
60 
61 /**
62  * Draw a shape handling the mask filter if present.
63  */
64 void DrawShapeWithMaskFilter(GrRecordingContext*,
65                              skgpu::ganesh::SurfaceDrawContext*,
66                              const GrClip*,
67                              const SkPaint&,
68                              const SkMatrix&,
69                              const GrStyledShape&);
70 
71 /**
72  * Draw a shape handling the mask filter. The mask filter is not optional.
73  * The GrPaint will be modified after return.
74  */
75 void DrawShapeWithMaskFilter(GrRecordingContext*,
76                              skgpu::ganesh::SurfaceDrawContext*,
77                              const GrClip*,
78                              const GrStyledShape&,
79                              GrPaint&&,
80                              const SkMatrix& viewMatrix,
81                              const SkMaskFilter*);
82 
83 /**
84  * Applies a 2D Gaussian blur to a given texture. The blurred result is returned
85  * as a surfaceDrawContext in case the caller wishes to draw into the result.
86  * The GrSurfaceOrigin of the result will watch the GrSurfaceOrigin of srcView. The output
87  * color type, color space, and alpha type will be the same as the src.
88  *
89  * Note: one of sigmaX and sigmaY should be non-zero!
90  * @param context         The GPU context
91  * @param srcView         The source to be blurred.
92  * @param srcColorType    The colorType of srcProxy
93  * @param srcAlphaType    The alphaType of srcProxy
94  * @param srcColorSpace   Color space of the source.
95  * @param dstBounds       The destination bounds, relative to the source texture.
96  * @param srcBounds       The source bounds, relative to the source texture's offset. No pixels
97  *                        will be sampled outside of this rectangle.
98  * @param sigmaX          The blur's standard deviation in X.
99  * @param sigmaY          The blur's standard deviation in Y.
100  * @param tileMode        The mode to handle samples outside bounds.
101  * @param fit             backing fit for the returned render target context
102  * @return                The surfaceDrawContext containing the blurred result.
103  */
104 std::unique_ptr<skgpu::ganesh::SurfaceDrawContext> GaussianBlur(
105         GrRecordingContext*,
106         GrSurfaceProxyView srcView,
107         GrColorType srcColorType,
108         SkAlphaType srcAlphaType,
109         sk_sp<SkColorSpace> srcColorSpace,
110         SkIRect dstBounds,
111         SkIRect srcBounds,
112         float sigmaX,
113         float sigmaY,
114         SkTileMode mode,
115         SkBackingFit fit = SkBackingFit::kApprox);
116 
117 }  // namespace GrBlurUtils
118 
119 #endif
120