blob: d2ab4c94c7c4c4d78db278ca94bd25ff07cb448e [file] [log] [blame]
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +00001/*
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
bsalomoneb1cb5c2015-05-22 08:01:09 -070011#include "GrCaps.h"
joshualitteb2a6762014-12-04 11:35:33 -080012#include "GrFragmentProcessor.h"
joshualittb0a8a372014-09-23 09:50:21 -070013#include "GrProcessor.h"
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000014#include "GrTypesPriv.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000015
egdaniel605dd0f2014-11-12 08:35:25 -080016class GrInvariantOutput;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000017class 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 */
joshualittb0a8a372014-09-23 09:50:21 -070024class GrConvexPolyEffect : public GrFragmentProcessor {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000025public:
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000026 enum {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000027 kMaxEdges = 8,
28 };
29
30 /**
31 * edges is a set of n edge equations where n is limited to kMaxEdges. It contains 3*n values.
32 * The edges should form a convex polygon. The positive half-plane is considered to be the
33 * inside. The equations should be normalized such that the first two coefficients are a unit
34 * 2d vector.
35 *
36 * Currently the edges are specified in device space. In the future we may prefer to specify
37 * them in src space. There are a number of ways this could be accomplished but we'd probably
38 * have to modify the effect/shaderbuilder interface to make it possible (e.g. give access
39 * to the view matrix or untransformed positions in the fragment shader).
40 */
Ethan Nicholas0f3c7322017-11-09 14:51:17 -050041 static std::unique_ptr<GrFragmentProcessor> Make(GrClipEdgeType edgeType, int n,
Brian Salomonaff329b2017-08-11 09:40:37 -040042 const SkScalar edges[]) {
Ethan Nicholas1706f842017-11-10 11:58:19 -050043 if (n <= 0 || n > kMaxEdges || GrClipEdgeType::kHairlineAA == edgeType) {
halcanary96fcdcc2015-08-27 07:41:13 -070044 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000045 }
Brian Salomonaff329b2017-08-11 09:40:37 -040046 return std::unique_ptr<GrFragmentProcessor>(new GrConvexPolyEffect(edgeType, n, edges));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000047 }
48
49 /**
50 * Creates an effect that clips against the path. If the path is not a convex polygon, is
Brian Salomon9a767722017-03-13 17:57:28 -040051 * inverse filled, or has too many edges, this will return nullptr.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000052 */
Ethan Nicholaseace9352018-10-15 20:09:54 +000053 static std::unique_ptr<GrFragmentProcessor> Make(GrClipEdgeType, const SkPath&);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000054
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000055 /**
Ethan Nicholaseace9352018-10-15 20:09:54 +000056 * Creates an effect that fills inside the rect with AA edges..
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000057 */
Ethan Nicholaseace9352018-10-15 20:09:54 +000058 static std::unique_ptr<GrFragmentProcessor> Make(GrClipEdgeType, const SkRect&);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000059
Brian Salomond3b65972017-03-22 12:05:03 -040060 ~GrConvexPolyEffect() override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000061
mtklein36352bf2015-03-25 18:17:31 -070062 const char* name() const override { return "ConvexPoly"; }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000063
Brian Salomonaff329b2017-08-11 09:40:37 -040064 std::unique_ptr<GrFragmentProcessor> clone() const override;
Brian Salomonfcc527b2017-07-26 12:21:21 -040065
Ethan Nicholas0f3c7322017-11-09 14:51:17 -050066 GrClipEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000067
68 int getEdgeCount() const { return fEdgeCount; }
69
70 const SkScalar* getEdges() const { return fEdges; }
71
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000072private:
Ethan Nicholas0f3c7322017-11-09 14:51:17 -050073 GrConvexPolyEffect(GrClipEdgeType edgeType, int n, const SkScalar edges[]);
Brian Salomonfcc527b2017-07-26 12:21:21 -040074 GrConvexPolyEffect(const GrConvexPolyEffect&);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000075
egdaniel57d3b032015-11-13 11:57:27 -080076 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -070077
Brian Salomon94efbf52016-11-29 13:43:05 -050078 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -070079
mtklein36352bf2015-03-25 18:17:31 -070080 bool onIsEqual(const GrFragmentProcessor& other) const override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000081
Ethan Nicholas0f3c7322017-11-09 14:51:17 -050082 GrClipEdgeType fEdgeType;
83 int fEdgeCount;
84 SkScalar fEdges[3 * kMaxEdges];
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000085
Brian Salomon0c26a9d2017-07-06 10:09:38 -040086 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000087
joshualittb0a8a372014-09-23 09:50:21 -070088 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000089};
90
91
92#endif