robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrAtlas.h" |
| 9 | #include "GrGpu.h" |
| 10 | #include "GrLayerCache.h" |
| 11 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 12 | #ifdef SK_DEBUG |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 13 | void GrCachedLayer::validate(const GrTexture* backingTexture) const { |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 14 | SkASSERT(SK_InvalidGenID != fKey.getPictureID()); |
| 15 | SkASSERT(-1 != fKey.getLayerID()); |
| 16 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 17 | |
| 18 | if (NULL != fTexture) { |
| 19 | // If the layer is in some texture then it must occupy some rectangle |
| 20 | SkASSERT(!fRect.isEmpty()); |
| 21 | if (!this->isAtlased()) { |
| 22 | // If it isn't atlased then the rectangle should start at the origin |
| 23 | SkASSERT(0.0f == fRect.fLeft && 0.0f == fRect.fTop); |
| 24 | } |
| 25 | } else { |
| 26 | SkASSERT(fRect.isEmpty()); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 27 | SkASSERT(NULL == fPlot); |
| 28 | } |
| 29 | |
| 30 | if (NULL != fPlot) { |
| 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. |
| 33 | SkASSERT(NULL != fTexture && backingTexture == fTexture); |
| 34 | SkASSERT(!fRect.isEmpty()); |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
| 38 | class GrAutoValidateLayer : ::SkNoncopyable { |
| 39 | public: |
| 40 | GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer) |
| 41 | : fBackingTexture(backingTexture) |
| 42 | , fLayer(layer) { |
| 43 | if (NULL != fLayer) { |
| 44 | fLayer->validate(backingTexture); |
| 45 | } |
| 46 | } |
| 47 | ~GrAutoValidateLayer() { |
| 48 | if (NULL != fLayer) { |
| 49 | fLayer->validate(fBackingTexture); |
| 50 | } |
| 51 | } |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 52 | void setBackingTexture(GrTexture* backingTexture) { |
| 53 | SkASSERT(NULL == fBackingTexture || fBackingTexture == backingTexture); |
| 54 | fBackingTexture = backingTexture; |
| 55 | } |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 56 | |
| 57 | private: |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 58 | const GrTexture* fBackingTexture; |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 59 | const GrCachedLayer* fLayer; |
| 60 | }; |
| 61 | #endif |
| 62 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 63 | GrLayerCache::GrLayerCache(GrContext* context) |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 64 | : fContext(context) { |
| 65 | this->initAtlas(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | GrLayerCache::~GrLayerCache() { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 69 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 70 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash); |
| 71 | for (; !iter.done(); ++iter) { |
| 72 | GrCachedLayer* layer = &(*iter); |
| 73 | this->unlock(layer); |
| 74 | SkDELETE(layer); |
| 75 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 76 | |
| 77 | // The atlas only lets go of its texture when the atlas is deleted. |
| 78 | fAtlas.free(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 79 | } |
| 80 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 81 | void GrLayerCache::initAtlas() { |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 82 | static const int kAtlasTextureWidth = 1024; |
| 83 | static const int kAtlasTextureHeight = 1024; |
| 84 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 85 | SkASSERT(NULL == fAtlas.get()); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 86 | |
| 87 | // The layer cache only gets 1 plot |
| 88 | SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight); |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 89 | fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig, |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 90 | kRenderTarget_GrTextureFlagBit, |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 91 | textureSize, kNumPlotsX, kNumPlotsY, false))); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void GrLayerCache::freeAll() { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 95 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 96 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash); |
| 97 | for (; !iter.done(); ++iter) { |
| 98 | GrCachedLayer* layer = &(*iter); |
| 99 | this->unlock(layer); |
| 100 | SkDELETE(layer); |
| 101 | } |
| 102 | fLayerHash.rewind(); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 103 | |
| 104 | // The atlas only lets go of its texture when the atlas is deleted. |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 105 | fAtlas.free(); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 106 | // GrLayerCache always assumes an atlas exists so recreate it. The atlas |
| 107 | // lazily allocates a replacement texture so reallocating a new |
| 108 | // atlas here won't disrupt a GrContext::contextDestroyed or freeGpuResources. |
| 109 | // TODO: Make GrLayerCache lazily allocate the atlas manager? |
| 110 | this->initAtlas(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 111 | } |
| 112 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 113 | GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID) { |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 114 | SkASSERT(picture->uniqueID() != SK_InvalidGenID && layerID >= 0); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 115 | |
| 116 | GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (picture->uniqueID(), layerID)); |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 117 | fLayerHash.add(layer); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 118 | return layer; |
| 119 | } |
| 120 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 121 | GrCachedLayer* GrLayerCache::findLayer(const SkPicture* picture, int layerID) { |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 122 | SkASSERT(picture->uniqueID() != SK_InvalidGenID && layerID >= 0); |
| 123 | return fLayerHash.find(GrCachedLayer::Key(picture->uniqueID(), layerID)); |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 124 | } |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 125 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 126 | GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int layerID) { |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 127 | SkASSERT(picture->uniqueID() != SK_InvalidGenID && layerID >= 0); |
| 128 | GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(picture->uniqueID(), layerID)); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 129 | if (NULL == layer) { |
| 130 | layer = this->createLayer(picture, layerID); |
| 131 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 132 | |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 133 | return layer; |
| 134 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 135 | |
| 136 | bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc) { |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 137 | SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);) |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 138 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 139 | if (NULL != layer->texture()) { |
| 140 | // This layer is already locked |
| 141 | #ifdef SK_DEBUG |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 142 | if (layer->isAtlased()) { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 143 | // It claims to be atlased |
| 144 | SkASSERT(layer->rect().width() == desc.fWidth); |
| 145 | SkASSERT(layer->rect().height() == desc.fHeight); |
| 146 | } |
| 147 | #endif |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | #if USE_ATLAS |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 152 | { |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 153 | GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID()); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 154 | if (NULL == pictInfo) { |
| 155 | pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID())); |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 156 | fPictureHash.add(pictInfo); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | SkIPoint16 loc; |
| 160 | GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage, |
| 161 | desc.fWidth, desc.fHeight, |
| 162 | NULL, &loc); |
| 163 | // addToAtlas can allocate the backing texture |
| 164 | SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture())); |
| 165 | if (NULL != plot) { |
| 166 | GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY, |
| 167 | SkToS16(desc.fWidth), SkToS16(desc.fHeight)); |
| 168 | layer->setTexture(fAtlas->getTexture(), bounds); |
| 169 | layer->setPlot(plot); |
| 170 | return false; |
| 171 | } |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 172 | } |
| 173 | #endif |
| 174 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 175 | // The texture wouldn't fit in the cache - give it it's own texture. |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 176 | // This path always uses a new scratch texture and (thus) doesn't cache anything. |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 177 | // This can yield a lot of re-rendering |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 178 | layer->setTexture(fContext->lockAndRefScratchTexture(desc, GrContext::kApprox_ScratchTexMatch), |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 179 | GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight))); |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 180 | return false; |
| 181 | } |
| 182 | |
| 183 | void GrLayerCache::unlock(GrCachedLayer* layer) { |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 184 | SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);) |
| 185 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 186 | if (NULL == layer || NULL == layer->texture()) { |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 190 | if (layer->isAtlased()) { |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 191 | SkASSERT(layer->texture() == fAtlas->getTexture()); |
| 192 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 193 | GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID()); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 194 | SkASSERT(NULL != pictInfo); |
| 195 | pictInfo->fPlotUsage.isEmpty(); // just to silence compiler warnings for the time being |
| 196 | |
| 197 | // TODO: purging from atlas goes here |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 198 | } else { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 199 | fContext->unlockScratchTexture(layer->texture()); |
| 200 | layer->setTexture(NULL, GrIRect16::MakeEmpty()); |
| 201 | } |
| 202 | } |
| 203 | |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 204 | #ifdef SK_DEBUG |
| 205 | void GrLayerCache::validate() const { |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 206 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash); |
| 207 | for (; !iter.done(); ++iter) { |
| 208 | (*iter).validate(fAtlas->getTexture()); |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | class GrAutoValidateCache : ::SkNoncopyable { |
| 213 | public: |
| 214 | explicit GrAutoValidateCache(GrLayerCache* cache) |
| 215 | : fCache(cache) { |
| 216 | fCache->validate(); |
| 217 | } |
| 218 | ~GrAutoValidateCache() { |
| 219 | fCache->validate(); |
| 220 | } |
| 221 | private: |
| 222 | GrLayerCache* fCache; |
| 223 | }; |
| 224 | #endif |
| 225 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 226 | void GrLayerCache::purge(const SkPicture* picture) { |
robertphillips | 21048b5 | 2014-07-15 19:46:35 -0700 | [diff] [blame] | 227 | SkDEBUGCODE(GrAutoValidateCache avc(this);) |
| 228 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 229 | // We need to find all the layers associated with 'picture' and remove them. |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 230 | SkTDArray<GrCachedLayer*> toBeRemoved; |
| 231 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 232 | SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash); |
| 233 | for (; !iter.done(); ++iter) { |
| 234 | if (picture->uniqueID() == (*iter).pictureID()) { |
| 235 | *toBeRemoved.append() = &(*iter); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | for (int i = 0; i < toBeRemoved.count(); ++i) { |
| 240 | this->unlock(toBeRemoved[i]); |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 241 | fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i])); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 242 | SkDELETE(toBeRemoved[i]); |
| 243 | } |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 244 | |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 245 | GrPictureInfo* pictInfo = fPictureHash.find(picture->uniqueID()); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 246 | if (NULL != pictInfo) { |
robertphillips | 3d533ac | 2014-07-20 09:40:00 -0700 | [diff] [blame^] | 247 | fPictureHash.remove(picture->uniqueID()); |
robertphillips | 261b8a9 | 2014-07-17 08:26:44 -0700 | [diff] [blame] | 248 | SkDELETE(pictInfo); |
| 249 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 250 | } |