blob: 1ebad4f530fe82b4dc9c6b5a0bc1578198d2ca7d [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
reed@google.com07f3ee12011-05-16 17:21:57 +000022class SkPath;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000023struct 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 */
reed@google.com07f3ee12011-05-16 17:21:57 +000040 virtual bool canDrawPath(const GrDrawTarget* target, const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000041 GrPathFill fill) const = 0;
42
43 /**
44 * Draws a path into the draw target. The target will already have its draw
45 * state configured for the draw.
46 * @param target the target to draw into.
47 * @param stages indicates which stages the are already
48 * in use. All enabled stages expect positions
49 * as texture coordinates. The path renderer
50 * use the remaining stages for its path
51 * filling algorithm.
52 * @param path the path to draw.
53 * @param fill the fill rule to apply.
54 * @param translate optional additional translation to apply to
55 * the path. NULL means (0,0).
56 */
57 virtual void drawPath(GrDrawTarget* target,
58 GrDrawTarget::StageBitfield stages,
reed@google.com07f3ee12011-05-16 17:21:57 +000059 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000060 GrPathFill fill,
61 const GrPoint* translate) = 0;
62
63 /**
64 * For complex clips Gr uses the stencil buffer. The path renderer must be
65 * able to render paths into the stencil buffer. However, the path renderer
66 * itself may require the stencil buffer to resolve the path fill rule. This
67 * function queries whether the path render needs its own stencil
68 * pass. If this returns false then drawPath() should not modify the
69 * the target's stencil settings but use those already set on target.
70 *
71 * @param target target that the path will be rendered to
72 * @param path the path that will be drawn
73 * @param fill the fill rule that will be used, will never be an inverse
74 * rule.
75 *
76 * @return false if this path renderer can generate interior-only fragments
77 * without changing the stencil settings on the target. If it
78 * returns true the drawPathToStencil will be used when rendering
79 * clips.
80 */
81 virtual bool requiresStencilPass(const GrDrawTarget* target,
reed@google.com07f3ee12011-05-16 17:21:57 +000082 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000083 GrPathFill fill) const { return false; }
84
85 /**
86 * Draws a path to the stencil buffer. Assume the writable stencil bits
87 * are already initialized to zero. Fill will always be either
88 * kWinding_PathFill or kEvenOdd_PathFill.
89 *
90 * Only called if requiresStencilPass returns true for the same combo of
91 * target, path, and fill. Never called with an inverse fill.
92 *
93 * The default implementation assumes the path filling algorithm doesn't
94 * require a separate stencil pass and so crashes.
95 *
96 *
97 * @param target the target to draw into.
98 * @param path the path to draw.
99 * @param fill the fill rule to apply.
100 * @param translate optional additional translation to apply to
101 * the path. NULL means (0,0).
102 */
103 virtual void drawPathToStencil(GrDrawTarget* target,
reed@google.com07f3ee12011-05-16 17:21:57 +0000104 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000105 GrPathFill fill,
106 const GrPoint* translate) {
107 GrCrash("Unexpected call to drawPathToStencil.");
108 }
109
110 /**
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000111 * @return true if the path renderer can perform anti-aliasing (aside from
112 * having FSAA enabled for a render target)
113 */
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000114 virtual bool supportsAA(GrDrawTarget* target,
reed@google.com07f3ee12011-05-16 17:21:57 +0000115 const SkPath& path,
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000116 GrPathFill fill) { return false; }
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000117
118 /**
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000119 * This is called to install a custom path renderer in every GrContext at
120 * create time. The default implementation in GrCreatePathRenderer_none.cpp
121 * returns NULL. Link against another implementation to install your own.
122 */
123 static GrPathRenderer* CreatePathRenderer();
124
125private:
126
127 typedef GrRefCnt INHERITED;
128};
129
130/**
131 * Subclass that renders the path using the stencil buffer to resolve fill
132 * rules (e.g. winding, even-odd)
133 */
bsalomon@google.com67dc5482011-04-04 18:45:32 +0000134class GR_API GrDefaultPathRenderer : public GrPathRenderer {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000135public:
136 GrDefaultPathRenderer(bool separateStencilSupport,
137 bool stencilWrapOpsSupport);
138
139 virtual bool canDrawPath(const GrDrawTarget* target,
reed@google.com07f3ee12011-05-16 17:21:57 +0000140 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000141 GrPathFill fill) const { return true; }
142
143 virtual void drawPath(GrDrawTarget* target,
144 GrDrawTarget::StageBitfield stages,
reed@google.com07f3ee12011-05-16 17:21:57 +0000145 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000146 GrPathFill fill,
147 const GrPoint* translate);
148 virtual bool requiresStencilPass(const GrDrawTarget* target,
reed@google.com07f3ee12011-05-16 17:21:57 +0000149 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000150 GrPathFill fill) const;
151 virtual void drawPathToStencil(GrDrawTarget* target,
reed@google.com07f3ee12011-05-16 17:21:57 +0000152 const SkPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000153 GrPathFill fill,
154 const GrPoint* translate);
155private:
156
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000157 void onDrawPath(GrDrawTarget* target,
158 GrDrawTarget::StageBitfield stages,
reed@google.com07f3ee12011-05-16 17:21:57 +0000159 const SkPath& path,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000160 GrPathFill fill,
161 const GrPoint* translate,
162 bool stencilOnly);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000163
164 bool fSeparateStencil;
165 bool fStencilWrapOps;
166
167 typedef GrPathRenderer INHERITED;
168};
169
170#endif