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