blob: 4d38aa064d64b3943b20fb17a3de3dea249db351 [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"
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000015
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +000016#include "SkDrawProcs.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000017#include "SkStrokeRec.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 *
bsalomon@google.com45a15f52012-12-10 19:10:17 +000027 * 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.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:
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.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,
85 const GrDrawState* drawState,
86 const SkPath& path,
87 const SkStrokeRec& stroke) const {
robertphillips@google.come79f3202014-02-11 16:30:21 +000088 SkASSERT(!path.isInverseFillType());
joshualitt9853cce2014-11-17 14:22:48 -080089 return this->onGetStencilSupport(target, drawState, 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 *
robertphillips@google.come79f3202014-02-11 16:30:21 +000097 * @param path The path to draw
sugoi@google.com12b4e272012-12-06 20:13:11 +000098 * @param stroke The stroke information (width, join, cap)
bsalomon@google.com208236d2012-03-12 13:15:33 +000099 * @param target The target that the path will be rendered to
100 * @param antiAlias True if anti-aliasing is required.
101 *
102 * @return true if the path can be drawn by this object, false otherwise.
103 */
joshualitt9853cce2014-11-17 14:22:48 -0800104 virtual bool canDrawPath(const GrDrawTarget* target,
105 const GrDrawState* drawState,
106 const SkPath& path,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000107 const SkStrokeRec& rec,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000108 bool antiAlias) const = 0;
bsalomon@google.comee435122011-07-01 14:57:55 +0000109 /**
robertphillips@google.come79f3202014-02-11 16:30:21 +0000110 * Draws the path into the draw target. If getStencilSupport() would return kNoRestriction then
111 * the subclass must respect the stencil settings of the target's draw state.
bsalomon@google.comee435122011-07-01 14:57:55 +0000112 *
robertphillips@google.come79f3202014-02-11 16:30:21 +0000113 * @param path the path to draw.
sugoi@google.com12b4e272012-12-06 20:13:11 +0000114 * @param stroke the stroke information (width, join, cap)
bsalomon@google.com208236d2012-03-12 13:15:33 +0000115 * @param target target that the path will be rendered to
bsalomon@google.com208236d2012-03-12 13:15:33 +0000116 * @param antiAlias true if anti-aliasing is required.
bsalomon@google.comee435122011-07-01 14:57:55 +0000117 */
joshualitt9853cce2014-11-17 14:22:48 -0800118 bool drawPath(GrDrawTarget* target,
119 GrDrawState* ds,
joshualitt2e3b3e32014-12-09 13:31:14 -0800120 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -0800121 const SkPath& path,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000122 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000123 bool antiAlias) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000124 SkASSERT(!path.isEmpty());
joshualitt9853cce2014-11-17 14:22:48 -0800125 SkASSERT(this->canDrawPath(target, ds, path, stroke, antiAlias));
126 SkASSERT(ds->getStencil().isDisabled() ||
127 kNoRestriction_StencilSupport == this->getStencilSupport(target, ds, path,
128 stroke));
joshualitt2e3b3e32014-12-09 13:31:14 -0800129 return this->onDrawPath(target, ds, color, path, stroke, antiAlias);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000130 }
bsalomon@google.comee435122011-07-01 14:57:55 +0000131
132 /**
robertphillips@google.come79f3202014-02-11 16:30:21 +0000133 * Draws the path to the stencil buffer. Assume the writable stencil bits are already
134 * initialized to zero. The pixels inside the path will have non-zero stencil values afterwards.
bsalomon@google.com208236d2012-03-12 13:15:33 +0000135 *
robertphillips@google.come79f3202014-02-11 16:30:21 +0000136 * @param path the path to draw.
sugoi@google.com12b4e272012-12-06 20:13:11 +0000137 * @param stroke the stroke information (width, join, cap)
bsalomon@google.com208236d2012-03-12 13:15:33 +0000138 * @param target target that the path will be rendered to
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000139 */
joshualitt9853cce2014-11-17 14:22:48 -0800140 void stencilPath(GrDrawTarget* target,
141 GrDrawState* ds,
142 const SkPath& path,
143 const SkStrokeRec& stroke) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000144 SkASSERT(!path.isEmpty());
joshualitt9853cce2014-11-17 14:22:48 -0800145 SkASSERT(kNoSupport_StencilSupport != this->getStencilSupport(target, ds, path, stroke));
146 this->onStencilPath(target, ds, path, stroke);
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000147 }
148
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000149 // Helper for determining if we can treat a thin stroke as a hairline w/ coverage.
150 // If we can, we draw lots faster (raster device does this same test).
151 static bool IsStrokeHairlineOrEquivalent(const SkStrokeRec& stroke, const SkMatrix& matrix,
152 SkScalar* outCoverage) {
153 if (stroke.isHairlineStyle()) {
bsalomon49f085d2014-09-05 13:34:00 -0700154 if (outCoverage) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000155 *outCoverage = SK_Scalar1;
156 }
157 return true;
158 }
159 return stroke.getStyle() == SkStrokeRec::kStroke_Style &&
160 SkDrawTreatAAStrokeAsHairline(stroke.getWidth(), matrix, outCoverage);
161 }
162
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000163protected:
164 /**
165 * Subclass overrides if it has any limitations of stenciling support.
166 */
joshualitt9853cce2014-11-17 14:22:48 -0800167 virtual StencilSupport onGetStencilSupport(const GrDrawTarget*,
168 const GrDrawState*,
169 const SkPath&,
170 const SkStrokeRec&) const {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000171 return kNoRestriction_StencilSupport;
172 }
173
174 /**
175 * Subclass implementation of drawPath()
bsalomon@google.com208236d2012-03-12 13:15:33 +0000176 */
joshualitt9853cce2014-11-17 14:22:48 -0800177 virtual bool onDrawPath(GrDrawTarget*,
178 GrDrawState*,
joshualitt2e3b3e32014-12-09 13:31:14 -0800179 GrColor,
joshualitt9853cce2014-11-17 14:22:48 -0800180 const SkPath&,
181 const SkStrokeRec&,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000182 bool antiAlias) = 0;
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000183
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000184 /**
185 * Subclass implementation of stencilPath(). Subclass must override iff it ever returns
186 * kStencilOnly in onGetStencilSupport().
187 */
joshualitt9853cce2014-11-17 14:22:48 -0800188 virtual void onStencilPath(GrDrawTarget* target,
189 GrDrawState* drawState,
190 const SkPath& path,
191 const SkStrokeRec& stroke) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000192 GR_STATIC_CONST_SAME_STENCIL(kIncrementStencil,
193 kReplace_StencilOp,
194 kReplace_StencilOp,
195 kAlways_StencilFunc,
196 0xffff,
197 0xffff,
198 0xffff);
199 drawState->setStencil(kIncrementStencil);
egdaniel080e6732014-12-22 07:35:52 -0800200 drawState->setDisableColorXPFactory();
joshualitt2e3b3e32014-12-09 13:31:14 -0800201 this->drawPath(target, drawState, GrColor_WHITE, path, stroke, false);
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000202 }
203
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000204 // Helper for getting the device bounds of a path. Inverse filled paths will have bounds set
205 // by devSize. Non-inverse path bounds will not necessarily be clipped to devSize.
206 static void GetPathDevBounds(const SkPath& path,
207 int devW,
208 int devH,
209 const SkMatrix& matrix,
210 SkRect* bounds);
211
212 // Helper version that gets the dev width and height from a GrSurface.
213 static void GetPathDevBounds(const SkPath& path,
214 const GrSurface* device,
215 const SkMatrix& matrix,
216 SkRect* bounds) {
217 GetPathDevBounds(path, device->width(), device->height(), matrix, bounds);
218 }
219
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000220private:
221
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000222 typedef SkRefCnt INHERITED;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000223};
224
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000225#endif