blob: 25dfafb032caa0e347a390e13a1435ee1c58704d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000010#ifndef GrPathRenderer_DEFINED
11#define GrPathRenderer_DEFINED
12
13#include "GrDrawTarget.h"
bsalomon@google.com30085192011-08-19 15:42:31 +000014#include "GrTArray.h"
15#include "GrPathRendererChain.h"
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000016
reed@google.com07f3ee12011-05-16 17:21:57 +000017class SkPath;
bsalomon@google.com30085192011-08-19 15:42:31 +000018
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000019struct GrPoint;
20
21/**
22 * Base class for drawing paths into a GrDrawTarget.
bsalomon@google.comee435122011-07-01 14:57:55 +000023 * Paths may be drawn multiple times as when tiling for supersampling. The
24 * calls on GrPathRenderer to draw a path will look like this:
25 *
26 * pr->setPath(target, path, fill, translate); // sets the path to draw
27 * pr->drawPath(...); // draw the path
28 * pr->drawPath(...);
29 * ...
30 * pr->clearPath(); // finished with the path
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000031 */
bsalomon@google.com67dc5482011-04-04 18:45:32 +000032class GR_API GrPathRenderer : public GrRefCnt {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000033public:
bsalomon@google.com30085192011-08-19 15:42:31 +000034
35 /**
36 * This is called to install custom path renderers in every GrContext at
37 * create time. The default implementation in GrCreatePathRenderer_none.cpp
38 * does not add any additional renderers. Link against another
39 * implementation to install your own. The most recently added is the
40 * most preferred path renderer.
41 *
42 * @param context the context that will use the path renderer
43 * @param flags flags indicating how path renderers will be used
44 * @param prChain the chain to add path renderers to.
45 */
46 static void AddPathRenderers(GrContext* context,
47 GrPathRendererChain::UsageFlags flags,
48 GrPathRendererChain* prChain);
49
50
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000051 GrPathRenderer(void);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000052 /**
53 * Returns true if this path renderer is able to render the path.
54 * Returning false allows the caller to fallback to another path renderer.
55 *
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000056 * @param path The path to draw
57 * @param fill The fill rule to use
58 *
59 * @return true if the path can be drawn by this object, false otherwise.
60 */
bsalomon@google.comee435122011-07-01 14:57:55 +000061 virtual bool canDrawPath(const SkPath& path, GrPathFill fill) const = 0;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000062
63 /**
64 * For complex clips Gr uses the stencil buffer. The path renderer must be
65 * able to render paths into the stencil buffer. However, the path renderer
bsalomon@google.comee435122011-07-01 14:57:55 +000066 * itself may require the stencil buffer to resolve the path fill rule.
67 * This function queries whether the path render needs its own stencil
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000068 * pass. If this returns false then drawPath() should not modify the
bsalomon@google.comee435122011-07-01 14:57:55 +000069 * the target's stencil settings but use those already set on target. The
70 * target is passed as a param in case the answer depends upon draw state.
71 * The view matrix and render target set on the draw target may change
72 * before setPath/drawPath is called and so shouldn't be considered.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000073 *
74 * @param target target that the path will be rendered to
75 * @param path the path that will be drawn
76 * @param fill the fill rule that will be used, will never be an inverse
77 * rule.
78 *
79 * @return false if this path renderer can generate interior-only fragments
80 * without changing the stencil settings on the target. If it
81 * returns true the drawPathToStencil will be used when rendering
82 * clips.
83 */
84 virtual bool requiresStencilPass(const GrDrawTarget* target,
reed@google.com07f3ee12011-05-16 17:21:57 +000085 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000086 GrPathFill fill) const { return false; }
87
88 /**
bsalomon@google.comee435122011-07-01 14:57:55 +000089 * @return true if the path renderer can perform anti-aliasing (aside from
90 * having FSAA enabled for a render target). Target is provided to
91 * communicate the draw state (blend mode, stage settings, etc).
92 */
bsalomon@google.com30085192011-08-19 15:42:31 +000093 virtual bool supportsAA(const GrDrawTarget* target,
bsalomon@google.comee435122011-07-01 14:57:55 +000094 const SkPath& path,
95 GrPathFill fill) { return false; }
96
97 /**
98 * Sets the path to render and target to render into. All calls to drawPath
99 * and drawPathToStencil must occur between setPath and clearPath. The
100 * path cannot be modified externally between setPath and clearPath. The
101 * path may be drawn several times (e.g. tiled supersampler). The target's
102 * state may change between setPath and drawPath* calls. However, if the
103 * path renderer specified vertices/indices during setPath or drawPath*
104 * they will still be set at subsequent drawPath* calls until the next
105 * clearPath. The target's draw state may change between drawPath* calls
106 * so if the subclass does any caching of tesselation, etc. then it must
107 * validate that target parameters that guided the decisions still hold.
108 *
109 * @param target the target to draw into.
110 * @param path the path to draw.
111 * @param fill the fill rule to apply.
112 * @param translate optional additional translation to apply to
113 * the path. NULL means (0,0).
114 */
115 void setPath(GrDrawTarget* target,
116 const SkPath* path,
117 GrPathFill fill,
118 const GrPoint* translate);
119
120 /**
121 * Notifies path renderer that path set in setPath is no longer in use.
122 */
123 void clearPath();
124
125 /**
126 * Draws the path into the draw target. If requiresStencilBuffer returned
127 * false then the target may be setup for stencil rendering (since the
128 * path renderer didn't claim that it needs to use the stencil internally).
129 *
130 * Only called between setPath / clearPath.
131 *
132 * @param stages bitfield that indicates which stages are
133 * in use. All enabled stages expect positions
134 * as texture coordinates. The path renderer
135 * use the remaining stages for its path
136 * filling algorithm.
137 */
138 virtual void drawPath(GrDrawTarget::StageBitfield stages) = 0;
139
140 /**
141 * Draws the path to the stencil buffer. Assume the writable stencil bits
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000142 * are already initialized to zero. Fill will always be either
143 * kWinding_PathFill or kEvenOdd_PathFill.
144 *
145 * Only called if requiresStencilPass returns true for the same combo of
146 * target, path, and fill. Never called with an inverse fill.
147 *
148 * The default implementation assumes the path filling algorithm doesn't
149 * require a separate stencil pass and so crashes.
150 *
bsalomon@google.comee435122011-07-01 14:57:55 +0000151 * Only called between setPath / clearPath.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000152 */
bsalomon@google.comee435122011-07-01 14:57:55 +0000153 virtual void drawPathToStencil() {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000154 GrCrash("Unexpected call to drawPathToStencil.");
155 }
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000156
157 /**
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000158 * Multiply curve tolerance by the given value, increasing or decreasing
159 * the maximum error permitted in tesselating curves with short straight
160 * line segments.
161 */
162 void scaleCurveTolerance(GrScalar multiplier) {
163 GrAssert(multiplier > 0);
164 fCurveTolerance = SkScalarMul(fCurveTolerance, multiplier);
165 }
166
bsalomon@google.comee435122011-07-01 14:57:55 +0000167 /**
168 * Helper that sets a path and automatically remove it in destructor.
169 */
170 class AutoClearPath {
171 public:
172 AutoClearPath() {
173 fPathRenderer = NULL;
174 }
175 AutoClearPath(GrPathRenderer* pr,
176 GrDrawTarget* target,
177 const SkPath* path,
178 GrPathFill fill,
179 const GrPoint* translate) {
180 GrAssert(NULL != pr);
181 pr->setPath(target, path, fill, translate);
182 fPathRenderer = pr;
183 }
184 void set(GrPathRenderer* pr,
185 GrDrawTarget* target,
186 const SkPath* path,
187 GrPathFill fill,
188 const GrPoint* translate) {
189 if (NULL != fPathRenderer) {
190 fPathRenderer->clearPath();
191 }
192 GrAssert(NULL != pr);
193 pr->setPath(target, path, fill, translate);
194 fPathRenderer = pr;
195 }
196 ~AutoClearPath() {
197 if (NULL != fPathRenderer) {
198 fPathRenderer->clearPath();
199 }
200 }
201 private:
202 GrPathRenderer* fPathRenderer;
203 };
204
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000205protected:
bsalomon@google.comee435122011-07-01 14:57:55 +0000206
207 // subclass can override these to be notified just after a path is set
208 // and just before the path is cleared.
209 virtual void pathWasSet() {}
210 virtual void pathWillClear() {}
211
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000212 GrScalar fCurveTolerance;
bsalomon@google.comee435122011-07-01 14:57:55 +0000213 const SkPath* fPath;
214 GrDrawTarget* fTarget;
215 GrPathFill fFill;
216 GrPoint fTranslate;
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000217
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000218private:
219
220 typedef GrRefCnt INHERITED;
221};
222
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000223#endif
bsalomon@google.comee435122011-07-01 14:57:55 +0000224