• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gpu/ganesh/GrTypesPriv.h"
12 #include "src/gpu/ganesh/GrCaps.h"
13 #include "src/gpu/ganesh/GrFragmentProcessor.h"
14 #include "src/gpu/ganesh/GrProcessor.h"
15 #include "src/gpu/ganesh/GrProcessorUnitTest.h"
16 
17 class GrInvariantOutput;
18 class SkPath;
19 
20 /**
21  * An effect that renders a convex polygon. It is intended to be used as a coverage effect.
22  * Bounding geometry is rendered and the effect computes coverage based on the fragment's
23  * position relative to the polygon.
24  */
25 class GrConvexPolyEffect : public GrFragmentProcessor {
26 public:
27     inline static constexpr int kMaxEdges = 8;
28 
29     /**
30      * edges is a set of n edge equations where n is limited to kMaxEdges. It contains 3*n values.
31      * The edges should form a convex polygon. The positive half-plane is considered to be the
32      * inside. The equations should be normalized such that the first two coefficients are a unit
33      * 2d vector.
34      *
35      * Currently the edges are specified in device space. In the future we may prefer to specify
36      * them in src space. There are a number of ways this could be accomplished but we'd probably
37      * have to modify the effect/shaderbuilder interface to make it possible (e.g. give access
38      * to the view matrix or untransformed positions in the fragment shader).
39      */
Make(std::unique_ptr<GrFragmentProcessor> inputFP,GrClipEdgeType edgeType,int n,const float edges[])40     static GrFPResult Make(std::unique_ptr<GrFragmentProcessor> inputFP,
41                            GrClipEdgeType edgeType,
42                            int n,
43                            const float edges[]) {
44         if (n <= 0 || n > kMaxEdges) {
45             return GrFPFailure(std::move(inputFP));
46         }
47 
48         return GrFPSuccess(std::unique_ptr<GrFragmentProcessor>(
49                     new GrConvexPolyEffect(std::move(inputFP), edgeType, n, edges)));
50     }
51 
52     /**
53      * Creates an effect that clips against the path. If the path is not a convex polygon, is
54      * inverse filled, or has too many edges, creation will fail.
55      */
56     static GrFPResult Make(std::unique_ptr<GrFragmentProcessor>, GrClipEdgeType, const SkPath&);
57 
58     ~GrConvexPolyEffect() override;
59 
name()60     const char* name() const override { return "ConvexPoly"; }
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&, skgpu::KeyBuilder*) 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