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@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 24 | class GrTexture; |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 25 | class SkPath; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 26 | /** |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 27 | * The clip mask creator handles the generation of the clip mask. If anti |
| 28 | * aliasing is requested it will (in the future) generate a single channel |
| 29 | * (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] | 30 | * mask in the stencil buffer. In the non anti-aliasing case, if the clip |
| 31 | * mask can be represented as a rectangle then scissoring is used. In all |
| 32 | * cases scissoring is used to bound the range of the clip mask. |
| 33 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 34 | class GrClipMaskManager : SkNoncopyable { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 35 | public: |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 36 | GrClipMaskManager(GrDrawTarget* owner); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 37 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 38 | /** |
| 39 | * Creates a clip mask if necessary as a stencil buffer or alpha texture |
| 40 | * 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] | 41 | * then the draw can be skipped. The AutoRestoreEffects is initialized by |
| 42 | * the manager when it must install additional effects to implement the |
| 43 | * clip. devBounds is optional but can help optimize clipping. |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 44 | */ |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 45 | bool setupClipping(const GrPipelineBuilder&, |
joshualitt | 4421a4c | 2015-07-13 09:36:41 -0700 | [diff] [blame] | 46 | GrPipelineBuilder::AutoRestoreFragmentProcessorState*, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 47 | GrPipelineBuilder::AutoRestoreStencil*, |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 48 | GrScissorState*, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 49 | const SkRect* devBounds); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 50 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 51 | bool isClipInStencil() const { |
| 52 | return kStencil_ClipMaskType == fCurrClipMaskType; |
| 53 | } |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 54 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 55 | bool isClipInAlpha() const { |
| 56 | return kAlpha_ClipMaskType == fCurrClipMaskType; |
| 57 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 58 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 59 | void adjustPathStencilParams(const GrStencilAttachment*, GrStencilSettings*); |
joshualitt | 329bf48 | 2014-10-29 12:31:28 -0700 | [diff] [blame] | 60 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 61 | private: |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 62 | inline GrContext* getContext(); |
| 63 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 64 | /** |
| 65 | * Informs the helper function adjustStencilParams() about how the stencil |
| 66 | * buffer clip is being used. |
| 67 | */ |
| 68 | enum StencilClipMode { |
| 69 | // Draw to the clip bit of the stencil buffer |
| 70 | kModifyClip_StencilClipMode, |
| 71 | // Clip against the existing representation of the clip in the high bit |
| 72 | // of the stencil buffer. |
| 73 | kRespectClip_StencilClipMode, |
| 74 | // Neither writing to nor clipping against the clip bit. |
| 75 | kIgnoreClip_StencilClipMode, |
| 76 | }; |
| 77 | |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 78 | // Attempts to install a series of coverage effects to implement the clip. Return indicates |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 79 | // whether the element list was successfully converted to effects. |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 80 | bool installClipEffects(const GrPipelineBuilder&, |
joshualitt | 4421a4c | 2015-07-13 09:36:41 -0700 | [diff] [blame] | 81 | GrPipelineBuilder::AutoRestoreFragmentProcessorState*, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 82 | const GrReducedClip::ElementList&, |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 83 | const SkVector& clipOffset, |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 84 | const SkRect* devBounds); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 85 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 86 | // Draws the clip into the stencil buffer |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 87 | bool createStencilClipMask(GrRenderTarget*, |
| 88 | int32_t elementsGenID, |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 89 | GrReducedClip::InitialState initialState, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 90 | const GrReducedClip::ElementList& elements, |
| 91 | const SkIRect& clipSpaceIBounds, |
| 92 | const SkIPoint& clipSpaceToStencilOffset); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 93 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 94 | // Creates an alpha mask of the clip. The mask is a rasterization of elements through the |
| 95 | // rect specified by clipSpaceIBounds. |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 96 | GrTexture* createAlphaClipMask(int32_t elementsGenID, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 97 | GrReducedClip::InitialState initialState, |
| 98 | const GrReducedClip::ElementList& elements, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 99 | const SkVector& clipToMaskOffset, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 100 | const SkIRect& clipSpaceIBounds); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 101 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 102 | // 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] | 103 | GrTexture* createSoftwareClipMask(int32_t elementsGenID, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 104 | GrReducedClip::InitialState initialState, |
| 105 | const GrReducedClip::ElementList& elements, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 106 | const SkVector& clipToMaskOffset, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 107 | const SkIRect& clipSpaceIBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 108 | |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame^] | 109 | bool useSWOnlyPath(const GrPipelineBuilder&, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 110 | const SkVector& clipToMaskOffset, |
| 111 | const GrReducedClip::ElementList& elements); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 112 | |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 113 | // 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] | 114 | // desired blend operation. Optionally if the caller already selected a path renderer it can |
| 115 | // 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] | 116 | bool drawElement(GrPipelineBuilder*, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 117 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 118 | GrTexture* target, |
| 119 | const SkClipStack::Element*, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 120 | GrPathRenderer* pr = nullptr); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 121 | |
| 122 | // Determines whether it is possible to draw the element to both the stencil buffer and the |
| 123 | // alpha mask simultaneously. If so and the element is a path a compatible path renderer is |
| 124 | // also returned. |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 125 | bool canStencilAndDrawElement(GrPipelineBuilder*, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 126 | GrTexture* target, |
| 127 | GrPathRenderer**, |
| 128 | const SkClipStack::Element*); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 129 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 130 | void mergeMask(GrPipelineBuilder*, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 131 | GrTexture* dstMask, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 132 | GrTexture* srcMask, |
| 133 | SkRegion::Op op, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 134 | const SkIRect& dstBound, |
| 135 | const SkIRect& srcBound); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 136 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 137 | GrTexture* createTempMask(int width, int height); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 138 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 139 | /** |
bsalomon | 9e5fc72 | 2015-02-23 10:01:36 -0800 | [diff] [blame] | 140 | * Called prior to return control back the GrGpu in setupClipping. It updates the |
| 141 | * GrPipelineBuilder with stencil settings that account for stencil-based clipping. |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 142 | */ |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 143 | void setPipelineBuilderStencil(const GrPipelineBuilder&, |
| 144 | GrPipelineBuilder::AutoRestoreStencil*); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * Adjusts the stencil settings to account for interaction with stencil |
| 148 | * clipping. |
| 149 | */ |
| 150 | void adjustStencilParams(GrStencilSettings* settings, |
| 151 | StencilClipMode mode, |
| 152 | int stencilBitCnt); |
| 153 | |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame^] | 154 | GrTexture* createCachedMask(int width, int height, const GrUniqueKey& key, bool renderTarget); |
| 155 | |
joshualitt | 329bf48 | 2014-10-29 12:31:28 -0700 | [diff] [blame] | 156 | /** |
| 157 | * We may represent the clip as a mask in the stencil buffer or as an alpha |
| 158 | * texture. It may be neither because the scissor rect suffices or we |
| 159 | * haven't yet examined the clip. |
| 160 | */ |
| 161 | enum ClipMaskType { |
| 162 | kNone_ClipMaskType, |
| 163 | kStencil_ClipMaskType, |
| 164 | kAlpha_ClipMaskType, |
| 165 | } fCurrClipMaskType; |
| 166 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 167 | GrDrawTarget* fDrawTarget; // This is our owning draw target. |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 168 | StencilClipMode fClipMode; |
joshualitt | 329bf48 | 2014-10-29 12:31:28 -0700 | [diff] [blame] | 169 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 170 | typedef SkNoncopyable INHERITED; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 171 | }; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 172 | #endif // GrClipMaskManager_DEFINED |