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