blob: e0eee5d369330e811216a28b5227ebb6743b338d [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
11#include "GrDrawTargetCaps.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 */
joshualittb0a8a372014-09-23 09:50:21 -070041 static GrFragmentProcessor* Create(GrPrimitiveEdgeType edgeType, int n,
42 const SkScalar edges[]) {
43 if (n <= 0 || n > kMaxEdges || kHairlineAA_GrProcessorEdgeType == edgeType) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000044 return NULL;
45 }
bsalomon55fad7a2014-07-08 07:34:20 -070046 return SkNEW_ARGS(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
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000051 * inverse filled, or has too many edges, this will return NULL. If offset is non-NULL, then
52 * the path is translated by the vector.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000053 */
joshualittb0a8a372014-09-23 09:50:21 -070054 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkPath&,
55 const SkVector* offset = NULL);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000056
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000057 /**
58 * Creates an effect that fills inside the rect with AA edges..
59 */
joshualittb0a8a372014-09-23 09:50:21 -070060 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkRect&);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000061
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000062 virtual ~GrConvexPolyEffect();
63
mtklein72c9faa2015-01-09 10:06:39 -080064 const char* name() const SK_OVERRIDE { return "ConvexPoly"; }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000065
joshualittb0a8a372014-09-23 09:50:21 -070066 GrPrimitiveEdgeType 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
mtklein72c9faa2015-01-09 10:06:39 -080072 void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) const SK_OVERRIDE;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000073
mtklein72c9faa2015-01-09 10:06:39 -080074 GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000075
76private:
joshualittb0a8a372014-09-23 09:50:21 -070077 GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, const SkScalar edges[]);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000078
mtklein72c9faa2015-01-09 10:06:39 -080079 bool onIsEqual(const GrFragmentProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000080
mtklein72c9faa2015-01-09 10:06:39 -080081 void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -070082
joshualittb0a8a372014-09-23 09:50:21 -070083 GrPrimitiveEdgeType fEdgeType;
84 int fEdgeCount;
85 SkScalar fEdges[3 * kMaxEdges];
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000086
joshualittb0a8a372014-09-23 09:50:21 -070087 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000088
joshualittb0a8a372014-09-23 09:50:21 -070089 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000090};
91
92
93#endif