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