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 "GrPathRendererChain.h" |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 15 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 16 | #include "SkTArray.h" |
| 17 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 18 | class SkPath; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 19 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 20 | struct GrPoint; |
| 21 | |
| 22 | /** |
| 23 | * Base class for drawing paths into a GrDrawTarget. |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 24 | * 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 | * |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 27 | * pr->setPath(target, path, fill, aa, translate); // sets the path to draw |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 28 | * pr->drawPath(...); // draw the path |
| 29 | * pr->drawPath(...); |
| 30 | * ... |
| 31 | * pr->clearPath(); // finished with the path |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 32 | */ |
bsalomon@google.com | 67dc548 | 2011-04-04 18:45:32 +0000 | [diff] [blame] | 33 | class GR_API GrPathRenderer : public GrRefCnt { |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 34 | public: |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 35 | |
| 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.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 40 | * implementation to install your own. The first added is the most preferred |
| 41 | * path renderer, second is second most preferred, etc. |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 42 | * |
| 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.com | d22b6e4 | 2011-06-24 15:53:40 +0000 | [diff] [blame] | 52 | GrPathRenderer(void); |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 53 | /** |
| 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.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 56 | * When searching for a path renderer capable of rendering a path this |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 57 | * function is called. |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 58 | * |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 59 | * @param targetCaps The caps of the draw target that will be used to draw |
| 60 | * the path. |
| 61 | * @param path The path to draw |
| 62 | * @param fill The fill rule to use |
| 63 | * @param antiAlias True if anti-aliasing is required. |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 64 | * |
| 65 | * @return true if the path can be drawn by this object, false otherwise. |
| 66 | */ |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 67 | virtual bool canDrawPath(const GrDrawTarget::Caps& targetCaps, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 68 | const SkPath& path, |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 69 | GrPathFill fill, |
| 70 | bool antiAlias) const = 0; |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * For complex clips Gr uses the stencil buffer. The path renderer must be |
| 74 | * 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] | 75 | * itself may require the stencil buffer to resolve the path fill rule. |
| 76 | * This function queries whether the path render needs its own stencil |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 77 | * pass. If this returns false then drawPath() should not modify the |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 78 | * the target's stencil settings but use those already set on target. The |
| 79 | * target is passed as a param in case the answer depends upon draw state. |
| 80 | * The view matrix and render target set on the draw target may change |
| 81 | * before setPath/drawPath is called and so shouldn't be considered. |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 82 | * |
| 83 | * @param target target that the path will be rendered to |
| 84 | * @param path the path that will be drawn |
| 85 | * @param fill the fill rule that will be used, will never be an inverse |
| 86 | * rule. |
| 87 | * |
| 88 | * @return false if this path renderer can generate interior-only fragments |
| 89 | * without changing the stencil settings on the target. If it |
| 90 | * returns true the drawPathToStencil will be used when rendering |
| 91 | * clips. |
| 92 | */ |
| 93 | virtual bool requiresStencilPass(const GrDrawTarget* target, |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 94 | const SkPath& path, |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 95 | GrPathFill fill) const { return false; } |
| 96 | |
| 97 | /** |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 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. |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 112 | * @param antiAlias perform antiAliasing when drawing the path. |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 113 | * @param translate optional additional translation to apply to |
| 114 | * the path. NULL means (0,0). |
| 115 | */ |
| 116 | void setPath(GrDrawTarget* target, |
| 117 | const SkPath* path, |
| 118 | GrPathFill fill, |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 119 | bool antiAlias, |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 120 | const GrPoint* translate); |
| 121 | |
| 122 | /** |
| 123 | * Notifies path renderer that path set in setPath is no longer in use. |
| 124 | */ |
| 125 | void clearPath(); |
| 126 | |
| 127 | /** |
| 128 | * Draws the path into the draw target. If requiresStencilBuffer returned |
| 129 | * false then the target may be setup for stencil rendering (since the |
| 130 | * path renderer didn't claim that it needs to use the stencil internally). |
| 131 | * |
| 132 | * Only called between setPath / clearPath. |
| 133 | * |
| 134 | * @param stages bitfield that indicates which stages are |
| 135 | * in use. All enabled stages expect positions |
| 136 | * as texture coordinates. The path renderer |
| 137 | * use the remaining stages for its path |
| 138 | * filling algorithm. |
| 139 | */ |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 140 | virtual void drawPath(GrDrawState::StageMask stageMask) = 0; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * 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] | 144 | * are already initialized to zero. Fill will always be either |
| 145 | * kWinding_PathFill or kEvenOdd_PathFill. |
| 146 | * |
| 147 | * Only called if requiresStencilPass returns true for the same combo of |
| 148 | * target, path, and fill. Never called with an inverse fill. |
| 149 | * |
| 150 | * The default implementation assumes the path filling algorithm doesn't |
| 151 | * require a separate stencil pass and so crashes. |
| 152 | * |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 153 | * Only called between setPath / clearPath. |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 154 | */ |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 155 | virtual void drawPathToStencil() { |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 156 | GrCrash("Unexpected call to drawPathToStencil."); |
| 157 | } |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 158 | |
| 159 | /** |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 160 | * Helper that sets a path and automatically remove it in destructor. |
| 161 | */ |
| 162 | class AutoClearPath { |
| 163 | public: |
| 164 | AutoClearPath() { |
| 165 | fPathRenderer = NULL; |
| 166 | } |
| 167 | AutoClearPath(GrPathRenderer* pr, |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 168 | GrDrawTarget* target, |
| 169 | const SkPath* path, |
| 170 | GrPathFill fill, |
| 171 | bool antiAlias, |
| 172 | const GrPoint* translate) { |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 173 | GrAssert(NULL != pr); |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 174 | pr->setPath(target, path, fill, antiAlias, translate); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 175 | fPathRenderer = pr; |
| 176 | } |
| 177 | void set(GrPathRenderer* pr, |
| 178 | GrDrawTarget* target, |
| 179 | const SkPath* path, |
| 180 | GrPathFill fill, |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 181 | bool antiAlias, |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 182 | const GrPoint* translate) { |
| 183 | if (NULL != fPathRenderer) { |
| 184 | fPathRenderer->clearPath(); |
| 185 | } |
| 186 | GrAssert(NULL != pr); |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 187 | pr->setPath(target, path, fill, antiAlias, translate); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 188 | fPathRenderer = pr; |
| 189 | } |
| 190 | ~AutoClearPath() { |
| 191 | if (NULL != fPathRenderer) { |
| 192 | fPathRenderer->clearPath(); |
| 193 | } |
| 194 | } |
| 195 | private: |
| 196 | GrPathRenderer* fPathRenderer; |
| 197 | }; |
| 198 | |
tomhudson@google.com | d22b6e4 | 2011-06-24 15:53:40 +0000 | [diff] [blame] | 199 | protected: |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 200 | |
| 201 | // subclass can override these to be notified just after a path is set |
| 202 | // and just before the path is cleared. |
| 203 | virtual void pathWasSet() {} |
| 204 | virtual void pathWillClear() {} |
| 205 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 206 | const SkPath* fPath; |
| 207 | GrDrawTarget* fTarget; |
| 208 | GrPathFill fFill; |
| 209 | GrPoint fTranslate; |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 210 | bool fAntiAlias; |
tomhudson@google.com | d22b6e4 | 2011-06-24 15:53:40 +0000 | [diff] [blame] | 211 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 212 | private: |
| 213 | |
| 214 | typedef GrRefCnt INHERITED; |
| 215 | }; |
| 216 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 217 | #endif |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 218 | |