blob: 1a0923004ba11dc2ea83c1a7a149e66251de2441 [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
robertphillips21048b52014-07-15 19:46:35 -070012#ifdef SK_DEBUG
robertphillips261b8a92014-07-17 08:26:44 -070013void GrCachedLayer::validate(const GrTexture* backingTexture) const {
robertphillips3d533ac2014-07-20 09:40:00 -070014 SkASSERT(SK_InvalidGenID != fKey.getPictureID());
15 SkASSERT(-1 != fKey.getLayerID());
16
robertphillips21048b52014-07-15 19:46:35 -070017
18 if (NULL != fTexture) {
19 // If the layer is in some texture then it must occupy some rectangle
20 SkASSERT(!fRect.isEmpty());
21 if (!this->isAtlased()) {
22 // If it isn't atlased then the rectangle should start at the origin
23 SkASSERT(0.0f == fRect.fLeft && 0.0f == fRect.fTop);
24 }
25 } else {
26 SkASSERT(fRect.isEmpty());
robertphillips261b8a92014-07-17 08:26:44 -070027 SkASSERT(NULL == fPlot);
28 }
29
30 if (NULL != fPlot) {
31 // If a layer has a plot (i.e., is atlased) then it must point to
32 // the backing texture. Additionally, its rect should be non-empty.
33 SkASSERT(NULL != fTexture && backingTexture == fTexture);
34 SkASSERT(!fRect.isEmpty());
robertphillips21048b52014-07-15 19:46:35 -070035 }
36}
37
38class GrAutoValidateLayer : ::SkNoncopyable {
39public:
40 GrAutoValidateLayer(GrTexture* backingTexture, const GrCachedLayer* layer)
41 : fBackingTexture(backingTexture)
42 , fLayer(layer) {
43 if (NULL != fLayer) {
44 fLayer->validate(backingTexture);
45 }
46 }
47 ~GrAutoValidateLayer() {
48 if (NULL != fLayer) {
49 fLayer->validate(fBackingTexture);
50 }
51 }
robertphillips261b8a92014-07-17 08:26:44 -070052 void setBackingTexture(GrTexture* backingTexture) {
53 SkASSERT(NULL == fBackingTexture || fBackingTexture == backingTexture);
54 fBackingTexture = backingTexture;
55 }
robertphillips21048b52014-07-15 19:46:35 -070056
57private:
robertphillips261b8a92014-07-17 08:26:44 -070058 const GrTexture* fBackingTexture;
robertphillips21048b52014-07-15 19:46:35 -070059 const GrCachedLayer* fLayer;
60};
61#endif
62
robertphillips4ec84da2014-06-24 13:10:43 -070063GrLayerCache::GrLayerCache(GrContext* context)
robertphillips952841b2014-06-30 08:26:50 -070064 : fContext(context) {
65 this->initAtlas();
robertphillips@google.come930a072014-04-03 00:34:27 +000066}
67
68GrLayerCache::~GrLayerCache() {
robertphillips952841b2014-06-30 08:26:50 -070069
robertphillips3d533ac2014-07-20 09:40:00 -070070 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
71 for (; !iter.done(); ++iter) {
72 GrCachedLayer* layer = &(*iter);
73 this->unlock(layer);
74 SkDELETE(layer);
75 }
robertphillips952841b2014-06-30 08:26:50 -070076
77 // The atlas only lets go of its texture when the atlas is deleted.
78 fAtlas.free();
robertphillips@google.come930a072014-04-03 00:34:27 +000079}
80
robertphillips952841b2014-06-30 08:26:50 -070081void GrLayerCache::initAtlas() {
robertphillips@google.come930a072014-04-03 00:34:27 +000082 static const int kAtlasTextureWidth = 1024;
83 static const int kAtlasTextureHeight = 1024;
84
robertphillips1d86ee82014-06-24 15:08:49 -070085 SkASSERT(NULL == fAtlas.get());
robertphillips@google.come930a072014-04-03 00:34:27 +000086
87 // The layer cache only gets 1 plot
88 SkISize textureSize = SkISize::Make(kAtlasTextureWidth, kAtlasTextureHeight);
robertphillips1d86ee82014-06-24 15:08:49 -070089 fAtlas.reset(SkNEW_ARGS(GrAtlas, (fContext->getGpu(), kSkia8888_GrPixelConfig,
robertphillips952841b2014-06-30 08:26:50 -070090 kRenderTarget_GrTextureFlagBit,
robertphillips261b8a92014-07-17 08:26:44 -070091 textureSize, kNumPlotsX, kNumPlotsY, false)));
robertphillips@google.come930a072014-04-03 00:34:27 +000092}
93
94void GrLayerCache::freeAll() {
robertphillips952841b2014-06-30 08:26:50 -070095
robertphillips3d533ac2014-07-20 09:40:00 -070096 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
97 for (; !iter.done(); ++iter) {
98 GrCachedLayer* layer = &(*iter);
99 this->unlock(layer);
100 SkDELETE(layer);
101 }
102 fLayerHash.rewind();
robertphillips952841b2014-06-30 08:26:50 -0700103
104 // The atlas only lets go of its texture when the atlas is deleted.
robertphillips1d86ee82014-06-24 15:08:49 -0700105 fAtlas.free();
robertphillips952841b2014-06-30 08:26:50 -0700106 // GrLayerCache always assumes an atlas exists so recreate it. The atlas
107 // lazily allocates a replacement texture so reallocating a new
108 // atlas here won't disrupt a GrContext::contextDestroyed or freeGpuResources.
109 // TODO: Make GrLayerCache lazily allocate the atlas manager?
110 this->initAtlas();
robertphillips@google.come930a072014-04-03 00:34:27 +0000111}
112
robertphillips9b14f262014-06-04 05:40:44 -0700113GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID) {
robertphillips3d533ac2014-07-20 09:40:00 -0700114 SkASSERT(picture->uniqueID() != SK_InvalidGenID && layerID >= 0);
robertphillips952841b2014-06-30 08:26:50 -0700115
116 GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (picture->uniqueID(), layerID));
robertphillips3d533ac2014-07-20 09:40:00 -0700117 fLayerHash.add(layer);
robertphillips@google.come930a072014-04-03 00:34:27 +0000118 return layer;
119}
120
robertphillips4ec84da2014-06-24 13:10:43 -0700121GrCachedLayer* GrLayerCache::findLayer(const SkPicture* picture, int layerID) {
robertphillips3d533ac2014-07-20 09:40:00 -0700122 SkASSERT(picture->uniqueID() != SK_InvalidGenID && layerID >= 0);
123 return fLayerHash.find(GrCachedLayer::Key(picture->uniqueID(), layerID));
robertphillips4ec84da2014-06-24 13:10:43 -0700124}
robertphillips@google.come930a072014-04-03 00:34:27 +0000125
robertphillips9b14f262014-06-04 05:40:44 -0700126GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int layerID) {
robertphillips3d533ac2014-07-20 09:40:00 -0700127 SkASSERT(picture->uniqueID() != SK_InvalidGenID && layerID >= 0);
128 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(picture->uniqueID(), layerID));
robertphillips@google.come930a072014-04-03 00:34:27 +0000129 if (NULL == layer) {
130 layer = this->createLayer(picture, layerID);
131 }
robertphillips4ec84da2014-06-24 13:10:43 -0700132
robertphillips@google.come930a072014-04-03 00:34:27 +0000133 return layer;
134}
robertphillips4ec84da2014-06-24 13:10:43 -0700135
136bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc) {
robertphillips21048b52014-07-15 19:46:35 -0700137 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
robertphillips4ec84da2014-06-24 13:10:43 -0700138
robertphillips952841b2014-06-30 08:26:50 -0700139 if (NULL != layer->texture()) {
140 // This layer is already locked
141#ifdef SK_DEBUG
robertphillips21048b52014-07-15 19:46:35 -0700142 if (layer->isAtlased()) {
robertphillips952841b2014-06-30 08:26:50 -0700143 // It claims to be atlased
144 SkASSERT(layer->rect().width() == desc.fWidth);
145 SkASSERT(layer->rect().height() == desc.fHeight);
146 }
147#endif
148 return true;
149 }
150
151#if USE_ATLAS
robertphillips261b8a92014-07-17 08:26:44 -0700152 {
robertphillips3d533ac2014-07-20 09:40:00 -0700153 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700154 if (NULL == pictInfo) {
155 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID()));
robertphillips3d533ac2014-07-20 09:40:00 -0700156 fPictureHash.add(pictInfo);
robertphillips261b8a92014-07-17 08:26:44 -0700157 }
158
159 SkIPoint16 loc;
160 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage,
161 desc.fWidth, desc.fHeight,
162 NULL, &loc);
163 // addToAtlas can allocate the backing texture
164 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture()));
165 if (NULL != plot) {
166 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY,
167 SkToS16(desc.fWidth), SkToS16(desc.fHeight));
168 layer->setTexture(fAtlas->getTexture(), bounds);
169 layer->setPlot(plot);
170 return false;
171 }
robertphillips952841b2014-06-30 08:26:50 -0700172 }
173#endif
174
robertphillips21048b52014-07-15 19:46:35 -0700175 // The texture wouldn't fit in the cache - give it it's own texture.
robertphillips952841b2014-06-30 08:26:50 -0700176 // This path always uses a new scratch texture and (thus) doesn't cache anything.
robertphillips4ec84da2014-06-24 13:10:43 -0700177 // This can yield a lot of re-rendering
robertphillips952841b2014-06-30 08:26:50 -0700178 layer->setTexture(fContext->lockAndRefScratchTexture(desc, GrContext::kApprox_ScratchTexMatch),
robertphillips21048b52014-07-15 19:46:35 -0700179 GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.fHeight)));
robertphillips4ec84da2014-06-24 13:10:43 -0700180 return false;
181}
182
183void GrLayerCache::unlock(GrCachedLayer* layer) {
robertphillips21048b52014-07-15 19:46:35 -0700184 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);)
185
robertphillips952841b2014-06-30 08:26:50 -0700186 if (NULL == layer || NULL == layer->texture()) {
robertphillips4ec84da2014-06-24 13:10:43 -0700187 return;
188 }
189
robertphillips21048b52014-07-15 19:46:35 -0700190 if (layer->isAtlased()) {
robertphillips261b8a92014-07-17 08:26:44 -0700191 SkASSERT(layer->texture() == fAtlas->getTexture());
192
robertphillips3d533ac2014-07-20 09:40:00 -0700193 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID());
robertphillips261b8a92014-07-17 08:26:44 -0700194 SkASSERT(NULL != pictInfo);
195 pictInfo->fPlotUsage.isEmpty(); // just to silence compiler warnings for the time being
196
197 // TODO: purging from atlas goes here
robertphillips21048b52014-07-15 19:46:35 -0700198 } else {
robertphillips952841b2014-06-30 08:26:50 -0700199 fContext->unlockScratchTexture(layer->texture());
200 layer->setTexture(NULL, GrIRect16::MakeEmpty());
201 }
202}
203
robertphillips21048b52014-07-15 19:46:35 -0700204#ifdef SK_DEBUG
205void GrLayerCache::validate() const {
robertphillips3d533ac2014-07-20 09:40:00 -0700206 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash);
207 for (; !iter.done(); ++iter) {
208 (*iter).validate(fAtlas->getTexture());
robertphillips21048b52014-07-15 19:46:35 -0700209 }
210}
211
212class GrAutoValidateCache : ::SkNoncopyable {
213public:
214 explicit GrAutoValidateCache(GrLayerCache* cache)
215 : fCache(cache) {
216 fCache->validate();
217 }
218 ~GrAutoValidateCache() {
219 fCache->validate();
220 }
221private:
222 GrLayerCache* fCache;
223};
224#endif
225
robertphillips952841b2014-06-30 08:26:50 -0700226void GrLayerCache::purge(const SkPicture* picture) {
robertphillips21048b52014-07-15 19:46:35 -0700227 SkDEBUGCODE(GrAutoValidateCache avc(this);)
228
robertphillips3d533ac2014-07-20 09:40:00 -0700229 // We need to find all the layers associated with 'picture' and remove them.
robertphillips952841b2014-06-30 08:26:50 -0700230 SkTDArray<GrCachedLayer*> toBeRemoved;
231
robertphillips3d533ac2014-07-20 09:40:00 -0700232 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash);
233 for (; !iter.done(); ++iter) {
234 if (picture->uniqueID() == (*iter).pictureID()) {
235 *toBeRemoved.append() = &(*iter);
robertphillips952841b2014-06-30 08:26:50 -0700236 }
237 }
238
239 for (int i = 0; i < toBeRemoved.count(); ++i) {
240 this->unlock(toBeRemoved[i]);
robertphillips3d533ac2014-07-20 09:40:00 -0700241 fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i]));
robertphillips952841b2014-06-30 08:26:50 -0700242 SkDELETE(toBeRemoved[i]);
243 }
robertphillips261b8a92014-07-17 08:26:44 -0700244
robertphillips3d533ac2014-07-20 09:40:00 -0700245 GrPictureInfo* pictInfo = fPictureHash.find(picture->uniqueID());
robertphillips261b8a92014-07-17 08:26:44 -0700246 if (NULL != pictInfo) {
robertphillips3d533ac2014-07-20 09:40:00 -0700247 fPictureHash.remove(picture->uniqueID());
robertphillips261b8a92014-07-17 08:26:44 -0700248 SkDELETE(pictInfo);
249 }
robertphillips4ec84da2014-06-24 13:10:43 -0700250}