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 , fLayerPool(16) { // TODO: may need to increase this later | 46 this->initAtlas(); |
47 } | 47 } |
48 | 48 |
49 GrLayerCache::~GrLayerCache() { | 49 GrLayerCache::~GrLayerCache() { |
| 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(); |
50 } | 59 } |
51 | 60 |
52 void GrLayerCache::init() { | 61 void GrLayerCache::initAtlas() { |
53 static const int kAtlasTextureWidth = 1024; | 62 static const int kAtlasTextureWidth = 1024; |
54 static const int kAtlasTextureHeight = 1024; | 63 static const int kAtlasTextureHeight = 1024; |
55 | 64 |
56 SkASSERT(NULL == fAtlas.get()); | 65 SkASSERT(NULL == fAtlas.get()); |
57 | 66 |
58 // The layer cache only gets 1 plot | 67 // The layer cache only gets 1 plot |
59 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight)
; | 68 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight)
; |
60 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfi
g, | 69 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfi
g, |
| 70 kRenderTarget_GrTextureFlagBit, |
61 textureSize, 1, 1, false))); | 71 textureSize, 1, 1, false))); |
62 } | 72 } |
63 | 73 |
64 void GrLayerCache::freeAll() { | 74 void GrLayerCache::freeAll() { |
| 75 SkTDArray<GrCachedLayer*>& layerArray = fLayerHash.getArray(); |
| 76 for (int i = 0; i < fLayerHash.count(); ++i) { |
| 77 this->unlock(layerArray[i]); |
| 78 } |
| 79 |
65 fLayerHash.deleteAll(); | 80 fLayerHash.deleteAll(); |
| 81 |
| 82 // The atlas only lets go of its texture when the atlas is deleted. |
66 fAtlas.free(); | 83 fAtlas.free(); |
| 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 freeGpuResource
s. |
| 87 // TODO: Make GrLayerCache lazily allocate the atlas manager? |
| 88 this->initAtlas(); |
67 } | 89 } |
68 | 90 |
69 GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID)
{ | 91 GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID)
{ |
70 GrCachedLayer* layer = fLayerPool.alloc(); | 92 SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
71 | 93 |
72 SkASSERT(picture->uniqueID() != SK_InvalidGenID); | 94 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (picture->uniqueID(), layer
ID)); |
73 layer->init(picture->uniqueID(), layerID); | |
74 fLayerHash.insert(PictureLayerKey(picture->uniqueID(), layerID), layer); | 95 fLayerHash.insert(PictureLayerKey(picture->uniqueID(), layerID), layer); |
75 return layer; | 96 return layer; |
76 } | 97 } |
77 | 98 |
78 GrCachedLayer* GrLayerCache::findLayer(const SkPicture* picture, int layerID) { | 99 GrCachedLayer* GrLayerCache::findLayer(const SkPicture* picture, int layerID) { |
79 SkASSERT(picture->uniqueID() != SK_InvalidGenID); | 100 SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
80 return fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID)); | 101 return fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID)); |
81 } | 102 } |
82 | 103 |
83 GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int lay
erID) { | 104 GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int lay
erID) { |
84 SkASSERT(picture->uniqueID() != SK_InvalidGenID); | 105 SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
85 GrCachedLayer* layer = fLayerHash.find(PictureLayerKey(picture->uniqueID(),
layerID)); | 106 GrCachedLayer* layer = fLayerHash.find(PictureLayerKey(picture->uniqueID(),
layerID)); |
86 if (NULL == layer) { | 107 if (NULL == layer) { |
87 layer = this->createLayer(picture, layerID); | 108 layer = this->createLayer(picture, layerID); |
88 } | 109 } |
89 | 110 |
90 return layer; | 111 return layer; |
91 } | 112 } |
92 | 113 |
93 bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc) { | 114 bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc) { |
94 SkASSERT(NULL == layer->getTexture()); | |
95 | 115 |
96 // This just uses scratch textures and doesn't cache the texture. | 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, NU
LL, &loc); |
| 131 if (NULL != plot) { |
| 132 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY, |
| 133 SkToS16(desc.fWidth), SkToS16(des
c.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 anyt
hing. |
97 // This can yield a lot of re-rendering | 140 // This can yield a lot of re-rendering |
98 layer->setTexture(fContext->lockAndRefScratchTexture(desc, GrContext::kAppro
x_ScratchTexMatch)); | 141 layer->setTexture(fContext->lockAndRefScratchTexture(desc, GrContext::kAppro
x_ScratchTexMatch), |
| 142 GrIRect16::MakeEmpty()); |
99 return false; | 143 return false; |
100 } | 144 } |
101 | 145 |
102 void GrLayerCache::unlock(GrCachedLayer* layer) { | 146 void GrLayerCache::unlock(GrCachedLayer* layer) { |
103 if (NULL == layer || NULL == layer->getTexture()) { | 147 if (NULL == layer || NULL == layer->texture()) { |
104 return; | 148 return; |
105 } | 149 } |
106 | 150 |
107 fContext->unlockScratchTexture(layer->getTexture()); | 151 // The atlas doesn't currently use a scratch texture (and we would have |
108 layer->setTexture(NULL); | 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 } |
109 } | 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 } |
| 182 } |
OLD | NEW |