blob: 815da47de190e31f008a5e1180ccb984faa9c17c [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
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000039 * implementation to install your own. The first added is the most preferred
40 * path renderer, second is second most preferred, etc.
bsalomon@google.com30085192011-08-19 15:42:31 +000041 *
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.
bsalomon@google.comaeb21602011-08-30 18:13:44 +000055 * When searching for a path renderer capable of rendering a path this
56 * function is called. The path renderer can examine the path, fill rule,
57 * and draw settings that will be used (via the targetparameter). If "true"
58 * is reported note that the caller is permitted to make modifications to
59 * the following settings of the target between the calls to canDrawPath and
60 * drawPath:
61 * 1. view matrix: The matrix at drawPath time may have additional scale
62 * scale and translation applied
63 * 2. render target: The render target may change between canDrawPath
64 * and drawPath.
65 * The GrPathRenderer subclass's decision about whether to return true
66 * or false in its implementation of this function should consider these
67 * possible state changes.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000068 *
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000069 * @param path The path to draw
70 * @param fill The fill rule to use
71 *
72 * @return true if the path can be drawn by this object, false otherwise.
73 */
bsalomon@google.comaeb21602011-08-30 18:13:44 +000074 virtual bool canDrawPath(const GrDrawTarget* target,
75 const SkPath& path,
76 GrPathFill fill) const = 0;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000077
78 /**
79 * For complex clips Gr uses the stencil buffer. The path renderer must be
80 * able to render paths into the stencil buffer. However, the path renderer
bsalomon@google.comee435122011-07-01 14:57:55 +000081 * itself may require the stencil buffer to resolve the path fill rule.
82 * This function queries whether the path render needs its own stencil
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000083 * pass. If this returns false then drawPath() should not modify the
bsalomon@google.comee435122011-07-01 14:57:55 +000084 * the target's stencil settings but use those already set on target. The
85 * target is passed as a param in case the answer depends upon draw state.
86 * The view matrix and render target set on the draw target may change
87 * before setPath/drawPath is called and so shouldn't be considered.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000088 *
89 * @param target target that the path will be rendered to
90 * @param path the path that will be drawn
91 * @param fill the fill rule that will be used, will never be an inverse
92 * rule.
93 *
94 * @return false if this path renderer can generate interior-only fragments
95 * without changing the stencil settings on the target. If it
96 * returns true the drawPathToStencil will be used when rendering
97 * clips.
98 */
99 virtual bool requiresStencilPass(const GrDrawTarget* target,
reed@google.com07f3ee12011-05-16 17:21:57 +0000100 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000101 GrPathFill fill) const { return false; }
102
103 /**
bsalomon@google.comee435122011-07-01 14:57:55 +0000104 * @return true if the path renderer can perform anti-aliasing (aside from
105 * having FSAA enabled for a render target). Target is provided to
106 * communicate the draw state (blend mode, stage settings, etc).
107 */
bsalomon@google.com30085192011-08-19 15:42:31 +0000108 virtual bool supportsAA(const GrDrawTarget* target,
bsalomon@google.comee435122011-07-01 14:57:55 +0000109 const SkPath& path,
110 GrPathFill fill) { return false; }
111
112 /**
113 * Sets the path to render and target to render into. All calls to drawPath
114 * and drawPathToStencil must occur between setPath and clearPath. The
115 * path cannot be modified externally between setPath and clearPath. The
116 * path may be drawn several times (e.g. tiled supersampler). The target's
117 * state may change between setPath and drawPath* calls. However, if the
118 * path renderer specified vertices/indices during setPath or drawPath*
119 * they will still be set at subsequent drawPath* calls until the next
120 * clearPath. The target's draw state may change between drawPath* calls
121 * so if the subclass does any caching of tesselation, etc. then it must
122 * validate that target parameters that guided the decisions still hold.
123 *
124 * @param target the target to draw into.
125 * @param path the path to draw.
126 * @param fill the fill rule to apply.
127 * @param translate optional additional translation to apply to
128 * the path. NULL means (0,0).
129 */
130 void setPath(GrDrawTarget* target,
131 const SkPath* path,
132 GrPathFill fill,
133 const GrPoint* translate);
134
135 /**
136 * Notifies path renderer that path set in setPath is no longer in use.
137 */
138 void clearPath();
139
140 /**
141 * Draws the path into the draw target. If requiresStencilBuffer returned
142 * false then the target may be setup for stencil rendering (since the
143 * path renderer didn't claim that it needs to use the stencil internally).
144 *
145 * Only called between setPath / clearPath.
146 *
147 * @param stages bitfield that indicates which stages are
148 * in use. All enabled stages expect positions
149 * as texture coordinates. The path renderer
150 * use the remaining stages for its path
151 * filling algorithm.
152 */
153 virtual void drawPath(GrDrawTarget::StageBitfield stages) = 0;
154
155 /**
156 * Draws the path to the stencil buffer. Assume the writable stencil bits
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000157 * are already initialized to zero. Fill will always be either
158 * kWinding_PathFill or kEvenOdd_PathFill.
159 *
160 * Only called if requiresStencilPass returns true for the same combo of
161 * target, path, and fill. Never called with an inverse fill.
162 *
163 * The default implementation assumes the path filling algorithm doesn't
164 * require a separate stencil pass and so crashes.
165 *
bsalomon@google.comee435122011-07-01 14:57:55 +0000166 * Only called between setPath / clearPath.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000167 */
bsalomon@google.comee435122011-07-01 14:57:55 +0000168 virtual void drawPathToStencil() {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000169 GrCrash("Unexpected call to drawPathToStencil.");
170 }
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000171
172 /**
bsalomon@google.comee435122011-07-01 14:57:55 +0000173 * Helper that sets a path and automatically remove it in destructor.
174 */
175 class AutoClearPath {
176 public:
177 AutoClearPath() {
178 fPathRenderer = NULL;
179 }
180 AutoClearPath(GrPathRenderer* pr,
181 GrDrawTarget* target,
182 const SkPath* path,
183 GrPathFill fill,
184 const GrPoint* translate) {
185 GrAssert(NULL != pr);
186 pr->setPath(target, path, fill, translate);
187 fPathRenderer = pr;
188 }
189 void set(GrPathRenderer* pr,
190 GrDrawTarget* target,
191 const SkPath* path,
192 GrPathFill fill,
193 const GrPoint* translate) {
194 if (NULL != fPathRenderer) {
195 fPathRenderer->clearPath();
196 }
197 GrAssert(NULL != pr);
198 pr->setPath(target, path, fill, translate);
199 fPathRenderer = pr;
200 }
201 ~AutoClearPath() {
202 if (NULL != fPathRenderer) {
203 fPathRenderer->clearPath();
204 }
205 }
206 private:
207 GrPathRenderer* fPathRenderer;
208 };
209
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000210protected:
bsalomon@google.comee435122011-07-01 14:57:55 +0000211
212 // subclass can override these to be notified just after a path is set
213 // and just before the path is cleared.
214 virtual void pathWasSet() {}
215 virtual void pathWillClear() {}
216
bsalomon@google.comee435122011-07-01 14:57:55 +0000217 const SkPath* fPath;
218 GrDrawTarget* fTarget;
219 GrPathFill fFill;
220 GrPoint fTranslate;
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000221
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000222private:
223
224 typedef GrRefCnt INHERITED;
225};
226
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000227#endif
bsalomon@google.comee435122011-07-01 14:57:55 +0000228