blob: ff9fdbbc053387fc4c7201938c13971bd4eb5fbe [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:
bsalomon@google.com30085192011-08-19 15:42:31 +000031
32 /**
33 * This is called to install custom path renderers in every GrContext at
34 * create time. The default implementation in GrCreatePathRenderer_none.cpp
35 * does not add any additional renderers. Link against another
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000036 * implementation to install your own. The first added is the most preferred
37 * path renderer, second is second most preferred, etc.
bsalomon@google.com30085192011-08-19 15:42:31 +000038 *
39 * @param context the context that will use the path renderer
40 * @param flags flags indicating how path renderers will be used
41 * @param prChain the chain to add path renderers to.
42 */
43 static void AddPathRenderers(GrContext* context,
44 GrPathRendererChain::UsageFlags flags,
45 GrPathRendererChain* prChain);
46
47
bsalomon@google.comc2099d22012-03-02 21:26:50 +000048 GrPathRenderer();
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000049
50 /**
51 * For complex clips Gr uses the stencil buffer. The path renderer must be
52 * able to render paths into the stencil buffer. However, the path renderer
bsalomon@google.comee435122011-07-01 14:57:55 +000053 * itself may require the stencil buffer to resolve the path fill rule.
54 * This function queries whether the path render needs its own stencil
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000055 * pass. If this returns false then drawPath() should not modify the
bsalomon@google.comee435122011-07-01 14:57:55 +000056 * the target's stencil settings but use those already set on target. The
57 * target is passed as a param in case the answer depends upon draw state.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000058 *
59 * @param target target that the path will be rendered to
60 * @param path the path that will be drawn
61 * @param fill the fill rule that will be used, will never be an inverse
62 * rule.
63 *
64 * @return false if this path renderer can generate interior-only fragments
65 * without changing the stencil settings on the target. If it
66 * returns true the drawPathToStencil will be used when rendering
67 * clips.
68 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +000069 virtual bool requiresStencilPass(const SkPath& path,
70 GrPathFill fill,
71 const GrDrawTarget* target) const {
72 return false;
73 }
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000074
bsalomon@google.com208236d2012-03-12 13:15:33 +000075 /**
76 * Returns true if this path renderer is able to render the path.
77 * Returning false allows the caller to fallback to another path renderer
78 * This function is called when searching for a path renderer capable of
79 * rendering a path.
80 *
81 * @param path The path to draw
82 * @param fill The fill rule to use
83 * @param target The target that the path will be rendered to
84 * @param antiAlias True if anti-aliasing is required.
85 *
86 * @return true if the path can be drawn by this object, false otherwise.
87 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +000088 virtual bool canDrawPath(const SkPath& path,
89 GrPathFill fill,
90 const GrDrawTarget* target,
91 bool antiAlias) const = 0;
bsalomon@google.comee435122011-07-01 14:57:55 +000092 /**
93 * Draws the path into the draw target. If requiresStencilBuffer returned
94 * false then the target may be setup for stencil rendering (since the
95 * path renderer didn't claim that it needs to use the stencil internally).
96 *
bsalomon@google.com208236d2012-03-12 13:15:33 +000097 * @param path the path to draw.
98 * @param fill the path filling rule to use.
99 * @param translate optional additional translation applied to
100 * the path (can be NULL)
101 * @param target target that the path will be rendered to
102 * @param stageMask bitfield that indicates which stages are
bsalomon@google.comee435122011-07-01 14:57:55 +0000103 * in use. All enabled stages expect positions
104 * as texture coordinates. The path renderer
bsalomon@google.com208236d2012-03-12 13:15:33 +0000105 * can use the remaining stages for its path
bsalomon@google.comee435122011-07-01 14:57:55 +0000106 * filling algorithm.
bsalomon@google.com208236d2012-03-12 13:15:33 +0000107 * @param antiAlias true if anti-aliasing is required.
bsalomon@google.comee435122011-07-01 14:57:55 +0000108 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000109 virtual bool drawPath(const SkPath& path,
110 GrPathFill fill,
111 const GrVec* translate,
112 GrDrawTarget* target,
113 GrDrawState::StageMask stageMask,
114 bool antiAlias) {
115 GrAssert(this->canDrawPath(path, fill, target, antiAlias));
116 return this->onDrawPath(path, fill, translate,
117 target, stageMask, antiAlias);
118 }
bsalomon@google.comee435122011-07-01 14:57:55 +0000119
120 /**
121 * Draws the path to the stencil buffer. Assume the writable stencil bits
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000122 * are already initialized to zero. Fill will always be either
123 * kWinding_PathFill or kEvenOdd_PathFill.
124 *
125 * Only called if requiresStencilPass returns true for the same combo of
126 * target, path, and fill. Never called with an inverse fill.
127 *
128 * The default implementation assumes the path filling algorithm doesn't
129 * require a separate stencil pass and so crashes.
130 *
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000131 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000132 virtual void drawPathToStencil(const SkPath& path,
133 GrPathFill fill,
134 GrDrawTarget* target) {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000135 GrCrash("Unexpected call to drawPathToStencil.");
136 }
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000137
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000138protected:
bsalomon@google.com208236d2012-03-12 13:15:33 +0000139 /**
140 * Draws the path into the draw target.
141 *
142 * @param path the path to draw.
143 * @param fill the path filling rule to use.
144 * @param translate optional additional translation applied to
145 * the path
146 * @param target target that the path will be rendered to
147 * @param stageMask bitfield that indicates which stages are
148 * in use. All enabled stages expect positions
149 * as texture coordinates. The path renderer
150 * use the remaining stages for its path
151 * filling algorithm.
152 * @param antiAlias whether antialiasing is enabled or not.
153 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000154 virtual bool onDrawPath(const SkPath& path,
155 GrPathFill fill,
156 const GrVec* translate,
157 GrDrawTarget* target,
158 GrDrawState::StageMask stageMask,
159 bool antiAlias) = 0;
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000160
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000161private:
162
163 typedef GrRefCnt INHERITED;
164};
165
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000166#endif
bsalomon@google.comee435122011-07-01 14:57:55 +0000167