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