blob: f86eb9fe54783b502875c7dc048e2041560c64a2 [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
sugoi@google.com5f74cf82012-12-17 21:16:45 +000016#include "SkStrokeRec.h"
bsalomon@google.com49313f62011-09-14 13:54:05 +000017#include "SkTArray.h"
18
reed@google.com07f3ee12011-05-16 17:21:57 +000019class SkPath;
bsalomon@google.com30085192011-08-19 15:42:31 +000020
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000021struct GrPoint;
22
23/**
24 * Base class for drawing paths into a GrDrawTarget.
robertphillips@google.combf5cad42012-05-10 12:40:40 +000025 *
bsalomon@google.com45a15f52012-12-10 19:10:17 +000026 * Derived classes can use stages GrPaint::kTotalStages through GrDrawState::kNumStages-1. The
27 * stages before GrPaint::kTotalStages are reserved for setting up the draw (i.e., textures and
28 * filter masks).
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000029 */
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000030class SK_API GrPathRenderer : public SkRefCnt {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000031public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000032 SK_DECLARE_INST_COUNT(GrPathRenderer)
bsalomon@google.com30085192011-08-19 15:42:31 +000033
34 /**
bsalomon@google.com45a15f52012-12-10 19:10:17 +000035 * This is called to install custom path renderers in every GrContext at create time. The
36 * default implementation in GrCreatePathRenderer_none.cpp does not add any additional
37 * renderers. Link against another implementation to install your own. The first added is the
38 * most preferred path renderer, second is second most preferred, etc.
bsalomon@google.com30085192011-08-19 15:42:31 +000039 *
40 * @param context the context that will use the path renderer
bsalomon@google.com30085192011-08-19 15:42:31 +000041 * @param prChain the chain to add path renderers to.
42 */
bsalomon@google.com45a15f52012-12-10 19:10:17 +000043 static void AddPathRenderers(GrContext* context, GrPathRendererChain* prChain);
bsalomon@google.com30085192011-08-19 15:42:31 +000044
45
bsalomon@google.comc2099d22012-03-02 21:26:50 +000046 GrPathRenderer();
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000047
48 /**
bsalomon@google.com45a15f52012-12-10 19:10:17 +000049 * A caller may wish to use a path renderer to draw a path into the stencil buffer. However,
50 * the path renderer itself may require use of the stencil buffer. Also a path renderer may
51 * use a GrEffect coverage stage that sets coverage to zero to eliminate pixels that are covered
52 * by bounding geometry but outside the path. These exterior pixels would still be rendered into
53 * the stencil.
54 *
55 * A GrPathRenderer can provide three levels of support for stenciling paths:
56 * 1) kNoRestriction: This is the most general. The caller sets up the GrDrawState on the target
57 * and calls drawPath(). The path is rendered exactly as the draw state
58 * indicates including support for simultaneous color and stenciling with
59 * arbitrary stenciling rules. Pixels partially covered by AA paths are
60 * affected by the stencil settings.
61 * 2) kStencilOnly: The path renderer cannot apply arbitrary stencil rules nor shade and stencil
62 * simultaneously. The path renderer does support the stencilPath() function
63 * which performs no color writes and writes a non-zero stencil value to pixels
64 * covered by the path.
65 * 3) kNoSupport: This path renderer cannot be used to stencil the path.
66 */
67 typedef GrPathRendererChain::StencilSupport StencilSupport;
68 static const StencilSupport kNoSupport_StencilSupport =
69 GrPathRendererChain::kNoSupport_StencilSupport;
70 static const StencilSupport kStencilOnly_StencilSupport =
71 GrPathRendererChain::kStencilOnly_StencilSupport;
72 static const StencilSupport kNoRestriction_StencilSupport =
73 GrPathRendererChain::kNoRestriction_StencilSupport;
74
75 /**
76 * This function is to get the stencil support for a particular path. The path's fill must
77 * not be an inverse type.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000078 *
sugoi@google.com12b4e272012-12-06 20:13:11 +000079 * @param target target that the path will be rendered to
80 * @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 */
bsalomon@google.com45a15f52012-12-10 19:10:17 +000083 StencilSupport getStencilSupport(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000084 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +000085 const GrDrawTarget* target) const {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000086 SkASSERT(!path.isInverseFillType());
bsalomon@google.com45a15f52012-12-10 19:10:17 +000087 return this->onGetStencilSupport(path, stroke, target);
bsalomon@google.comc2099d22012-03-02 21:26:50 +000088 }
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000089
bsalomon@google.com208236d2012-03-12 13:15:33 +000090 /**
bsalomon@google.com45a15f52012-12-10 19:10:17 +000091 * Returns true if this path renderer is able to render the path. Returning false allows the
92 * caller to fallback to another path renderer This function is called when searching for a path
93 * renderer capable of rendering a path.
bsalomon@google.com208236d2012-03-12 13:15:33 +000094 *
95 * @param path The path to draw
sugoi@google.com12b4e272012-12-06 20:13:11 +000096 * @param stroke The stroke information (width, join, cap)
bsalomon@google.com208236d2012-03-12 13:15:33 +000097 * @param target The target that the path will be rendered to
98 * @param antiAlias True if anti-aliasing is required.
99 *
100 * @return true if the path can be drawn by this object, false otherwise.
101 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000102 virtual bool canDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000103 const SkStrokeRec& rec,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000104 const GrDrawTarget* target,
105 bool antiAlias) const = 0;
bsalomon@google.comee435122011-07-01 14:57:55 +0000106 /**
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000107 * Draws the path into the draw target. If getStencilSupport() would return kNoRestriction then
108 * the subclass must respect the stencil settings of the target's draw state.
bsalomon@google.comee435122011-07-01 14:57:55 +0000109 *
bsalomon@google.com208236d2012-03-12 13:15:33 +0000110 * @param path the path to draw.
sugoi@google.com12b4e272012-12-06 20:13:11 +0000111 * @param stroke the stroke information (width, join, cap)
bsalomon@google.com208236d2012-03-12 13:15:33 +0000112 * @param target target that the path will be rendered to
bsalomon@google.com208236d2012-03-12 13:15:33 +0000113 * @param antiAlias true if anti-aliasing is required.
bsalomon@google.comee435122011-07-01 14:57:55 +0000114 */
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000115 bool drawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000116 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000117 GrDrawTarget* target,
118 bool antiAlias) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000119 SkASSERT(!path.isEmpty());
120 SkASSERT(this->canDrawPath(path, stroke, target, antiAlias));
121 SkASSERT(target->drawState()->getStencil().isDisabled() ||
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000122 kNoRestriction_StencilSupport == this->getStencilSupport(path, stroke, target));
sugoi@google.com12b4e272012-12-06 20:13:11 +0000123 return this->onDrawPath(path, stroke, target, antiAlias);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000124 }
bsalomon@google.comee435122011-07-01 14:57:55 +0000125
126 /**
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000127 * Draws the path to the stencil buffer. Assume the writable stencil bits are already
128 * initialized to zero. The pixels inside the path will have non-zero stencil values afterwards.
bsalomon@google.com208236d2012-03-12 13:15:33 +0000129 *
130 * @param path the path to draw.
sugoi@google.com12b4e272012-12-06 20:13:11 +0000131 * @param stroke the stroke information (width, join, cap)
bsalomon@google.com208236d2012-03-12 13:15:33 +0000132 * @param target target that the path will be rendered to
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000133 */
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000134 void stencilPath(const SkPath& path, const SkStrokeRec& stroke, GrDrawTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000135 SkASSERT(!path.isEmpty());
136 SkASSERT(kNoSupport_StencilSupport != this->getStencilSupport(path, stroke, target));
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000137 this->onStencilPath(path, stroke, target);
138 }
139
140protected:
141 /**
142 * Subclass overrides if it has any limitations of stenciling support.
143 */
144 virtual StencilSupport onGetStencilSupport(const SkPath&,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000145 const SkStrokeRec&,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000146 const GrDrawTarget*) const {
147 return kNoRestriction_StencilSupport;
148 }
149
150 /**
151 * Subclass implementation of drawPath()
bsalomon@google.com208236d2012-03-12 13:15:33 +0000152 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000153 virtual bool onDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000154 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000155 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000156 bool antiAlias) = 0;
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000157
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000158 /**
159 * Subclass implementation of stencilPath(). Subclass must override iff it ever returns
160 * kStencilOnly in onGetStencilSupport().
161 */
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000162 virtual void onStencilPath(const SkPath& path, const SkStrokeRec& stroke, GrDrawTarget* target) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000163 GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kPreserve_ASRInit);
164 GrDrawState* drawState = target->drawState();
165 GR_STATIC_CONST_SAME_STENCIL(kIncrementStencil,
166 kReplace_StencilOp,
167 kReplace_StencilOp,
168 kAlways_StencilFunc,
169 0xffff,
170 0xffff,
171 0xffff);
172 drawState->setStencil(kIncrementStencil);
173 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
174 this->drawPath(path, stroke, target, false);
175 }
176
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000177 // Helper for getting the device bounds of a path. Inverse filled paths will have bounds set
178 // by devSize. Non-inverse path bounds will not necessarily be clipped to devSize.
179 static void GetPathDevBounds(const SkPath& path,
180 int devW,
181 int devH,
182 const SkMatrix& matrix,
183 SkRect* bounds);
184
185 // Helper version that gets the dev width and height from a GrSurface.
186 static void GetPathDevBounds(const SkPath& path,
187 const GrSurface* device,
188 const SkMatrix& matrix,
189 SkRect* bounds) {
190 GetPathDevBounds(path, device->width(), device->height(), matrix, bounds);
191 }
192
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000193private:
194
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000195 typedef SkRefCnt INHERITED;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000196};
197
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000198#endif