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 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 25 | class GrGpu; |
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 | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 39 | class GrClipMaskManager : public 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() |
| 42 | : fGpu(NULL) |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 43 | , fCurrClipMaskType(kNone_ClipMaskType) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 44 | } |
| 45 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 46 | /** |
| 47 | * Creates a clip mask if necessary as a stencil buffer or alpha texture |
| 48 | * and sets the GrGpu's scissor and stencil state. If the return is false |
| 49 | * then the draw can be skipped. |
| 50 | */ |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 51 | bool setupClipping(const GrClipData* clipDataIn, GrDrawState::AutoRestoreEffects*); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 52 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 53 | void releaseResources(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 54 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 55 | bool isClipInStencil() const { |
| 56 | return kStencil_ClipMaskType == fCurrClipMaskType; |
| 57 | } |
| 58 | bool isClipInAlpha() const { |
| 59 | return kAlpha_ClipMaskType == fCurrClipMaskType; |
| 60 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 61 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 62 | void invalidateStencilMask() { |
| 63 | if (kStencil_ClipMaskType == fCurrClipMaskType) { |
| 64 | fCurrClipMaskType = kNone_ClipMaskType; |
| 65 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 66 | } |
| 67 | |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 68 | GrContext* getContext() { |
| 69 | return fAACache.getContext(); |
| 70 | } |
| 71 | |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 72 | void setGpu(GrGpu* gpu); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 73 | |
| 74 | void adjustPathStencilParams(GrStencilSettings* settings); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 75 | private: |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 76 | /** |
| 77 | * Informs the helper function adjustStencilParams() about how the stencil |
| 78 | * buffer clip is being used. |
| 79 | */ |
| 80 | enum StencilClipMode { |
| 81 | // Draw to the clip bit of the stencil buffer |
| 82 | kModifyClip_StencilClipMode, |
| 83 | // Clip against the existing representation of the clip in the high bit |
| 84 | // of the stencil buffer. |
| 85 | kRespectClip_StencilClipMode, |
| 86 | // Neither writing to nor clipping against the clip bit. |
| 87 | kIgnoreClip_StencilClipMode, |
| 88 | }; |
| 89 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 90 | GrGpu* fGpu; |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * We may represent the clip as a mask in the stencil buffer or as an alpha |
| 94 | * texture. It may be neither because the scissor rect suffices or we |
| 95 | * haven't yet examined the clip. |
| 96 | */ |
| 97 | enum ClipMaskType { |
| 98 | kNone_ClipMaskType, |
| 99 | kStencil_ClipMaskType, |
| 100 | kAlpha_ClipMaskType, |
| 101 | } fCurrClipMaskType; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 102 | |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 103 | GrClipMaskCache fAACache; // cache for the AA path |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 104 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 105 | // Draws the clip into the stencil buffer |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 106 | bool createStencilClipMask(int32_t elementsGenID, |
| 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); |
| 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, |
| 116 | const SkIRect& clipSpaceIBounds); |
| 117 | // 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] | 118 | GrTexture* createSoftwareClipMask(int32_t elementsGenID, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 119 | GrReducedClip::InitialState initialState, |
| 120 | const GrReducedClip::ElementList& elements, |
| 121 | const SkIRect& clipSpaceIBounds); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 122 | |
skia.committer@gmail.com | d21444a | 2012-12-07 02:01:25 +0000 | [diff] [blame] | 123 | // Gets a texture to use for the clip mask. If true is returned then a cached mask was found |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 124 | // that already contains the rasterization of the clip stack, otherwise an uninitialized texture |
robertphillips@google.com | 2d2e5c4 | 2013-10-30 21:30:43 +0000 | [diff] [blame] | 125 | // is returned. 'willUpload' is set when the alpha mask needs to be uploaded from the CPU. |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 126 | bool getMaskTexture(int32_t elementsGenID, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 127 | const SkIRect& clipSpaceIBounds, |
robertphillips@google.com | 2d2e5c4 | 2013-10-30 21:30:43 +0000 | [diff] [blame] | 128 | GrTexture** result, |
| 129 | bool willUpload); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 130 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 131 | bool useSWOnlyPath(const GrReducedClip::ElementList& elements); |
| 132 | |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 133 | // Draws a clip element into the target alpha mask. The caller should have already setup the |
| 134 | // desired blend operation. Optionally if the caller already selected a path renderer it can |
| 135 | // be passed. Otherwise the function will select one if the element is a path. |
| 136 | bool drawElement(GrTexture* target, const SkClipStack::Element*, GrPathRenderer* = NULL); |
| 137 | |
| 138 | // Determines whether it is possible to draw the element to both the stencil buffer and the |
| 139 | // alpha mask simultaneously. If so and the element is a path a compatible path renderer is |
| 140 | // also returned. |
| 141 | bool canStencilAndDrawElement(GrTexture* target, const SkClipStack::Element*, GrPathRenderer**); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 142 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 143 | void mergeMask(GrTexture* dstMask, |
| 144 | GrTexture* srcMask, |
| 145 | SkRegion::Op op, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 146 | const SkIRect& dstBound, |
| 147 | const SkIRect& srcBound); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 148 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 149 | void getTemp(int width, int height, GrAutoScratchTexture* temp); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 150 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 151 | void setupCache(const SkClipStack& clip, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 152 | const SkIRect& bounds); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 153 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 154 | /** |
| 155 | * Called prior to return control back the GrGpu in setupClipping. It |
| 156 | * updates the GrGpu with stencil settings that account stencil-based |
| 157 | * clipping. |
| 158 | */ |
| 159 | void setGpuStencil(); |
| 160 | |
| 161 | /** |
| 162 | * Adjusts the stencil settings to account for interaction with stencil |
| 163 | * clipping. |
| 164 | */ |
| 165 | void adjustStencilParams(GrStencilSettings* settings, |
| 166 | StencilClipMode mode, |
| 167 | int stencilBitCnt); |
| 168 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 169 | typedef SkNoncopyable INHERITED; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 172 | #endif // GrClipMaskManager_DEFINED |