blob: ab9d313c16d8cecdb3e9708eac4e0924a7062a7f [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"
12#include "GrEffect.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000013
14class GrGLConvexPolyEffect;
15class SkPath;
16
17/**
18 * An effect that renders a convex polygon. It is intended to be used as a coverage effect.
19 * Bounding geometry is rendered and the effect computes coverage based on the fragment's
20 * position relative to the polygon.
21 */
22class GrConvexPolyEffect : public GrEffect {
23public:
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000024 enum EdgeType {
25 kFillNoAA_EdgeType,
26 kFillAA_EdgeType,
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +000027 kInverseFillNoAA_EdgeType,
28 kInverseFillAA_EdgeType,
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +000029
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +000030 kLastEdgeType = kInverseFillAA_EdgeType,
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000031 };
32
33 enum {
34 kEdgeTypeCnt = kLastEdgeType + 1,
35 kMaxEdges = 8,
36 };
37
38 /**
39 * edges is a set of n edge equations where n is limited to kMaxEdges. It contains 3*n values.
40 * The edges should form a convex polygon. The positive half-plane is considered to be the
41 * inside. The equations should be normalized such that the first two coefficients are a unit
42 * 2d vector.
43 *
44 * Currently the edges are specified in device space. In the future we may prefer to specify
45 * them in src space. There are a number of ways this could be accomplished but we'd probably
46 * have to modify the effect/shaderbuilder interface to make it possible (e.g. give access
47 * to the view matrix or untransformed positions in the fragment shader).
48 */
49 static GrEffectRef* Create(EdgeType edgeType, int n, const SkScalar edges[]) {
50 if (n <= 0 || n > kMaxEdges) {
51 return NULL;
52 }
53 return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(GrConvexPolyEffect,
54 (edgeType, n, edges))));
55 }
56
57 /**
58 * 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 +000059 * inverse filled, or has too many edges, this will return NULL. If offset is non-NULL, then
60 * the path is translated by the vector.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000061 */
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +000062 static GrEffectRef* Create(EdgeType, const SkPath&, const SkVector* offset= NULL);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000063
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000064 /**
65 * Creates an effect that fills inside the rect with AA edges..
66 */
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +000067 static GrEffectRef* Create(EdgeType, const SkRect&);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000068
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000069 virtual ~GrConvexPolyEffect();
70
71 static const char* Name() { return "ConvexPoly"; }
72
73 EdgeType getEdgeType() const { return fEdgeType; }
74
75 int getEdgeCount() const { return fEdgeCount; }
76
77 const SkScalar* getEdges() const { return fEdges; }
78
79 typedef GrGLConvexPolyEffect GLEffect;
80
81 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
82
83 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
84
85private:
86 GrConvexPolyEffect(EdgeType edgeType, int n, const SkScalar edges[]);
87
88 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE;
89
90 EdgeType fEdgeType;
91 int fEdgeCount;
92 SkScalar fEdges[3 * kMaxEdges];
93
94 GR_DECLARE_EFFECT_TEST;
95
96 typedef GrEffect INHERITED;
97};
98
99
100#endif