robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #ifndef GrClipMaskManager_DEFINED |
| 10 | #define GrClipMaskManager_DEFINED |
| 11 | |
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 | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 14 | #include "GrNoncopyable.h" |
| 15 | #include "GrRect.h" |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 16 | #include "GrReducedClip.h" |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 17 | #include "GrStencil.h" |
| 18 | #include "GrTexture.h" |
| 19 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 20 | #include "SkClipStack.h" |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 21 | #include "SkDeque.h" |
| 22 | #include "SkPath.h" |
| 23 | #include "SkRefCnt.h" |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 24 | #include "SkTLList.h" |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 25 | |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 26 | #include "GrClipMaskCache.h" |
| 27 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 28 | class GrGpu; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 29 | class GrPathRenderer; |
| 30 | class GrPathRendererChain; |
| 31 | class SkPath; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 32 | class GrTexture; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 33 | |
| 34 | /** |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 35 | * The clip mask creator handles the generation of the clip mask. If anti |
| 36 | * aliasing is requested it will (in the future) generate a single channel |
| 37 | * (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] | 38 | * mask in the stencil buffer. In the non anti-aliasing case, if the clip |
| 39 | * mask can be represented as a rectangle then scissoring is used. In all |
| 40 | * cases scissoring is used to bound the range of the clip mask. |
| 41 | */ |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 42 | class GrClipMaskManager : public GrNoncopyable { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 43 | public: |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 44 | GrClipMaskManager() |
| 45 | : fGpu(NULL) |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 46 | , fCurrClipMaskType(kNone_ClipMaskType) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 47 | } |
| 48 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 49 | /** |
| 50 | * Creates a clip mask if necessary as a stencil buffer or alpha texture |
| 51 | * and sets the GrGpu's scissor and stencil state. If the return is false |
| 52 | * then the draw can be skipped. |
| 53 | */ |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame^] | 54 | bool setupClipping(const GrClipData* clipDataIn, GrDrawState::AutoRestoreEffects*); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 55 | |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 56 | void releaseResources(); |
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 | } |
| 61 | bool isClipInAlpha() const { |
| 62 | return kAlpha_ClipMaskType == fCurrClipMaskType; |
| 63 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 64 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 65 | void invalidateStencilMask() { |
| 66 | if (kStencil_ClipMaskType == fCurrClipMaskType) { |
| 67 | fCurrClipMaskType = kNone_ClipMaskType; |
| 68 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 69 | } |
| 70 | |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 71 | GrContext* getContext() { |
| 72 | return fAACache.getContext(); |
| 73 | } |
| 74 | |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 75 | void setGpu(GrGpu* gpu); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 76 | private: |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 77 | /** |
| 78 | * Informs the helper function adjustStencilParams() about how the stencil |
| 79 | * buffer clip is being used. |
| 80 | */ |
| 81 | enum StencilClipMode { |
| 82 | // Draw to the clip bit of the stencil buffer |
| 83 | kModifyClip_StencilClipMode, |
| 84 | // Clip against the existing representation of the clip in the high bit |
| 85 | // of the stencil buffer. |
| 86 | kRespectClip_StencilClipMode, |
| 87 | // Neither writing to nor clipping against the clip bit. |
| 88 | kIgnoreClip_StencilClipMode, |
| 89 | }; |
| 90 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 91 | GrGpu* fGpu; |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * We may represent the clip as a mask in the stencil buffer or as an alpha |
| 95 | * texture. It may be neither because the scissor rect suffices or we |
| 96 | * haven't yet examined the clip. |
| 97 | */ |
| 98 | enum ClipMaskType { |
| 99 | kNone_ClipMaskType, |
| 100 | kStencil_ClipMaskType, |
| 101 | kAlpha_ClipMaskType, |
| 102 | } fCurrClipMaskType; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 103 | |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 104 | GrClipMaskCache fAACache; // cache for the AA path |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 105 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 106 | // Draws the clip into the stencil buffer |
| 107 | bool createStencilClipMask(GrReducedClip::InitialState initialState, |
| 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. |
| 113 | GrTexture* createAlphaClipMask(int32_t clipStackGenID, |
| 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. |
| 118 | GrTexture* createSoftwareClipMask(int32_t clipStackGenID, |
| 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 |
| 125 | // is returned. |
| 126 | bool getMaskTexture(int32_t clipStackGenID, |
| 127 | const SkIRect& clipSpaceIBounds, |
| 128 | GrTexture** result); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 129 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 130 | bool useSWOnlyPath(const GrReducedClip::ElementList& elements); |
| 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 |
| 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. |
| 135 | bool drawElement(GrTexture* target, const SkClipStack::Element*, GrPathRenderer* = NULL); |
| 136 | |
| 137 | // Determines whether it is possible to draw the element to both the stencil buffer and the |
| 138 | // alpha mask simultaneously. If so and the element is a path a compatible path renderer is |
| 139 | // also returned. |
| 140 | bool canStencilAndDrawElement(GrTexture* target, const SkClipStack::Element*, GrPathRenderer**); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 141 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 142 | void mergeMask(GrTexture* dstMask, |
| 143 | GrTexture* srcMask, |
| 144 | SkRegion::Op op, |
| 145 | const GrIRect& dstBound, |
| 146 | const GrIRect& srcBound); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 147 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 148 | void getTemp(int width, int height, GrAutoScratchTexture* temp); |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 149 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 150 | void setupCache(const SkClipStack& clip, |
robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 151 | const GrIRect& bounds); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 152 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 153 | /** |
| 154 | * Called prior to return control back the GrGpu in setupClipping. It |
| 155 | * updates the GrGpu with stencil settings that account stencil-based |
| 156 | * clipping. |
| 157 | */ |
| 158 | void setGpuStencil(); |
| 159 | |
| 160 | /** |
| 161 | * Adjusts the stencil settings to account for interaction with stencil |
| 162 | * clipping. |
| 163 | */ |
| 164 | void adjustStencilParams(GrStencilSettings* settings, |
| 165 | StencilClipMode mode, |
| 166 | int stencilBitCnt); |
| 167 | |
robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 168 | typedef GrNoncopyable INHERITED; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 171 | #endif // GrClipMaskManager_DEFINED |