| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrAtlas.h" | 8 #include "GrAtlas.h" |
| 9 #include "GrGpu.h" | 9 #include "GrGpu.h" |
| 10 #include "GrLayerCache.h" | 10 #include "GrLayerCache.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 static bool Equals(const GrCachedLayer& layer, const PictureLayerKey& key) { | 35 static bool Equals(const GrCachedLayer& layer, const PictureLayerKey& key) { |
| 36 return layer.pictureID() == key.pictureID() && layer.layerID() == key.la
yerID(); | 36 return layer.pictureID() == key.pictureID() && layer.layerID() == key.la
yerID(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 private: | 39 private: |
| 40 uint32_t fPictureID; | 40 uint32_t fPictureID; |
| 41 int fLayerID; | 41 int fLayerID; |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 GrLayerCache::GrLayerCache(GrContext* context) | 44 GrLayerCache::GrLayerCache(GrContext* context) |
| 45 : fContext(context) { | 45 : fContext(context) |
| 46 this->initAtlas(); | 46 , fLayerPool(16) { // TODO: may need to increase this later |
| 47 } | 47 } |
| 48 | 48 |
| 49 GrLayerCache::~GrLayerCache() { | 49 GrLayerCache::~GrLayerCache() { |
| 50 } | 50 } |
| 51 | 51 |
| 52 void GrLayerCache::initAtlas() { | 52 void GrLayerCache::init() { |
| 53 static const int kAtlasTextureWidth = 1024; | 53 static const int kAtlasTextureWidth = 1024; |
| 54 static const int kAtlasTextureHeight = 1024; | 54 static const int kAtlasTextureHeight = 1024; |
| 55 | 55 |
| 56 SkASSERT(NULL == fAtlas.get()); | 56 SkASSERT(NULL == fAtlas.get()); |
| 57 | 57 |
| 58 // The layer cache only gets 1 plot | 58 // The layer cache only gets 1 plot |
| 59 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight)
; | 59 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight)
; |
| 60 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfi
g, | 60 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfi
g, |
| 61 kRenderTarget_GrTextureFlagBit, | |
| 62 textureSize, 1, 1, false))); | 61 textureSize, 1, 1, false))); |
| 63 } | 62 } |
| 64 | 63 |
| 65 void GrLayerCache::freeAll() { | 64 void GrLayerCache::freeAll() { |
| 66 SkTDArray<GrCachedLayer*>& layerArray = fLayerHash.getArray(); | |
| 67 for (int i = 0; i < fLayerHash.count(); ++i) { | |
| 68 this->unlock(layerArray[i]); | |
| 69 } | |
| 70 | |
| 71 fLayerHash.deleteAll(); | 65 fLayerHash.deleteAll(); |
| 72 | |
| 73 // The atlas only lets go of its texture when the atlas is deleted. | |
| 74 fAtlas.free(); | 66 fAtlas.free(); |
| 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 freeGpuResource
s. | |
| 78 // TODO: Make GrLayerCache lazily allocate the atlas manager? | |
| 79 this->initAtlas(); | |
| 80 } | 67 } |
| 81 | 68 |
| 82 GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID)
{ | 69 GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID)
{ |
| 70 GrCachedLayer* layer = fLayerPool.alloc(); |
| 71 |
| 83 SkASSERT(picture->uniqueID() != SK_InvalidGenID); | 72 SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
| 84 | 73 layer->init(picture->uniqueID(), layerID); |
| 85 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (picture->uniqueID(), layer
ID)); | |
| 86 fLayerHash.insert(PictureLayerKey(picture->uniqueID(), layerID), layer); | 74 fLayerHash.insert(PictureLayerKey(picture->uniqueID(), layerID), layer); |
| 87 return layer; | 75 return layer; |
| 88 } | 76 } |
| 89 | 77 |
| 90 GrCachedLayer* GrLayerCache::findLayer(const SkPicture* picture, int layerID) { | 78 GrCachedLayer* GrLayerCache::findLayer(const SkPicture* picture, int layerID) { |
| 91 SkASSERT(picture->uniqueID() != SK_InvalidGenID); | 79 SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
| 92 return fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID)); | 80 return fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID)); |
| 93 } | 81 } |
| 94 | 82 |
| 95 GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int lay
erID) { | 83 GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int lay
erID) { |
| 96 SkASSERT(picture->uniqueID() != SK_InvalidGenID); | 84 SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
| 97 GrCachedLayer* layer = fLayerHash.find(PictureLayerKey(picture->uniqueID(),
layerID)); | 85 GrCachedLayer* layer = fLayerHash.find(PictureLayerKey(picture->uniqueID(),
layerID)); |
| 98 if (NULL == layer) { | 86 if (NULL == layer) { |
| 99 layer = this->createLayer(picture, layerID); | 87 layer = this->createLayer(picture, layerID); |
| 100 } | 88 } |
| 101 | 89 |
| 102 return layer; | 90 return layer; |
| 103 } | 91 } |
| 104 | 92 |
| 105 bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc) { | 93 bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc) { |
| 94 SkASSERT(NULL == layer->getTexture()); |
| 106 | 95 |
| 107 if (NULL != layer->texture()) { | 96 // This just uses scratch textures and doesn't cache the 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, NU
LL, &loc); | |
| 122 if (NULL != plot) { | |
| 123 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY, | |
| 124 SkToS16(desc.fWidth), SkToS16(des
c.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 anyt
hing. | |
| 131 // This can yield a lot of re-rendering | 97 // This can yield a lot of re-rendering |
| 132 layer->setTexture(fContext->lockAndRefScratchTexture(desc, GrContext::kAppro
x_ScratchTexMatch), | 98 layer->setTexture(fContext->lockAndRefScratchTexture(desc, GrContext::kAppro
x_ScratchTexMatch)); |
| 133 GrIRect16::MakeEmpty()); | |
| 134 return false; | 99 return false; |
| 135 } | 100 } |
| 136 | 101 |
| 137 void GrLayerCache::unlock(GrCachedLayer* layer) { | 102 void GrLayerCache::unlock(GrCachedLayer* layer) { |
| 138 if (NULL == layer || NULL == layer->texture()) { | 103 if (NULL == layer || NULL == layer->getTexture()) { |
| 139 return; | 104 return; |
| 140 } | 105 } |
| 141 | 106 |
| 142 // The atlas doesn't currently use a scratch texture (and we would have | 107 fContext->unlockScratchTexture(layer->getTexture()); |
| 143 // to free up space differently anyways) | 108 layer->setTexture(NULL); |
| 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 } | 109 } |
| 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 } | |
| 172 } | |
| OLD | NEW |