blob: 89a81242c4f57186cf5a0fe9dbf24d03b49d5120 [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 */
bungeman06ca8ec2016-06-09 08:01:03 -070041 static sk_sp<GrFragmentProcessor> Make(GrPrimitiveEdgeType edgeType, int n,
42 const SkScalar edges[]) {
joshualittb0a8a372014-09-23 09:50:21 -070043 if (n <= 0 || n > kMaxEdges || kHairlineAA_GrProcessorEdgeType == edgeType) {
halcanary96fcdcc2015-08-27 07:41:13 -070044 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000045 }
bungeman06ca8ec2016-06-09 08:01:03 -070046 return sk_sp<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 */
Brian Salomon9a767722017-03-13 17:57:28 -040053 static sk_sp<GrFragmentProcessor> Make(GrPrimitiveEdgeType, const SkPath&);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000054
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000055 /**
56 * Creates an effect that fills inside the rect with AA edges..
57 */
bungeman06ca8ec2016-06-09 08:01:03 -070058 static sk_sp<GrFragmentProcessor> Make(GrPrimitiveEdgeType, const SkRect&);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000059
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000060 virtual ~GrConvexPolyEffect();
61
mtklein36352bf2015-03-25 18:17:31 -070062 const char* name() const override { return "ConvexPoly"; }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000063
joshualittb0a8a372014-09-23 09:50:21 -070064 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000065
66 int getEdgeCount() const { return fEdgeCount; }
67
68 const SkScalar* getEdges() const { return fEdges; }
69
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000070private:
joshualittb0a8a372014-09-23 09:50:21 -070071 GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, const SkScalar edges[]);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000072
egdaniel57d3b032015-11-13 11:57:27 -080073 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -070074
Brian Salomon94efbf52016-11-29 13:43:05 -050075 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -070076
mtklein36352bf2015-03-25 18:17:31 -070077 bool onIsEqual(const GrFragmentProcessor& other) const override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000078
joshualittb0a8a372014-09-23 09:50:21 -070079 GrPrimitiveEdgeType fEdgeType;
80 int fEdgeCount;
81 SkScalar fEdges[3 * kMaxEdges];
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000082
joshualittb0a8a372014-09-23 09:50:21 -070083 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000084
joshualittb0a8a372014-09-23 09:50:21 -070085 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000086};
87
88
89#endif