blob: 19e284f3fa2ed62208ab9e0e322a477ffa8471ed [file] [log] [blame]
bsalomon@google.comffca4002011-02-22 20:34:01 +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/**
reed@google.com480ab7d2011-03-08 15:36:57 +000026 * Base class for drawing paths into a GrDrawTarget.
bsalomon@google.comffca4002011-02-22 20:34:01 +000027 */
28class GrPathRenderer {
29public:
djsollen@google.comcd9d69b2011-03-14 20:30:14 +000030 virtual ~GrPathRenderer() { };
31
bsalomon@google.comffca4002011-02-22 20:34:01 +000032 /**
33 * Draws a path into the draw target. The target will already have its draw
34 * state configured for the draw.
35 * @param target the target to draw into.
36 * @param stages indicates which stages the are already
37 * in use. All enabled stages expect positions
38 * as texture coordinates. The path renderer
bsalomon@google.comd302f142011-03-03 13:54:13 +000039 * use the remaining stages for its path
bsalomon@google.comffca4002011-02-22 20:34:01 +000040 * filling algorithm.
41 * @param path the path to draw.
42 * @param fill the fill rule to apply.
43 * @param translate optional additional translation to apply to
bsalomon@google.comd302f142011-03-03 13:54:13 +000044 * the path. NULL means (0,0).
bsalomon@google.comffca4002011-02-22 20:34:01 +000045 */
46 virtual void drawPath(GrDrawTarget* target,
47 GrDrawTarget::StageBitfield stages,
48 GrPathIter* path,
49 GrPathFill fill,
50 const GrPoint* translate) = 0;
bsalomon@google.comd302f142011-03-03 13:54:13 +000051
52 void drawPath(GrDrawTarget* target,
53 GrDrawTarget::StageBitfield stages,
54 const GrPath& path,
55 GrPathFill fill,
56 const GrPoint* translate) {
57 GrPath::Iter iter(path);
58 this->drawPath(target, stages, &iter, fill, translate);
59 }
60
61 /**
62 * For complex clips Gr uses the stencil buffer. The path renderer must be
63 * able to render paths into the stencil buffer. However, the path renderer
64 * itself may require the stencil buffer to resolve the path fill rule. This
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000065 * function queries whether the path render needs its own stencil
bsalomon@google.comd302f142011-03-03 13:54:13 +000066 * pass. If this returns false then drawPath() should not modify the
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000067 * the target's stencil settings but use those already set on target.
68 *
69 * @param target target that the path will be rendered to
70 * @param path the path that will be drawn
71 * @param fill the fill rule that will be used
bsalomon@google.comd302f142011-03-03 13:54:13 +000072 *
73 * @return false if this path renderer can generate interior-only fragments
74 * without changing the stencil settings on the target. If it
75 * returns true the drawPathToStencil will be used when rendering
76 * clips.
77 */
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000078 virtual bool requiresStencilPass(const GrDrawTarget* target,
reed@google.com480ab7d2011-03-08 15:36:57 +000079 GrPathIter* path,
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000080 GrPathFill fill) const { return false; }
reed@google.com480ab7d2011-03-08 15:36:57 +000081
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000082 bool requiresStencilPass(const GrDrawTarget* target,
reed@google.com480ab7d2011-03-08 15:36:57 +000083 const GrPath& path,
84 GrPathFill fill) const {
bsalomon@google.comd302f142011-03-03 13:54:13 +000085 GrPath::Iter iter(path);
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000086 return requiresStencilPass(target, &iter, fill);
bsalomon@google.comd302f142011-03-03 13:54:13 +000087 }
88
89 /**
reed@google.com480ab7d2011-03-08 15:36:57 +000090 * Draws a path to the stencil buffer. Assume the writable stencil bits
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000091 * are already initialized to zero. Fill will always be either
92 * kWinding_PathFill or kEvenOdd_PathFill.
bsalomon@google.comd302f142011-03-03 13:54:13 +000093 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000094 * Only called if requiresStencilPass returns true for the same combo of
95 * target, path, and fill (or inverse of the fill).
96 *
97 * The default implementation assumes the path filling algorithm doesn't
98 * require a separate stencil pass and so crashes.
99 *
bsalomon@google.comd302f142011-03-03 13:54:13 +0000100 *
101 * @param target the target to draw into.
102 * @param path the path to draw.
103 * @param fill the fill rule to apply.
104 * @param translate optional additional translation to apply to
105 * the path. NULL means (0,0).
106 */
107 virtual void drawPathToStencil(GrDrawTarget* target,
108 GrPathIter* path,
109 GrPathFill fill,
110 const GrPoint* translate) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000111 GrCrash("Unexpected call to drawPathToStencil.");
bsalomon@google.comd302f142011-03-03 13:54:13 +0000112 }
113
114 void drawPathToStencil(GrDrawTarget* target,
115 const GrPath& path,
116 GrPathFill fill,
117 const GrPoint* translate) {
118 GrPath::Iter iter(path);
119 this->drawPathToStencil(target, &iter, fill, translate);
120 }
bsalomon@google.comffca4002011-02-22 20:34:01 +0000121};
122
reed@google.com480ab7d2011-03-08 15:36:57 +0000123/**
124 * Subclass that renders the path using the stencil buffer to resolve fill
125 * rules (e.g. winding, even-odd)
126 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000127class GrDefaultPathRenderer : public GrPathRenderer {
128public:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000129 GrDefaultPathRenderer(bool separateStencilSupport,
130 bool stencilWrapOpsSupport);
bsalomon@google.comffca4002011-02-22 20:34:01 +0000131
132 virtual void drawPath(GrDrawTarget* target,
133 GrDrawTarget::StageBitfield stages,
134 GrPathIter* path,
135 GrPathFill fill,
136 const GrPoint* translate);
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000137 virtual bool requiresStencilPass(const GrDrawTarget* target,
reed@google.com480ab7d2011-03-08 15:36:57 +0000138 GrPathIter* path,
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000139 GrPathFill fill) const;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000140 virtual void drawPathToStencil(GrDrawTarget* target,
141 GrPathIter* path,
142 GrPathFill fill,
143 const GrPoint* translate);
bsalomon@google.comffca4002011-02-22 20:34:01 +0000144private:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000145
146 void drawPathHelper(GrDrawTarget* target,
147 GrDrawTarget::StageBitfield stages,
148 GrPathIter* path,
149 GrPathFill fill,
150 const GrPoint* translate,
151 bool stencilOnly);
152
153 bool fSeparateStencil;
154 bool fStencilWrapOps;
bsalomon@google.comffca4002011-02-22 20:34:01 +0000155};
156
reed@google.com480ab7d2011-03-08 15:36:57 +0000157#endif