blob: 692cefc7f1a4de6097dfebd4472ab6c8bdc2d912 [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
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.com3e0c64a2014-02-10 16:46:23 +000077 * This function is to get the stencil support for the current 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 *
sugoi@google.com12b4e272012-12-06 20:13:11 +000080 * @param stroke the stroke information (width, join, cap).
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +000081 * @param target target that the path will be rendered to
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000082 */
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +000083 StencilSupport getStencilSupport(const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +000084 const GrDrawTarget* target) const {
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +000085 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.comc2099d22012-03-02 21:26:50 +0000100 }
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000101
bsalomon@google.com208236d2012-03-12 13:15:33 +0000102 /**
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000103 * 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.com208236d2012-03-12 13:15:33 +0000106 *
sugoi@google.com12b4e272012-12-06 20:13:11 +0000107 * @param stroke The stroke information (width, join, cap)
bsalomon@google.com208236d2012-03-12 13:15:33 +0000108 * @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.com3e0c64a2014-02-10 16:46:23 +0000113 virtual bool canDrawPath(const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000114 const GrDrawTarget* target,
115 bool antiAlias) const = 0;
bsalomon@google.comee435122011-07-01 14:57:55 +0000116 /**
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000117 * 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.comee435122011-07-01 14:57:55 +0000120 *
sugoi@google.com12b4e272012-12-06 20:13:11 +0000121 * @param stroke the stroke information (width, join, cap)
bsalomon@google.com208236d2012-03-12 13:15:33 +0000122 * @param target target that the path will be rendered to
bsalomon@google.com208236d2012-03-12 13:15:33 +0000123 * @param antiAlias true if anti-aliasing is required.
bsalomon@google.comee435122011-07-01 14:57:55 +0000124 */
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000125 bool drawPath(const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000126 GrDrawTarget* target,
127 bool antiAlias) {
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000128 SkASSERT(!fPath.isEmpty());
129 SkASSERT(this->canDrawPath(stroke, target, antiAlias));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000130 SkASSERT(target->drawState()->getStencil().isDisabled() ||
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000131 kNoRestriction_StencilSupport == this->getStencilSupport(stroke, target));
132 return this->onDrawPath(stroke, target, antiAlias);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000133 }
bsalomon@google.comee435122011-07-01 14:57:55 +0000134
135 /**
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000136 * 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.com208236d2012-03-12 13:15:33 +0000139 *
sugoi@google.com12b4e272012-12-06 20:13:11 +0000140 * @param stroke the stroke information (width, join, cap)
bsalomon@google.com208236d2012-03-12 13:15:33 +0000141 * @param target target that the path will be rendered to
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000142 */
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000143 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.com45a15f52012-12-10 19:10:17 +0000147 }
148
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000149 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.orge0a868c2013-11-22 07:02:11 +0000179 // 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.com45a15f52012-12-10 19:10:17 +0000193protected:
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000194 const SkPath& path() const {
195 return fPath;
196 }
197
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000198 /**
199 * Subclass overrides if it has any limitations of stenciling support.
200 */
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000201 virtual StencilSupport onGetStencilSupport(const SkStrokeRec&,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000202 const GrDrawTarget*) const {
203 return kNoRestriction_StencilSupport;
204 }
205
206 /**
207 * Subclass implementation of drawPath()
bsalomon@google.com208236d2012-03-12 13:15:33 +0000208 */
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000209 virtual bool onDrawPath(const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000210 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000211 bool antiAlias) = 0;
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000212
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000213 /**
214 * Subclass implementation of stencilPath(). Subclass must override iff it ever returns
215 * kStencilOnly in onGetStencilSupport().
216 */
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000217 virtual void onStencilPath(const SkStrokeRec& stroke, GrDrawTarget* target) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000218 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.com3e0c64a2014-02-10 16:46:23 +0000229 this->drawPath(stroke, target, false);
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000230 }
231
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000232 // 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.comdfe75bc2011-03-25 12:31:16 +0000248private:
robertphillips@google.com3e0c64a2014-02-10 16:46:23 +0000249 SkPath fPath;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000250
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000251 typedef SkRefCnt INHERITED;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000252};
253
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000254#endif