| 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 | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 13 | #include "GrNoncopyable.h" |
| 14 | #include "GrRect.h" |
| 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" |
| robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 23 | |
| robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 24 | #include "GrClipMaskCache.h" |
| 25 | |
| robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 26 | class GrGpu; |
| robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 27 | class GrPathRenderer; |
| 28 | class GrPathRendererChain; |
| 29 | class SkPath; |
| robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 30 | class GrTexture; |
| 31 | class GrDrawState; |
| robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 34 | * The clip mask creator handles the generation of the clip mask. If anti |
| 35 | * aliasing is requested it will (in the future) generate a single channel |
| 36 | * (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] | 37 | * mask in the stencil buffer. In the non anti-aliasing case, if the clip |
| 38 | * mask can be represented as a rectangle then scissoring is used. In all |
| 39 | * cases scissoring is used to bound the range of the clip mask. |
| 40 | */ |
| robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 41 | class GrClipMaskManager : public GrNoncopyable { |
| robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 42 | public: |
| robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 43 | GR_DECLARE_RESOURCE_CACHE_DOMAIN(GetAlphaMaskDomain) |
| 44 | |
| robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 45 | GrClipMaskManager() |
| 46 | : fGpu(NULL) |
| bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 47 | , fCurrClipMaskType(kNone_ClipMaskType) { |
| robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 50 | /** |
| 51 | * Creates a clip mask if necessary as a stencil buffer or alpha texture |
| 52 | * and sets the GrGpu's scissor and stencil state. If the return is false |
| 53 | * then the draw can be skipped. |
| 54 | */ |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 55 | bool setupClipping(const GrClipData* clipDataIn); |
| robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 56 | |
| robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 57 | void releaseResources(); |
| robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 58 | |
| bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 59 | bool isClipInStencil() const { |
| 60 | return kStencil_ClipMaskType == fCurrClipMaskType; |
| 61 | } |
| 62 | bool isClipInAlpha() const { |
| 63 | return kAlpha_ClipMaskType == fCurrClipMaskType; |
| 64 | } |
| robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 65 | |
| bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 66 | void invalidateStencilMask() { |
| 67 | if (kStencil_ClipMaskType == fCurrClipMaskType) { |
| 68 | fCurrClipMaskType = kNone_ClipMaskType; |
| 69 | } |
| robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 72 | void setContext(GrContext* context) { |
| 73 | fAACache.setContext(context); |
| 74 | } |
| 75 | |
| robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 76 | GrContext* getContext() { |
| 77 | return fAACache.getContext(); |
| 78 | } |
| 79 | |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 80 | void setGpu(GrGpu* gpu) { |
| robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 81 | fGpu = gpu; |
| 82 | } |
| 83 | |
| bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 84 | private: |
| bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 85 | /** |
| 86 | * Informs the helper function adjustStencilParams() about how the stencil |
| 87 | * buffer clip is being used. |
| 88 | */ |
| 89 | enum StencilClipMode { |
| 90 | // Draw to the clip bit of the stencil buffer |
| 91 | kModifyClip_StencilClipMode, |
| 92 | // Clip against the existing representation of the clip in the high bit |
| 93 | // of the stencil buffer. |
| 94 | kRespectClip_StencilClipMode, |
| 95 | // Neither writing to nor clipping against the clip bit. |
| 96 | kIgnoreClip_StencilClipMode, |
| 97 | }; |
| 98 | |
| bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 99 | GrGpu* fGpu; |
| bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * We may represent the clip as a mask in the stencil buffer or as an alpha |
| 103 | * texture. It may be neither because the scissor rect suffices or we |
| 104 | * haven't yet examined the clip. |
| 105 | */ |
| 106 | enum ClipMaskType { |
| 107 | kNone_ClipMaskType, |
| 108 | kStencil_ClipMaskType, |
| 109 | kAlpha_ClipMaskType, |
| 110 | } fCurrClipMaskType; |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 111 | |
| robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 112 | GrClipMaskCache fAACache; // cache for the AA path |
| robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 113 | |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 114 | bool createStencilClipMask(const GrClipData& clipDataIn, |
| robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 115 | const GrIRect& devClipBounds); |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 116 | bool createAlphaClipMask(const GrClipData& clipDataIn, |
| robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 117 | GrTexture** result, |
| robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 118 | GrIRect *devResultBounds); |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 119 | bool createSoftwareClipMask(const GrClipData& clipDataIn, |
| robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 120 | GrTexture** result, |
| robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 121 | GrIRect *devResultBounds); |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 122 | bool clipMaskPreamble(const GrClipData& clipDataIn, |
| robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 123 | GrTexture** result, |
| robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 124 | GrIRect *devResultBounds); |
| robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 125 | |
| robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 126 | bool useSWOnlyPath(const SkClipStack& clipIn); |
| robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 127 | |
| bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 128 | bool drawClipShape(GrTexture* target, |
| bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame^] | 129 | const SkClipStack::Element* element, |
| bsalomon@google.com | 2e0c79f | 2012-11-12 13:38:57 +0000 | [diff] [blame] | 130 | const GrIRect& resultBounds); |
| robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 131 | |
| bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 132 | void mergeMask(GrTexture* dstMask, |
| 133 | GrTexture* srcMask, |
| 134 | SkRegion::Op op, |
| 135 | const GrIRect& dstBound, |
| 136 | const GrIRect& srcBound); |
| robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 137 | |
| robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 138 | void getTemp(const GrIRect& bounds, GrAutoScratchTexture* temp); |
| robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 139 | |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 140 | void setupCache(const SkClipStack& clip, |
| robertphillips@google.com | 6623fcd | 2012-05-15 16:47:23 +0000 | [diff] [blame] | 141 | const GrIRect& bounds); |
| robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 142 | |
| bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 143 | /** |
| 144 | * Called prior to return control back the GrGpu in setupClipping. It |
| 145 | * updates the GrGpu with stencil settings that account stencil-based |
| 146 | * clipping. |
| 147 | */ |
| 148 | void setGpuStencil(); |
| 149 | |
| 150 | /** |
| 151 | * Adjusts the stencil settings to account for interaction with stencil |
| 152 | * clipping. |
| 153 | */ |
| 154 | void adjustStencilParams(GrStencilSettings* settings, |
| 155 | StencilClipMode mode, |
| 156 | int stencilBitCnt); |
| 157 | |
| robertphillips@google.com | fd6daf5 | 2012-05-02 19:32:32 +0000 | [diff] [blame] | 158 | typedef GrNoncopyable INHERITED; |
| robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 161 | namespace GrReducedClip { |
| 162 | |
| bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame^] | 163 | typedef SkTLList<SkClipStack::Element> ElementList; |
| 164 | |
| bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 165 | enum InitialState { |
| 166 | kAllIn_InitialState, |
| 167 | kAllOut_InitialState, |
| 168 | }; |
| 169 | |
| 170 | /** This function takes a clip stack and produces a reduced set of SkClipStack::Iter::Clip elements |
| 171 | * in param clips that are equivalent to the full stack. If a finite bound for the area inside the |
| 172 | * clip can be determined resultsAreBounds will be true and resultBounds will be those bounds. When |
| 173 | * the results are bounded it is assumed that the caller will restrict the effect of each operation |
| 174 | * to the bounds or intersect with the bounds as a final step. The initial state of the bounds (or |
| 175 | * the unbounded plane when resultsArBounded is false) before the first element of clips is applied |
| 176 | * is returned via initialState. This function is declared here so that it can be unit-tested. It |
| 177 | * may become a member function of SkClipStack when its interface is determined to be stable. |
| 178 | */ |
| 179 | void GrReduceClipStack(const SkClipStack& stack, |
| bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame^] | 180 | ElementList* result, |
| bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 181 | SkRect* resultBounds, |
| 182 | bool* resultsAreBounded, |
| 183 | InitialState* initialState); |
| 184 | |
| 185 | } // namespace GrReducedClip |
| 186 | |
| robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 187 | #endif // GrClipMaskManager_DEFINED |