blob: 8e03edb8939ee7ad3c623128fce56fcaab532106 [file] [log] [blame]
skia.committer@gmail.com3e50e992013-05-21 07:01:40 +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
9#ifndef GrPathRenderer_DEFINED
10#define GrPathRenderer_DEFINED
11
12#include "GrDrawTarget.h"
bsalomon@google.com30085192011-08-19 15:42:31 +000013#include "GrPathRendererChain.h"
bsalomon@google.com45a15f52012-12-10 19:10:17 +000014#include "GrStencil.h"
kkinnunen18996512015-04-26 23:18:49 -070015#include "GrStrokeInfo.h"
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000016
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +000017#include "SkDrawProcs.h"
bsalomon@google.com49313f62011-09-14 13:54:05 +000018#include "SkTArray.h"
19
reed@google.com07f3ee12011-05-16 17:21:57 +000020class SkPath;
bsalomon@google.com30085192011-08-19 15:42:31 +000021
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000022struct GrPoint;
23
24/**
25 * Base class for drawing paths into a GrDrawTarget.
robertphillips@google.combf5cad42012-05-10 12:40:40 +000026 *
egdaniel8dd688b2015-01-22 10:16:09 -080027 * Derived classes can use stages GrPaint::kTotalStages through GrPipelineBuilder::kNumStages-1.
28 * The stages before GrPaint::kTotalStages are reserved for setting up the draw (i.e., textures and
bsalomon@google.com45a15f52012-12-10 19:10:17 +000029 * filter masks).
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000030 */
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000031class SK_API GrPathRenderer : public SkRefCnt {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000032public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000033 SK_DECLARE_INST_COUNT(GrPathRenderer)
bsalomon@google.com30085192011-08-19 15:42:31 +000034
35 /**
bsalomon@google.com45a15f52012-12-10 19:10:17 +000036 * 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.com30085192011-08-19 15:42:31 +000040 *
41 * @param context the context that will use the path renderer
bsalomon@google.com30085192011-08-19 15:42:31 +000042 * @param prChain the chain to add path renderers to.
43 */
bsalomon@google.com45a15f52012-12-10 19:10:17 +000044 static void AddPathRenderers(GrContext* context, GrPathRendererChain* prChain);
bsalomon@google.com30085192011-08-19 15:42:31 +000045
46
bsalomon@google.comc2099d22012-03-02 21:26:50 +000047 GrPathRenderer();
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000048
49 /**
bsalomon@google.com45a15f52012-12-10 19:10:17 +000050 * 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
joshualittb0a8a372014-09-23 09:50:21 -070052 * use a GrProcessor coverage stage that sets coverage to zero to eliminate pixels that are
53 * covered by bounding geometry but outside the path. These exterior pixels would still be
54 * rendered into the stencil.
bsalomon@google.com45a15f52012-12-10 19:10:17 +000055 *
56 * A GrPathRenderer can provide three levels of support for stenciling paths:
egdaniel8dd688b2015-01-22 10:16:09 -080057 * 1) kNoRestriction: This is the most general. The caller sets up the GrPipelineBuilder on the target
bsalomon@google.com45a15f52012-12-10 19:10:17 +000058 * 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.come79f3202014-02-11 16:30:21 +000077 * This function is to get the stencil support for a particular path. The path's fill must
bsalomon@google.com45a15f52012-12-10 19:10:17 +000078 * not be an inverse type.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000079 *
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +000080 * @param target target that the path will be rendered to
robertphillips@google.come79f3202014-02-11 16:30:21 +000081 * @param path the path that will be drawn
82 * @param stroke the stroke information (width, join, cap).
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000083 */
joshualitt9853cce2014-11-17 14:22:48 -080084 StencilSupport getStencilSupport(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -080085 const GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -080086 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -070087 const GrStrokeInfo& stroke) const {
robertphillips@google.come79f3202014-02-11 16:30:21 +000088 SkASSERT(!path.isInverseFillType());
egdaniel8dd688b2015-01-22 10:16:09 -080089 return this->onGetStencilSupport(target, pipelineBuilder, path, stroke);
bsalomon@google.comc2099d22012-03-02 21:26:50 +000090 }
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000091
bsalomon@google.com208236d2012-03-12 13:15:33 +000092 /**
robertphillips@google.come79f3202014-02-11 16:30:21 +000093 * Returns true if this path renderer is able to render the path. Returning false allows the
94 * caller to fallback to another path renderer This function is called when searching for a path
95 * renderer capable of rendering a path.
bsalomon@google.com208236d2012-03-12 13:15:33 +000096 *
egdaniel8dd688b2015-01-22 10:16:09 -080097 * @param target The target that the path will be rendered to
98 * @param pipelineBuilder The pipelineBuilder
99 * @param viewMatrix The viewMatrix
100 * @param path The path to draw
101 * @param stroke The stroke information (width, join, cap)
102 * @param antiAlias True if anti-aliasing is required.
bsalomon@google.com208236d2012-03-12 13:15:33 +0000103 *
104 * @return true if the path can be drawn by this object, false otherwise.
105 */
joshualitt9853cce2014-11-17 14:22:48 -0800106 virtual bool canDrawPath(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800107 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800108 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800109 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -0700110 const GrStrokeInfo& rec,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000111 bool antiAlias) const = 0;
bsalomon@google.comee435122011-07-01 14:57:55 +0000112 /**
robertphillips@google.come79f3202014-02-11 16:30:21 +0000113 * Draws the path into the draw target. If getStencilSupport() would return kNoRestriction then
114 * the subclass must respect the stencil settings of the target's draw state.
bsalomon@google.comee435122011-07-01 14:57:55 +0000115 *
joshualitt8059eb92014-12-29 15:10:07 -0800116 * @param target The target that the path will be rendered to
egdaniel8dd688b2015-01-22 10:16:09 -0800117 * @param pipelineBuilder The pipelineBuilder
joshualitt8059eb92014-12-29 15:10:07 -0800118 * @param viewMatrix The viewMatrix
robertphillips@google.come79f3202014-02-11 16:30:21 +0000119 * @param path the path to draw.
sugoi@google.com12b4e272012-12-06 20:13:11 +0000120 * @param stroke the stroke information (width, join, cap)
bsalomon@google.com208236d2012-03-12 13:15:33 +0000121 * @param antiAlias true if anti-aliasing is required.
bsalomon@google.comee435122011-07-01 14:57:55 +0000122 */
joshualitt9853cce2014-11-17 14:22:48 -0800123 bool drawPath(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800124 GrPipelineBuilder* ds,
joshualitt2e3b3e32014-12-09 13:31:14 -0800125 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -0800126 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800127 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -0700128 const GrStrokeInfo& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000129 bool antiAlias) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000130 SkASSERT(!path.isEmpty());
joshualitt8059eb92014-12-29 15:10:07 -0800131 SkASSERT(this->canDrawPath(target, ds, viewMatrix, path, stroke, antiAlias));
joshualitt9853cce2014-11-17 14:22:48 -0800132 SkASSERT(ds->getStencil().isDisabled() ||
133 kNoRestriction_StencilSupport == this->getStencilSupport(target, ds, path,
134 stroke));
joshualitt8059eb92014-12-29 15:10:07 -0800135 return this->onDrawPath(target, ds, color, viewMatrix, path, stroke, antiAlias);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000136 }
bsalomon@google.comee435122011-07-01 14:57:55 +0000137
138 /**
robertphillips@google.come79f3202014-02-11 16:30:21 +0000139 * Draws the path to the stencil buffer. Assume the writable stencil bits are already
140 * initialized to zero. The pixels inside the path will have non-zero stencil values afterwards.
bsalomon@google.com208236d2012-03-12 13:15:33 +0000141 *
robertphillips@google.come79f3202014-02-11 16:30:21 +0000142 * @param path the path to draw.
sugoi@google.com12b4e272012-12-06 20:13:11 +0000143 * @param stroke the stroke information (width, join, cap)
bsalomon@google.com208236d2012-03-12 13:15:33 +0000144 * @param target target that the path will be rendered to
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000145 */
joshualitt9853cce2014-11-17 14:22:48 -0800146 void stencilPath(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800147 GrPipelineBuilder* ds,
joshualitt8059eb92014-12-29 15:10:07 -0800148 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800149 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -0700150 const GrStrokeInfo& stroke) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000151 SkASSERT(!path.isEmpty());
joshualitt9853cce2014-11-17 14:22:48 -0800152 SkASSERT(kNoSupport_StencilSupport != this->getStencilSupport(target, ds, path, stroke));
joshualitt8059eb92014-12-29 15:10:07 -0800153 this->onStencilPath(target, ds, viewMatrix, path, stroke);
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000154 }
155
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000156 // Helper for determining if we can treat a thin stroke as a hairline w/ coverage.
157 // If we can, we draw lots faster (raster device does this same test).
kkinnunen18996512015-04-26 23:18:49 -0700158 static bool IsStrokeHairlineOrEquivalent(const GrStrokeInfo& stroke, const SkMatrix& matrix,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000159 SkScalar* outCoverage) {
kkinnunen18996512015-04-26 23:18:49 -0700160 if (stroke.isDashed()) {
161 return false;
162 }
kkinnunend156d362015-05-18 22:23:54 -0700163 if (stroke.isHairlineStyle()) {
bsalomon49f085d2014-09-05 13:34:00 -0700164 if (outCoverage) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000165 *outCoverage = SK_Scalar1;
166 }
167 return true;
168 }
kkinnunend156d362015-05-18 22:23:54 -0700169 return stroke.getStyle() == SkStrokeRec::kStroke_Style &&
170 SkDrawTreatAAStrokeAsHairline(stroke.getWidth(), matrix, outCoverage);
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000171 }
172
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000173protected:
174 /**
175 * Subclass overrides if it has any limitations of stenciling support.
176 */
joshualitt9853cce2014-11-17 14:22:48 -0800177 virtual StencilSupport onGetStencilSupport(const GrDrawTarget*,
egdaniel8dd688b2015-01-22 10:16:09 -0800178 const GrPipelineBuilder*,
joshualitt9853cce2014-11-17 14:22:48 -0800179 const SkPath&,
kkinnunen18996512015-04-26 23:18:49 -0700180 const GrStrokeInfo&) const {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000181 return kNoRestriction_StencilSupport;
182 }
183
184 /**
185 * Subclass implementation of drawPath()
bsalomon@google.com208236d2012-03-12 13:15:33 +0000186 */
joshualitt9853cce2014-11-17 14:22:48 -0800187 virtual bool onDrawPath(GrDrawTarget*,
egdaniel8dd688b2015-01-22 10:16:09 -0800188 GrPipelineBuilder*,
joshualitt2e3b3e32014-12-09 13:31:14 -0800189 GrColor,
joshualitt8059eb92014-12-29 15:10:07 -0800190 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800191 const SkPath&,
kkinnunen18996512015-04-26 23:18:49 -0700192 const GrStrokeInfo&,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000193 bool antiAlias) = 0;
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000194
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000195 /**
196 * Subclass implementation of stencilPath(). Subclass must override iff it ever returns
197 * kStencilOnly in onGetStencilSupport().
198 */
joshualitt9853cce2014-11-17 14:22:48 -0800199 virtual void onStencilPath(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800200 GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800201 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800202 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -0700203 const GrStrokeInfo& stroke) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000204 GR_STATIC_CONST_SAME_STENCIL(kIncrementStencil,
205 kReplace_StencilOp,
206 kReplace_StencilOp,
207 kAlways_StencilFunc,
208 0xffff,
209 0xffff,
210 0xffff);
egdaniel8dd688b2015-01-22 10:16:09 -0800211 pipelineBuilder->setStencil(kIncrementStencil);
212 pipelineBuilder->setDisableColorXPFactory();
213 this->drawPath(target, pipelineBuilder, GrColor_WHITE, viewMatrix, path, stroke, false);
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000214 }
215
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000216 // Helper for getting the device bounds of a path. Inverse filled paths will have bounds set
217 // by devSize. Non-inverse path bounds will not necessarily be clipped to devSize.
218 static void GetPathDevBounds(const SkPath& path,
219 int devW,
220 int devH,
221 const SkMatrix& matrix,
222 SkRect* bounds);
223
224 // Helper version that gets the dev width and height from a GrSurface.
225 static void GetPathDevBounds(const SkPath& path,
226 const GrSurface* device,
227 const SkMatrix& matrix,
228 SkRect* bounds) {
229 GetPathDevBounds(path, device->width(), device->height(), matrix, bounds);
230 }
231
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000232private:
233
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000234 typedef SkRefCnt INHERITED;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000235};
236
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000237#endif