blob: 9d525663f165a4dfab775ae899e2f43dd8f6b34d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +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
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000010#ifndef GrPathRenderer_DEFINED
11#define GrPathRenderer_DEFINED
12
13#include "GrDrawTarget.h"
bsalomon@google.com30085192011-08-19 15:42:31 +000014#include "GrPathRendererChain.h"
bsalomon@google.com45a15f52012-12-10 19:10:17 +000015#include "GrStencil.h"
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000016
bsalomon@google.com45a15f52012-12-10 19:10:17 +000017#include "SkStroke.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 */
bsalomon@google.com67dc5482011-04-04 18:45:32 +000031class GR_API GrPathRenderer : public GrRefCnt {
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.com12b4e272012-12-06 20:13:11 +000085 const SkStroke& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +000086 const GrDrawTarget* target) const {
bsalomon@google.com45a15f52012-12-10 19:10:17 +000087 GrAssert(!path.isInverseFillType());
88 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.com12b4e272012-12-06 20:13:11 +0000104 const SkStroke& stroke,
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,
117 const SkStroke& stroke,
118 GrDrawTarget* target,
119 bool antiAlias) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000120 GrAssert(this->canDrawPath(path, stroke, target, antiAlias));
121 return this->onDrawPath(path, stroke, target, antiAlias);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000122 }
bsalomon@google.comee435122011-07-01 14:57:55 +0000123
124 /**
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000125 * Draws the path to the stencil buffer. Assume the writable stencil bits are already
126 * initialized to zero. The pixels inside the path will have non-zero stencil values afterwards.
bsalomon@google.com208236d2012-03-12 13:15:33 +0000127 *
128 * @param path the path to draw.
sugoi@google.com12b4e272012-12-06 20:13:11 +0000129 * @param stroke the stroke information (width, join, cap)
bsalomon@google.com208236d2012-03-12 13:15:33 +0000130 * @param target target that the path will be rendered to
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000131 */
132 void stencilPath(const SkPath& path, const SkStroke& stroke, GrDrawTarget* target) {
133 GrAssert(kNoSupport_StencilSupport != this->getStencilSupport(path, stroke, target));
134 this->onStencilPath(path, stroke, target);
135 }
136
137protected:
138 /**
139 * Subclass overrides if it has any limitations of stenciling support.
140 */
141 virtual StencilSupport onGetStencilSupport(const SkPath&,
142 const SkStroke&,
143 const GrDrawTarget*) const {
144 return kNoRestriction_StencilSupport;
145 }
146
147 /**
148 * Subclass implementation of drawPath()
bsalomon@google.com208236d2012-03-12 13:15:33 +0000149 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000150 virtual bool onDrawPath(const SkPath& path,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000151 const SkStroke& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000152 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000153 bool antiAlias) = 0;
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000154
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000155 /**
156 * Subclass implementation of stencilPath(). Subclass must override iff it ever returns
157 * kStencilOnly in onGetStencilSupport().
158 */
159 virtual void onStencilPath(const SkPath& path, const SkStroke& stroke, GrDrawTarget* target) {
160 GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kPreserve_ASRInit);
161 GrDrawState* drawState = target->drawState();
162 GR_STATIC_CONST_SAME_STENCIL(kIncrementStencil,
163 kReplace_StencilOp,
164 kReplace_StencilOp,
165 kAlways_StencilFunc,
166 0xffff,
167 0xffff,
168 0xffff);
169 drawState->setStencil(kIncrementStencil);
170 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
171 this->drawPath(path, stroke, target, false);
172 }
173
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000174private:
175
176 typedef GrRefCnt INHERITED;
177};
178
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000179#endif
bsalomon@google.comee435122011-07-01 14:57:55 +0000180