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" |
robertphillips | 84ac082 | 2014-10-14 07:07:59 -0700 | [diff] [blame] | 11 | #include "GrSurfacePriv.h" |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 12 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 13 | #ifdef SK_DEBUG |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 14 | void GrCachedLayer::validate(const GrTexture* backingTexture) const { |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 15 | SkASSERT(SK_InvalidGenID != fKey.pictureID()); |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 16 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 17 | if (fTexture) { |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 18 | // If the layer is in some texture then it must occupy some rectangle |
| 19 | SkASSERT(!fRect.isEmpty()); |
| 20 | if (!this->isAtlased()) { |
| 21 | // If it isn't atlased then the rectangle should start at the origin |
| 22 | SkASSERT(0.0f == fRect.fLeft && 0.0f == fRect.fTop); |
| 23 | } |
| 24 | } else { |
| 25 | SkASSERT(fRect.isEmpty()); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 26 | SkASSERT(NULL == fPlot); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 27 | SkASSERT(!fLocked); // layers without a texture cannot be locked |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 28 | } |
| 29 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 30 | if (fPlot) { |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 31 | // If a layer has a plot (i.e., is atlased) then it must point to |
| 32 | // the backing texture. Additionally, its rect should be non-empty. |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 33 | SkASSERT(fTexture && backingTexture == fTexture); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 34 | SkASSERT(!fRect.isEmpty()); |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 35 | } |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 36 | |
| 37 | if (fLocked) { |
| 38 | // If a layer is locked it must have a texture (though it need not be |
| 39 | // the atlas-backing texture) and occupy some space. |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 40 | SkASSERT(fTexture); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 41 | SkASSERT(!fRect.isEmpty()); |
| 42 | } |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 43 | |
| 44 | // Unfortunately there is a brief time where a layer can be locked |
| 45 | // but not used, so we can only check the "used implies locked" |
| 46 | // invariant. |
| 47 | if (fUses > 0) { |
| 48 | SkASSERT(fLocked); |
| 49 | } else { |
| 50 | SkASSERT(0 == fUses); |
| 51 | } |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | class GrAutoValidateLayer : ::SkNoncopyable { |
| 55 | public: |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 56 | GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer) |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 57 | : fBackingTexture(backingTexture) |
| 58 | , fLayer(layer) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 59 | if (fLayer) { |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 60 | fLayer->validate(backingTexture); |
| 61 | } |
| 62 | } |
| 63 | ~GrAutoValidateLayer() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 64 | if (fLayer) { |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 65 | fLayer->validate(fBackingTexture); |
| 66 | } |
| 67 | } |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 68 | void setBackingTexture(GrTexture* backingTexture) { |
| 69 | SkASSERT(NULL == fBackingTexture || fBackingTexture == backingTexture); |
| 70 | fBackingTexture = backingTexture; |
| 71 | } |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 72 | |
| 73 | private: |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 74 | const GrTexture* fBackingTexture; |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 75 | const GrCachedLayer* fLayer; |
| 76 | }; |
| 77 | #endif |
| 78 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 79 | GrLayerCache::GrLayerCache(GrContext* context) |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 80 | : fContext(context) { |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 81 | memset(fPlotLocks, 0, sizeof(fPlotLocks)); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | GrLayerCache::~GrLayerCache() { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 85 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 86 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash); |
| 87 | for (; !iter.done(); ++iter) { |
| 88 | GrCachedLayer* layer = &(*iter); |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 89 | SkASSERT(0 == layer->uses()); |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 90 | this->unlock(layer); |
| 91 | SkDELETE(layer); |
| 92 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 93 | |
robertphillips | b32f0ad | 2014-11-04 06:46:11 -0800 | [diff] [blame] | 94 | SkASSERT(0 == fPictureHash.count()); |
| 95 | |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 96 | // The atlas only lets go of its texture when the atlas is deleted. |
| 97 | fAtlas.free(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 98 | } |
| 99 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 100 | void GrLayerCache::initAtlas() { |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 101 | SkASSERT(NULL == fAtlas.get()); |
robertphillips | 225a627 | 2014-10-30 11:39:19 -0700 | [diff] [blame] | 102 | GR_STATIC_ASSERT(kNumPlotsX*kNumPlotsX == GrPictureInfo::kNumPlots); |
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, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 106 | kRenderTarget_GrSurfaceFlag, |
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 | |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 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@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 122 | } |
| 123 | |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 124 | GrCachedLayer* GrLayerCache::createLayer(uint32_t pictureID, |
| 125 | int start, int stop, |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 126 | const SkIRect& bounds, |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 127 | const SkMatrix& initialMat, |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame^] | 128 | const unsigned* key, |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 129 | int keySize, |
robertphillips | 4aa6dfc | 2014-09-17 07:50:47 -0700 | [diff] [blame] | 130 | const SkPaint* paint) { |
robertphillips | 410dd05 | 2014-10-06 12:19:50 -0700 | [diff] [blame] | 131 | SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 132 | |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 133 | GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (pictureID, start, stop, bounds, initialMat, |
| 134 | key, keySize, paint)); |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 135 | fLayerHash.add(layer); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 136 | return layer; |
| 137 | } |
| 138 | |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 139 | GrCachedLayer* GrLayerCache::findLayer(uint32_t pictureID, const SkMatrix& initialMat, |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame^] | 140 | const unsigned* key, int keySize) { |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 141 | SkASSERT(pictureID != SK_InvalidGenID); |
| 142 | return fLayerHash.find(GrCachedLayer::Key(pictureID, initialMat, key, keySize)); |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 143 | } |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 144 | |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 145 | GrCachedLayer* GrLayerCache::findLayerOrCreate(uint32_t pictureID, |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 146 | int start, int stop, |
robertphillips | 3aac6e0 | 2014-10-20 08:52:40 -0700 | [diff] [blame] | 147 | const SkIRect& bounds, |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 148 | const SkMatrix& initialMat, |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame^] | 149 | const unsigned* key, |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 150 | int keySize, |
robertphillips | 4aa6dfc | 2014-09-17 07:50:47 -0700 | [diff] [blame] | 151 | const SkPaint* paint) { |
robertphillips | 410dd05 | 2014-10-06 12:19:50 -0700 | [diff] [blame] | 152 | SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0); |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 153 | GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, initialMat, key, keySize)); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 154 | if (NULL == layer) { |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 155 | layer = this->createLayer(pictureID, start, stop, bounds, initialMat, key, keySize, paint); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 156 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 157 | |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 158 | return layer; |
| 159 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 160 | |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 161 | bool GrLayerCache::tryToAtlas(GrCachedLayer* layer, |
| 162 | const GrSurfaceDesc& desc, |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 163 | bool* needsRendering) { |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 164 | SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);) |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 165 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 166 | SkASSERT(PlausiblyAtlasable(desc.fWidth, desc.fHeight)); |
robertphillips | a63f32e | 2014-11-10 08:10:42 -0800 | [diff] [blame] | 167 | SkASSERT(0 == desc.fSampleCnt); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 168 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 169 | if (layer->locked()) { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 170 | // This layer is already locked |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 171 | SkASSERT(fAtlas); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 172 | SkASSERT(layer->isAtlased()); |
| 173 | SkASSERT(layer->rect().width() == desc.fWidth); |
| 174 | SkASSERT(layer->rect().height() == desc.fHeight); |
| 175 | *needsRendering = false; |
| 176 | return true; |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 177 | } |
| 178 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 179 | if (layer->isAtlased()) { |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 180 | SkASSERT(fAtlas); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 181 | // Hooray it is still in the atlas - make sure it stays there |
| 182 | layer->setLocked(true); |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 183 | this->incPlotLock(layer->plot()->id()); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 184 | *needsRendering = false; |
| 185 | return true; |
| 186 | } else { |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 187 | if (!fAtlas) { |
| 188 | this->initAtlas(); |
| 189 | if (!fAtlas) { |
| 190 | return false; |
| 191 | } |
| 192 | } |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 193 | // Not in the atlas - will it fit? |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 194 | GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID()); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 195 | if (NULL == pictInfo) { |
| 196 | pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID())); |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 197 | fPictureHash.add(pictInfo); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | SkIPoint16 loc; |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 201 | for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge |
| 202 | GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage, |
| 203 | desc.fWidth, desc.fHeight, |
| 204 | NULL, &loc); |
| 205 | // addToAtlas can allocate the backing texture |
| 206 | SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture())); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 207 | if (plot) { |
robertphillips | 225a627 | 2014-10-30 11:39:19 -0700 | [diff] [blame] | 208 | #if !GR_CACHE_HOISTED_LAYERS |
| 209 | pictInfo->incPlotUsage(plot->id()); |
| 210 | #endif |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 211 | // The layer was successfully added to the atlas |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame^] | 212 | const SkIRect bounds = SkIRect::MakeXYWH(loc.fX, loc.fY, |
| 213 | desc.fWidth, desc.fHeight); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 214 | layer->setTexture(fAtlas->getTexture(), bounds); |
| 215 | layer->setPlot(plot); |
| 216 | layer->setLocked(true); |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 217 | this->incPlotLock(layer->plot()->id()); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 218 | *needsRendering = true; |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 219 | return true; |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 220 | } |
| 221 | |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 222 | // The layer was rejected by the atlas (even though we know it is |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 223 | // plausibly atlas-able). See if a plot can be purged and try again. |
| 224 | if (!this->purgePlot()) { |
| 225 | break; // We weren't able to purge any plots |
| 226 | } |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 227 | } |
robertphillips | bc0c4bd | 2014-12-01 11:39:59 -0800 | [diff] [blame] | 228 | |
| 229 | if (pictInfo->fPlotUsage.isEmpty()) { |
| 230 | fPictureHash.remove(pictInfo->fPictureID); |
| 231 | SkDELETE(pictInfo); |
| 232 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 233 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 234 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 235 | return false; |
| 236 | } |
| 237 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 238 | bool GrLayerCache::lock(GrCachedLayer* layer, const GrSurfaceDesc& desc, bool* needsRendering) { |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 239 | if (layer->locked()) { |
| 240 | // This layer is already locked |
| 241 | *needsRendering = false; |
| 242 | return true; |
| 243 | } |
| 244 | |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 245 | SkAutoTUnref<GrTexture> tex( |
| 246 | fContext->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch)); |
robertphillips | ed6f03e | 2014-07-30 07:31:35 -0700 | [diff] [blame] | 247 | |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 248 | if (!tex) { |
| 249 | return false; |
| 250 | } |
| 251 | |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame^] | 252 | layer->setTexture(tex, SkIRect::MakeWH(desc.fWidth, desc.fHeight)); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 253 | layer->setLocked(true); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 254 | *needsRendering = true; |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 255 | return true; |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void GrLayerCache::unlock(GrCachedLayer* layer) { |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 259 | SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas ? fAtlas->getTexture() : NULL, layer);) |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 260 | |
robertphillips | a32c6bc | 2014-10-09 12:47:01 -0700 | [diff] [blame] | 261 | if (NULL == layer || !layer->locked()) { |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 262 | // invalid or not locked |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 266 | if (layer->isAtlased()) { |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 267 | const int plotID = layer->plot()->id(); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 268 | |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 269 | this->decPlotLock(plotID); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 270 | // At this point we could aggressively clear out un-locked plots but |
| 271 | // by delaying we may be able to reuse some of the atlased layers later. |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 272 | #if !GR_CACHE_HOISTED_LAYERS |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 273 | // This testing code aggressively removes the atlased layers. This |
| 274 | // can be used to separate the performance contribution of less |
| 275 | // render target pingponging from that due to the re-use of cached layers |
| 276 | GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 277 | SkASSERT(pictInfo); |
robertphillips | 225a627 | 2014-10-30 11:39:19 -0700 | [diff] [blame] | 278 | |
| 279 | pictInfo->decPlotUsage(plotID); |
| 280 | |
| 281 | if (0 == pictInfo->plotUsage(plotID)) { |
| 282 | GrAtlas::RemovePlot(&pictInfo->fPlotUsage, layer->plot()); |
robertphillips | b32f0ad | 2014-11-04 06:46:11 -0800 | [diff] [blame] | 283 | |
| 284 | if (pictInfo->fPlotUsage.isEmpty()) { |
| 285 | fPictureHash.remove(pictInfo->fPictureID); |
| 286 | SkDELETE(pictInfo); |
| 287 | } |
robertphillips | 225a627 | 2014-10-30 11:39:19 -0700 | [diff] [blame] | 288 | } |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 289 | |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 290 | layer->setPlot(NULL); |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame^] | 291 | layer->setTexture(NULL, SkIRect::MakeEmpty()); |
robertphillips | 0c42332 | 2014-07-31 11:02:38 -0700 | [diff] [blame] | 292 | #endif |
| 293 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 294 | } else { |
robertphillips | e99d499 | 2014-12-03 07:33:57 -0800 | [diff] [blame^] | 295 | layer->setTexture(NULL, SkIRect::MakeEmpty()); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 296 | } |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 297 | |
| 298 | layer->setLocked(false); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 299 | } |
| 300 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 301 | #ifdef SK_DEBUG |
| 302 | void GrLayerCache::validate() const { |
robertphillips | a32c6bc | 2014-10-09 12:47:01 -0700 | [diff] [blame] | 303 | int plotLocks[kNumPlotsX * kNumPlotsY]; |
| 304 | memset(plotLocks, 0, sizeof(plotLocks)); |
| 305 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 306 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash); |
| 307 | for (; !iter.done(); ++iter) { |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 308 | const GrCachedLayer* layer = &(*iter); |
| 309 | |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 310 | layer->validate(fAtlas.get() ? fAtlas->getTexture() : NULL); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 311 | |
| 312 | const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID()); |
robertphillips | fd61ed0 | 2014-10-28 07:21:44 -0700 | [diff] [blame] | 313 | if (!pictInfo) { |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 314 | // If there is no picture info for this picture then all of its |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 315 | // layers should be non-atlased. |
| 316 | SkASSERT(!layer->isAtlased()); |
| 317 | } |
| 318 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 319 | if (layer->plot()) { |
| 320 | SkASSERT(pictInfo); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 321 | SkASSERT(pictInfo->fPictureID == layer->pictureID()); |
| 322 | |
| 323 | SkASSERT(pictInfo->fPlotUsage.contains(layer->plot())); |
robertphillips | 225a627 | 2014-10-30 11:39:19 -0700 | [diff] [blame] | 324 | #if !GR_CACHE_HOISTED_LAYERS |
| 325 | SkASSERT(pictInfo->plotUsage(layer->plot()->id()) > 0); |
| 326 | #endif |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 327 | |
| 328 | if (layer->locked()) { |
robertphillips | a32c6bc | 2014-10-09 12:47:01 -0700 | [diff] [blame] | 329 | plotLocks[layer->plot()->id()]++; |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 330 | } |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 331 | } |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 332 | } |
robertphillips | a32c6bc | 2014-10-09 12:47:01 -0700 | [diff] [blame] | 333 | |
| 334 | for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) { |
| 335 | SkASSERT(plotLocks[i] == fPlotLocks[i]); |
| 336 | } |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | class GrAutoValidateCache : ::SkNoncopyable { |
| 340 | public: |
| 341 | explicit GrAutoValidateCache(GrLayerCache* cache) |
| 342 | : fCache(cache) { |
| 343 | fCache->validate(); |
| 344 | } |
| 345 | ~GrAutoValidateCache() { |
| 346 | fCache->validate(); |
| 347 | } |
| 348 | private: |
| 349 | GrLayerCache* fCache; |
| 350 | }; |
| 351 | #endif |
| 352 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 353 | void GrLayerCache::purge(uint32_t pictureID) { |
| 354 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 355 | SkDEBUGCODE(GrAutoValidateCache avc(this);) |
| 356 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 357 | // We need to find all the layers associated with 'picture' and remove them. |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 358 | SkTDArray<GrCachedLayer*> toBeRemoved; |
| 359 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 360 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash); |
| 361 | for (; !iter.done(); ++iter) { |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 362 | if (pictureID == (*iter).pictureID()) { |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 363 | *toBeRemoved.append() = &(*iter); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
| 367 | for (int i = 0; i < toBeRemoved.count(); ++i) { |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 368 | SkASSERT(0 == toBeRemoved[i]->uses()); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 369 | this->unlock(toBeRemoved[i]); |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame] | 370 | fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i])); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 371 | SkDELETE(toBeRemoved[i]); |
| 372 | } |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 373 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 374 | GrPictureInfo* pictInfo = fPictureHash.find(pictureID); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 375 | if (pictInfo) { |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 376 | fPictureHash.remove(pictureID); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 377 | SkDELETE(pictInfo); |
| 378 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 379 | } |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 380 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 381 | bool GrLayerCache::purgePlot() { |
| 382 | SkDEBUGCODE(GrAutoValidateCache avc(this);) |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 383 | SkASSERT(fAtlas); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 384 | |
| 385 | GrAtlas::PlotIter iter; |
| 386 | GrPlot* plot; |
| 387 | for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 388 | plot; |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 389 | plot = iter.prev()) { |
| 390 | if (fPlotLocks[plot->id()] > 0) { |
| 391 | continue; |
| 392 | } |
| 393 | |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 394 | this->purgePlot(plot); |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 395 | return true; |
| 396 | } |
| 397 | |
| 398 | return false; |
| 399 | } |
| 400 | |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 401 | void GrLayerCache::purgePlot(GrPlot* plot) { |
| 402 | SkASSERT(0 == fPlotLocks[plot->id()]); |
| 403 | |
| 404 | // We need to find all the layers in 'plot' and remove them. |
| 405 | SkTDArray<GrCachedLayer*> toBeRemoved; |
| 406 | |
| 407 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash); |
| 408 | for (; !iter.done(); ++iter) { |
| 409 | if (plot == (*iter).plot()) { |
| 410 | *toBeRemoved.append() = &(*iter); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | for (int i = 0; i < toBeRemoved.count(); ++i) { |
robertphillips | 7bb9ed7 | 2014-10-10 11:38:29 -0700 | [diff] [blame] | 415 | SkASSERT(0 == toBeRemoved[i]->uses()); |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 416 | SkASSERT(!toBeRemoved[i]->locked()); |
| 417 | |
robertphillips | 410dd05 | 2014-10-06 12:19:50 -0700 | [diff] [blame] | 418 | uint32_t pictureIDToRemove = toBeRemoved[i]->pictureID(); |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 419 | |
robertphillips | 410dd05 | 2014-10-06 12:19:50 -0700 | [diff] [blame] | 420 | // Aggressively remove layers and, if it becomes totally uncached, delete the picture info |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 421 | fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i])); |
| 422 | SkDELETE(toBeRemoved[i]); |
| 423 | |
robertphillips | 410dd05 | 2014-10-06 12:19:50 -0700 | [diff] [blame] | 424 | GrPictureInfo* pictInfo = fPictureHash.find(pictureIDToRemove); |
| 425 | if (pictInfo) { |
robertphillips | 225a627 | 2014-10-30 11:39:19 -0700 | [diff] [blame] | 426 | #if !GR_CACHE_HOISTED_LAYERS |
| 427 | SkASSERT(0 == pictInfo->plotUsage(plot->id())); |
| 428 | #endif |
robertphillips | 410dd05 | 2014-10-06 12:19:50 -0700 | [diff] [blame] | 429 | GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot); |
| 430 | |
| 431 | if (pictInfo->fPlotUsage.isEmpty()) { |
| 432 | fPictureHash.remove(pictInfo->fPictureID); |
| 433 | SkDELETE(pictInfo); |
| 434 | } |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | plot->resetRects(); |
| 439 | } |
| 440 | |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 441 | #if !GR_CACHE_HOISTED_LAYERS |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 442 | void GrLayerCache::purgeAll() { |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 443 | if (!fAtlas) { |
| 444 | return; |
| 445 | } |
| 446 | |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 447 | GrAtlas::PlotIter iter; |
| 448 | GrPlot* plot; |
| 449 | for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 450 | plot; |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 451 | plot = iter.prev()) { |
| 452 | SkASSERT(0 == fPlotLocks[plot->id()]); |
| 453 | |
| 454 | this->purgePlot(plot); |
| 455 | } |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 456 | |
robertphillips | b32f0ad | 2014-11-04 06:46:11 -0800 | [diff] [blame] | 457 | SkASSERT(0 == fPictureHash.count()); |
| 458 | |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 459 | fContext->discardRenderTarget(fAtlas->getTexture()->asRenderTarget()); |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 460 | } |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 461 | #endif |
robertphillips | 6f294af | 2014-08-18 08:50:03 -0700 | [diff] [blame] | 462 | |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 463 | void GrLayerCache::processDeletedPictures() { |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 464 | SkTDArray<SkPicture::DeletionMessage> deletedPictures; |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 465 | fPictDeletionInbox.poll(&deletedPictures); |
| 466 | |
| 467 | for (int i = 0; i < deletedPictures.count(); i++) { |
mtklein | 04c9695 | 2014-11-24 08:20:57 -0800 | [diff] [blame] | 468 | this->purge(deletedPictures[i].fUniqueID); |
robertphillips | d771f6b | 2014-07-22 10:18:06 -0700 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
robertphillips | 84ac082 | 2014-10-14 07:07:59 -0700 | [diff] [blame] | 472 | #ifdef SK_DEVELOPER |
| 473 | void GrLayerCache::writeLayersToDisk(const SkString& dirName) { |
| 474 | |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 475 | if (fAtlas) { |
| 476 | GrTexture* atlasTexture = fAtlas->getTexture(); |
| 477 | if (NULL != atlasTexture) { |
| 478 | SkString fileName(dirName); |
| 479 | fileName.append("\\atlas.png"); |
robertphillips | 84ac082 | 2014-10-14 07:07:59 -0700 | [diff] [blame] | 480 | |
robertphillips | 4ab5a90 | 2014-10-29 13:56:02 -0700 | [diff] [blame] | 481 | atlasTexture->surfacePriv().savePixels(fileName.c_str()); |
| 482 | } |
robertphillips | 84ac082 | 2014-10-14 07:07:59 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash); |
| 486 | for (; !iter.done(); ++iter) { |
| 487 | GrCachedLayer* layer = &(*iter); |
| 488 | |
| 489 | if (layer->isAtlased() || !layer->texture()) { |
| 490 | continue; |
| 491 | } |
| 492 | |
| 493 | SkString fileName(dirName); |
robertphillips | 01d6e5f | 2014-12-01 09:09:27 -0800 | [diff] [blame] | 494 | fileName.appendf("\\%d", layer->fKey.pictureID()); |
| 495 | for (int i = 0; i < layer->fKey.keySize(); ++i) { |
| 496 | fileName.appendf("-%d", layer->fKey.key()[i]); |
| 497 | } |
| 498 | fileName.appendf(".png"); |
robertphillips | 84ac082 | 2014-10-14 07:07:59 -0700 | [diff] [blame] | 499 | |
| 500 | layer->texture()->surfacePriv().savePixels(fileName.c_str()); |
| 501 | } |
| 502 | } |
| 503 | #endif |