blob: c2d30c0bd58c2afddda51e64219c65240dec7a33 [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:
30 /**
31 * Draws a path into the draw target. The target will already have its draw
32 * state configured for the draw.
33 * @param target the target to draw into.
34 * @param stages indicates which stages the are already
35 * in use. All enabled stages expect positions
36 * as texture coordinates. The path renderer
bsalomon@google.comd302f142011-03-03 13:54:13 +000037 * use the remaining stages for its path
bsalomon@google.comffca4002011-02-22 20:34:01 +000038 * filling algorithm.
39 * @param path the path to draw.
40 * @param fill the fill rule to apply.
41 * @param translate optional additional translation to apply to
bsalomon@google.comd302f142011-03-03 13:54:13 +000042 * the path. NULL means (0,0).
bsalomon@google.comffca4002011-02-22 20:34:01 +000043 */
44 virtual void drawPath(GrDrawTarget* target,
45 GrDrawTarget::StageBitfield stages,
46 GrPathIter* path,
47 GrPathFill fill,
48 const GrPoint* translate) = 0;
bsalomon@google.comd302f142011-03-03 13:54:13 +000049
50 void drawPath(GrDrawTarget* target,
51 GrDrawTarget::StageBitfield stages,
52 const GrPath& path,
53 GrPathFill fill,
54 const GrPoint* translate) {
55 GrPath::Iter iter(path);
56 this->drawPath(target, stages, &iter, fill, translate);
57 }
58
59 /**
60 * For complex clips Gr uses the stencil buffer. The path renderer must be
61 * able to render paths into the stencil buffer. However, the path renderer
62 * itself may require the stencil buffer to resolve the path fill rule. This
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000063 * function queries whether the path render needs its own stencil
bsalomon@google.comd302f142011-03-03 13:54:13 +000064 * pass. If this returns false then drawPath() should not modify the
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000065 * the target's stencil settings but use those already set on target.
66 *
67 * @param target target that the path will be rendered to
68 * @param path the path that will be drawn
69 * @param fill the fill rule that will be used
bsalomon@google.comd302f142011-03-03 13:54:13 +000070 *
71 * @return false if this path renderer can generate interior-only fragments
72 * without changing the stencil settings on the target. If it
73 * returns true the drawPathToStencil will be used when rendering
74 * clips.
75 */
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000076 virtual bool requiresStencilPass(const GrDrawTarget* target,
reed@google.com480ab7d2011-03-08 15:36:57 +000077 GrPathIter* path,
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000078 GrPathFill fill) const { return false; }
reed@google.com480ab7d2011-03-08 15:36:57 +000079
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000080 bool requiresStencilPass(const GrDrawTarget* target,
reed@google.com480ab7d2011-03-08 15:36:57 +000081 const GrPath& path,
82 GrPathFill fill) const {
bsalomon@google.comd302f142011-03-03 13:54:13 +000083 GrPath::Iter iter(path);
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000084 return requiresStencilPass(target, &iter, fill);
bsalomon@google.comd302f142011-03-03 13:54:13 +000085 }
86
87 /**
reed@google.com480ab7d2011-03-08 15:36:57 +000088 * Draws a path to the stencil buffer. Assume the writable stencil bits
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000089 * are already initialized to zero. Fill will always be either
90 * kWinding_PathFill or kEvenOdd_PathFill.
bsalomon@google.comd302f142011-03-03 13:54:13 +000091 *
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000092 * Only called if requiresStencilPass returns true for the same combo of
93 * target, path, and fill (or inverse of the fill).
94 *
95 * The default implementation assumes the path filling algorithm doesn't
96 * require a separate stencil pass and so crashes.
97 *
bsalomon@google.comd302f142011-03-03 13:54:13 +000098 *
99 * @param target the target to draw into.
100 * @param path the path to draw.
101 * @param fill the fill rule to apply.
102 * @param translate optional additional translation to apply to
103 * the path. NULL means (0,0).
104 */
105 virtual void drawPathToStencil(GrDrawTarget* target,
106 GrPathIter* path,
107 GrPathFill fill,
108 const GrPoint* translate) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000109 GrCrash("Unexpected call to drawPathToStencil.");
bsalomon@google.comd302f142011-03-03 13:54:13 +0000110 }
111
112 void drawPathToStencil(GrDrawTarget* target,
113 const GrPath& path,
114 GrPathFill fill,
115 const GrPoint* translate) {
116 GrPath::Iter iter(path);
117 this->drawPathToStencil(target, &iter, fill, translate);
118 }
bsalomon@google.comffca4002011-02-22 20:34:01 +0000119};
120
reed@google.com480ab7d2011-03-08 15:36:57 +0000121/**
122 * Subclass that renders the path using the stencil buffer to resolve fill
123 * rules (e.g. winding, even-odd)
124 */
bsalomon@google.comffca4002011-02-22 20:34:01 +0000125class GrDefaultPathRenderer : public GrPathRenderer {
126public:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000127 GrDefaultPathRenderer(bool separateStencilSupport,
128 bool stencilWrapOpsSupport);
bsalomon@google.comffca4002011-02-22 20:34:01 +0000129
130 virtual void drawPath(GrDrawTarget* target,
131 GrDrawTarget::StageBitfield stages,
132 GrPathIter* path,
133 GrPathFill fill,
134 const GrPoint* translate);
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000135 virtual bool requiresStencilPass(const GrDrawTarget* target,
reed@google.com480ab7d2011-03-08 15:36:57 +0000136 GrPathIter* path,
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000137 GrPathFill fill) const;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000138 virtual void drawPathToStencil(GrDrawTarget* target,
139 GrPathIter* path,
140 GrPathFill fill,
141 const GrPoint* translate);
bsalomon@google.comffca4002011-02-22 20:34:01 +0000142private:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000143
144 void drawPathHelper(GrDrawTarget* target,
145 GrDrawTarget::StageBitfield stages,
146 GrPathIter* path,
147 GrPathFill fill,
148 const GrPoint* translate,
149 bool stencilOnly);
150
151 bool fSeparateStencil;
152 bool fStencilWrapOps;
bsalomon@google.comffca4002011-02-22 20:34:01 +0000153};
154
reed@google.com480ab7d2011-03-08 15:36:57 +0000155#endif