robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +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 GrClipMaskCache_DEFINED |
| 9 | #define GrClipMaskCache_DEFINED |
| 10 | |
| 11 | #include "GrContext.h" |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 12 | #include "SkClipStack.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 13 | #include "SkTypes.h" |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 14 | |
| 15 | class GrTexture; |
| 16 | |
| 17 | /** |
| 18 | * The stencil buffer stores the last clip path - providing a single entry |
| 19 | * "cache". This class provides similar functionality for AA clip paths |
| 20 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame^] | 21 | class GrClipMaskCache : SkNoncopyable { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 22 | public: |
| 23 | GrClipMaskCache(); |
| 24 | |
| 25 | ~GrClipMaskCache() { |
| 26 | |
| 27 | while (!fStack.empty()) { |
| 28 | GrClipStackFrame* temp = (GrClipStackFrame*) fStack.back(); |
| 29 | temp->~GrClipStackFrame(); |
| 30 | fStack.pop_back(); |
| 31 | } |
| 32 | } |
| 33 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 34 | bool canReuse(int32_t clipGenID, const SkIRect& bounds) { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 35 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 36 | SkASSERT(clipGenID != SkClipStack::kWideOpenGenID); |
| 37 | SkASSERT(clipGenID != SkClipStack::kEmptyGenID); |
| 38 | |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 39 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 40 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 41 | // We could reuse the mask if bounds is a subset of last bounds. We'd have to communicate |
| 42 | // an offset to the caller. |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 43 | if (back->fLastMask.texture() && |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 44 | back->fLastBound == bounds && |
| 45 | back->fLastClipGenID == clipGenID) { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 46 | return true; |
| 47 | } |
| 48 | |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | void reset() { |
| 53 | if (fStack.empty()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 54 | // SkASSERT(false); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | |
| 58 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 59 | |
| 60 | back->reset(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * After a "push" the clip state is entirely open. Currently, the |
| 65 | * entire clip stack will be re-rendered into a new clip mask. |
| 66 | * TODO: can we take advantage of the nested nature of the clips to |
| 67 | * reduce the mask creation cost? |
| 68 | */ |
| 69 | void push(); |
| 70 | |
| 71 | void pop() { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 72 | //SkASSERT(!fStack.empty()); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 73 | |
| 74 | if (!fStack.empty()) { |
| 75 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 76 | |
| 77 | back->~GrClipStackFrame(); |
| 78 | fStack.pop_back(); |
| 79 | } |
| 80 | } |
| 81 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 82 | int32_t getLastClipGenID() const { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 83 | |
| 84 | if (fStack.empty()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 85 | return SkClipStack::kInvalidGenID; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 86 | } |
| 87 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 88 | return ((GrClipStackFrame*) fStack.back())->fLastClipGenID; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | GrTexture* getLastMask() { |
| 92 | |
| 93 | if (fStack.empty()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 94 | SkASSERT(false); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 99 | |
| 100 | return back->fLastMask.texture(); |
| 101 | } |
| 102 | |
| 103 | const GrTexture* getLastMask() const { |
| 104 | |
| 105 | if (fStack.empty()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 106 | SkASSERT(false); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 111 | |
| 112 | return back->fLastMask.texture(); |
| 113 | } |
| 114 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 115 | void acquireMask(int32_t clipGenID, |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 116 | const GrTextureDesc& desc, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 117 | const SkIRect& bound) { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 118 | |
| 119 | if (fStack.empty()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 120 | SkASSERT(false); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | |
| 124 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 125 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 126 | back->acquireMask(fContext, clipGenID, desc, bound); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | int getLastMaskWidth() const { |
| 130 | |
| 131 | if (fStack.empty()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 132 | SkASSERT(false); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 137 | |
| 138 | if (NULL == back->fLastMask.texture()) { |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | return back->fLastMask.texture()->width(); |
| 143 | } |
| 144 | |
| 145 | int getLastMaskHeight() const { |
| 146 | |
| 147 | if (fStack.empty()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 148 | SkASSERT(false); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 153 | |
| 154 | if (NULL == back->fLastMask.texture()) { |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | return back->fLastMask.texture()->height(); |
| 159 | } |
| 160 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 161 | void getLastBound(SkIRect* bound) const { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 162 | |
| 163 | if (fStack.empty()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 164 | SkASSERT(false); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 165 | bound->setEmpty(); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 170 | |
| 171 | *bound = back->fLastBound; |
| 172 | } |
| 173 | |
| 174 | void setContext(GrContext* context) { |
| 175 | fContext = context; |
| 176 | } |
| 177 | |
| 178 | GrContext* getContext() { |
| 179 | return fContext; |
| 180 | } |
| 181 | |
| 182 | void releaseResources() { |
| 183 | |
| 184 | SkDeque::F2BIter iter(fStack); |
| 185 | for (GrClipStackFrame* frame = (GrClipStackFrame*) iter.next(); |
| 186 | frame != NULL; |
| 187 | frame = (GrClipStackFrame*) iter.next()) { |
| 188 | frame->reset(); |
| 189 | } |
| 190 | } |
| 191 | |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 192 | private: |
| 193 | struct GrClipStackFrame { |
| 194 | |
| 195 | GrClipStackFrame() { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 196 | this->reset(); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void acquireMask(GrContext* context, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 200 | int32_t clipGenID, |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 201 | const GrTextureDesc& desc, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 202 | const SkIRect& bound) { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 203 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 204 | fLastClipGenID = clipGenID; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 205 | |
| 206 | fLastMask.set(context, desc); |
| 207 | |
| 208 | fLastBound = bound; |
| 209 | } |
| 210 | |
| 211 | void reset () { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 212 | fLastClipGenID = SkClipStack::kInvalidGenID; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 213 | |
| 214 | GrTextureDesc desc; |
| 215 | |
| 216 | fLastMask.set(NULL, desc); |
| 217 | fLastBound.setEmpty(); |
| 218 | } |
| 219 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 220 | int32_t fLastClipGenID; |
| 221 | // The mask's width & height values are used by GrClipMaskManager to correctly scale the |
| 222 | // texture coords for the geometry drawn with this mask. |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 223 | GrAutoScratchTexture fLastMask; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 224 | // fLastBound stores the bounding box of the clip mask in clip-stack space. This rect is |
| 225 | // used by GrClipMaskManager to position a rect and compute texture coords for the mask. |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 226 | SkIRect fLastBound; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | GrContext* fContext; |
| 230 | SkDeque fStack; |
| 231 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 232 | typedef SkNoncopyable INHERITED; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | #endif // GrClipMaskCache_DEFINED |