blob: ef96c87074237c75612d5992c76ac31d3c0d63c8 [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"
joshualittb0a8a372014-09-23 09:50:21 -070012#include "GrProcessor.h"
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000013#include "GrTypesPriv.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000014
15class GrGLConvexPolyEffect;
16class SkPath;
17
18/**
19 * An effect that renders a convex polygon. It is intended to be used as a coverage effect.
20 * Bounding geometry is rendered and the effect computes coverage based on the fragment's
21 * position relative to the polygon.
22 */
joshualittb0a8a372014-09-23 09:50:21 -070023class GrConvexPolyEffect : public GrFragmentProcessor {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000024public:
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000025 enum {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000026 kMaxEdges = 8,
27 };
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 */
joshualittb0a8a372014-09-23 09:50:21 -070040 static GrFragmentProcessor* Create(GrPrimitiveEdgeType edgeType, int n,
41 const SkScalar edges[]) {
42 if (n <= 0 || n > kMaxEdges || kHairlineAA_GrProcessorEdgeType == edgeType) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000043 return NULL;
44 }
bsalomon55fad7a2014-07-08 07:34:20 -070045 return SkNEW_ARGS(GrConvexPolyEffect, (edgeType, n, edges));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000046 }
47
48 /**
49 * 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 +000050 * inverse filled, or has too many edges, this will return NULL. If offset is non-NULL, then
51 * the path is translated by the vector.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000052 */
joshualittb0a8a372014-09-23 09:50:21 -070053 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkPath&,
54 const SkVector* offset = NULL);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000055
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000056 /**
57 * Creates an effect that fills inside the rect with AA edges..
58 */
joshualittb0a8a372014-09-23 09:50:21 -070059 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkRect&);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000060
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000061 virtual ~GrConvexPolyEffect();
62
63 static const char* Name() { return "ConvexPoly"; }
64
joshualittb0a8a372014-09-23 09:50:21 -070065 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000066
67 int getEdgeCount() const { return fEdgeCount; }
68
69 const SkScalar* getEdges() const { return fEdges; }
70
joshualittb0a8a372014-09-23 09:50:21 -070071 typedef GrGLConvexPolyEffect GLProcessor;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000072
joshualittb0a8a372014-09-23 09:50:21 -070073 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000074
75private:
joshualittb0a8a372014-09-23 09:50:21 -070076 GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, const SkScalar edges[]);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000077
bsalomon0e08fc12014-10-15 08:19:04 -070078 virtual bool onIsEqual(const GrFragmentProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000079
egdaniel1a8ecdf2014-10-03 06:24:12 -070080 virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE;
81
joshualittb0a8a372014-09-23 09:50:21 -070082 GrPrimitiveEdgeType fEdgeType;
83 int fEdgeCount;
84 SkScalar fEdges[3 * kMaxEdges];
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000085
joshualittb0a8a372014-09-23 09:50:21 -070086 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