blob: 25bb74877bf07c5c4c7a9b55d848546b244c693e [file] [log] [blame]
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001/*
2 Copyright 2011 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17#ifndef GrPathRenderer_DEFINED
18#define GrPathRenderer_DEFINED
19
20#include "GrDrawTarget.h"
21
22class GrPathIter;
23struct GrPoint;
24
25/**
26 * Base class for drawing paths into a GrDrawTarget.
27 */
bsalomon@google.com67dc5482011-04-04 18:45:32 +000028class GR_API GrPathRenderer : public GrRefCnt {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000029public:
30 /**
31 * Returns true if this path renderer is able to render the path.
32 * Returning false allows the caller to fallback to another path renderer.
33 *
34 * @param target The target to draw into
35 * @param path The path to draw
36 * @param fill The fill rule to use
37 *
38 * @return true if the path can be drawn by this object, false otherwise.
39 */
40 virtual bool canDrawPath(const GrDrawTarget* target,
41 GrPathIter* path,
42 GrPathFill fill) const = 0;
43
44 /**
45 * Draws a path into the draw target. The target will already have its draw
46 * state configured for the draw.
47 * @param target the target to draw into.
48 * @param stages indicates which stages the are already
49 * in use. All enabled stages expect positions
50 * as texture coordinates. The path renderer
51 * use the remaining stages for its path
52 * filling algorithm.
53 * @param path the path to draw.
54 * @param fill the fill rule to apply.
55 * @param translate optional additional translation to apply to
56 * the path. NULL means (0,0).
57 */
58 virtual void drawPath(GrDrawTarget* target,
59 GrDrawTarget::StageBitfield stages,
60 GrPathIter* path,
61 GrPathFill fill,
62 const GrPoint* translate) = 0;
63
64 /**
65 * For complex clips Gr uses the stencil buffer. The path renderer must be
66 * able to render paths into the stencil buffer. However, the path renderer
67 * itself may require the stencil buffer to resolve the path fill rule. This
68 * function queries whether the path render needs its own stencil
69 * pass. If this returns false then drawPath() should not modify the
70 * the target's stencil settings but use those already set on target.
71 *
72 * @param target target that the path will be rendered to
73 * @param path the path that will be drawn
74 * @param fill the fill rule that will be used, will never be an inverse
75 * rule.
76 *
77 * @return false if this path renderer can generate interior-only fragments
78 * without changing the stencil settings on the target. If it
79 * returns true the drawPathToStencil will be used when rendering
80 * clips.
81 */
82 virtual bool requiresStencilPass(const GrDrawTarget* target,
83 GrPathIter* path,
84 GrPathFill fill) const { return false; }
85
86 /**
87 * Draws a path to the stencil buffer. Assume the writable stencil bits
88 * are already initialized to zero. Fill will always be either
89 * kWinding_PathFill or kEvenOdd_PathFill.
90 *
91 * Only called if requiresStencilPass returns true for the same combo of
92 * target, path, and fill. Never called with an inverse fill.
93 *
94 * The default implementation assumes the path filling algorithm doesn't
95 * require a separate stencil pass and so crashes.
96 *
97 *
98 * @param target the target to draw into.
99 * @param path the path to draw.
100 * @param fill the fill rule to apply.
101 * @param translate optional additional translation to apply to
102 * the path. NULL means (0,0).
103 */
104 virtual void drawPathToStencil(GrDrawTarget* target,
105 GrPathIter* path,
106 GrPathFill fill,
107 const GrPoint* translate) {
108 GrCrash("Unexpected call to drawPathToStencil.");
109 }
110
111 /**
112 * This is called to install a custom path renderer in every GrContext at
113 * create time. The default implementation in GrCreatePathRenderer_none.cpp
114 * returns NULL. Link against another implementation to install your own.
115 */
116 static GrPathRenderer* CreatePathRenderer();
117
118private:
119
120 typedef GrRefCnt INHERITED;
121};
122
123/**
124 * Subclass that renders the path using the stencil buffer to resolve fill
125 * rules (e.g. winding, even-odd)
126 */
bsalomon@google.com67dc5482011-04-04 18:45:32 +0000127class GR_API GrDefaultPathRenderer : public GrPathRenderer {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000128public:
129 GrDefaultPathRenderer(bool separateStencilSupport,
130 bool stencilWrapOpsSupport);
131
132 virtual bool canDrawPath(const GrDrawTarget* target,
133 GrPathIter* path,
134 GrPathFill fill) const { return true; }
135
136 virtual void drawPath(GrDrawTarget* target,
137 GrDrawTarget::StageBitfield stages,
138 GrPathIter* path,
139 GrPathFill fill,
140 const GrPoint* translate);
141 virtual bool requiresStencilPass(const GrDrawTarget* target,
142 GrPathIter* path,
143 GrPathFill fill) const;
144 virtual void drawPathToStencil(GrDrawTarget* target,
145 GrPathIter* path,
146 GrPathFill fill,
147 const GrPoint* translate);
148private:
149
150 void drawPathHelper(GrDrawTarget* target,
151 GrDrawTarget::StageBitfield stages,
152 GrPathIter* path,
153 GrPathFill fill,
154 const GrPoint* translate,
155 bool stencilOnly);
156
157 bool fSeparateStencil;
158 bool fStencilWrapOps;
159
160 typedef GrPathRenderer INHERITED;
161};
162
163#endif