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