bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef GrPathRenderer_DEFINED |
| 9 | #define GrPathRenderer_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkRefCnt.h" |
| 12 | #include "include/private/GrTypesPriv.h" |
| 13 | #include "include/private/SkTArray.h" |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 14 | |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 15 | class GrCaps; |
| 16 | class GrClip; |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 17 | class GrFixedClip; |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 18 | class GrHardClip; |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 19 | class GrPaint; |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 20 | class GrRecordingContext; |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 21 | class GrRenderTargetContext; |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 22 | class GrRenderTargetProxy; |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 23 | class GrShape; |
| 24 | class GrStyle; |
| 25 | struct GrUserStencilSettings; |
| 26 | struct SkIRect; |
| 27 | class SkMatrix; |
| 28 | class SkPath; |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 31 | * Base class for drawing paths into a GrOpsTask. |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 32 | */ |
Brian Salomon | 57f211b | 2019-08-21 15:21:09 -0400 | [diff] [blame] | 33 | class GrPathRenderer : public SkRefCnt { |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 34 | public: |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 35 | GrPathRenderer(); |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 36 | |
| 37 | /** |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 38 | * A caller may wish to use a path renderer to draw a path into the stencil buffer. However, |
| 39 | * the path renderer itself may require use of the stencil buffer. Also a path renderer may |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 40 | * use a GrProcessor coverage stage that sets coverage to zero to eliminate pixels that are |
| 41 | * covered by bounding geometry but outside the path. These exterior pixels would still be |
| 42 | * rendered into the stencil. |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 43 | * |
| 44 | * A GrPathRenderer can provide three levels of support for stenciling paths: |
Brian Salomon | e5b399e | 2017-07-19 13:50:54 -0400 | [diff] [blame] | 45 | * 1) kNoRestriction: This is the most general. The caller passes a GrPaint and calls drawPath(). |
| 46 | * The path is rendered exactly as the draw state indicates including support |
| 47 | * for simultaneous color and stenciling with arbitrary stenciling rules. |
| 48 | * Pixels partially covered by AA paths are affected by the stencil settings. |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 49 | * 2) kStencilOnly: The path renderer cannot apply arbitrary stencil rules nor shade and stencil |
| 50 | * simultaneously. The path renderer does support the stencilPath() function |
| 51 | * which performs no color writes and writes a non-zero stencil value to pixels |
| 52 | * covered by the path. |
| 53 | * 3) kNoSupport: This path renderer cannot be used to stencil the path. |
| 54 | */ |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 55 | enum StencilSupport { |
| 56 | kNoSupport_StencilSupport, |
| 57 | kStencilOnly_StencilSupport, |
| 58 | kNoRestriction_StencilSupport, |
| 59 | }; |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 60 | |
| 61 | /** |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 62 | * This function is to get the stencil support for a particular path. The path's fill must |
bsalomon | d6f25bf | 2016-05-05 09:26:21 -0700 | [diff] [blame] | 63 | * not be an inverse type. The path will always be filled and not stroked. |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 64 | * |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 65 | * @param shape the shape that will be drawn. Must be simple fill styled and non-inverse |
| 66 | * filled. |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 67 | */ |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 68 | StencilSupport getStencilSupport(const GrShape& shape) const; |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 69 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 70 | enum class CanDrawPath { |
| 71 | kNo, |
| 72 | kAsBackup, // i.e. This renderer is better than SW fallback if no others can draw the path. |
| 73 | kYes |
| 74 | }; |
| 75 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 76 | struct CanDrawPathArgs { |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 77 | SkDEBUGCODE(CanDrawPathArgs() { memset(this, 0, sizeof(*this)); }) // For validation. |
| 78 | |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 79 | const GrCaps* fCaps; |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 80 | const GrRenderTargetProxy* fProxy; |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 81 | const SkIRect* fClipConservativeBounds; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 82 | const SkMatrix* fViewMatrix; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 83 | const GrShape* fShape; |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 84 | GrAAType fAAType; |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 85 | bool fTargetIsWrappedVkSecondaryCB; |
robertphillips | e7d4b2f | 2015-08-13 07:57:10 -0700 | [diff] [blame] | 86 | |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 87 | // This is only used by GrStencilAndCoverPathRenderer |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 88 | bool fHasUserStencilSettings; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 89 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 90 | #ifdef SK_DEBUG |
robertphillips | e7d4b2f | 2015-08-13 07:57:10 -0700 | [diff] [blame] | 91 | void validate() const { |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 92 | SkASSERT(fCaps); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 93 | SkASSERT(fProxy); |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 94 | SkASSERT(fClipConservativeBounds); |
robertphillips | e7d4b2f | 2015-08-13 07:57:10 -0700 | [diff] [blame] | 95 | SkASSERT(fViewMatrix); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 96 | SkASSERT(fShape); |
robertphillips | e7d4b2f | 2015-08-13 07:57:10 -0700 | [diff] [blame] | 97 | } |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 98 | #endif |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
bsalomon@google.com | 208236d | 2012-03-12 13:15:33 +0000 | [diff] [blame] | 101 | /** |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 102 | * Returns how well this path renderer is able to render the given path. Returning kNo or |
| 103 | * kAsBackup allows the caller to keep searching for a better path renderer. This function is |
| 104 | * called when searching for the best path renderer to draw a path. |
bsalomon@google.com | 208236d | 2012-03-12 13:15:33 +0000 | [diff] [blame] | 105 | */ |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 106 | CanDrawPath canDrawPath(const CanDrawPathArgs& args) const { |
robertphillips | e7d4b2f | 2015-08-13 07:57:10 -0700 | [diff] [blame] | 107 | SkDEBUGCODE(args.validate();) |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 108 | return this->onCanDrawPath(args); |
| 109 | } |
| 110 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 111 | struct DrawPathArgs { |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 112 | GrRecordingContext* fContext; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 113 | GrPaint&& fPaint; |
| 114 | const GrUserStencilSettings* fUserStencilSettings; |
| 115 | GrRenderTargetContext* fRenderTargetContext; |
| 116 | const GrClip* fClip; |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 117 | const SkIRect* fClipConservativeBounds; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 118 | const SkMatrix* fViewMatrix; |
| 119 | const GrShape* fShape; |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 120 | GrAAType fAAType; |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 121 | bool fGammaCorrect; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 122 | #ifdef SK_DEBUG |
robertphillips | e7d4b2f | 2015-08-13 07:57:10 -0700 | [diff] [blame] | 123 | void validate() const { |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 124 | SkASSERT(fContext); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 125 | SkASSERT(fUserStencilSettings); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 126 | SkASSERT(fRenderTargetContext); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 127 | SkASSERT(fClip); |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 128 | SkASSERT(fClipConservativeBounds); |
robertphillips | e7d4b2f | 2015-08-13 07:57:10 -0700 | [diff] [blame] | 129 | SkASSERT(fViewMatrix); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 130 | SkASSERT(fShape); |
robertphillips | e7d4b2f | 2015-08-13 07:57:10 -0700 | [diff] [blame] | 131 | } |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 132 | #endif |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 135 | /** |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 136 | * Draws the path into the draw target. If getStencilSupport() would return kNoRestriction then |
Brian Salomon | e5b399e | 2017-07-19 13:50:54 -0400 | [diff] [blame] | 137 | * the subclass must respect the stencil settings. |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 138 | */ |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 139 | bool drawPath(const DrawPathArgs& args); |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 140 | /** |
| 141 | * Args to stencilPath(). fAAType cannot be kCoverage. |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 142 | */ |
| 143 | struct StencilPathArgs { |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 144 | SkDEBUGCODE(StencilPathArgs() { memset(this, 0, sizeof(*this)); }) // For validation. |
| 145 | |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 146 | GrRecordingContext* fContext; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 147 | GrRenderTargetContext* fRenderTargetContext; |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 148 | const GrHardClip* fClip; |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 149 | const SkIRect* fClipConservativeBounds; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 150 | const SkMatrix* fViewMatrix; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 151 | const GrShape* fShape; |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame] | 152 | GrAA fDoStencilMSAA; |
robertphillips | e7d4b2f | 2015-08-13 07:57:10 -0700 | [diff] [blame] | 153 | |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 154 | SkDEBUGCODE(void validate() const); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 157 | /** |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 158 | * Draws the path to the stencil buffer. Assume the writable stencil bits are already |
| 159 | * initialized to zero. The pixels inside the path will have non-zero stencil values afterwards. |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 160 | */ |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 161 | void stencilPath(const StencilPathArgs& args) { |
robertphillips | e7d4b2f | 2015-08-13 07:57:10 -0700 | [diff] [blame] | 162 | SkDEBUGCODE(args.validate();) |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 163 | SkASSERT(kNoSupport_StencilSupport != this->getStencilSupport(*args.fShape)); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 164 | this->onStencilPath(args); |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 165 | } |
| 166 | |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 167 | // Helper for determining if we can treat a thin stroke as a hairline w/ coverage. |
| 168 | // If we can, we draw lots faster (raster device does this same test). |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 169 | static bool IsStrokeHairlineOrEquivalent(const GrStyle&, const SkMatrix&, |
| 170 | SkScalar* outCoverage); |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 171 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 172 | protected: |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 173 | // Helper for getting the device bounds of a path. Inverse filled paths will have bounds set |
| 174 | // by devSize. Non-inverse path bounds will not necessarily be clipped to devSize. |
| 175 | static void GetPathDevBounds(const SkPath& path, |
| 176 | int devW, |
| 177 | int devH, |
| 178 | const SkMatrix& matrix, |
| 179 | SkRect* bounds); |
| 180 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 181 | private: |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Subclass overrides if it has any limitations of stenciling support. |
| 184 | */ |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 185 | virtual StencilSupport onGetStencilSupport(const GrShape&) const { |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 186 | return kNoRestriction_StencilSupport; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Subclass implementation of drawPath() |
| 191 | */ |
| 192 | virtual bool onDrawPath(const DrawPathArgs& args) = 0; |
| 193 | |
| 194 | /** |
| 195 | * Subclass implementation of canDrawPath() |
| 196 | */ |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 197 | virtual CanDrawPath onCanDrawPath(const CanDrawPathArgs& args) const = 0; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * Subclass implementation of stencilPath(). Subclass must override iff it ever returns |
| 201 | * kStencilOnly in onGetStencilSupport(). |
| 202 | */ |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 203 | virtual void onStencilPath(const StencilPathArgs&); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 204 | |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 205 | typedef SkRefCnt INHERITED; |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 208 | #endif |