skia.committer@gmail.com | 3e50e99 | 2013-05-21 07:01:40 +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 | |
| 9 | #ifndef GrPathRenderer_DEFINED |
| 10 | #define GrPathRenderer_DEFINED |
| 11 | |
| 12 | #include "GrDrawTarget.h" |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 13 | #include "GrPathRendererChain.h" |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 14 | #include "GrStencil.h" |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 15 | |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 16 | #include "SkDrawProcs.h" |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 17 | #include "SkStrokeRec.h" |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 18 | #include "SkTArray.h" |
| 19 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 20 | class SkPath; |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 21 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 22 | struct GrPoint; |
| 23 | |
| 24 | /** |
| 25 | * Base class for drawing paths into a GrDrawTarget. |
robertphillips@google.com | bf5cad4 | 2012-05-10 12:40:40 +0000 | [diff] [blame] | 26 | * |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 27 | * Derived classes can use stages GrPaint::kTotalStages through GrDrawState::kNumStages-1. The |
| 28 | * stages before GrPaint::kTotalStages are reserved for setting up the draw (i.e., textures and |
| 29 | * filter masks). |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 30 | */ |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 31 | class SK_API GrPathRenderer : public SkRefCnt { |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 32 | public: |
reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 33 | SK_DECLARE_INST_COUNT(GrPathRenderer) |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 36 | * This is called to install custom path renderers in every GrContext at create time. The |
| 37 | * default implementation in GrCreatePathRenderer_none.cpp does not add any additional |
| 38 | * renderers. Link against another implementation to install your own. The first added is the |
| 39 | * most preferred path renderer, second is second most preferred, etc. |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 40 | * |
| 41 | * @param context the context that will use the path renderer |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 42 | * @param prChain the chain to add path renderers to. |
| 43 | */ |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 44 | static void AddPathRenderers(GrContext* context, GrPathRendererChain* prChain); |
bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 45 | |
| 46 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 47 | GrPathRenderer(); |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 48 | |
| 49 | /** |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 50 | * A caller may wish to use a path renderer to draw a path into the stencil buffer. However, |
| 51 | * the path renderer itself may require use of the stencil buffer. Also a path renderer may |
| 52 | * use a GrEffect coverage stage that sets coverage to zero to eliminate pixels that are covered |
| 53 | * by bounding geometry but outside the path. These exterior pixels would still be rendered into |
| 54 | * the stencil. |
| 55 | * |
| 56 | * A GrPathRenderer can provide three levels of support for stenciling paths: |
| 57 | * 1) kNoRestriction: This is the most general. The caller sets up the GrDrawState on the target |
| 58 | * and calls drawPath(). The path is rendered exactly as the draw state |
| 59 | * indicates including support for simultaneous color and stenciling with |
| 60 | * arbitrary stenciling rules. Pixels partially covered by AA paths are |
| 61 | * affected by the stencil settings. |
| 62 | * 2) kStencilOnly: The path renderer cannot apply arbitrary stencil rules nor shade and stencil |
| 63 | * simultaneously. The path renderer does support the stencilPath() function |
| 64 | * which performs no color writes and writes a non-zero stencil value to pixels |
| 65 | * covered by the path. |
| 66 | * 3) kNoSupport: This path renderer cannot be used to stencil the path. |
| 67 | */ |
| 68 | typedef GrPathRendererChain::StencilSupport StencilSupport; |
| 69 | static const StencilSupport kNoSupport_StencilSupport = |
| 70 | GrPathRendererChain::kNoSupport_StencilSupport; |
| 71 | static const StencilSupport kStencilOnly_StencilSupport = |
| 72 | GrPathRendererChain::kStencilOnly_StencilSupport; |
| 73 | static const StencilSupport kNoRestriction_StencilSupport = |
| 74 | GrPathRendererChain::kNoRestriction_StencilSupport; |
| 75 | |
| 76 | /** |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 77 | * This function is to get the stencil support for the current path. The path's fill must |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 78 | * not be an inverse type. |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 79 | * |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 80 | * @param stroke the stroke information (width, join, cap). |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 81 | * @param target target that the path will be rendered to |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 82 | */ |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 83 | StencilSupport getStencilSupport(const SkStrokeRec& stroke, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 84 | const GrDrawTarget* target) const { |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 85 | SkASSERT(!fPath.isInverseFillType()); |
| 86 | return this->onGetStencilSupport(stroke, target); |
| 87 | } |
| 88 | |
| 89 | // Set the path and fill type the path renderer is to use. |
| 90 | // 'fillType' is included as a parameter b.c. we often want to draw |
| 91 | // inverse filled paths normally filled. |
| 92 | void setPath(const SkPath& path, SkPath::FillType fillType) { |
| 93 | SkASSERT(fPath.isEmpty()); |
| 94 | fPath = path; |
| 95 | fPath.setFillType(fillType); |
| 96 | } |
| 97 | |
| 98 | void resetPath() { |
| 99 | fPath.reset(); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 100 | } |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 101 | |
bsalomon@google.com | 208236d | 2012-03-12 13:15:33 +0000 | [diff] [blame] | 102 | /** |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 103 | * Returns true if this path renderer is able to render the current path. Returning false |
| 104 | * allows the caller to fallback to another path renderer This function is called when |
| 105 | * searching for a path renderer capable of rendering a path. |
bsalomon@google.com | 208236d | 2012-03-12 13:15:33 +0000 | [diff] [blame] | 106 | * |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 107 | * @param stroke The stroke information (width, join, cap) |
bsalomon@google.com | 208236d | 2012-03-12 13:15:33 +0000 | [diff] [blame] | 108 | * @param target The target that the path will be rendered to |
| 109 | * @param antiAlias True if anti-aliasing is required. |
| 110 | * |
| 111 | * @return true if the path can be drawn by this object, false otherwise. |
| 112 | */ |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 113 | virtual bool canDrawPath(const SkStrokeRec& stroke, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 114 | const GrDrawTarget* target, |
| 115 | bool antiAlias) const = 0; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 116 | /** |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 117 | * Draws the current path into the draw target. If getStencilSupport() would return |
| 118 | * kNoRestriction then the subclass must respect the stencil settings of the |
| 119 | * target's draw state. |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 120 | * |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 121 | * @param stroke the stroke information (width, join, cap) |
bsalomon@google.com | 208236d | 2012-03-12 13:15:33 +0000 | [diff] [blame] | 122 | * @param target target that the path will be rendered to |
bsalomon@google.com | 208236d | 2012-03-12 13:15:33 +0000 | [diff] [blame] | 123 | * @param antiAlias true if anti-aliasing is required. |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 124 | */ |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 125 | bool drawPath(const SkStrokeRec& stroke, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 126 | GrDrawTarget* target, |
| 127 | bool antiAlias) { |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 128 | SkASSERT(!fPath.isEmpty()); |
| 129 | SkASSERT(this->canDrawPath(stroke, target, antiAlias)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 130 | SkASSERT(target->drawState()->getStencil().isDisabled() || |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 131 | kNoRestriction_StencilSupport == this->getStencilSupport(stroke, target)); |
| 132 | return this->onDrawPath(stroke, target, antiAlias); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 133 | } |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 134 | |
| 135 | /** |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 136 | * Draws the current path to the stencil buffer. Assume the writable stencil bits are already |
| 137 | * initialized to zero. The pixels inside the path will have non-zero stencil values |
| 138 | * afterwards. |
bsalomon@google.com | 208236d | 2012-03-12 13:15:33 +0000 | [diff] [blame] | 139 | * |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 140 | * @param stroke the stroke information (width, join, cap) |
bsalomon@google.com | 208236d | 2012-03-12 13:15:33 +0000 | [diff] [blame] | 141 | * @param target target that the path will be rendered to |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 142 | */ |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 143 | void stencilPath(const SkStrokeRec& stroke, GrDrawTarget* target) { |
| 144 | SkASSERT(!fPath.isEmpty()); |
| 145 | SkASSERT(kNoSupport_StencilSupport != this->getStencilSupport(stroke, target)); |
| 146 | this->onStencilPath(stroke, target); |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 147 | } |
| 148 | |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 149 | class AutoClearPath : ::SkNoncopyable { |
| 150 | public: |
| 151 | AutoClearPath(GrPathRenderer* renderer) : fRenderer(renderer) {} |
| 152 | AutoClearPath() : fRenderer(NULL) {} |
| 153 | ~AutoClearPath() { |
| 154 | this->reset(); |
| 155 | } |
| 156 | |
| 157 | GrPathRenderer* renderer() { |
| 158 | return fRenderer; |
| 159 | } |
| 160 | |
| 161 | void set(GrPathRenderer* renderer) { |
| 162 | this->reset(); |
| 163 | fRenderer = renderer; |
| 164 | } |
| 165 | |
| 166 | GrPathRenderer* operator->() { return fRenderer; } |
| 167 | |
| 168 | private: |
| 169 | void reset() { |
| 170 | if (NULL != fRenderer) { |
| 171 | fRenderer->resetPath(); |
| 172 | } |
| 173 | fRenderer = NULL; |
| 174 | } |
| 175 | |
| 176 | GrPathRenderer* fRenderer; |
| 177 | }; |
| 178 | |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 179 | // Helper for determining if we can treat a thin stroke as a hairline w/ coverage. |
| 180 | // If we can, we draw lots faster (raster device does this same test). |
| 181 | static bool IsStrokeHairlineOrEquivalent(const SkStrokeRec& stroke, const SkMatrix& matrix, |
| 182 | SkScalar* outCoverage) { |
| 183 | if (stroke.isHairlineStyle()) { |
| 184 | if (NULL != outCoverage) { |
| 185 | *outCoverage = SK_Scalar1; |
| 186 | } |
| 187 | return true; |
| 188 | } |
| 189 | return stroke.getStyle() == SkStrokeRec::kStroke_Style && |
| 190 | SkDrawTreatAAStrokeAsHairline(stroke.getWidth(), matrix, outCoverage); |
| 191 | } |
| 192 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 193 | protected: |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 194 | const SkPath& path() const { |
| 195 | return fPath; |
| 196 | } |
| 197 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 198 | /** |
| 199 | * Subclass overrides if it has any limitations of stenciling support. |
| 200 | */ |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 201 | virtual StencilSupport onGetStencilSupport(const SkStrokeRec&, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 202 | const GrDrawTarget*) const { |
| 203 | return kNoRestriction_StencilSupport; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Subclass implementation of drawPath() |
bsalomon@google.com | 208236d | 2012-03-12 13:15:33 +0000 | [diff] [blame] | 208 | */ |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 209 | virtual bool onDrawPath(const SkStrokeRec& stroke, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 210 | GrDrawTarget* target, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 211 | bool antiAlias) = 0; |
tomhudson@google.com | d22b6e4 | 2011-06-24 15:53:40 +0000 | [diff] [blame] | 212 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 213 | /** |
| 214 | * Subclass implementation of stencilPath(). Subclass must override iff it ever returns |
| 215 | * kStencilOnly in onGetStencilSupport(). |
| 216 | */ |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 217 | virtual void onStencilPath(const SkStrokeRec& stroke, GrDrawTarget* target) { |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 218 | GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kPreserve_ASRInit); |
| 219 | GrDrawState* drawState = target->drawState(); |
| 220 | GR_STATIC_CONST_SAME_STENCIL(kIncrementStencil, |
| 221 | kReplace_StencilOp, |
| 222 | kReplace_StencilOp, |
| 223 | kAlways_StencilFunc, |
| 224 | 0xffff, |
| 225 | 0xffff, |
| 226 | 0xffff); |
| 227 | drawState->setStencil(kIncrementStencil); |
| 228 | drawState->enableState(GrDrawState::kNoColorWrites_StateBit); |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 229 | this->drawPath(stroke, target, false); |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 230 | } |
| 231 | |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 232 | // Helper for getting the device bounds of a path. Inverse filled paths will have bounds set |
| 233 | // by devSize. Non-inverse path bounds will not necessarily be clipped to devSize. |
| 234 | static void GetPathDevBounds(const SkPath& path, |
| 235 | int devW, |
| 236 | int devH, |
| 237 | const SkMatrix& matrix, |
| 238 | SkRect* bounds); |
| 239 | |
| 240 | // Helper version that gets the dev width and height from a GrSurface. |
| 241 | static void GetPathDevBounds(const SkPath& path, |
| 242 | const GrSurface* device, |
| 243 | const SkMatrix& matrix, |
| 244 | SkRect* bounds) { |
| 245 | GetPathDevBounds(path, device->width(), device->height(), matrix, bounds); |
| 246 | } |
| 247 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 248 | private: |
robertphillips@google.com | 3e0c64a | 2014-02-10 16:46:23 +0000 | [diff] [blame^] | 249 | SkPath fPath; |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 250 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 251 | typedef SkRefCnt INHERITED; |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 254 | #endif |