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