blob: f73e43ca9c44a337bb62edd62aa03f5b40e44d7e [file] [log] [blame]
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comdfe75bc2011-03-25 12:31:16 +00006 */
7
8#ifndef GrPathRenderer_DEFINED
9#define GrPathRenderer_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkRefCnt.h"
12#include "include/private/GrTypesPriv.h"
13#include "include/private/SkTArray.h"
bsalomon@google.com49313f62011-09-14 13:54:05 +000014
Brian Salomon653f42f2018-07-10 10:07:31 -040015class GrCaps;
16class GrClip;
robertphillips976f5f02016-06-03 10:59:20 -070017class GrFixedClip;
Chris Daltonbbfd5162017-11-07 13:35:22 -070018class GrHardClip;
Brian Salomon653f42f2018-07-10 10:07:31 -040019class GrPaint;
Robert Phillips6f0e02f2019-02-13 11:02:28 -050020class GrRecordingContext;
Brian Salomon653f42f2018-07-10 10:07:31 -040021class GrRenderTargetContext;
Chris Daltoneffee202019-07-01 22:28:03 -060022class GrRenderTargetProxy;
Brian Salomon653f42f2018-07-10 10:07:31 -040023class GrShape;
24class GrStyle;
25struct GrUserStencilSettings;
26struct SkIRect;
27class SkMatrix;
28class SkPath;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000029
30/**
Robert Phillipsf2361d22016-10-25 14:20:06 -040031 * Base class for drawing paths into a GrOpList.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000032 */
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000033class SK_API GrPathRenderer : public SkRefCnt {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000034public:
bsalomon@google.comc2099d22012-03-02 21:26:50 +000035 GrPathRenderer();
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000036
37 /**
bsalomon@google.com45a15f52012-12-10 19:10:17 +000038 * 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
joshualittb0a8a372014-09-23 09:50:21 -070040 * 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.com45a15f52012-12-10 19:10:17 +000043 *
44 * A GrPathRenderer can provide three levels of support for stenciling paths:
Brian Salomone5b399e2017-07-19 13:50:54 -040045 * 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.com45a15f52012-12-10 19:10:17 +000049 * 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 */
robertphillips68737822015-10-29 12:12:21 -070055 enum StencilSupport {
56 kNoSupport_StencilSupport,
57 kStencilOnly_StencilSupport,
58 kNoRestriction_StencilSupport,
59 };
bsalomon@google.com45a15f52012-12-10 19:10:17 +000060
61 /**
robertphillips@google.come79f3202014-02-11 16:30:21 +000062 * This function is to get the stencil support for a particular path. The path's fill must
bsalomond6f25bf2016-05-05 09:26:21 -070063 * not be an inverse type. The path will always be filled and not stroked.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000064 *
bsalomon8acedde2016-06-24 10:42:16 -070065 * @param shape the shape that will be drawn. Must be simple fill styled and non-inverse
66 * filled.
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000067 */
Brian Salomon653f42f2018-07-10 10:07:31 -040068 StencilSupport getStencilSupport(const GrShape& shape) const;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000069
Chris Dalton5ed44232017-09-07 13:22:46 -060070 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
bsalomon0aff2fa2015-07-31 06:48:27 -070076 struct CanDrawPathArgs {
Chris Daltondb91c6e2017-09-08 16:25:08 -060077 SkDEBUGCODE(CanDrawPathArgs() { memset(this, 0, sizeof(*this)); }) // For validation.
78
Eric Karl5c779752017-05-08 12:02:07 -070079 const GrCaps* fCaps;
Chris Daltoneffee202019-07-01 22:28:03 -060080 const GrRenderTargetProxy* fProxy;
Chris Daltondb91c6e2017-09-08 16:25:08 -060081 const SkIRect* fClipConservativeBounds;
bsalomon0aff2fa2015-07-31 06:48:27 -070082 const SkMatrix* fViewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -070083 const GrShape* fShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -060084 GrAAType fAAType;
Greg Danielbe7fc462019-01-03 16:40:42 -050085 bool fTargetIsWrappedVkSecondaryCB;
robertphillipse7d4b2f2015-08-13 07:57:10 -070086
Greg Danielbe7fc462019-01-03 16:40:42 -050087 // This is only used by GrStencilAndCoverPathRenderer
cdalton93a379b2016-05-11 13:58:08 -070088 bool fHasUserStencilSettings;
robertphillips68737822015-10-29 12:12:21 -070089
bsalomon8acedde2016-06-24 10:42:16 -070090#ifdef SK_DEBUG
robertphillipse7d4b2f2015-08-13 07:57:10 -070091 void validate() const {
Eric Karl5c779752017-05-08 12:02:07 -070092 SkASSERT(fCaps);
Chris Daltoneffee202019-07-01 22:28:03 -060093 SkASSERT(fProxy);
Chris Daltondb91c6e2017-09-08 16:25:08 -060094 SkASSERT(fClipConservativeBounds);
robertphillipse7d4b2f2015-08-13 07:57:10 -070095 SkASSERT(fViewMatrix);
bsalomon8acedde2016-06-24 10:42:16 -070096 SkASSERT(fShape);
robertphillipse7d4b2f2015-08-13 07:57:10 -070097 }
bsalomon8acedde2016-06-24 10:42:16 -070098#endif
bsalomon0aff2fa2015-07-31 06:48:27 -070099 };
100
bsalomon@google.com208236d2012-03-12 13:15:33 +0000101 /**
Chris Dalton5ed44232017-09-07 13:22:46 -0600102 * 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.com208236d2012-03-12 13:15:33 +0000105 */
Chris Dalton5ed44232017-09-07 13:22:46 -0600106 CanDrawPath canDrawPath(const CanDrawPathArgs& args) const {
robertphillipse7d4b2f2015-08-13 07:57:10 -0700107 SkDEBUGCODE(args.validate();)
bsalomon0aff2fa2015-07-31 06:48:27 -0700108 return this->onCanDrawPath(args);
109 }
110
bsalomon0aff2fa2015-07-31 06:48:27 -0700111 struct DrawPathArgs {
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500112 GrRecordingContext* fContext;
Brian Salomon82f44312017-01-11 13:42:54 -0500113 GrPaint&& fPaint;
114 const GrUserStencilSettings* fUserStencilSettings;
115 GrRenderTargetContext* fRenderTargetContext;
116 const GrClip* fClip;
Chris Daltondb91c6e2017-09-08 16:25:08 -0600117 const SkIRect* fClipConservativeBounds;
Brian Salomon82f44312017-01-11 13:42:54 -0500118 const SkMatrix* fViewMatrix;
119 const GrShape* fShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600120 GrAAType fAAType;
Brian Salomon82f44312017-01-11 13:42:54 -0500121 bool fGammaCorrect;
bsalomon8acedde2016-06-24 10:42:16 -0700122#ifdef SK_DEBUG
robertphillipse7d4b2f2015-08-13 07:57:10 -0700123 void validate() const {
Robert Phillips256c37b2017-03-01 14:32:46 -0500124 SkASSERT(fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700125 SkASSERT(fUserStencilSettings);
Brian Osman11052242016-10-27 14:47:55 -0400126 SkASSERT(fRenderTargetContext);
cdalton862cff32016-05-12 15:09:48 -0700127 SkASSERT(fClip);
Chris Daltondb91c6e2017-09-08 16:25:08 -0600128 SkASSERT(fClipConservativeBounds);
robertphillipse7d4b2f2015-08-13 07:57:10 -0700129 SkASSERT(fViewMatrix);
bsalomon8acedde2016-06-24 10:42:16 -0700130 SkASSERT(fShape);
robertphillipse7d4b2f2015-08-13 07:57:10 -0700131 }
bsalomon8acedde2016-06-24 10:42:16 -0700132#endif
bsalomon0aff2fa2015-07-31 06:48:27 -0700133 };
134
bsalomon@google.comee435122011-07-01 14:57:55 +0000135 /**
robertphillips@google.come79f3202014-02-11 16:30:21 +0000136 * Draws the path into the draw target. If getStencilSupport() would return kNoRestriction then
Brian Salomone5b399e2017-07-19 13:50:54 -0400137 * the subclass must respect the stencil settings.
bsalomon@google.comee435122011-07-01 14:57:55 +0000138 */
Brian Salomon653f42f2018-07-10 10:07:31 -0400139 bool drawPath(const DrawPathArgs& args);
Chris Daltondb91c6e2017-09-08 16:25:08 -0600140 /**
141 * Args to stencilPath(). fAAType cannot be kCoverage.
bsalomon0aff2fa2015-07-31 06:48:27 -0700142 */
143 struct StencilPathArgs {
Chris Daltondb91c6e2017-09-08 16:25:08 -0600144 SkDEBUGCODE(StencilPathArgs() { memset(this, 0, sizeof(*this)); }) // For validation.
145
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500146 GrRecordingContext* fContext;
Brian Osman11052242016-10-27 14:47:55 -0400147 GrRenderTargetContext* fRenderTargetContext;
Chris Daltonbbfd5162017-11-07 13:35:22 -0700148 const GrHardClip* fClip;
Chris Daltondb91c6e2017-09-08 16:25:08 -0600149 const SkIRect* fClipConservativeBounds;
Brian Osman11052242016-10-27 14:47:55 -0400150 const SkMatrix* fViewMatrix;
Brian Osman11052242016-10-27 14:47:55 -0400151 const GrShape* fShape;
Chris Dalton09e56892019-03-13 00:22:01 -0600152 GrAA fDoStencilMSAA;
robertphillipse7d4b2f2015-08-13 07:57:10 -0700153
Brian Salomon653f42f2018-07-10 10:07:31 -0400154 SkDEBUGCODE(void validate() const);
bsalomon0aff2fa2015-07-31 06:48:27 -0700155 };
156
bsalomon@google.comee435122011-07-01 14:57:55 +0000157 /**
robertphillips@google.come79f3202014-02-11 16:30:21 +0000158 * 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.com45a15f52012-12-10 19:10:17 +0000160 */
bsalomon0aff2fa2015-07-31 06:48:27 -0700161 void stencilPath(const StencilPathArgs& args) {
robertphillipse7d4b2f2015-08-13 07:57:10 -0700162 SkDEBUGCODE(args.validate();)
bsalomon8acedde2016-06-24 10:42:16 -0700163 SkASSERT(kNoSupport_StencilSupport != this->getStencilSupport(*args.fShape));
bsalomon0aff2fa2015-07-31 06:48:27 -0700164 this->onStencilPath(args);
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000165 }
166
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000167 // 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 Salomon653f42f2018-07-10 10:07:31 -0400169 static bool IsStrokeHairlineOrEquivalent(const GrStyle&, const SkMatrix&,
170 SkScalar* outCoverage);
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000171
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000172protected:
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000173 // 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.comdfe75bc2011-03-25 12:31:16 +0000181private:
bsalomon0aff2fa2015-07-31 06:48:27 -0700182 /**
183 * Subclass overrides if it has any limitations of stenciling support.
184 */
bsalomon8acedde2016-06-24 10:42:16 -0700185 virtual StencilSupport onGetStencilSupport(const GrShape&) const {
bsalomon0aff2fa2015-07-31 06:48:27 -0700186 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 Dalton5ed44232017-09-07 13:22:46 -0600197 virtual CanDrawPath onCanDrawPath(const CanDrawPathArgs& args) const = 0;
bsalomon0aff2fa2015-07-31 06:48:27 -0700198
199 /**
200 * Subclass implementation of stencilPath(). Subclass must override iff it ever returns
201 * kStencilOnly in onGetStencilSupport().
202 */
Brian Salomon653f42f2018-07-10 10:07:31 -0400203 virtual void onStencilPath(const StencilPathArgs&);
bsalomon0aff2fa2015-07-31 06:48:27 -0700204
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000205 typedef SkRefCnt INHERITED;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000206};
207
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000208#endif