blob: 9164b3ecf24ebff8ac3ab759cc81fcbf4f01d61e [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.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 */
23class GrConvexPolyEffect : public GrEffect {
24public:
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 */
bsalomon83d081a2014-07-08 09:56:10 -070040 static GrEffect* Create(GrEffectEdgeType edgeType, int n, const SkScalar edges[]) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000041 if (n <= 0 || n > kMaxEdges || kHairlineAA_GrEffectEdgeType == edgeType) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000042 return NULL;
43 }
bsalomon55fad7a2014-07-08 07:34:20 -070044 return SkNEW_ARGS(GrConvexPolyEffect, (edgeType, n, edges));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000045 }
46
47 /**
48 * 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 +000049 * inverse filled, or has too many edges, this will return NULL. If offset is non-NULL, then
50 * the path is translated by the vector.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000051 */
bsalomon83d081a2014-07-08 09:56:10 -070052 static GrEffect* Create(GrEffectEdgeType, const SkPath&, const SkVector* offset = NULL);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000053
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000054 /**
55 * Creates an effect that fills inside the rect with AA edges..
56 */
bsalomon83d081a2014-07-08 09:56:10 -070057 static GrEffect* Create(GrEffectEdgeType, const SkRect&);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000058
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000059 virtual ~GrConvexPolyEffect();
60
61 static const char* Name() { return "ConvexPoly"; }
62
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000063 GrEffectEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000064
65 int getEdgeCount() const { return fEdgeCount; }
66
67 const SkScalar* getEdges() const { return fEdges; }
68
69 typedef GrGLConvexPolyEffect GLEffect;
70
71 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
72
73 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
74
75private:
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000076 GrConvexPolyEffect(GrEffectEdgeType edgeType, int n, const SkScalar edges[]);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000077
78 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE;
79
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000080 GrEffectEdgeType fEdgeType;
81 int fEdgeCount;
82 SkScalar fEdges[3 * kMaxEdges];
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000083
84 GR_DECLARE_EFFECT_TEST;
85
86 typedef GrEffect INHERITED;
87};
88
89
90#endif