blob: 58994790b732e3b3e515652e85c3887449020904 [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
robertphillips320c9232014-07-29 06:07:19 -0700158 if (layer->isAtlased()) {
159 // Hooray it is still in the atlas - make sure it stays there
160 layer->setLocked(true);
161 fPlotLocks[layer->plot()->id()]++;
162 return true;
163 } else if (PlausiblyAtlasable(desc.fWidth, desc.fHeight)) {
164 // Not in the atlas - will it fit?
robertphillips3d533ac2014-07-20 09:40:00 -0700165 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700166 if (NULL == pictInfo) {
167 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700168 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700169 }
170
171 SkIPoint16 loc;
robertphillips320c9232014-07-29 06:07:19 -0700172 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but are able to purge
173 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
174 desc.fWidth, desc.fHeight,
175 NULL, &loc);
176 // addToAtlas can allocate the backing texture
177 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
178 if (NULL != plot) {
179 // The layer was successfully added to the atlas
180 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY,
181 SkToS16(desc.fWidth),
182 SkToS16(desc.fHeight));
183 layer->setTexture(fAtlas->getTexture(), bounds);
184 layer->setPlot(plot);
185 layer->setLocked(true);
186 fPlotLocks[layer->plot()->id()]++;
187 return false;
188 }
189
190 // The layer was rejected by the atlas (even though we know it is
191 // plausibly atlas-able). See if a plot can be purged and try again.
192 if (!this->purgePlot()) {
193 break; // We weren't able to purge any plots
194 }
robertphillips261b8a92014-07-17 08:26:44 -0700195 }
robertphillips952841b2014-06-30 08:26:50 -0700196 }
robertphillips952841b2014-06-30 08:26:50 -0700197
robertphillips21048b52014-07-15 19:46:35 -0700198 // The texture wouldn't fit in the cache - give it it's own texture.
robertphillips952841b2014-06-30 08:26:50 -0700199 // This path always uses a new scratch texture and (thus) doesn't cache anything.
robertphillips4ec84da2014-06-24 13:10:43 -0700200 // This can yield a lot of re-rendering
robertphillips952841b2014-06-30 08:26:50 -0700201 layer->setTexture(fContext->lockAndRefScratchTexture(desc, GrContext::kApprox_ScratchTexMatch),
robertphillips21048b52014-07-15 19:46:35 -0700202 GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
robertphillips320c9232014-07-29 06:07:19 -0700203 layer->setLocked(true);
robertphillips4ec84da2014-06-24 13:10:43 -0700204 return false;
205}
206
207void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips21048b52014-07-15 19:46:35 -0700208 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
209
robertphillips320c9232014-07-29 06:07:19 -0700210 if (NULL == layer || !layer->locked()) {
211 // invalid or not locked
robertphillips4ec84da2014-06-24 13:10:43 -0700212 return;
213 }
214
robertphillips21048b52014-07-15 19:46:35 -0700215 if (layer->isAtlased()) {
robertphillips320c9232014-07-29 06:07:19 -0700216 const int plotID = layer->plot()->id();
robertphillips261b8a92014-07-17 08:26:44 -0700217
robertphillips320c9232014-07-29 06:07:19 -0700218 SkASSERT(fPlotLocks[plotID] > 0);
219 fPlotLocks[plotID]--;
220 // At this point we could aggressively clear out un-locked plots but
221 // by delaying we may be able to reuse some of the atlased layers later.
robertphillips21048b52014-07-15 19:46:35 -0700222 } else {
robertphillips952841b2014-06-30 08:26:50 -0700223 fContext->unlockScratchTexture(layer->texture());
224 layer->setTexture(NULL, GrIRect16::MakeEmpty());
225 }
robertphillips320c9232014-07-29 06:07:19 -0700226
227 layer->setLocked(false);
robertphillips952841b2014-06-30 08:26:50 -0700228}
229
robertphillips21048b52014-07-15 19:46:35 -0700230#ifdef SK_DEBUG
231void GrLayerCache::validate() const {
robertphillips320c9232014-07-29 06:07:19 -0700232 int plotLocks[kNumPlotsX * kNumPlotsY];
233 memset(plotLocks, 0, sizeof(plotLocks));
234
robertphillips3d533ac2014-07-20 09:40:00 -0700235 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
236 for (; !iter.done(); ++iter) {
robertphillips320c9232014-07-29 06:07:19 -0700237 const GrCachedLayer* layer = &(*iter);
238
239 layer->validate(fAtlas->getTexture());
240
241 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
242 if (NULL != pictInfo) {
243 // In aggressive cleanup mode a picture info should only exist if
244 // it has some atlased layers
245 SkASSERT(!pictInfo->fPlotUsage.isEmpty());
246 } else {
247 // If there is no picture info for this layer then all of its
248 // layers should be non-atlased.
249 SkASSERT(!layer->isAtlased());
250 }
251
252 if (NULL != layer->plot()) {
253 SkASSERT(NULL != pictInfo);
254 SkASSERT(pictInfo->fPictureID == layer->pictureID());
255
256 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot()));
257
258 if (layer->locked()) {
259 plotLocks[layer->plot()->id()]++;
260 }
261 }
262 }
263
264 for (int i = 0; i < kNumPlotsX*kNumPlotsY; ++i) {
265 SkASSERT(plotLocks[i] == fPlotLocks[i]);
robertphillips21048b52014-07-15 19:46:35 -0700266 }
267}
268
269class GrAutoValidateCache : ::SkNoncopyable {
270public:
271 explicit GrAutoValidateCache(GrLayerCache* cache)
272 : fCache(cache) {
273 fCache->validate();
274 }
275 ~GrAutoValidateCache() {
276 fCache->validate();
277 }
278private:
279 GrLayerCache* fCache;
280};
281#endif
282
robertphillipsd771f6b2014-07-22 10:18:06 -0700283void GrLayerCache::purge(uint32_t pictureID) {
284
robertphillips21048b52014-07-15 19:46:35 -0700285 SkDEBUGCODE(GrAutoValidateCache avc(this);)
286
robertphillips3d533ac2014-07-20 09:40:00 -0700287 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700288 SkTDArray<GrCachedLayer*> toBeRemoved;
289
robertphillips3d533ac2014-07-20 09:40:00 -0700290 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
291 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700292 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700293 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700294 }
295 }
296
297 for (int i = 0; i < toBeRemoved.count(); ++i) {
298 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700299 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700300 SkDELETE(toBeRemoved[i]);
301 }
robertphillips261b8a92014-07-17 08:26:44 -0700302
robertphillipsd771f6b2014-07-22 10:18:06 -0700303 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700304 if (NULL != pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700305 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700306 SkDELETE(pictInfo);
307 }
robertphillips4ec84da2014-06-24 13:10:43 -0700308}
robertphillipsd771f6b2014-07-22 10:18:06 -0700309
robertphillips320c9232014-07-29 06:07:19 -0700310bool GrLayerCache::purgePlot() {
311 SkDEBUGCODE(GrAutoValidateCache avc(this);)
312
313 GrAtlas::PlotIter iter;
314 GrPlot* plot;
315 for (plot = fAtlas->iterInit(&iter, GrAtlas::kLRUFirst_IterOrder);
316 NULL != plot;
317 plot = iter.prev()) {
318 if (fPlotLocks[plot->id()] > 0) {
319 continue;
320 }
321
322 // We need to find all the layers in 'plot' and remove them.
323 SkTDArray<GrCachedLayer*> toBeRemoved;
324
325 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
326 for (; !iter.done(); ++iter) {
327 if (plot == (*iter).plot()) {
328 *toBeRemoved.append() = &(*iter);
329 }
330 }
331
332 for (int i = 0; i < toBeRemoved.count(); ++i) {
333 SkASSERT(!toBeRemoved[i]->locked());
334
335 GrPictureInfo* pictInfo = fPictureHash.find(toBeRemoved[i]->pictureID());
336 SkASSERT(NULL != pictInfo);
337
338 GrAtlas::RemovePlot(&pictInfo->fPlotUsage, plot);
339
340 // Aggressively remove layers and, if now totally uncached, picture info
341 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
342 SkDELETE(toBeRemoved[i]);
343
344 if (pictInfo->fPlotUsage.isEmpty()) {
345 fPictureHash.remove(pictInfo->fPictureID);
346 SkDELETE(pictInfo);
347 }
348 }
349
350 plot->resetRects();
351 return true;
352 }
353
354 return false;
355}
356
robertphillipsd771f6b2014-07-22 10:18:06 -0700357class GrPictureDeletionListener : public SkPicture::DeletionListener {
358 virtual void onDeletion(uint32_t pictureID) SK_OVERRIDE{
359 const GrPictureDeletedMessage message = { pictureID };
360 SkMessageBus<GrPictureDeletedMessage>::Post(message);
361 }
362};
363
364void GrLayerCache::trackPicture(const SkPicture* picture) {
365 if (NULL == fDeletionListener) {
366 fDeletionListener.reset(SkNEW(GrPictureDeletionListener));
367 }
368
369 picture->addDeletionListener(fDeletionListener);
370}
371
372void GrLayerCache::processDeletedPictures() {
373 SkTDArray<GrPictureDeletedMessage> deletedPictures;
374 fPictDeletionInbox.poll(&deletedPictures);
375
376 for (int i = 0; i < deletedPictures.count(); i++) {
377 this->purge(deletedPictures[i].pictureID);
378 }
379}
380