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 | |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 11 | #include "GrResourceProvider.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: |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 23 | GrClipMaskCache(GrResourceProvider*); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 24 | |
| 25 | ~GrClipMaskCache() { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 26 | while (!fStack.empty()) { |
| 27 | GrClipStackFrame* temp = (GrClipStackFrame*) fStack.back(); |
| 28 | temp->~GrClipStackFrame(); |
| 29 | fStack.pop_back(); |
| 30 | } |
| 31 | } |
| 32 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 33 | bool canReuse(int32_t clipGenID, const SkIRect& bounds) { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 34 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 35 | SkASSERT(clipGenID != SkClipStack::kWideOpenGenID); |
| 36 | SkASSERT(clipGenID != SkClipStack::kEmptyGenID); |
| 37 | |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 38 | GrClipStackFrame* back = (GrClipStackFrame*) fStack.back(); |
| 39 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 40 | // We could reuse the mask if bounds is a subset of last bounds. We'd have to communicate |
| 41 | // an offset to the caller. |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 42 | if (back->fLastMask && |
| 43 | !back->fLastMask->wasDestroyed() && |
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 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 100 | return back->fLastMask; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 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 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 112 | return back->fLastMask; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 113 | } |
| 114 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 115 | void acquireMask(int32_t clipGenID, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 116 | const GrSurfaceDesc& 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 | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 126 | back->acquireMask(fResourceProvider, 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 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 138 | if (NULL == back->fLastMask) { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 139 | return -1; |
| 140 | } |
| 141 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 142 | return back->fLastMask->width(); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 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 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 154 | if (NULL == back->fLastMask) { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 155 | return -1; |
| 156 | } |
| 157 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 158 | return back->fLastMask->height(); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 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 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 174 | // TODO: Remove this when we hold cache keys instead of refs to textures. |
| 175 | void purgeResources() { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 176 | SkDeque::F2BIter iter(fStack); |
| 177 | for (GrClipStackFrame* frame = (GrClipStackFrame*) iter.next(); |
| 178 | frame != NULL; |
| 179 | frame = (GrClipStackFrame*) iter.next()) { |
| 180 | frame->reset(); |
| 181 | } |
| 182 | } |
| 183 | |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 184 | private: |
| 185 | struct GrClipStackFrame { |
| 186 | |
| 187 | GrClipStackFrame() { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 188 | this->reset(); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 189 | } |
| 190 | |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 191 | void acquireMask(GrResourceProvider* resourceProvider, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 192 | int32_t clipGenID, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 193 | const GrSurfaceDesc& desc, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 194 | const SkIRect& bound) { |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 195 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 196 | fLastClipGenID = clipGenID; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 197 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 198 | // TODO: Determine if we really need the NoPendingIO flag anymore. |
| 199 | // (http://skbug.com/4156) |
| 200 | static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag; |
| 201 | fLastMask.reset(resourceProvider->createApproxTexture(desc, kFlags)); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 202 | |
| 203 | fLastBound = bound; |
| 204 | } |
| 205 | |
| 206 | void reset () { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 207 | fLastClipGenID = SkClipStack::kInvalidGenID; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 208 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 209 | GrSurfaceDesc desc; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 210 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 211 | fLastMask.reset(NULL); |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 212 | fLastBound.setEmpty(); |
| 213 | } |
| 214 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 215 | int32_t fLastClipGenID; |
| 216 | // 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] | 217 | // texture coords for the geometry drawn with this mask. TODO: This should be a cache key |
| 218 | // and not a hard ref to a texture. |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 219 | SkAutoTUnref<GrTexture> fLastMask; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 220 | // fLastBound stores the bounding box of the clip mask in clip-stack space. This rect is |
| 221 | // 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] | 222 | SkIRect fLastBound; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 225 | SkDeque fStack; |
| 226 | GrResourceProvider* fResourceProvider; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 227 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 228 | typedef SkNoncopyable INHERITED; |
robertphillips@google.com | 1fcc1b8 | 2012-08-29 12:52:05 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | #endif // GrClipMaskCache_DEFINED |