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