epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * 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.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 10 | #ifndef GrPathRenderer_DEFINED |
| 11 | #define GrPathRenderer_DEFINED |
| 12 | |
| 13 | #include "GrDrawTarget.h" |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 14 | #include "GrTArray.h" |
| 15 | #include "GrPathRendererChain.h" |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 16 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 17 | class SkPath; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 18 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 19 | struct GrPoint; |
| 20 | |
| 21 | /** |
| 22 | * Base class for drawing paths into a GrDrawTarget. |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 23 | * 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.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 31 | */ |
bsalomon@google.com | 67dc548 | 2011-04-04 18:45:32 +0000 | [diff] [blame] | 32 | class GR_API GrPathRenderer : public GrRefCnt { |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 33 | public: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 34 | |
| 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.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame^] | 39 | * implementation to install your own. The first added is the most preferred |
| 40 | * path renderer, second is second most preferred, etc. |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 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.com | d22b6e4 | 2011-06-24 15:53:40 +0000 | [diff] [blame] | 51 | GrPathRenderer(void); |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 52 | /** |
| 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.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 55 | * 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.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 68 | * |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 69 | * @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.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 74 | virtual bool canDrawPath(const GrDrawTarget* target, |
| 75 | const SkPath& path, |
| 76 | GrPathFill fill) const = 0; |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 77 | |
| 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.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 81 | * 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.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 83 | * pass. If this returns false then drawPath() should not modify the |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 84 | * 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.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 88 | * |
| 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.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 100 | const SkPath& path, |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 101 | GrPathFill fill) const { return false; } |
| 102 | |
| 103 | /** |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 104 | * @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.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 108 | virtual bool supportsAA(const GrDrawTarget* target, |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 109 | 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.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 157 | * 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.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 166 | * Only called between setPath / clearPath. |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 167 | */ |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 168 | virtual void drawPathToStencil() { |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 169 | GrCrash("Unexpected call to drawPathToStencil."); |
| 170 | } |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 171 | |
| 172 | /** |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 173 | * 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.com | d22b6e4 | 2011-06-24 15:53:40 +0000 | [diff] [blame] | 210 | protected: |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 211 | |
| 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.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 217 | const SkPath* fPath; |
| 218 | GrDrawTarget* fTarget; |
| 219 | GrPathFill fFill; |
| 220 | GrPoint fTranslate; |
tomhudson@google.com | d22b6e4 | 2011-06-24 15:53:40 +0000 | [diff] [blame] | 221 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 222 | private: |
| 223 | |
| 224 | typedef GrRefCnt INHERITED; |
| 225 | }; |
| 226 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 227 | #endif |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 228 | |