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