blob: d77aad66b27771cb6818d2a643961bee540471f3 [file] [log] [blame]
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001/*
2 Copyright 2011 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17#ifndef GrPathRenderer_DEFINED
18#define GrPathRenderer_DEFINED
19
20#include "GrDrawTarget.h"
21
reed@google.com07f3ee12011-05-16 17:21:57 +000022class SkPath;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000023struct GrPoint;
24
25/**
26 * Base class for drawing paths into a GrDrawTarget.
27 */
bsalomon@google.com67dc5482011-04-04 18:45:32 +000028class GR_API GrPathRenderer : public GrRefCnt {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000029public:
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000030 GrPathRenderer(void);
31
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000032 /**
33 * Returns true if this path renderer is able to render the path.
34 * Returning false allows the caller to fallback to another path renderer.
35 *
36 * @param target The target to draw into
37 * @param path The path to draw
38 * @param fill The fill rule to use
39 *
40 * @return true if the path can be drawn by this object, false otherwise.
41 */
reed@google.com07f3ee12011-05-16 17:21:57 +000042 virtual bool canDrawPath(const GrDrawTarget* target, const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000043 GrPathFill fill) const = 0;
44
45 /**
46 * Draws a path into the draw target. The target will already have its draw
47 * state configured for the draw.
48 * @param target the target to draw into.
49 * @param stages indicates which stages the are already
50 * in use. All enabled stages expect positions
51 * as texture coordinates. The path renderer
52 * use the remaining stages for its path
53 * filling algorithm.
54 * @param path the path to draw.
55 * @param fill the fill rule to apply.
56 * @param translate optional additional translation to apply to
57 * the path. NULL means (0,0).
58 */
59 virtual void drawPath(GrDrawTarget* target,
60 GrDrawTarget::StageBitfield stages,
reed@google.com07f3ee12011-05-16 17:21:57 +000061 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000062 GrPathFill fill,
63 const GrPoint* translate) = 0;
64
65 /**
66 * For complex clips Gr uses the stencil buffer. The path renderer must be
67 * able to render paths into the stencil buffer. However, the path renderer
68 * itself may require the stencil buffer to resolve the path fill rule. This
69 * function queries whether the path render needs its own stencil
70 * pass. If this returns false then drawPath() should not modify the
71 * the target's stencil settings but use those already set on target.
72 *
73 * @param target target that the path will be rendered to
74 * @param path the path that will be drawn
75 * @param fill the fill rule that will be used, will never be an inverse
76 * rule.
77 *
78 * @return false if this path renderer can generate interior-only fragments
79 * without changing the stencil settings on the target. If it
80 * returns true the drawPathToStencil will be used when rendering
81 * clips.
82 */
83 virtual bool requiresStencilPass(const GrDrawTarget* target,
reed@google.com07f3ee12011-05-16 17:21:57 +000084 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000085 GrPathFill fill) const { return false; }
86
87 /**
88 * Draws a path to the stencil buffer. Assume the writable stencil bits
89 * are already initialized to zero. Fill will always be either
90 * kWinding_PathFill or kEvenOdd_PathFill.
91 *
92 * Only called if requiresStencilPass returns true for the same combo of
93 * target, path, and fill. Never called with an inverse fill.
94 *
95 * The default implementation assumes the path filling algorithm doesn't
96 * require a separate stencil pass and so crashes.
97 *
98 *
99 * @param target the target to draw into.
100 * @param path the path to draw.
101 * @param fill the fill rule to apply.
102 * @param translate optional additional translation to apply to
103 * the path. NULL means (0,0).
104 */
105 virtual void drawPathToStencil(GrDrawTarget* target,
reed@google.com07f3ee12011-05-16 17:21:57 +0000106 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000107 GrPathFill fill,
108 const GrPoint* translate) {
109 GrCrash("Unexpected call to drawPathToStencil.");
110 }
111
112 /**
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000113 * @return true if the path renderer can perform anti-aliasing (aside from
114 * having FSAA enabled for a render target)
115 */
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000116 virtual bool supportsAA(GrDrawTarget* target,
reed@google.com07f3ee12011-05-16 17:21:57 +0000117 const SkPath& path,
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000118 GrPathFill fill) { return false; }
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000119
120 /**
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000121 * This is called to install a custom path renderer in every GrContext at
122 * create time. The default implementation in GrCreatePathRenderer_none.cpp
123 * returns NULL. Link against another implementation to install your own.
124 */
125 static GrPathRenderer* CreatePathRenderer();
126
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000127 /**
128 * Multiply curve tolerance by the given value, increasing or decreasing
129 * the maximum error permitted in tesselating curves with short straight
130 * line segments.
131 */
132 void scaleCurveTolerance(GrScalar multiplier) {
133 GrAssert(multiplier > 0);
134 fCurveTolerance = SkScalarMul(fCurveTolerance, multiplier);
135 }
136
137protected:
138 GrScalar fCurveTolerance;
139
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000140private:
141
142 typedef GrRefCnt INHERITED;
143};
144
145/**
146 * Subclass that renders the path using the stencil buffer to resolve fill
147 * rules (e.g. winding, even-odd)
148 */
bsalomon@google.com67dc5482011-04-04 18:45:32 +0000149class GR_API GrDefaultPathRenderer : public GrPathRenderer {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000150public:
151 GrDefaultPathRenderer(bool separateStencilSupport,
152 bool stencilWrapOpsSupport);
153
154 virtual bool canDrawPath(const GrDrawTarget* target,
reed@google.com07f3ee12011-05-16 17:21:57 +0000155 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000156 GrPathFill fill) const { return true; }
157
158 virtual void drawPath(GrDrawTarget* target,
159 GrDrawTarget::StageBitfield stages,
reed@google.com07f3ee12011-05-16 17:21:57 +0000160 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000161 GrPathFill fill,
162 const GrPoint* translate);
163 virtual bool requiresStencilPass(const GrDrawTarget* target,
reed@google.com07f3ee12011-05-16 17:21:57 +0000164 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000165 GrPathFill fill) const;
166 virtual void drawPathToStencil(GrDrawTarget* target,
reed@google.com07f3ee12011-05-16 17:21:57 +0000167 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000168 GrPathFill fill,
169 const GrPoint* translate);
170private:
171
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000172 void onDrawPath(GrDrawTarget* target,
173 GrDrawTarget::StageBitfield stages,
reed@google.com07f3ee12011-05-16 17:21:57 +0000174 const SkPath& path,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000175 GrPathFill fill,
176 const GrPoint* translate,
177 bool stencilOnly);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000178
179 bool fSeparateStencil;
180 bool fStencilWrapOps;
181
182 typedef GrPathRenderer INHERITED;
183};
184
185#endif