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