xref: /aosp_15_r20/external/skia/src/gpu/ganesh/effects/GrConvexPolyEffect.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2014 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 GrConvexPolyEffect_DEFINED
9 #define GrConvexPolyEffect_DEFINED
10 
11 #include "include/core/SkScalar.h"
12 #include "src/gpu/ganesh/GrFragmentProcessor.h"
13 #include "src/gpu/ganesh/GrProcessorUnitTest.h"
14 
15 #include <array>
16 #include <memory>
17 #include <utility>
18 
19 class SkPath;
20 enum class GrClipEdgeType;
21 struct GrShaderCaps;
22 
23 namespace skgpu { class KeyBuilder; }
24 
25 /**
26  * An effect that renders a convex polygon. It is intended to be used as a coverage effect.
27  * Bounding geometry is rendered and the effect computes coverage based on the fragment's
28  * position relative to the polygon.
29  */
30 class GrConvexPolyEffect : public GrFragmentProcessor {
31 public:
32     inline static constexpr int kMaxEdges = 8;
33 
34     /**
35      * edges is a set of n edge equations where n is limited to kMaxEdges. It contains 3*n values.
36      * The edges should form a convex polygon. The positive half-plane is considered to be the
37      * inside. The equations should be normalized such that the first two coefficients are a unit
38      * 2d vector.
39      *
40      * Currently the edges are specified in device space. In the future we may prefer to specify
41      * them in src space. There are a number of ways this could be accomplished but we'd probably
42      * have to modify the effect/shaderbuilder interface to make it possible (e.g. give access
43      * to the view matrix or untransformed positions in the fragment shader).
44      */
Make(std::unique_ptr<GrFragmentProcessor> inputFP,GrClipEdgeType edgeType,int n,const float edges[])45     static GrFPResult Make(std::unique_ptr<GrFragmentProcessor> inputFP,
46                            GrClipEdgeType edgeType,
47                            int n,
48                            const float edges[]) {
49         if (n <= 0 || n > kMaxEdges) {
50             return GrFPFailure(std::move(inputFP));
51         }
52 
53         return GrFPSuccess(std::unique_ptr<GrFragmentProcessor>(
54                     new GrConvexPolyEffect(std::move(inputFP), edgeType, n, edges)));
55     }
56 
57     /**
58      * Creates an effect that clips against the path. If the path is not a convex polygon, is
59      * inverse filled, or has too many edges, creation will fail.
60      */
61     static GrFPResult Make(std::unique_ptr<GrFragmentProcessor>, GrClipEdgeType, const SkPath&);
62 
63     ~GrConvexPolyEffect() override;
64 
name()65     const char* name() const override { return "ConvexPoly"; }
66     std::unique_ptr<GrFragmentProcessor> clone() const override;
67 
68 private:
69     GrConvexPolyEffect(std::unique_ptr<GrFragmentProcessor> inputFP,
70                        GrClipEdgeType edgeType,
71                        int n, const SkScalar edges[]);
72     GrConvexPolyEffect(const GrConvexPolyEffect&);
73 
74     std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override;
75 
76     void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override;
77 
78     bool onIsEqual(const GrFragmentProcessor& other) const override;
79 
80     GrClipEdgeType                 fEdgeType;
81     int                            fEdgeCount;
82     std::array<float, 3*kMaxEdges> fEdges;
83 
84     GR_DECLARE_FRAGMENT_PROCESSOR_TEST
85 
86     using INHERITED = GrFragmentProcessor;
87 };
88 
89 
90 #endif
91