robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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. |
| 6 | */ |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 7 | #ifndef GrClipMaskManager_DEFINED |
| 8 | #define GrClipMaskManager_DEFINED |
| 9 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 10 | #include "GrPipelineBuilder.h" |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 11 | #include "GrReducedClip.h" |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 12 | #include "GrStencil.h" |
| 13 | #include "GrTexture.h" |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 14 | #include "SkClipStack.h" |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 15 | #include "SkDeque.h" |
| 16 | #include "SkPath.h" |
| 17 | #include "SkRefCnt.h" |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 18 | #include "SkTLList.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 19 | #include "SkTypes.h" |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 20 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 21 | class GrDrawTarget; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 22 | class GrPathRenderer; |
| 23 | class GrPathRendererChain; |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame^] | 24 | class GrResourceProvider; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 25 | class GrTexture; |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 26 | class SkPath; |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Produced by GrClipMaskManager. It provides a set of modifications to the drawing state that |
| 30 | * are used to create the final GrPipeline for a GrBatch. This is a work in progress. It will |
| 31 | * eventually encapsulate all mechanisms for modifying the scissor, shaders, and stencil state |
| 32 | * to implement clipping. |
| 33 | */ |
| 34 | class GrAppliedClip : public SkNoncopyable { |
| 35 | public: |
| 36 | GrAppliedClip() {} |
| 37 | const GrFragmentProcessor* clipCoverageFragmentProcessor() const { return fClipCoverageFP; } |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame] | 38 | const GrScissorState& scissorState() const { return fScissorState; } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 39 | |
| 40 | private: |
| 41 | SkAutoTUnref<const GrFragmentProcessor> fClipCoverageFP; |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame] | 42 | GrScissorState fScissorState; |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 43 | friend class GrClipMaskManager; |
| 44 | |
| 45 | typedef SkNoncopyable INHERITED; |
| 46 | }; |
| 47 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 48 | /** |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 49 | * The clip mask creator handles the generation of the clip mask. If anti |
| 50 | * aliasing is requested it will (in the future) generate a single channel |
| 51 | * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 52 | * mask in the stencil buffer. In the non anti-aliasing case, if the clip |
| 53 | * mask can be represented as a rectangle then scissoring is used. In all |
| 54 | * cases scissoring is used to bound the range of the clip mask. |
| 55 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 56 | class GrClipMaskManager : SkNoncopyable { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 57 | public: |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 58 | GrClipMaskManager(GrDrawTarget* owner); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 59 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 60 | /** |
| 61 | * Creates a clip mask if necessary as a stencil buffer or alpha texture |
| 62 | * and sets the GrGpu's scissor and stencil state. If the return is false |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 63 | * then the draw can be skipped. The AutoRestoreEffects is initialized by |
| 64 | * the manager when it must install additional effects to implement the |
| 65 | * clip. devBounds is optional but can help optimize clipping. |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 66 | */ |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 67 | bool setupClipping(const GrPipelineBuilder&, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 68 | GrPipelineBuilder::AutoRestoreStencil*, |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 69 | const SkRect* devBounds, |
| 70 | GrAppliedClip*); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 71 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 72 | void adjustPathStencilParams(const GrStencilAttachment*, GrStencilSettings*); |
joshualitt | 329bf48 | 2014-10-29 12:31:28 -0700 | [diff] [blame] | 73 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 74 | private: |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 75 | inline GrContext* getContext(); |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame^] | 76 | inline const GrCaps* caps() const; |
| 77 | inline GrResourceProvider* resourceProvider(); |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 78 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 79 | /** |
| 80 | * Informs the helper function adjustStencilParams() about how the stencil |
| 81 | * buffer clip is being used. |
| 82 | */ |
| 83 | enum StencilClipMode { |
| 84 | // Draw to the clip bit of the stencil buffer |
| 85 | kModifyClip_StencilClipMode, |
| 86 | // Clip against the existing representation of the clip in the high bit |
| 87 | // of the stencil buffer. |
| 88 | kRespectClip_StencilClipMode, |
| 89 | // Neither writing to nor clipping against the clip bit. |
| 90 | kIgnoreClip_StencilClipMode, |
| 91 | }; |
| 92 | |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 93 | // Attempts to install a series of coverage effects to implement the clip. Return indicates |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 94 | // whether the element list was successfully converted to processors. *fp may be nullptr even |
| 95 | // when the function succeeds because all the elements were ignored. TODO: Make clip reduction |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 96 | // bounds-aware and stop checking bounds in this function. Similarly, we shouldn't need to pass |
| 97 | // abortIfAA, but we don't yet know if all the AA elements will be eliminated. |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 98 | bool getAnalyticClipProcessor(const GrReducedClip::ElementList&, |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 99 | bool abortIfAA, |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 100 | SkVector& clipOffset, |
| 101 | const SkRect* devBounds, |
| 102 | const GrFragmentProcessor** fp); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 103 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 104 | // Draws the clip into the stencil buffer |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 105 | bool createStencilClipMask(GrRenderTarget*, |
| 106 | int32_t elementsGenID, |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 107 | GrReducedClip::InitialState initialState, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 108 | const GrReducedClip::ElementList& elements, |
| 109 | const SkIRect& clipSpaceIBounds, |
| 110 | const SkIPoint& clipSpaceToStencilOffset); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 111 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 112 | // Creates an alpha mask of the clip. The mask is a rasterization of elements through the |
| 113 | // rect specified by clipSpaceIBounds. |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 114 | GrTexture* createAlphaClipMask(int32_t elementsGenID, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 115 | GrReducedClip::InitialState initialState, |
| 116 | const GrReducedClip::ElementList& elements, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 117 | const SkVector& clipToMaskOffset, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 118 | const SkIRect& clipSpaceIBounds); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 119 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 120 | // Similar to createAlphaClipMask but it rasterizes in SW and uploads to the result texture. |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 121 | GrTexture* createSoftwareClipMask(int32_t elementsGenID, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 122 | GrReducedClip::InitialState initialState, |
| 123 | const GrReducedClip::ElementList& elements, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 124 | const SkVector& clipToMaskOffset, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 125 | const SkIRect& clipSpaceIBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 126 | |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 127 | bool useSWOnlyPath(const GrPipelineBuilder&, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 128 | const SkVector& clipToMaskOffset, |
| 129 | const GrReducedClip::ElementList& elements); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 130 | |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 131 | // Draws a clip element into the target alpha mask. The caller should have already setup the |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 132 | // desired blend operation. Optionally if the caller already selected a path renderer it can |
| 133 | // be passed. Otherwise the function will select one if the element is a path. |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 134 | bool drawElement(GrPipelineBuilder*, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 135 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 136 | GrTexture* target, |
| 137 | const SkClipStack::Element*, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 138 | GrPathRenderer* pr = nullptr); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 139 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 140 | /** |
bsalomon | 9e5fc72 | 2015-02-23 10:01:36 -0800 | [diff] [blame] | 141 | * Called prior to return control back the GrGpu in setupClipping. It updates the |
| 142 | * GrPipelineBuilder with stencil settings that account for stencil-based clipping. |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 143 | */ |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 144 | void setPipelineBuilderStencil(const GrPipelineBuilder&, |
| 145 | GrPipelineBuilder::AutoRestoreStencil*); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * Adjusts the stencil settings to account for interaction with stencil |
| 149 | * clipping. |
| 150 | */ |
| 151 | void adjustStencilParams(GrStencilSettings* settings, |
| 152 | StencilClipMode mode, |
| 153 | int stencilBitCnt); |
| 154 | |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 155 | GrTexture* createCachedMask(int width, int height, const GrUniqueKey& key, bool renderTarget); |
| 156 | |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 157 | static const int kMaxAnalyticElements = 4; |
| 158 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 159 | GrDrawTarget* fDrawTarget; // This is our owning draw target. |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 160 | StencilClipMode fClipMode; |
joshualitt | 329bf48 | 2014-10-29 12:31:28 -0700 | [diff] [blame] | 161 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 162 | typedef SkNoncopyable INHERITED; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 163 | }; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 164 | #endif // GrClipMaskManager_DEFINED |