blob: 2a57dccb4c8e492503fca89f8dae1c52d867dc03 [file] [log] [blame]
robertphillips@google.come930a072014-04-03 00:34:27 +00001/*
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
robertphillipsd771f6b2014-07-22 10:18:06 -070012DECLARE_SKMESSAGEBUS_MESSAGE(GrPictureDeletedMessage);
13
robertphillips21048b52014-07-15 19:46:35 -070014#ifdef SK_DEBUG
robertphillips261b8a92014-07-17 08:26:44 -070015void GrCachedLayer::validate(const GrTexture* backingTexture) const {
robertphillips3d533ac2014-07-20 09:40:00 -070016 SkASSERT(SK_InvalidGenID != fKey.getPictureID());
17 SkASSERT(-1 != fKey.getLayerID());
18
robertphillips21048b52014-07-15 19:46:35 -070019
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());
robertphillips261b8a92014-07-17 08:26:44 -070029 SkASSERT(NULL == fPlot);
robertphillips320c9232014-07-29 06:07:19 -070030 SkASSERT(!fLocked); // layers without a texture cannot be locked
robertphillips261b8a92014-07-17 08:26:44 -070031 }
32
33 if (NULL != fPlot) {
34 // If a layer has a plot (i.e., is atlased) then it must point to
35 // the backing texture. Additionally, its rect should be non-empty.
36 SkASSERT(NULL != fTexture && backingTexture == fTexture);
37 SkASSERT(!fRect.isEmpty());
robertphillips21048b52014-07-15 19:46:35 -070038 }
robertphillips320c9232014-07-29 06:07:19 -070039
40 if (fLocked) {
41 // If a layer is locked it must have a texture (though it need not be
42 // the atlas-backing texture) and occupy some space.
43 SkASSERT(NULL != fTexture);
44 SkASSERT(!fRect.isEmpty());
45 }
robertphillips21048b52014-07-15 19:46:35 -070046}
47
48class GrAutoValidateLayer : ::SkNoncopyable {
49public:
50 GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer)
51 : fBackingTexture(backingTexture)
52 , fLayer(layer) {
53 if (NULL != fLayer) {
54 fLayer->validate(backingTexture);
55 }
56 }
57 ~GrAutoValidateLayer() {
58 if (NULL != fLayer) {
59 fLayer->validate(fBackingTexture);
60 }
61 }
robertphillips261b8a92014-07-17 08:26:44 -070062 void setBackingTexture(GrTexture* backingTexture) {
63 SkASSERT(NULL == fBackingTexture || fBackingTexture == backingTexture);
64 fBackingTexture = backingTexture;
65 }
robertphillips21048b52014-07-15 19:46:35 -070066
67private:
robertphillips261b8a92014-07-17 08:26:44 -070068 const GrTexture* fBackingTexture;
robertphillips21048b52014-07-15 19:46:35 -070069 const GrCachedLayer* fLayer;
70};
71#endif
72
robertphillips4ec84da2014-06-24 13:10:43 -070073GrLayerCache::GrLayerCache(GrContext* context)
robertphillips952841b2014-06-30 08:26:50 -070074 : fContext(context) {
75 this->initAtlas();
robertphillips320c9232014-07-29 06:07:19 -070076 memset(fPlotLocks, 0, sizeof(fPlotLocks));
robertphillips@google.come930a072014-04-03 00:34:27 +000077}
78
79GrLayerCache::~GrLayerCache() {
robertphillips952841b2014-06-30 08:26:50 -070080
robertphillips3d533ac2014-07-20 09:40:00 -070081 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
82 for (; !iter.done(); ++iter) {
83 GrCachedLayer* layer = &(*iter);
84 this->unlock(layer);
85 SkDELETE(layer);
86 }
robertphillips952841b2014-06-30 08:26:50 -070087
88 // The atlas only lets go of its texture when the atlas is deleted.
89 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +000090}
91
robertphillips952841b2014-06-30 08:26:50 -070092void GrLayerCache::initAtlas() {
robertphillips1d86ee82014-06-24 15:08:49 -070093 SkASSERT(NULL == fAtlas.get());
robertphillips@google.come930a072014-04-03 00:34:27 +000094
robertphillips@google.come930a072014-04-03 00:34:27 +000095 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight);
robertphillips1d86ee82014-06-24 15:08:49 -070096 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig,
robertphillips952841b2014-06-30 08:26:50 -070097 kRenderTarget_GrTextureFlagBit,
robertphillips261b8a92014-07-17 08:26:44 -070098 textureSize, kNumPlotsX, kNumPlotsY, false)));
robertphillips@google.come930a072014-04-03 00:34:27 +000099}
100
101void GrLayerCache::freeAll() {
robertphillips952841b2014-06-30 08:26:50 -0700102
robertphillips3d533ac2014-07-20 09:40:00 -0700103 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
104 for (; !iter.done(); ++iter) {
105 GrCachedLayer* layer = &(*iter);
106 this->unlock(layer);
107 SkDELETE(layer);
108 }
109 fLayerHash.rewind();
robertphillips952841b2014-06-30 08:26:50 -0700110
111 // The atlas only lets go of its texture when the atlas is deleted.
robertphillips1d86ee82014-06-24 15:08:49 -0700112 fAtlas.free();
robertphillips952841b2014-06-30 08:26:50 -0700113 // GrLayerCache always assumes an atlas exists so recreate it. The atlas
114 // lazily allocates a replacement texture so reallocating a new
115 // atlas here won't disrupt a GrContext::contextDestroyed or freeGpuResources.
116 // TODO: Make GrLayerCache lazily allocate the atlas manager?
117 this->initAtlas();
robertphillips@google.come930a072014-04-03 00:34:27 +0000118}
119
robertphillips9b14f262014-06-04 05:40:44 -0700120GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID) {
robertphillips3d533ac2014-07-20 09:40:00 -0700121 SkASSERT(picture->uniqueID() != SK_InvalidGenID && layerID >= 0);
robertphillips952841b2014-06-30 08:26:50 -0700122
123 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (picture->uniqueID(), layerID));
robertphillips3d533ac2014-07-20 09:40:00 -0700124 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000125 return layer;
126}
127
robertphillips4ec84da2014-06-24 13:10:43 -0700128GrCachedLayer* GrLayerCache::findLayer(const SkPicture* picture, int layerID) {
robertphillips3d533ac2014-07-20 09:40:00 -0700129 SkASSERT(picture->uniqueID() != SK_InvalidGenID && layerID >= 0);
130 return fLayerHash.find(GrCachedLayer::Key(picture->uniqueID(), layerID));
robertphillips4ec84da2014-06-24 13:10:43 -0700131}
robertphillips@google.come930a072014-04-03 00:34:27 +0000132
robertphillips9b14f262014-06-04 05:40:44 -0700133GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int layerID) {
robertphillips3d533ac2014-07-20 09:40:00 -0700134 SkASSERT(picture->uniqueID() != SK_InvalidGenID && layerID >= 0);
135 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(picture->uniqueID(), layerID));
robertphillips@google.come930a072014-04-03 00:34:27 +0000136 if (NULL == layer) {
137 layer = this->createLayer(picture, layerID);
138 }
robertphillips4ec84da2014-06-24 13:10:43 -0700139
robertphillips@google.come930a072014-04-03 00:34:27 +0000140 return layer;
141}
robertphillips4ec84da2014-06-24 13:10:43 -0700142
143bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc) {
robertphillips21048b52014-07-15 19:46:35 -0700144 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700145
robertphillips320c9232014-07-29 06:07:19 -0700146 if (layer->locked()) {
robertphillips952841b2014-06-30 08:26:50 -0700147 // This layer is already locked
148#ifdef SK_DEBUG
robertphillips21048b52014-07-15 19:46:35 -0700149 if (layer->isAtlased()) {
robertphillips952841b2014-06-30 08:26:50 -0700150 // It claims to be atlased
151 SkASSERT(layer->rect().width() == desc.fWidth);
152 SkASSERT(layer->rect().height() == desc.fHeight);
153 }
154#endif
155 return true;
156 }
157
158#if USE_ATLAS
robertphillips320c9232014-07-29 06:07:19 -0700159 if (layer->isAtlased()) {
160 // Hooray it is still in the atlas - make sure it stays there
161 layer->setLocked(true);
162 fPlotLocks[layer->plot()->id()]++;
163 return true;
164 } else if (PlausiblyAtlasable(desc.fWidth, desc.fHeight)) {
165 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700166 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700167 if (NULL == pictInfo) {
168 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700169 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700170 }
171
172 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700173 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
174 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
175 desc.fWidth, desc.fHeight,
176 NULL, &loc);
177 // addToAtlas can allocate the backing texture
178 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
179 if (NULL != plot) {
180 // The layer was successfully added to the atlas
181 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY,
182 SkToS16(desc.fWidth),
183 SkToS16(desc.fHeight));
184 layer->setTexture(fAtlas->getTexture(), bounds);
185 layer->setPlot(plot);
186 layer->setLocked(true);
187 fPlotLocks[layer->plot()->id()]++;
188 return false;
189 }
190
191 // The layer was rejected by the atlas (even though we know it is
192 // plausibly atlas-able). See if a plot can be purged and try again.
193 if (!this->purgePlot()) {
194 break; // We weren't able to purge any plots
195 }
robertphillips261b8a92014-07-17 08:26:44 -0700196 }
robertphillips952841b2014-06-30 08:26:50 -0700197 }
198#endif
199
robertphillips21048b52014-07-15 19:46:35 -0700200 // The texture wouldn't fit in the cache - give it it's own texture.
robertphillips952841b2014-06-30 08:26:50 -0700201 // This path always uses a new scratch texture and (thus) doesn't cache anything.
robertphillips4ec84da2014-06-24 13:10:43 -0700202 // This can yield a lot of re-rendering
robertphillips952841b2014-06-30 08:26:50 -0700203 layer->setTexture(fContext->lockAndRefScratchTexture(desc, GrContext::kApprox_ScratchTexMatch),
robertphillips21048b52014-07-15 19:46:35 -0700204 GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
robertphillips320c9232014-07-29 06:07:19 -0700205 layer->setLocked(true);
robertphillips4ec84da2014-06-24 13:10:43 -0700206 return false;
207}
208
209void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips21048b52014-07-15 19:46:35 -0700210 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
211
robertphillips320c9232014-07-29 06:07:19 -0700212 if (NULL == layer || !layer->locked()) {
213 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700214 return;
215 }
216
robertphillips21048b52014-07-15 19:46:35 -0700217 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700218 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700219
robertphillips320c9232014-07-29 06:07:19 -0700220 SkASSERT(fPlotLocks[plotID] > 0);
221 fPlotLocks[plotID]--;
222 // At this point we could aggressively clear out un-locked plots but
223 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips21048b52014-07-15 19:46:35 -0700224 } else {
robertphillips952841b2014-06-30 08:26:50 -0700225 fContext->unlockScratchTexture(layer->texture());
226 layer->setTexture(NULL, GrIRect16::MakeEmpty());
227 }
robertphillips320c9232014-07-29 06:07:19 -0700228
229 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700230}
231
robertphillips21048b52014-07-15 19:46:35 -0700232#ifdef SK_DEBUG
233void GrLayerCache::validate() const {
robertphillips320c9232014-07-29 06:07:19 -0700234 int plotLocks[kNumPlotsX * kNumPlotsY];
235 memset(plotLocks, 0, sizeof(plotLocks));
236
robertphillips3d533ac2014-07-20 09:40:00 -0700237 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
238 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700239 const GrCachedLayer* layer = &(*iter);
240
241 layer->validate(fAtlas->getTexture());
242
243 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
244 if (NULL != pictInfo) {
245 // In aggressive cleanup mode a picture info should only exist if
246 // it has some atlased layers
247 SkASSERT(!pictInfo->fPlotUsage.isEmpty());
248 } else {
249 // If there is no picture info for this layer then all of its
250 // layers should be non-atlased.
251 SkASSERT(!layer->isAtlased());
252 }
253
254 if (NULL != layer->plot()) {
255 SkASSERT(NULL != pictInfo);
256 SkASSERT(pictInfo->fPictureID == layer->pictureID());
257
258 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
259
260 if (layer->locked()) {
261 plotLocks[layer->plot()->id()]++;
262 }
263 }
264 }
265
266 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
267 SkASSERT(plotLocks[i] == fPlotLocks[i]);
robertphillips21048b52014-07-15 19:46:35 -0700268 }
269}
270
271class GrAutoValidateCache : ::SkNoncopyable {
272public:
273 explicit GrAutoValidateCache(GrLayerCache* cache)
274 : fCache(cache) {
275 fCache->validate();
276 }
277 ~GrAutoValidateCache() {
278 fCache->validate();
279 }
280private:
281 GrLayerCache* fCache;
282};
283#endif
284
robertphillipsd771f6b2014-07-22 10:18:06 -0700285void GrLayerCache::purge(uint32_t pictureID) {
286
robertphillips21048b52014-07-15 19:46:35 -0700287 SkDEBUGCODE(GrAutoValidateCache avc(this);)
288
robertphillips3d533ac2014-07-20 09:40:00 -0700289 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700290 SkTDArray<GrCachedLayer*> toBeRemoved;
291
robertphillips3d533ac2014-07-20 09:40:00 -0700292 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
293 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700294 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700295 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700296 }
297 }
298
299 for (int i = 0; i < toBeRemoved.count(); ++i) {
300 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700301 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700302 SkDELETE(toBeRemoved[i]);
303 }
robertphillips261b8a92014-07-17 08:26:44 -0700304
robertphillipsd771f6b2014-07-22 10:18:06 -0700305 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700306 if (NULL != pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700307 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700308 SkDELETE(pictInfo);
309 }
robertphillips4ec84da2014-06-24 13:10:43 -0700310}
robertphillipsd771f6b2014-07-22 10:18:06 -0700311
robertphillips320c9232014-07-29 06:07:19 -0700312bool GrLayerCache::purgePlot() {
313 SkDEBUGCODE(GrAutoValidateCache avc(this);)
314
315 GrAtlas::PlotIter iter;
316 GrPlot* plot;
317 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
318 NULL != plot;
319 plot = iter.prev()) {
320 if (fPlotLocks[plot->id()] > 0) {
321 continue;
322 }
323
324 // We need to find all the layers in 'plot' and remove them.
325 SkTDArray<GrCachedLayer*> toBeRemoved;
326
327 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
328 for (; !iter.done(); ++iter) {
329 if (plot == (*iter).plot()) {
330 *toBeRemoved.append() = &(*iter);
331 }
332 }
333
334 for (int i = 0; i < toBeRemoved.count(); ++i) {
335 SkASSERT(!toBeRemoved[i]->locked());
336
337 GrPictureInfo* pictInfo = fPictureHash.find(toBeRemoved[i]->pictureID());
338 SkASSERT(NULL != pictInfo);
339
340 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
341
342 // Aggressively remove layers and, if now totally uncached, picture info
343 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
344 SkDELETE(toBeRemoved[i]);
345
346 if (pictInfo->fPlotUsage.isEmpty()) {
347 fPictureHash.remove(pictInfo->fPictureID);
348 SkDELETE(pictInfo);
349 }
350 }
351
352 plot->resetRects();
353 return true;
354 }
355
356 return false;
357}
358
robertphillipsd771f6b2014-07-22 10:18:06 -0700359class GrPictureDeletionListener : public SkPicture::DeletionListener {
360 virtual void onDeletion(uint32_t pictureID) SK_OVERRIDE{
361 const GrPictureDeletedMessage message = { pictureID };
362 SkMessageBus<GrPictureDeletedMessage>::Post(message);
363 }
364};
365
366void GrLayerCache::trackPicture(const SkPicture* picture) {
367 if (NULL == fDeletionListener) {
368 fDeletionListener.reset(SkNEW(GrPictureDeletionListener));
369 }
370
371 picture->addDeletionListener(fDeletionListener);
372}
373
374void GrLayerCache::processDeletedPictures() {
375 SkTDArray<GrPictureDeletedMessage> deletedPictures;
376 fPictDeletionInbox.poll(&deletedPictures);
377
378 for (int i = 0; i < deletedPictures.count(); i++) {
379 this->purge(deletedPictures[i].pictureID);
380 }
381}
382