| 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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 const SkPaint* paint) { | 154 const SkPaint* paint) { |
| 155 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0); | 155 SkASSERT(pictureID != SK_InvalidGenID && start >= 0 && stop > 0); |
| 156 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, start,
bounds, ctm)); | 156 GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(pictureID, start,
bounds, ctm)); |
| 157 if (NULL == layer) { | 157 if (NULL == layer) { |
| 158 layer = this->createLayer(pictureID, start, stop, bounds, ctm, paint); | 158 layer = this->createLayer(pictureID, start, stop, bounds, ctm, paint); |
| 159 } | 159 } |
| 160 | 160 |
| 161 return layer; | 161 return layer; |
| 162 } | 162 } |
| 163 | 163 |
| 164 bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool do
ntAtlas) { | 164 bool GrLayerCache::tryToAtlas(GrCachedLayer* layer, |
| 165 const GrTextureDesc& desc, |
| 166 bool* needsRendering) { |
| 165 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);) | 167 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);) |
| 166 | 168 |
| 169 SkASSERT(PlausiblyAtlasable(desc.fWidth, desc.fHeight)); |
| 170 |
| 167 if (layer->locked()) { | 171 if (layer->locked()) { |
| 168 // This layer is already locked | 172 // This layer is already locked |
| 169 #ifdef SK_DEBUG | 173 SkASSERT(layer->isAtlased()); |
| 170 if (layer->isAtlased()) { | 174 SkASSERT(layer->rect().width() == desc.fWidth); |
| 171 // It claims to be atlased | 175 SkASSERT(layer->rect().height() == desc.fHeight); |
| 172 SkASSERT(!dontAtlas); | 176 *needsRendering = false; |
| 173 SkASSERT(layer->rect().width() == desc.fWidth); | 177 return true; |
| 174 SkASSERT(layer->rect().height() == desc.fHeight); | |
| 175 } | |
| 176 #endif | |
| 177 return false; | |
| 178 } | 178 } |
| 179 | 179 |
| 180 if (layer->isAtlased()) { | 180 if (layer->isAtlased()) { |
| 181 // Hooray it is still in the atlas - make sure it stays there | 181 // Hooray it is still in the atlas - make sure it stays there |
| 182 SkASSERT(!dontAtlas); | |
| 183 layer->setLocked(true); | 182 layer->setLocked(true); |
| 184 this->incPlotLock(layer->plot()->id()); | 183 this->incPlotLock(layer->plot()->id()); |
| 185 return false; | 184 *needsRendering = false; |
| 186 } else if (!dontAtlas && PlausiblyAtlasable(desc.fWidth, desc.fHeight)) { | 185 return true; |
| 186 } else { |
| 187 // Not in the atlas - will it fit? | 187 // Not in the atlas - will it fit? |
| 188 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID()); | 188 GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID()); |
| 189 if (NULL == pictInfo) { | 189 if (NULL == pictInfo) { |
| 190 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID())); | 190 pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID())); |
| 191 fPictureHash.add(pictInfo); | 191 fPictureHash.add(pictInfo); |
| 192 } | 192 } |
| 193 | 193 |
| 194 SkIPoint16 loc; | 194 SkIPoint16 loc; |
| 195 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but a
re able to purge | 195 for (int i = 0; i < 2; ++i) { // extra pass in case we fail to add but a
re able to purge |
| 196 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage, | 196 GrPlot* plot = fAtlas->addToAtlas(&pictInfo->fPlotUsage, |
| 197 desc.fWidth, desc.fHeight, | 197 desc.fWidth, desc.fHeight, |
| 198 NULL, &loc); | 198 NULL, &loc); |
| 199 // addToAtlas can allocate the backing texture | 199 // addToAtlas can allocate the backing texture |
| 200 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture())); | 200 SkDEBUGCODE(avl.setBackingTexture(fAtlas->getTexture())); |
| 201 if (plot) { | 201 if (plot) { |
| 202 // The layer was successfully added to the atlas | 202 // The layer was successfully added to the atlas |
| 203 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY, | 203 GrIRect16 bounds = GrIRect16::MakeXYWH(loc.fX, loc.fY, |
| 204 SkToS16(desc.fWidth), | 204 SkToS16(desc.fWidth), |
| 205 SkToS16(desc.fHeight)); | 205 SkToS16(desc.fHeight)); |
| 206 layer->setTexture(fAtlas->getTexture(), bounds); | 206 layer->setTexture(fAtlas->getTexture(), bounds); |
| 207 layer->setPlot(plot); | 207 layer->setPlot(plot); |
| 208 layer->setLocked(true); | 208 layer->setLocked(true); |
| 209 this->incPlotLock(layer->plot()->id()); | 209 this->incPlotLock(layer->plot()->id()); |
| 210 *needsRendering = true; |
| 210 return true; | 211 return true; |
| 211 } | 212 } |
| 212 | 213 |
| 213 // The layer was rejected by the atlas (even though we know it is | 214 // The layer was rejected by the atlas (even though we know it is |
| 214 // plausibly atlas-able). See if a plot can be purged and try again. | 215 // plausibly atlas-able). See if a plot can be purged and try again. |
| 215 if (!this->purgePlot()) { | 216 if (!this->purgePlot()) { |
| 216 break; // We weren't able to purge any plots | 217 break; // We weren't able to purge any plots |
| 217 } | 218 } |
| 218 } | 219 } |
| 219 } | 220 } |
| 220 | 221 |
| 221 // The texture wouldn't fit in the cache - give it it's own texture. | 222 return false; |
| 222 // This path always uses a new scratch texture and (thus) doesn't cache anyt
hing. | 223 } |
| 223 // This can yield a lot of re-rendering | 224 |
| 225 bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc, bool* n
eedsRendering) { |
| 226 if (layer->locked()) { |
| 227 // This layer is already locked |
| 228 *needsRendering = false; |
| 229 return true; |
| 230 } |
| 231 |
| 224 SkAutoTUnref<GrTexture> tex( | 232 SkAutoTUnref<GrTexture> tex( |
| 225 fContext->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch)); | 233 fContext->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch)); |
| 226 | 234 |
| 235 if (!tex) { |
| 236 return false; |
| 237 } |
| 238 |
| 227 layer->setTexture(tex, GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.
fHeight))); | 239 layer->setTexture(tex, GrIRect16::MakeWH(SkToS16(desc.fWidth), SkToS16(desc.
fHeight))); |
| 228 layer->setLocked(true); | 240 layer->setLocked(true); |
| 241 *needsRendering = true; |
| 229 return true; | 242 return true; |
| 230 } | 243 } |
| 231 | 244 |
| 232 void GrLayerCache::unlock(GrCachedLayer* layer) { | 245 void GrLayerCache::unlock(GrCachedLayer* layer) { |
| 233 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);) | 246 SkDEBUGCODE(GrAutoValidateLayer avl(fAtlas->getTexture(), layer);) |
| 234 | 247 |
| 235 if (NULL == layer || !layer->locked()) { | 248 if (NULL == layer || !layer->locked()) { |
| 236 // invalid or not locked | 249 // invalid or not locked |
| 237 return; | 250 return; |
| 238 } | 251 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 268 int plotLocks[kNumPlotsX * kNumPlotsY]; | 281 int plotLocks[kNumPlotsX * kNumPlotsY]; |
| 269 memset(plotLocks, 0, sizeof(plotLocks)); | 282 memset(plotLocks, 0, sizeof(plotLocks)); |
| 270 | 283 |
| 271 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHas
h); | 284 SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHas
h); |
| 272 for (; !iter.done(); ++iter) { | 285 for (; !iter.done(); ++iter) { |
| 273 const GrCachedLayer* layer = &(*iter); | 286 const GrCachedLayer* layer = &(*iter); |
| 274 | 287 |
| 275 layer->validate(fAtlas->getTexture()); | 288 layer->validate(fAtlas->getTexture()); |
| 276 | 289 |
| 277 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID()); | 290 const GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID()); |
| 278 if (pictInfo) { | 291 if (!pictInfo) { |
| 279 // In aggressive cleanup mode a picture info should only exist if | |
| 280 // it has some atlased layers | |
| 281 #if !DISABLE_CACHING | |
| 282 SkASSERT(!pictInfo->fPlotUsage.isEmpty()); | |
| 283 #endif | |
| 284 } else { | |
| 285 // If there is no picture info for this picture then all of its | 292 // If there is no picture info for this picture then all of its |
| 286 // layers should be non-atlased. | 293 // layers should be non-atlased. |
| 287 SkASSERT(!layer->isAtlased()); | 294 SkASSERT(!layer->isAtlased()); |
| 288 } | 295 } |
| 289 | 296 |
| 290 if (layer->plot()) { | 297 if (layer->plot()) { |
| 291 SkASSERT(pictInfo); | 298 SkASSERT(pictInfo); |
| 292 SkASSERT(pictInfo->fPictureID == layer->pictureID()); | 299 SkASSERT(pictInfo->fPictureID == layer->pictureID()); |
| 293 | 300 |
| 294 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot())); | 301 SkASSERT(pictInfo->fPlotUsage.contains(layer->plot())); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 continue; | 464 continue; |
| 458 } | 465 } |
| 459 | 466 |
| 460 SkString fileName(dirName); | 467 SkString fileName(dirName); |
| 461 fileName.appendf("\\%d-%d.png", layer->fKey.pictureID(), layer->fKey.sta
rt()); | 468 fileName.appendf("\\%d-%d.png", layer->fKey.pictureID(), layer->fKey.sta
rt()); |
| 462 | 469 |
| 463 layer->texture()->surfacePriv().savePixels(fileName.c_str()); | 470 layer->texture()->surfacePriv().savePixels(fileName.c_str()); |
| 464 } | 471 } |
| 465 } | 472 } |
| 466 #endif | 473 #endif |
| OLD | NEW |