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. |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 43 | if (back->fLastMask && |
| 44 | !back->fLastMask->wasDestroyed() && |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 45 | back->fLastBound == bounds && |
| 46 | back->fLastClipGenID == clipGenID) { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 47 | return true; |
| 48 | } |
| 49 | |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | void reset() { |
| 54 | if (fStack.empty()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 55 | // SkASSERT(false); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 56 | return; |
| 57 | } |
| 58 | |
| 59 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 60 | |
| 61 | back->reset(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * After a "push" the clip state is entirely open. Currently, the |
| 66 | * entire clip stack will be re-rendered into a new clip mask. |
| 67 | * TODO: can we take advantage of the nested nature of the clips to |
| 68 | * reduce the mask creation cost? |
| 69 | */ |
| 70 | void push(); |
| 71 | |
| 72 | void pop() { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 73 | //SkASSERT(!fStack.empty()); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 74 | |
| 75 | if (!fStack.empty()) { |
| 76 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 77 | |
| 78 | back->~GrClipStackFrame(); |
| 79 | fStack.pop_back(); |
| 80 | } |
| 81 | } |
| 82 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 83 | int32_t getLastClipGenID() const { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 84 | |
| 85 | if (fStack.empty()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 86 | return SkClipStack::kInvalidGenID; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 87 | } |
| 88 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 89 | return ((GrClipStackFrame*) fStack.back())->fLastClipGenID; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | GrTexture* getLastMask() { |
| 93 | |
| 94 | if (fStack.empty()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 95 | SkASSERT(false); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 100 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 101 | return back->fLastMask; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | const GrTexture* getLastMask() const { |
| 105 | |
| 106 | if (fStack.empty()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 107 | SkASSERT(false); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 112 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 113 | return back->fLastMask; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 114 | } |
| 115 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 116 | void acquireMask(int32_t clipGenID, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 117 | const GrSurfaceDesc& desc, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 118 | const SkIRect& bound) { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 119 | |
| 120 | if (fStack.empty()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 121 | SkASSERT(false); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | |
| 125 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 126 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 127 | back->acquireMask(fContext, clipGenID, desc, bound); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | int getLastMaskWidth() const { |
| 131 | |
| 132 | if (fStack.empty()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 133 | SkASSERT(false); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 138 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 139 | if (NULL == back->fLastMask) { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 140 | return -1; |
| 141 | } |
| 142 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 143 | return back->fLastMask->width(); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | int getLastMaskHeight() const { |
| 147 | |
| 148 | if (fStack.empty()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 149 | SkASSERT(false); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 154 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 155 | if (NULL == back->fLastMask) { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 156 | return -1; |
| 157 | } |
| 158 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 159 | return back->fLastMask->height(); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 160 | } |
| 161 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 162 | void getLastBound(SkIRect* bound) const { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 163 | |
| 164 | if (fStack.empty()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 165 | SkASSERT(false); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 166 | bound->setEmpty(); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 171 | |
| 172 | *bound = back->fLastBound; |
| 173 | } |
| 174 | |
| 175 | void setContext(GrContext* context) { |
| 176 | fContext = context; |
| 177 | } |
| 178 | |
| 179 | GrContext* getContext() { |
| 180 | return fContext; |
| 181 | } |
| 182 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 183 | // TODO: Remove this when we hold cache keys instead of refs to textures. |
| 184 | void purgeResources() { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 185 | SkDeque::F2BIter iter(fStack); |
| 186 | for (GrClipStackFrame* frame = (GrClipStackFrame*) iter.next(); |
| 187 | frame != NULL; |
| 188 | frame = (GrClipStackFrame*) iter.next()) { |
| 189 | frame->reset(); |
| 190 | } |
| 191 | } |
| 192 | |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 193 | private: |
| 194 | struct GrClipStackFrame { |
| 195 | |
| 196 | GrClipStackFrame() { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 197 | this->reset(); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void acquireMask(GrContext* context, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 201 | int32_t clipGenID, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 202 | const GrSurfaceDesc& desc, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 203 | const SkIRect& bound) { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 204 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 205 | fLastClipGenID = clipGenID; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 206 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 207 | // HACK: set the last param to true to indicate that this request is at |
| 208 | // flush time and therefore we require a scratch texture with no pending IO operations. |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 209 | fLastMask.reset(context->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch, |
| 210 | /*flushing=*/true)); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 211 | |
| 212 | fLastBound = bound; |
| 213 | } |
| 214 | |
| 215 | void reset () { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 216 | fLastClipGenID = SkClipStack::kInvalidGenID; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 217 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 218 | GrSurfaceDesc desc; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 219 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 220 | fLastMask.reset(NULL); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 221 | fLastBound.setEmpty(); |
| 222 | } |
| 223 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 224 | int32_t fLastClipGenID; |
| 225 | // The mask's width & height values are used by GrClipMaskManager to correctly scale the |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 226 | // texture coords for the geometry drawn with this mask. TODO: This should be a cache key |
| 227 | // and not a hard ref to a texture. |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 228 | SkAutoTUnref<GrTexture> fLastMask; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 229 | // fLastBound stores the bounding box of the clip mask in clip-stack space. This rect is |
| 230 | // 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] | 231 | SkIRect fLastBound; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | GrContext* fContext; |
| 235 | SkDeque fStack; |
| 236 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 237 | typedef SkNoncopyable INHERITED; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | #endif // GrClipMaskCache_DEFINED |