blob: d95cc85afc836ee277ec713029d77efee6f8b553 [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 "GrPathRendererChain.h"
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000015
bsalomon@google.com49313f62011-09-14 13:54:05 +000016#include "SkTArray.h"
17
reed@google.com07f3ee12011-05-16 17:21:57 +000018class SkPath;
bsalomon@google.com30085192011-08-19 15:42:31 +000019
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000020struct GrPoint;
21
22/**
23 * Base class for drawing paths into a GrDrawTarget.
bsalomon@google.comee435122011-07-01 14:57:55 +000024 * Paths may be drawn multiple times as when tiling for supersampling. The
25 * calls on GrPathRenderer to draw a path will look like this:
26 *
27 * pr->setPath(target, path, fill, translate); // sets the path to draw
28 * pr->drawPath(...); // draw the path
29 * pr->drawPath(...);
30 * ...
31 * pr->clearPath(); // finished with the path
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000032 */
bsalomon@google.com67dc5482011-04-04 18:45:32 +000033class GR_API GrPathRenderer : public GrRefCnt {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000034public:
bsalomon@google.com30085192011-08-19 15:42:31 +000035
36 /**
37 * This is called to install custom path renderers in every GrContext at
38 * create time. The default implementation in GrCreatePathRenderer_none.cpp
39 * does not add any additional renderers. Link against another
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000040 * implementation to install your own. The first added is the most preferred
41 * path renderer, second is second most preferred, etc.
bsalomon@google.com30085192011-08-19 15:42:31 +000042 *
43 * @param context the context that will use the path renderer
44 * @param flags flags indicating how path renderers will be used
45 * @param prChain the chain to add path renderers to.
46 */
47 static void AddPathRenderers(GrContext* context,
48 GrPathRendererChain::UsageFlags flags,
49 GrPathRendererChain* prChain);
50
51
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000052 GrPathRenderer(void);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000053 /**
54 * Returns true if this path renderer is able to render the path.
55 * Returning false allows the caller to fallback to another path renderer.
bsalomon@google.comaeb21602011-08-30 18:13:44 +000056 * When searching for a path renderer capable of rendering a path this
57 * function is called. The path renderer can examine the path, fill rule,
58 * and draw settings that will be used (via the targetparameter). If "true"
59 * is reported note that the caller is permitted to make modifications to
60 * the following settings of the target between the calls to canDrawPath and
61 * drawPath:
62 * 1. view matrix: The matrix at drawPath time may have additional scale
63 * scale and translation applied
64 * 2. render target: The render target may change between canDrawPath
65 * and drawPath.
66 * The GrPathRenderer subclass's decision about whether to return true
67 * or false in its implementation of this function should consider these
68 * possible state changes.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000069 *
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000070 * @param path The path to draw
71 * @param fill The fill rule to use
72 *
73 * @return true if the path can be drawn by this object, false otherwise.
74 */
bsalomon@google.comaeb21602011-08-30 18:13:44 +000075 virtual bool canDrawPath(const GrDrawTarget* target,
76 const SkPath& path,
77 GrPathFill fill) const = 0;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000078
79 /**
80 * For complex clips Gr uses the stencil buffer. The path renderer must be
81 * able to render paths into the stencil buffer. However, the path renderer
bsalomon@google.comee435122011-07-01 14:57:55 +000082 * itself may require the stencil buffer to resolve the path fill rule.
83 * This function queries whether the path render needs its own stencil
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000084 * pass. If this returns false then drawPath() should not modify the
bsalomon@google.comee435122011-07-01 14:57:55 +000085 * the target's stencil settings but use those already set on target. The
86 * target is passed as a param in case the answer depends upon draw state.
87 * The view matrix and render target set on the draw target may change
88 * before setPath/drawPath is called and so shouldn't be considered.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000089 *
90 * @param target target that the path will be rendered to
91 * @param path the path that will be drawn
92 * @param fill the fill rule that will be used, will never be an inverse
93 * rule.
94 *
95 * @return false if this path renderer can generate interior-only fragments
96 * without changing the stencil settings on the target. If it
97 * returns true the drawPathToStencil will be used when rendering
98 * clips.
99 */
100 virtual bool requiresStencilPass(const GrDrawTarget* target,
reed@google.com07f3ee12011-05-16 17:21:57 +0000101 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000102 GrPathFill fill) const { return false; }
103
104 /**
bsalomon@google.comee435122011-07-01 14:57:55 +0000105 * @return true if the path renderer can perform anti-aliasing (aside from
106 * having FSAA enabled for a render target). Target is provided to
107 * communicate the draw state (blend mode, stage settings, etc).
108 */
bsalomon@google.com30085192011-08-19 15:42:31 +0000109 virtual bool supportsAA(const GrDrawTarget* target,
bsalomon@google.comee435122011-07-01 14:57:55 +0000110 const SkPath& path,
bsalomon@google.com0d2aa842011-09-26 15:59:20 +0000111 GrPathFill fill) const { return false; }
bsalomon@google.comee435122011-07-01 14:57:55 +0000112
113 /**
114 * Sets the path to render and target to render into. All calls to drawPath
115 * and drawPathToStencil must occur between setPath and clearPath. The
116 * path cannot be modified externally between setPath and clearPath. The
117 * path may be drawn several times (e.g. tiled supersampler). The target's
118 * state may change between setPath and drawPath* calls. However, if the
119 * path renderer specified vertices/indices during setPath or drawPath*
120 * they will still be set at subsequent drawPath* calls until the next
121 * clearPath. The target's draw state may change between drawPath* calls
122 * so if the subclass does any caching of tesselation, etc. then it must
123 * validate that target parameters that guided the decisions still hold.
124 *
125 * @param target the target to draw into.
126 * @param path the path to draw.
127 * @param fill the fill rule to apply.
128 * @param translate optional additional translation to apply to
129 * the path. NULL means (0,0).
130 */
131 void setPath(GrDrawTarget* target,
132 const SkPath* path,
133 GrPathFill fill,
134 const GrPoint* translate);
135
136 /**
137 * Notifies path renderer that path set in setPath is no longer in use.
138 */
139 void clearPath();
140
141 /**
142 * Draws the path into the draw target. If requiresStencilBuffer returned
143 * false then the target may be setup for stencil rendering (since the
144 * path renderer didn't claim that it needs to use the stencil internally).
145 *
146 * Only called between setPath / clearPath.
147 *
148 * @param stages bitfield that indicates which stages are
149 * in use. All enabled stages expect positions
150 * as texture coordinates. The path renderer
151 * use the remaining stages for its path
152 * filling algorithm.
153 */
154 virtual void drawPath(GrDrawTarget::StageBitfield stages) = 0;
155
156 /**
157 * Draws the path to the stencil buffer. Assume the writable stencil bits
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000158 * are already initialized to zero. Fill will always be either
159 * kWinding_PathFill or kEvenOdd_PathFill.
160 *
161 * Only called if requiresStencilPass returns true for the same combo of
162 * target, path, and fill. Never called with an inverse fill.
163 *
164 * The default implementation assumes the path filling algorithm doesn't
165 * require a separate stencil pass and so crashes.
166 *
bsalomon@google.comee435122011-07-01 14:57:55 +0000167 * Only called between setPath / clearPath.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000168 */
bsalomon@google.comee435122011-07-01 14:57:55 +0000169 virtual void drawPathToStencil() {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000170 GrCrash("Unexpected call to drawPathToStencil.");
171 }
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000172
173 /**
bsalomon@google.comee435122011-07-01 14:57:55 +0000174 * Helper that sets a path and automatically remove it in destructor.
175 */
176 class AutoClearPath {
177 public:
178 AutoClearPath() {
179 fPathRenderer = NULL;
180 }
181 AutoClearPath(GrPathRenderer* pr,
182 GrDrawTarget* target,
183 const SkPath* path,
184 GrPathFill fill,
185 const GrPoint* translate) {
186 GrAssert(NULL != pr);
187 pr->setPath(target, path, fill, translate);
188 fPathRenderer = pr;
189 }
190 void set(GrPathRenderer* pr,
191 GrDrawTarget* target,
192 const SkPath* path,
193 GrPathFill fill,
194 const GrPoint* translate) {
195 if (NULL != fPathRenderer) {
196 fPathRenderer->clearPath();
197 }
198 GrAssert(NULL != pr);
199 pr->setPath(target, path, fill, translate);
200 fPathRenderer = pr;
201 }
202 ~AutoClearPath() {
203 if (NULL != fPathRenderer) {
204 fPathRenderer->clearPath();
205 }
206 }
207 private:
208 GrPathRenderer* fPathRenderer;
209 };
210
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000211protected:
bsalomon@google.comee435122011-07-01 14:57:55 +0000212
213 // subclass can override these to be notified just after a path is set
214 // and just before the path is cleared.
215 virtual void pathWasSet() {}
216 virtual void pathWillClear() {}
217
bsalomon@google.comee435122011-07-01 14:57:55 +0000218 const SkPath* fPath;
219 GrDrawTarget* fTarget;
220 GrPathFill fFill;
221 GrPoint fTranslate;
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000222
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000223private:
224
225 typedef GrRefCnt INHERITED;
226};
227
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000228#endif
bsalomon@google.comee435122011-07-01 14:57:55 +0000229