blob: 88665c1d423eedd03259c5c54fd805259c843b11 [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 /**
77 * This function is to get the stencil support for a particular path. The path's fill must
78 * not be an inverse type.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000079 *
sugoi@google.com12b4e272012-12-06 20:13:11 +000080 * @param target target that the path will be rendered to
81 * @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 */
bsalomon@google.com45a15f52012-12-10 19:10:17 +000084 StencilSupport getStencilSupport(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000085 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +000086 const GrDrawTarget* target) const {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000087 SkASSERT(!path.isInverseFillType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +000088 return this->onGetStencilSupport(path, stroke, target);
bsalomon@google.comc2099d22012-03-02 21:26:50 +000089 }
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000090
bsalomon@google.com208236d2012-03-12 13:15:33 +000091 /**
bsalomon@google.com45a15f52012-12-10 19:10:17 +000092 * Returns true if this path renderer is able to render the path. Returning false allows the
93 * caller to fallback to another path renderer This function is called when searching for a path
94 * renderer capable of rendering a path.
bsalomon@google.com208236d2012-03-12 13:15:33 +000095 *
96 * @param path The path to draw
sugoi@google.com12b4e272012-12-06 20:13:11 +000097 * @param stroke The stroke information (width, join, cap)
bsalomon@google.com208236d2012-03-12 13:15:33 +000098 * @param target The target that the path will be rendered to
99 * @param antiAlias True if anti-aliasing is required.
100 *
101 * @return true if the path can be drawn by this object, false otherwise.
102 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000103 virtual bool canDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000104 const SkStrokeRec& rec,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000105 const GrDrawTarget* target,
106 bool antiAlias) const = 0;
bsalomon@google.comee435122011-07-01 14:57:55 +0000107 /**
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000108 * Draws the path into the draw target. If getStencilSupport() would return kNoRestriction then
109 * the subclass must respect the stencil settings of the target's draw state.
bsalomon@google.comee435122011-07-01 14:57:55 +0000110 *
bsalomon@google.com208236d2012-03-12 13:15:33 +0000111 * @param path the path to draw.
sugoi@google.com12b4e272012-12-06 20:13:11 +0000112 * @param stroke the stroke information (width, join, cap)
bsalomon@google.com208236d2012-03-12 13:15:33 +0000113 * @param target target that the path will be rendered to
bsalomon@google.com208236d2012-03-12 13:15:33 +0000114 * @param antiAlias true if anti-aliasing is required.
bsalomon@google.comee435122011-07-01 14:57:55 +0000115 */
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000116 bool drawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000117 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000118 GrDrawTarget* target,
119 bool antiAlias) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000120 SkASSERT(!path.isEmpty());
121 SkASSERT(this->canDrawPath(path, stroke, target, antiAlias));
122 SkASSERT(target->drawState()->getStencil().isDisabled() ||
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000123 kNoRestriction_StencilSupport == this->getStencilSupport(path, stroke, target));
sugoi@google.com12b4e272012-12-06 20:13:11 +0000124 return this->onDrawPath(path, stroke, target, antiAlias);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000125 }
bsalomon@google.comee435122011-07-01 14:57:55 +0000126
127 /**
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000128 * Draws the path to the stencil buffer. Assume the writable stencil bits are already
129 * initialized to zero. The pixels inside the path will have non-zero stencil values afterwards.
bsalomon@google.com208236d2012-03-12 13:15:33 +0000130 *
131 * @param path the path to draw.
sugoi@google.com12b4e272012-12-06 20:13:11 +0000132 * @param stroke the stroke information (width, join, cap)
bsalomon@google.com208236d2012-03-12 13:15:33 +0000133 * @param target target that the path will be rendered to
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000134 */
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000135 void stencilPath(const SkPath& path, const SkStrokeRec& stroke, GrDrawTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000136 SkASSERT(!path.isEmpty());
137 SkASSERT(kNoSupport_StencilSupport != this->getStencilSupport(path, stroke, target));
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000138 this->onStencilPath(path, stroke, target);
139 }
140
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000141 // Helper for determining if we can treat a thin stroke as a hairline w/ coverage.
142 // If we can, we draw lots faster (raster device does this same test).
143 static bool IsStrokeHairlineOrEquivalent(const SkStrokeRec& stroke, const SkMatrix& matrix,
144 SkScalar* outCoverage) {
145 if (stroke.isHairlineStyle()) {
146 if (NULL != outCoverage) {
147 *outCoverage = SK_Scalar1;
148 }
149 return true;
150 }
151 return stroke.getStyle() == SkStrokeRec::kStroke_Style &&
152 SkDrawTreatAAStrokeAsHairline(stroke.getWidth(), matrix, outCoverage);
153 }
154
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000155protected:
156 /**
157 * Subclass overrides if it has any limitations of stenciling support.
158 */
159 virtual StencilSupport onGetStencilSupport(const SkPath&,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000160 const SkStrokeRec&,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000161 const GrDrawTarget*) const {
162 return kNoRestriction_StencilSupport;
163 }
164
165 /**
166 * Subclass implementation of drawPath()
bsalomon@google.com208236d2012-03-12 13:15:33 +0000167 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000168 virtual bool onDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000169 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000170 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000171 bool antiAlias) = 0;
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000172
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000173 /**
174 * Subclass implementation of stencilPath(). Subclass must override iff it ever returns
175 * kStencilOnly in onGetStencilSupport().
176 */
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000177 virtual void onStencilPath(const SkPath& path, const SkStrokeRec& stroke, GrDrawTarget* target) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000178 GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kPreserve_ASRInit);
179 GrDrawState* drawState = target->drawState();
180 GR_STATIC_CONST_SAME_STENCIL(kIncrementStencil,
181 kReplace_StencilOp,
182 kReplace_StencilOp,
183 kAlways_StencilFunc,
184 0xffff,
185 0xffff,
186 0xffff);
187 drawState->setStencil(kIncrementStencil);
188 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
189 this->drawPath(path, stroke, target, false);
190 }
191
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000192 // Helper for getting the device bounds of a path. Inverse filled paths will have bounds set
193 // by devSize. Non-inverse path bounds will not necessarily be clipped to devSize.
194 static void GetPathDevBounds(const SkPath& path,
195 int devW,
196 int devH,
197 const SkMatrix& matrix,
198 SkRect* bounds);
199
200 // Helper version that gets the dev width and height from a GrSurface.
201 static void GetPathDevBounds(const SkPath& path,
202 const GrSurface* device,
203 const SkMatrix& matrix,
204 SkRect* bounds) {
205 GetPathDevBounds(path, device->width(), device->height(), matrix, bounds);
206 }
207
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000208private:
209
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000210 typedef SkRefCnt INHERITED;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000211};
212
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000213#endif