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