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