blob: 50cf7483029e13f72bae86b29a5fd091edce3f36 [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:
mtklein2766c002015-06-26 11:45:03 -070033
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.come79f3202014-02-11 16:30:21 +000080 * @param path the path that will be drawn
81 * @param stroke the stroke information (width, join, cap).
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000082 */
robertphillipse7d4b2f2015-08-13 07:57:10 -070083 StencilSupport getStencilSupport(const SkPath& path, const GrStrokeInfo& stroke) const {
robertphillips@google.come79f3202014-02-11 16:30:21 +000084 SkASSERT(!path.isInverseFillType());
robertphillipse7d4b2f2015-08-13 07:57:10 -070085 return this->onGetStencilSupport(path, stroke);
bsalomon@google.comc2099d22012-03-02 21:26:50 +000086 }
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000087
bsalomon0aff2fa2015-07-31 06:48:27 -070088 /** Args to canDrawPath()
89 *
robertphillipse7d4b2f2015-08-13 07:57:10 -070090 * fShaderCaps The shader caps
bsalomon0aff2fa2015-07-31 06:48:27 -070091 * fPipelineBuilder The pipelineBuilder
92 * fViewMatrix The viewMatrix
93 * fPath The path to draw
94 * fStroke The stroke information (width, join, cap)
95 * fAntiAlias True if anti-aliasing is required.
96 */
97 struct CanDrawPathArgs {
robertphillipse7d4b2f2015-08-13 07:57:10 -070098 const GrShaderCaps* fShaderCaps;
99 const GrPipelineBuilder* fPipelineBuilder;// only used by GrStencilAndCoverPathRenderer
bsalomon0aff2fa2015-07-31 06:48:27 -0700100 const SkMatrix* fViewMatrix;
101 const SkPath* fPath;
102 const GrStrokeInfo* fStroke;
103 bool fAntiAlias;
robertphillipse7d4b2f2015-08-13 07:57:10 -0700104
105 void validate() const {
106 SkASSERT(fShaderCaps);
107 SkASSERT(fPipelineBuilder);
108 SkASSERT(fViewMatrix);
109 SkASSERT(fPath);
110 SkASSERT(fStroke);
111 SkASSERT(!fPath->isEmpty());
112 }
bsalomon0aff2fa2015-07-31 06:48:27 -0700113 };
114
bsalomon@google.com208236d2012-03-12 13:15:33 +0000115 /**
robertphillips@google.come79f3202014-02-11 16:30:21 +0000116 * Returns true if this path renderer is able to render the path. Returning false allows the
117 * caller to fallback to another path renderer This function is called when searching for a path
118 * renderer capable of rendering a path.
bsalomon@google.com208236d2012-03-12 13:15:33 +0000119 *
bsalomon@google.com208236d2012-03-12 13:15:33 +0000120 * @return true if the path can be drawn by this object, false otherwise.
121 */
bsalomon0aff2fa2015-07-31 06:48:27 -0700122 bool canDrawPath(const CanDrawPathArgs& args) const {
robertphillipse7d4b2f2015-08-13 07:57:10 -0700123 SkDEBUGCODE(args.validate();)
bsalomon0aff2fa2015-07-31 06:48:27 -0700124 return this->onCanDrawPath(args);
125 }
126
127 /**
128 * Args to drawPath()
129 *
130 * fTarget The target that the path will be rendered to
131 * fResourceProvider The resource provider for creating gpu resources to render the path
132 * fPipelineBuilder The pipelineBuilder
133 * fViewMatrix The viewMatrix
134 * fPath the path to draw.
135 * fStroke the stroke information (width, join, cap)
136 * fAntiAlias true if anti-aliasing is required.
137 */
138 struct DrawPathArgs {
139 GrDrawTarget* fTarget;
140 GrResourceProvider* fResourceProvider;
141 GrPipelineBuilder* fPipelineBuilder;
142 GrColor fColor;
143 const SkMatrix* fViewMatrix;
144 const SkPath* fPath;
145 const GrStrokeInfo* fStroke;
146 bool fAntiAlias;
robertphillipse7d4b2f2015-08-13 07:57:10 -0700147
148 void validate() const {
149 SkASSERT(fTarget);
150 SkASSERT(fResourceProvider);
151 SkASSERT(fPipelineBuilder);
152 SkASSERT(fViewMatrix);
153 SkASSERT(fPath);
154 SkASSERT(fStroke);
155 SkASSERT(!fPath->isEmpty());
156 }
bsalomon0aff2fa2015-07-31 06:48:27 -0700157 };
158
bsalomon@google.comee435122011-07-01 14:57:55 +0000159 /**
robertphillips@google.come79f3202014-02-11 16:30:21 +0000160 * Draws the path into the draw target. If getStencilSupport() would return kNoRestriction then
bsalomon0aff2fa2015-07-31 06:48:27 -0700161 * the subclass must respect the stencil settings of the GrPipelineBuilder.
bsalomon@google.comee435122011-07-01 14:57:55 +0000162 */
bsalomon0aff2fa2015-07-31 06:48:27 -0700163 bool drawPath(const DrawPathArgs& args) {
robertphillipse7d4b2f2015-08-13 07:57:10 -0700164 SkDEBUGCODE(args.validate();)
bsalomon0aff2fa2015-07-31 06:48:27 -0700165#ifdef SK_DEBUG
166 CanDrawPathArgs canArgs;
robertphillipse7d4b2f2015-08-13 07:57:10 -0700167 canArgs.fShaderCaps = args.fTarget->caps()->shaderCaps();
bsalomon0aff2fa2015-07-31 06:48:27 -0700168 canArgs.fPipelineBuilder = args.fPipelineBuilder;
169 canArgs.fViewMatrix = args.fViewMatrix;
170 canArgs.fPath = args.fPath;
171 canArgs.fStroke = args.fStroke;
172 canArgs.fAntiAlias = args.fAntiAlias;
173 SkASSERT(this->canDrawPath(canArgs));
174 SkASSERT(args.fPipelineBuilder->getStencil().isDisabled() ||
robertphillipse7d4b2f2015-08-13 07:57:10 -0700175 kNoRestriction_StencilSupport == this->getStencilSupport(*args.fPath,
bsalomon0aff2fa2015-07-31 06:48:27 -0700176 *args.fStroke));
177#endif
178 return this->onDrawPath(args);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000179 }
bsalomon@google.comee435122011-07-01 14:57:55 +0000180
bsalomon0aff2fa2015-07-31 06:48:27 -0700181 /* Args to stencilPath().
182 *
183 * fTarget The target that the path will be rendered to.
robertphillipse7d4b2f2015-08-13 07:57:10 -0700184 * fResourceProvider The resource provider for creating gpu resources to render the path
bsalomon0aff2fa2015-07-31 06:48:27 -0700185 * fPipelineBuilder The pipeline builder.
186 * fViewMatrix Matrix applied to the path.
187 * fPath The path to draw.
188 * fStroke The stroke information (width, join, cap)
189 */
190 struct StencilPathArgs {
191 GrDrawTarget* fTarget;
bsalomon0aff2fa2015-07-31 06:48:27 -0700192 GrResourceProvider* fResourceProvider;
robertphillipse7d4b2f2015-08-13 07:57:10 -0700193 GrPipelineBuilder* fPipelineBuilder;
bsalomon0aff2fa2015-07-31 06:48:27 -0700194 const SkMatrix* fViewMatrix;
195 const SkPath* fPath;
196 const GrStrokeInfo* fStroke;
robertphillipse7d4b2f2015-08-13 07:57:10 -0700197
198 void validate() const {
199 SkASSERT(fTarget);
200 SkASSERT(fResourceProvider);
201 SkASSERT(fPipelineBuilder);
202 SkASSERT(fViewMatrix);
203 SkASSERT(fPath);
204 SkASSERT(fStroke);
205 SkASSERT(!fPath->isEmpty());
206 }
bsalomon0aff2fa2015-07-31 06:48:27 -0700207 };
208
bsalomon@google.comee435122011-07-01 14:57:55 +0000209 /**
robertphillips@google.come79f3202014-02-11 16:30:21 +0000210 * Draws the path to the stencil buffer. Assume the writable stencil bits are already
211 * initialized to zero. The pixels inside the path will have non-zero stencil values afterwards.
bsalomon@google.com208236d2012-03-12 13:15:33 +0000212 *
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000213 */
bsalomon0aff2fa2015-07-31 06:48:27 -0700214 void stencilPath(const StencilPathArgs& args) {
robertphillipse7d4b2f2015-08-13 07:57:10 -0700215 SkDEBUGCODE(args.validate();)
216 SkASSERT(kNoSupport_StencilSupport != this->getStencilSupport(*args.fPath, *args.fStroke));
217
bsalomon0aff2fa2015-07-31 06:48:27 -0700218 this->onStencilPath(args);
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000219 }
220
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000221 // Helper for determining if we can treat a thin stroke as a hairline w/ coverage.
222 // If we can, we draw lots faster (raster device does this same test).
kkinnunen18996512015-04-26 23:18:49 -0700223 static bool IsStrokeHairlineOrEquivalent(const GrStrokeInfo& stroke, const SkMatrix& matrix,
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000224 SkScalar* outCoverage) {
kkinnunen18996512015-04-26 23:18:49 -0700225 if (stroke.isDashed()) {
226 return false;
227 }
kkinnunend156d362015-05-18 22:23:54 -0700228 if (stroke.isHairlineStyle()) {
bsalomon49f085d2014-09-05 13:34:00 -0700229 if (outCoverage) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000230 *outCoverage = SK_Scalar1;
231 }
232 return true;
233 }
kkinnunend156d362015-05-18 22:23:54 -0700234 return stroke.getStyle() == SkStrokeRec::kStroke_Style &&
235 SkDrawTreatAAStrokeAsHairline(stroke.getWidth(), matrix, outCoverage);
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000236 }
237
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000238protected:
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000239 // Helper for getting the device bounds of a path. Inverse filled paths will have bounds set
240 // by devSize. Non-inverse path bounds will not necessarily be clipped to devSize.
241 static void GetPathDevBounds(const SkPath& path,
242 int devW,
243 int devH,
244 const SkMatrix& matrix,
245 SkRect* bounds);
246
247 // Helper version that gets the dev width and height from a GrSurface.
248 static void GetPathDevBounds(const SkPath& path,
249 const GrSurface* device,
250 const SkMatrix& matrix,
251 SkRect* bounds) {
252 GetPathDevBounds(path, device->width(), device->height(), matrix, bounds);
253 }
254
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000255private:
bsalomon0aff2fa2015-07-31 06:48:27 -0700256 /**
257 * Subclass overrides if it has any limitations of stenciling support.
258 */
robertphillipse7d4b2f2015-08-13 07:57:10 -0700259 virtual StencilSupport onGetStencilSupport(const SkPath&, const GrStrokeInfo&) const {
bsalomon0aff2fa2015-07-31 06:48:27 -0700260 return kNoRestriction_StencilSupport;
261 }
262
263 /**
264 * Subclass implementation of drawPath()
265 */
266 virtual bool onDrawPath(const DrawPathArgs& args) = 0;
267
268 /**
269 * Subclass implementation of canDrawPath()
270 */
271 virtual bool onCanDrawPath(const CanDrawPathArgs& args) const = 0;
272
273 /**
274 * Subclass implementation of stencilPath(). Subclass must override iff it ever returns
275 * kStencilOnly in onGetStencilSupport().
276 */
277 virtual void onStencilPath(const StencilPathArgs& args) {
278 GR_STATIC_CONST_SAME_STENCIL(kIncrementStencil,
279 kReplace_StencilOp,
280 kReplace_StencilOp,
281 kAlways_StencilFunc,
282 0xffff,
283 0xffff,
284 0xffff);
285 args.fPipelineBuilder->setStencil(kIncrementStencil);
286 args.fPipelineBuilder->setDisableColorXPFactory();
287 DrawPathArgs drawArgs;
288 drawArgs.fTarget = args.fTarget;
289 drawArgs.fResourceProvider = args.fResourceProvider;
290 drawArgs.fPipelineBuilder = args.fPipelineBuilder;
291 drawArgs.fColor = 0xFFFFFFFF;
292 drawArgs.fViewMatrix = args.fViewMatrix;
293 drawArgs.fPath = args.fPath;
294 drawArgs.fStroke = args.fStroke;
295 drawArgs.fAntiAlias = false;
296 this->drawPath(drawArgs);
297 }
298
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000299
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000300 typedef SkRefCnt INHERITED;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000301};
302
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000303#endif