robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 GrLayerCache_DEFINED |
| 9 | #define GrLayerCache_DEFINED |
| 10 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 11 | #include "GrAtlas.h" |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 12 | #include "GrRect.h" |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 13 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 14 | #include "SkChecksum.h" |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 15 | #include "SkImageFilter.h" |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 16 | #include "SkMessageBus.h" |
robertphillips | 8236591 | 2014-11-12 09:32:34 -0800 | [diff] [blame] | 17 | #include "SkPicture.h" |
robertphillips | 9e6835d | 2014-10-22 05:33:52 -0700 | [diff] [blame] | 18 | #include "SkTDynamicHash.h" |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 19 | |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 20 | // Set to 0 to disable caching of hoisted layers |
| 21 | #define GR_CACHE_HOISTED_LAYERS 0 |
| 22 | |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 23 | // GrPictureInfo stores the atlas plots used by a single picture. A single |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 24 | // plot may be used to store layers from multiple pictures. |
| 25 | struct GrPictureInfo { |
| 26 | public: |
robertphillips | 225a627 | 2014-10-30 11:39:19 -0700 | [diff] [blame] | 27 | static const int kNumPlots = 4; |
| 28 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 29 | // for SkTDynamicHash - just use the pictureID as the hash key |
| 30 | static const uint32_t& GetKey(const GrPictureInfo& pictInfo) { return pictInfo.fPictureID; } |
| 31 | static uint32_t Hash(const uint32_t& key) { return SkChecksum::Mix(key); } |
| 32 | |
| 33 | // GrPictureInfo proper |
robertphillips | 225a627 | 2014-10-30 11:39:19 -0700 | [diff] [blame] | 34 | GrPictureInfo(uint32_t pictureID) : fPictureID(pictureID) { |
| 35 | #if !GR_CACHE_HOISTED_LAYERS |
| 36 | memset(fPlotUses, 0, sizeof(fPlotUses)); |
| 37 | #endif |
| 38 | } |
| 39 | |
| 40 | #if !GR_CACHE_HOISTED_LAYERS |
| 41 | void incPlotUsage(int plotID) { |
| 42 | SkASSERT(plotID < kNumPlots); |
| 43 | fPlotUses[plotID]++; |
| 44 | } |
| 45 | |
| 46 | void decPlotUsage(int plotID) { |
| 47 | SkASSERT(plotID < kNumPlots); |
| 48 | SkASSERT(fPlotUses[plotID] > 0); |
| 49 | fPlotUses[plotID]--; |
| 50 | } |
| 51 | |
| 52 | int plotUsage(int plotID) const { |
| 53 | SkASSERT(plotID < kNumPlots); |
| 54 | return fPlotUses[plotID]; |
| 55 | } |
| 56 | #endif |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 57 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 58 | const uint32_t fPictureID; |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 59 | GrAtlas::ClientPlotUsage fPlotUsage; |
robertphillips | 225a627 | 2014-10-30 11:39:19 -0700 | [diff] [blame] | 60 | |
| 61 | #if !GR_CACHE_HOISTED_LAYERS |
| 62 | private: |
| 63 | int fPlotUses[kNumPlots]; |
| 64 | #endif |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 67 | // GrCachedLayer encapsulates the caching information for a single saveLayer. |
| 68 | // |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 69 | // Atlased layers get a ref to the backing GrTexture while non-atlased layers |
| 70 | // get a ref to the GrTexture in which they reside. In both cases 'fRect' |
| 71 | // contains the layer's extent in its texture. |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 72 | // Atlased layers also get a pointer to the plot in which they reside. |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 73 | // For non-atlased layers, the lock field just corresponds to locking in |
| 74 | // the resource cache. For atlased layers, it implements an additional level |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 75 | // of locking to allow atlased layers to be reused multiple times. |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 76 | struct GrCachedLayer { |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 77 | public: |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 78 | // For SkTDynamicHash |
| 79 | struct Key { |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 80 | Key(uint32_t pictureID, const SkMatrix& initialMat, |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 81 | const unsigned* key, int keySize, bool copyKey = false) |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 82 | : fKeySize(keySize) |
| 83 | , fFreeKey(copyKey) { |
| 84 | fIDMatrix.fPictureID = pictureID; |
| 85 | fIDMatrix.fInitialMat = initialMat; |
| 86 | fIDMatrix.fInitialMat.getType(); // force initialization of type so hashes match |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 87 | |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 88 | if (copyKey) { |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 89 | unsigned* tempKey = SkNEW_ARRAY(unsigned, keySize); |
| 90 | memcpy(tempKey, key, keySize*sizeof(unsigned)); |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 91 | fKey = tempKey; |
| 92 | } else { |
| 93 | fKey = key; |
| 94 | } |
| 95 | |
| 96 | // The pictureID/matrix portion needs to be tightly packed. |
| 97 | GR_STATIC_ASSERT(sizeof(IDMatrix) == sizeof(uint32_t)+ // pictureID |
| 98 | 9 * sizeof(SkScalar) + sizeof(uint32_t)); // matrix |
| 99 | } |
| 100 | |
| 101 | ~Key() { |
| 102 | if (fFreeKey) { |
| 103 | SkDELETE_ARRAY(fKey); |
| 104 | } |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 105 | } |
| 106 | |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 107 | bool operator==(const Key& other) const { |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 108 | if (fKeySize != other.fKeySize) { |
| 109 | return false; |
| 110 | } |
| 111 | return fIDMatrix.fPictureID == other.fIDMatrix.fPictureID && |
| 112 | fIDMatrix.fInitialMat.cheapEqualTo(other.fIDMatrix.fInitialMat) && |
| 113 | !memcmp(fKey, other.fKey, fKeySize * sizeof(int)); |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 114 | } |
| 115 | |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 116 | uint32_t pictureID() const { return fIDMatrix.fPictureID; } |
| 117 | |
| 118 | // TODO: remove these when GrCachedLayer & ReplacementInfo fuse |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 119 | const unsigned* key() const { SkASSERT(fFreeKey); return fKey; } |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 120 | int keySize() const { SkASSERT(fFreeKey); return fKeySize; } |
| 121 | |
| 122 | uint32_t hash() const { |
| 123 | uint32_t hash = SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(fKey), |
| 124 | fKeySize * sizeof(int)); |
| 125 | return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&fIDMatrix), |
| 126 | sizeof(IDMatrix), hash); |
| 127 | } |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 128 | |
| 129 | private: |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 130 | struct IDMatrix { |
| 131 | // ID of the picture of which this layer is a part |
| 132 | uint32_t fPictureID; |
| 133 | // The initial matrix passed into drawPicture |
| 134 | SkMatrix fInitialMat; |
| 135 | } fIDMatrix; |
| 136 | |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 137 | const unsigned* fKey; |
| 138 | const int fKeySize; |
| 139 | bool fFreeKey; |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | static const Key& GetKey(const GrCachedLayer& layer) { return layer.fKey; } |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 143 | static uint32_t Hash(const Key& key) { return key.hash(); } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 144 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 145 | // GrCachedLayer proper |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 146 | GrCachedLayer(uint32_t pictureID, unsigned start, unsigned stop, |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 147 | const SkIRect& srcIR, const SkIRect& dstIR, |
| 148 | const SkMatrix& ctm, |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 149 | const unsigned* key, int keySize, |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 150 | const SkPaint* paint) |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 151 | : fKey(pictureID, ctm, key, keySize, true) |
| 152 | , fStart(start) |
robertphillips | ed42059 | 2014-09-29 11:39:38 -0700 | [diff] [blame] | 153 | , fStop(stop) |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 154 | , fSrcIR(srcIR) |
| 155 | , fDstIR(dstIR) |
| 156 | , fOffset(SkIPoint::Make(0, 0)) |
robertphillips | a0537de | 2014-09-18 08:01:23 -0700 | [diff] [blame] | 157 | , fPaint(paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL) |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 158 | , fFilter(NULL) |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 159 | , fTexture(NULL) |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 160 | , fRect(SkIRect::MakeEmpty()) |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 161 | , fPlot(NULL) |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 162 | , fUses(0) |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 163 | , fLocked(false) { |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 164 | SkASSERT(SK_InvalidGenID != pictureID); |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 165 | |
| 166 | if (fPaint) { |
robertphillips | 95145a9 | 2015-01-14 08:08:21 -0800 | [diff] [blame] | 167 | if (fPaint->getImageFilter()) { |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 168 | fFilter = SkSafeRef(fPaint->getImageFilter()); |
| 169 | fPaint->setImageFilter(NULL); |
| 170 | } |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 171 | } |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 172 | } |
| 173 | |
robertphillips | ed6f03e | 2014-07-30 07:31:35 -0700 | [diff] [blame] | 174 | ~GrCachedLayer() { |
| 175 | SkSafeUnref(fTexture); |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 176 | SkSafeUnref(fFilter); |
robertphillips | a0537de | 2014-09-18 08:01:23 -0700 | [diff] [blame] | 177 | SkDELETE(fPaint); |
robertphillips | ed6f03e | 2014-07-30 07:31:35 -0700 | [diff] [blame] | 178 | } |
| 179 | |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 180 | uint32_t pictureID() const { return fKey.pictureID(); } |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 181 | // TODO: remove these when GrCachedLayer & ReplacementInfo fuse |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 182 | const unsigned* key() const { return fKey.key(); } |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 183 | int keySize() const { return fKey.keySize(); } |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 184 | |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 185 | unsigned start() const { return fStart; } |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 186 | // TODO: make bound debug only |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 187 | const SkIRect& srcIR() const { return fSrcIR; } |
| 188 | const SkIRect& dstIR() const { return fDstIR; } |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 189 | unsigned stop() const { return fStop; } |
| 190 | void setTexture(GrTexture* texture, const SkIRect& rect) { |
robertphillips | ed6f03e | 2014-07-30 07:31:35 -0700 | [diff] [blame] | 191 | SkRefCnt_SafeAssign(fTexture, texture); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 192 | fRect = rect; |
robertphillips | 95145a9 | 2015-01-14 08:08:21 -0800 | [diff] [blame] | 193 | if (!fTexture) { |
| 194 | fLocked = false; |
| 195 | } |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 196 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 197 | GrTexture* texture() { return fTexture; } |
robertphillips | 4aa6dfc | 2014-09-17 07:50:47 -0700 | [diff] [blame] | 198 | const SkPaint* paint() const { return fPaint; } |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 199 | const SkImageFilter* filter() const { return fFilter; } |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 200 | const SkIRect& rect() const { return fRect; } |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 201 | |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 202 | void setOffset(const SkIPoint& offset) { fOffset = offset; } |
| 203 | const SkIPoint& offset() const { return fOffset; } |
| 204 | |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 205 | void setPlot(GrPlot* plot) { |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 206 | SkASSERT(NULL == plot || NULL == fPlot); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 207 | fPlot = plot; |
| 208 | } |
| 209 | GrPlot* plot() { return fPlot; } |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 210 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 211 | bool isAtlased() const { return SkToBool(fPlot); } |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 212 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 213 | void setLocked(bool locked) { fLocked = locked; } |
| 214 | bool locked() const { return fLocked; } |
| 215 | |
| 216 | SkDEBUGCODE(const GrPlot* plot() const { return fPlot; }) |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 217 | SkDEBUGCODE(void validate(const GrTexture* backingTexture) const;) |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 218 | |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 219 | private: |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 220 | const Key fKey; |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 221 | |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 222 | // The "saveLayer" operation index of the cached layer |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 223 | const unsigned fStart; |
robertphillips | ed42059 | 2014-09-29 11:39:38 -0700 | [diff] [blame] | 224 | // The final "restore" operation index of the cached layer |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 225 | const unsigned fStop; |
robertphillips | ed42059 | 2014-09-29 11:39:38 -0700 | [diff] [blame] | 226 | |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 227 | // The layer's src rect (i.e., the portion of the source scene required |
| 228 | // for filtering). |
| 229 | const SkIRect fSrcIR; |
| 230 | // The layer's dest rect (i.e., where it will land in device space) |
| 231 | const SkIRect fDstIR; |
| 232 | // Offset sometimes required by image filters |
| 233 | SkIPoint fOffset; |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 234 | |
robertphillips | 4aa6dfc | 2014-09-17 07:50:47 -0700 | [diff] [blame] | 235 | // The paint used when dropping the layer down into the owning canvas. |
robertphillips | a0537de | 2014-09-18 08:01:23 -0700 | [diff] [blame] | 236 | // Can be NULL. This class makes a copy for itself. |
robertphillips | 7b9e8a4 | 2014-12-11 08:20:31 -0800 | [diff] [blame] | 237 | SkPaint* fPaint; |
| 238 | |
| 239 | // The imagefilter that needs to be applied to the layer prior to it being |
| 240 | // composited with the rest of the scene. |
| 241 | const SkImageFilter* fFilter; |
robertphillips | 4aa6dfc | 2014-09-17 07:50:47 -0700 | [diff] [blame] | 242 | |
skia.committer@gmail.com | d942731 | 2014-04-12 03:05:59 +0000 | [diff] [blame] | 243 | // fTexture is a ref on the atlasing texture for atlased layers and a |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 244 | // ref on a GrTexture for non-atlased textures. |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 245 | GrTexture* fTexture; |
| 246 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 247 | // For both atlased and non-atlased layers 'fRect' contains the bound of |
| 248 | // the layer in whichever texture it resides. It is empty when 'fTexture' |
| 249 | // is NULL. |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 250 | SkIRect fRect; |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 251 | |
| 252 | // For atlased layers, fPlot stores the atlas plot in which the layer rests. |
| 253 | // It is always NULL for non-atlased layers. |
| 254 | GrPlot* fPlot; |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 255 | |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 256 | // The number of actively hoisted layers using this cached image (e.g., |
| 257 | // extant GrHoistedLayers pointing at this object). This object will |
| 258 | // be unlocked when the use count reaches 0. |
| 259 | int fUses; |
| 260 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 261 | // For non-atlased layers 'fLocked' should always match "fTexture". |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 262 | // (i.e., if there is a texture it is locked). |
| 263 | // For atlased layers, 'fLocked' is true if the layer is in a plot and |
| 264 | // actively required for rendering. If the layer is in a plot but not |
| 265 | // actively required for rendering, then 'fLocked' is false. If the |
| 266 | // layer isn't in a plot then is can never be locked. |
| 267 | bool fLocked; |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 268 | |
| 269 | void addUse() { ++fUses; } |
| 270 | void removeUse() { SkASSERT(fUses > 0); --fUses; } |
| 271 | int uses() const { return fUses; } |
| 272 | |
| 273 | friend class GrLayerCache; // for access to usage methods |
| 274 | friend class TestingAccess; // for testing |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | // The GrLayerCache caches pre-computed saveLayers for later rendering. |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 278 | // Non-atlased layers are stored in their own GrTexture while the atlased |
| 279 | // layers share a single GrTexture. |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 280 | // Unlike the GrFontCache, the GrTexture atlas only has one GrAtlas (for 8888) |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 281 | // and one GrPlot (for the entire atlas). As such, the GrLayerCache |
| 282 | // roughly combines the functionality of the GrFontCache and GrTextStrike |
| 283 | // classes. |
| 284 | class GrLayerCache { |
| 285 | public: |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 286 | GrLayerCache(GrContext*); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 287 | ~GrLayerCache(); |
| 288 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 289 | // As a cache, the GrLayerCache can be ordered to free up all its cached |
| 290 | // elements by the GrContext |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 291 | void freeAll(); |
| 292 | |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 293 | GrCachedLayer* findLayer(uint32_t pictureID, const SkMatrix& ctm, |
| 294 | const unsigned* key, int keySize); |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 295 | GrCachedLayer* findLayerOrCreate(uint32_t pictureID, |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 296 | int start, int stop, |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 297 | const SkIRect& srcIR, |
| 298 | const SkIRect& dstIR, |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 299 | const SkMatrix& initialMat, |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 300 | const unsigned* key, int keySize, |
robertphillips | 4aa6dfc | 2014-09-17 07:50:47 -0700 | [diff] [blame] | 301 | const SkPaint* paint); |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 302 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 303 | // Attempt to place 'layer' in the atlas. Return true on success; false on failure. |
| 304 | // When true is returned, 'needsRendering' will indicate if the layer must be (re)drawn. |
| 305 | // Additionally, the GPU resources will be locked. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 306 | bool tryToAtlas(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 307 | |
| 308 | // Attempt to lock the GPU resources required for a layer. Return true on success; |
| 309 | // false on failure. When true is returned 'needsRendering' will indicate if the |
| 310 | // layer must be (re)drawn. |
| 311 | // Note that atlased layers should already have been locked and rendered so only |
| 312 | // free floating layers will have 'needsRendering' set. |
| 313 | // Currently, this path always uses a new scratch texture for non-Atlased layers |
| 314 | // and (thus) doesn't cache anything. This can yield a lot of re-rendering. |
| 315 | // TODO: allow rediscovery of free-floating layers that are still in the resource cache. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 316 | bool lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering); |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 317 | |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 318 | // addUse is just here to keep the API symmetric |
| 319 | void addUse(GrCachedLayer* layer) { layer->addUse(); } |
| 320 | void removeUse(GrCachedLayer* layer) { |
| 321 | layer->removeUse(); |
| 322 | if (layer->uses() == 0) { |
| 323 | // If no one cares about the layer allow it to be recycled. |
| 324 | this->unlock(layer); |
| 325 | } |
| 326 | } |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 327 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 328 | // Cleanup after any SkPicture deletions |
| 329 | void processDeletedPictures(); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 330 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 331 | SkDEBUGCODE(void validate() const;) |
| 332 | |
robertphillips | 84ac082 | 2014-10-14 07:07:59 -0700 | [diff] [blame] | 333 | #ifdef SK_DEVELOPER |
| 334 | void writeLayersToDisk(const SkString& dirName); |
| 335 | #endif |
| 336 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 337 | static bool PlausiblyAtlasable(int width, int height) { |
| 338 | return width <= kPlotWidth && height <= kPlotHeight; |
| 339 | } |
| 340 | |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 341 | #if !GR_CACHE_HOISTED_LAYERS |
| 342 | void purgeAll(); |
| 343 | #endif |
| 344 | |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 345 | private: |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 346 | static const int kAtlasTextureWidth = 1024; |
| 347 | static const int kAtlasTextureHeight = 1024; |
| 348 | |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 349 | static const int kNumPlotsX = 2; |
| 350 | static const int kNumPlotsY = 2; |
| 351 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 352 | static const int kPlotWidth = kAtlasTextureWidth / kNumPlotsX; |
| 353 | static const int kPlotHeight = kAtlasTextureHeight / kNumPlotsY; |
| 354 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 355 | GrContext* fContext; // pointer back to owning context |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 356 | SkAutoTDelete<GrAtlas> fAtlas; // TODO: could lazily allocate |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 357 | |
| 358 | // We cache this information here (rather then, say, on the owning picture) |
| 359 | // because we want to be able to clean it up as needed (e.g., if a picture |
| 360 | // is leaked and never cleans itself up we still want to be able to |
| 361 | // remove the GrPictureInfo once its layers are purged from all the atlas |
| 362 | // plots). |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 363 | SkTDynamicHash<GrPictureInfo, uint32_t> fPictureHash; |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 364 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 365 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key> fLayerHash; |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 366 | |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 367 | SkMessageBus<SkPicture::DeletionMessage>::Inbox fPictDeletionInbox; |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 368 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 369 | // This implements a plot-centric locking mechanism (since the atlas |
| 370 | // backing texture is always locked). Each layer that is locked (i.e., |
| 371 | // needed for the current rendering) in a plot increments the plot lock |
robertphillips | a32c6bc | 2014-10-09 12:47:01 -0700 | [diff] [blame] | 372 | // count for that plot. Similarly, once a rendering is complete all the |
| 373 | // layers used in it decrement the lock count for the used plots. |
| 374 | // Plots with a 0 lock count are open for recycling/purging. |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 375 | int fPlotLocks[kNumPlotsX * kNumPlotsY]; |
| 376 | |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 377 | // Inform the cache that layer's cached image is not currently required |
| 378 | void unlock(GrCachedLayer* layer); |
| 379 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 380 | void initAtlas(); |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 381 | GrCachedLayer* createLayer(uint32_t pictureID, int start, int stop, |
robertphillips | 478dd72 | 2014-12-16 08:25:55 -0800 | [diff] [blame] | 382 | const SkIRect& srcIR, const SkIRect& dstIR, |
| 383 | const SkMatrix& initialMat, |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame] | 384 | const unsigned* key, int keySize, |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 385 | const SkPaint* paint); |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 386 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 387 | // Remove all the layers (and unlock any resources) associated with 'pictureID' |
| 388 | void purge(uint32_t pictureID); |
| 389 | |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 390 | void purgePlot(GrPlot* plot); |
| 391 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 392 | // Try to find a purgeable plot and clear it out. Return true if a plot |
| 393 | // was purged; false otherwise. |
| 394 | bool purgePlot(); |
| 395 | |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 396 | void incPlotLock(int plotIdx) { ++fPlotLocks[plotIdx]; } |
| 397 | void decPlotLock(int plotIdx) { |
| 398 | SkASSERT(fPlotLocks[plotIdx] > 0); |
| 399 | --fPlotLocks[plotIdx]; |
| 400 | } |
| 401 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 402 | // for testing |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 403 | friend class TestingAccess; |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 404 | int numLayers() const { return fLayerHash.count(); } |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 405 | }; |
| 406 | |
| 407 | #endif |