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 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 12 | DECLARE_SKMESSAGEBUS_MESSAGE(GrPictureDeletedMessage); |
| 13 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 14 | #ifdef SK_DEBUG |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 15 | void GrCachedLayer::validate(const GrTexture* backingTexture) const { |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 16 | SkASSERT(SK_InvalidGenID != fKey.pictureID()); |
| 17 | SkASSERT(fKey.start() > 0 && fKey.stop() > 0); |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 18 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 19 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 20 | if (fTexture) { |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 21 | // If the layer is in some texture then it must occupy some rectangle |
| 22 | SkASSERT(!fRect.isEmpty()); |
| 23 | if (!this->isAtlased()) { |
| 24 | // If it isn't atlased then the rectangle should start at the origin |
| 25 | SkASSERT(0.0f == fRect.fLeft && 0.0f == fRect.fTop); |
| 26 | } |
| 27 | } else { |
| 28 | SkASSERT(fRect.isEmpty()); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 29 | SkASSERT(NULL == fPlot); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 30 | SkASSERT(!fLocked); // layers without a texture cannot be locked |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 31 | } |
| 32 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 33 | if (fPlot) { |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 34 | // If a layer has a plot (i.e., is atlased) then it must point to |
| 35 | // the backing texture. Additionally, its rect should be non-empty. |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 36 | SkASSERT(fTexture && backingTexture == fTexture); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 37 | SkASSERT(!fRect.isEmpty()); |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 38 | } |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 39 | |
| 40 | if (fLocked) { |
| 41 | // If a layer is locked it must have a texture (though it need not be |
| 42 | // the atlas-backing texture) and occupy some space. |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 43 | SkASSERT(fTexture); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 44 | SkASSERT(!fRect.isEmpty()); |
| 45 | } |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | class GrAutoValidateLayer : ::SkNoncopyable { |
| 49 | public: |
| 50 | GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer) |
| 51 | : fBackingTexture(backingTexture) |
| 52 | , fLayer(layer) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 53 | if (fLayer) { |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 54 | fLayer->validate(backingTexture); |
| 55 | } |
| 56 | } |
| 57 | ~GrAutoValidateLayer() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 58 | if (fLayer) { |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 59 | fLayer->validate(fBackingTexture); |
| 60 | } |
| 61 | } |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 62 | void setBackingTexture(GrTexture* backingTexture) { |
| 63 | SkASSERT(NULL == fBackingTexture || fBackingTexture == backingTexture); |
| 64 | fBackingTexture = backingTexture; |
| 65 | } |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 66 | |
| 67 | private: |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 68 | const GrTexture* fBackingTexture; |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 69 | const GrCachedLayer* fLayer; |
| 70 | }; |
| 71 | #endif |
| 72 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 73 | GrLayerCache::GrLayerCache(GrContext* context) |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 74 | : fContext(context) { |
| 75 | this->initAtlas(); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 76 | memset(fPlotLocks, 0, sizeof(fPlotLocks)); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | GrLayerCache::~GrLayerCache() { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 80 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 81 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash); |
| 82 | for (; !iter.done(); ++iter) { |
| 83 | GrCachedLayer* layer = &(*iter); |
| 84 | this->unlock(layer); |
| 85 | SkDELETE(layer); |
| 86 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 87 | |
| 88 | // The atlas only lets go of its texture when the atlas is deleted. |
| 89 | fAtlas.free(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 90 | } |
| 91 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 92 | void GrLayerCache::initAtlas() { |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 93 | SkASSERT(NULL == fAtlas.get()); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 94 | |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 95 | SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight); |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 96 | fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig, |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 97 | kRenderTarget_GrTextureFlagBit, |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 98 | textureSize, kNumPlotsX, kNumPlotsY, false))); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void GrLayerCache::freeAll() { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 102 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 103 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash); |
| 104 | for (; !iter.done(); ++iter) { |
| 105 | GrCachedLayer* layer = &(*iter); |
| 106 | this->unlock(layer); |
| 107 | SkDELETE(layer); |
| 108 | } |
| 109 | fLayerHash.rewind(); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 110 | |
| 111 | // The atlas only lets go of its texture when the atlas is deleted. |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 112 | fAtlas.free(); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 113 | // GrLayerCache always assumes an atlas exists so recreate it. The atlas |
| 114 | // lazily allocates a replacement texture so reallocating a new |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 115 | // atlas here won't disrupt a GrContext::abandonContext or freeGpuResources. |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 116 | // TODO: Make GrLayerCache lazily allocate the atlas manager? |
| 117 | this->initAtlas(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 118 | } |
| 119 | |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 120 | GrCachedLayer* GrLayerCache::createLayer(uint32_t pictureID, |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 121 | int start, int stop, |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 122 | const SkIPoint& offset, |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 123 | const SkMatrix& ctm) { |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 124 | SkASSERT(pictureID != SK_InvalidGenID && start > 0 && stop > 0); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 125 | |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 126 | GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (pictureID, start, stop, offset, ctm)); |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 127 | fLayerHash.add(layer); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 128 | return layer; |
| 129 | } |
| 130 | |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 131 | GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID, |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 132 | int start, int stop, |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 133 | const SkIPoint& offset, |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 134 | const SkMatrix& ctm) { |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 135 | SkASSERT(pictureID != SK_InvalidGenID && start > 0 && stop > 0); |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 136 | return fLayerHash.find(GrCachedLayer::Key(pictureID, start, stop, offset, ctm)); |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 137 | } |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 138 | |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 139 | GrCachedLayer* GrLayerCache::findLayerOrCreate(uint32_t pictureID, |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 140 | int start, int stop, |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 141 | const SkIPoint& offset, |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 142 | const SkMatrix& ctm) { |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 143 | SkASSERT(pictureID != SK_InvalidGenID && start > 0 && stop > 0); |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 144 | GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, start, stop, offset, ctm)); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 145 | if (NULL == layer) { |
robertphillips | 4815fe5 | 2014-09-16 10:32:43 -0700 | [diff] [blame] | 146 | layer = this->createLayer(pictureID, start, stop, offset, ctm); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 147 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 148 | |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 149 | return layer; |
| 150 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 151 | |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 152 | bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool dontAtlas) { |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 153 | SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);) |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 154 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 155 | if (layer->locked()) { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 156 | // This layer is already locked |
| 157 | #ifdef SK_DEBUG |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 158 | if (layer->isAtlased()) { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 159 | // It claims to be atlased |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 160 | SkASSERT(!dontAtlas); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 161 | SkASSERT(layer->rect().width() == desc.fWidth); |
| 162 | SkASSERT(layer->rect().height() == desc.fHeight); |
| 163 | } |
| 164 | #endif |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 165 | return false; |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 166 | } |
| 167 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 168 | if (layer->isAtlased()) { |
| 169 | // Hooray it is still in the atlas - make sure it stays there |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 170 | SkASSERT(!dontAtlas); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 171 | layer->setLocked(true); |
| 172 | fPlotLocks[layer->plot()->id()]++; |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 173 | return false; |
| 174 | } else if (!dontAtlas && PlausiblyAtlasable(desc.fWidth, desc.fHeight)) { |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 175 | // Not in the atlas - will it fit? |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 176 | GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID()); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 177 | if (NULL == pictInfo) { |
| 178 | pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID())); |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 179 | fPictureHash.add(pictInfo); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | SkIPoint16 loc; |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 183 | for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge |
| 184 | GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage, |
| 185 | desc.fWidth, desc.fHeight, |
| 186 | NULL, &loc); |
| 187 | // addToAtlas can allocate the backing texture |
| 188 | SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture())); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 189 | if (plot) { |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 190 | // The layer was successfully added to the atlas |
| 191 | GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY, |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 192 | SkToS16(desc.fWidth), |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 193 | SkToS16(desc.fHeight)); |
| 194 | layer->setTexture(fAtlas->getTexture(), bounds); |
| 195 | layer->setPlot(plot); |
| 196 | layer->setLocked(true); |
| 197 | fPlotLocks[layer->plot()->id()]++; |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 198 | return true; |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // The layer was rejected by the atlas (even though we know it is |
| 202 | // plausibly atlas-able). See if a plot can be purged and try again. |
| 203 | if (!this->purgePlot()) { |
| 204 | break; // We weren't able to purge any plots |
| 205 | } |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 206 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 207 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 208 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 209 | // 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] | 210 | // 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] | 211 | // This can yield a lot of re-rendering |
robertphillips | ed6f03e | 2014-07-30 07:31:35 -0700 | [diff] [blame] | 212 | SkAutoTUnref<GrTexture> tex(fContext->lockAndRefScratchTexture(desc, |
| 213 | GrContext::kApprox_ScratchTexMatch)); |
| 214 | |
| 215 | layer->setTexture(tex, GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight))); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 216 | layer->setLocked(true); |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 217 | return true; |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void GrLayerCache::unlock(GrCachedLayer* layer) { |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 221 | SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);) |
| 222 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 223 | if (NULL == layer || !layer->locked()) { |
| 224 | // invalid or not locked |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 225 | return; |
| 226 | } |
| 227 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 228 | if (layer->isAtlased()) { |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 229 | const int plotID = layer->plot()->id(); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 230 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 231 | SkASSERT(fPlotLocks[plotID] > 0); |
| 232 | fPlotLocks[plotID]--; |
| 233 | // At this point we could aggressively clear out un-locked plots but |
| 234 | // by delaying we may be able to reuse some of the atlased layers later. |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 235 | #if DISABLE_CACHING |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 236 | // This testing code aggressively removes the atlased layers. This |
| 237 | // can be used to separate the performance contribution of less |
| 238 | // render target pingponging from that due to the re-use of cached layers |
| 239 | GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 240 | SkASSERT(pictInfo); |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 241 | |
| 242 | GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot()); |
| 243 | |
| 244 | layer->setPlot(NULL); |
| 245 | layer->setTexture(NULL, GrIRect16::MakeEmpty()); |
| 246 | #endif |
| 247 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 248 | } else { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 249 | fContext->unlockScratchTexture(layer->texture()); |
| 250 | layer->setTexture(NULL, GrIRect16::MakeEmpty()); |
| 251 | } |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 252 | |
| 253 | layer->setLocked(false); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 254 | } |
| 255 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 256 | #ifdef SK_DEBUG |
| 257 | void GrLayerCache::validate() const { |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 258 | int plotLocks[kNumPlotsX * kNumPlotsY]; |
| 259 | memset(plotLocks, 0, sizeof(plotLocks)); |
| 260 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 261 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash); |
| 262 | for (; !iter.done(); ++iter) { |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 263 | const GrCachedLayer* layer = &(*iter); |
| 264 | |
| 265 | layer->validate(fAtlas->getTexture()); |
| 266 | |
| 267 | const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 268 | if (pictInfo) { |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 269 | // In aggressive cleanup mode a picture info should only exist if |
| 270 | // it has some atlased layers |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 271 | #if !DISABLE_CACHING |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 272 | SkASSERT(!pictInfo->fPlotUsage.isEmpty()); |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 273 | #endif |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 274 | } else { |
| 275 | // If there is no picture info for this layer then all of its |
| 276 | // layers should be non-atlased. |
| 277 | SkASSERT(!layer->isAtlased()); |
| 278 | } |
| 279 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 280 | if (layer->plot()) { |
| 281 | SkASSERT(pictInfo); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 282 | SkASSERT(pictInfo->fPictureID == layer->pictureID()); |
| 283 | |
| 284 | SkASSERT(pictInfo->fPlotUsage.contains(layer->plot())); |
| 285 | |
| 286 | if (layer->locked()) { |
| 287 | plotLocks[layer->plot()->id()]++; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) { |
| 293 | SkASSERT(plotLocks[i] == fPlotLocks[i]); |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | class GrAutoValidateCache : ::SkNoncopyable { |
| 298 | public: |
| 299 | explicit GrAutoValidateCache(GrLayerCache* cache) |
| 300 | : fCache(cache) { |
| 301 | fCache->validate(); |
| 302 | } |
| 303 | ~GrAutoValidateCache() { |
| 304 | fCache->validate(); |
| 305 | } |
| 306 | private: |
| 307 | GrLayerCache* fCache; |
| 308 | }; |
| 309 | #endif |
| 310 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 311 | void GrLayerCache::purge(uint32_t pictureID) { |
| 312 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 313 | SkDEBUGCODE(GrAutoValidateCache avc(this);) |
| 314 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 315 | // We need to find all the layers associated with 'picture' and remove them. |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 316 | SkTDArray<GrCachedLayer*> toBeRemoved; |
| 317 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 318 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash); |
| 319 | for (; !iter.done(); ++iter) { |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 320 | if (pictureID == (*iter).pictureID()) { |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 321 | *toBeRemoved.append() = &(*iter); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | for (int i = 0; i < toBeRemoved.count(); ++i) { |
| 326 | this->unlock(toBeRemoved[i]); |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 327 | fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i])); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 328 | SkDELETE(toBeRemoved[i]); |
| 329 | } |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 330 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 331 | GrPictureInfo* pictInfo = fPictureHash.find(pictureID); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 332 | if (pictInfo) { |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 333 | fPictureHash.remove(pictureID); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 334 | SkDELETE(pictInfo); |
| 335 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 336 | } |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 337 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 338 | bool GrLayerCache::purgePlot() { |
| 339 | SkDEBUGCODE(GrAutoValidateCache avc(this);) |
| 340 | |
| 341 | GrAtlas::PlotIter iter; |
| 342 | GrPlot* plot; |
| 343 | for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 344 | plot; |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 345 | plot = iter.prev()) { |
| 346 | if (fPlotLocks[plot->id()] > 0) { |
| 347 | continue; |
| 348 | } |
| 349 | |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 350 | this->purgePlot(plot); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 351 | return true; |
| 352 | } |
| 353 | |
| 354 | return false; |
| 355 | } |
| 356 | |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 357 | void GrLayerCache::purgePlot(GrPlot* plot) { |
| 358 | SkASSERT(0 == fPlotLocks[plot->id()]); |
| 359 | |
| 360 | // We need to find all the layers in 'plot' and remove them. |
| 361 | SkTDArray<GrCachedLayer*> toBeRemoved; |
| 362 | |
| 363 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash); |
| 364 | for (; !iter.done(); ++iter) { |
| 365 | if (plot == (*iter).plot()) { |
| 366 | *toBeRemoved.append() = &(*iter); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | for (int i = 0; i < toBeRemoved.count(); ++i) { |
| 371 | SkASSERT(!toBeRemoved[i]->locked()); |
| 372 | |
| 373 | GrPictureInfo* pictInfo = fPictureHash.find(toBeRemoved[i]->pictureID()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 374 | SkASSERT(pictInfo); |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 375 | |
| 376 | GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot); |
| 377 | |
| 378 | // Aggressively remove layers and, if now totally uncached, picture info |
| 379 | fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i])); |
| 380 | SkDELETE(toBeRemoved[i]); |
| 381 | |
| 382 | if (pictInfo->fPlotUsage.isEmpty()) { |
| 383 | fPictureHash.remove(pictInfo->fPictureID); |
| 384 | SkDELETE(pictInfo); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | plot->resetRects(); |
| 389 | } |
| 390 | |
| 391 | void GrLayerCache::purgeAll() { |
| 392 | GrAtlas::PlotIter iter; |
| 393 | GrPlot* plot; |
| 394 | for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 395 | plot; |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 396 | plot = iter.prev()) { |
| 397 | SkASSERT(0 == fPlotLocks[plot->id()]); |
| 398 | |
| 399 | this->purgePlot(plot); |
| 400 | } |
| 401 | } |
| 402 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 403 | class GrPictureDeletionListener : public SkPicture::DeletionListener { |
| 404 | virtual void onDeletion(uint32_t pictureID) SK_OVERRIDE{ |
| 405 | const GrPictureDeletedMessage message = { pictureID }; |
| 406 | SkMessageBus<GrPictureDeletedMessage>::Post(message); |
| 407 | } |
| 408 | }; |
| 409 | |
| 410 | void GrLayerCache::trackPicture(const SkPicture* picture) { |
| 411 | if (NULL == fDeletionListener) { |
| 412 | fDeletionListener.reset(SkNEW(GrPictureDeletionListener)); |
| 413 | } |
| 414 | |
| 415 | picture->addDeletionListener(fDeletionListener); |
| 416 | } |
| 417 | |
| 418 | void GrLayerCache::processDeletedPictures() { |
| 419 | SkTDArray<GrPictureDeletedMessage> deletedPictures; |
| 420 | fPictDeletionInbox.poll(&deletedPictures); |
| 421 | |
| 422 | for (int i = 0; i < deletedPictures.count(); i++) { |
| 423 | this->purge(deletedPictures[i].pictureID); |
| 424 | } |
| 425 | } |
| 426 | |