blob: 32985be4abe64b586dbabdc7b534a1122b3a0088 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000010#ifndef GrPathRenderer_DEFINED
11#define GrPathRenderer_DEFINED
12
13#include "GrDrawTarget.h"
bsalomon@google.com30085192011-08-19 15:42:31 +000014#include "GrPathRendererChain.h"
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000015
bsalomon@google.com49313f62011-09-14 13:54:05 +000016#include "SkTArray.h"
17
reed@google.com07f3ee12011-05-16 17:21:57 +000018class SkPath;
bsalomon@google.com30085192011-08-19 15:42:31 +000019
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000020struct GrPoint;
21
22/**
23 * Base class for drawing paths into a GrDrawTarget.
robertphillips@google.combf5cad42012-05-10 12:40:40 +000024 *
25 * Derived classes can use stages GrPaint::kTotalStages through
26 * GrDrawState::kNumStages-1. The stages before GrPaint::kTotalStages
27 * are reserved for setting up the draw (i.e., textures and filter masks).
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000028 */
bsalomon@google.com67dc5482011-04-04 18:45:32 +000029class GR_API GrPathRenderer : public GrRefCnt {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000030public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000031 SK_DECLARE_INST_COUNT(GrPathRenderer)
bsalomon@google.com30085192011-08-19 15:42:31 +000032
33 /**
34 * This is called to install custom path renderers in every GrContext at
35 * create time. The default implementation in GrCreatePathRenderer_none.cpp
36 * does not add any additional renderers. Link against another
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000037 * implementation to install your own. The first added is the most preferred
38 * path renderer, second is second most preferred, etc.
bsalomon@google.com30085192011-08-19 15:42:31 +000039 *
40 * @param context the context that will use the path renderer
41 * @param flags flags indicating how path renderers will be used
42 * @param prChain the chain to add path renderers to.
43 */
44 static void AddPathRenderers(GrContext* context,
45 GrPathRendererChain::UsageFlags flags,
46 GrPathRendererChain* prChain);
47
48
bsalomon@google.comc2099d22012-03-02 21:26:50 +000049 GrPathRenderer();
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000050
51 /**
52 * For complex clips Gr uses the stencil buffer. The path renderer must be
53 * able to render paths into the stencil buffer. However, the path renderer
bsalomon@google.comee435122011-07-01 14:57:55 +000054 * itself may require the stencil buffer to resolve the path fill rule.
55 * This function queries whether the path render needs its own stencil
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000056 * pass. If this returns false then drawPath() should not modify the
bsalomon@google.comee435122011-07-01 14:57:55 +000057 * the target's stencil settings but use those already set on target. The
58 * target is passed as a param in case the answer depends upon draw state.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000059 *
60 * @param target target that the path will be rendered to
61 * @param path the path that will be drawn
62 * @param fill the fill rule that will be used, will never be an inverse
63 * rule.
64 *
65 * @return false if this path renderer can generate interior-only fragments
66 * without changing the stencil settings on the target. If it
67 * returns true the drawPathToStencil will be used when rendering
68 * clips.
69 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +000070 virtual bool requiresStencilPass(const SkPath& path,
71 GrPathFill fill,
72 const GrDrawTarget* target) const {
73 return false;
74 }
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000075
bsalomon@google.com208236d2012-03-12 13:15:33 +000076 /**
77 * Returns true if this path renderer is able to render the path.
78 * Returning false allows the caller to fallback to another path renderer
79 * This function is called when searching for a path renderer capable of
80 * rendering a path.
81 *
82 * @param path The path to draw
83 * @param fill The fill rule to use
84 * @param target The target that the path will be rendered to
85 * @param antiAlias True if anti-aliasing is required.
86 *
87 * @return true if the path can be drawn by this object, false otherwise.
88 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +000089 virtual bool canDrawPath(const SkPath& path,
90 GrPathFill fill,
91 const GrDrawTarget* target,
92 bool antiAlias) const = 0;
bsalomon@google.comee435122011-07-01 14:57:55 +000093 /**
94 * Draws the path into the draw target. If requiresStencilBuffer returned
95 * false then the target may be setup for stencil rendering (since the
96 * path renderer didn't claim that it needs to use the stencil internally).
97 *
bsalomon@google.com208236d2012-03-12 13:15:33 +000098 * @param path the path to draw.
99 * @param fill the path filling rule to use.
100 * @param translate optional additional translation applied to
101 * the path (can be NULL)
102 * @param target target that the path will be rendered to
103 * @param stageMask bitfield that indicates which stages are
bsalomon@google.comee435122011-07-01 14:57:55 +0000104 * in use. All enabled stages expect positions
105 * as texture coordinates. The path renderer
bsalomon@google.com208236d2012-03-12 13:15:33 +0000106 * can use the remaining stages for its path
bsalomon@google.comee435122011-07-01 14:57:55 +0000107 * filling algorithm.
bsalomon@google.com208236d2012-03-12 13:15:33 +0000108 * @param antiAlias true if anti-aliasing is required.
bsalomon@google.comee435122011-07-01 14:57:55 +0000109 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000110 virtual bool drawPath(const SkPath& path,
111 GrPathFill fill,
112 const GrVec* translate,
113 GrDrawTarget* target,
114 GrDrawState::StageMask stageMask,
115 bool antiAlias) {
116 GrAssert(this->canDrawPath(path, fill, target, antiAlias));
117 return this->onDrawPath(path, fill, translate,
118 target, stageMask, antiAlias);
119 }
bsalomon@google.comee435122011-07-01 14:57:55 +0000120
121 /**
122 * Draws the path to the stencil buffer. Assume the writable stencil bits
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000123 * are already initialized to zero. Fill will always be either
bsalomon@google.com47059542012-06-06 20:51:20 +0000124 * kWinding_GrPathFill or kEvenOdd_GrPathFill.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000125 *
126 * Only called if requiresStencilPass returns true for the same combo of
127 * target, path, and fill. Never called with an inverse fill.
128 *
129 * The default implementation assumes the path filling algorithm doesn't
130 * require a separate stencil pass and so crashes.
131 *
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000132 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000133 virtual void drawPathToStencil(const SkPath& path,
134 GrPathFill fill,
135 GrDrawTarget* target) {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000136 GrCrash("Unexpected call to drawPathToStencil.");
137 }
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000138
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000139protected:
bsalomon@google.com208236d2012-03-12 13:15:33 +0000140 /**
141 * Draws the path into the draw target.
142 *
143 * @param path the path to draw.
144 * @param fill the path filling rule to use.
145 * @param translate optional additional translation applied to
146 * the path
147 * @param target target that the path will be rendered to
148 * @param stageMask bitfield that indicates which stages are
149 * in use. All enabled stages expect positions
150 * as texture coordinates. The path renderer
151 * use the remaining stages for its path
152 * filling algorithm.
153 * @param antiAlias whether antialiasing is enabled or not.
154 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000155 virtual bool onDrawPath(const SkPath& path,
156 GrPathFill fill,
157 const GrVec* translate,
158 GrDrawTarget* target,
159 GrDrawState::StageMask stageMask,
160 bool antiAlias) = 0;
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000161
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000162private:
163
164 typedef GrRefCnt INHERITED;
165};
166
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000167#endif
bsalomon@google.comee435122011-07-01 14:57:55 +0000168