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 | #include "GrAtlas.h" |
| 9 | #include "GrGpu.h" |
| 10 | #include "GrLayerCache.h" |
| 11 | |
| 12 | /** |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 13 | * PictureLayerKey just wraps a saveLayer's id in a picture for GrTHashTable. |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 14 | */ |
| 15 | class GrLayerCache::PictureLayerKey { |
| 16 | public: |
skia.committer@gmail.com | a915772 | 2014-04-03 03:04:26 +0000 | [diff] [blame] | 17 | PictureLayerKey(uint32_t pictureID, int layerID) |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 18 | : fPictureID(pictureID) |
| 19 | , fLayerID(layerID) { |
| 20 | } |
| 21 | |
| 22 | uint32_t pictureID() const { return fPictureID; } |
| 23 | int layerID() const { return fLayerID; } |
| 24 | |
| 25 | uint32_t getHash() const { return (fPictureID << 16) | fLayerID; } |
| 26 | |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 27 | static bool LessThan(const GrCachedLayer& layer, const PictureLayerKey& key) { |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 28 | if (layer.pictureID() == key.pictureID()) { |
| 29 | return layer.layerID() < key.layerID(); |
| 30 | } |
| 31 | |
| 32 | return layer.pictureID() < key.pictureID(); |
| 33 | } |
| 34 | |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 35 | static bool Equals(const GrCachedLayer& layer, const PictureLayerKey& key) { |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 36 | return layer.pictureID() == key.pictureID() && layer.layerID() == key.layerID(); |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | uint32_t fPictureID; |
| 41 | int fLayerID; |
| 42 | }; |
| 43 | |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame^] | 44 | /** |
| 45 | * PictureKey just wraps a picture's unique ID for GrTHashTable. It is used to |
| 46 | * look up a picture's GrPictureInfo (i.e., its GrPlot usage). |
| 47 | */ |
| 48 | class GrLayerCache::PictureKey { |
| 49 | public: |
| 50 | PictureKey(uint32_t pictureID) : fPictureID(pictureID) { } |
| 51 | |
| 52 | uint32_t pictureID() const { return fPictureID; } |
| 53 | |
| 54 | uint32_t getHash() const { return fPictureID; } |
| 55 | |
| 56 | static bool LessThan(const GrPictureInfo& pictInfo, const PictureKey& key) { |
| 57 | return pictInfo.fPictureID < key.pictureID(); |
| 58 | } |
| 59 | |
| 60 | static bool Equals(const GrPictureInfo& pictInfo, const PictureKey& key) { |
| 61 | return pictInfo.fPictureID == key.pictureID(); |
| 62 | |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | uint32_t fPictureID; |
| 67 | }; |
| 68 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 69 | #ifdef SK_DEBUG |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame^] | 70 | void GrCachedLayer::validate(const GrTexture* backingTexture) const { |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 71 | SkASSERT(SK_InvalidGenID != fPictureID); |
| 72 | SkASSERT(-1 != fLayerID); |
| 73 | |
| 74 | if (NULL != fTexture) { |
| 75 | // If the layer is in some texture then it must occupy some rectangle |
| 76 | SkASSERT(!fRect.isEmpty()); |
| 77 | if (!this->isAtlased()) { |
| 78 | // If it isn't atlased then the rectangle should start at the origin |
| 79 | SkASSERT(0.0f == fRect.fLeft && 0.0f == fRect.fTop); |
| 80 | } |
| 81 | } else { |
| 82 | SkASSERT(fRect.isEmpty()); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame^] | 83 | SkASSERT(NULL == fPlot); |
| 84 | } |
| 85 | |
| 86 | if (NULL != fPlot) { |
| 87 | // If a layer has a plot (i.e., is atlased) then it must point to |
| 88 | // the backing texture. Additionally, its rect should be non-empty. |
| 89 | SkASSERT(NULL != fTexture && backingTexture == fTexture); |
| 90 | SkASSERT(!fRect.isEmpty()); |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | class GrAutoValidateLayer : ::SkNoncopyable { |
| 95 | public: |
| 96 | GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer) |
| 97 | : fBackingTexture(backingTexture) |
| 98 | , fLayer(layer) { |
| 99 | if (NULL != fLayer) { |
| 100 | fLayer->validate(backingTexture); |
| 101 | } |
| 102 | } |
| 103 | ~GrAutoValidateLayer() { |
| 104 | if (NULL != fLayer) { |
| 105 | fLayer->validate(fBackingTexture); |
| 106 | } |
| 107 | } |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame^] | 108 | void setBackingTexture(GrTexture* backingTexture) { |
| 109 | SkASSERT(NULL == fBackingTexture || fBackingTexture == backingTexture); |
| 110 | fBackingTexture = backingTexture; |
| 111 | } |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 112 | |
| 113 | private: |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame^] | 114 | const GrTexture* fBackingTexture; |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 115 | const GrCachedLayer* fLayer; |
| 116 | }; |
| 117 | #endif |
| 118 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 119 | GrLayerCache::GrLayerCache(GrContext* context) |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 120 | : fContext(context) { |
| 121 | this->initAtlas(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | GrLayerCache::~GrLayerCache() { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 125 | SkTDArray<GrCachedLayer*>& layerArray = fLayerHash.getArray(); |
| 126 | for (int i = 0; i < fLayerHash.count(); ++i) { |
| 127 | this->unlock(layerArray[i]); |
| 128 | } |
| 129 | |
| 130 | fLayerHash.deleteAll(); |
| 131 | |
| 132 | // The atlas only lets go of its texture when the atlas is deleted. |
| 133 | fAtlas.free(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 134 | } |
| 135 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 136 | void GrLayerCache::initAtlas() { |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 137 | static const int kAtlasTextureWidth = 1024; |
| 138 | static const int kAtlasTextureHeight = 1024; |
| 139 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 140 | SkASSERT(NULL == fAtlas.get()); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 141 | |
| 142 | // The layer cache only gets 1 plot |
| 143 | SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight); |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 144 | fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig, |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 145 | kRenderTarget_GrTextureFlagBit, |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame^] | 146 | textureSize, kNumPlotsX, kNumPlotsY, false))); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void GrLayerCache::freeAll() { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 150 | SkTDArray<GrCachedLayer*>& layerArray = fLayerHash.getArray(); |
| 151 | for (int i = 0; i < fLayerHash.count(); ++i) { |
| 152 | this->unlock(layerArray[i]); |
| 153 | } |
| 154 | |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 155 | fLayerHash.deleteAll(); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 156 | |
| 157 | // The atlas only lets go of its texture when the atlas is deleted. |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 158 | fAtlas.free(); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 159 | // GrLayerCache always assumes an atlas exists so recreate it. The atlas |
| 160 | // lazily allocates a replacement texture so reallocating a new |
| 161 | // atlas here won't disrupt a GrContext::contextDestroyed or freeGpuResources. |
| 162 | // TODO: Make GrLayerCache lazily allocate the atlas manager? |
| 163 | this->initAtlas(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 164 | } |
| 165 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 166 | GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID) { |
robertphillips | e462f2b | 2014-06-29 17:16:27 -0700 | [diff] [blame] | 167 | SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 168 | |
| 169 | GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (picture->uniqueID(), layerID)); |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 170 | fLayerHash.insert(PictureLayerKey(picture->uniqueID(), layerID), layer); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 171 | return layer; |
| 172 | } |
| 173 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 174 | GrCachedLayer* GrLayerCache::findLayer(const SkPicture* picture, int layerID) { |
| 175 | SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
| 176 | return fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID)); |
| 177 | } |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 178 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 179 | GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int layerID) { |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 180 | SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 181 | GrCachedLayer* layer = fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID)); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 182 | if (NULL == layer) { |
| 183 | layer = this->createLayer(picture, layerID); |
| 184 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 185 | |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 186 | return layer; |
| 187 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 188 | |
| 189 | bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc) { |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 190 | SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);) |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 191 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 192 | if (NULL != layer->texture()) { |
| 193 | // This layer is already locked |
| 194 | #ifdef SK_DEBUG |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 195 | if (layer->isAtlased()) { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 196 | // It claims to be atlased |
| 197 | SkASSERT(layer->rect().width() == desc.fWidth); |
| 198 | SkASSERT(layer->rect().height() == desc.fHeight); |
| 199 | } |
| 200 | #endif |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | #if USE_ATLAS |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame^] | 205 | { |
| 206 | GrPictureInfo* pictInfo = fPictureHash.find(PictureKey(layer->pictureID())); |
| 207 | if (NULL == pictInfo) { |
| 208 | pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID())); |
| 209 | fPictureHash.insert(PictureKey(layer->pictureID()), pictInfo); |
| 210 | } |
| 211 | |
| 212 | SkIPoint16 loc; |
| 213 | GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage, |
| 214 | desc.fWidth, desc.fHeight, |
| 215 | NULL, &loc); |
| 216 | // addToAtlas can allocate the backing texture |
| 217 | SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture())); |
| 218 | if (NULL != plot) { |
| 219 | GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY, |
| 220 | SkToS16(desc.fWidth), SkToS16(desc.fHeight)); |
| 221 | layer->setTexture(fAtlas->getTexture(), bounds); |
| 222 | layer->setPlot(plot); |
| 223 | return false; |
| 224 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 225 | } |
| 226 | #endif |
| 227 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 228 | // The texture wouldn't fit in the cache - give it it's own texture. |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 229 | // This path always uses a new scratch texture and (thus) doesn't cache anything. |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 230 | // This can yield a lot of re-rendering |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 231 | layer->setTexture(fContext->lockAndRefScratchTexture(desc, GrContext::kApprox_ScratchTexMatch), |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 232 | GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight))); |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 233 | return false; |
| 234 | } |
| 235 | |
| 236 | void GrLayerCache::unlock(GrCachedLayer* layer) { |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 237 | SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);) |
| 238 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 239 | if (NULL == layer || NULL == layer->texture()) { |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 243 | if (layer->isAtlased()) { |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame^] | 244 | SkASSERT(layer->texture() == fAtlas->getTexture()); |
| 245 | |
| 246 | GrPictureInfo* pictInfo = fPictureHash.find(PictureKey(layer->pictureID())); |
| 247 | SkASSERT(NULL != pictInfo); |
| 248 | pictInfo->fPlotUsage.isEmpty(); // just to silence compiler warnings for the time being |
| 249 | |
| 250 | // TODO: purging from atlas goes here |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 251 | } else { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 252 | fContext->unlockScratchTexture(layer->texture()); |
| 253 | layer->setTexture(NULL, GrIRect16::MakeEmpty()); |
| 254 | } |
| 255 | } |
| 256 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 257 | #ifdef SK_DEBUG |
| 258 | void GrLayerCache::validate() const { |
| 259 | const SkTDArray<GrCachedLayer*>& layerArray = fLayerHash.getArray(); |
| 260 | for (int i = 0; i < fLayerHash.count(); ++i) { |
| 261 | layerArray[i]->validate(fAtlas->getTexture()); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | class GrAutoValidateCache : ::SkNoncopyable { |
| 266 | public: |
| 267 | explicit GrAutoValidateCache(GrLayerCache* cache) |
| 268 | : fCache(cache) { |
| 269 | fCache->validate(); |
| 270 | } |
| 271 | ~GrAutoValidateCache() { |
| 272 | fCache->validate(); |
| 273 | } |
| 274 | private: |
| 275 | GrLayerCache* fCache; |
| 276 | }; |
| 277 | #endif |
| 278 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 279 | void GrLayerCache::purge(const SkPicture* picture) { |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 280 | SkDEBUGCODE(GrAutoValidateCache avc(this);) |
| 281 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 282 | // This is somewhat of an abuse of GrTHashTable. We need to find all the |
| 283 | // layers associated with 'picture' but the usual hash calls only look for |
| 284 | // exact key matches. This code peeks into the hash table's innards to |
| 285 | // find all the 'picture'-related layers. |
| 286 | // TODO: use a different data structure for the layer hash? |
| 287 | SkTDArray<GrCachedLayer*> toBeRemoved; |
| 288 | |
| 289 | const SkTDArray<GrCachedLayer*>& layerArray = fLayerHash.getArray(); |
| 290 | for (int i = 0; i < fLayerHash.count(); ++i) { |
| 291 | if (picture->uniqueID() == layerArray[i]->pictureID()) { |
| 292 | *toBeRemoved.append() = layerArray[i]; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | for (int i = 0; i < toBeRemoved.count(); ++i) { |
| 297 | this->unlock(toBeRemoved[i]); |
| 298 | |
| 299 | PictureLayerKey key(picture->uniqueID(), toBeRemoved[i]->layerID()); |
| 300 | fLayerHash.remove(key, toBeRemoved[i]); |
| 301 | SkDELETE(toBeRemoved[i]); |
| 302 | } |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame^] | 303 | |
| 304 | GrPictureInfo* pictInfo = fPictureHash.find(PictureKey(picture->uniqueID())); |
| 305 | if (NULL != pictInfo) { |
| 306 | fPictureHash.remove(PictureKey(picture->uniqueID()), pictInfo); |
| 307 | SkDELETE(pictInfo); |
| 308 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 309 | } |