2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrConvexPolyEffect_DEFINED
9 #define GrConvexPolyEffect_DEFINED
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"
16 class GrInvariantOutput;
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.
24 class GrConvexPolyEffect : public GrFragmentProcessor {
26 inline static constexpr int kMaxEdges = 8;
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
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).
39 static GrFPResult Make(std::unique_ptr<GrFragmentProcessor> inputFP,
40 GrClipEdgeType edgeType,
42 const float edges[]) {
43 if (n <= 0 || n > kMaxEdges) {
44 return GrFPFailure(std::move(inputFP));
47 return GrFPSuccess(std::unique_ptr<GrFragmentProcessor>(
48 new GrConvexPolyEffect(std::move(inputFP), edgeType, n, edges)));
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.
55 static GrFPResult Make(std::unique_ptr<GrFragmentProcessor>, GrClipEdgeType, const SkPath&);
57 ~GrConvexPolyEffect() override;
59 const char* name() const override { return "ConvexPoly"; }
60 std::unique_ptr<GrFragmentProcessor> clone() const override;
63 GrConvexPolyEffect(std::unique_ptr<GrFragmentProcessor> inputFP,
64 GrClipEdgeType edgeType,
65 int n, const SkScalar edges[]);
66 GrConvexPolyEffect(const GrConvexPolyEffect&);
68 std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override;
70 void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override;
72 bool onIsEqual(const GrFragmentProcessor& other) const override;
74 GrClipEdgeType fEdgeType;
76 std::array<float, 3*kMaxEdges> fEdges;
78 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
80 using INHERITED = GrFragmentProcessor;