blob: 5cbaab1693a173b4a85e2a7578807a08f4797f1f [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 *
rmistry@google.comd6176b02012-08-23 18:14:13 +000025 * Derived classes can use stages GrPaint::kTotalStages through
robertphillips@google.combf5cad42012-05-10 12:40:40 +000026 * 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.
rmistry@google.comd6176b02012-08-23 18:14:13 +000078 * Returning false allows the caller to fallback to another path renderer
bsalomon@google.com208236d2012-03-12 13:15:33 +000079 * 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
rmistry@google.comd6176b02012-08-23 18:14:13 +000095 * false then the target may be setup for stencil rendering (since the
bsalomon@google.comee435122011-07-01 14:57:55 +000096 * 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
bsalomon@google.com208236d2012-03-12 13:15:33 +0000103 * @param antiAlias true if anti-aliasing is required.
bsalomon@google.comee435122011-07-01 14:57:55 +0000104 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000105 virtual bool drawPath(const SkPath& path,
106 GrPathFill fill,
107 const GrVec* translate,
108 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000109 bool antiAlias) {
110 GrAssert(this->canDrawPath(path, fill, target, antiAlias));
bsalomon@google.come3d32162012-07-20 13:37:06 +0000111 return this->onDrawPath(path, fill, translate, target, antiAlias);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000112 }
bsalomon@google.comee435122011-07-01 14:57:55 +0000113
114 /**
115 * Draws the path to the stencil buffer. Assume the writable stencil bits
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000116 * are already initialized to zero. Fill will always be either
bsalomon@google.com47059542012-06-06 20:51:20 +0000117 * kWinding_GrPathFill or kEvenOdd_GrPathFill.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000118 *
119 * Only called if requiresStencilPass returns true for the same combo of
120 * target, path, and fill. Never called with an inverse fill.
121 *
122 * The default implementation assumes the path filling algorithm doesn't
123 * require a separate stencil pass and so crashes.
124 *
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000125 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000126 virtual void drawPathToStencil(const SkPath& path,
127 GrPathFill fill,
128 GrDrawTarget* target) {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000129 GrCrash("Unexpected call to drawPathToStencil.");
130 }
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000131
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000132protected:
bsalomon@google.com208236d2012-03-12 13:15:33 +0000133 /**
134 * Draws the path into the draw target.
135 *
136 * @param path the path to draw.
137 * @param fill the path filling rule to use.
138 * @param translate optional additional translation applied to
139 * the path
140 * @param target target that the path will be rendered to
bsalomon@google.com208236d2012-03-12 13:15:33 +0000141 * @param antiAlias whether antialiasing is enabled or not.
142 */
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000143 virtual bool onDrawPath(const SkPath& path,
144 GrPathFill fill,
145 const GrVec* translate,
146 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000147 bool antiAlias) = 0;
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000148
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000149private:
150
151 typedef GrRefCnt INHERITED;
152};
153
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000154#endif
bsalomon@google.comee435122011-07-01 14:57:55 +0000155