blob: 32f5a5b6c44bf1e5a56cb4dab7cd8bb94ec1045a [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);
30 }
31
32 if (NULL != fPlot) {
33 // If a layer has a plot (i.e., is atlased) then it must point to
34 // the backing texture. Additionally, its rect should be non-empty.
35 SkASSERT(NULL != fTexture && backingTexture == fTexture);
36 SkASSERT(!fRect.isEmpty());
robertphillips21048b52014-07-15 19:46:35 -070037 }
38}
39
40class GrAutoValidateLayer : ::SkNoncopyable {
41public:
42 GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer)
43 : fBackingTexture(backingTexture)
44 , fLayer(layer) {
45 if (NULL != fLayer) {
46 fLayer->validate(backingTexture);
47 }
48 }
49 ~GrAutoValidateLayer() {
50 if (NULL != fLayer) {
51 fLayer->validate(fBackingTexture);
52 }
53 }
robertphillips261b8a92014-07-17 08:26:44 -070054 void setBackingTexture(GrTexture* backingTexture) {
55 SkASSERT(NULL == fBackingTexture || fBackingTexture == backingTexture);
56 fBackingTexture = backingTexture;
57 }
robertphillips21048b52014-07-15 19:46:35 -070058
59private:
robertphillips261b8a92014-07-17 08:26:44 -070060 const GrTexture* fBackingTexture;
robertphillips21048b52014-07-15 19:46:35 -070061 const GrCachedLayer* fLayer;
62};
63#endif
64
robertphillips4ec84da2014-06-24 13:10:43 -070065GrLayerCache::GrLayerCache(GrContext* context)
robertphillips952841b2014-06-30 08:26:50 -070066 : fContext(context) {
67 this->initAtlas();
robertphillips@google.come930a072014-04-03 00:34:27 +000068}
69
70GrLayerCache::~GrLayerCache() {
robertphillips952841b2014-06-30 08:26:50 -070071
robertphillips3d533ac2014-07-20 09:40:00 -070072 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
73 for (; !iter.done(); ++iter) {
74 GrCachedLayer* layer = &(*iter);
75 this->unlock(layer);
76 SkDELETE(layer);
77 }
robertphillips952841b2014-06-30 08:26:50 -070078
79 // The atlas only lets go of its texture when the atlas is deleted.
80 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +000081}
82
robertphillips952841b2014-06-30 08:26:50 -070083void GrLayerCache::initAtlas() {
robertphillips@google.come930a072014-04-03 00:34:27 +000084 static const int kAtlasTextureWidth = 1024;
85 static const int kAtlasTextureHeight = 1024;
86
robertphillips1d86ee82014-06-24 15:08:49 -070087 SkASSERT(NULL == fAtlas.get());
robertphillips@google.come930a072014-04-03 00:34:27 +000088
89 // The layer cache only gets 1 plot
90 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight);
robertphillips1d86ee82014-06-24 15:08:49 -070091 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig,
robertphillips952841b2014-06-30 08:26:50 -070092 kRenderTarget_GrTextureFlagBit,
robertphillips261b8a92014-07-17 08:26:44 -070093 textureSize, kNumPlotsX, kNumPlotsY, false)));
robertphillips@google.come930a072014-04-03 00:34:27 +000094}
95
96void GrLayerCache::freeAll() {
robertphillips952841b2014-06-30 08:26:50 -070097
robertphillips3d533ac2014-07-20 09:40:00 -070098 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
99 for (; !iter.done(); ++iter) {
100 GrCachedLayer* layer = &(*iter);
101 this->unlock(layer);
102 SkDELETE(layer);
103 }
104 fLayerHash.rewind();
robertphillips952841b2014-06-30 08:26:50 -0700105
106 // The atlas only lets go of its texture when the atlas is deleted.
robertphillips1d86ee82014-06-24 15:08:49 -0700107 fAtlas.free();
robertphillips952841b2014-06-30 08:26:50 -0700108 // GrLayerCache always assumes an atlas exists so recreate it. The atlas
109 // lazily allocates a replacement texture so reallocating a new
110 // atlas here won't disrupt a GrContext::contextDestroyed or freeGpuResources.
111 // TODO: Make GrLayerCache lazily allocate the atlas manager?
112 this->initAtlas();
robertphillips@google.come930a072014-04-03 00:34:27 +0000113}
114
robertphillips9b14f262014-06-04 05:40:44 -0700115GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID) {
robertphillips3d533ac2014-07-20 09:40:00 -0700116 SkASSERT(picture->uniqueID() != SK_InvalidGenID && layerID >= 0);
robertphillips952841b2014-06-30 08:26:50 -0700117
118 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (picture->uniqueID(), layerID));
robertphillips3d533ac2014-07-20 09:40:00 -0700119 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000120 return layer;
121}
122
robertphillips4ec84da2014-06-24 13:10:43 -0700123GrCachedLayer* GrLayerCache::findLayer(const SkPicture* picture, int layerID) {
robertphillips3d533ac2014-07-20 09:40:00 -0700124 SkASSERT(picture->uniqueID() != SK_InvalidGenID && layerID >= 0);
125 return fLayerHash.find(GrCachedLayer::Key(picture->uniqueID(), layerID));
robertphillips4ec84da2014-06-24 13:10:43 -0700126}
robertphillips@google.come930a072014-04-03 00:34:27 +0000127
robertphillips9b14f262014-06-04 05:40:44 -0700128GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int layerID) {
robertphillips3d533ac2014-07-20 09:40:00 -0700129 SkASSERT(picture->uniqueID() != SK_InvalidGenID && layerID >= 0);
130 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(picture->uniqueID(), layerID));
robertphillips@google.come930a072014-04-03 00:34:27 +0000131 if (NULL == layer) {
132 layer = this->createLayer(picture, layerID);
133 }
robertphillips4ec84da2014-06-24 13:10:43 -0700134
robertphillips@google.come930a072014-04-03 00:34:27 +0000135 return layer;
136}
robertphillips4ec84da2014-06-24 13:10:43 -0700137
138bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc) {
robertphillips21048b52014-07-15 19:46:35 -0700139 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700140
robertphillips952841b2014-06-30 08:26:50 -0700141 if (NULL != layer->texture()) {
142 // This layer is already locked
143#ifdef SK_DEBUG
robertphillips21048b52014-07-15 19:46:35 -0700144 if (layer->isAtlased()) {
robertphillips952841b2014-06-30 08:26:50 -0700145 // It claims to be atlased
146 SkASSERT(layer->rect().width() == desc.fWidth);
147 SkASSERT(layer->rect().height() == desc.fHeight);
148 }
149#endif
150 return true;
151 }
152
153#if USE_ATLAS
robertphillips261b8a92014-07-17 08:26:44 -0700154 {
robertphillips3d533ac2014-07-20 09:40:00 -0700155 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700156 if (NULL == pictInfo) {
157 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700158 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700159 }
160
161 SkIPoint16 loc;
162 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
163 desc.fWidth, desc.fHeight,
164 NULL, &loc);
165 // addToAtlas can allocate the backing texture
166 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
167 if (NULL != plot) {
168 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY,
169 SkToS16(desc.fWidth), SkToS16(desc.fHeight));
170 layer->setTexture(fAtlas->getTexture(), bounds);
171 layer->setPlot(plot);
172 return false;
173 }
robertphillips952841b2014-06-30 08:26:50 -0700174 }
175#endif
176
robertphillips21048b52014-07-15 19:46:35 -0700177 // The texture wouldn't fit in the cache - give it it's own texture.
robertphillips952841b2014-06-30 08:26:50 -0700178 // This path always uses a new scratch texture and (thus) doesn't cache anything.
robertphillips4ec84da2014-06-24 13:10:43 -0700179 // This can yield a lot of re-rendering
robertphillips952841b2014-06-30 08:26:50 -0700180 layer->setTexture(fContext->lockAndRefScratchTexture(desc, GrContext::kApprox_ScratchTexMatch),
robertphillips21048b52014-07-15 19:46:35 -0700181 GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
robertphillips4ec84da2014-06-24 13:10:43 -0700182 return false;
183}
184
185void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips21048b52014-07-15 19:46:35 -0700186 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
187
robertphillips952841b2014-06-30 08:26:50 -0700188 if (NULL == layer || NULL == layer->texture()) {
robertphillips4ec84da2014-06-24 13:10:43 -0700189 return;
190 }
191
robertphillips21048b52014-07-15 19:46:35 -0700192 if (layer->isAtlased()) {
robertphillips261b8a92014-07-17 08:26:44 -0700193 SkASSERT(layer->texture() == fAtlas->getTexture());
194
robertphillips3d533ac2014-07-20 09:40:00 -0700195 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700196 SkASSERT(NULL != pictInfo);
197 pictInfo->fPlotUsage.isEmpty(); // just to silence compiler warnings for the time being
198
199 // TODO: purging from atlas goes here
robertphillips21048b52014-07-15 19:46:35 -0700200 } else {
robertphillips952841b2014-06-30 08:26:50 -0700201 fContext->unlockScratchTexture(layer->texture());
202 layer->setTexture(NULL, GrIRect16::MakeEmpty());
203 }
204}
205
robertphillips21048b52014-07-15 19:46:35 -0700206#ifdef SK_DEBUG
207void GrLayerCache::validate() const {
robertphillips3d533ac2014-07-20 09:40:00 -0700208 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
209 for (; !iter.done(); ++iter) {
210 (*iter).validate(fAtlas->getTexture());
robertphillips21048b52014-07-15 19:46:35 -0700211 }
212}
213
214class GrAutoValidateCache : ::SkNoncopyable {
215public:
216 explicit GrAutoValidateCache(GrLayerCache* cache)
217 : fCache(cache) {
218 fCache->validate();
219 }
220 ~GrAutoValidateCache() {
221 fCache->validate();
222 }
223private:
224 GrLayerCache* fCache;
225};
226#endif
227
robertphillipsd771f6b2014-07-22 10:18:06 -0700228void GrLayerCache::purge(uint32_t pictureID) {
229
robertphillips21048b52014-07-15 19:46:35 -0700230 SkDEBUGCODE(GrAutoValidateCache avc(this);)
231
robertphillips3d533ac2014-07-20 09:40:00 -0700232 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700233 SkTDArray<GrCachedLayer*> toBeRemoved;
234
robertphillips3d533ac2014-07-20 09:40:00 -0700235 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
236 for (; !iter.done(); ++iter) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700237 if (pictureID == (*iter).pictureID()) {
robertphillips3d533ac2014-07-20 09:40:00 -0700238 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700239 }
240 }
241
242 for (int i = 0; i < toBeRemoved.count(); ++i) {
243 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700244 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700245 SkDELETE(toBeRemoved[i]);
246 }
robertphillips261b8a92014-07-17 08:26:44 -0700247
robertphillipsd771f6b2014-07-22 10:18:06 -0700248 GrPictureInfo* pictInfo = fPictureHash.find(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700249 if (NULL != pictInfo) {
robertphillipsd771f6b2014-07-22 10:18:06 -0700250 fPictureHash.remove(pictureID);
robertphillips261b8a92014-07-17 08:26:44 -0700251 SkDELETE(pictInfo);
252 }
robertphillips4ec84da2014-06-24 13:10:43 -0700253}
robertphillipsd771f6b2014-07-22 10:18:06 -0700254
255class GrPictureDeletionListener : public SkPicture::DeletionListener {
256 virtual void onDeletion(uint32_t pictureID) SK_OVERRIDE{
257 const GrPictureDeletedMessage message = { pictureID };
258 SkMessageBus<GrPictureDeletedMessage>::Post(message);
259 }
260};
261
262void GrLayerCache::trackPicture(const SkPicture* picture) {
263 if (NULL == fDeletionListener) {
264 fDeletionListener.reset(SkNEW(GrPictureDeletionListener));
265 }
266
267 picture->addDeletionListener(fDeletionListener);
268}
269
270void GrLayerCache::processDeletedPictures() {
271 SkTDArray<GrPictureDeletedMessage> deletedPictures;
272 fPictDeletionInbox.poll(&deletedPictures);
273
274 for (int i = 0; i < deletedPictures.count(); i++) {
275 this->purge(deletedPictures[i].pictureID);
276 }
277}
278