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 | |
| 12 | /** |
| 13 | * PictureLayerKey just wraps a saveLayer's id in the picture for GrTHashTable. |
| 14 | */ |
| 15 | class GrLayerCache::PictureLayerKey { |
| 16 | public: |
skia.committer@gmail.com | a915772 | 2014-04-03 03:04:26 +0000 | [diff] [blame] | 17 | PictureLayerKey(uint32_t pictureID, int layerID) |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 18 | : fPictureID(pictureID) |
| 19 | , fLayerID(layerID) { |
| 20 | } |
| 21 | |
| 22 | uint32_t pictureID() const { return fPictureID; } |
| 23 | int layerID() const { return fLayerID; } |
| 24 | |
| 25 | uint32_t getHash() const { return (fPictureID << 16) | fLayerID; } |
| 26 | |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 27 | static bool LessThan(const GrCachedLayer& layer, const PictureLayerKey& key) { |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 28 | if (layer.pictureID() == key.pictureID()) { |
| 29 | return layer.layerID() < key.layerID(); |
| 30 | } |
| 31 | |
| 32 | return layer.pictureID() < key.pictureID(); |
| 33 | } |
| 34 | |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 35 | static bool Equals(const GrCachedLayer& layer, const PictureLayerKey& key) { |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 36 | return layer.pictureID() == key.pictureID() && layer.layerID() == key.layerID(); |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | uint32_t fPictureID; |
| 41 | int fLayerID; |
| 42 | }; |
| 43 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 44 | GrLayerCache::GrLayerCache(GrContext* context) |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame^] | 45 | : fContext(context) { |
| 46 | this->initAtlas(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | GrLayerCache::~GrLayerCache() { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame^] | 50 | SkTDArray<GrCachedLayer*>& layerArray = fLayerHash.getArray(); |
| 51 | for (int i = 0; i < fLayerHash.count(); ++i) { |
| 52 | this->unlock(layerArray[i]); |
| 53 | } |
| 54 | |
| 55 | fLayerHash.deleteAll(); |
| 56 | |
| 57 | // The atlas only lets go of its texture when the atlas is deleted. |
| 58 | fAtlas.free(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 59 | } |
| 60 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame^] | 61 | void GrLayerCache::initAtlas() { |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 62 | static const int kAtlasTextureWidth = 1024; |
| 63 | static const int kAtlasTextureHeight = 1024; |
| 64 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 65 | SkASSERT(NULL == fAtlas.get()); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 66 | |
| 67 | // The layer cache only gets 1 plot |
| 68 | SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight); |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 69 | fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig, |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame^] | 70 | kRenderTarget_GrTextureFlagBit, |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 71 | textureSize, 1, 1, false))); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void GrLayerCache::freeAll() { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame^] | 75 | SkTDArray<GrCachedLayer*>& layerArray = fLayerHash.getArray(); |
| 76 | for (int i = 0; i < fLayerHash.count(); ++i) { |
| 77 | this->unlock(layerArray[i]); |
| 78 | } |
| 79 | |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 80 | fLayerHash.deleteAll(); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame^] | 81 | |
| 82 | // The atlas only lets go of its texture when the atlas is deleted. |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 83 | fAtlas.free(); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame^] | 84 | // GrLayerCache always assumes an atlas exists so recreate it. The atlas |
| 85 | // lazily allocates a replacement texture so reallocating a new |
| 86 | // atlas here won't disrupt a GrContext::contextDestroyed or freeGpuResources. |
| 87 | // TODO: Make GrLayerCache lazily allocate the atlas manager? |
| 88 | this->initAtlas(); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 89 | } |
| 90 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 91 | GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID) { |
robertphillips | e462f2b | 2014-06-29 17:16:27 -0700 | [diff] [blame] | 92 | SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame^] | 93 | |
| 94 | GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (picture->uniqueID(), layerID)); |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 95 | fLayerHash.insert(PictureLayerKey(picture->uniqueID(), layerID), layer); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 96 | return layer; |
| 97 | } |
| 98 | |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 99 | GrCachedLayer* GrLayerCache::findLayer(const SkPicture* picture, int layerID) { |
| 100 | SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
| 101 | return fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID)); |
| 102 | } |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 103 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 104 | GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int layerID) { |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 105 | SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
commit-bot@chromium.org | 365cd31 | 2014-04-11 15:53:47 +0000 | [diff] [blame] | 106 | GrCachedLayer* layer = fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID)); |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 107 | if (NULL == layer) { |
| 108 | layer = this->createLayer(picture, layerID); |
| 109 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 110 | |
robertphillips@google.com | e930a07 | 2014-04-03 00:34:27 +0000 | [diff] [blame] | 111 | return layer; |
| 112 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 113 | |
| 114 | bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc) { |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 115 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame^] | 116 | if (NULL != layer->texture()) { |
| 117 | // This layer is already locked |
| 118 | #ifdef SK_DEBUG |
| 119 | if (!layer->rect().isEmpty()) { |
| 120 | // It claims to be atlased |
| 121 | SkASSERT(layer->rect().width() == desc.fWidth); |
| 122 | SkASSERT(layer->rect().height() == desc.fHeight); |
| 123 | } |
| 124 | #endif |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | #if USE_ATLAS |
| 129 | SkIPoint16 loc; |
| 130 | GrPlot* plot = fAtlas->addToAtlas(&fPlotUsage, desc.fWidth, desc.fHeight, NULL, &loc); |
| 131 | if (NULL != plot) { |
| 132 | GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY, |
| 133 | SkToS16(desc.fWidth), SkToS16(desc.fHeight)); |
| 134 | layer->setTexture(fAtlas->getTexture(), bounds); |
| 135 | return false; |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | // 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] | 140 | // This can yield a lot of re-rendering |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame^] | 141 | layer->setTexture(fContext->lockAndRefScratchTexture(desc, GrContext::kApprox_ScratchTexMatch), |
| 142 | GrIRect16::MakeEmpty()); |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | |
| 146 | void GrLayerCache::unlock(GrCachedLayer* layer) { |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame^] | 147 | if (NULL == layer || NULL == layer->texture()) { |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame^] | 151 | // The atlas doesn't currently use a scratch texture (and we would have |
| 152 | // to free up space differently anyways) |
| 153 | // TODO: unlock atlas space when a recycling rectanizer is available |
| 154 | if (layer->texture() != fAtlas->getTexture()) { |
| 155 | fContext->unlockScratchTexture(layer->texture()); |
| 156 | layer->setTexture(NULL, GrIRect16::MakeEmpty()); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void GrLayerCache::purge(const SkPicture* picture) { |
| 161 | // This is somewhat of an abuse of GrTHashTable. We need to find all the |
| 162 | // layers associated with 'picture' but the usual hash calls only look for |
| 163 | // exact key matches. This code peeks into the hash table's innards to |
| 164 | // find all the 'picture'-related layers. |
| 165 | // TODO: use a different data structure for the layer hash? |
| 166 | SkTDArray<GrCachedLayer*> toBeRemoved; |
| 167 | |
| 168 | const SkTDArray<GrCachedLayer*>& layerArray = fLayerHash.getArray(); |
| 169 | for (int i = 0; i < fLayerHash.count(); ++i) { |
| 170 | if (picture->uniqueID() == layerArray[i]->pictureID()) { |
| 171 | *toBeRemoved.append() = layerArray[i]; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | for (int i = 0; i < toBeRemoved.count(); ++i) { |
| 176 | this->unlock(toBeRemoved[i]); |
| 177 | |
| 178 | PictureLayerKey key(picture->uniqueID(), toBeRemoved[i]->layerID()); |
| 179 | fLayerHash.remove(key, toBeRemoved[i]); |
| 180 | SkDELETE(toBeRemoved[i]); |
| 181 | } |
robertphillips | 4ec84da | 2014-06-24 13:10:43 -0700 | [diff] [blame] | 182 | } |