blob: 21cab6bb1aa1b0d64f074427e4c7706c56b7ef43 [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 /**
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000112 * @return true if the path renderer can perform anti-aliasing (aside from
113 * having FSAA enabled for a render target)
114 */
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000115 virtual bool supportsAA(GrDrawTarget* target,
116 GrPathIter* path,
117 GrPathFill fill) { return false; }
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000118
119 /**
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000120 * This is called to install a custom path renderer in every GrContext at
121 * create time. The default implementation in GrCreatePathRenderer_none.cpp
122 * returns NULL. Link against another implementation to install your own.
123 */
124 static GrPathRenderer* CreatePathRenderer();
125
126private:
127
128 typedef GrRefCnt INHERITED;
129};
130
131/**
132 * Subclass that renders the path using the stencil buffer to resolve fill
133 * rules (e.g. winding, even-odd)
134 */
bsalomon@google.com67dc5482011-04-04 18:45:32 +0000135class GR_API GrDefaultPathRenderer : public GrPathRenderer {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000136public:
137 GrDefaultPathRenderer(bool separateStencilSupport,
138 bool stencilWrapOpsSupport);
139
140 virtual bool canDrawPath(const GrDrawTarget* target,
141 GrPathIter* path,
142 GrPathFill fill) const { return true; }
143
144 virtual void drawPath(GrDrawTarget* target,
145 GrDrawTarget::StageBitfield stages,
146 GrPathIter* path,
147 GrPathFill fill,
148 const GrPoint* translate);
149 virtual bool requiresStencilPass(const GrDrawTarget* target,
150 GrPathIter* path,
151 GrPathFill fill) const;
152 virtual void drawPathToStencil(GrDrawTarget* target,
153 GrPathIter* path,
154 GrPathFill fill,
155 const GrPoint* translate);
156private:
157
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000158 void onDrawPath(GrDrawTarget* target,
159 GrDrawTarget::StageBitfield stages,
160 GrPathIter* path,
161 GrPathFill fill,
162 const GrPoint* translate,
163 bool stencilOnly);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000164
165 bool fSeparateStencil;
166 bool fStencilWrapOps;
167
168 typedef GrPathRenderer INHERITED;
169};
170
171#endif